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.pojo.bo.CalculatedParam;
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.line.RMpPartHarmonicDetailDMapper;
import com.njcn.prepare.harmonic.mapper.mysql.line.RMpPartHarmonicDetailMMapper;
import com.njcn.prepare.harmonic.mapper.mysql.line.RMpSurplusHarmonicDetailDMapper;
import com.njcn.prepare.harmonic.mapper.mysql.line.RMpSurplusHarmonicDetailMMapper;

import com.njcn.prepare.harmonic.pojo.mysql.po.RStatHarmonicVoltageDPO;
import com.njcn.prepare.harmonic.pojo.mysql.po.RStatHarmonicVoltageMPO;
import com.njcn.prepare.harmonic.service.mysql.area.RStatHarmonicVoltageDPOService;
import com.njcn.prepare.harmonic.service.mysql.area.RStatHarmonicVoltageService;
import com.njcn.prepare.harmonic.service.mysql.newalgorithm.RStatHarmonicVoltageMPOService;
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 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.math.BigDecimal;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Description:
 * Date: 2023/6/6  10:08【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@RequiredArgsConstructor
public class RStatHarmonicVoltageServiceImpl implements RStatHarmonicVoltageService {

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

    private final RStatHarmonicVoltageMPOService rStatHarmonicVoltageMPOService;

    private final RMpPartHarmonicDetailMMapper rMpPartHarmonicDetailMMapper;
    private final RMpSurplusHarmonicDetailMMapper rMpSurplusHarmonicDetailMMapper;
    private final RStatOrgMMapper rStatOrgMMapper;

    private final
    DicDataFeignClient dicDataFeignClient;


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

        List<RStatHarmonicVoltageDPO> rStatHarmonicVoltageDPOList = new ArrayList<>();

        LocalDate localDate = LocalDateTimeUtil.parseDate(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN);


        QueryWrapper<RMpPartHarmonicDetailD> queryWrapper = new QueryWrapper<>();
        QueryWrapper<RMpSurplusHarmonicDetailD> rMpSurplusHarmonicDetailDQueryWrapper = new QueryWrapper<>();
        QueryWrapper<RStatOrgDPO> rStatOrgDPOQueryWrapper = new QueryWrapper<>();
        DecimalFormat df = new DecimalFormat("0.00");

