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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lutao.cloud.common.core.util.MessageInfo;
import com.lutao.cloud.common.core.util.StringUtil;
import com.lutao.cloud.oil.mapper.EquipmentInfoMapper;
import com.lutao.cloud.oil.mapper.ProfEquipmentInfoMapper;
import com.lutao.cloud.oil.pojo.param.BaseParam;
import com.lutao.cloud.oil.pojo.param.EquipTypeParam;
import com.lutao.cloud.oil.pojo.vo.*;
import com.lutao.cloud.oil.service.EquipmentInfoService;
import com.lutao.cloud.oil.service.IBusinessEquipmentInfoService;
import com.lutao.cloud.oil.service.ProfEquipmentInfoService;
import com.lutao.entity.ProfEquipmentInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

@Service
@Slf4j
public class BusinessEquipmentInfoServiceImpl implements IBusinessEquipmentInfoService {

    @Resource
    private EquipmentInfoMapper equipmentInfoMapper;

    @Resource
    private ProfEquipmentInfoMapper profEquipmentInfoMapper;

    @Resource
    private EquipmentInfoService equipmentInfoService;

    @Resource
    private ProfEquipmentInfoService profEquipmentInfoService;


    @Override
    public MessageInfo<EquipmentStatisticsVO> getEquipmentNumberStatistics(BaseParam baseParam) {
        List<EquipmentNameAndNumberVO> equipmentNameAndNumberVOS = equipmentInfoMapper.selectEquipmentNameAndNumber(baseParam);
        long count = equipmentNameAndNumberVOS.stream().mapToLong(EquipmentNameAndNumberVO::getEquipmentNumber).count();
        EquipmentStatisticsVO equipmentStatisticsVO = new EquipmentStatisticsVO()
                .setEquipmentCount(count)
                .setEquipmentNameAndNumberVOS(equipmentNameAndNumberVOS);
        return MessageInfo.ok(equipmentStatisticsVO);
    }

    @Override
    public MessageInfo<EquipmentTypeStatisticsVO> getEquipmentTypeStatistics(Page page, EquipTypeParam param) {
        IPage<EquipmentTypeVO> equipmentTypeVOIPage = equipmentInfoMapper.selectEquipTypePage(page, param);

        //合计
        int count = profEquipmentInfoService.count(Wrappers.lambdaQuery(ProfEquipmentInfo.class)
                .eq(StringUtil.isNotBlank(param.getAppId()), ProfEquipmentInfo::getAppId, param.getAppId())
                .eq(StringUtil.isNotBlank(param.getPlatformId()), ProfEquipmentInfo::getPlatformCode, param.getPlatformId()));

        Integer distinctEquipTypeCount = equipmentInfoMapper.getDistinctEquipTypeCount(param);

        EquipmentTypeStatisticsVO equipmentTypeStatisticsVO = new EquipmentTypeStatisticsVO()
                .setAmount(distinctEquipTypeCount + "类" + " " + count + "个")
                .setEquipmentTypeVOIPage(equipmentTypeVOIPage);

        return MessageInfo.ok(equipmentTypeStatisticsVO);
    }

