package com.njcn.prepare.harmonic.service.mysql.newalgorithm.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.njcn.device.biz.pojo.dto.DeptGetChildrenMoreDTO;
import com.njcn.device.pms.pojo.po.RStatOrgPO;
import com.njcn.harmonic.pojo.po.RStatOrgMPO;
import com.njcn.prepare.harmonic.pojo.bo.CalculatedParam;
import com.njcn.prepare.harmonic.mapper.mysql.area.*;
import com.njcn.prepare.harmonic.pojo.mysql.po.*;
import com.njcn.prepare.harmonic.service.mysql.area.RStatHarmonicDPOService;
import com.njcn.prepare.harmonic.service.mysql.area.RStatHarmonicMPOService;
import com.njcn.prepare.harmonic.service.mysql.area.RStatHarmonicQPOService;
import com.njcn.prepare.harmonic.service.mysql.area.RStatHarmonicYPOService;
import com.njcn.prepare.harmonic.service.mysql.newalgorithm.*;
import com.njcn.prepare.harmonic.utils.PublicUtil;
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.pojo.po.Dept;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Description:
 * Date: 2023/8/14  10:14【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@RequiredArgsConstructor
public class RStatLoadTypeServiceImpl implements RStatLoadTypeService {

    private final RStatLoadTypeDPOService rStatLoadTypeDPOService;
    private final RStatLoadTypeMPOService rStatLoadTypeMPOService;
    private final RStatLoadTypeQPOService rStatLoadTypeQPOService;
    private final RStatLoadTypeYPOService rStatLoadTypeYPOService;



    private final
    DicDataFeignClient dicDataFeignClient;
    private final RStatHarmonicDPOService rStatHarmonicDPOService;
    private final RStatHarmonicMPOMapper rStatHarmonicMPOMapper;
    private final RStatHarmonicQPOMapper rStatHarmonicQPOMapper;
    private final RStatHarmonicYPOMapper rStatHarmonicYPOMapper;


    private final RStatOrgMMapper rStatOrgMMapper;
    private final RStatOrgQMapper rStatOrgQMapper;
    private final RStatOrgYMapper rStatOrgYMapper;

    @Override
    public void handleDay(CalculatedParam<DeptGetChildrenMoreDTO> calculatedParam) {
        LocalDate localDate = LocalDateTimeUtil.parseDate(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN);
        List<RStatLoadTypeDPO> result = new ArrayList<>();
        List<DictData> dataList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.MONITORING_LABELS.getCode()).getData();
        //过滤监测对象类型(陆上风电、光伏电站、电气化铁路、其他非线性负荷)
        Map<String, DictData> collect = dataList.stream().filter(temp -> Objects.equals(temp.getLevel(), 1)).collect(Collectors.toMap(DictData::getId, dictData -> dictData));

