
package com.lutao.cloud.oil.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lutao.cloud.common.core.util.MessageInfo;
import com.lutao.cloud.common.core.util.StringUtil;
import com.lutao.cloud.oil.mapper.AppInfoMapper;
import com.lutao.cloud.oil.mapper.AtmosphereMonitorInfoMapper;
import com.lutao.cloud.oil.mapper.PlatformInfoMapper;
import com.lutao.cloud.oil.pojo.param.BaseParam;
import com.lutao.cloud.oil.pojo.vo.*;
import com.lutao.cloud.oil.service.PlatformInfoService;
import com.lutao.entity.AtmosphereMonitorInfo;
import com.lutao.entity.PlatformInfo;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;

/**
 * 平台详情
 *
 * @author guoyc
 * @date 2023-07-20 10:56:48
 */
@Service
public class PlatformInfoServiceImpl extends ServiceImpl<PlatformInfoMapper, PlatformInfo> implements PlatformInfoService {

    @Resource
    private PlatformInfoMapper platformInfoMapper;
    @Resource
    private AtmosphereMonitorInfoMapper atmosphereMonitorInfoMapper;
    @Resource
    private AppInfoMapper appInfoMapper;

    @Override
    public MessageInfo<SeaPlatformOverallSituationVO> querySeaPlatformOverallSituation(BaseParam param) {
        //平台设施总数
        int platformCount = this.count(Wrappers.lambdaQuery(PlatformInfo.class)
                .eq(StringUtil.isNotBlank(param.getAppId()), PlatformInfo::getAppId, param.getAppId())
                .eq(StringUtil.isNotBlank(param.getPlatformId()), PlatformInfo::getPlatformCode, param.getPlatformId()));


         //平台类型 1固定平台 2FPSO 3钻井平台 4人工岛

        //固定平台
        int seaPlatformNumber = this.count(Wrappers.lambdaQuery(PlatformInfo.class)
                .eq(PlatformInfo::getPlatformType, 1)
                .eq(StringUtil.isNotBlank(param.getAppId()), PlatformInfo::getAppId, param.getAppId())
                .eq(StringUtil.isNotBlank(param.getPlatformId()), PlatformInfo::getPlatformCode, param.getPlatformId()));

        //2 FPSO
        int FPSONumber = this.count(Wrappers.lambdaQuery(PlatformInfo.class)
                .eq(PlatformInfo::getPlatformType, 2)
                .eq(StringUtil.isNotBlank(param.getAppId()), PlatformInfo::getAppId, param.getAppId())
                .eq(StringUtil.isNotBlank(param.getPlatformId()), PlatformInfo::getPlatformCode, param.getPlatformId()));

        //3钻井平台
        int movePlatformNumber = this.count(Wrappers.lambdaQuery(PlatformInfo.class)
                .eq(PlatformInfo::getPlatformType, 3)
                .eq(StringUtil.isNotBlank(param.getAppId()), PlatformInfo::getAppId, param.getAppId())
                .eq(StringUtil.isNotBlank(param.getPlatformId()), PlatformInfo::getPlatformCode, param.getPlatformId()));

        //4人工岛
        int artificialIslandNumber = this.count(Wrappers.lambdaQuery(PlatformInfo.class)
                .eq(PlatformInfo::getPlatformType, 4)
                .eq(StringUtil.isNotBlank(param.getAppId()), PlatformInfo::getAppId, param.getAppId())
                .eq(StringUtil.isNotBlank(param.getPlatformId()), PlatformInfo::getPlatformCode, param.getPlatformId()));

        List<AppPlatformNumberVO> appPlatformNumberVOS = appInfoMapper.appPlatformNumber();

        SeaPlatformOverallSituationVO seaPlatformOverallSituationVO = new SeaPlatformOverallSituationVO().setPlatformCount(platformCount)
                .setSeaPlatformNumber(seaPlatformNumber)
                .setArtificialIslandNumber(artificialIslandNumber)
                .setMovePlatformNumber(movePlatformNumber)
                .setAppPlatformNumberVOList(appPlatformNumberVOS)
                .setFPSONumber(FPSONumber);
        return MessageInfo.ok(seaPlatformOverallSituationVO);
    }

    @Override
    public MessageInfo<IPage<MapDisplayVO>> queryMapDisplay(Page page, BaseParam param) {
        IPage<MapDisplayVO> mapDisplayVOIPage = new Page<>();
        mapDisplayVOIPage.setCurrent(1);
        mapDisplayVOIPage.setPages(1);
        mapDisplayVOIPage.setTotal(0);
        List<MapDisplayVO> records = Lists.newArrayList();
        mapDisplayVOIPage.setRecords(records);

        List<PlatformInfo> platformInfos = platformInfoMapper.selectList(new LambdaQueryWrapper<>());
        if(CollUtil.isNotEmpty(platformInfos)) {
            mapDisplayVOIPage.setTotal(platformInfos.size());
            platformInfos.forEach(platformInfo -> {
                MapDisplayVO vo = new MapDisplayVO();
                records.add(vo);

                AtmosphereMonitorInfo atmosphereMonitorInfo = atmosphereMonitorInfoMapper.selectOne(
                        new LambdaQueryWrapper<AtmosphereMonitorInfo>()
                        .eq(AtmosphereMonitorInfo::getPlatformCode, platformInfo.getPlatformCode())
                        .orderByDesc(AtmosphereMonitorInfo::getCreateTime).last(" limit 1"));

                BeanUtil.copyProperties(atmosphereMonitorInfo, vo);
                BeanUtil.copyProperties(platformInfo, vo);
            });
        }

        records.sort((o1, o2) -> (StringUtils.isNotBlank(o2.getTemp()) ? o2.getTemp() : "") .compareTo((StringUtils.isNotBlank(o1.getTemp()) ? o1.getTemp() : "")));

        return MessageInfo.ok(mapDisplayVOIPage);
    }

    @Override
    public MessageInfo<List<PlatformInfoCountVO>> queryPlatformNumber(BaseParam param) {
        return MessageInfo.ok(platformInfoMapper.selectPlatformInfoCount(param));
    }
}
