package com.njcn.prepare.harmonic.service.mysql.Impl.area;

import cn.hutool.core.collection.CollUtil;
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.biz.pojo.dto.LineDevGetDTO;
import com.njcn.harmonic.pojo.po.*;
import com.njcn.prepare.harmonic.mapper.mysql.area.RStatOrgDMapper;
import com.njcn.prepare.harmonic.mapper.mysql.area.RStatOrgMMapper;
import com.njcn.prepare.harmonic.mapper.mysql.area.RStatOrgQMapper;
import com.njcn.prepare.harmonic.mapper.mysql.area.RStatOrgYMapper;
import com.njcn.prepare.harmonic.mapper.mysql.line.*;
import com.njcn.prepare.harmonic.pojo.bo.CalculatedParam;
import com.njcn.prepare.harmonic.pojo.mysql.ROperatingMonitorDPO;
import com.njcn.prepare.harmonic.pojo.mysql.po.*;
import com.njcn.prepare.harmonic.service.mysql.area.*;
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 lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Description:
 * Date: 2023/5/26  15:41【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */

@Service
@RequiredArgsConstructor
public class RStatHarmonicServiceImpl implements RStatHarmonicService {

    private final RStatHarmonicDPOService rStatHarmonicDPOService;
    private final RStatHarmonicMPOService rStatHarmonicMPOService;
    private final RStatHarmonicQPOService rStatHarmonicQPOService;
    private final RStatHarmonicYPOService rStatHarmonicYPOService;


    private final RMpPartHarmonicDetailDMapper rMpPartHarmonicDetailDMapper;
    private final RMpSurplusHarmonicDetailDMapper rMpSurplusHarmonicDetailDMapper;
    private final RStatOrgDMapper rStatOrgDMapper;

    private final RMpPartHarmonicDetailMMapper rMpPartHarmonicDetailMMapper;
    private final RMpSurplusHarmonicDetailMMapper rMpSurplusHarmonicDetailMMapper;
    private final RStatOrgMMapper rStatOrgMMapper;
    private final RStatOrgQMapper rStatOrgQMapper;
    private final RStatOrgYMapper rStatOrgYMapper;
    private final ROperatingMonitorDMapper rOperatingMonitorDMapper;




    private final DicDataFeignClient dicDataFeignClient;


