package com.njcn.process.service.impl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import com.njcn.device.biz.commApi.CommTerminalGeneralClient;
import com.njcn.device.biz.pojo.dto.DeptGetBase;
import com.njcn.device.biz.pojo.param.DeptGetLineParam;
import com.njcn.process.mapper.SupvReportMMapper;
import com.njcn.process.pojo.dto.ProcessPublicDTO;
import com.njcn.process.pojo.po.SupvAlarm;
import com.njcn.process.pojo.po.SupvAlarmBack;
import com.njcn.process.pojo.po.SupvReportM;
import com.njcn.process.service.ISupvAlarmBackService;
import com.njcn.process.service.ISupvAlarmService;
import com.njcn.process.service.ISupvReportMService;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.enums.DicDataEnum;
import com.njcn.system.enums.DicDataTypeEnum;
import com.njcn.system.pojo.po.DictData;
import com.njcn.user.api.DeptFeignClient;
import com.njcn.user.pojo.po.Dept;
import com.njcn.user.pojo.vo.PvTerminalTreeVO;
import com.njcn.web.factory.PageFactory;
import com.njcn.web.pojo.param.BaseParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hongawen
 * @since 2023-06-26
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class SupvReportMServiceImpl extends MppServiceImpl<SupvReportMMapper, SupvReportM> implements ISupvReportMService {

    private final CommTerminalGeneralClient commTerminalGeneralClient;

    private final DeptFeignClient deptFeignClient;

    private final DicDataFeignClient dicDataFeignClient;
    private final ISupvAlarmService supvAlarmService;
    private final ISupvAlarmBackService supvAlarmBackService;

    @Override
    @Async("asyncExecutor")
    public void statisticSuperviseReport(LocalDate timeId) {
        log.info(LocalDateTime.now()+"_________________________开始执行");
        TimeInterval timeInterval = new TimeInterval();

        //当月第一天
        LocalDateTime firstDay = timeId.with(TemporalAdjusters.firstDayOfMonth()).atTime(0,0,0);

        //当年第一天
        LocalDateTime firstYearDay = timeId.with(TemporalAdjusters.firstDayOfYear()).atTime(0,0,0);

        //年度最后一天
        LocalDateTime endYearDay = timeId.with(TemporalAdjusters.lastDayOfYear()).atTime(23,59,59);

        //当前统计时间(当月最后一天)
        LocalDateTime endTime = timeId.with(TemporalAdjusters.lastDayOfMonth()).atTime(23,59,59);

        //统计时间的上一个月
        LocalDateTime lastEndTime = endTime.minusMonths(1L).with(TemporalAdjusters.lastDayOfMonth());

        List<DictData> dictDataUhv = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.SUPV_TYPE.getCode()).getData();
        Map<String,DictData> mapStatistic = dictDataUhv.stream().collect(Collectors.toMap(DictData::getCode, Function.identity()));
        DeptGetLineParam deptGetLineParam = new DeptGetLineParam();
        deptGetLineParam.setDeptId(deptFeignClient.getRootDept().getData().getId());
        List<DeptGetBase> deptGetBaseList = commTerminalGeneralClient.getDeptChildrenByParent(deptGetLineParam).getData();

        List<PvTerminalTreeVO> deptList = deptFeignClient.allDeptList().getData();
        Map<String,PvTerminalTreeVO> mapCode = deptList.stream().collect(Collectors.toMap(PvTerminalTreeVO::getCode, Function.identity()));
        Map<String,PvTerminalTreeVO> mapList = deptList.stream().collect(Collectors.toMap(PvTerminalTreeVO::getId, Function.identity()));

        //特高压换流站月报监督数量
        List<ProcessPublicDTO> processPublicDTOListM = this.baseMapper.statisticPlanReport(firstDay,endTime,mapStatistic.get(DicDataEnum.UHV_Converter.getCode()).getId(),null,null);
        List<ProcessPublicDTO> processPublicDTOListY = this.baseMapper.statisticPlanReportYear(firstYearDay,endYearDay,mapStatistic.get(DicDataEnum.UHV_Converter.getCode()).getId(),null,null);
        List<ProcessPublicDTO> processPublicDTOListAll = this.baseMapper.statisticPlanReport(firstYearDay,endTime,mapStatistic.get(DicDataEnum.UHV_Converter.getCode()).getId(),null, null);

        //新能源场站(在运站)月报监督数量
        List<ProcessPublicDTO> processPublicDTOListNewM = this.baseMapper.statisticPlanReport(firstDay,endTime,mapStatistic.get(DicDataEnum.New_Energy.getCode()).getId(),"01",null);
        List<ProcessPublicDTO> processPublicDTOListNewY = this.baseMapper.statisticPlanReportYear(firstYearDay,endYearDay,mapStatistic.get(DicDataEnum.New_Energy.getCode()).getId(),"01",null);
        List<ProcessPublicDTO> processPublicDTOListNewAll = this.baseMapper.statisticPlanReport(firstYearDay,endTime,mapStatistic.get(DicDataEnum.New_Energy.getCode()).getId(),"01",null);

        //新能源场站(改扩建)月报监督新数量
        List<ProcessPublicDTO> processPublicDTOListNewZM = this.baseMapper.statisticPlanReport(firstDay,endTime,mapStatistic.get(DicDataEnum.New_Energy.getCode()).getId(),"02",null);
        List<ProcessPublicDTO> processPublicDTOListNewZY = this.baseMapper.statisticPlanReportYear(firstYearDay,endYearDay,mapStatistic.get(DicDataEnum.New_Energy.getCode()).getId(),"02",null);
        List<ProcessPublicDTO> processPublicDTOListNewZAll = this.baseMapper.statisticPlanReport(firstYearDay,endTime,mapStatistic.get(DicDataEnum.New_Energy.getCode()).getId(),"02",null);

        //敏感用户月报监督数量
        List<ProcessPublicDTO> processPublicDTOMingGanM = this.baseMapper.statisticPlanReport(firstDay,endTime,mapStatistic.get(DicDataEnum.POWER_QUALITY.getCode()).getId(),null,null);
        List<ProcessPublicDTO> processPublicDTOMingGanY = this.baseMapper.statisticPlanReportYear(firstYearDay,endYearDay,mapStatistic.get(DicDataEnum.POWER_QUALITY.getCode()).getId(),null,null);
        List<ProcessPublicDTO> processPublicDTOMingGanAll = this.baseMapper.statisticPlanReport(firstYearDay,endTime,mapStatistic.get(DicDataEnum.POWER_QUALITY.getCode()).getId(),null,null);




        //换流站问题总数量
        List<ProcessPublicDTO> processPublicDTOQesM = this.baseMapper.statisticQueReport(firstDay,endTime,mapStatistic.get(DicDataEnum.UHV_Converter.getCode()).getId(),null,null);
        List<ProcessPublicDTO> processPublicDTOQesAll = this.baseMapper.statisticQueReport(firstYearDay,endTime,mapStatistic.get(DicDataEnum.UHV_Converter.getCode()).getId(),null,null);

        //换流站问题已整改数量
        List<ProcessPublicDTO> processPublicDTOQesYesM = this.baseMapper.statisticQueReportRectify(firstDay,endTime,mapStatistic.get(DicDataEnum.UHV_Converter.getCode()).getId(),"01",null);
        List<ProcessPublicDTO> processPublicDTOQesYesAll = this.baseMapper.statisticQueReportRectify(firstYearDay,endTime,mapStatistic.get(DicDataEnum.UHV_Converter.getCode()).getId(),"01",null);

        //新能源(在运站)问题总数量
        List<ProcessPublicDTO> processPublicDTOQesNewM = this.baseMapper.statisticQueReport(firstDay,endTime,mapStatistic.get(DicDataEnum.New_Energy.getCode()).getId(),null,"01");
        List<ProcessPublicDTO> processPublicDTOQesNewAll = this.baseMapper.statisticQueReport(firstYearDay,endTime,mapStatistic.get(DicDataEnum.New_Energy.getCode()).getId(),null,"01");

        //新能源(在运站)问题已整改数量
        List<ProcessPublicDTO> processPublicDTOQesNewYesM = this.baseMapper.statisticQueReportRectify(firstDay,endTime,mapStatistic.get(DicDataEnum.New_Energy.getCode()).getId(),"02","01");
        List<ProcessPublicDTO> processPublicDTOQesNewYesAll = this.baseMapper.statisticQueReportRectify(firstYearDay,endTime,mapStatistic.get(DicDataEnum.New_Energy.getCode()).getId(),"02","01");

        //新能源(改扩建)问题数量
        List<ProcessPublicDTO> processPublicDTOQesNewGaiM = this.baseMapper.statisticQueReport(firstDay,endTime,mapStatistic.get(DicDataEnum.New_Energy.getCode()).getId(),null,"02");
        List<ProcessPublicDTO> processPublicDTOQesNewGaiAll = this.baseMapper.statisticQueReport(firstYearDay,endTime,mapStatistic.get(DicDataEnum.New_Energy.getCode()).getId(),null,"02");

        //新能源(改扩建)整改问题数量
        List<ProcessPublicDTO> processPublicDTOQesNewGaiYesM = this.baseMapper.statisticQueReportRectify(firstDay,endTime,mapStatistic.get(DicDataEnum.New_Energy.getCode()).getId(),"02","02");
        List<ProcessPublicDTO> processPublicDTOQesNewGaiYesAll = this.baseMapper.statisticQueReportRectify(firstYearDay,endTime,mapStatistic.get(DicDataEnum.New_Energy.getCode()).getId(),"02","02");

        //敏感用户问题总数量
        List<ProcessPublicDTO> processPublicDTOQesMingGanM = this.baseMapper.statisticQueReport(firstDay,endTime,mapStatistic.get(DicDataEnum.POWER_QUALITY.getCode()).getId(),null,null);
        List<ProcessPublicDTO> processPublicDTOQesMingGanAll = this.baseMapper.statisticQueReport(firstYearDay,endTime,mapStatistic.get(DicDataEnum.POWER_QUALITY.getCode()).getId(),null,null);

        //敏感用户问题已整改数量
        List<ProcessPublicDTO> processPublicDTOQesMingGanYesM = this.baseMapper.statisticQueReportRectify(firstDay,endTime,mapStatistic.get(DicDataEnum.POWER_QUALITY.getCode()).getId(),"02",null);
        List<ProcessPublicDTO> processPublicDTOQesMingGanYesAll = this.baseMapper.statisticQueReportRectify(firstYearDay,endTime,mapStatistic.get(DicDataEnum.POWER_QUALITY.getCode()).getId(),"02",null);

        //供电电压本月前统计
        List<ProcessPublicDTO> processPublicDTODianYaListM = this.baseMapper.statisticPlanReport(firstYearDay,endTime,mapStatistic.get(DicDataEnum.Technical_Super.getCode()).getId(),null,null);
        List<ProcessPublicDTO> processPublicDTODianYaListAll = this.baseMapper.statisticPlanReport(firstYearDay,endTime,mapStatistic.get(DicDataEnum.Technical_Super.getCode()).getId(),null,null);
        List<ProcessPublicDTO> processPublicDTODianYaYearPlan = this.baseMapper.statisticPlanReportYear(firstYearDay,endYearDay,mapStatistic.get(DicDataEnum.Technical_Super.getCode()).getId(),null,null);


        //供电电压问题数量
        List<ProcessPublicDTO> processPublicDTODianYaGanM = this.baseMapper.statisticQueReport(firstDay,endTime,mapStatistic.get(DicDataEnum.Technical_Super.getCode()).getId(),null,null);
        List<ProcessPublicDTO> processPublicDTODianYaGanAll = this.baseMapper.statisticQueReport(firstYearDay,endTime,mapStatistic.get(DicDataEnum.Technical_Super.getCode()).getId(),null,null);
        //供电电压已整改问题
        List<ProcessPublicDTO> processPublicDTODianYaGanYesM = this.baseMapper.statisticQueReportRectify(firstDay,endTime,mapStatistic.get(DicDataEnum.Technical_Super.getCode()).getId(),"02",null);
        List<ProcessPublicDTO> processPublicDTODianYaGanYesAll = this.baseMapper.statisticQueReportRectify(firstYearDay,endTime,mapStatistic.get(DicDataEnum.Technical_Super.getCode()).getId(),"02",null);


        //电容器组月前统计
        List<ProcessPublicDTO> processPublicYearDTOS = this.baseMapper.statisticPlanReportYear(firstYearDay, endYearDay, mapStatistic.get(DicDataEnum.capacitor_bank.getCode()).getId(), null, null);
        List<ProcessPublicDTO> processPublicDTOCapacitorListM = this.baseMapper.statisticPlanReport(firstDay,endTime,mapStatistic.get(DicDataEnum.capacitor_bank.getCode()).getId(),null,null);
        List<ProcessPublicDTO> processPublicDTOCapacitorAll = this.baseMapper.statisticPlanReport(firstYearDay,endTime,mapStatistic.get(DicDataEnum.capacitor_bank.getCode()).getId(),null,null);
        //电容器组问题数量
        List<ProcessPublicDTO> processPublicDTOCapacitorGanM = this.baseMapper.statisticQueReport(firstDay,endTime,mapStatistic.get(DicDataEnum.capacitor_bank.getCode()).getId(),null,null);
        List<ProcessPublicDTO> processPublicDTOCapacitorGanAll = this.baseMapper.statisticQueReport(firstYearDay,endTime,mapStatistic.get(DicDataEnum.capacitor_bank.getCode()).getId(),null,null);
        //电容器组已整改问题
        List<ProcessPublicDTO> processPublicDTOCapacitorGanYesM = this.baseMapper.statisticQueReportRectify(firstDay,endTime,mapStatistic.get(DicDataEnum.capacitor_bank.getCode()).getId(),"02",null);
        List<ProcessPublicDTO> processPublicDTOCapacitorYesAll = this.baseMapper.statisticQueReportRectify(firstYearDay,endTime,mapStatistic.get(DicDataEnum.capacitor_bank.getCode()).getId(),"02",null);

        //评估报告月前统计
        List<ProcessPublicDTO> processPublicYearDTOAppraisalS = this.baseMapper.statisticPlanReportYear(firstYearDay, endYearDay, mapStatistic.get(DicDataEnum.report_supervision.getCode()).getId(), null, null);
        List<ProcessPublicDTO> processPublicDTOAppraisalListM = this.baseMapper.statisticPlanReport(firstDay,endTime,mapStatistic.get(DicDataEnum.report_supervision.getCode()).getId(),null,null);
        List<ProcessPublicDTO> processPublicDTOAppraisalAll = this.baseMapper.statisticPlanReport(firstYearDay,endTime,mapStatistic.get(DicDataEnum.report_supervision.getCode()).getId(),null,null);
        //评估报告问题数量
        List<ProcessPublicDTO> processPublicDTOAppraisalGanM = this.baseMapper.statisticQueReport(firstDay,endTime,mapStatistic.get(DicDataEnum.report_supervision.getCode()).getId(),null,null);
        List<ProcessPublicDTO> processPublicDTOAppraisalGanAll = this.baseMapper.statisticQueReport(firstYearDay,endTime,mapStatistic.get(DicDataEnum.report_supervision.getCode()).getId(),null,null);
        //评估报告已整改问题
        List<ProcessPublicDTO> processPublicDTOAppraisalGanYesM = this.baseMapper.statisticQueReportRectify(firstDay,endTime,mapStatistic.get(DicDataEnum.report_supervision.getCode()).getId(),"02",null);
        List<ProcessPublicDTO> processPublicDTOAppraisalYesAll = this.baseMapper.statisticQueReportRectify(firstYearDay,endTime,mapStatistic.get(DicDataEnum.report_supervision.getCode()).getId(),"02",null);



        List<SupvReportM> supvReportMBatch = new ArrayList<>();
        Integer dyProvinceCount = 0;
        String provinceId ="";

        for(DeptGetBase deptGetBase : deptGetBaseList){

            if(deptGetBase.getDeptLevel() !=2 && deptGetBase.getDeptLevel() !=3 && deptGetBase.getDeptLevel() !=4){
                continue;
            }

            if(deptGetBase.getDeptLevel() ==2){
                provinceId = deptGetBase.getUnitId();
            }
            //月累计监督数量
            Integer all = 0;
            Integer yearAll = 0;
            SupvReportM supvReportM = new SupvReportM();
            supvReportM.setStatisticsDate(timeId.with(TemporalAdjusters.firstDayOfMonth()));
            supvReportM.setStatisticsDept(deptGetBase.getUnitId());

            List<String> childrenDeptList = deptGetBase.getUnitChildrenList();
            if(CollUtil.isNotEmpty(childrenDeptList)){
                supvReportM.setConvertMonthSupvNum(dealData(childrenDeptList,processPublicDTOListM));


                supvReportM.setConvertYearSupvNum(dealData(childrenDeptList,processPublicDTOListY));
                yearAll+=supvReportM.getConvertYearSupvNum();

                supvReportM.setConvertTotalSupvNum(dealData(childrenDeptList,processPublicDTOListAll));
                all+=supvReportM.getConvertTotalSupvNum();
                supvReportM.setEnergyMonthSupvNum(dealData(childrenDeptList,processPublicDTOListNewM));


                supvReportM.setEnergyYearSupvNum(dealData(childrenDeptList,processPublicDTOListNewY));
                yearAll+=supvReportM.getEnergyYearSupvNum();


                supvReportM.setEnergyTotalSupvNum(dealData(childrenDeptList,processPublicDTOListNewAll));
                all+=supvReportM.getEnergyTotalSupvNum();


                supvReportM.setEnergyMonthSupvNewNum(dealData(childrenDeptList,processPublicDTOListNewZM));
                supvReportM.setEnergyYearSupvNewNum(dealData(childrenDeptList,processPublicDTOListNewZY));
                yearAll+=supvReportM.getEnergyYearSupvNewNum();
                supvReportM.setEnergyTotalSupvNewNum(dealData(childrenDeptList,processPublicDTOListNewZAll));
                supvReportM.setConvertMonthQuesNum(dealData(childrenDeptList,processPublicDTOQesM));
                supvReportM.setConvertTotalQuesNum(dealData(childrenDeptList,processPublicDTOQesAll));
                supvReportM.setConvertMonthReformNum(dealData(childrenDeptList,processPublicDTOQesYesM));
                supvReportM.setConvertTotalReformNum(dealData(childrenDeptList,processPublicDTOQesYesAll));

                supvReportM.setEnergyMonthQuesNum(dealData(childrenDeptList,processPublicDTOQesNewM));
                supvReportM.setEnergyTotalQuesNum(dealData(childrenDeptList,processPublicDTOQesNewAll));
                supvReportM.setEnergyMonthReformNum(dealData(childrenDeptList,processPublicDTOQesNewYesM));
                supvReportM.setEnergyTotalReformNum(dealData(childrenDeptList,processPublicDTOQesNewYesAll));
                supvReportM.setEnergyMonthQuesNewNum(dealData(childrenDeptList,processPublicDTOQesNewGaiM));
                supvReportM.setEnergyTotalQuesNewNum(dealData(childrenDeptList,processPublicDTOQesNewGaiAll));
                supvReportM.setEnergyMonthReformNewNum(dealData(childrenDeptList,processPublicDTOQesNewGaiYesM));
                supvReportM.setEnergyTotalReformNewNum(dealData(childrenDeptList,processPublicDTOQesNewGaiYesAll));

                //敏感用户
                supvReportM.setSensitiveMonthSupvNum(dealData(childrenDeptList,processPublicDTOMingGanM));
                supvReportM.setSensitiveYearSupvNum(dealData(childrenDeptList,processPublicDTOMingGanY));
                yearAll+=supvReportM.getSensitiveYearSupvNum();
                supvReportM.setSensitiveTotalSupvNum(dealData(childrenDeptList,processPublicDTOMingGanAll));
                all+=supvReportM.getSensitiveTotalSupvNum();
                supvReportM.setSensitiveMonthQuesNum(dealData(childrenDeptList,processPublicDTOQesMingGanM));
                supvReportM.setSensitiveTotalQuesNum(dealData(childrenDeptList,processPublicDTOQesMingGanAll));
                supvReportM.setSensitiveMonthReformNum(dealData(childrenDeptList,processPublicDTOQesMingGanYesM));
                supvReportM.setSensitiveTotalReformNum(dealData(childrenDeptList,processPublicDTOQesMingGanYesAll));

                //电压
                Integer dyMCount = dealData(childrenDeptList,processPublicDTODianYaListM);
                Integer dyYCount = dealData(childrenDeptList,processPublicDTODianYaListAll);
                supvReportM.setPowerMonthPlanNum(dyMCount>0?1:0);
                supvReportM.setPowerMonthConductedNum(dyYCount>0?1:0);
                if(deptGetBase.getDeptLevel() == 3){
                    dyProvinceCount+=supvReportM.getPowerMonthPlanNum();
                }


                all+=supvReportM.getPowerMonthConductedNum();
                Integer dd = dealData(childrenDeptList,processPublicDTODianYaYearPlan);
                yearAll+=dd;
                supvReportM.setPowerMonthQuesNum(dealData(childrenDeptList,processPublicDTODianYaGanM));
                supvReportM.setPowerTotalQuesNum(dealData(childrenDeptList,processPublicDTODianYaGanAll));
                supvReportM.setPowerMonthReformNum(dealData(childrenDeptList,processPublicDTODianYaGanYesM));
                supvReportM.setPowerTotalReformNum(dealData(childrenDeptList,processPublicDTODianYaGanYesAll));


                //电容器组
                supvReportM.setCapacitorYearSupvNum(dealData(childrenDeptList,processPublicYearDTOS));
                supvReportM.setCapacitorMonthSupvNum(dealData(childrenDeptList,processPublicDTOCapacitorListM));
                supvReportM.setCapacitorTotalSupvNum(dealData(childrenDeptList,processPublicDTOCapacitorAll));
                supvReportM.setCapacitorMonthQuesNum(dealData(childrenDeptList,processPublicDTOCapacitorGanM));
                supvReportM.setCapacitorTotalQuesNum(dealData(childrenDeptList,processPublicDTOCapacitorGanAll));
                supvReportM.setCapacitorMonthReformNum(dealData(childrenDeptList,processPublicDTOCapacitorGanYesM));
                supvReportM.setCapacitorTotalReformNum(dealData(childrenDeptList,processPublicDTOCapacitorYesAll));
                all+=supvReportM.getCapacitorTotalSupvNum();
                yearAll+=supvReportM.getCapacitorYearSupvNum();
                //评估报告
                supvReportM.setAppraisalReportYearSupvNum(dealData(childrenDeptList,processPublicYearDTOAppraisalS));
                supvReportM.setAppraisalReportMonthSupvNum(dealData(childrenDeptList,processPublicDTOAppraisalListM));
                supvReportM.setAppraisalReportTotalSupvNum(dealData(childrenDeptList,processPublicDTOAppraisalAll));
                supvReportM.setAppraisalReportMonthQuesNum(dealData(childrenDeptList,processPublicDTOAppraisalGanM));
                supvReportM.setAppraisalReportTotalQuesNum(dealData(childrenDeptList,processPublicDTOAppraisalGanAll));
                supvReportM.setAppraisalReportMonthReformNum(dealData(childrenDeptList,processPublicDTOAppraisalGanYesM));
                supvReportM.setAppraisalReportTotalReformNum(dealData(childrenDeptList,processPublicDTOAppraisalYesAll));
                all+=supvReportM.getAppraisalReportTotalSupvNum();
                yearAll+=supvReportM.getAppraisalReportYearSupvNum();

                supvReportM.setPlanYearCountNum(yearAll);

            }

            if(deptGetBase.getDeptLevel() == 2){
                //省级
                supvReportM.setProvinceId(deptGetBase.getUnitId());
                supvReportM.setProvinceName(deptGetBase.getUnitName());


                supvReportM.setStatisticsLevel("3");
            }else if(deptGetBase.getDeptLevel() == 3){
                //市
                PvTerminalTreeVO pvTerminalTreeVOOne = mapCode.get(deptGetBase.getUnitId());
                if(Objects.nonNull(pvTerminalTreeVOOne)){
                   PvTerminalTreeVO pvTerminalTreeVOTwo = mapList.get(pvTerminalTreeVOOne.getPid());
                    supvReportM.setCityId(deptGetBase.getUnitId());
                    supvReportM.setCityName(deptGetBase.getUnitName());
                    supvReportM.setProvinceName(pvTerminalTreeVOTwo.getName());
                    supvReportM.setProvinceId(pvTerminalTreeVOTwo.getCode());
                }
                supvReportM.setStatisticsLevel("4");
            }else if(deptGetBase.getDeptLevel() == 4){
                //县
                PvTerminalTreeVO pvTerminalTreeVOOne = mapCode.get(deptGetBase.getUnitId());
                if(Objects.nonNull(pvTerminalTreeVOOne)) {
                    PvTerminalTreeVO pvTerminalTreeVOTwo = mapList.get(pvTerminalTreeVOOne.getPid());
                    if(Objects.nonNull(pvTerminalTreeVOTwo)){
                        PvTerminalTreeVO pvTerminalTreeVOThree = mapList.get(pvTerminalTreeVOTwo.getPid());
                        supvReportM.setCountyId(deptGetBase.getUnitId());
                        supvReportM.setCountyName(deptGetBase.getUnitName());
                        supvReportM.setCityId(pvTerminalTreeVOTwo.getCode());
                        supvReportM.setCityName(pvTerminalTreeVOTwo.getName());
                        supvReportM.setProvinceName(pvTerminalTreeVOThree.getName());
                        supvReportM.setProvinceId(pvTerminalTreeVOThree.getCode());
                    }

                }
                supvReportM.setStatisticsLevel("5");
            }
            supvReportM.setSupvNum(all);
            //特殊处理每月第一天需要处理上送国网标识
            if(timeId.getDayOfMonth() == 1){
                supvReportM.setIsUploadHead(0);
            }
            supvReportMBatch.add(supvReportM);

        }

        for(SupvReportM supvReportM : supvReportMBatch){
            if(supvReportM.getStatisticsDept().equals(provinceId)){
                supvReportM.setPowerMonthPlanNum(dyProvinceCount);
                supvReportM.setPowerMonthConductedNum(dyProvinceCount);
            }
        }

        this.saveOrUpdateBatchByMultiId(supvReportMBatch);

        log.info(LocalDateTime.now()+"_________________________执行时长："+timeInterval.interval());
    }

    @Override
    public Page<SupvReportM> statisticReportPage(BaseParam baseParam) {
        List<String> deptCodes = new ArrayList<>();
        if(StrUtil.isNotBlank(baseParam.getSearchValue())){
            List<Dept>deptList = deptFeignClient.getDirectSonSelf(baseParam.getSearchValue()).getData();
            deptCodes = deptList.stream().map(Dept::getCode).collect(Collectors.toList());
        }
        LambdaQueryWrapper<SupvReportM> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(StrUtil.isNotBlank(baseParam.getSearchValue()),SupvReportM::getStatisticsDept,deptCodes)
                .between(StrUtil.isNotBlank(baseParam.getSearchBeginTime()),SupvReportM::getStatisticsDate,baseParam.getSearchBeginTime(),baseParam.getSearchEndTime());
        Page<SupvReportM> page = this.page(new Page<>(PageFactory.getPageNum(baseParam), PageFactory.getPageSize(baseParam)), lambdaQueryWrapper);
        List<SupvReportM> records = page.getRecords();
        if(CollUtil.isNotEmpty(records)){
            for (SupvReportM record : records) {

                record.setPlanCountNum(record.getConvertTotalSupvNum()+
                                record.getEnergyTotalSupvNum()+
                                record.getEnergyTotalSupvNewNum()+
                                record.getSensitiveTotalSupvNum()+
                                record.getPowerMonthConductedNum()+
                                record.getCapacitorTotalSupvNum()+
                                record.getAppraisalReportTotalSupvNum()
                );
                record.setProblemCountNum(record.getConvertTotalQuesNum()+
                        record.getEnergyTotalQuesNum()+
                        record.getEnergyTotalQuesNewNum()+
                        record.getSensitiveTotalQuesNum()+
                        record.getPowerTotalQuesNum()+
                        record.getCapacitorTotalQuesNum()+
                        record.getAppraisalReportTotalQuesNum()

                );
                record.setReformCountNum(record.getConvertTotalReformNum()+
                        record.getEnergyTotalReformNum()+
                        record.getEnergyTotalReformNewNum()+
                        record.getSensitiveTotalReformNum()+
                        record.getPowerTotalReformNum()+
                        record.getCapacitorTotalReformNum()+
                        record.getAppraisalReportTotalReformNum()

                );
                List<String> deptIds = deptFeignClient.getDepSonSelfCodetByCode(record.getStatisticsDept()).getData();
                //获取预告警单信息
                List<SupvAlarm> list = supvAlarmService.list(new LambdaQueryWrapper<SupvAlarm>().in(SupvAlarm::getCreaterOrgId, deptIds)
                        .between(StrUtil.isNotBlank(baseParam.getSearchBeginTime()),SupvAlarm::getCreaterTime,
                                DateUtil.beginOfYear(DateUtil.parse(baseParam.getSearchBeginTime())),
                                DateUtil.endOfYear(DateUtil.parse(baseParam.getSearchEndTime())))
                );
                int count =0;
                if(CollUtil.isNotEmpty(list)){
                    List<String> alarmIds = list.stream().map(SupvAlarm::getAlarmId).distinct().collect(Collectors.toList());
                    count=supvAlarmBackService.count(new LambdaQueryWrapper<SupvAlarmBack>().in(SupvAlarmBack::getWorkAlarmId, alarmIds));
                }
                record.setAlarmCountNum(list.size());
                record.setAlarmBackCountNum(count);
            }
        }
        return page;

    }


    private Integer dealData(List<String> childrenDeptList,List<ProcessPublicDTO> processPublicDTOList){
        Integer count = 0;
        for(ProcessPublicDTO processPublicDTO :processPublicDTOList) {
            if (childrenDeptList.contains(processPublicDTO.getId())) {
                count += processPublicDTO.getValue();
            }
        }
        return count;
    }

}
