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

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.event.pojo.po.RMpEventDetailD;
import com.njcn.event.pojo.po.RMpEventDetailM;
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.line.RMpEventDetailDMapper;
import com.njcn.prepare.harmonic.mapper.mysql.line.RMpEventDetailMMapper;
import com.njcn.prepare.harmonic.mapper.mysql.newalgorithm.RStatEventVoltageDPOMapper;
import com.njcn.prepare.harmonic.pojo.bo.CalculatedParam;
import com.njcn.prepare.harmonic.pojo.po.RMpEventDetailDNewPO;
import com.njcn.prepare.harmonic.pojo.po.RStatEventVoltageDPO;
import com.njcn.prepare.harmonic.pojo.po.RStatEventVoltageMPO;
import com.njcn.prepare.harmonic.service.mysql.newalgorithm.RStatEventVoltageDPOService;
import com.njcn.prepare.harmonic.service.mysql.newalgorithm.RStatEventVoltageMPOService;
import com.njcn.prepare.harmonic.service.mysql.newalgorithm.RStatEventVoltageService;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.enums.DicDataEnum;
import com.njcn.system.enums.DicDataTypeEnum;
import com.njcn.system.pojo.po.DictData;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

/**
 * <p>
 * 暂态指标统计（电压等级）
 * </p>
 *
 * @author zbj
 * @since 2023-06-09
 */
@Service
@RequiredArgsConstructor
public class RStatEventVoltageServiceImpl implements RStatEventVoltageService {

    private final DicDataFeignClient dicDataFeignClient;

    private final RMpEventDetailDMapper rMpEventDetailDMapper;

    private final RMpEventDetailMMapper rMpEventDetailMMapper;

    private final RStatEventVoltageDPOService rStatEventVoltageDPOService;

    private final RStatEventVoltageMPOService rStatEventVoltageMPOService;

    private final RStatEventVoltageDPOMapper rStatEventVoltageDPOMapper;

    private final RStatOrgDMapper rStatOrgDMapper;

    private final RStatOrgMMapper rStatOrgMMapper;