    @Override
    public void handleDay(CalculatedParam<DeptGetChildrenMoreDTO> calculatedParam) {
        List<RStatHarmonicDPO> rStatHarmonicDPOList = new ArrayList<>();
        LocalDate localDate = LocalDateTimeUtil.parseDate(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN);

        /*获取监测点类型类型*/
        QueryWrapper<ROperatingMonitorDPO> rOperatingMonitorDPOQueryWrapper = new QueryWrapper<>();
        QueryWrapper<RMpPartHarmonicDetailD> queryWrapper = new QueryWrapper<>();
        QueryWrapper<RMpSurplusHarmonicDetailD> rMpSurplusHarmonicDetailDQueryWrapper = new QueryWrapper<>();
        QueryWrapper<RStatOrgDPO> rStatOrgDPOQueryWrapper = new QueryWrapper<>();
        DecimalFormat df = new DecimalFormat("0.00000");

        /*获取稳态统计指标*/
        List<DictData> steadyStatis = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.STEADY_STATIS.getCode()).getData();
        /*获取数据类型*/

        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> lineSortList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.LINE_SORT.getCode()).getData();
        Map<String, DictData> lineSortMap = lineSortList.stream().collect(Collectors.toMap(DictData::getCode, dictData -> dictData));


        calculatedParam.getIdList().forEach(deptGetChildrenMoreDTO -> {

            /*主网*/
            if(!CollectionUtils.isEmpty(deptGetChildrenMoreDTO.getLineBaseList())){
                List<LineDevGetDTO> lineBaseList = deptGetChildrenMoreDTO.getLineBaseList();
                Map<String, List<LineDevGetDTO>> collect = lineBaseList.stream().collect(Collectors.groupingBy(LineDevGetDTO::getLineTag));
                collect.forEach((k, v)->{
                    List<String> collect1 = v.stream().map(LineDevGetDTO::getPointId).collect(Collectors.toList());
                    /*todo 取有效监测点进行匹配*/
                    rOperatingMonitorDPOQueryWrapper.clear();
                    rOperatingMonitorDPOQueryWrapper.select("measurement_point_id").
                            in("measurement_point_id",collect1).
                            gt("effective_minute_count",0).
                            eq("data_date",localDate);
                    List<ROperatingMonitorDPO> rOperatingMonitorDPOS = rOperatingMonitorDMapper.selectList(rOperatingMonitorDPOQueryWrapper);
                    List<String> effLineIds = rOperatingMonitorDPOS.stream().map(ROperatingMonitorDPO::getMeasurementPointId).distinct().collect(Collectors.toList());


                    if(CollectionUtil.isEmpty(effLineIds)){
                        return;
                    }

                    queryWrapper.clear();
                    queryWrapper.select("is_freq","is_v_dev","is_v","is_i","is_unbalance","is_i_neg").
                            in("measurement_point_id",effLineIds).
                            eq("data_date",localDate);
                    List<RMpPartHarmonicDetailD> rMpPartHarmonicDetailDS = rMpPartHarmonicDetailDMapper.selectList(queryWrapper);

                    rMpSurplusHarmonicDetailDQueryWrapper.clear();
                    rMpSurplusHarmonicDetailDQueryWrapper.select("is_flicker","is_inuharm").
                            in("measurement_point_id",effLineIds).
                            eq("data_date",localDate);
                    List<RMpSurplusHarmonicDetailD> rMpSurplusHarmonicDetailDS = rMpSurplusHarmonicDetailDMapper.selectList(rMpSurplusHarmonicDetailDQueryWrapper);

                    rStatOrgDPOQueryWrapper.clear();
                    rStatOrgDPOQueryWrapper.select("effective_measurement").
                            eq("org_no",deptGetChildrenMoreDTO.getUnitId()).
                            eq("measurement_type_class",k).
                            eq("data_type",dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId()).
                            eq("data_date",localDate);
                    RStatOrgDPO rStatOrgDPO = rStatOrgDMapper.selectOne(rStatOrgDPOQueryWrapper);

                    steadyStatis.forEach(dictData -> {
                        if(Objects.equals(dictData.getCode(),DicDataEnum.TOTAL_INDICATOR.getCode())){
                            return;
                        }
                        RStatHarmonicDPO rStatHarmonicDPO = new RStatHarmonicDPO();
                        rStatHarmonicDPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                        rStatHarmonicDPO.setDataDate(localDate);
                        rStatHarmonicDPO.setHarmonicType(dictData.getId());
                        rStatHarmonicDPO.setDataType(dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId());
                        rStatHarmonicDPO.setMeasurementTypeClass(k);
                        rStatHarmonicDPO.setOverLimitMeasurement(getSteadyStatisData(dictData,rMpPartHarmonicDetailDS,rMpSurplusHarmonicDetailDS));
                        if(rStatOrgDPO.getEffectiveMeasurement()==0){
                            rStatHarmonicDPO.setOverLimitMeasurementRatio(0.00);

                        }else{
                            String value = df.format((float)rStatHarmonicDPO.getOverLimitMeasurement() / rStatOrgDPO.getEffectiveMeasurement());
                            rStatHarmonicDPO.setOverLimitMeasurementRatio(Double.parseDouble(value));
                        }
                        rStatHarmonicDPOList.add(rStatHarmonicDPO);
                    });

                });

            }
            /*配网*/
            if(!CollectionUtils.isEmpty(deptGetChildrenMoreDTO.getPwMonitorIds())) {
                List<LineDevGetDTO> pwMonitorIds = deptGetChildrenMoreDTO.getPwMonitorIds();
                Map<Integer, List<LineDevGetDTO>> pwMonitorMap = pwMonitorIds.stream().collect(Collectors.groupingBy(LineDevGetDTO::getLineType));
                pwMonitorMap.forEach((k, v) -> {
                    List<String> collect1 = v.stream().map(LineDevGetDTO::getPointId).collect(Collectors.toList());

                    queryWrapper.clear();
                    queryWrapper.select("is_freq", "is_v_dev", "is_v", "is_i", "is_unbalance", "is_i_neg").
                            in("measurement_point_id", collect1).
                            eq("data_date",localDate);
                    List<RMpPartHarmonicDetailD> rMpPartHarmonicDetailDS = rMpPartHarmonicDetailDMapper.selectList(queryWrapper);

                    rMpSurplusHarmonicDetailDQueryWrapper.clear();
                    rMpSurplusHarmonicDetailDQueryWrapper.select("is_flicker", "is_inuharm").
                            in("measurement_point_id", collect1).
                            eq("data_date",localDate);
                    List<RMpSurplusHarmonicDetailD> rMpSurplusHarmonicDetailDS = rMpSurplusHarmonicDetailDMapper.selectList(rMpSurplusHarmonicDetailDQueryWrapper);

                    rStatOrgDPOQueryWrapper.clear();
                    rStatOrgDPOQueryWrapper.select("effective_measurement").
                            eq("org_no", deptGetChildrenMoreDTO.getUnitId()).
                            eq("measurement_type_class", getmeasurementType(k, lineSortMap)).
                            eq("data_type", dataTypeMap.get(DicDataEnum.DISTRIBUTION_POINT.getCode()).getId()).
                            eq("data_date",localDate);
                    RStatOrgDPO rStatOrgDPO = rStatOrgDMapper.selectOne(rStatOrgDPOQueryWrapper);

                    steadyStatis.forEach(dictData -> {
                        if (Objects.equals(dictData.getCode(), DicDataEnum.NEG_CURRENT.getCode())) {
                            return;
                        }
                        RStatHarmonicDPO rStatHarmonicDPO = new RStatHarmonicDPO();
                        rStatHarmonicDPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                        rStatHarmonicDPO.setDataDate(localDate);
                        rStatHarmonicDPO.setHarmonicType(dictData.getId());
                        rStatHarmonicDPO.setDataType(dataTypeMap.get(DicDataEnum.DISTRIBUTION_POINT.getCode()).getId());
                        rStatHarmonicDPO.setMeasurementTypeClass(getmeasurementType(k, lineSortMap));
                        rStatHarmonicDPO.setOverLimitMeasurement(getSteadyStatisData(dictData, rMpPartHarmonicDetailDS, rMpSurplusHarmonicDetailDS));
                        if (rStatOrgDPO.getEffectiveMeasurement() == 0) {
                            rStatHarmonicDPO.setOverLimitMeasurementRatio(0.00);

                        } else {
                            String value = df.format(rStatHarmonicDPO.getOverLimitMeasurement() / rStatOrgDPO.getEffectiveMeasurement());
                            rStatHarmonicDPO.setOverLimitMeasurementRatio(Double.parseDouble(value));
                        }
                        rStatHarmonicDPOList.add(rStatHarmonicDPO);
                    });

                });
            }

        });
        if(!CollectionUtils.isEmpty(rStatHarmonicDPOList)){
            rStatHarmonicDPOService.saveOrUpdateBatchByMultiId(rStatHarmonicDPOList,500);
        }
    }

    @Override
    public void handleMonth(CalculatedParam<DeptGetChildrenMoreDTO> calculatedParam) {
        List<RStatHarmonicMPO> rStatHarmonicMPOList = new ArrayList<>();
        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);

        QueryWrapper<ROperatingMonitorDPO> rOperatingMonitorDPOQueryWrapper = new QueryWrapper<>();
        QueryWrapper<RMpPartHarmonicDetailM> queryWrapper = new QueryWrapper<>();
        QueryWrapper<RMpSurplusHarmonicDetailM> rMpSurplusHarmonicDetailMQueryWrapper = new QueryWrapper<>();
        QueryWrapper<RStatOrgMPO> rStatOrgMPOQueryWrapper = new QueryWrapper<>();
        QueryWrapper<RStatHarmonicDPO> harmonicDPOQueryWrapper = new QueryWrapper<>();

        DecimalFormat df = new DecimalFormat("0.00000");

        /*获取稳态统计指标*/
        List<DictData> steadyStatis = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.STEADY_STATIS.getCode()).getData();
        /*获取数据类型*/

        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> lineSortList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.LINE_SORT.getCode()).getData();
        Map<String, DictData> lineSortMap = lineSortList.stream().collect(Collectors.toMap(DictData::getCode, dictData -> dictData));


        calculatedParam.getIdList().forEach(deptGetChildrenMoreDTO -> {

            /*主网*/
            if (!CollectionUtils.isEmpty(deptGetChildrenMoreDTO.getLineBaseList())) {
                List<LineDevGetDTO> lineBaseList = deptGetChildrenMoreDTO.getLineBaseList();
                Map<String, List<LineDevGetDTO>> collect = lineBaseList.stream().collect(Collectors.groupingBy(LineDevGetDTO::getLineTag));
                collect.forEach((k, v) -> {
                    List<String> collect1 = v.stream().map(LineDevGetDTO::getPointId).collect(Collectors.toList());
                    /*todo 取有效监测点进行匹配*/
                    rOperatingMonitorDPOQueryWrapper.clear();
                    rOperatingMonitorDPOQueryWrapper.select("measurement_point_id").
                            in("measurement_point_id",collect1).
                            gt("effective_minute_count",0).
                            between("data_date",begin,end);
                    List<ROperatingMonitorDPO> rOperatingMonitorDPOS = rOperatingMonitorDMapper.selectList(rOperatingMonitorDPOQueryWrapper);
                    List<String> effLineIds = rOperatingMonitorDPOS.stream().map(ROperatingMonitorDPO::getMeasurementPointId).distinct().collect(Collectors.toList());

                    if(CollUtil.isEmpty(effLineIds)){
                        //目前部门下面没有有效监测点，先做调过处理
                        return;
                    }

                    queryWrapper.clear();
                    queryWrapper.select("measurement_point_id","freq_over_day", "v_dev_over_day", "v_over_day", "i_over_day", "unbalance_over_day", "i_neg_over_day").
                            in("measurement_point_id", effLineIds).
                            between("data_date",begin,end);
                    List<RMpPartHarmonicDetailM> rMpPartHarmonicDetailMS = rMpPartHarmonicDetailMMapper.selectList(queryWrapper);

                    rMpSurplusHarmonicDetailMQueryWrapper.clear();
                    rMpSurplusHarmonicDetailMQueryWrapper.select("measurement_point_id","flicker_over_day", "inuharm_over_day").
                            in("measurement_point_id", effLineIds).
                            between("data_date",begin,end);
                    List<RMpSurplusHarmonicDetailM> rMpSurplusHarmonicDetailMS = rMpSurplusHarmonicDetailMMapper.selectList(rMpSurplusHarmonicDetailMQueryWrapper);

                    rStatOrgMPOQueryWrapper.clear();
                    rStatOrgMPOQueryWrapper.select("effective_measurement_accrued").
                            eq("org_no", deptGetChildrenMoreDTO.getUnitId()).
                            eq("measurement_type_class", k).
                            eq("data_type", dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId()).
                            between("data_date",begin,end);
                    RStatOrgMPO rStatOrgMPO = rStatOrgMMapper.selectOne(rStatOrgMPOQueryWrapper);



                    steadyStatis.forEach(dictData -> {
                        if (Objects.equals(dictData.getCode(), DicDataEnum.TOTAL_INDICATOR.getCode())) {
                            return;
                        }
                        RStatHarmonicMPO rStatHarmonicMPO = new RStatHarmonicMPO();
                        rStatHarmonicMPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                        rStatHarmonicMPO.setDataDate(localDate);
                        rStatHarmonicMPO.setHarmonicType(dictData.getId());
                        rStatHarmonicMPO.setDataType(dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId());
                        rStatHarmonicMPO.setMeasurementTypeClass(k);
                        /**4、累计超标监测点数*/
                        rStatHarmonicMPO.setOverLimitMeasurementAccrued(getOverLimitMeasurementAccruedTime(dictData, rMpPartHarmonicDetailMS, rMpSurplusHarmonicDetailMS));
                        /*3、日均超标监测点数,6、日均超标监测点数占比*/
                        harmonicDPOQueryWrapper.clear();
                        harmonicDPOQueryWrapper.eq("org_no", deptGetChildrenMoreDTO.getUnitId()).
                                eq("measurement_type_class", k).
                                eq("data_type", dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId()).
                                eq("harmonic_type", dictData.getId()).
                                between("data_date",begin,end);
                        List<RStatHarmonicDPO> rStatHarmonicDPOList = rStatHarmonicDPOService.list(harmonicDPOQueryWrapper);
                        double asDouble = rStatHarmonicDPOList.stream().mapToDouble(RStatHarmonicDPO::getOverLimitMeasurement).average().orElse(0.00);
                        asDouble = BigDecimal.valueOf(asDouble).setScale(5,   BigDecimal.ROUND_HALF_UP).doubleValue();
                        double asDouble1 = rStatHarmonicDPOList.stream().mapToDouble(RStatHarmonicDPO::getOverLimitMeasurementRatio).average().orElse(0.00);
                        asDouble1 = BigDecimal.valueOf(asDouble1).setScale(5,   BigDecimal.ROUND_HALF_UP).doubleValue();
                        rStatHarmonicMPO.setOverLimitMeasurementAverage(asDouble);
                        Integer sum = rStatHarmonicDPOList.stream().mapToInt(RStatHarmonicDPO::getOverLimitMeasurement).sum();
                        rStatHarmonicMPO.setOverLimitMeasurementRatioAverage(asDouble1);

                        int i = BigDecimal.valueOf(asDouble1).setScale(0, BigDecimal.ROUND_HALF_UP).intValue();

                        rStatHarmonicMPO.setOverDay(sum);

                        if (rStatOrgMPO.getEffectiveMeasurementAccrued() == 0) {
                            //todo为0时如何设值
                            rStatHarmonicMPO.setOverLimitMeasurementRatioAccrued(0.00);

                        } else {
                            String value = df.format((float) rStatHarmonicMPO.getOverLimitMeasurementAccrued() / rStatOrgMPO.getEffectiveMeasurementAccrued());
                            rStatHarmonicMPO.setOverLimitMeasurementRatioAccrued(Double.parseDouble(value));
                        }
                        rStatHarmonicMPOList.add(rStatHarmonicMPO);
                    });

                });

            }
            /*配网*/
            if(CollectionUtil.isNotEmpty(deptGetChildrenMoreDTO.getPwMonitorIds())){
                List<LineDevGetDTO> pwMonitorIds = deptGetChildrenMoreDTO.getPwMonitorIds();
                Map<Integer, List<LineDevGetDTO>> collect = pwMonitorIds.stream().collect(Collectors.groupingBy(LineDevGetDTO::getLineType));
                collect.forEach((k, v) -> {
                    List<String> collect1 = v.stream().map(LineDevGetDTO::getPointId).collect(Collectors.toList());

                    queryWrapper.clear();
                    queryWrapper.select("measurement_point_id","freq_over_day", "v_dev_over_day", "v_over_day", "i_over_day", "unbalance_over_day", "i_neg_over_day").
                            in("measurement_point_id", collect1).
                            between("data_date",begin,end);
                    List<RMpPartHarmonicDetailM> rMpPartHarmonicDetailMS = rMpPartHarmonicDetailMMapper.selectList(queryWrapper);

                    rMpSurplusHarmonicDetailMQueryWrapper.clear();
                    rMpSurplusHarmonicDetailMQueryWrapper.select("measurement_point_id","flicker_over_day", "inuharm_over_day").
                            in("measurement_point_id", collect1).
                            between("data_date",begin,end);
                    List<RMpSurplusHarmonicDetailM> rMpSurplusHarmonicDetailMS = rMpSurplusHarmonicDetailMMapper.selectList(rMpSurplusHarmonicDetailMQueryWrapper);

                    rStatOrgMPOQueryWrapper.clear();
                    rStatOrgMPOQueryWrapper.select("effective_measurement_accrued").
                            eq("org_no", deptGetChildrenMoreDTO.getUnitId()).
                            eq("measurement_type_class", getmeasurementType(k, lineSortMap)).
                            eq("data_type", dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId()).
                            between("data_date",begin,end);
                    RStatOrgMPO rStatOrgMPO = rStatOrgMMapper.selectOne(rStatOrgMPOQueryWrapper);



                    steadyStatis.forEach(dictData -> {
                        if (Objects.equals(dictData.getCode(), DicDataEnum.TOTAL_INDICATOR.getCode())) {
                            return;
                        }
                        RStatHarmonicMPO rStatHarmonicMPO = new RStatHarmonicMPO();
                        rStatHarmonicMPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                        rStatHarmonicMPO.setDataDate(localDate);
                        rStatHarmonicMPO.setHarmonicType(dictData.getId());
                        rStatHarmonicMPO.setDataType(dataTypeMap.get(DicDataEnum.DISTRIBUTION_POINT.getCode()).getId());
                        rStatHarmonicMPO.setMeasurementTypeClass(getmeasurementType(k, lineSortMap));
                        /**4、累计超标监测点数*/
                        rStatHarmonicMPO.setOverLimitMeasurementAccrued(getOverLimitMeasurementAccruedTime(dictData, rMpPartHarmonicDetailMS, rMpSurplusHarmonicDetailMS));
                        /*3、日均超标监测点数,6、日均超标监测点数占比*/
                        harmonicDPOQueryWrapper.clear();
                        harmonicDPOQueryWrapper.eq("org_no", deptGetChildrenMoreDTO.getUnitId()).
                                eq("measurement_type_class", getmeasurementType(k, lineSortMap)).
                                eq("data_type", dataTypeMap.get(DicDataEnum.DISTRIBUTION_POINT.getCode()).getId()).
                                eq("harmonic_type", dictData.getId()).
                                between("data_date",begin,end);
                        List<RStatHarmonicDPO> rStatHarmonicDPOList = rStatHarmonicDPOService.list(harmonicDPOQueryWrapper);
                        double asDouble = rStatHarmonicDPOList.stream().mapToDouble(RStatHarmonicDPO::getOverLimitMeasurement).average().orElse(0.00);
                        asDouble = BigDecimal.valueOf(asDouble).setScale(5,   BigDecimal.ROUND_HALF_UP).doubleValue();
                        double asDouble1 = rStatHarmonicDPOList.stream().mapToDouble(RStatHarmonicDPO::getOverLimitMeasurementRatio).average().orElse(0.00);
                        asDouble1 = BigDecimal.valueOf(asDouble1).setScale(5,   BigDecimal.ROUND_HALF_UP).doubleValue();
                        rStatHarmonicMPO.setOverLimitMeasurementAverage(asDouble);
                        Integer sum = rStatHarmonicDPOList.stream().mapToInt(RStatHarmonicDPO::getOverLimitMeasurement).sum();
                        rStatHarmonicMPO.setOverLimitMeasurementRatioAverage(asDouble1);

                        int i = BigDecimal.valueOf(asDouble1).setScale(0, BigDecimal.ROUND_HALF_UP).intValue();

                        rStatHarmonicMPO.setOverDay(sum);

                        if (rStatOrgMPO.getEffectiveMeasurementAccrued() == 0) {
                            //todo为0时如何设值
                            rStatHarmonicMPO.setOverLimitMeasurementRatioAccrued(0.00);

                        } else {
                            String value = df.format((float) rStatHarmonicMPO.getOverLimitMeasurementAccrued() / rStatOrgMPO.getEffectiveMeasurementAccrued());
                            rStatHarmonicMPO.setOverLimitMeasurementRatioAccrued(Double.parseDouble(value));
                        }
                        rStatHarmonicMPOList.add(rStatHarmonicMPO);
                    });

                });

            }
        });
        if(CollectionUtil.isNotEmpty(rStatHarmonicMPOList)){
            rStatHarmonicMPOService.saveOrUpdateBatchByMultiId(rStatHarmonicMPOList,500);

        }

    }

    @Override
    public void handleQtr(CalculatedParam<DeptGetChildrenMoreDTO> calculatedParam) {
        List<RStatHarmonicQPO> rStatHarmonicQPOList = new ArrayList<>();

        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);

        QueryWrapper<ROperatingMonitorDPO> rOperatingMonitorDPOQueryWrapper = new QueryWrapper<>();
        QueryWrapper<RMpPartHarmonicDetailM> queryWrapper = new QueryWrapper<>();
        QueryWrapper<RMpSurplusHarmonicDetailM> rMpSurplusHarmonicDetailMQueryWrapper = new QueryWrapper<>();
        QueryWrapper<RStatOrgQPO> rStatOrgQPOQueryWrapper = new QueryWrapper<>();
        QueryWrapper<RStatHarmonicMPO> harmonicMPOQueryWrapper = new QueryWrapper<>();

        DecimalFormat df = new DecimalFormat("0.00000");

        /*获取稳态统计指标*/
        List<DictData> steadyStatis = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.STEADY_STATIS.getCode()).getData();
        /*获取数据类型*/

        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> lineSortList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.LINE_SORT.getCode()).getData();
        Map<String, DictData> lineSortMap = lineSortList.stream().collect(Collectors.toMap(DictData::getCode, dictData -> dictData));


        calculatedParam.getIdList().forEach(deptGetChildrenMoreDTO -> {

            /*主网*/
            if (!CollectionUtils.isEmpty(deptGetChildrenMoreDTO.getLineBaseList())) {
                List<LineDevGetDTO> lineBaseList = deptGetChildrenMoreDTO.getLineBaseList();
                Map<String, List<LineDevGetDTO>> collect = lineBaseList.stream().collect(Collectors.groupingBy(LineDevGetDTO::getLineTag));
                collect.forEach((k, v) -> {
                    List<String> collect1 = v.stream().map(LineDevGetDTO::getPointId).collect(Collectors.toList());
                    /*todo 取有效监测点进行匹配*/
                    rOperatingMonitorDPOQueryWrapper.clear();
                    rOperatingMonitorDPOQueryWrapper.select("measurement_point_id").
                            in("measurement_point_id",collect1).
                            gt("effective_minute_count",0).
                            between("data_date",begin,end);
                    List<ROperatingMonitorDPO> rOperatingMonitorDPOS = rOperatingMonitorDMapper.selectList(rOperatingMonitorDPOQueryWrapper);
                    List<String> effLineIds = rOperatingMonitorDPOS.stream().map(ROperatingMonitorDPO::getMeasurementPointId).distinct().collect(Collectors.toList());
                    List<RMpPartHarmonicDetailM> rMpPartHarmonicDetailMS =new ArrayList<>();
                    List<RMpSurplusHarmonicDetailM> rMpSurplusHarmonicDetailMS=new ArrayList<>();
                    if(CollUtil.isNotEmpty(effLineIds)){
                        queryWrapper.clear();
                        queryWrapper.select("measurement_point_id","freq_over_day", "v_dev_over_day", "v_over_day", "i_over_day", "unbalance_over_day", "i_neg_over_day").
                                in("measurement_point_id", effLineIds).
                                between("data_date",begin,end);
                         rMpPartHarmonicDetailMS.addAll(rMpPartHarmonicDetailMMapper.selectList(queryWrapper));
                        rMpSurplusHarmonicDetailMQueryWrapper.clear();
                        rMpSurplusHarmonicDetailMQueryWrapper.select("measurement_point_id","flicker_over_day", "inuharm_over_day").
                                in("measurement_point_id", effLineIds).
                                between("data_date",begin,end);
                        rMpSurplusHarmonicDetailMS.addAll(rMpSurplusHarmonicDetailMMapper.selectList(rMpSurplusHarmonicDetailMQueryWrapper));
                    }
                    rStatOrgQPOQueryWrapper.clear();
                    rStatOrgQPOQueryWrapper.select("effective_measurement_accrued").
                            eq("org_no", deptGetChildrenMoreDTO.getUnitId()).
                            eq("measurement_type_class", k).
                            eq("data_type", dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId()).
                            between("data_date",begin,end);
                    RStatOrgQPO rStatOrgQPO = rStatOrgQMapper.selectOne(rStatOrgQPOQueryWrapper);



                    steadyStatis.forEach(dictData -> {
                        if (Objects.equals(dictData.getCode(), DicDataEnum.TOTAL_INDICATOR.getCode())) {
                            return;
                        }
                        RStatHarmonicQPO rStatHarmonicQPO = new RStatHarmonicQPO();
                        rStatHarmonicQPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                        rStatHarmonicQPO.setDataDate(localDate);
                        rStatHarmonicQPO.setHarmonicType(dictData.getId());
                        rStatHarmonicQPO.setDataType(dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId());
                        rStatHarmonicQPO.setMeasurementTypeClass(k);
                        /**4、累计超标监测点数*/
                        rStatHarmonicQPO.setOverLimitMeasurementAccrued(getOverLimitMeasurementAccruedTime(dictData, rMpPartHarmonicDetailMS, rMpSurplusHarmonicDetailMS));
                        /*3、日均超标监测点数,6、日均超标监测点数占比*/
                        harmonicMPOQueryWrapper.clear();
                        harmonicMPOQueryWrapper.eq("org_no", deptGetChildrenMoreDTO.getUnitId()).
                                eq("measurement_type_class", k).
                                eq("data_type", dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId()).
                                eq("harmonic_type", dictData.getId()).
                                between("data_date",begin,end);
                        List<RStatHarmonicMPO> list = rStatHarmonicMPOService.list(harmonicMPOQueryWrapper);
                        double asDouble = list.stream().mapToDouble(RStatHarmonicMPO::getOverLimitMeasurementAverage).average().orElse(0.00);
                        asDouble = BigDecimal.valueOf(asDouble).setScale(5,   BigDecimal.ROUND_HALF_UP).doubleValue();
                        double asDouble1 = list.stream().mapToDouble(RStatHarmonicMPO::getOverLimitMeasurementRatioAverage).average().orElse(0.00);
                        asDouble1 = BigDecimal.valueOf(asDouble1).setScale(5,   BigDecimal.ROUND_HALF_UP).doubleValue();
                        int sum1 = list.stream().mapToInt(RStatHarmonicMPO::getOverDay).sum();
                        rStatHarmonicQPO.setOverLimitMeasurementAverage(asDouble);

                        rStatHarmonicQPO.setOverLimitMeasurementRatioAverage(asDouble1);


                        if (rStatHarmonicQPO.getOverLimitMeasurementAccrued() == 0) {
                            //todo为0时如何设值
                            rStatHarmonicQPO.setAverageOverDay(0.00);

                        } else {
                            String value = df.format((float) sum1 / rStatHarmonicQPO.getOverLimitMeasurementAccrued());
                            rStatHarmonicQPO.setAverageOverDay(Double.parseDouble(value));
                        }

                        if (rStatOrgQPO.getEffectiveMeasurementAccrued() == 0) {
                            //todo为0时如何设值
                            rStatHarmonicQPO.setOverLimitMeasurementRatioAccrued(0.00);

                        } else {
                            String value = df.format((float) rStatHarmonicQPO.getOverLimitMeasurementAccrued() / rStatOrgQPO.getEffectiveMeasurementAccrued());
                            double v1 = Double.parseDouble(value);
                            rStatHarmonicQPO.setOverLimitMeasurementRatioAccrued(v1);
                        }
                        rStatHarmonicQPOList.add(rStatHarmonicQPO);
                    });

                });

            }
            /*配网*/
            if(CollectionUtil.isNotEmpty(deptGetChildrenMoreDTO.getPwMonitorIds())){
                List<LineDevGetDTO> pwMonitorIds = deptGetChildrenMoreDTO.getPwMonitorIds();
                Map<Integer, List<LineDevGetDTO>> collect = pwMonitorIds.stream().collect(Collectors.groupingBy(LineDevGetDTO::getLineType));
                collect.forEach((k, v) -> {
                    List<String> collect1 = v.stream().map(LineDevGetDTO::getPointId).collect(Collectors.toList());

                    queryWrapper.clear();
                    queryWrapper.select("measurement_point_id","freq_over_day", "v_dev_over_day", "v_over_day", "i_over_day", "unbalance_over_day", "i_neg_over_day").
                            in("measurement_point_id", collect1).
                            between("data_date",begin,end);

                    List<RMpPartHarmonicDetailM> rMpPartHarmonicDetailMS = rMpPartHarmonicDetailMMapper.selectList(queryWrapper);

                    rMpSurplusHarmonicDetailMQueryWrapper.clear();
                    rMpSurplusHarmonicDetailMQueryWrapper.select("measurement_point_id","flicker_over_day", "inuharm_over_day").
                            in("measurement_point_id", collect1).
                            between("data_date",begin,end);
                    List<RMpSurplusHarmonicDetailM> rMpSurplusHarmonicDetailMS = rMpSurplusHarmonicDetailMMapper.selectList(rMpSurplusHarmonicDetailMQueryWrapper);

                    rStatOrgQPOQueryWrapper.clear();
                    rStatOrgQPOQueryWrapper.select("effective_measurement_accrued").
                            eq("org_no", deptGetChildrenMoreDTO.getUnitId()).
                            eq("measurement_type_class", getmeasurementType(k, lineSortMap)).
                            eq("data_type", dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId()).
                            between("data_date",begin,end);
                    RStatOrgQPO rStatOrgQPO = rStatOrgQMapper.selectOne(rStatOrgQPOQueryWrapper);



                    steadyStatis.forEach(dictData -> {
                        if (Objects.equals(dictData.getCode(), DicDataEnum.TOTAL_INDICATOR.getCode())) {
                            return;
                        }
                        RStatHarmonicQPO rStatHarmonicQPO = new RStatHarmonicQPO();
                        rStatHarmonicQPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                        rStatHarmonicQPO.setDataDate(localDate);
                        rStatHarmonicQPO.setHarmonicType(dictData.getId());
                        rStatHarmonicQPO.setDataType(dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId());
                        rStatHarmonicQPO.setMeasurementTypeClass(getmeasurementType(k, lineSortMap));
                        /**4、累计超标监测点数*/
                        rStatHarmonicQPO.setOverLimitMeasurementAccrued(getOverLimitMeasurementAccruedTime(dictData, rMpPartHarmonicDetailMS, rMpSurplusHarmonicDetailMS));
                        /*3、日均超标监测点数,6、日均超标监测点数占比*/
                        harmonicMPOQueryWrapper.clear();
                        harmonicMPOQueryWrapper.eq("org_no", deptGetChildrenMoreDTO.getUnitId()).
                                eq("measurement_type_class", getmeasurementType(k, lineSortMap)).
                                eq("data_type", dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId()).
                                eq("harmonic_type", dictData.getId()).
                                between("data_date",begin,end);
                        List<RStatHarmonicMPO> list = rStatHarmonicMPOService.list(harmonicMPOQueryWrapper);
                        double asDouble = list.stream().mapToDouble(RStatHarmonicMPO::getOverLimitMeasurementAverage).average().orElse(0.00);
                        asDouble = BigDecimal.valueOf(asDouble).setScale(5,   BigDecimal.ROUND_HALF_UP).doubleValue();
                        double asDouble1 = list.stream().mapToDouble(RStatHarmonicMPO::getOverLimitMeasurementRatioAverage).average().orElse(0.00);
                        asDouble1 = BigDecimal.valueOf(asDouble1).setScale(5,   BigDecimal.ROUND_HALF_UP).doubleValue();
                        int sum1 = list.stream().mapToInt(RStatHarmonicMPO::getOverDay).sum();
                        rStatHarmonicQPO.setOverLimitMeasurementAverage(asDouble);

                        rStatHarmonicQPO.setOverLimitMeasurementRatioAverage(asDouble1);


                        if (rStatHarmonicQPO.getOverLimitMeasurementAccrued() == 0) {
                            //todo为0时如何设值
                            rStatHarmonicQPO.setAverageOverDay(0.00);

                        } else {
                            String value = df.format((float) sum1 / rStatHarmonicQPO.getOverLimitMeasurementAccrued());
                            rStatHarmonicQPO.setAverageOverDay(Double.parseDouble(value));
                        }

                        if (rStatOrgQPO.getEffectiveMeasurementAccrued() == 0) {
                            //todo为0时如何设值
                            rStatHarmonicQPO.setOverLimitMeasurementRatioAccrued(0.00);

                        } else {
                            String value = df.format((float) rStatHarmonicQPO.getOverLimitMeasurementAccrued() / rStatOrgQPO.getEffectiveMeasurementAccrued());
                            rStatHarmonicQPO.setOverLimitMeasurementRatioAccrued(Double.parseDouble(value));
                        }
                        rStatHarmonicQPOList.add(rStatHarmonicQPO);
                    });


                });

            }
        });
        if(CollectionUtil.isNotEmpty(rStatHarmonicQPOList)){
            rStatHarmonicQPOService.saveOrUpdateBatchByMultiId(rStatHarmonicQPOList,500);

        }

    }

    @Override
    public void handleYear(CalculatedParam<DeptGetChildrenMoreDTO> calculatedParam) {
        List<RStatHarmonicYPO> rStatHarmonicYPOList = new ArrayList<>();

        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);


        /*获取监测点类型类型*/
        QueryWrapper<ROperatingMonitorDPO> rOperatingMonitorDPOQueryWrapper = new QueryWrapper<>();
        QueryWrapper<RMpPartHarmonicDetailM> queryWrapper = new QueryWrapper<>();
        QueryWrapper<RMpSurplusHarmonicDetailM> rMpSurplusHarmonicDetailMQueryWrapper = new QueryWrapper<>();
        QueryWrapper<RStatOrgYPO> rStatOrgYPOQueryWrapper = new QueryWrapper<>();
        QueryWrapper<RStatHarmonicMPO> harmonicMPOQueryWrapper = new QueryWrapper<>();

        DecimalFormat df = new DecimalFormat("0.00000");

        /*获取稳态统计指标*/
        List<DictData> steadyStatis = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.STEADY_STATIS.getCode()).getData();
        /*获取数据类型*/

        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> lineSortList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.LINE_SORT.getCode()).getData();
        Map<String, DictData> lineSortMap = lineSortList.stream().collect(Collectors.toMap(DictData::getCode, dictData -> dictData));


        calculatedParam.getIdList().forEach(deptGetChildrenMoreDTO -> {

            /*主网*/
            if (!CollectionUtils.isEmpty(deptGetChildrenMoreDTO.getLineBaseList())) {
                List<LineDevGetDTO> lineBaseList = deptGetChildrenMoreDTO.getLineBaseList();
                Map<String, List<LineDevGetDTO>> collect = lineBaseList.stream().collect(Collectors.groupingBy(LineDevGetDTO::getLineTag));
                collect.forEach((k, v) -> {
                    List<String> collect1 = v.stream().map(LineDevGetDTO::getPointId).collect(Collectors.toList());
                    /*todo 取有效监测点进行匹配*/
                    rOperatingMonitorDPOQueryWrapper.clear();
                    rOperatingMonitorDPOQueryWrapper.select("measurement_point_id").
                            in("measurement_point_id",collect1).
                            gt("effective_minute_count",0).
                            between("data_date",begin,end);
                    List<ROperatingMonitorDPO> rOperatingMonitorDPOS = rOperatingMonitorDMapper.selectList(rOperatingMonitorDPOQueryWrapper);
                    List<String> effLineIds = rOperatingMonitorDPOS.stream().map(ROperatingMonitorDPO::getMeasurementPointId).distinct().collect(Collectors.toList());
                    if(CollUtil.isEmpty(effLineIds)){
                        //目前部门下面没有有效监测点，先做调过处理
                        return;
                    }
                    queryWrapper.clear();
                    queryWrapper.select("measurement_point_id","freq_over_day", "v_dev_over_day", "v_over_day", "i_over_day", "unbalance_over_day", "i_neg_over_day").
                            in("measurement_point_id", effLineIds).
                            between("data_date",begin,end);

                    List<RMpPartHarmonicDetailM> rMpPartHarmonicDetailMS = rMpPartHarmonicDetailMMapper.selectList(queryWrapper);

                    rMpSurplusHarmonicDetailMQueryWrapper.clear();
                    rMpSurplusHarmonicDetailMQueryWrapper.select("measurement_point_id","flicker_over_day", "inuharm_over_day").
                            in("measurement_point_id", effLineIds).
                            between("data_date",begin,end);
                    List<RMpSurplusHarmonicDetailM> rMpSurplusHarmonicDetailMS = rMpSurplusHarmonicDetailMMapper.selectList(rMpSurplusHarmonicDetailMQueryWrapper);

                    rStatOrgYPOQueryWrapper.clear();
                    rStatOrgYPOQueryWrapper.select("effective_measurement_accrued").
                            eq("org_no", deptGetChildrenMoreDTO.getUnitId()).
                            eq("measurement_type_class", k).
                            eq("data_type", dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId()).
                            between("data_date",begin,end);
                    RStatOrgYPO rStatOrgYPO = rStatOrgYMapper.selectOne(rStatOrgYPOQueryWrapper);



                    steadyStatis.forEach(dictData -> {
                        if (Objects.equals(dictData.getCode(), DicDataEnum.TOTAL_INDICATOR.getCode())) {
                            return;
                        }
                        RStatHarmonicYPO rStatHarmonicYPO = new RStatHarmonicYPO();
                        rStatHarmonicYPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                        rStatHarmonicYPO.setDataDate(localDate);
                        rStatHarmonicYPO.setHarmonicType(dictData.getId());
                        rStatHarmonicYPO.setDataType(dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId());
                        rStatHarmonicYPO.setMeasurementTypeClass(k);
                        /**4、累计超标监测点数*/
                        rStatHarmonicYPO.setOverLimitMeasurementAccrued(getOverLimitMeasurementAccruedTime(dictData, rMpPartHarmonicDetailMS, rMpSurplusHarmonicDetailMS));
                        /*3、日均超标监测点数,6、日均超标监测点数占比*/
                        harmonicMPOQueryWrapper.clear();
                        harmonicMPOQueryWrapper.eq("org_no", deptGetChildrenMoreDTO.getUnitId()).
                                eq("measurement_type_class", k).
                                eq("data_type", dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId()).
                                eq("harmonic_type", dictData.getId()).
                                between("data_date",begin,end);
                        List<RStatHarmonicMPO> list = rStatHarmonicMPOService.list(harmonicMPOQueryWrapper);
                        double asDouble = list.stream().mapToDouble(RStatHarmonicMPO::getOverLimitMeasurementAverage).average().orElse(0.00);
                        asDouble = BigDecimal.valueOf(asDouble).setScale(5,   BigDecimal.ROUND_HALF_UP).doubleValue();
                        double asDouble1 = list.stream().mapToDouble(RStatHarmonicMPO::getOverLimitMeasurementRatioAverage).average().orElse(0.00);
                        asDouble1 = BigDecimal.valueOf(asDouble1).setScale(5,   BigDecimal.ROUND_HALF_UP).doubleValue();
                        int sum1 = list.stream().mapToInt(RStatHarmonicMPO::getOverDay).sum();
                        rStatHarmonicYPO.setOverLimitMeasurementAverage(asDouble);

                        rStatHarmonicYPO.setOverLimitMeasurementRatioAverage(asDouble1);


                        if (rStatHarmonicYPO.getOverLimitMeasurementAccrued() == 0) {
                            //todo为0时如何设值
                            rStatHarmonicYPO.setAverageOverDay(0.00);

                        } else {
                            String value = df.format((float) sum1 / rStatHarmonicYPO.getOverLimitMeasurementAccrued());
                            rStatHarmonicYPO.setAverageOverDay(Double.parseDouble(value));
                        }

                        if (rStatOrgYPO.getEffectiveMeasurementAccrued() == 0) {
                            //todo为0时如何设值
                            rStatHarmonicYPO.setOverLimitMeasurementRatioAccrued(0.00);

                        } else {
                            String value = df.format((float) rStatHarmonicYPO.getOverLimitMeasurementAccrued() / rStatOrgYPO.getEffectiveMeasurementAccrued());
                            rStatHarmonicYPO.setOverLimitMeasurementRatioAccrued(Double.parseDouble(value));
                        }
                        rStatHarmonicYPOList.add(rStatHarmonicYPO);
                    });

                });

            }
            /*配网*/
            if(CollectionUtil.isNotEmpty(deptGetChildrenMoreDTO.getPwMonitorIds())){
                List<LineDevGetDTO> pwMonitorIds = deptGetChildrenMoreDTO.getPwMonitorIds();
                Map<Integer, List<LineDevGetDTO>> collect = pwMonitorIds.stream().collect(Collectors.groupingBy(LineDevGetDTO::getLineType));
                collect.forEach((k, v) -> {
                    List<String> collect1 = v.stream().map(LineDevGetDTO::getPointId).collect(Collectors.toList());

                    queryWrapper.clear();
                    queryWrapper.select("measurement_point_id","freq_over_day", "v_dev_over_day", "v_over_day", "i_over_day", "unbalance_over_day", "i_neg_over_day").
                            in("measurement_point_id", collect1).
                            between("data_date",begin,end);

                    List<RMpPartHarmonicDetailM> rMpPartHarmonicDetailMS = rMpPartHarmonicDetailMMapper.selectList(queryWrapper);

                    rMpSurplusHarmonicDetailMQueryWrapper.clear();
                    rMpSurplusHarmonicDetailMQueryWrapper.select("measurement_point_id","flicker_over_day", "inuharm_over_day").
                            in("measurement_point_id", collect1).
                            between("data_date",begin,end);
                    List<RMpSurplusHarmonicDetailM> rMpSurplusHarmonicDetailMS = rMpSurplusHarmonicDetailMMapper.selectList(rMpSurplusHarmonicDetailMQueryWrapper);

                    rStatOrgYPOQueryWrapper.clear();
                    rStatOrgYPOQueryWrapper.select("effective_measurement_accrued").
                            eq("org_no", deptGetChildrenMoreDTO.getUnitId()).
                            eq("measurement_type_class", getmeasurementType(k, lineSortMap)).
                            eq("data_type", dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId()).
                            between("data_date",begin,end);
                    RStatOrgYPO rStatOrgYPO = rStatOrgYMapper.selectOne(rStatOrgYPOQueryWrapper);



                    steadyStatis.forEach(dictData -> {
                        if (Objects.equals(dictData.getCode(), DicDataEnum.TOTAL_INDICATOR.getCode())) {
                            return;
                        }
                        RStatHarmonicYPO rStatHarmonicYPO = new RStatHarmonicYPO();
                        rStatHarmonicYPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                        rStatHarmonicYPO.setDataDate(localDate);
                        rStatHarmonicYPO.setHarmonicType(dictData.getId());
                        rStatHarmonicYPO.setDataType(dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId());
                        rStatHarmonicYPO.setMeasurementTypeClass(getmeasurementType(k, lineSortMap));
                        /**4、累计超标监测点数*/
                        rStatHarmonicYPO.setOverLimitMeasurementAccrued(getOverLimitMeasurementAccruedTime(dictData, rMpPartHarmonicDetailMS, rMpSurplusHarmonicDetailMS));
                        /*3、日均超标监测点数,6、日均超标监测点数占比*/
                        harmonicMPOQueryWrapper.clear();
                        harmonicMPOQueryWrapper.eq("org_no", deptGetChildrenMoreDTO.getUnitId()).
                                eq("measurement_type_class", getmeasurementType(k, lineSortMap)).
                                eq("data_type", dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId()).
                                eq("harmonic_type", dictData.getId()).
                                between("data_date",begin,end);
                        List<RStatHarmonicMPO> list = rStatHarmonicMPOService.list(harmonicMPOQueryWrapper);
                        double asDouble = list.stream().mapToDouble(RStatHarmonicMPO::getOverLimitMeasurementAverage).average().orElse(0.00);
                        asDouble = BigDecimal.valueOf(asDouble).setScale(5,   BigDecimal.ROUND_HALF_UP).doubleValue();
                        double asDouble1 = list.stream().mapToDouble(RStatHarmonicMPO::getOverLimitMeasurementRatioAverage).average().orElse(0.00);
                        asDouble1 = BigDecimal.valueOf(asDouble1).setScale(5,   BigDecimal.ROUND_HALF_UP).doubleValue();
                        int sum1 = list.stream().mapToInt(RStatHarmonicMPO::getOverDay).sum();
                        rStatHarmonicYPO.setOverLimitMeasurementAverage(asDouble);

                        rStatHarmonicYPO.setOverLimitMeasurementRatioAverage(asDouble1);


                        if (rStatHarmonicYPO.getOverLimitMeasurementAccrued() == 0) {
                            //todo为0时如何设值
                            rStatHarmonicYPO.setAverageOverDay(0.00);

                        } else {
                            String value = df.format((float) sum1 / rStatHarmonicYPO.getOverLimitMeasurementAccrued());
                            rStatHarmonicYPO.setAverageOverDay(Double.parseDouble(value));
                        }

                        if (rStatOrgYPO.getEffectiveMeasurementAccrued() == 0) {
                            //todo为0时如何设值
                            rStatHarmonicYPO.setOverLimitMeasurementRatioAccrued(0.00);

                        } else {
                            String value = df.format((float) rStatHarmonicYPO.getOverLimitMeasurementAccrued() / rStatOrgYPO.getEffectiveMeasurementAccrued());
                            rStatHarmonicYPO.setOverLimitMeasurementRatioAccrued(Double.parseDouble(value));
                        }
                        rStatHarmonicYPOList.add(rStatHarmonicYPO);
                    });


                });

            }
        });
        if(CollectionUtil.isNotEmpty(rStatHarmonicYPOList)){
            rStatHarmonicYPOService.saveOrUpdateBatchByMultiId(rStatHarmonicYPOList,500);

        }

    }

    /***----------------------------------------------------------------------------------------------**/


    private Integer getOverLimitMeasurementAccruedTime(DictData dictData, List<RMpPartHarmonicDetailM> rMpPartHarmonicDetailMS, List<RMpSurplusHarmonicDetailM> rMpSurplusHarmonicDetailMS) {
        Integer value = 0;
        if (Objects.equals(dictData.getCode(),DicDataEnum.VOLTAGE_DEV.getCode())) {
            value = Integer.valueOf(rMpPartHarmonicDetailMS.stream().filter(temp ->temp.getVDevOverDay()>0).map(RMpPartHarmonicDetailM::getMeasurementPointId).distinct().count()+"");
        }
        if (Objects.equals(dictData.getCode(),DicDataEnum.FLICKER.getCode())) {
            value = Integer.valueOf(rMpSurplusHarmonicDetailMS.stream().filter(temp ->temp.getFlickerOverDay()>0).map(RMpSurplusHarmonicDetailM::getMeasurementPointId).distinct().count()+"");
        }
        if (Objects.equals(dictData.getCode(),DicDataEnum.HARMONIC_VOLTAGE.getCode())) {
            value = Integer.valueOf(rMpPartHarmonicDetailMS.stream().filter(temp ->temp.getVOverDay()>0).map(RMpPartHarmonicDetailM::getMeasurementPointId).distinct().count()+"");
        }
        if (Objects.equals(dictData.getCode(),DicDataEnum.HARMONIC_CURRENT.getCode())) {
            value = Integer.valueOf(rMpPartHarmonicDetailMS.stream().filter(temp ->temp.getIOverDay()>0).map(RMpPartHarmonicDetailM::getMeasurementPointId).distinct().count()+"");
        }
        if (Objects.equals(dictData.getCode(),DicDataEnum.INTERHARMONIC_VOLTAGE.getCode())) {
            value = Integer.valueOf(rMpSurplusHarmonicDetailMS.stream().filter(temp ->temp.getInuharmOverDay()>0).map(RMpSurplusHarmonicDetailM::getMeasurementPointId).distinct().count()+"");
        }
        if (Objects.equals(dictData.getCode(),DicDataEnum.PHASE_VOLTAGE.getCode())) {
            value = Integer.valueOf(rMpPartHarmonicDetailMS.stream().filter(temp ->temp.getUnbalanceOverDay()>0).map(RMpPartHarmonicDetailM::getMeasurementPointId).distinct().count()+"");
        }
        if (Objects.equals(dictData.getCode(),DicDataEnum.FREQUENCY_DEV.getCode())) {
            value = Integer.valueOf(rMpPartHarmonicDetailMS.stream().filter(temp ->temp.getFreqOverDay()>0).map(RMpPartHarmonicDetailM::getMeasurementPointId).distinct().count()+"");
        }
        if (Objects.equals(dictData.getCode(),DicDataEnum.NEG_CURRENT.getCode())) {
            value = Integer.valueOf(rMpPartHarmonicDetailMS.stream().filter(temp ->temp.getINegOverDay()>0).map(RMpPartHarmonicDetailM::getMeasurementPointId).distinct().count()+"");
        }
        return value;
    }

    private String getmeasurementType(Integer k, Map<String, DictData> lineSortMap) {
        String value ="";
        switch (k) {
            case 1:
                value =lineSortMap.get(DicDataEnum.ONE_LINE.getCode()).getId();
                break;
            case 2:
                value =lineSortMap.get(DicDataEnum.TWO_LINE.getCode()).getId();
                break;
            case 3:
                value =lineSortMap.get(DicDataEnum.THREE_LINE.getCode()).getId();
                break;
            default:
                break;
        }
        return value;
    }

    private Integer getSteadyStatisData(DictData dictData, List<RMpPartHarmonicDetailD> rMpPartHarmonicDetailDS, List<RMpSurplusHarmonicDetailD> rMpSurplusHarmonicDetailDS) {
        Integer value = 0;
        if (Objects.equals(dictData.getCode(),DicDataEnum.VOLTAGE_DEV.getCode())) {
            value = Integer.valueOf(rMpPartHarmonicDetailDS.stream().filter(temp ->temp.getIsVDev()==1).count()+"");
        }
        if (Objects.equals(dictData.getCode(),DicDataEnum.FLICKER.getCode())) {
            value = Integer.valueOf(rMpSurplusHarmonicDetailDS.stream().filter(temp ->temp.getIsFlicker()==1).count()+"");
        }
        if (Objects.equals(dictData.getCode(),DicDataEnum.HARMONIC_VOLTAGE.getCode())) {
            value = Integer.valueOf(rMpPartHarmonicDetailDS.stream().filter(temp ->temp.getIsV()==1).count()+"");
        }
        if (Objects.equals(dictData.getCode(),DicDataEnum.HARMONIC_CURRENT.getCode())) {
            value = Integer.valueOf(rMpPartHarmonicDetailDS.stream().filter(temp ->temp.getIsI()==1).count()+"");
        }
        if (Objects.equals(dictData.getCode(),DicDataEnum.INTERHARMONIC_VOLTAGE.getCode())) {
            value = Integer.valueOf(rMpSurplusHarmonicDetailDS.stream().filter(temp ->temp.getIsInuharm()==1).count()+"");
        }
        if (Objects.equals(dictData.getCode(),DicDataEnum.PHASE_VOLTAGE.getCode())) {
            value = Integer.valueOf(rMpPartHarmonicDetailDS.stream().filter(temp ->temp.getIsUnbalance()==1).count()+"");
        }
        if (Objects.equals(dictData.getCode(),DicDataEnum.FREQUENCY_DEV.getCode())) {
            value = Integer.valueOf(rMpPartHarmonicDetailDS.stream().filter(temp ->temp.getIsFreq()==1).count()+"");

        }
        if (Objects.equals(dictData.getCode(),DicDataEnum.NEG_CURRENT.getCode())) {
            value = Integer.valueOf(rMpPartHarmonicDetailDS.stream().filter(temp ->temp.getIsINeg()==1).count()+"");
        }

        return value;

    }
}