        /*获取稳态统计指标*/
        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)->{
                    Map<String, List<LineDevGetDTO>> collect2 = v.stream().collect(Collectors.groupingBy(LineDevGetDTO::getVoltageLevel));
                    collect2.forEach((k1, v1) -> {

                        List<String> collect3 = v1.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",collect3).
                                eq("data_date",localDate);
                        List<RMpPartHarmonicDetailD> rMpPartHarmonicDetailDS = rMpPartHarmonicDetailDMapper.selectList(queryWrapper);

                        rMpSurplusHarmonicDetailDQueryWrapper.clear();
                        rMpSurplusHarmonicDetailDQueryWrapper.select("is_flicker","is_inuharm").
                                in("measurement_point_id",collect3).
                                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;
                            }
                            RStatHarmonicVoltageDPO rStatHarmonicVoltageDPO = new RStatHarmonicVoltageDPO();
                            rStatHarmonicVoltageDPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                            rStatHarmonicVoltageDPO.setDataDate(localDate);
                            rStatHarmonicVoltageDPO.setHarmonicType(dictData.getId());
                            rStatHarmonicVoltageDPO.setDataType(dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId());
                            rStatHarmonicVoltageDPO.setMeasurementTypeClass(k);
                            rStatHarmonicVoltageDPO.setVoltageType(k1);
                            rStatHarmonicVoltageDPO.setOverLimitMeasurementAverage(getSteadyStatisData(dictData,rMpPartHarmonicDetailDS,rMpSurplusHarmonicDetailDS));
                            if(rStatOrgDPO.getEffectiveMeasurement()==0){
                                rStatHarmonicVoltageDPO.setOverLimitMeasurementRatioAverage(0.00);

                            }else{
                                String value = df.format((float)rStatHarmonicVoltageDPO.getOverLimitMeasurementAverage()/ rStatOrgDPO.getEffectiveMeasurement());
                                rStatHarmonicVoltageDPO.setOverLimitMeasurementRatioAverage(Double.parseDouble(value));
                            }
                            rStatHarmonicVoltageDPOList.add(rStatHarmonicVoltageDPO);
                        })

                        ;}
                    );

                });

            }
            /*配网*/
            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)->{
                    Map<String, List<LineDevGetDTO>> collect2 = v.stream().collect(Collectors.groupingBy(LineDevGetDTO::getVoltageLevel));
                    collect2.forEach((k1, v1) -> {

                        List<String> collect3 = v1.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",collect3).
                                eq("data_date",localDate);
                        List<RMpPartHarmonicDetailD> rMpPartHarmonicDetailDS = rMpPartHarmonicDetailDMapper.selectList(queryWrapper);

                        rMpSurplusHarmonicDetailDQueryWrapper.clear();
                        rMpSurplusHarmonicDetailDQueryWrapper.select("is_flicker","is_inuharm").
                                in("measurement_point_id",collect3).
                                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.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;
                            }
                            RStatHarmonicVoltageDPO rStatHarmonicVoltageDPO = new RStatHarmonicVoltageDPO();
                            rStatHarmonicVoltageDPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                            rStatHarmonicVoltageDPO.setDataDate(localDate);
                            rStatHarmonicVoltageDPO.setHarmonicType(dictData.getId());
                            rStatHarmonicVoltageDPO.setDataType(dataTypeMap.get(DicDataEnum.DISTRIBUTION_POINT.getCode()).getId());
                            rStatHarmonicVoltageDPO.setMeasurementTypeClass(getmeasurementType(k,lineSortMap));
                            rStatHarmonicVoltageDPO.setVoltageType(k1);
                            rStatHarmonicVoltageDPO.setOverLimitMeasurementAverage(getSteadyStatisData(dictData,rMpPartHarmonicDetailDS,rMpSurplusHarmonicDetailDS));
                            if(rStatOrgDPO.getEffectiveMeasurement()==0){
                                rStatHarmonicVoltageDPO.setOverLimitMeasurementRatioAverage(0.00);

                            }else{
                                String value = df.format((float)rStatHarmonicVoltageDPO.getOverLimitMeasurementAverage()/ rStatOrgDPO.getEffectiveMeasurement());
                                rStatHarmonicVoltageDPO.setOverLimitMeasurementRatioAverage(Double.parseDouble(value));
                            }
                            rStatHarmonicVoltageDPOList.add(rStatHarmonicVoltageDPO);
                        })

                        ;}
                    );

                });
            }

        });
        if(!CollectionUtils.isEmpty(rStatHarmonicVoltageDPOList)){
            rStatHarmonicVoltageDPOService.saveOrUpdateBatchByMultiId(rStatHarmonicVoltageDPOList,500);
        }


    }

    @Override
    public void handleMonth(CalculatedParam<DeptGetChildrenMoreDTO> calculatedParam) {
        List<RStatHarmonicVoltageMPO> rStatHarmonicVoltageMPOList = 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<RMpPartHarmonicDetailM> queryWrapper = new QueryWrapper<>();
        QueryWrapper<RMpSurplusHarmonicDetailM> rMpSurplusHarmonicDetailMQueryWrapper = new QueryWrapper<>();
        QueryWrapper<RStatOrgMPO> rStatOrgMPOQueryWrapper = new QueryWrapper<>();
        QueryWrapper<RStatHarmonicVoltageDPO> harmonicVoltageDPOQueryWrapper = new QueryWrapper<>();

        DecimalFormat df = new DecimalFormat("0.00");

        /*获取稳态统计指标*/
        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) -> {
                    Map<String, List<LineDevGetDTO>> collect2 = v.stream().collect(Collectors.groupingBy(LineDevGetDTO::getVoltageLevel));
                    collect2.forEach((k1, v1) -> {

                        List<String> collect3 = v1.stream().map(LineDevGetDTO::getPointId).collect(Collectors.toList());

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

                        rMpSurplusHarmonicDetailMQueryWrapper.clear();
                        rMpSurplusHarmonicDetailMQueryWrapper.select("flicker_over_day", "inuharm_over_day").
                                in("measurement_point_id", collect3).
                                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;
                            }
                            RStatHarmonicVoltageMPO rStatHarmonicVoltageMPO = new RStatHarmonicVoltageMPO();
                            rStatHarmonicVoltageMPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                            rStatHarmonicVoltageMPO.setDataDate(localDate);
                            rStatHarmonicVoltageMPO.setHarmonicType(dictData.getId());
                            rStatHarmonicVoltageMPO.setDataType(dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId());
                            rStatHarmonicVoltageMPO.setMeasurementTypeClass(k);
                            rStatHarmonicVoltageMPO.setVoltageType(k1);

                            /**4、累计超标监测点数*/
                            rStatHarmonicVoltageMPO.setOverLimitMeasurementAccrued(getOverLimitMeasurementAccruedTime(dictData, rMpPartHarmonicDetailMS, rMpSurplusHarmonicDetailMS));
                            /*3、日均超标监测点数,6、日均超标监测点数占比*/
                            harmonicVoltageDPOQueryWrapper.clear();
                            harmonicVoltageDPOQueryWrapper.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()).
                                    eq("voltage_type",k1).
                                    between("data_date",begin,end);
                            List<RStatHarmonicVoltageDPO> list = rStatHarmonicVoltageDPOService.list(harmonicVoltageDPOQueryWrapper);
                            Double asDouble = 0.0;
                            Double asDouble1 = 0.0;
                            Integer sum = 0;
                            if(CollUtil.isNotEmpty(list)){
                                asDouble= BigDecimal.valueOf(list.stream().mapToDouble(RStatHarmonicVoltageDPO::getOverLimitMeasurementAverage)
                                                .average().getAsDouble())
                                        .setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
                                asDouble1 = BigDecimal.valueOf(list.stream().mapToDouble(RStatHarmonicVoltageDPO::getOverLimitMeasurementRatioAverage)
                                                .average().getAsDouble())
                                        .setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
                                sum = list.stream().mapToInt(RStatHarmonicVoltageDPO::getOverLimitMeasurementAverage).sum();
                            }
                            rStatHarmonicVoltageMPO.setOverLimitMeasurementAverage(asDouble);
                            rStatHarmonicVoltageMPO.setOverLimitMeasurementRatioAccrued(asDouble1);
                            rStatHarmonicVoltageMPO.setOverDay(sum);

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

                            } else {
                                String value = df.format((float) rStatHarmonicVoltageMPO.getOverLimitMeasurementAccrued() / rStatOrgMPO.getEffectiveMeasurementAccrued());
                                rStatHarmonicVoltageMPO.setOverLimitMeasurementRatioAverage(Double.parseDouble(value));
                            }
                            rStatHarmonicVoltageMPOList.add(rStatHarmonicVoltageMPO);
                        });

                    });
                });
            }
            /*配网*/
            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());
                        Map<String, List<LineDevGetDTO>> collect2 = v.stream().collect(Collectors.groupingBy(LineDevGetDTO::getVoltageLevel));
                        collect2.forEach((k1, v1) -> {
                            queryWrapper.clear();
                            queryWrapper.select("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("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", 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;
                                }
                                RStatHarmonicVoltageMPO rStatHarmonicVoltageMPO = new RStatHarmonicVoltageMPO();
                                rStatHarmonicVoltageMPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                                rStatHarmonicVoltageMPO.setDataDate(localDate);
                                rStatHarmonicVoltageMPO.setHarmonicType(dictData.getId());
                                rStatHarmonicVoltageMPO.setDataType(dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId());
                                rStatHarmonicVoltageMPO.setMeasurementTypeClass(getmeasurementType(k,lineSortMap));
                                rStatHarmonicVoltageMPO.setVoltageType(k1);

                                /**4、累计超标监测点数*/
                                rStatHarmonicVoltageMPO.setOverLimitMeasurementAccrued(getOverLimitMeasurementAccruedTime(dictData, rMpPartHarmonicDetailMS, rMpSurplusHarmonicDetailMS));
                                /*3、日均超标监测点数,6、日均超标监测点数占比*/
                                harmonicVoltageDPOQueryWrapper.clear();
                                harmonicVoltageDPOQueryWrapper.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()).
                                        eq("voltage_type",k1).
                                        between("data_date",begin,end);
                                List<RStatHarmonicVoltageDPO> list = rStatHarmonicVoltageDPOService.list(harmonicVoltageDPOQueryWrapper);
                                Double asDouble = 0.0;
                                Double asDouble1 = 0.0;
                                Integer sum = 0;
                                if(CollUtil.isNotEmpty(list)){
                                    asDouble= BigDecimal.valueOf(list.stream().mapToDouble(RStatHarmonicVoltageDPO::getOverLimitMeasurementAverage)
                                                    .average().getAsDouble())
                                            .setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
                                    asDouble1 = BigDecimal.valueOf(list.stream().mapToDouble(RStatHarmonicVoltageDPO::getOverLimitMeasurementRatioAverage)
                                                    .average().getAsDouble())
                                            .setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
                                    sum = list.stream().mapToInt(RStatHarmonicVoltageDPO::getOverLimitMeasurementAverage).sum();
                                }
                                rStatHarmonicVoltageMPO.setOverLimitMeasurementAverage(asDouble);
                                rStatHarmonicVoltageMPO.setOverLimitMeasurementRatioAccrued(asDouble1);
                                rStatHarmonicVoltageMPO.setOverDay(sum);



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

                                } else {
                                    String value = df.format((float) rStatHarmonicVoltageMPO.getOverLimitMeasurementAccrued() / rStatOrgMPO.getEffectiveMeasurementAccrued());
                                    rStatHarmonicVoltageMPO.setOverLimitMeasurementRatioAverage(Double.parseDouble(value));
                                }
                                rStatHarmonicVoltageMPOList.add(rStatHarmonicVoltageMPO);
                            });

                        });
                    }


                });

            }
        });
        if(CollectionUtil.isNotEmpty(rStatHarmonicVoltageMPOList)){
            rStatHarmonicVoltageMPOService.saveOrUpdateBatchByMultiId(rStatHarmonicVoltageMPOList,500);

        }
    }



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

    @Override
    public void handlerByPowerLevel(List<DeptGetChildrenMoreDTO> data, String dataDate, Integer type) {
        switch (type) {
//            case 1:
//                this.hanlderOracleYear (lineParam);
//                break;
//            case 2:
//                this.hanlderOracleQtr (lineParam);
//                break;
            case 3:
                this.hanlderMByPl (data,  dataDate,  type);
                break;
            case 5:
                this.hanlderDayByPl ( data,  dataDate,  type);
                break;
            default:
                break;
        }

    }


    @Transactional(rollbackFor = {Exception.class})
    @Async("asyncExecutor")
    @SneakyThrows
    private void hanlderMByPl(List<DeptGetChildrenMoreDTO> data, String dataDate, Integer type) {
        List<RStatHarmonicVoltageMPO> rStatHarmonicVoltageMPOList = new ArrayList<>();
        Date date = DateUtil.parse(dataDate);
        LocalDate localDate = LocalDateTimeUtil.parseDate(dataDate, DatePattern.NORM_DATE_PATTERN);

        QueryWrapper<RMpPartHarmonicDetailM> queryWrapper = new QueryWrapper<>();
        QueryWrapper<RMpSurplusHarmonicDetailM> rMpSurplusHarmonicDetailMQueryWrapper = new QueryWrapper<>();
        QueryWrapper<RStatOrgMPO> rStatOrgMPOQueryWrapper = new QueryWrapper<>();
        QueryWrapper<RStatHarmonicVoltageDPO> harmonicVoltageDPOQueryWrapper = new QueryWrapper<>();

        DecimalFormat df = new DecimalFormat("0.00");

        /*获取稳态统计指标*/
        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));


        data.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());
                Map<String, List<LineDevGetDTO>> collect2 = v.stream().collect(Collectors.groupingBy(LineDevGetDTO::getVoltageLevel));
                collect2.forEach((k1, v1) -> {
                    queryWrapper.clear();
                    queryWrapper.select("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).
                            eq("DATE_FORMAT( data_date ,'%Y-%m')",dataDate.substring(0, 7));
                    List<RMpPartHarmonicDetailM> rMpPartHarmonicDetailMS = rMpPartHarmonicDetailMMapper.selectList(queryWrapper);

                    rMpSurplusHarmonicDetailMQueryWrapper.clear();
                    rMpSurplusHarmonicDetailMQueryWrapper.select("flicker_over_day", "inuharm_over_day").
                            in("measurement_point_id", collect1).
                            eq("DATE_FORMAT( data_date ,'%Y-%m')",dataDate.substring(0, 7));
                    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()).
                            eq("DATE_FORMAT( data_date ,'%Y-%m')",dataDate.substring(0, 7));
                    RStatOrgMPO rStatOrgMPO = rStatOrgMMapper.selectOne(rStatOrgMPOQueryWrapper);



                    steadyStatis.forEach(dictData -> {
                        if (Objects.equals(dictData.getCode(), DicDataEnum.TOTAL_INDICATOR.getCode())) {
                            return;
                        }
                        RStatHarmonicVoltageMPO rStatHarmonicVoltageMPO = new RStatHarmonicVoltageMPO();
                        rStatHarmonicVoltageMPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                        rStatHarmonicVoltageMPO.setDataDate(localDate);
                        rStatHarmonicVoltageMPO.setHarmonicType(dictData.getId());
                        rStatHarmonicVoltageMPO.setDataType(dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId());
                        rStatHarmonicVoltageMPO.setMeasurementTypeClass(k);
                        rStatHarmonicVoltageMPO.setVoltageType(k1);

                        /**4、累计超标监测点数*/
                        rStatHarmonicVoltageMPO.setOverLimitMeasurementAccrued(getOverLimitMeasurementAccruedTime(dictData, rMpPartHarmonicDetailMS, rMpSurplusHarmonicDetailMS));
                        /*3、日均超标监测点数,6、日均超标监测点数占比*/
                        harmonicVoltageDPOQueryWrapper.clear();
                        harmonicVoltageDPOQueryWrapper.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()).
                                eq("voltage_type",k1).
                                eq("DATE_FORMAT( data_date ,'%Y-%m')",dataDate.substring(0, 7));
                        List<RStatHarmonicVoltageDPO> list = rStatHarmonicVoltageDPOService.list(harmonicVoltageDPOQueryWrapper);
                        double asDouble = list.stream().mapToDouble(RStatHarmonicVoltageDPO::getOverLimitMeasurementAverage).average().getAsDouble();
                        asDouble = BigDecimal.valueOf(asDouble).setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
                        rStatHarmonicVoltageMPO.setOverLimitMeasurementAverage(asDouble);
                        double asDouble1 = list.stream().mapToDouble(RStatHarmonicVoltageDPO::getOverLimitMeasurementRatioAverage).average().getAsDouble();
                        asDouble1 = BigDecimal.valueOf(asDouble1).setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
                        rStatHarmonicVoltageMPO.setOverLimitMeasurementRatioAccrued(asDouble);
                        int sum = list.stream().mapToInt(RStatHarmonicVoltageDPO::getOverLimitMeasurementAverage).sum();
                        rStatHarmonicVoltageMPO.setOverDay(sum);


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

                        } else {
                            String value = df.format((float) rStatHarmonicVoltageMPO.getOverLimitMeasurementAccrued() / rStatOrgMPO.getEffectiveMeasurementAccrued());
                            rStatHarmonicVoltageMPO.setOverLimitMeasurementRatioAverage(Double.parseDouble(value));
                        }
                        rStatHarmonicVoltageMPOList.add(rStatHarmonicVoltageMPO);
                    });

                });
                });
            }
            /*配网*/
            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());
                        Map<String, List<LineDevGetDTO>> collect2 = v.stream().collect(Collectors.groupingBy(LineDevGetDTO::getVoltageLevel));
                        collect2.forEach((k1, v1) -> {
                            queryWrapper.clear();
                            queryWrapper.select("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).
                                    eq("DATE_FORMAT( data_date ,'%Y-%m')",dataDate.substring(0, 7));
                            List<RMpPartHarmonicDetailM> rMpPartHarmonicDetailMS = rMpPartHarmonicDetailMMapper.selectList(queryWrapper);

                            rMpSurplusHarmonicDetailMQueryWrapper.clear();
                            rMpSurplusHarmonicDetailMQueryWrapper.select("flicker_over_day", "inuharm_over_day").
                                    in("measurement_point_id", collect1).
                                    eq("DATE_FORMAT( data_date ,'%Y-%m')",dataDate.substring(0, 7));
                            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()).
                                    eq("DATE_FORMAT( data_date ,'%Y-%m')",dataDate.substring(0, 7));
                            RStatOrgMPO rStatOrgMPO = rStatOrgMMapper.selectOne(rStatOrgMPOQueryWrapper);



                            steadyStatis.forEach(dictData -> {
                                if (Objects.equals(dictData.getCode(), DicDataEnum.TOTAL_INDICATOR.getCode())) {
                                    return;
                                }
                                RStatHarmonicVoltageMPO rStatHarmonicVoltageMPO = new RStatHarmonicVoltageMPO();
                                rStatHarmonicVoltageMPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                                rStatHarmonicVoltageMPO.setDataDate(localDate);
                                rStatHarmonicVoltageMPO.setHarmonicType(dictData.getId());
                                rStatHarmonicVoltageMPO.setDataType(dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId());
                                rStatHarmonicVoltageMPO.setMeasurementTypeClass(getmeasurementType(k,lineSortMap));
                                rStatHarmonicVoltageMPO.setVoltageType(k1);

                                /**4、累计超标监测点数*/
                                rStatHarmonicVoltageMPO.setOverLimitMeasurementAccrued(getOverLimitMeasurementAccruedTime(dictData, rMpPartHarmonicDetailMS, rMpSurplusHarmonicDetailMS));
                                /*3、日均超标监测点数,6、日均超标监测点数占比*/
                                harmonicVoltageDPOQueryWrapper.clear();
                                harmonicVoltageDPOQueryWrapper.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()).
                                        eq("voltage_type",k1).
                                        eq("DATE_FORMAT( data_date ,'%Y-%m')",dataDate.substring(0, 7));
                                List<RStatHarmonicVoltageDPO> list = rStatHarmonicVoltageDPOService.list(harmonicVoltageDPOQueryWrapper);
                                double asDouble = list.stream().mapToDouble(RStatHarmonicVoltageDPO::getOverLimitMeasurementAverage).average().getAsDouble();
                                asDouble = BigDecimal.valueOf(asDouble).setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
                                rStatHarmonicVoltageMPO.setOverLimitMeasurementAverage(asDouble);
                                double asDouble1 = list.stream().mapToDouble(RStatHarmonicVoltageDPO::getOverLimitMeasurementRatioAverage).average().getAsDouble();
                                asDouble1 = BigDecimal.valueOf(asDouble1).setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
                                rStatHarmonicVoltageMPO.setOverLimitMeasurementRatioAccrued(asDouble);
                                int sum = list.stream().mapToInt(RStatHarmonicVoltageDPO::getOverLimitMeasurementAverage).sum();
                                rStatHarmonicVoltageMPO.setOverDay(sum);


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

                                } else {
                                    String value = df.format((float) rStatHarmonicVoltageMPO.getOverLimitMeasurementAccrued() / rStatOrgMPO.getEffectiveMeasurementAccrued());
                                    rStatHarmonicVoltageMPO.setOverLimitMeasurementRatioAverage(Double.parseDouble(value));
                                }
                                rStatHarmonicVoltageMPOList.add(rStatHarmonicVoltageMPO);
                            });

                        });
                    }


                });

            }
        });
        if(CollectionUtil.isNotEmpty(rStatHarmonicVoltageMPOList)){
            rStatHarmonicVoltageMPOService.saveOrUpdateBatchByMultiId(rStatHarmonicVoltageMPOList,500);

        }
    }

    @Transactional(rollbackFor = {Exception.class})
    @Async("asyncExecutor")
    @SneakyThrows
    private void hanlderDayByPl(List<DeptGetChildrenMoreDTO> data, String dataDate, Integer type) {
        List<RStatHarmonicVoltageDPO> rStatHarmonicVoltageDPOList = new ArrayList<>();
        Date date = DateUtil.parse(dataDate);
        LocalDate localDate = LocalDateTimeUtil.parseDate(dataDate, DatePattern.NORM_DATE_PATTERN);

        QueryWrapper<RMpPartHarmonicDetailD> queryWrapper = new QueryWrapper<>();
        QueryWrapper<RMpSurplusHarmonicDetailD> rMpSurplusHarmonicDetailDQueryWrapper = new QueryWrapper<>();
        QueryWrapper<RStatOrgDPO> rStatOrgDPOQueryWrapper = new QueryWrapper<>();
        DecimalFormat df = new DecimalFormat("0.00");

        /*获取稳态统计指标*/
        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));


        data.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)->{
                    Map<String, List<LineDevGetDTO>> collect2 = v.stream().collect(Collectors.groupingBy(LineDevGetDTO::getVoltageLevel));
                    collect2.forEach((k1, v1) -> {

                        List<String> collect3 = v1.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",collect3).
                                eq("DATE_FORMAT( data_date ,'%Y-%m-%d')",dataDate);
                        List<RMpPartHarmonicDetailD> rMpPartHarmonicDetailDS = rMpPartHarmonicDetailDMapper.selectList(queryWrapper);

                        rMpSurplusHarmonicDetailDQueryWrapper.clear();
                        rMpSurplusHarmonicDetailDQueryWrapper.select("is_flicker","is_inuharm").
                                in("measurement_point_id",collect3).
                                eq("DATE_FORMAT( data_date ,'%Y-%m-%d')",dataDate);
                        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("DATE_FORMAT( data_date ,'%Y-%m-%d')",dataDate);
                        RStatOrgDPO rStatOrgDPO = rStatOrgDMapper.selectOne(rStatOrgDPOQueryWrapper);
                        steadyStatis.forEach(dictData -> {
                            if(Objects.equals(dictData.getCode(),DicDataEnum.TOTAL_INDICATOR.getCode())){
                                return;
                            }
                            RStatHarmonicVoltageDPO rStatHarmonicVoltageDPO = new RStatHarmonicVoltageDPO();
                            rStatHarmonicVoltageDPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                            rStatHarmonicVoltageDPO.setDataDate(localDate);
                            rStatHarmonicVoltageDPO.setHarmonicType(dictData.getId());
                            rStatHarmonicVoltageDPO.setDataType(dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId());
                            rStatHarmonicVoltageDPO.setMeasurementTypeClass(k);
                            rStatHarmonicVoltageDPO.setVoltageType(k1);
                            rStatHarmonicVoltageDPO.setOverLimitMeasurementAverage(getSteadyStatisData(dictData,rMpPartHarmonicDetailDS,rMpSurplusHarmonicDetailDS));
                            if(rStatOrgDPO.getEffectiveMeasurement()==0){
                                rStatHarmonicVoltageDPO.setOverLimitMeasurementRatioAverage(0.00);

                            }else{
                                String value = df.format((float)rStatHarmonicVoltageDPO.getOverLimitMeasurementAverage()/ rStatOrgDPO.getEffectiveMeasurement());
                                rStatHarmonicVoltageDPO.setOverLimitMeasurementRatioAverage(Double.parseDouble(value));
                            }
                            rStatHarmonicVoltageDPOList.add(rStatHarmonicVoltageDPO);
                        })

                        ;}
                    );

                });

            }
            /*配网*/
            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)->{
                    Map<String, List<LineDevGetDTO>> collect2 = v.stream().collect(Collectors.groupingBy(LineDevGetDTO::getVoltageLevel));
                    collect2.forEach((k1, v1) -> {

                        List<String> collect3 = v1.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",collect3).
                                eq("DATE_FORMAT( data_date ,'%Y-%m-%d')",dataDate);
                        List<RMpPartHarmonicDetailD> rMpPartHarmonicDetailDS = rMpPartHarmonicDetailDMapper.selectList(queryWrapper);

                        rMpSurplusHarmonicDetailDQueryWrapper.clear();
                        rMpSurplusHarmonicDetailDQueryWrapper.select("is_flicker","is_inuharm").
                                in("measurement_point_id",collect3).
                                eq("DATE_FORMAT( data_date ,'%Y-%m-%d')",dataDate);
                        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.MAINNET_POINT.getCode()).getId()).
                                eq("DATE_FORMAT( data_date ,'%Y-%m-%d')",dataDate);
                        RStatOrgDPO rStatOrgDPO = rStatOrgDMapper.selectOne(rStatOrgDPOQueryWrapper);
                        steadyStatis.forEach(dictData -> {
                            if(Objects.equals(dictData.getCode(),DicDataEnum.TOTAL_INDICATOR.getCode())){
                                return;
                            }
                            RStatHarmonicVoltageDPO rStatHarmonicVoltageDPO = new RStatHarmonicVoltageDPO();
                            rStatHarmonicVoltageDPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                            rStatHarmonicVoltageDPO.setDataDate(localDate);
                            rStatHarmonicVoltageDPO.setHarmonicType(dictData.getId());
                            rStatHarmonicVoltageDPO.setDataType(dataTypeMap.get(DicDataEnum.DISTRIBUTION_POINT.getCode()).getId());
                            rStatHarmonicVoltageDPO.setMeasurementTypeClass(getmeasurementType(k,lineSortMap));
                            rStatHarmonicVoltageDPO.setVoltageType(k1);
                            rStatHarmonicVoltageDPO.setOverLimitMeasurementAverage(getSteadyStatisData(dictData,rMpPartHarmonicDetailDS,rMpSurplusHarmonicDetailDS));
                            if(rStatOrgDPO.getEffectiveMeasurement()==0){
                                rStatHarmonicVoltageDPO.setOverLimitMeasurementRatioAverage(0.00);

                            }else{
                                String value = df.format((float)rStatHarmonicVoltageDPO.getOverLimitMeasurementAverage()/ rStatOrgDPO.getEffectiveMeasurement());
                                rStatHarmonicVoltageDPO.setOverLimitMeasurementRatioAverage(Double.parseDouble(value));
                            }
                            rStatHarmonicVoltageDPOList.add(rStatHarmonicVoltageDPO);
                        })

                        ;}
                    );

                });
            }

        });
        if(!CollectionUtils.isEmpty(rStatHarmonicVoltageDPOList)){
            rStatHarmonicVoltageDPOService.saveOrUpdateBatchByMultiId(rStatHarmonicVoltageDPOList,500);
        }
    }

    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;

    }

    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).count()+"");
        }
        if (Objects.equals(dictData.getCode(),DicDataEnum.FLICKER.getCode())) {
            value = Integer.valueOf(rMpSurplusHarmonicDetailMS.stream().filter(temp ->temp.getFlickerOverDay()>0).count()+"");
        }
        if (Objects.equals(dictData.getCode(),DicDataEnum.HARMONIC_VOLTAGE.getCode())) {
            value = Integer.valueOf(rMpPartHarmonicDetailMS.stream().filter(temp ->temp.getVOverDay()>0).count()+"");
        }
        if (Objects.equals(dictData.getCode(),DicDataEnum.HARMONIC_CURRENT.getCode())) {
            value = Integer.valueOf(rMpPartHarmonicDetailMS.stream().filter(temp ->temp.getIOverDay()>0).count()+"");
        }
        if (Objects.equals(dictData.getCode(),DicDataEnum.INTERHARMONIC_VOLTAGE.getCode())) {
            value = Integer.valueOf(rMpSurplusHarmonicDetailMS.stream().filter(temp ->temp.getInuharmOverDay()>0).count()+"");
        }
        if (Objects.equals(dictData.getCode(),DicDataEnum.PHASE_VOLTAGE.getCode())) {
            value = Integer.valueOf(rMpPartHarmonicDetailMS.stream().filter(temp ->temp.getUnbalanceOverDay()>0).count()+"");
        }
        if (Objects.equals(dictData.getCode(),DicDataEnum.FREQUENCY_DEV.getCode())) {
            value = Integer.valueOf(rMpPartHarmonicDetailMS.stream().filter(temp ->temp.getFreqOverDay()>0).count()+"");

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

        return value;
    }

}