    @Override
    public MessageInfo<EquipmentInspectStatisticsVO> getEquipmentInspectStatistics(BaseParam param) {
        //近三个月需要年检数量
        LocalDate currentTime = LocalDate.now();
        LocalDate threeMonthLater = currentTime.plusMonths(3);

        Long recentThreeMonthYearInspectNumber = equipmentInfoMapper.getRecentThreeMonthYearInspectNumber(param);

        //已年检数量
        int inspectAnnuallyNumber = profEquipmentInfoService.count(Wrappers.lambdaQuery(ProfEquipmentInfo.class)
                .like(ProfEquipmentInfo::getInspectionType, "2")
                .ge(ProfEquipmentInfo::getNextYearInspectionDate, currentTime)
                .eq(StringUtil.isNotBlank(param.getPlatformId()), ProfEquipmentInfo::getPlatformCode, param.getPlatformId())
                .eq(StringUtil.isNotBlank(param.getAppId()), ProfEquipmentInfo::getAppId, param.getAppId())
        );


        //即将过期年检数量
        Long willExpireYearInspectNumber = equipmentInfoMapper.getWillExpireYearInspectNumber(param);

        //已经过期年检数量
        Long expireYearInspectNumber = equipmentInfoMapper.getExpireYearInspectNumber(param);

        //近三个月需定检数量
        Long recentThreeMonthPeriodicalInspectNumber = equipmentInfoMapper.getRecentThreeMonthPeriodicalInspectNumber(param);

        //已定检数量
        int periodicalInspection = profEquipmentInfoService.count(Wrappers.lambdaQuery(ProfEquipmentInfo.class)
                .like(ProfEquipmentInfo::getInspectionType, "定")
                .ge(ProfEquipmentInfo::getNextPeriodicInspectionDate, currentTime)
                .eq(StringUtil.isNotBlank(param.getPlatformId()), ProfEquipmentInfo::getPlatformCode, param.getPlatformId())
                .eq(StringUtil.isNotBlank(param.getAppId()), ProfEquipmentInfo::getAppId, param.getAppId()));

        //即将过期定检数量
        Long willExpirePeriodicalInspect = equipmentInfoMapper.getWillExpirePeriodicalInspect(param);

        //已经过期定检数量
        Long expirePeriodicalInspectNumber = equipmentInfoMapper.getExpirePeriodicalInspectNumber(param);


        EquipmentInspectStatisticsVO equipmentInspectStatisticsVO = new EquipmentInspectStatisticsVO()
                .setThreeMonthInspectAnnuallyNumber(recentThreeMonthYearInspectNumber)
                .setInspectAnnuallyNumber((long) inspectAnnuallyNumber)
                .setThreeMonthPeriodicalInspection(recentThreeMonthPeriodicalInspectNumber)
                .setPeriodicalInspection((long) periodicalInspection)
                .setWillExpireYearInspectNumber(willExpireYearInspectNumber)
                .setWillExpirePeriodicalInspect(willExpirePeriodicalInspect)
                .setExpireYearInspectNumber(expireYearInspectNumber)
                .setExpirePeriodicalInspectNumber(expirePeriodicalInspectNumber);
        return MessageInfo.ok(equipmentInspectStatisticsVO);
    }

    @Override
    public MessageInfo<List<YearInspectionDistributionVO>> getEquipmentAnnuallyInspectDistribution(BaseParam baseParam) {
        List<YearInspectionDistributionVO> equipmentAnnuallyInspectDistribution = equipmentInfoMapper.getEquipmentAnnuallyInspectDistribution(baseParam);
        return MessageInfo.ok(equipmentAnnuallyInspectDistribution);
    }

    @Override
    public MessageInfo<List<YearInspectionDistributionVO>> getEquipmentPeriodicalInspectDistribution(BaseParam baseParam) {
        List<YearInspectionDistributionVO> equipmentPeriodicalInspectDistribution = equipmentInfoMapper.getEquipmentPeriodicalInspectDistribution(baseParam);
        return MessageInfo.ok(equipmentPeriodicalInspectDistribution);
    }

