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

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.device.biz.pojo.dto.DeptGetChildrenMoreDTO;
import com.njcn.device.biz.pojo.dto.LineDevGetDTO;
import com.njcn.device.pms.pojo.po.RStatOrgPO;
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.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.*;
import com.njcn.prepare.harmonic.service.mysql.Impl.line.RMpPartHarmonicDetailDServiceImpl;
import com.njcn.prepare.harmonic.service.mysql.area.*;
import com.njcn.prepare.harmonic.service.mysql.line.IRMpPartHarmonicDetailDService;
import com.njcn.prepare.harmonic.utils.PublicUtil;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.enums.DicDataEnum;
import com.njcn.system.enums.DicDataTypeEnum;
import com.njcn.system.pojo.po.DictData;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.apache.poi.ss.formula.functions.T;
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.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
    DicDataFeignClient dicDataFeignClient;

    @Override
    public void handler(List<DeptGetChildrenMoreDTO> data, String dataDate, Integer type) {
        switch (type) {
            case 1:
                this.hanlderOracleYear (data,  dataDate,  type);
                break;
            case 2:
                this.hanlderOracleQtr (data,  dataDate,  type);
                break;
            case 3:
                this.hanlderM ( data,  dataDate,  type);
                break;
            case 5:
                this.hanlderDay ( data,  dataDate,  type);
                break;
            default:
                break;
        }
    }
    @Transactional
    @Async("asyncExecutor")
    @SneakyThrows
    private void hanlderOracleYear(List<DeptGetChildrenMoreDTO> data, String dataDate, Integer type) {
        List<RStatHarmonicYPO> rStatHarmonicYPOList = new ArrayList<>();
        Date date = DateUtil.parse(dataDate);
        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.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());

                    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("QUARTER( data_date)", PublicUtil.getNowQuarter (dataDate));

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

                    rMpSurplusHarmonicDetailMQueryWrapper.clear();
                    rMpSurplusHarmonicDetailMQueryWrapper.select("flicker_over_day", "inuharm_over_day").
                            in("measurement_point_id", collect1).
                            eq("QUARTER( data_date)", PublicUtil.getNowQuarter (dataDate));
                    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()).
                            eq("YEAR( data_date)", dataDate.substring(0,4));
                    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(date);
                        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()).
                                eq("QUARTER( data_date)", PublicUtil.getNowQuarter (dataDate));
                        List<RStatHarmonicMPO> list = rStatHarmonicMPOService.list(harmonicMPOQueryWrapper);
                        double asDouble = list.stream().mapToDouble(RStatHarmonicMPO::getOverLimitMeasurementAverage).average().getAsDouble();
                        asDouble = BigDecimal.valueOf(asDouble).setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
                        double asDouble1 = list.stream().mapToDouble(RStatHarmonicMPO::getOverLimitMeasurementRatioAverage).average().getAsDouble();
                        asDouble1 = BigDecimal.valueOf(asDouble1).setScale(2,   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("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("QUARTER( data_date)", PublicUtil.getNowQuarter (dataDate));

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

                    rMpSurplusHarmonicDetailMQueryWrapper.clear();
                    rMpSurplusHarmonicDetailMQueryWrapper.select("flicker_over_day", "inuharm_over_day").
                            in("measurement_point_id", collect1).
                            eq("QUARTER( data_date)", PublicUtil.getNowQuarter (dataDate));
                    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()).
                            eq("YEAR( data_date)", dataDate.substring(0,4));
                    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(date);
                        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()).
                                eq("QUARTER( data_date)", PublicUtil.getNowQuarter (dataDate));
                        List<RStatHarmonicMPO> list = rStatHarmonicMPOService.list(harmonicMPOQueryWrapper);
                        double asDouble = list.stream().mapToDouble(RStatHarmonicMPO::getOverLimitMeasurementAverage).average().getAsDouble();
                        asDouble = BigDecimal.valueOf(asDouble).setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
                        double asDouble1 = list.stream().mapToDouble(RStatHarmonicMPO::getOverLimitMeasurementRatioAverage).average().getAsDouble();
                        asDouble1 = BigDecimal.valueOf(asDouble1).setScale(2,   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);

        }
    }

    @Transactional
    @Async("asyncExecutor")
    @SneakyThrows
    private void hanlderOracleQtr(List<DeptGetChildrenMoreDTO> data, String dataDate, Integer type) {
        List<RStatHarmonicQPO> rStatHarmonicQPOList = new ArrayList<>();
        Date date = DateUtil.parse(dataDate);
        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.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());

                    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("QUARTER( data_date)", PublicUtil.getNowQuarter (dataDate));

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

                    rMpSurplusHarmonicDetailMQueryWrapper.clear();
                    rMpSurplusHarmonicDetailMQueryWrapper.select("flicker_over_day", "inuharm_over_day").
                            in("measurement_point_id", collect1).
                            eq("QUARTER( data_date)", PublicUtil.getNowQuarter (dataDate));
                    List<RMpSurplusHarmonicDetailM> rMpSurplusHarmonicDetailMS = 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()).
                            eq("QUARTER( data_date)", PublicUtil.getNowQuarter (dataDate));
                    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(date);
                        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()).
                                eq("QUARTER( data_date)", PublicUtil.getNowQuarter (dataDate));
                        List<RStatHarmonicMPO> list = rStatHarmonicMPOService.list(harmonicMPOQueryWrapper);
                        double asDouble = list.stream().mapToDouble(RStatHarmonicMPO::getOverLimitMeasurementAverage).average().getAsDouble();
                        asDouble = BigDecimal.valueOf(asDouble).setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
                        double asDouble1 = list.stream().mapToDouble(RStatHarmonicMPO::getOverLimitMeasurementRatioAverage).average().getAsDouble();
                        asDouble1 = BigDecimal.valueOf(asDouble1).setScale(2,   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(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("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("QUARTER( data_date)", PublicUtil.getNowQuarter (dataDate));

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

                    rMpSurplusHarmonicDetailMQueryWrapper.clear();
                    rMpSurplusHarmonicDetailMQueryWrapper.select("flicker_over_day", "inuharm_over_day").
                            in("measurement_point_id", collect1).
                            eq("QUARTER( data_date)", PublicUtil.getNowQuarter (dataDate));
                    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()).
                            eq("QUARTER( data_date)", PublicUtil.getNowQuarter (dataDate));
                    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(date);
                        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()).
                                eq("QUARTER( data_date)", PublicUtil.getNowQuarter (dataDate));
                        List<RStatHarmonicMPO> list = rStatHarmonicMPOService.list(harmonicMPOQueryWrapper);
                        double asDouble = list.stream().mapToDouble(RStatHarmonicMPO::getOverLimitMeasurementAverage).average().getAsDouble();
                        asDouble = BigDecimal.valueOf(asDouble).setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
                        double asDouble1 = list.stream().mapToDouble(RStatHarmonicMPO::getOverLimitMeasurementRatioAverage).average().getAsDouble();
                        asDouble1 = BigDecimal.valueOf(asDouble1).setScale(2,   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);

        }
    }

    @Transactional
    @Async("asyncExecutor")
    @SneakyThrows
    private void hanlderM(List<DeptGetChildrenMoreDTO> data, String dataDate, Integer type) {

        List<RStatHarmonicMPO> rStatHarmonicMPOList = new ArrayList<>();
        Date date = DateUtil.parse(dataDate);
        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.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());

                    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;
                        }
                        RStatHarmonicMPO rStatHarmonicMPO = new RStatHarmonicMPO();
                        rStatHarmonicMPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                        rStatHarmonicMPO.setDataDate(date);
                        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()).
                                eq("DATE_FORMAT( data_date ,'%Y-%m')",dataDate.substring(0, 7));
                        List<RStatHarmonicDPO> rStatHarmonicDPOList = rStatHarmonicDPOService.list(harmonicDPOQueryWrapper);
                        double asDouble = rStatHarmonicDPOList.stream().mapToDouble(RStatHarmonicDPO::getOverLimitMeasurement).average().getAsDouble();
                        asDouble = BigDecimal.valueOf(asDouble).setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
                        double asDouble1 = rStatHarmonicDPOList.stream().mapToDouble(RStatHarmonicDPO::getOverLimitMeasurementRatio).average().getAsDouble();
                        asDouble1 = BigDecimal.valueOf(asDouble1).setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
                        rStatHarmonicMPO.setOverLimitMeasurementAverage(asDouble);
                        double sum = rStatHarmonicDPOList.stream().mapToDouble(RStatHarmonicDPO::getOverLimitMeasurement).sum();
                        rStatHarmonicMPO.setOverLimitMeasurementRatioAverage(asDouble1);

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

                        rStatHarmonicMPO.setOverDay(i);

                        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("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", getmeasurementType(k, lineSortMap)).
                            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;
                        }
                        RStatHarmonicMPO rStatHarmonicMPO = new RStatHarmonicMPO();
                        rStatHarmonicMPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                        rStatHarmonicMPO.setDataDate(date);
                        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()).
                                eq("DATE_FORMAT( data_date ,'%Y-%m')",dataDate.substring(0, 7));
                        List<RStatHarmonicDPO> rStatHarmonicDPOList = rStatHarmonicDPOService.list(harmonicDPOQueryWrapper);
                        double asDouble = rStatHarmonicDPOList.stream().mapToDouble(RStatHarmonicDPO::getOverLimitMeasurement).average().getAsDouble();
                        asDouble = BigDecimal.valueOf(asDouble).setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
                        double asDouble1 = rStatHarmonicDPOList.stream().mapToDouble(RStatHarmonicDPO::getOverLimitMeasurementRatio).average().getAsDouble();
                        asDouble1 = BigDecimal.valueOf(asDouble1).setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
                        rStatHarmonicMPO.setOverLimitMeasurementAverage(asDouble);
                        double sum = rStatHarmonicDPOList.stream().mapToDouble(RStatHarmonicDPO::getOverLimitMeasurement).sum();
                        rStatHarmonicMPO.setOverLimitMeasurementRatioAverage(asDouble1);

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

                        rStatHarmonicMPO.setOverDay(i);

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

        }
    }

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


    @Transactional(rollbackFor = {Exception.class})
    @Async("asyncExecutor")
    @SneakyThrows
    private void hanlderDay(List<DeptGetChildrenMoreDTO> data, String dataDate, Integer type) {
        List<RStatHarmonicDPO> rStatHarmonicDPOList = new ArrayList<>();
        Date date = DateUtil.parse(dataDate);
        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)->{
                    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("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",collect1).
                            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;
                        }
                        RStatHarmonicDPO rStatHarmonicDPO = new RStatHarmonicDPO();
                        rStatHarmonicDPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                        rStatHarmonicDPO.setDataDate(date);
                        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(1.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("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", collect1).
                            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.DISTRIBUTION_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.NEG_CURRENT.getCode())) {
                            return;
                        }
                        RStatHarmonicDPO rStatHarmonicDPO = new RStatHarmonicDPO();
                        rStatHarmonicDPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                        rStatHarmonicDPO.setDataDate(date);
                        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(1.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);
        }


    }

    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;

    }
}