        List<DictData> dataTypeList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.DATA_TYPE.getCode()).getData();
        Map<String, DictData> dataTypeMap = dataTypeList.stream().collect(Collectors.toMap(DictData::getCode, dictData -> dictData));
        ;
        /*获取稳态统计指标*/
        List<DictData> steadyStatis = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.STEADY_STATIS.getCode()).getData();
        Map<String, DictData> steadyStatisMap = steadyStatis.stream().collect(Collectors.toMap(DictData::getCode, dictData -> dictData));
        calculatedParam.getIdList().forEach(dept -> {


            collect.forEach((m,n)->{
                RStatLoadTypeDPO rStatLoadTypeDPO = new RStatLoadTypeDPO();
                rStatLoadTypeDPO.setDataDate(localDate);
                rStatLoadTypeDPO.setOrgNo(dept.getUnitId());
                rStatLoadTypeDPO.setLoadType(m);
                /*只计算谐波电压、谐波电流、三相电压不平衡度、负序电流/闪变*/
                RStatHarmonicDPO one = rStatHarmonicDPOService.lambdaQuery().eq(RStatHarmonicDPO::getOrgNo, dept.getUnitId()).
                        eq(RStatHarmonicDPO::getDataDate, localDate).
                        eq(RStatHarmonicDPO::getMeasurementTypeClass, m).
                        eq(RStatHarmonicDPO::getDataType, dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId()).
                        eq(RStatHarmonicDPO::getHarmonicType, steadyStatisMap.get(DicDataEnum.HARMONIC_VOLTAGE.getCode()).getId()).one();
                if(Objects.nonNull(one)){
                    rStatLoadTypeDPO.setVRatio(one.getOverLimitMeasurementRatio());
                }else {
                    rStatLoadTypeDPO.setVRatio(0.00);
                }
                RStatHarmonicDPO two = rStatHarmonicDPOService.lambdaQuery().eq(RStatHarmonicDPO::getOrgNo, dept.getUnitId()).
                        eq(RStatHarmonicDPO::getDataDate, localDate).
                        eq(RStatHarmonicDPO::getMeasurementTypeClass, m).
                        eq(RStatHarmonicDPO::getDataType, dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId()).
                        eq(RStatHarmonicDPO::getHarmonicType, steadyStatisMap.get(DicDataEnum.HARMONIC_CURRENT.getCode()).getId()).one();
                if(Objects.nonNull(two)){
                    rStatLoadTypeDPO.setIRatio(two.getOverLimitMeasurementRatio());
                }else {
                    rStatLoadTypeDPO.setIRatio(0.00);
                }
                RStatHarmonicDPO three = rStatHarmonicDPOService.lambdaQuery().eq(RStatHarmonicDPO::getOrgNo, dept.getUnitId()).
                        eq(RStatHarmonicDPO::getDataDate, localDate).
                        eq(RStatHarmonicDPO::getMeasurementTypeClass, m).
                        eq(RStatHarmonicDPO::getDataType, dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId()).
                        eq(RStatHarmonicDPO::getHarmonicType, steadyStatisMap.get(DicDataEnum.PHASE_VOLTAGE.getCode()).getId()).one();
                if(Objects.nonNull(three)){
                    rStatLoadTypeDPO.setUnbalanceRatio(three.getOverLimitMeasurementRatio());
                }else {
                    rStatLoadTypeDPO.setUnbalanceRatio(0.00);
                }
                RStatHarmonicDPO four = rStatHarmonicDPOService.lambdaQuery().eq(RStatHarmonicDPO::getOrgNo, dept.getUnitId()).
                        eq(RStatHarmonicDPO::getDataDate, localDate).
                        eq(RStatHarmonicDPO::getMeasurementTypeClass, m).
                        eq(RStatHarmonicDPO::getDataType, dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId()).
                        eq(RStatHarmonicDPO::getHarmonicType, steadyStatisMap.get(DicDataEnum.NEG_CURRENT.getCode()).getId()).one();
                if(Objects.nonNull(four)){
                    rStatLoadTypeDPO.setINegRatio(four.getOverLimitMeasurementRatio());
                }else {
                    rStatLoadTypeDPO.setINegRatio(0.00);
                }
                RStatHarmonicDPO five = rStatHarmonicDPOService.lambdaQuery().eq(RStatHarmonicDPO::getOrgNo, dept.getUnitId()).
                        eq(RStatHarmonicDPO::getDataDate, localDate).
                        eq(RStatHarmonicDPO::getMeasurementTypeClass, m).
                        eq(RStatHarmonicDPO::getDataType, dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId()).
                        eq(RStatHarmonicDPO::getHarmonicType, steadyStatisMap.get(DicDataEnum.FLICKER.getCode()).getId()).one();
                if(Objects.nonNull(five)){
                    rStatLoadTypeDPO.setFlickerRatio(five.getOverLimitMeasurementRatio());
                }else {
                    rStatLoadTypeDPO.setFlickerRatio(0.00);
                }
                result.add(rStatLoadTypeDPO);
            });
        });

        if(CollectionUtil.isNotEmpty(result)){
            rStatLoadTypeDPOService.saveOrUpdateBatchByMultiId(result,500);
        }

    }

    @Override
    public void handleMonth(CalculatedParam<DeptGetChildrenMoreDTO> calculatedParam) {

        DateTime begin = DateUtil.beginOfMonth(DateUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN));
        DateTime end = DateUtil.endOfMonth(DateUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN));
        LocalDate localDate = LocalDateTimeUtil.parseDate( DateUtil.format(begin, DatePattern.NORM_DATE_PATTERN), DatePattern.NORM_DATE_PATTERN);
        List<RStatLoadTypeMPO> result = new ArrayList<>();
        QueryWrapper<RStatHarmonicMPO> queryWrapper = new QueryWrapper<>();
        QueryWrapper<RStatOrgMPO> rStatOrgMPOQueryWrapper = new QueryWrapper<>();
        QueryWrapper<RStatLoadTypeDPO> rStatLoadTypeDPOQueryWrapper = new QueryWrapper<>();

        List<DictData> dataList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.MONITORING_LABELS.getCode()).getData();
        //过滤监测对象类型(陆上风电、分布式光伏、电气化铁路、其他非线性负荷)
        Map<String, DictData> collect = dataList.stream().filter(temp -> Objects.equals(temp.getLevel(), 1)).collect(Collectors.toMap(DictData::getId, dictData -> dictData));

        List<DictData> dataTypeList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.DATA_TYPE.getCode()).getData();
        Map<String, DictData> dataTypeMap = dataTypeList.stream().collect(Collectors.toMap(DictData::getCode, dictData -> dictData));
        ;
        /*获取稳态统计指标*/
        List<DictData> steadyStatis = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.STEADY_STATIS.getCode()).getData();
        Map<String, DictData> steadyStatisMap = steadyStatis.stream().collect(Collectors.toMap(DictData::getCode, dictData -> dictData));
        calculatedParam.getIdList().forEach(dept -> {


            collect.forEach((m,n)->{
                rStatOrgMPOQueryWrapper.clear();
                rStatOrgMPOQueryWrapper.eq("measurement_type_class",m).
                        eq("org_no",dept.getUnitId()).
                        between("data_date",begin,end);
                List<RStatOrgMPO> rStatOrgMPOS = rStatOrgMMapper.selectList(rStatOrgMPOQueryWrapper);

                queryWrapper.clear();
                queryWrapper.eq("org_no",dept.getUnitId()).
                        between("data_date",begin,end);
                List<RStatHarmonicMPO> rStatHarmonicMPOS = rStatHarmonicMPOMapper.selectList(queryWrapper);
                rStatLoadTypeDPOQueryWrapper.clear();
                rStatLoadTypeDPOQueryWrapper.eq("org_no",dept.getUnitId()).
                        eq("load_type",m).
                        between("data_date",begin,end);
                List<RStatLoadTypeDPO> rStatLoadTypeDPOS = rStatLoadTypeDPOService.getBaseMapper().selectList(rStatLoadTypeDPOQueryWrapper);

                RStatLoadTypeMPO rStatLoadTypeMPO = new RStatLoadTypeMPO();
                rStatLoadTypeMPO.setDataDate(localDate);
                rStatLoadTypeMPO.setOrgNo(dept.getUnitId());
                rStatLoadTypeMPO.setLoadType(m);
                /*2、有效接入监测点数*/
                if(CollectionUtils.isEmpty(rStatOrgMPOS)){
                    rStatLoadTypeMPO.setEffectiveMeasurement(0);
                }else {
                    rStatLoadTypeMPO.setEffectiveMeasurement(rStatOrgMPOS.get(0).getEffectiveMeasurementAccrued());
                }
                /*4、谐波电压超标点数6、谐波电流超标点数*/
                if(CollectionUtils.isEmpty(rStatHarmonicMPOS)){
                    rStatLoadTypeMPO.setVCount(0);
                    rStatLoadTypeMPO.setICount(0);
                    rStatLoadTypeMPO.setINegCount(0);
                    rStatLoadTypeMPO.setUnbalanceCount(0);
                    rStatLoadTypeMPO.setFlickerCount(0);
                }else {
                    Optional<RStatHarmonicMPO> vfirst = rStatHarmonicMPOS.stream().filter(temp -> Objects.equals(temp.getHarmonicType(), steadyStatisMap.get(DicDataEnum.HARMONIC_VOLTAGE.getCode()).getId())
                            &&Objects.equals(temp.getMeasurementTypeClass(), m)
                    ).findFirst();
                    rStatLoadTypeMPO.setVCount(vfirst.isPresent() ? vfirst.get().getOverLimitMeasurementAccrued(): 0);
                    Optional<RStatHarmonicMPO> ifirst = rStatHarmonicMPOS.stream().filter(temp -> Objects.equals(temp.getHarmonicType(), steadyStatisMap.get(DicDataEnum.HARMONIC_CURRENT.getCode()).getId())
                            &&Objects.equals(temp.getMeasurementTypeClass(), m)
                    ).findFirst();
                    rStatLoadTypeMPO.setICount(ifirst.isPresent() ? ifirst.get().getOverLimitMeasurementAccrued(): 0);
                    Optional<RStatHarmonicMPO> pfirst = rStatHarmonicMPOS.stream().filter(temp -> Objects.equals(temp.getHarmonicType(), steadyStatisMap.get(DicDataEnum.PHASE_VOLTAGE.getCode()).getId())
                            &&Objects.equals(temp.getMeasurementTypeClass(), m)
                    ).findFirst();
                    Optional<RStatHarmonicMPO> nfirst = rStatHarmonicMPOS.stream().filter(temp -> Objects.equals(temp.getHarmonicType(), steadyStatisMap.get(DicDataEnum.NEG_CURRENT.getCode()).getId())
                            &&Objects.equals(temp.getMeasurementTypeClass(), m)
                    ).findFirst();
                    Optional<RStatHarmonicMPO> ffirst = rStatHarmonicMPOS.stream().filter(temp -> Objects.equals(temp.getHarmonicType(), steadyStatisMap.get(DicDataEnum.FLICKER.getCode()).getId())
                            &&Objects.equals(temp.getMeasurementTypeClass(), m)
                    ).findFirst();
                    rStatLoadTypeMPO.setINegCount(nfirst.isPresent() ? nfirst.get().getOverLimitMeasurementAccrued(): 0);
                    rStatLoadTypeMPO.setUnbalanceCount(pfirst.isPresent() ? pfirst.get().getOverLimitMeasurementAccrued(): 0);
                    rStatLoadTypeMPO.setFlickerCount(ffirst.isPresent() ? ffirst.get().getOverLimitMeasurementAccrued(): 0);

                }
                /*3、日均谐波电压超标点占比5、日均谐波电流超标点占比,7、日均三相电压不平衡度超标点占比9、日均负序电流超标点占比11、日均闪变超标点占比*/
                if(CollectionUtils.isEmpty(rStatLoadTypeDPOS)){
                    rStatLoadTypeMPO.setVAverage(0.00);
                    rStatLoadTypeMPO.setIAverage(0.00);
                    rStatLoadTypeMPO.setUnbalanceAverage(0.00);
                    rStatLoadTypeMPO.setFlickerAverage(0.00);
                    rStatLoadTypeMPO.setINegAverage(0.00);
                }else {
                    double vavg = rStatLoadTypeDPOS.stream().mapToDouble(RStatLoadTypeDPO::getVRatio).average().getAsDouble();
                    rStatLoadTypeMPO.setVAverage(vavg);
                    double iavge = rStatLoadTypeDPOS.stream().mapToDouble(RStatLoadTypeDPO::getIRatio).average().getAsDouble();
                    rStatLoadTypeMPO.setIAverage(iavge);
                    double uavge = rStatLoadTypeDPOS.stream().mapToDouble(RStatLoadTypeDPO::getUnbalanceRatio).average().getAsDouble();
                    rStatLoadTypeMPO.setUnbalanceAverage(uavge);
                    double inavge = rStatLoadTypeDPOS.stream().mapToDouble(RStatLoadTypeDPO::getINegRatio).average().getAsDouble();
                    double favg = rStatLoadTypeDPOS.stream().mapToDouble(RStatLoadTypeDPO::getFlickerRatio).average().getAsDouble();
                    rStatLoadTypeMPO.setFlickerAverage(favg);
                    rStatLoadTypeMPO.setINegAverage(inavge);

                }
                result.add(rStatLoadTypeMPO);
            });
        });
        if(CollectionUtil.isNotEmpty(result)){
            rStatLoadTypeMPOService.saveOrUpdateBatchByMultiId(result,500);
        }

    }

    @Override
    public void handleQtr(CalculatedParam<DeptGetChildrenMoreDTO> calculatedParam) {

        DateTime begin = DateUtil.beginOfQuarter(DateUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN));
        DateTime end = DateUtil.endOfQuarter(DateUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN));
        LocalDate localDate = LocalDateTimeUtil.parseDate( DateUtil.format(begin, DatePattern.NORM_DATE_PATTERN), DatePattern.NORM_DATE_PATTERN);

        List<RStatLoadTypeQPO> result = new ArrayList<>();
        QueryWrapper<RStatHarmonicQPO> queryWrapper = new QueryWrapper<>();
        QueryWrapper<RStatOrgQPO> rStatOrgQPOQueryWrapper = new QueryWrapper<>();
        QueryWrapper<RStatLoadTypeMPO> rStatLoadTypeMPOQueryWrapper = new QueryWrapper<>();

        List<DictData> dataList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.MONITORING_LABELS.getCode()).getData();
        //过滤监测对象类型(陆上风电、分布式光伏、电气化铁路、其他非线性负荷)
        Map<String, DictData> collect = dataList.stream().filter(temp -> Objects.equals(temp.getLevel(), 1)).collect(Collectors.toMap(DictData::getId, dictData -> dictData));

        List<DictData> dataTypeList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.DATA_TYPE.getCode()).getData();
        Map<String, DictData> dataTypeMap = dataTypeList.stream().collect(Collectors.toMap(DictData::getCode, dictData -> dictData));
        ;
        /*获取稳态统计指标*/
        List<DictData> steadyStatis = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.STEADY_STATIS.getCode()).getData();
        Map<String, DictData> steadyStatisMap = steadyStatis.stream().collect(Collectors.toMap(DictData::getCode, dictData -> dictData));
        calculatedParam.getIdList().forEach(dept -> {


            collect.forEach((m,n)->{
                rStatOrgQPOQueryWrapper.clear();
                rStatOrgQPOQueryWrapper.eq("measurement_type_class",m).
                        eq("org_no",dept.getUnitId()).
                        between("data_date",begin,end);
                List<RStatOrgQPO> rStatOrgQPOS = rStatOrgQMapper.selectList(rStatOrgQPOQueryWrapper);

                queryWrapper.clear();
                queryWrapper.eq("org_no",dept.getUnitId()).
                        between("data_date",begin,end);
                List<RStatHarmonicQPO> rStatHarmonicQPOS = rStatHarmonicQPOMapper.selectList(queryWrapper);
                rStatLoadTypeMPOQueryWrapper.clear();
                rStatLoadTypeMPOQueryWrapper.eq("org_no",dept.getUnitId()).
                        eq("load_type",m).
                        between("data_date",begin,end);
                List<RStatLoadTypeMPO> rStatLoadTypeMPOS = rStatLoadTypeMPOService.getBaseMapper().selectList(rStatLoadTypeMPOQueryWrapper);

                RStatLoadTypeQPO rStatLoadTypeQPO = new RStatLoadTypeQPO();
                rStatLoadTypeQPO.setDataDate(localDate);
                rStatLoadTypeQPO.setOrgNo(dept.getUnitId());
                rStatLoadTypeQPO.setLoadType(m);
                /*2、有效接入监测点数*/
                if(CollectionUtils.isEmpty(rStatOrgQPOS)){
                    rStatLoadTypeQPO.setEffectiveMeasurement(0);
                }else {
                    rStatLoadTypeQPO.setEffectiveMeasurement(rStatOrgQPOS.get(0).getEffectiveMeasurementAccrued());
                }
                /*4、谐波电压超标点数6、谐波电流超标点数*/
                if(CollectionUtils.isEmpty(rStatHarmonicQPOS)){
                    rStatLoadTypeQPO.setVCount(0);
                    rStatLoadTypeQPO.setICount(0);
                    rStatLoadTypeQPO.setINegCount(0);
                    rStatLoadTypeQPO.setUnbalanceCount(0);
                    rStatLoadTypeQPO.setFlickerCount(0);
                }else {
                    Optional<RStatHarmonicQPO> vfirst = rStatHarmonicQPOS.stream().filter(temp -> Objects.equals(temp.getHarmonicType(), steadyStatisMap.get(DicDataEnum.HARMONIC_VOLTAGE.getCode()).getId())
                            &&Objects.equals(temp.getMeasurementTypeClass(), m)
                    ).findFirst();
                    rStatLoadTypeQPO.setVCount(vfirst.isPresent() ? vfirst.get().getOverLimitMeasurementAccrued(): 0);
                    Optional<RStatHarmonicQPO> ifirst = rStatHarmonicQPOS.stream().filter(temp -> Objects.equals(temp.getHarmonicType(), steadyStatisMap.get(DicDataEnum.HARMONIC_CURRENT.getCode()).getId())
                            &&Objects.equals(temp.getMeasurementTypeClass(), m)
                    ).findFirst();
                    rStatLoadTypeQPO.setICount(ifirst.isPresent() ? ifirst.get().getOverLimitMeasurementAccrued(): 0);
                    Optional<RStatHarmonicQPO> pfirst = rStatHarmonicQPOS.stream().filter(temp -> Objects.equals(temp.getHarmonicType(), steadyStatisMap.get(DicDataEnum.PHASE_VOLTAGE.getCode()).getId())
                            &&Objects.equals(temp.getMeasurementTypeClass(), m)
                    ).findFirst();
                    Optional<RStatHarmonicQPO> nfirst = rStatHarmonicQPOS.stream().filter(temp -> Objects.equals(temp.getHarmonicType(), steadyStatisMap.get(DicDataEnum.NEG_CURRENT.getCode()).getId())
                            &&Objects.equals(temp.getMeasurementTypeClass(), m)
                    ).findFirst();
                    Optional<RStatHarmonicQPO> ffirst = rStatHarmonicQPOS.stream().filter(temp -> Objects.equals(temp.getHarmonicType(), steadyStatisMap.get(DicDataEnum.FLICKER.getCode()).getId())
                            &&Objects.equals(temp.getMeasurementTypeClass(), m)
                    ).findFirst();

                    rStatLoadTypeQPO.setINegCount(nfirst.isPresent() ? nfirst.get().getOverLimitMeasurementAccrued(): 0);
                    rStatLoadTypeQPO.setUnbalanceCount(pfirst.isPresent() ? pfirst.get().getOverLimitMeasurementAccrued(): 0);
                    rStatLoadTypeQPO.setFlickerCount(ffirst.isPresent() ? ffirst.get().getOverLimitMeasurementAccrued(): 0);

                }
                /*3、日均谐波电压超标点占比5、日均谐波电流超标点占比,7、日均三相电压不平衡度超标点占比9、日均负序电流超标点占比11、日均闪变超标点占比*/
                if(CollectionUtils.isEmpty(rStatLoadTypeMPOS)){
                    rStatLoadTypeQPO.setVAverage(0.00);
                    rStatLoadTypeQPO.setIAverage(0.00);
                    rStatLoadTypeQPO.setUnbalanceAverage(0.00);
                    rStatLoadTypeQPO.setFlickerAverage(0.00);
                    rStatLoadTypeQPO.setINegAverage(0.00);
                }else {
                    double vavg = rStatLoadTypeMPOS.stream().mapToDouble(RStatLoadTypeMPO::getVAverage).average().getAsDouble();
                    rStatLoadTypeQPO.setVAverage(vavg);
                    double iavge = rStatLoadTypeMPOS.stream().mapToDouble(RStatLoadTypeMPO::getIAverage).average().getAsDouble();
                    rStatLoadTypeQPO.setIAverage(iavge);
                    double uavge = rStatLoadTypeMPOS.stream().mapToDouble(RStatLoadTypeMPO::getUnbalanceAverage).average().getAsDouble();
                    rStatLoadTypeQPO.setUnbalanceAverage(uavge);
                    double inavge = rStatLoadTypeMPOS.stream().mapToDouble(RStatLoadTypeMPO::getINegAverage).average().getAsDouble();
                    double favg = rStatLoadTypeMPOS.stream().mapToDouble(RStatLoadTypeMPO::getFlickerAverage).average().getAsDouble();
                    rStatLoadTypeQPO.setFlickerAverage(favg);
                    rStatLoadTypeQPO.setINegAverage(inavge);

                }
                result.add(rStatLoadTypeQPO);
            });
        });
        if(CollectionUtil.isNotEmpty(result)){
            rStatLoadTypeQPOService.saveOrUpdateBatchByMultiId(result,500);
        }

    }

    @Override
    public void handleYear(CalculatedParam<DeptGetChildrenMoreDTO> calculatedParam) {


        DateTime begin = DateUtil.beginOfYear(DateUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN));
        DateTime end = DateUtil.endOfYear(DateUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN));
        LocalDate localDate = LocalDateTimeUtil.parseDate( DateUtil.format(begin, DatePattern.NORM_DATE_PATTERN), DatePattern.NORM_DATE_PATTERN);

        List<RStatLoadTypeYPO> result = new ArrayList<>();
        QueryWrapper<RStatHarmonicYPO> queryWrapper = new QueryWrapper<>();
        QueryWrapper<RStatOrgYPO> rStatOrgYPOQueryWrapper = new QueryWrapper<>();
        QueryWrapper<RStatLoadTypeMPO> rStatLoadTypeMPOQueryWrapper = new QueryWrapper<>();

        List<DictData> dataList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.MONITORING_LABELS.getCode()).getData();
        //过滤监测对象类型(陆上风电、分布式光伏、电气化铁路、其他非线性负荷)
        Map<String, DictData> collect = dataList.stream().filter(temp -> Objects.equals(temp.getLevel(), 1)).collect(Collectors.toMap(DictData::getId, dictData -> dictData));

        List<DictData> dataTypeList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.DATA_TYPE.getCode()).getData();
        Map<String, DictData> dataTypeMap = dataTypeList.stream().collect(Collectors.toMap(DictData::getCode, dictData -> dictData));
        ;
        /*获取稳态统计指标*/
        List<DictData> steadyStatis = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.STEADY_STATIS.getCode()).getData();
        Map<String, DictData> steadyStatisMap = steadyStatis.stream().collect(Collectors.toMap(DictData::getCode, dictData -> dictData));
        calculatedParam.getIdList().forEach(dept -> {


            collect.forEach((m,n)->{
                rStatOrgYPOQueryWrapper.clear();
                rStatOrgYPOQueryWrapper.eq("measurement_type_class",m).
                        eq("org_no",dept.getUnitId()).
                        between("data_date",begin,end);
                List<RStatOrgYPO> rStatOrgYPOS = rStatOrgYMapper.selectList(rStatOrgYPOQueryWrapper);

                queryWrapper.clear();
                queryWrapper.eq("org_no",dept.getUnitId()).
                        between("data_date",begin,end);
                List<RStatHarmonicYPO> rStatHarmonicYPOS = rStatHarmonicYPOMapper.selectList(queryWrapper);
                rStatLoadTypeMPOQueryWrapper.clear();
                rStatLoadTypeMPOQueryWrapper.eq("org_no",dept.getUnitId()).
                        eq("load_type",m).
                        between("data_date",begin,end);
                List<RStatLoadTypeMPO> rStatLoadTypeMPOS = rStatLoadTypeMPOService.getBaseMapper().selectList(rStatLoadTypeMPOQueryWrapper);

                RStatLoadTypeYPO rStatLoadTypeYPO = new RStatLoadTypeYPO();
                rStatLoadTypeYPO.setDataDate(localDate);
                rStatLoadTypeYPO.setOrgNo(dept.getUnitId());
                rStatLoadTypeYPO.setLoadType(m);
                /*2、有效接入监测点数*/
                if(CollectionUtils.isEmpty(rStatOrgYPOS)){
                    rStatLoadTypeYPO.setEffectiveMeasurement(0);
                }else {
                    rStatLoadTypeYPO.setEffectiveMeasurement(rStatOrgYPOS.get(0).getEffectiveMeasurementAccrued());
                }
                /*4、谐波电压超标点数6、谐波电流超标点数*/
                if(CollectionUtils.isEmpty(rStatHarmonicYPOS)){
                    rStatLoadTypeYPO.setVCount(0);
                    rStatLoadTypeYPO.setICount(0);
                    rStatLoadTypeYPO.setINegCount(0);
                    rStatLoadTypeYPO.setUnbalanceCount(0);
                    rStatLoadTypeYPO.setFlickerCount(0);
                }else {
                    Optional<RStatHarmonicYPO> vfirst = rStatHarmonicYPOS.stream().filter(temp -> Objects.equals(temp.getHarmonicType(), steadyStatisMap.get(DicDataEnum.HARMONIC_VOLTAGE.getCode()).getId())
                            &&Objects.equals(temp.getMeasurementTypeClass(), m)
                    ).findFirst();
                    rStatLoadTypeYPO.setVCount(vfirst.isPresent() ? vfirst.get().getOverLimitMeasurementAccrued(): 0);
                    Optional<RStatHarmonicYPO> ifirst = rStatHarmonicYPOS.stream().filter(temp -> Objects.equals(temp.getHarmonicType(), steadyStatisMap.get(DicDataEnum.HARMONIC_CURRENT.getCode()).getId())
                            &&Objects.equals(temp.getMeasurementTypeClass(), m)
                    ).findFirst();
                    rStatLoadTypeYPO.setICount(ifirst.isPresent() ? ifirst.get().getOverLimitMeasurementAccrued(): 0);
                    Optional<RStatHarmonicYPO> pfirst = rStatHarmonicYPOS.stream().filter(temp -> Objects.equals(temp.getHarmonicType(), steadyStatisMap.get(DicDataEnum.PHASE_VOLTAGE.getCode()).getId())
                            &&Objects.equals(temp.getMeasurementTypeClass(), m)
                    ).findFirst();
                    Optional<RStatHarmonicYPO> nfirst = rStatHarmonicYPOS.stream().filter(temp -> Objects.equals(temp.getHarmonicType(), steadyStatisMap.get(DicDataEnum.NEG_CURRENT.getCode()).getId())
                            &&Objects.equals(temp.getMeasurementTypeClass(), m)
                    ).findFirst();
                    Optional<RStatHarmonicYPO> ffirst = rStatHarmonicYPOS.stream().filter(temp -> Objects.equals(temp.getHarmonicType(), steadyStatisMap.get(DicDataEnum.FLICKER.getCode()).getId())
                            &&Objects.equals(temp.getMeasurementTypeClass(), m)
                    ).findFirst();

                    rStatLoadTypeYPO.setINegCount(nfirst.isPresent() ? nfirst.get().getOverLimitMeasurementAccrued(): 0);
                    rStatLoadTypeYPO.setUnbalanceCount(pfirst.isPresent() ? pfirst.get().getOverLimitMeasurementAccrued(): 0);
                    rStatLoadTypeYPO.setFlickerCount(ffirst.isPresent() ? ffirst.get().getOverLimitMeasurementAccrued(): 0);

                }
                /*3、日均谐波电压超标点占比5、日均谐波电流超标点占比,7、日均三相电压不平衡度超标点占比9、日均负序电流超标点占比11、日均闪变超标点占比*/
                if(CollectionUtils.isEmpty(rStatLoadTypeMPOS)){
                    rStatLoadTypeYPO.setVAverage(0.00);
                    rStatLoadTypeYPO.setIAverage(0.00);
                    rStatLoadTypeYPO.setUnbalanceAverage(0.00);
                    rStatLoadTypeYPO.setFlickerAverage(0.00);
                    rStatLoadTypeYPO.setINegAverage(0.00);
                }else {
                    double vavg = rStatLoadTypeMPOS.stream().mapToDouble(RStatLoadTypeMPO::getVAverage).average().getAsDouble();
                    rStatLoadTypeYPO.setVAverage(vavg);
                    double iavge = rStatLoadTypeMPOS.stream().mapToDouble(RStatLoadTypeMPO::getIAverage).average().getAsDouble();
                    rStatLoadTypeYPO.setIAverage(iavge);
                    double uavge = rStatLoadTypeMPOS.stream().mapToDouble(RStatLoadTypeMPO::getUnbalanceAverage).average().getAsDouble();
                    rStatLoadTypeYPO.setUnbalanceAverage(uavge);
                    double inavge = rStatLoadTypeMPOS.stream().mapToDouble(RStatLoadTypeMPO::getINegAverage).average().getAsDouble();
                    double favg = rStatLoadTypeMPOS.stream().mapToDouble(RStatLoadTypeMPO::getFlickerAverage).average().getAsDouble();
                    rStatLoadTypeYPO.setFlickerAverage(favg);
                    rStatLoadTypeYPO.setINegAverage(inavge);

                }
                result.add(rStatLoadTypeYPO);
            });
        });
        if(CollectionUtil.isNotEmpty(result)){
            rStatLoadTypeYPOService.saveOrUpdateBatchByMultiId(result,500);
        }

    }
    @Override
    public void handler(List<Dept> deptList, String dataDate, Integer type) {
        switch (type) {
            case 1:
                this.hanlderYear (deptList,  dataDate,  type);
                break;
            case 2:
                this.hanlderQtr (deptList,  dataDate,  type);
                break;
            case 3:
                this.hanlderMonth (deptList,  dataDate,  type);
                break;
            case 5:
                this.hanlderDay ( deptList,  dataDate,  type);
                break;
            default:
                break;
        }
    }



    @Transactional(rollbackFor = {Exception.class})
    @Async("asyncExecutor")
    @SneakyThrows
    private void hanlderYear(List<Dept> deptList, String dataDate, Integer type) {
        LocalDate localDate = LocalDateTimeUtil.parseDate(dataDate, DatePattern.NORM_DATE_PATTERN);
        List<RStatLoadTypeYPO> result = new ArrayList<>();
        QueryWrapper<RStatHarmonicYPO> queryWrapper = new QueryWrapper<>();
        QueryWrapper<RStatOrgYPO> rStatOrgYPOQueryWrapper = new QueryWrapper<>();
        QueryWrapper<RStatLoadTypeMPO> rStatLoadTypeMPOQueryWrapper = new QueryWrapper<>();

        List<DictData> dataList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.MONITORING_LABELS.getCode()).getData();
        //过滤监测对象类型(陆上风电、分布式光伏、电气化铁路、其他非线性负荷)
        Map<String, DictData> collect = dataList.stream().filter(temp -> Objects.equals(temp.getLevel(), 1)).collect(Collectors.toMap(DictData::getId, dictData -> dictData));

        List<DictData> dataTypeList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.DATA_TYPE.getCode()).getData();
        Map<String, DictData> dataTypeMap = dataTypeList.stream().collect(Collectors.toMap(DictData::getCode, dictData -> dictData));
        ;
        /*获取稳态统计指标*/
        List<DictData> steadyStatis = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.STEADY_STATIS.getCode()).getData();
        Map<String, DictData> steadyStatisMap = steadyStatis.stream().collect(Collectors.toMap(DictData::getCode, dictData -> dictData));
        deptList.forEach(dept -> {


            collect.forEach((m,n)->{
                rStatOrgYPOQueryWrapper.clear();
                rStatOrgYPOQueryWrapper.eq("measurement_type_class",m).
                        eq("org_no",dept.getId()).
                        eq("YEAR( data_date)", dataDate.substring(0,4));
                List<RStatOrgYPO> rStatOrgYPOS = rStatOrgYMapper.selectList(rStatOrgYPOQueryWrapper);

                queryWrapper.clear();
                queryWrapper.eq("org_no",dept.getId()).
                        eq("YEAR( data_date)", dataDate.substring(0,4));
                List<RStatHarmonicYPO> rStatHarmonicYPOS = rStatHarmonicYPOMapper.selectList(queryWrapper);
                rStatLoadTypeMPOQueryWrapper.clear();
                rStatLoadTypeMPOQueryWrapper.eq("org_no",dept.getId()).
                        eq("load_type",m).
                        eq("YEAR( data_date)", dataDate.substring(0,4));
                List<RStatLoadTypeMPO> rStatLoadTypeMPOS = rStatLoadTypeMPOService.getBaseMapper().selectList(rStatLoadTypeMPOQueryWrapper);

                RStatLoadTypeYPO rStatLoadTypeYPO = new RStatLoadTypeYPO();
                rStatLoadTypeYPO.setDataDate(localDate);
                rStatLoadTypeYPO.setOrgNo(dept.getId());
                rStatLoadTypeYPO.setLoadType(m);
                /*2、有效接入监测点数*/
                if(CollectionUtils.isEmpty(rStatOrgYPOS)){
                    rStatLoadTypeYPO.setEffectiveMeasurement(0);
                }else {
                    rStatLoadTypeYPO.setEffectiveMeasurement(rStatOrgYPOS.get(0).getEffectiveMeasurementAccrued());
                }
                /*4、谐波电压超标点数6、谐波电流超标点数*/
                if(CollectionUtils.isEmpty(rStatHarmonicYPOS)){
                    rStatLoadTypeYPO.setVCount(0);
                    rStatLoadTypeYPO.setICount(0);
                    rStatLoadTypeYPO.setINegCount(0);
                    rStatLoadTypeYPO.setUnbalanceCount(0);
                    rStatLoadTypeYPO.setFlickerCount(0);
                }else {
                    Optional<RStatHarmonicYPO> vfirst = rStatHarmonicYPOS.stream().filter(temp -> Objects.equals(temp.getHarmonicType(), steadyStatisMap.get(DicDataEnum.HARMONIC_VOLTAGE).getId())).findFirst();
                    rStatLoadTypeYPO.setVCount(vfirst.isPresent() ? vfirst.get().getOverLimitMeasurementAccrued(): 0);
                    Optional<RStatHarmonicYPO> ifirst = rStatHarmonicYPOS.stream().filter(temp -> Objects.equals(temp.getHarmonicType(), steadyStatisMap.get(DicDataEnum.HARMONIC_CURRENT).getId())).findFirst();
                    rStatLoadTypeYPO.setVCount(ifirst.isPresent() ? ifirst.get().getOverLimitMeasurementAccrued(): 0);
                    Optional<RStatHarmonicYPO> pfirst = rStatHarmonicYPOS.stream().filter(temp -> Objects.equals(temp.getHarmonicType(), steadyStatisMap.get(DicDataEnum.PHASE_VOLTAGE).getId())).findFirst();
                    Optional<RStatHarmonicYPO> nfirst = rStatHarmonicYPOS.stream().filter(temp -> Objects.equals(temp.getHarmonicType(), steadyStatisMap.get(DicDataEnum.NEG_CURRENT).getId())).findFirst();
                    Optional<RStatHarmonicYPO> ffirst = rStatHarmonicYPOS.stream().filter(temp -> Objects.equals(temp.getHarmonicType(), steadyStatisMap.get(DicDataEnum.FLICKER).getId())).findFirst();

                    rStatLoadTypeYPO.setINegCount(nfirst.isPresent() ? nfirst.get().getOverLimitMeasurementAccrued(): 0);
                    rStatLoadTypeYPO.setUnbalanceCount(pfirst.isPresent() ? pfirst.get().getOverLimitMeasurementAccrued(): 0);
                    rStatLoadTypeYPO.setFlickerCount(ffirst.isPresent() ? ffirst.get().getOverLimitMeasurementAccrued(): 0);

                }
                /*3、日均谐波电压超标点占比5、日均谐波电流超标点占比,7、日均三相电压不平衡度超标点占比9、日均负序电流超标点占比11、日均闪变超标点占比*/
                if(CollectionUtils.isEmpty(rStatLoadTypeMPOS)){
                    rStatLoadTypeYPO.setVAverage(0.00);
                    rStatLoadTypeYPO.setIAverage(0.00);
                    rStatLoadTypeYPO.setUnbalanceAverage(0.00);
                    rStatLoadTypeYPO.setFlickerAverage(0.00);
                    rStatLoadTypeYPO.setINegAverage(0.00);
                }else {
                    double vavg = rStatLoadTypeMPOS.stream().mapToDouble(RStatLoadTypeMPO::getVAverage).average().getAsDouble();
                    rStatLoadTypeYPO.setVAverage(vavg);
                    double iavge = rStatLoadTypeMPOS.stream().mapToDouble(RStatLoadTypeMPO::getIAverage).average().getAsDouble();
                    rStatLoadTypeYPO.setIAverage(iavge);
                    double uavge = rStatLoadTypeMPOS.stream().mapToDouble(RStatLoadTypeMPO::getUnbalanceAverage).average().getAsDouble();
                    rStatLoadTypeYPO.setUnbalanceAverage(uavge);
                    double inavge = rStatLoadTypeMPOS.stream().mapToDouble(RStatLoadTypeMPO::getINegAverage).average().getAsDouble();
                    double favg = rStatLoadTypeMPOS.stream().mapToDouble(RStatLoadTypeMPO::getFlickerAverage).average().getAsDouble();
                    rStatLoadTypeYPO.setFlickerAverage(favg);
                    rStatLoadTypeYPO.setINegAverage(inavge);

                }
                result.add(rStatLoadTypeYPO);
            });
        });
        if(CollectionUtil.isNotEmpty(result)){
            rStatLoadTypeYPOService.saveOrUpdateBatchByMultiId(result,500);
        }
    }
    @Transactional(rollbackFor = {Exception.class})
    @Async("asyncExecutor")
    @SneakyThrows
    private void hanlderQtr(List<Dept> deptList, String dataDate, Integer type) {
        LocalDate localDate = LocalDateTimeUtil.parseDate(dataDate, DatePattern.NORM_DATE_PATTERN);
        List<RStatLoadTypeQPO> result = new ArrayList<>();
        QueryWrapper<RStatHarmonicQPO> queryWrapper = new QueryWrapper<>();
        QueryWrapper<RStatOrgQPO> rStatOrgQPOQueryWrapper = new QueryWrapper<>();
        QueryWrapper<RStatLoadTypeMPO> rStatLoadTypeMPOQueryWrapper = new QueryWrapper<>();

        List<DictData> dataList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.MONITORING_LABELS.getCode()).getData();
        //过滤监测对象类型(陆上风电、分布式光伏、电气化铁路、其他非线性负荷)
        Map<String, DictData> collect = dataList.stream().filter(temp -> Objects.equals(temp.getLevel(), 1)).collect(Collectors.toMap(DictData::getId, dictData -> dictData));

        List<DictData> dataTypeList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.DATA_TYPE.getCode()).getData();
        Map<String, DictData> dataTypeMap = dataTypeList.stream().collect(Collectors.toMap(DictData::getCode, dictData -> dictData));
        ;
        /*获取稳态统计指标*/
        List<DictData> steadyStatis = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.STEADY_STATIS.getCode()).getData();
        Map<String, DictData> steadyStatisMap = steadyStatis.stream().collect(Collectors.toMap(DictData::getCode, dictData -> dictData));
        deptList.forEach(dept -> {


            collect.forEach((m,n)->{
                rStatOrgQPOQueryWrapper.clear();
                rStatOrgQPOQueryWrapper.eq("measurement_type_class",m).
                        eq("org_no",dept.getId()).
                        eq("QUARTER( data_date)", PublicUtil.getNowQuarter (dataDate));
                List<RStatOrgQPO> rStatOrgQPOS = rStatOrgQMapper.selectList(rStatOrgQPOQueryWrapper);

                queryWrapper.clear();
                queryWrapper.eq("org_no",dept.getId()).
                        eq("QUARTER( data_date)", PublicUtil.getNowQuarter (dataDate));
                List<RStatHarmonicQPO> rStatHarmonicQPOS = rStatHarmonicQPOMapper.selectList(queryWrapper);
                rStatLoadTypeMPOQueryWrapper.clear();
                rStatLoadTypeMPOQueryWrapper.eq("org_no",dept.getId()).
                        eq("load_type",m).
                        eq("QUARTER( data_date)", PublicUtil.getNowQuarter (dataDate));
                List<RStatLoadTypeMPO> rStatLoadTypeMPOS = rStatLoadTypeMPOService.getBaseMapper().selectList(rStatLoadTypeMPOQueryWrapper);

                RStatLoadTypeQPO rStatLoadTypeQPO = new RStatLoadTypeQPO();
                rStatLoadTypeQPO.setDataDate(localDate);
                rStatLoadTypeQPO.setOrgNo(dept.getId());
                rStatLoadTypeQPO.setLoadType(m);
                /*2、有效接入监测点数*/
                if(CollectionUtils.isEmpty(rStatOrgQPOS)){
                    rStatLoadTypeQPO.setEffectiveMeasurement(0);
                }else {
                    rStatLoadTypeQPO.setEffectiveMeasurement(rStatOrgQPOS.get(0).getEffectiveMeasurementAccrued());
                }
                /*4、谐波电压超标点数6、谐波电流超标点数*/
                if(CollectionUtils.isEmpty(rStatHarmonicQPOS)){
                    rStatLoadTypeQPO.setVCount(0);
                    rStatLoadTypeQPO.setICount(0);
                    rStatLoadTypeQPO.setINegCount(0);
                    rStatLoadTypeQPO.setUnbalanceCount(0);
                    rStatLoadTypeQPO.setFlickerCount(0);
                }else {
                    Optional<RStatHarmonicQPO> vfirst = rStatHarmonicQPOS.stream().filter(temp -> Objects.equals(temp.getHarmonicType(), steadyStatisMap.get(DicDataEnum.HARMONIC_VOLTAGE).getId())).findFirst();
                    rStatLoadTypeQPO.setVCount(vfirst.isPresent() ? vfirst.get().getOverLimitMeasurementAccrued(): 0);
                    Optional<RStatHarmonicQPO> ifirst = rStatHarmonicQPOS.stream().filter(temp -> Objects.equals(temp.getHarmonicType(), steadyStatisMap.get(DicDataEnum.HARMONIC_CURRENT).getId())).findFirst();
                    rStatLoadTypeQPO.setVCount(ifirst.isPresent() ? ifirst.get().getOverLimitMeasurementAccrued(): 0);
                    Optional<RStatHarmonicQPO> pfirst = rStatHarmonicQPOS.stream().filter(temp -> Objects.equals(temp.getHarmonicType(), steadyStatisMap.get(DicDataEnum.PHASE_VOLTAGE).getId())).findFirst();
                    Optional<RStatHarmonicQPO> nfirst = rStatHarmonicQPOS.stream().filter(temp -> Objects.equals(temp.getHarmonicType(), steadyStatisMap.get(DicDataEnum.NEG_CURRENT).getId())).findFirst();
                    Optional<RStatHarmonicQPO> ffirst = rStatHarmonicQPOS.stream().filter(temp -> Objects.equals(temp.getHarmonicType(), steadyStatisMap.get(DicDataEnum.FLICKER).getId())).findFirst();

                    rStatLoadTypeQPO.setINegCount(nfirst.isPresent() ? nfirst.get().getOverLimitMeasurementAccrued(): 0);
                    rStatLoadTypeQPO.setUnbalanceCount(pfirst.isPresent() ? pfirst.get().getOverLimitMeasurementAccrued(): 0);
                    rStatLoadTypeQPO.setFlickerCount(ffirst.isPresent() ? ffirst.get().getOverLimitMeasurementAccrued(): 0);

                }
                /*3、日均谐波电压超标点占比5、日均谐波电流超标点占比,7、日均三相电压不平衡度超标点占比9、日均负序电流超标点占比11、日均闪变超标点占比*/
                if(CollectionUtils.isEmpty(rStatLoadTypeMPOS)){
                    rStatLoadTypeQPO.setVAverage(0.00);
                    rStatLoadTypeQPO.setIAverage(0.00);
                    rStatLoadTypeQPO.setUnbalanceAverage(0.00);
                    rStatLoadTypeQPO.setFlickerAverage(0.00);
                    rStatLoadTypeQPO.setINegAverage(0.00);
                }else {
                    double vavg = rStatLoadTypeMPOS.stream().mapToDouble(RStatLoadTypeMPO::getVAverage).average().getAsDouble();
                    rStatLoadTypeQPO.setVAverage(vavg);
                    double iavge = rStatLoadTypeMPOS.stream().mapToDouble(RStatLoadTypeMPO::getIAverage).average().getAsDouble();
                    rStatLoadTypeQPO.setIAverage(iavge);
                    double uavge = rStatLoadTypeMPOS.stream().mapToDouble(RStatLoadTypeMPO::getUnbalanceAverage).average().getAsDouble();
                    rStatLoadTypeQPO.setUnbalanceAverage(uavge);
                    double inavge = rStatLoadTypeMPOS.stream().mapToDouble(RStatLoadTypeMPO::getINegAverage).average().getAsDouble();
                    double favg = rStatLoadTypeMPOS.stream().mapToDouble(RStatLoadTypeMPO::getFlickerAverage).average().getAsDouble();
                    rStatLoadTypeQPO.setFlickerAverage(favg);
                    rStatLoadTypeQPO.setINegAverage(inavge);

                }
                result.add(rStatLoadTypeQPO);
            });
        });
        if(CollectionUtil.isNotEmpty(result)){
            rStatLoadTypeQPOService.saveOrUpdateBatchByMultiId(result,500);
        }

    }

    @Transactional(rollbackFor = {Exception.class})
    @Async("asyncExecutor")
    @SneakyThrows
    private void hanlderMonth(List<Dept> deptList, String dataDate, Integer type) {
        LocalDate localDate = LocalDateTimeUtil.parseDate(dataDate, DatePattern.NORM_DATE_PATTERN);
        List<RStatLoadTypeMPO> result = new ArrayList<>();
        QueryWrapper<RStatHarmonicMPO> queryWrapper = new QueryWrapper<>();
        QueryWrapper<RStatOrgMPO> rStatOrgMPOQueryWrapper = new QueryWrapper<>();
        QueryWrapper<RStatLoadTypeDPO> rStatLoadTypeDPOQueryWrapper = new QueryWrapper<>();

        List<DictData> dataList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.MONITORING_LABELS.getCode()).getData();
        //过滤监测对象类型(陆上风电、分布式光伏、电气化铁路、其他非线性负荷)
        Map<String, DictData> collect = dataList.stream().filter(temp -> Objects.equals(temp.getLevel(), 1)).collect(Collectors.toMap(DictData::getId, dictData -> dictData));

        List<DictData> dataTypeList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.DATA_TYPE.getCode()).getData();
        Map<String, DictData> dataTypeMap = dataTypeList.stream().collect(Collectors.toMap(DictData::getCode, dictData -> dictData));
        ;
        /*获取稳态统计指标*/
        List<DictData> steadyStatis = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.STEADY_STATIS.getCode()).getData();
        Map<String, DictData> steadyStatisMap = steadyStatis.stream().collect(Collectors.toMap(DictData::getCode, dictData -> dictData));
        deptList.forEach(dept -> {


            collect.forEach((m,n)->{
                rStatOrgMPOQueryWrapper.clear();
                rStatOrgMPOQueryWrapper.eq("measurement_type_class",m).
                        eq("org_no",dept.getId()).
                        eq("DATE_FORMAT( data_date ,'%Y-%m')",dataDate.substring(0, 7));
                List<RStatOrgMPO> rStatOrgMPOS = rStatOrgMMapper.selectList(rStatOrgMPOQueryWrapper);

                queryWrapper.clear();
                queryWrapper.eq("org_no",dept.getId()).
                        eq("DATE_FORMAT( data_date ,'%Y-%m')",dataDate.substring(0, 7));
                List<RStatHarmonicMPO> rStatHarmonicMPOS = rStatHarmonicMPOMapper.selectList(queryWrapper);
                rStatLoadTypeDPOQueryWrapper.clear();
                rStatLoadTypeDPOQueryWrapper.eq("org_no",dept.getId()).
                        eq("load_type",m).
                        eq("DATE_FORMAT( data_date ,'%Y-%m')",dataDate.substring(0, 7));
                List<RStatLoadTypeDPO> rStatLoadTypeDPOS = rStatLoadTypeDPOService.getBaseMapper().selectList(rStatLoadTypeDPOQueryWrapper);

                RStatLoadTypeMPO rStatLoadTypeMPO = new RStatLoadTypeMPO();
                rStatLoadTypeMPO.setDataDate(localDate);
                rStatLoadTypeMPO.setOrgNo(dept.getId());
                rStatLoadTypeMPO.setLoadType(m);
                /*2、有效接入监测点数*/
                if(CollectionUtils.isEmpty(rStatOrgMPOS)){
                    rStatLoadTypeMPO.setEffectiveMeasurement(0);
                }else {
                    rStatLoadTypeMPO.setEffectiveMeasurement(rStatOrgMPOS.get(0).getEffectiveMeasurementAccrued());
                }
                /*4、谐波电压超标点数6、谐波电流超标点数*/
                if(CollectionUtils.isEmpty(rStatHarmonicMPOS)){
                    rStatLoadTypeMPO.setVCount(0);
                    rStatLoadTypeMPO.setICount(0);
                    rStatLoadTypeMPO.setINegCount(0);
                    rStatLoadTypeMPO.setUnbalanceCount(0);
                    rStatLoadTypeMPO.setFlickerCount(0);
                }else {
                    Optional<RStatHarmonicMPO> vfirst = rStatHarmonicMPOS.stream().filter(temp -> Objects.equals(temp.getHarmonicType(), steadyStatisMap.get(DicDataEnum.HARMONIC_VOLTAGE).getId())).findFirst();
                    rStatLoadTypeMPO.setVCount(vfirst.isPresent() ? vfirst.get().getOverLimitMeasurementAccrued(): 0);
                    Optional<RStatHarmonicMPO> ifirst = rStatHarmonicMPOS.stream().filter(temp -> Objects.equals(temp.getHarmonicType(), steadyStatisMap.get(DicDataEnum.HARMONIC_CURRENT).getId())).findFirst();
                    rStatLoadTypeMPO.setVCount(ifirst.isPresent() ? ifirst.get().getOverLimitMeasurementAccrued(): 0);
                    Optional<RStatHarmonicMPO> pfirst = rStatHarmonicMPOS.stream().filter(temp -> Objects.equals(temp.getHarmonicType(), steadyStatisMap.get(DicDataEnum.PHASE_VOLTAGE).getId())).findFirst();
                    Optional<RStatHarmonicMPO> nfirst = rStatHarmonicMPOS.stream().filter(temp -> Objects.equals(temp.getHarmonicType(), steadyStatisMap.get(DicDataEnum.NEG_CURRENT).getId())).findFirst();
                    Optional<RStatHarmonicMPO> ffirst = rStatHarmonicMPOS.stream().filter(temp -> Objects.equals(temp.getHarmonicType(), steadyStatisMap.get(DicDataEnum.FLICKER).getId())).findFirst();

                    rStatLoadTypeMPO.setINegCount(nfirst.isPresent() ? nfirst.get().getOverLimitMeasurementAccrued(): 0);
                    rStatLoadTypeMPO.setUnbalanceCount(pfirst.isPresent() ? pfirst.get().getOverLimitMeasurementAccrued(): 0);
                    rStatLoadTypeMPO.setFlickerCount(ffirst.isPresent() ? ffirst.get().getOverLimitMeasurementAccrued(): 0);

                }
                /*3、日均谐波电压超标点占比5、日均谐波电流超标点占比,7、日均三相电压不平衡度超标点占比9、日均负序电流超标点占比11、日均闪变超标点占比*/
                if(CollectionUtils.isEmpty(rStatLoadTypeDPOS)){
                    rStatLoadTypeMPO.setVAverage(0.00);
                    rStatLoadTypeMPO.setIAverage(0.00);
                    rStatLoadTypeMPO.setUnbalanceAverage(0.00);
                    rStatLoadTypeMPO.setFlickerAverage(0.00);
                    rStatLoadTypeMPO.setINegAverage(0.00);
                }else {
                    double vavg = rStatLoadTypeDPOS.stream().mapToDouble(RStatLoadTypeDPO::getVRatio).average().getAsDouble();
                    rStatLoadTypeMPO.setVAverage(vavg);
                    double iavge = rStatLoadTypeDPOS.stream().mapToDouble(RStatLoadTypeDPO::getIRatio).average().getAsDouble();
                    rStatLoadTypeMPO.setIAverage(iavge);
                    double uavge = rStatLoadTypeDPOS.stream().mapToDouble(RStatLoadTypeDPO::getUnbalanceRatio).average().getAsDouble();
                    rStatLoadTypeMPO.setUnbalanceAverage(uavge);
                    double inavge = rStatLoadTypeDPOS.stream().mapToDouble(RStatLoadTypeDPO::getINegRatio).average().getAsDouble();
                    double favg = rStatLoadTypeDPOS.stream().mapToDouble(RStatLoadTypeDPO::getFlickerRatio).average().getAsDouble();
                    rStatLoadTypeMPO.setFlickerAverage(favg);
                    rStatLoadTypeMPO.setINegAverage(inavge);

                }
                result.add(rStatLoadTypeMPO);
            });
        });
        if(CollectionUtil.isNotEmpty(result)){
            rStatLoadTypeMPOService.saveOrUpdateBatchByMultiId(result,500);
        }


    }

    @Transactional(rollbackFor = {Exception.class})
    @Async("asyncExecutor")
    @SneakyThrows
    private void hanlderDay(List<Dept> deptList, String dataDate, Integer type) {
        LocalDate localDate = LocalDateTimeUtil.parseDate(dataDate, DatePattern.NORM_DATE_PATTERN);
        List<RStatLoadTypeDPO> result = new ArrayList<>();
        List<DictData> dataList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.MONITORING_LABELS.getCode()).getData();
        //过滤监测对象类型(陆上风电、分布式光伏、电气化铁路、其他非线性负荷)
        Map<String, DictData> collect = dataList.stream().filter(temp -> Objects.equals(temp.getLevel(), 1)).collect(Collectors.toMap(DictData::getId, dictData -> dictData));

        List<DictData> dataTypeList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.DATA_TYPE.getCode()).getData();
        Map<String, DictData> dataTypeMap = dataTypeList.stream().collect(Collectors.toMap(DictData::getCode, dictData -> dictData));
        ;
        /*获取稳态统计指标*/
        List<DictData> steadyStatis = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.STEADY_STATIS.getCode()).getData();
        Map<String, DictData> steadyStatisMap = steadyStatis.stream().collect(Collectors.toMap(DictData::getCode, dictData -> dictData));
        deptList.forEach(dept -> {


                collect.forEach((m,n)->{
                    RStatLoadTypeDPO rStatLoadTypeDPO = new RStatLoadTypeDPO();
                    rStatLoadTypeDPO.setDataDate(localDate);
                    rStatLoadTypeDPO.setOrgNo(dept.getId());
                    rStatLoadTypeDPO.setLoadType(m);
                    /*只计算谐波电压、谐波电流、三相电压不平衡度、负序电流/闪变*/
                    RStatHarmonicDPO one = rStatHarmonicDPOService.lambdaQuery().eq(RStatHarmonicDPO::getOrgNo, dept.getId()).
                            eq(RStatHarmonicDPO::getDataDate, localDate).
                            eq(RStatHarmonicDPO::getMeasurementTypeClass, m).
                            eq(RStatHarmonicDPO::getDataType, dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId()).
                            eq(RStatHarmonicDPO::getHarmonicType, steadyStatisMap.get(DicDataEnum.HARMONIC_VOLTAGE).getId()).one();
                    if(Objects.nonNull(one)){
                        rStatLoadTypeDPO.setVRatio(one.getOverLimitMeasurementRatio());
                    }else {
                        rStatLoadTypeDPO.setVRatio(0.00);
                    }
                    RStatHarmonicDPO two = rStatHarmonicDPOService.lambdaQuery().eq(RStatHarmonicDPO::getOrgNo, dept.getId()).
                            eq(RStatHarmonicDPO::getDataDate, localDate).
                            eq(RStatHarmonicDPO::getMeasurementTypeClass, m).
                            eq(RStatHarmonicDPO::getDataType, dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId()).
                            eq(RStatHarmonicDPO::getHarmonicType, steadyStatisMap.get(DicDataEnum.HARMONIC_CURRENT).getId()).one();
                    if(Objects.nonNull(two)){
                        rStatLoadTypeDPO.setIRatio(two.getOverLimitMeasurementRatio());
                    }else {
                        rStatLoadTypeDPO.setIRatio(0.00);
                    }
                    RStatHarmonicDPO three = rStatHarmonicDPOService.lambdaQuery().eq(RStatHarmonicDPO::getOrgNo, dept.getId()).
                            eq(RStatHarmonicDPO::getDataDate, localDate).
                            eq(RStatHarmonicDPO::getMeasurementTypeClass, m).
                            eq(RStatHarmonicDPO::getDataType, dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId()).
                            eq(RStatHarmonicDPO::getHarmonicType, steadyStatisMap.get(DicDataEnum.PHASE_VOLTAGE).getId()).one();
                    if(Objects.nonNull(three)){
                        rStatLoadTypeDPO.setUnbalanceRatio(three.getOverLimitMeasurementRatio());
                    }else {
                        rStatLoadTypeDPO.setUnbalanceRatio(0.00);
                    }
                    RStatHarmonicDPO four = rStatHarmonicDPOService.lambdaQuery().eq(RStatHarmonicDPO::getOrgNo, dept.getId()).
                            eq(RStatHarmonicDPO::getDataDate, localDate).
                            eq(RStatHarmonicDPO::getMeasurementTypeClass, m).
                            eq(RStatHarmonicDPO::getDataType, dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId()).
                            eq(RStatHarmonicDPO::getHarmonicType, steadyStatisMap.get(DicDataEnum.NEG_CURRENT).getId()).one();
                    if(Objects.nonNull(four)){
                        rStatLoadTypeDPO.setINegRatio(four.getOverLimitMeasurementRatio());
                    }else {
                        rStatLoadTypeDPO.setINegRatio(0.00);
                    }
                    RStatHarmonicDPO five = rStatHarmonicDPOService.lambdaQuery().eq(RStatHarmonicDPO::getOrgNo, dept.getId()).
                            eq(RStatHarmonicDPO::getDataDate, localDate).
                            eq(RStatHarmonicDPO::getMeasurementTypeClass, m).
                            eq(RStatHarmonicDPO::getDataType, dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId()).
                            eq(RStatHarmonicDPO::getHarmonicType, steadyStatisMap.get(DicDataEnum.FLICKER).getId()).one();
                    if(Objects.nonNull(five)){
                        rStatLoadTypeDPO.setFlickerRatio(five.getOverLimitMeasurementRatio());
                    }else {
                        rStatLoadTypeDPO.setFlickerRatio(0.00);
                    }
                    result.add(rStatLoadTypeDPO);
                });
        });

        if(CollectionUtil.isNotEmpty(result)){
            rStatLoadTypeDPOService.saveOrUpdateBatchByMultiId(result,500);
        }


    }

}