    @Override
    public MessageInfo<ScreenDisplayEquipmentVO> queryScreenDisplayEquipment(Page page, BaseParam param) {

        //即将过期年检数量
        Long willExpireYearInspectNumber = equipmentInfoMapper.getWillExpireYearInspectNumber(param);

        //已经过期年检数量
        Long expireYearInspectNumber = equipmentInfoMapper.getExpireYearInspectNumber(param);

        //即将过期定检数量
        Long willExpirePeriodicalInspect = equipmentInfoMapper.getWillExpirePeriodicalInspect(param);

        //已经过期定检数量
        Long expirePeriodicalInspectNumber = equipmentInfoMapper.getExpirePeriodicalInspectNumber(param);

        //总数
        int equipmentCount = profEquipmentInfoService.count();

        //设备和数量list
        IPage<ScreenDisplayEquipmentNameAndNumber> screenEquipmentNameAndNumber = equipmentInfoMapper.getScreenEquipmentNameAndNumber(page, param);


        return MessageInfo.ok(new ScreenDisplayEquipmentVO().setEquipmentCount((long) equipmentCount)
                .setExpiredYearInspectNumber(expireYearInspectNumber)
                .setSoonToExpireYearInspectNumber(willExpireYearInspectNumber)
                .setEquipmentNameAndNumberVOList(screenEquipmentNameAndNumber)
                .setSoonToExpirePeriodicalInspectionNumber(willExpirePeriodicalInspect)
                .setExpiredPeriodicalInspectionNumber(expirePeriodicalInspectNumber));
    }

    @Override
    public MessageInfo<List<EquipTypeNameVO>> getEquipmentTypeNameList() {
        return MessageInfo.ok(equipmentInfoMapper.selectEquipmentTypeNameList());
    }

    @Override
    public MessageInfo<List<EquipmentTypePieVO>> getEquipmentTypePieChart(BaseParam param) {
        return MessageInfo.ok(profEquipmentInfoMapper.getEquipmentTypePie(param));
    }

    @Override
    public MessageInfo<List<EquipTypeInspectVO>> getEquipmentTypeYearInspect(BaseParam param) {
        return MessageInfo.ok(profEquipmentInfoMapper.getEquipTypeInpectList(param, 1));
    }

    @Override
    public MessageInfo<List<EquipTypeInspectVO>> getEquipmentTypePeriodicalInspect(BaseParam param) {
        return MessageInfo.ok(profEquipmentInfoMapper.getEquipTypeInpectList(param, 2));
    }

    @Override
    public MessageInfo<ScreenDisplayEquipmentBarVO> queryScreenDisplayEquipmentBar(BaseParam param) {

        List<ProfEquipmentInfo> profEquipmentInfos = profEquipmentInfoService.list();

        //中海油设备总数
        long zhongHaiYouEquipmentCount = profEquipmentInfos.stream().filter(i->i.getAppId() != null).filter(i -> i.getAppId().equals("zhonghaiyou")).count();

        //中海油即将过期数
        long zhongHaiYouWillExpireCount = equipmentInfoMapper.getWillExpireNumber(param.setAppId("zhonghaiyou"));

        //中石油设备总数
        long zhongShiYouEquipmentCount = profEquipmentInfos.stream().filter(i->i.getAppId() != null).filter(i -> i.getAppId().equals("zhongshiyou")).count();

        //中石油即将过期数
        long zhongShiYouWillExpireCount = equipmentInfoMapper.getWillExpireNumber(param.setAppId("zhongshiyou"));

        //中石化设备总数
        long zhongShiHuaEquipmentCount = profEquipmentInfos.stream().filter(i->i.getAppId() != null).filter(i -> i.getAppId().equals("zhongshihua")).count();

        //中石化即将过期数
        long zhongShiHuaWillExpireCount = equipmentInfoMapper.getWillExpireNumber(param.setAppId("zhongshihua"));

        ScreenDisplayEquipmentBarVO screenDisplayEquipmentBarVO = new ScreenDisplayEquipmentBarVO()
                .setZhongHaiYouEquipmentCount(zhongHaiYouEquipmentCount)
                .setZhongHaiYouWillExpireCount(zhongHaiYouWillExpireCount)
                .setZhongShiYouEquipmentCount(zhongShiYouEquipmentCount)
                .setZhongShiYouWillExpireCount(zhongShiYouWillExpireCount)
                .setZhongShiHuaEquipmentCount(zhongShiHuaEquipmentCount)
                .setZhongShiHuaWillExpireCount(zhongShiHuaWillExpireCount);

        return MessageInfo.ok(screenDisplayEquipmentBarVO);
    }


}