    @Override
    public void handleDay(CalculatedParam<DeptGetChildrenMoreDTO> calculatedParam) {
        List<RStatEventVoltageDPO> rStatEventVoltageDPOS = new ArrayList<>();
        DecimalFormat df = new DecimalFormat("0.00000");
        LocalDate localDate = LocalDateTimeUtil.parseDate(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN);


        /*获取暂态统计指标*/
        List<DictData> eventStatis = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.EVENT_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));

        /*获取监测点电压等级*/
        List<DictData> voltageList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.DEV_VOLTAGE_STAND.getCode()).getData();
        Map<String, DictData> voltageMap = voltageList.stream().collect(Collectors.toMap(DictData::getValue, dictData -> dictData));

        QueryWrapper<RMpEventDetailD> rMpEventDetailDPOQueryWrapper = new QueryWrapper<>();
        QueryWrapper<RStatOrgDPO> rStatOrgDPOQueryWrapper = new QueryWrapper<>();

        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> collect1 = v.stream().map(LineDevGetDTO::getPointId).collect(Collectors.toList());

                        rMpEventDetailDPOQueryWrapper.clear();
                        rMpEventDetailDPOQueryWrapper.
                                in("measurement_point_id", collect1).
                                eq("data_date",localDate);
                        List<RMpEventDetailD> rMpEventDetailDS = rMpEventDetailDMapper.selectList(rMpEventDetailDPOQueryWrapper);

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

                        eventStatis.forEach(dictData -> {
                                    if (Objects.equals(dictData.getCode(), DicDataEnum.DISTURBANCE.getCode())
                                            || Objects.equals(dictData.getCode(), DicDataEnum.OTHER.getCode())
                                            || Objects.equals(dictData.getCode(), DicDataEnum.RECORDING_WAVE.getCode())
                                            || Objects.equals(dictData.getCode(), DicDataEnum.TOTAL_INDICATORS.getCode())) {
                                        return;
                                    }

                                    List<Integer> transientStatisData = getTransientStatisDataNew(dictData, rMpEventDetailDS);
                                    RStatEventVoltageDPO rStatEventVoltageDPO = new RStatEventVoltageDPO();
                                    rStatEventVoltageDPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                                    rStatEventVoltageDPO.setDataDate(localDate);
                                    rStatEventVoltageDPO.setMeasurementTypeClass(k);
                                    rStatEventVoltageDPO.setEventType(dictData.getId());
                                    rStatEventVoltageDPO.setVoltageType(k1);
                                    rStatEventVoltageDPO.setEventMeasurement(transientStatisData.get(0));
                                    rStatEventVoltageDPO.setEventCount(transientStatisData.get(1));
                                    rStatEventVoltageDPO.setDataType(dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId());
//                                    if (Objects.isNull(rStatOrgDPO)) {
//                                        rStatEventVoltageDPO.setEventMeasurementRatio(0.00f);
//                                    } else {
//                                        if (rStatOrgDPO.getEffectiveMeasurement() == 0) {
//                                            rStatEventVoltageDPO.setEventMeasurementRatio(0.00f);
//                                        } else {
                                            String value = df.format(Double.valueOf(rStatEventVoltageDPO.getEventMeasurement() )/ collect1.size());
                                            rStatEventVoltageDPO.setEventMeasurementRatio(Float.parseFloat(value));
//                                        }
//                                    }
                                    rStatEventVoltageDPOS.add(rStatEventVoltageDPO);

                                }
                        );
                    });


                });
            }

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

                    collect2.forEach((k1, v1) -> {
                        List<String> collect1 = v1.stream().map(LineDevGetDTO::getPointId).collect(Collectors.toList());
                        rMpEventDetailDPOQueryWrapper.clear();
                        rMpEventDetailDPOQueryWrapper.
                                in("measurement_point_id", collect1).
                                eq("data_date",localDate);
                        List<RMpEventDetailD> rMpEventDetailDS = rMpEventDetailDMapper.selectList(rMpEventDetailDPOQueryWrapper);

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

                        eventStatis.forEach(dictData -> {
                            if (Objects.equals(dictData.getCode(), DicDataEnum.DISTURBANCE.getCode())
                                    || Objects.equals(dictData.getCode(), DicDataEnum.OTHER.getCode())
                                    || Objects.equals(dictData.getCode(), DicDataEnum.RECORDING_WAVE.getCode())
                                    || Objects.equals(dictData.getCode(), DicDataEnum.TOTAL_INDICATORS.getCode())) {
                                return;
                            }
                            List<Integer> transientStatisData = getTransientStatisDataNew(dictData, rMpEventDetailDS);
                            RStatEventVoltageDPO rStatEventVoltageDPO = new RStatEventVoltageDPO();
                            rStatEventVoltageDPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                            rStatEventVoltageDPO.setDataDate(localDate);
                            rStatEventVoltageDPO.setMeasurementTypeClass(getmeasurementType(k, lineSortMap));
                            rStatEventVoltageDPO.setEventType(dictData.getId());
                            rStatEventVoltageDPO.setVoltageType(k1);
                            rStatEventVoltageDPO.setEventMeasurement(transientStatisData.get(0));
                            rStatEventVoltageDPO.setEventCount(transientStatisData.get(1));
                            rStatEventVoltageDPO.setDataType(dataTypeMap.get(DicDataEnum.DISTRIBUTION_POINT.getCode()).getId());
//                            if (Objects.isNull(rStatOrgDPO)) {
//                                rStatEventVoltageDPO.setEventMeasurementRatio(0.00f);
//                            } else {
//                                if (rStatOrgDPO.getEffectiveMeasurement() == 0) {
//                                    rStatEventVoltageDPO.setEventMeasurementRatio(0.00f);
//                                } else {
                                    String value = df.format(Double.valueOf(rStatEventVoltageDPO.getEventMeasurement() )/ collect1.size());
                                    rStatEventVoltageDPO.setEventMeasurementRatio(Float.parseFloat(value));
//                                }
//                            }
                            rStatEventVoltageDPOS.add(rStatEventVoltageDPO);


                        });

                    });

                });
            }

        });
        if (!CollectionUtils.isEmpty(rStatEventVoltageDPOS)) {
            rStatEventVoltageDPOService.saveOrUpdateBatchByMultiId(rStatEventVoltageDPOS, 500);
        }
    }

    private List<Integer> getTransientStatisDataNew(DictData dictData, List<RMpEventDetailD> rMpEventDetailDS) {
        List<Integer> result = new ArrayList<>(2);
        Integer value = 0;
        Integer count = 0;
        if (Objects.equals(dictData.getCode(), DicDataEnum.VOLTAGE_DIP.getCode())) {
            value = Integer.valueOf(rMpEventDetailDS.stream().filter(temp -> temp.getSagTimes() > 0).count() + "");
            count = Integer.valueOf(rMpEventDetailDS.stream().mapToInt(RMpEventDetailD::getSagTimes).sum() + "");
        }
        if (Objects.equals(dictData.getCode(), DicDataEnum.VOLTAGE_RISE.getCode())) {
            value = Integer.valueOf(rMpEventDetailDS.stream().filter(temp -> temp.getSwellTimes() > 0).count() + "");
            count = Integer.valueOf(rMpEventDetailDS.stream().mapToInt(RMpEventDetailD::getSwellTimes).sum() + "");
        }
        if (Objects.equals(dictData.getCode(), DicDataEnum.SHORT_INTERRUPTIONS.getCode())) {
            value = Integer.valueOf(rMpEventDetailDS.stream().filter(temp -> temp.getInterruptTimes() > 0).count() + "");
            count = Integer.valueOf(rMpEventDetailDS.stream().mapToInt(RMpEventDetailD::getInterruptTimes).sum() + "");
        }
        result.add(value);
        result.add(count);
        return result;
    }

    @Override
    public void handleMonth(CalculatedParam<DeptGetChildrenMoreDTO> calculatedParam) {
        List<RStatEventVoltageMPO> rStatEventVoltageMPOS = new ArrayList<>();
        DecimalFormat df = new DecimalFormat("0.00000");

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


        /*获取暂态统计指标*/
        List<DictData> eventStatis = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.EVENT_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));

        /*获取监测点电压等级*/
        List<DictData> voltageList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.DEV_VOLTAGE_STAND.getCode()).getData();
        Map<String, DictData> voltageMap = voltageList.stream().collect(Collectors.toMap(DictData::getValue, dictData -> dictData));

        //详情表
        QueryWrapper<RMpEventDetailM> rMpEventDetailMPOQueryWrapper = new QueryWrapper<>();
        //org表
        QueryWrapper<RStatOrgMPO> rStatOrgMPOQueryWrapper = new QueryWrapper<>();
        //天表
        QueryWrapper<RStatEventVoltageDPO> rStatEventVoltageDPOQueryWrapper = new QueryWrapper<>();

        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> collect1 = v.stream().map(LineDevGetDTO::getPointId).collect(Collectors.toList());

                        rMpEventDetailMPOQueryWrapper.clear();
                        rMpEventDetailMPOQueryWrapper.
                                in("measurement_point_id", collect1).
                                between("data_date",begin,end);
                        List<RMpEventDetailM> rMpEventDetailMS = rMpEventDetailMMapper.selectList(rMpEventDetailMPOQueryWrapper);

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



                        eventStatis.forEach(dictData -> {
                            if (Objects.equals(dictData.getCode(), DicDataEnum.DISTURBANCE.getCode())
                                    || Objects.equals(dictData.getCode(), DicDataEnum.OTHER.getCode())
                                    || Objects.equals(dictData.getCode(), DicDataEnum.RECORDING_WAVE.getCode())
                                    || Objects.equals(dictData.getCode(), DicDataEnum.TOTAL_INDICATORS.getCode())) {
                                return;
                            }

                            //天表
                            rStatEventVoltageDPOQueryWrapper.clear();
                            rStatEventVoltageDPOQueryWrapper.
                                    eq("org_no", deptGetChildrenMoreDTO.getUnitId()).
                                    between("data_date",begin,end).
                                    eq("measurement_type_class", k).
                                    eq("event_type", dictData.getId()).
                                    eq("voltage_type",k1).
                                    eq("data_type", dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId());
                            List<RStatEventVoltageDPO> rStatEventVoltageDPOS = rStatEventVoltageDPOMapper.selectList(rStatEventVoltageDPOQueryWrapper);

                            //累计发生暂态监测点数
                            List<Integer> transientStatisDataNew = getTransientStatisDataNewByM(dictData, rMpEventDetailMS);

                            RStatEventVoltageMPO rStatEventVoltageMPO = new RStatEventVoltageMPO();
                            rStatEventVoltageMPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                            rStatEventVoltageMPO.setDataDate(localDate);
                            rStatEventVoltageMPO.setMeasurementTypeClass(k);
                            rStatEventVoltageMPO.setEventType(dictData.getId());
                            rStatEventVoltageMPO.setVoltageType(k1);
                            rStatEventVoltageMPO.setDataType(dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId());

                            /*3、日均发生暂态监测点数*/
                            double asDouble = rStatEventVoltageDPOS.stream().mapToDouble(RStatEventVoltageDPO::getEventMeasurement).average().getAsDouble();
                            asDouble = BigDecimal.valueOf(asDouble).setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
                            rStatEventVoltageMPO.setEventMeasurementAverage(Float.parseFloat(asDouble+""));

                            /*4、累计发生暂态监测点数*/
                            rStatEventVoltageMPO.setEventMeasurementAccrued(transientStatisDataNew.get(0));

                            int eventMeasurementsum = rStatEventVoltageDPOS.stream().mapToInt(RStatEventVoltageDPO::getEventMeasurement).sum();
                            int eventCountsum = rStatEventVoltageDPOS.stream().mapToInt(RStatEventVoltageDPO::getEventCount).sum();
                            /*5、暂态指标发生频次*/
                            if (eventMeasurementsum==0){
                                rStatEventVoltageMPO.setEventFreq(0.00f);
                            }else {
                                String value = df.format((float) eventCountsum / eventMeasurementsum);
                                rStatEventVoltageMPO.setEventFreq(Float.parseFloat(value));

                            }

                            /*6、暂态指标发生次数*/
                            rStatEventVoltageMPO.setEventCount(eventCountsum);
                            /*7、日均发生暂态监测点数占比*/
                            double asDouble2 = rStatEventVoltageDPOS.stream().mapToDouble(RStatEventVoltageDPO::getEventMeasurementRatio).average().getAsDouble();
                            asDouble2 = BigDecimal.valueOf(asDouble2).setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
                            rStatEventVoltageMPO.setEventMeasurementRatioAverage(Float.parseFloat(asDouble2+""));

//                            if (Objects.isNull(rStatOrgMPO)) {
//                                rStatEventVoltageMPO.setEventMeasurementRatioAccrued(0.00f);
//                            } else {
//                                if (rStatOrgMPO.getEffectiveMeasurementAccrued() == 0) {
//                                    rStatEventVoltageMPO.setEventMeasurementRatioAccrued(0.00f);
//                                } else {
                                    String value = df.format(Double.valueOf(rStatEventVoltageMPO.getEventMeasurementAccrued()) / collect1.size());
                                    rStatEventVoltageMPO.setEventMeasurementRatioAccrued(Float.parseFloat(value));
//                                }
//                            }
                            rStatEventVoltageMPOS.add(rStatEventVoltageMPO);
                        });
                    });


                });
            }

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

                    collect2.forEach((k1, v1) -> {
                        List<String> collect1 = v.stream().map(LineDevGetDTO::getPointId).collect(Collectors.toList());

                        rMpEventDetailMPOQueryWrapper.clear();
                        rMpEventDetailMPOQueryWrapper.
                                in("measurement_point_id", collect1).
                                between("data_date",begin,end);
                        List<RMpEventDetailM> rMpEventDetailMS = rMpEventDetailMMapper.selectList(rMpEventDetailMPOQueryWrapper);

                        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);
                        eventStatis.forEach(dictData -> {
                            if (Objects.equals(dictData.getCode(), DicDataEnum.DISTURBANCE.getCode())
                                    || Objects.equals(dictData.getCode(), DicDataEnum.OTHER.getCode())
                                    || Objects.equals(dictData.getCode(), DicDataEnum.RECORDING_WAVE.getCode())
                                    || Objects.equals(dictData.getCode(), DicDataEnum.TOTAL_INDICATORS.getCode())) {
                                return;
                            }
                            //天表
                            rStatEventVoltageDPOQueryWrapper.clear();
                            rStatEventVoltageDPOQueryWrapper.
                                    eq("org_no", deptGetChildrenMoreDTO.getUnitId()).
                                    between("data_date",begin,end).
                                    eq("measurement_type_class", k).
                                    eq("event_type", dictData.getId()).
                                    eq("voltage_type",k1).
                                    eq("data_type", dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId());
                            List<RStatEventVoltageDPO> rStatEventVoltageDPOS = rStatEventVoltageDPOMapper.selectList(rStatEventVoltageDPOQueryWrapper);

                            RStatEventVoltageMPO rStatEventVoltageMPO = new RStatEventVoltageMPO();
                            rStatEventVoltageMPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                            rStatEventVoltageMPO.setDataDate(localDate);
                            rStatEventVoltageMPO.setMeasurementTypeClass(getmeasurementType(k, lineSortMap));
                            rStatEventVoltageMPO.setEventType(dictData.getId());
                            rStatEventVoltageMPO.setVoltageType(k1);
                            rStatEventVoltageMPO.setDataType(dataTypeMap.get(DicDataEnum.DISTRIBUTION_POINT.getCode()).getId());

                            List<Integer> transientStatisDataNew = getTransientStatisDataNewByM(dictData, rMpEventDetailMS);

                            /*3、日均发生暂态监测点数*/
                            double asDouble = rStatEventVoltageDPOS.stream().mapToDouble(RStatEventVoltageDPO::getEventMeasurement).average().getAsDouble();
                            asDouble = BigDecimal.valueOf(asDouble).setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
                            rStatEventVoltageMPO.setEventMeasurementAverage(Float.parseFloat(asDouble+""));

                            /*4、累计发生暂态监测点数*/
                            rStatEventVoltageMPO.setEventMeasurementAccrued(transientStatisDataNew.get(0));

                            int eventMeasurementsum = rStatEventVoltageDPOS.stream().mapToInt(RStatEventVoltageDPO::getEventMeasurement).sum();
                            int eventCountsum = rStatEventVoltageDPOS.stream().mapToInt(RStatEventVoltageDPO::getEventCount).sum();
                            /*5、暂态指标发生频次*/
                            if (eventMeasurementsum==0){
                                rStatEventVoltageMPO.setEventFreq(0.00f);
                            }else {
                                String value = df.format((float) eventCountsum / eventMeasurementsum);
                                rStatEventVoltageMPO.setEventFreq(Float.parseFloat(value));

                            }

                            /*6、暂态指标发生次数*/
                            rStatEventVoltageMPO.setEventCount(eventCountsum);
                            /*7、日均发生暂态监测点数占比*/
                            double asDouble2 = rStatEventVoltageDPOS.stream().mapToDouble(RStatEventVoltageDPO::getEventMeasurementRatio).average().getAsDouble();
                            asDouble2 = BigDecimal.valueOf(asDouble2).setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
                            rStatEventVoltageMPO.setEventMeasurementRatioAverage(Float.parseFloat(asDouble2+""));

//                            if (Objects.isNull(rStatOrgMPO)) {
//                                rStatEventVoltageMPO.setEventMeasurementRatioAccrued(0.00f);
//                            } else {
//                                if (rStatOrgMPO.getEffectiveMeasurementAccrued() == 0) {
//                                    rStatEventVoltageMPO.setEventMeasurementRatioAccrued(0.00f);
//                                } else {
                                    String value = df.format(Double.valueOf(rStatEventVoltageMPO.getEventMeasurementAccrued()) / collect1.size());
                                    rStatEventVoltageMPO.setEventMeasurementRatioAccrued(Float.parseFloat(value));
//                                }
//                            }
                            rStatEventVoltageMPOS.add(rStatEventVoltageMPO);
                        });

                    });
                });
            }

        });
        if (!CollectionUtils.isEmpty(rStatEventVoltageMPOS)) {
            rStatEventVoltageMPOService.saveOrUpdateBatchByMultiId(rStatEventVoltageMPOS, 500);
        }
    }

    private List<Integer> getTransientStatisDataNewByM(DictData dictData, List<RMpEventDetailM> rMpEventDetailMS) {
        List<Integer> result = new ArrayList<>(2);
        Integer value = 0;
        Integer count = 0;
        if (Objects.equals(dictData.getCode(), DicDataEnum.VOLTAGE_DIP.getCode())) {
            value = Integer.valueOf(rMpEventDetailMS.stream().filter(temp -> temp.getSagTimes() > 0).count() + "");
            count = Integer.valueOf(rMpEventDetailMS.stream().mapToInt(RMpEventDetailM::getSagTimes).sum() + "");
        }
        if (Objects.equals(dictData.getCode(), DicDataEnum.VOLTAGE_RISE.getCode())) {
            value = Integer.valueOf(rMpEventDetailMS.stream().filter(temp -> temp.getSwellTimes() > 0).count() + "");
            count = Integer.valueOf(rMpEventDetailMS.stream().mapToInt(RMpEventDetailM::getSwellTimes).sum() + "");
        }
        if (Objects.equals(dictData.getCode(), DicDataEnum.SHORT_INTERRUPTIONS.getCode())) {
            value = Integer.valueOf(rMpEventDetailMS.stream().filter(temp -> temp.getInterruptTimes() > 0).count() + "");
            count = Integer.valueOf(rMpEventDetailMS.stream().mapToInt(RMpEventDetailM::getInterruptTimes).sum() + "");
        }
        result.add(value);
        result.add(count);
        return result;
    }

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


    @Override
    public void handler(List<DeptGetChildrenMoreDTO> data, String dataDate, Integer type) {
        switch (type) {
            case 3:
                this.rStartEventVoltageMHandler(data, dataDate, type);
                break;
            case 5:
                this.rStartEventVoltageDHandler(data, dataDate, type);
                break;
            default:
                break;
        }
    }



    //天表
    public void rStartEventVoltageDHandler(List<DeptGetChildrenMoreDTO> data, String dataDate, Integer type) {
        List<RStatEventVoltageDPO> rStatEventVoltageDPOS = new ArrayList<>();
        DecimalFormat df = new DecimalFormat("0.00");
        Date date = DateUtil.parse(dataDate);
        LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

        /*获取暂态统计指标*/
        List<DictData> eventStatis = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.EVENT_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));

        /*获取监测点电压等级*/
        List<DictData> voltageList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.DEV_VOLTAGE_STAND.getCode()).getData();
        Map<String, DictData> voltageMap = voltageList.stream().collect(Collectors.toMap(DictData::getValue, dictData -> dictData));

        QueryWrapper<RMpEventDetailD> rMpEventDetailDPOQueryWrapper = new QueryWrapper<>();
        QueryWrapper<RStatOrgDPO> rStatOrgDPOQueryWrapper = new QueryWrapper<>();

        data.forEach(deptGetChildrenMoreDTO -> {

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

                    rMpEventDetailDPOQueryWrapper.clear();
                    rMpEventDetailDPOQueryWrapper.
                            in("measurement_point_id", collect1).
                            eq("DATE_FORMAT( data_date ,'%Y-%m-%d')", dataDate);
                    List<RMpEventDetailD> rMpEventDetailDS = rMpEventDetailDMapper.selectList(rMpEventDetailDPOQueryWrapper);

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

                    //循环拷贝
                    List<RMpEventDetailDNewPO> poList = new ArrayList<>();
                    for (RMpEventDetailD rMpEventDetailD : rMpEventDetailDS) {
                        RMpEventDetailDNewPO po = new RMpEventDetailDNewPO();
                        BeanUtils.copyProperties(rMpEventDetailD, po);
                        poList.add(po);
                    }
                    for (RMpEventDetailDNewPO rMpEventDetailDNewPO : poList) {
                        for (LineDevGetDTO lineDevGetDTO : v) {
                            if (Objects.equals(lineDevGetDTO.getPointId(), rMpEventDetailDNewPO.getMeasurementPointId())) {
                                rMpEventDetailDNewPO.setVoltage(lineDevGetDTO.getVoltageLevel());
                            }
                        }
                    }
                    eventStatis.forEach(dictData -> {
                                if (Objects.equals(dictData.getCode(), DicDataEnum.DISTURBANCE.getCode())
                                        || Objects.equals(dictData.getCode(), DicDataEnum.OTHER.getCode())
                                        || Objects.equals(dictData.getCode(), DicDataEnum.RECORDING_WAVE.getCode())
                                        || Objects.equals(dictData.getCode(), DicDataEnum.TOTAL_INDICATORS.getCode())) {
                                    return;
                                }
                                //电压 
                                voltageMap.forEach((k1, v1) -> {
                                    List<RMpEventDetailDNewPO> poResult = poList.stream().filter(p -> Objects.equals(p.getVoltage(), k1)).collect(Collectors.toList());
                                    if (CollectionUtils.isEmpty(poResult)) {
                                        return;
                                    }
                                    List<Integer> transientStatisData = getTransientStatisData(dictData, poResult);
                                    RStatEventVoltageDPO rStatEventVoltageDPO = new RStatEventVoltageDPO();
                                    rStatEventVoltageDPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                                    rStatEventVoltageDPO.setDataDate(localDate);
                                    rStatEventVoltageDPO.setMeasurementTypeClass(getmeasurementType(k, lineSortMap));
                                    rStatEventVoltageDPO.setEventType(dictData.getId());
                                    rStatEventVoltageDPO.setVoltageType(v1.getId());
                                    rStatEventVoltageDPO.setEventMeasurement(transientStatisData.get(0));
                                    rStatEventVoltageDPO.setEventCount(transientStatisData.get(1));
                                    rStatEventVoltageDPO.setDataType(dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId());
                                    if (Objects.isNull(rStatOrgDPO)) {
                                        rStatEventVoltageDPO.setEventMeasurementRatio(1.00f);
                                    } else {
                                        if (rStatOrgDPO.getEffectiveMeasurement() == 0) {
                                            rStatEventVoltageDPO.setEventMeasurementRatio(1.00f);
                                        } else {
                                            String value = df.format(rStatEventVoltageDPO.getEventMeasurement() / rStatOrgDPO.getEffectiveMeasurement().floatValue());
                                            rStatEventVoltageDPO.setEventMeasurementRatio(Float.parseFloat(value));
                                        }
                                    }
                                    rStatEventVoltageDPOS.add(rStatEventVoltageDPO);
                                });
                            }
                    );
                });
            }

            /*配网*/
            if (!CollectionUtils.isEmpty(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());
                    rMpEventDetailDPOQueryWrapper.clear();
                    rMpEventDetailDPOQueryWrapper.
                            in("measurement_point_id", collect1).
                            eq("DATE_FORMAT( data_date ,'%Y-%m-%d')", dataDate);
                    List<RMpEventDetailD> rMpEventDetailDS = rMpEventDetailDMapper.selectList(rMpEventDetailDPOQueryWrapper);

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

                    //循环拷贝
                    List<RMpEventDetailDNewPO> poList = new ArrayList<>();
                    for (RMpEventDetailD rMpEventDetailD : rMpEventDetailDS) {
                        RMpEventDetailDNewPO po = new RMpEventDetailDNewPO();
                        BeanUtils.copyProperties(rMpEventDetailD, po);
                        poList.add(po);
                    }
                    for (RMpEventDetailDNewPO rMpEventDetailDNewPO : poList) {
                        for (LineDevGetDTO lineDevGetDTO : v) {
                            if (Objects.equals(lineDevGetDTO.getPointId(), rMpEventDetailDNewPO.getMeasurementPointId())) {
                                rMpEventDetailDNewPO.setVoltage(lineDevGetDTO.getVoltageLevel());
                            }
                        }
                    }
                    eventStatis.forEach(dictData -> {
                                if (Objects.equals(dictData.getCode(), DicDataEnum.DISTURBANCE.getCode())
                                        || Objects.equals(dictData.getCode(), DicDataEnum.OTHER.getCode())
                                        || Objects.equals(dictData.getCode(), DicDataEnum.RECORDING_WAVE.getCode())
                                        || Objects.equals(dictData.getCode(), DicDataEnum.TOTAL_INDICATORS.getCode())) {
                                    return;
                                }
                                //电压
                                voltageMap.forEach((k1, v1) -> {
                                    List<RMpEventDetailDNewPO> poResult = poList.stream().filter(p -> Objects.equals(p.getVoltage(), k1)).collect(Collectors.toList());
                                    if (CollectionUtils.isEmpty(poResult)) {
                                        return;
                                    }
                                    List<Integer> transientStatisData = getTransientStatisData(dictData, poResult);
                                    RStatEventVoltageDPO rStatEventVoltageDPO = new RStatEventVoltageDPO();
                                    rStatEventVoltageDPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                                    rStatEventVoltageDPO.setDataDate(localDate);
                                    rStatEventVoltageDPO.setMeasurementTypeClass(getmeasurementType(k, lineSortMap));
                                    rStatEventVoltageDPO.setEventType(dictData.getId());
                                    rStatEventVoltageDPO.setVoltageType(v1.getId());
                                    rStatEventVoltageDPO.setEventMeasurement(transientStatisData.get(0));
                                    rStatEventVoltageDPO.setEventCount(transientStatisData.get(1));
                                    rStatEventVoltageDPO.setDataType(dataTypeMap.get(DicDataEnum.DISTRIBUTION_POINT.getCode()).getId());
                                    if (Objects.isNull(rStatOrgDPO)) {
                                        rStatEventVoltageDPO.setEventMeasurementRatio(1.00f);
                                    } else {
                                        if (rStatOrgDPO.getEffectiveMeasurement() == 0) {
                                            rStatEventVoltageDPO.setEventMeasurementRatio(1.00f);
                                        } else {
                                            String value = df.format(rStatEventVoltageDPO.getEventMeasurement() / rStatOrgDPO.getEffectiveMeasurement().floatValue());
                                            rStatEventVoltageDPO.setEventMeasurementRatio(Float.parseFloat(value));
                                        }
                                    }
                                    rStatEventVoltageDPOS.add(rStatEventVoltageDPO);
                                });
                            }
                    );
                });
            }

        });
        if (!CollectionUtils.isEmpty(rStatEventVoltageDPOS)) {
            rStatEventVoltageDPOService.saveOrUpdateBatchByMultiId(rStatEventVoltageDPOS, 500);
        }
    }

    //月表
    public void rStartEventVoltageMHandler(List<DeptGetChildrenMoreDTO> data, String dataDate, Integer type) {
        List<RStatEventVoltageMPO> rStatEventVoltageMPOS = new ArrayList<>();
        DecimalFormat df = new DecimalFormat("0.00");
        Date date = DateUtil.parse(dataDate);
        LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

        /*获取暂态统计指标*/
        List<DictData> eventStatis = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.EVENT_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));

        /*获取监测点电压等级*/
        List<DictData> voltageList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.DEV_VOLTAGE_STAND.getCode()).getData();
        Map<String, DictData> voltageMap = voltageList.stream().collect(Collectors.toMap(DictData::getValue, dictData -> dictData));

        //详情表
        QueryWrapper<RMpEventDetailM> rMpEventDetailMPOQueryWrapper = new QueryWrapper<>();
        //org表
        QueryWrapper<RStatOrgMPO> rStatOrgMPOQueryWrapper = new QueryWrapper<>();
        //天表
        QueryWrapper<RStatEventVoltageDPO> rStatEventVoltageDPOQueryWrapper = new QueryWrapper<>();

        data.forEach(deptGetChildrenMoreDTO -> {

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

                    rMpEventDetailMPOQueryWrapper.clear();
                    rMpEventDetailMPOQueryWrapper.
                            in("measurement_point_id", collect1).
                            eq("DATE_FORMAT( data_date ,'%Y-%m')", dataDate.substring(0, 7));
                    List<RMpEventDetailM> rMpEventDetailMS = rMpEventDetailMMapper.selectList(rMpEventDetailMPOQueryWrapper);

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

                    //天表
                    rStatEventVoltageDPOQueryWrapper.clear();
                    rStatEventVoltageDPOQueryWrapper.
                            eq("org_no", deptGetChildrenMoreDTO.getUnitId()).
                            eq("DATE_FORMAT( data_date ,'%Y-%m')", dataDate.substring(0, 7)).
                            eq("measurement_type_class", getmeasurementType(k, lineSortMap)).
                            eq("data_type", dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId());
                    List<RStatEventVoltageDPO> rStatEventVoltageDPOS = rStatEventVoltageDPOMapper.selectList(rStatEventVoltageDPOQueryWrapper);


                    //求各个类型的平均值
                    Map<String, Map<String, Double>> avgEM = rStatEventVoltageDPOS.stream()
                            .collect(Collectors.groupingBy(RStatEventVoltageDPO::getEventType,
                                    Collectors.groupingBy(RStatEventVoltageDPO::getVoltageType, Collectors.averagingInt(RStatEventVoltageDPO::getEventMeasurement))));

                    //event_measurement的和
                    Map<String, Map<String, Integer>> sumEM = rStatEventVoltageDPOS.stream()
                            .collect(Collectors.groupingBy(RStatEventVoltageDPO::getEventType,
                                    Collectors.groupingBy(RStatEventVoltageDPO::getVoltageType, Collectors.summingInt(RStatEventVoltageDPO::getEventMeasurement))));

                    //event_count
                    Map<String, Map<String, Integer>> sumEC = rStatEventVoltageDPOS.stream()
                            .collect(Collectors.groupingBy(RStatEventVoltageDPO::getEventType,
                                    Collectors.groupingBy(RStatEventVoltageDPO::getVoltageType, Collectors.summingInt(RStatEventVoltageDPO::getEventCount))));

                    //event_measurement_ratio
                    Map<String, Map<String, Double>> avgEMR = rStatEventVoltageDPOS.stream()
                            .collect(Collectors.groupingBy(RStatEventVoltageDPO::getEventType,
                                    Collectors.groupingBy(RStatEventVoltageDPO::getVoltageType, Collectors.averagingDouble(RStatEventVoltageDPO::getEventMeasurementRatio))));

                    //循环拷贝
                    List<RMpEventDetailMNewPO> poList = new ArrayList<>();
                    for (RMpEventDetailM rMpEventDetailM : rMpEventDetailMS) {
                        RMpEventDetailMNewPO po = new RMpEventDetailMNewPO();
                        BeanUtils.copyProperties(rMpEventDetailM, po);
                        poList.add(po);
                    }
                    for (RMpEventDetailMNewPO rMpEventDetailMNewPO : poList) {
                        for (LineDevGetDTO lineDevGetDTO : v) {
                            if (Objects.equals(lineDevGetDTO.getPointId(), rMpEventDetailMNewPO.getMeasurementPointId())) {
                                rMpEventDetailMNewPO.setVoltage(lineDevGetDTO.getVoltageLevel());
                            }
                        }
                    }
                    eventStatis.forEach(dictData -> {
                        if (Objects.equals(dictData.getCode(), DicDataEnum.DISTURBANCE.getCode())
                                || Objects.equals(dictData.getCode(), DicDataEnum.OTHER.getCode())
                                || Objects.equals(dictData.getCode(), DicDataEnum.RECORDING_WAVE.getCode())
                                || Objects.equals(dictData.getCode(), DicDataEnum.TOTAL_INDICATORS.getCode())) {
                            return;
                        }
                        //电压
                        voltageMap.forEach((k1, v1) -> {
                            List<RMpEventDetailMNewPO> poResult = poList.stream().filter(p -> Objects.equals(p.getVoltage(), k1)).collect(Collectors.toList());
                            if (CollectionUtils.isEmpty(poResult)) {
                                return;
                            }
                            //累计发生暂态监测点数
                            Integer statisData = getStatisData(dictData, k1, poResult);

                            RStatEventVoltageMPO rStatEventVoltageMPO = new RStatEventVoltageMPO();
                            rStatEventVoltageMPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                            rStatEventVoltageMPO.setDataDate(localDate);
                            rStatEventVoltageMPO.setMeasurementTypeClass(getmeasurementType(k, lineSortMap));
                            rStatEventVoltageMPO.setEventType(dictData.getId());
                            rStatEventVoltageMPO.setVoltageType(v1.getId());

                            rStatEventVoltageMPO.setEventMeasurementAverage(Float.parseFloat(df.format(avgEM.get(dictData.getId()).get(v1.getId()))));
                            rStatEventVoltageMPO.setEventMeasurementAccrued(statisData);
                            rStatEventVoltageMPO.setEventCount(sumEC.get(dictData.getId()).get(v1.getId()));
                            rStatEventVoltageMPO.setEventMeasurementRatioAverage(Float.parseFloat(df.format(avgEMR.get(dictData.getId()).get(v1.getId()))));
                            rStatEventVoltageMPO.setDataType(dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId());

                            if (sumEM.get(dictData.getId()).get(v1.getId()) == 0) {
                                rStatEventVoltageMPO.setEventFreq(1.00f);
                            } else {
                                String value = df.format(sumEC.get(dictData.getId()).get(v1.getId()) / sumEM.get(dictData.getId()).get(v1.getId()).floatValue());
                                rStatEventVoltageMPO.setEventFreq(Float.parseFloat(value));
                            }
                            if (Objects.isNull(rStatOrgMPO)) {
                                rStatEventVoltageMPO.setEventMeasurementRatioAccrued(1.00f);
                            } else {
                                if (rStatOrgMPO.getEffectiveMeasurementAccrued() == 0) {
                                    rStatEventVoltageMPO.setEventMeasurementRatioAccrued(1.00f);
                                } else {
                                    String value = df.format(statisData / rStatOrgMPO.getEffectiveMeasurementAccrued().floatValue());
                                    rStatEventVoltageMPO.setEventMeasurementRatioAccrued(Float.parseFloat(value));
                                }
                            }
                            rStatEventVoltageMPOS.add(rStatEventVoltageMPO);
                        });
                    });
                });
            }

            /*配网*/
            if (!CollectionUtils.isEmpty(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());
                    rMpEventDetailMPOQueryWrapper.clear();
                    rMpEventDetailMPOQueryWrapper.
                            in("measurement_point_id", collect1).
                            eq("DATE_FORMAT( data_date ,'%Y-%m')", dataDate.substring(0, 7));
                    List<RMpEventDetailM> rMpEventDetailMS = rMpEventDetailMMapper.selectList(rMpEventDetailMPOQueryWrapper);

                    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.DISTRIBUTION_POINT.getCode()).getId()).
                            eq("DATE_FORMAT( data_date ,'%Y-%m')", dataDate.substring(0, 7));
                    RStatOrgMPO rStatOrgMPO = rStatOrgMMapper.selectOne(rStatOrgMPOQueryWrapper);

                    //天表
                    rStatEventVoltageDPOQueryWrapper.clear();
                    rStatEventVoltageDPOQueryWrapper.
                            eq("org_no", deptGetChildrenMoreDTO.getUnitId()).
                            eq("DATE_FORMAT( data_date ,'%Y-%m')", dataDate.substring(0, 7)).
                            eq("measurement_type_class", getmeasurementType(k, lineSortMap)).
                            eq("data_type", dataTypeMap.get(DicDataEnum.DISTRIBUTION_POINT.getCode()).getId());
                    List<RStatEventVoltageDPO> rStatEventVoltageDPOS = rStatEventVoltageDPOMapper.selectList(rStatEventVoltageDPOQueryWrapper);


                    //求各个类型的平均值
                    Map<String, Map<String, Double>> avgEM = rStatEventVoltageDPOS.stream()
                            .collect(Collectors.groupingBy(RStatEventVoltageDPO::getEventType,
                                    Collectors.groupingBy(RStatEventVoltageDPO::getVoltageType, Collectors.averagingInt(RStatEventVoltageDPO::getEventMeasurement))));

                    //event_measurement的和
                    Map<String, Map<String, Integer>> sumEM = rStatEventVoltageDPOS.stream()
                            .collect(Collectors.groupingBy(RStatEventVoltageDPO::getEventType,
                                    Collectors.groupingBy(RStatEventVoltageDPO::getVoltageType, Collectors.summingInt(RStatEventVoltageDPO::getEventMeasurement))));

                    //event_count
                    Map<String, Map<String, Integer>> sumEC = rStatEventVoltageDPOS.stream()
                            .collect(Collectors.groupingBy(RStatEventVoltageDPO::getEventType,
                                    Collectors.groupingBy(RStatEventVoltageDPO::getVoltageType, Collectors.summingInt(RStatEventVoltageDPO::getEventCount))));

                    //event_measurement_ratio
                    Map<String, Map<String, Double>> avgEMR = rStatEventVoltageDPOS.stream()
                            .collect(Collectors.groupingBy(RStatEventVoltageDPO::getEventType,
                                    Collectors.groupingBy(RStatEventVoltageDPO::getVoltageType, Collectors.averagingDouble(RStatEventVoltageDPO::getEventMeasurementRatio))));

                    //循环拷贝
                    List<RMpEventDetailMNewPO> poList = new ArrayList<>();
                    for (RMpEventDetailM rMpEventDetailM : rMpEventDetailMS) {
                        RMpEventDetailMNewPO po = new RMpEventDetailMNewPO();
                        BeanUtils.copyProperties(rMpEventDetailM, po);
                        poList.add(po);
                    }
                    for (RMpEventDetailMNewPO rMpEventDetailMNewPO : poList) {
                        for (LineDevGetDTO lineDevGetDTO : v) {
                            if (Objects.equals(lineDevGetDTO.getPointId(), rMpEventDetailMNewPO.getMeasurementPointId())) {
                                rMpEventDetailMNewPO.setVoltage(lineDevGetDTO.getVoltageLevel());
                            }
                        }
                    }
                    eventStatis.forEach(dictData -> {
                        if (Objects.equals(dictData.getCode(), DicDataEnum.DISTURBANCE.getCode())
                                || Objects.equals(dictData.getCode(), DicDataEnum.OTHER.getCode())
                                || Objects.equals(dictData.getCode(), DicDataEnum.RECORDING_WAVE.getCode())
                                || Objects.equals(dictData.getCode(), DicDataEnum.TOTAL_INDICATORS.getCode())) {
                            return;
                        }
                        //电压
                        voltageMap.forEach((k1, v1) -> {
                            List<RMpEventDetailMNewPO> poResult = poList.stream().filter(p -> Objects.equals(p.getVoltage(), k1)).collect(Collectors.toList());
                            if (CollectionUtils.isEmpty(poResult)) {
                                return;
                            }
                            //累计发生暂态监测点数
                            Integer statisData = getStatisData(dictData, k1, poResult);

                            RStatEventVoltageMPO rStatEventVoltageMPO = new RStatEventVoltageMPO();
                            rStatEventVoltageMPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                            rStatEventVoltageMPO.setDataDate(localDate);
                            rStatEventVoltageMPO.setMeasurementTypeClass(getmeasurementType(k, lineSortMap));
                            rStatEventVoltageMPO.setEventType(dictData.getId());
                            rStatEventVoltageMPO.setVoltageType(v1.getId());

                            rStatEventVoltageMPO.setEventMeasurementAverage(Float.parseFloat(df.format(avgEM.get(dictData.getId()).get(v1.getId()))));
                            rStatEventVoltageMPO.setEventMeasurementAccrued(statisData);
                            rStatEventVoltageMPO.setEventCount(sumEC.get(dictData.getId()).get(v1.getId()));
                            rStatEventVoltageMPO.setEventMeasurementRatioAverage(Float.parseFloat(df.format(avgEMR.get(dictData.getId()).get(v1.getId()))));
                            rStatEventVoltageMPO.setDataType(dataTypeMap.get(DicDataEnum.DISTRIBUTION_POINT.getCode()).getId());

                            if (sumEM.get(dictData.getId()).get(v1.getId()) == 0) {
                                rStatEventVoltageMPO.setEventFreq(1.00f);
                            } else {
                                String value = df.format(sumEC.get(dictData.getId()).get(v1.getId()) / sumEM.get(dictData.getId()).get(v1.getId()).floatValue());
                                rStatEventVoltageMPO.setEventFreq(Float.parseFloat(value));
                            }
                            if (Objects.isNull(rStatOrgMPO)) {
                                rStatEventVoltageMPO.setEventMeasurementRatioAccrued(1.00f);
                            } else {
                                if (rStatOrgMPO.getEffectiveMeasurementAccrued() == 0) {
                                    rStatEventVoltageMPO.setEventMeasurementRatioAccrued(1.00f);
                                } else {
                                    String value = df.format(statisData / rStatOrgMPO.getEffectiveMeasurementAccrued().floatValue());
                                    rStatEventVoltageMPO.setEventMeasurementRatioAccrued(Float.parseFloat(value));
                                }
                            }
                            rStatEventVoltageMPOS.add(rStatEventVoltageMPO);
                        });
                    });
                });
            }

        });
        if (!CollectionUtils.isEmpty(rStatEventVoltageMPOS)) {
            rStatEventVoltageMPOService.saveOrUpdateBatchByMultiId(rStatEventVoltageMPOS, 500);
        }
    }


    //获取监测点类型 1/2/3类字典id
    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 List<Integer> getTransientStatisData(DictData dictData, List<RMpEventDetailDNewPO> list) {
        List<Integer> result = new ArrayList<>(2);
        Integer value = 0;
        Integer count = 0;
        if (Objects.equals(dictData.getCode(), DicDataEnum.VOLTAGE_DIP.getCode())) {
            value = Integer.valueOf(list.stream().filter(temp -> temp.getSagTimes() > 0).count() + "");
            count = Integer.valueOf(list.stream().mapToInt(RMpEventDetailDNewPO::getSagTimes).sum() + "");
        }
        if (Objects.equals(dictData.getCode(), DicDataEnum.VOLTAGE_RISE.getCode())) {
            value = Integer.valueOf(list.stream().filter(temp -> temp.getSwellTimes() > 0).count() + "");
            count = Integer.valueOf(list.stream().mapToInt(RMpEventDetailDNewPO::getSwellTimes).sum() + "");
        }
        if (Objects.equals(dictData.getCode(), DicDataEnum.SHORT_INTERRUPTIONS.getCode())) {
            value = Integer.valueOf(list.stream().filter(temp -> temp.getInterruptTimes() > 0).count() + "");
            count = Integer.valueOf(list.stream().mapToInt(RMpEventDetailDNewPO::getInterruptTimes).sum() + "");
        }
        result.add(value);
        result.add(count);
        return result;

    }

    //获取暂态各个类型发生的监测点数量
    private Integer getStatisData(DictData dictData, String voltage, List<RMpEventDetailMNewPO> list) {
        Integer value = 0;
        if (Objects.equals(dictData.getCode(), DicDataEnum.VOLTAGE_DIP.getCode())) {
            value = Integer.valueOf(list.stream().filter(temp -> temp.getSagTimes() > 0 && Objects.equals(temp.getVoltage(), voltage)).count() + "");
        }
        if (Objects.equals(dictData.getCode(), DicDataEnum.VOLTAGE_RISE.getCode())) {
            value = Integer.valueOf(list.stream().filter(temp -> temp.getSwellTimes() > 0 && Objects.equals(temp.getVoltage(), voltage)).count() + "");
        }
        if (Objects.equals(dictData.getCode(), DicDataEnum.SHORT_INTERRUPTIONS.getCode())) {
            value = Integer.valueOf(list.stream().filter(temp -> temp.getInterruptTimes() > 0 && Objects.equals(temp.getVoltage(), voltage)).count() + "");
        }

        return value;

    }

}
