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

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.njcn.device.biz.pojo.dto.DeptGetChildrenMoreDTO;
import com.njcn.device.biz.pojo.dto.LineDevGetDTO;
import com.njcn.harmonic.pojo.po.RMpEventDetailDPO;
import com.njcn.harmonic.pojo.po.RMpEventDetailMPO;
import com.njcn.harmonic.pojo.po.RStatOrgDPO;
import com.njcn.harmonic.pojo.po.RStatOrgMPO;
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.RMpEventDetailDMapper;
import com.njcn.prepare.harmonic.mapper.mysql.line.RMpEventDetailMMapper;
import com.njcn.prepare.harmonic.mapper.mysql.newalgorithm.*;
import com.njcn.prepare.harmonic.pojo.po.*;
import com.njcn.prepare.harmonic.service.mysql.newalgorithm.*;
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.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 暂态指标统计
 * </p>
 *
 * @author zbj
 * @since 2023-06-06
 */
@Service
@RequiredArgsConstructor
public class RStatEventOrgServiceImpl implements RStatEventOrgService {

    private final DicDataFeignClient dicDataFeignClient;

    private final RStatEventDPOMapper rStatEventDPOMapper;

    private final RStatOrgDMapper rStatOrgDMapper;

    private final RStatOrgMMapper rStatOrgMMapper;

    private final RStatOrgQMapper rStatOrgQMapper;

    private final RStatOrgYMapper rStatOrgYMapper;

    private final RStatEventOrgDPOMapper rStatEventOrgDPOMapper;

    private final RStatEventOrgMPOMapper rStatEventOrgMPOMapper;

    private final RStatEventMPOMapper rStatEventMPOMapper;

    private final RStatEventQPOMapper rStatEventQPOMapper;

    private final RStatEventYPOMapper rStatEventYPOMapper;

    private final RStatEventOrgDPOService rStatEventOrgDPOService;

    private final RStatEventOrgMPOService rStatEventOrgMPOService;

    private final RStatEventOrgQPOService rStatEventOrgQPOService;

    private final RStatEventOrgYPOService rStatEventOrgYPOService;

    @Override
    public void handler(List<DeptGetChildrenMoreDTO> data, String dataDate, Integer type) {
        switch (type) {
            case 1:
                this.rStartEventOrgYHandler(data, dataDate, type);
                break;
            case 2:
                this.rStartEventOrgQHandler(data, dataDate, type);
                break;
            case 3:
                this.rStartEventOrgMHandler(data, dataDate, type);
                break;
            case 5:
                this.rStartEventOrgDHandler(data, dataDate, type);
                break;
            default:
                break;
        }
    }

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

        /*获取数据类型*/
        List<DictData> dataTypeList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.DATA_TYPE.getCode()).getData();
        Map<String, DictData> dataTypeMap = dataTypeList.stream().collect(Collectors.toMap(DictData::getCode, dictData -> dictData));

        data.forEach(deptGetChildrenMoreDTO -> {

            List<RStatEventDVO> rStatEventDVO = rStatEventDPOMapper.listAll(dataDate, deptGetChildrenMoreDTO.getUnitId());

            List<RStatOrgDVO> rStatOrgDVOS = rStatOrgDMapper.listAll(dataDate, deptGetChildrenMoreDTO.getUnitId());

            //集合判空
            if (!CollectionUtils.isEmpty(rStatEventDVO)) {
                //list转为map
                Map<String, Integer> emMap = new HashMap<>();
                if (!CollectionUtils.isEmpty(rStatOrgDVOS)) {
                    if (rStatOrgDVOS.size() == 1) {
                        if (Objects.equals(rStatOrgDVOS.get(0).getDataType(), dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId())) {
                            emMap.put(rStatOrgDVOS.get(0).getDataType(), rStatOrgDVOS.get(0).getEffectiveMeasurement());
                            emMap.put(dataTypeMap.get(DicDataEnum.DISTRIBUTION_POINT.getCode()).getId(), 0);
                        } else {
                            emMap.put(rStatOrgDVOS.get(0).getDataType(), rStatOrgDVOS.get(0).getEffectiveMeasurement());
                            emMap.put(dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId(), 0);
                        }
                    } else {
                        emMap.put(rStatOrgDVOS.get(0).getDataType(), rStatOrgDVOS.get(0).getEffectiveMeasurement());
                        emMap.put(rStatOrgDVOS.get(1).getDataType(), rStatOrgDVOS.get(1).getEffectiveMeasurement());
                    }
                } else {
                    emMap.put(dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId(), 0);
                    emMap.put(dataTypeMap.get(DicDataEnum.DISTRIBUTION_POINT.getCode()).getId(), 0);
                }

                //循环
                rStatEventDVO.forEach(o -> {
                    RStatEventOrgDPO rStatEventOrgDPO = new RStatEventOrgDPO();
                    rStatEventOrgDPO.setOrgNo(o.getOrgNo());
                    rStatEventOrgDPO.setDataDate(localDate);
                    rStatEventOrgDPO.setEventType(o.getEventType());
                    rStatEventOrgDPO.setEventMeasurement(o.getEmSum());
                    rStatEventOrgDPO.setEventCount(o.getEcSum());
                    rStatEventOrgDPO.setDataType(o.getDataType());

                    if (Objects.equals(o.getDataType(), dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId())) {
                        if (emMap.get(o.getDataType()) == 0) {
                            rStatEventOrgDPO.setEventMeasurementRatio(1.00f);
                        } else {
                            String value = df.format(o.getEmSum().floatValue() / emMap.get(o.getDataType()));
                            rStatEventOrgDPO.setEventMeasurementRatio(Float.parseFloat(value));
                        }
                    } else {
                        if (emMap.get(o.getDataType()) == 0) {
                            rStatEventOrgDPO.setEventMeasurementRatio(1.00f);
                        } else {
                            String value = df.format(o.getEmSum().floatValue() / emMap.get(o.getDataType()));
                            rStatEventOrgDPO.setEventMeasurementRatio(Float.parseFloat(value));
                        }
                    }
                    rStatEventOrgDPOS.add(rStatEventOrgDPO);
                });
            }
        });
        if (!CollectionUtils.isEmpty(rStatEventOrgDPOS)) {
            rStatEventOrgDPOService.saveOrUpdateBatchByMultiId(rStatEventOrgDPOS, 500);
        }
    }

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

        //传入字符串日期获取月的起始日期与结束日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        calendar.set(Calendar.DAY_OF_MONTH, 1); // 将日期设置为当月的第一天
        String startTime = sdf.format(calendar.getTime()); // 获取当月的起始日期

        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); // 将日期设置为当月的最后一天
        String endTime = sdf.format(calendar.getTime());// 获取当月的结束日期

        /*获取暂态统计指标*/
        List<DictData> eventStatis = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.EVENT_STATIS.getCode()).getData();
        Map<String, DictData> eventStatisMap = eventStatis.stream().collect(Collectors.toMap(DictData::getCode, dictData -> dictData));

        /*获取数据类型*/
        List<DictData> dataTypeList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.DATA_TYPE.getCode()).getData();
        Map<String, DictData> dataTypeMap = dataTypeList.stream().collect(Collectors.toMap(DictData::getCode, dictData -> dictData));

        data.forEach(deptGetChildrenMoreDTO -> {

            //主网天表
            List<RStatEventOrgDVO> zwList = rStatEventOrgDPOMapper.selectMlist(startTime, endTime,
                    deptGetChildrenMoreDTO.getUnitId(), dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId());

            //配网天表
            List<RStatEventOrgDVO> pwList = rStatEventOrgDPOMapper.selectMlist(startTime, endTime,
                    deptGetChildrenMoreDTO.getUnitId(), dataTypeMap.get(DicDataEnum.DISTRIBUTION_POINT.getCode()).getId());

            //主网r_stat_event_m
            List<Map<String, Object>> zwEmaSum = rStatEventMPOMapper.selectMlist(startTime, endTime,
                    deptGetChildrenMoreDTO.getUnitId(), dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId());

            //配网r_stat_event_m
            List<Map<String, Object>> pwEmaSum = rStatEventMPOMapper.selectMlist(startTime, endTime,
                    deptGetChildrenMoreDTO.getUnitId(), dataTypeMap.get(DicDataEnum.DISTRIBUTION_POINT.getCode()).getId());

            //主网r_stat_org_m
            Map<String, Object> zwEfSum = rStatOrgMMapper.selectMlist(startTime, endTime,
                    deptGetChildrenMoreDTO.getUnitId(), dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId());

            //配网r_stat_org_m
            Map<String, Object> pwEfSum = rStatOrgMMapper.selectMlist(startTime, endTime,
                    deptGetChildrenMoreDTO.getUnitId(), dataTypeMap.get(DicDataEnum.DISTRIBUTION_POINT.getCode()).getId());


            //主网
            if (!CollectionUtils.isEmpty(zwList)) {
                //转map
                Map<String, Integer> emaMap = new HashMap<>();
                if (!CollectionUtils.isEmpty(zwEmaSum)) {
                    for (Map<String, Object> map : zwEmaSum) {
                        emaMap.put(map.get("eventType").toString(), Integer.valueOf(map.get("emaSum").toString()));
                    }
                } else {
                    emaMap.put(eventStatisMap.get(DicDataEnum.VOLTAGE_DIP.getCode()).getId(), 0);
                    emaMap.put(eventStatisMap.get(DicDataEnum.VOLTAGE_RISE.getCode()).getId(), 0);
                    emaMap.put(eventStatisMap.get(DicDataEnum.SHORT_INTERRUPTIONS.getCode()).getId(), 0);
                }

                //循环
                zwList.forEach(obj -> {
                    RStatEventOrgMPO rStatEventOrgMPO = new RStatEventOrgMPO();
                    rStatEventOrgMPO.setOrgNo(obj.getOrgNo());
                    rStatEventOrgMPO.setDataDate(localDate);
                    rStatEventOrgMPO.setEventType(obj.getEventType());
                    rStatEventOrgMPO.setEventMeasurementAverage(obj.getEmrAvg());
                    rStatEventOrgMPO.setEventCount(obj.getEcSum());
                    rStatEventOrgMPO.setEventMeasurementRatioAverage(obj.getEmrAvg());
                    rStatEventOrgMPO.setDataType(dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId());

                    if (emaMap.get(obj.getEventType()) == 0) {
                        rStatEventOrgMPO.setEventMeasurementAccrued(0);
                    } else {
                        rStatEventOrgMPO.setEventMeasurementAccrued(emaMap.get(obj.getEventType()));
                    }
                    if (obj.getEmSum() == 0) {
                        rStatEventOrgMPO.setEventFreq(0.00f);
                    } else {
                        String value = df.format(obj.getEcSum().floatValue() / obj.getEmSum());
                        rStatEventOrgMPO.setEventFreq(Float.parseFloat(value));
                    }
                    if (!CollectionUtils.isEmpty(zwEfSum)) {
                        if (Integer.parseInt(zwEfSum.get("efSum").toString()) == 0) {
                            rStatEventOrgMPO.setEventMeasurementRatioAccrued(0.00f);
                        } else {
                            String value = df.format(rStatEventOrgMPO.getEventMeasurementAccrued() / Float.parseFloat(zwEfSum.get("efSum").toString()));
                            rStatEventOrgMPO.setEventMeasurementRatioAccrued(Float.parseFloat(value));
                        }
                    } else {
                        rStatEventOrgMPO.setEventMeasurementRatioAccrued(0.00f);
                    }
                    rStatEventOrgMPOS.add(rStatEventOrgMPO);
                });
            }

            //配网
            if (!CollectionUtils.isEmpty(pwList)) {
                //转map
                Map<String, Integer> emaMap = new HashMap<>();
                if (!CollectionUtils.isEmpty(pwEmaSum)) {
                    for (Map<String, Object> map : pwEmaSum) {
                        emaMap.put(map.get("eventType").toString(), Integer.valueOf(map.get("emaSum").toString()));
                    }
                } else {
                    emaMap.put(eventStatisMap.get(DicDataEnum.VOLTAGE_DIP.getCode()).getId(), 0);
                    emaMap.put(eventStatisMap.get(DicDataEnum.VOLTAGE_RISE.getCode()).getId(), 0);
                    emaMap.put(eventStatisMap.get(DicDataEnum.SHORT_INTERRUPTIONS.getCode()).getId(), 0);
                }

                //循环
                pwList.forEach(obj -> {
                    RStatEventOrgMPO rStatEventOrgMPO = new RStatEventOrgMPO();
                    rStatEventOrgMPO.setOrgNo(obj.getOrgNo());
                    rStatEventOrgMPO.setDataDate(localDate);
                    rStatEventOrgMPO.setEventType(obj.getEventType());
                    rStatEventOrgMPO.setEventMeasurementAverage(obj.getEmrAvg());
                    rStatEventOrgMPO.setEventCount(obj.getEcSum());
                    rStatEventOrgMPO.setEventMeasurementRatioAverage(obj.getEmrAvg());
                    rStatEventOrgMPO.setDataType(dataTypeMap.get(DicDataEnum.DISTRIBUTION_POINT.getCode()).getId());

                    if (emaMap.get(obj.getEventType()) == 0) {
                        rStatEventOrgMPO.setEventMeasurementAccrued(0);
                    } else {
                        rStatEventOrgMPO.setEventMeasurementAccrued(emaMap.get(obj.getEventType()));
                    }
                    if (obj.getEmSum() == 0) {
                        rStatEventOrgMPO.setEventFreq(0.00f);
                    } else {
                        String value = df.format(obj.getEcSum() / obj.getEmSum().floatValue());
                        rStatEventOrgMPO.setEventFreq(Float.parseFloat(value));
                    }
                    if (!CollectionUtils.isEmpty(pwEfSum)) {
                        if (Integer.parseInt(pwEfSum.get("efSum").toString()) == 0) {
                            rStatEventOrgMPO.setEventMeasurementRatioAccrued(0.00f);
                        } else {
                            String value = df.format(rStatEventOrgMPO.getEventMeasurementAccrued() / Float.parseFloat(pwEfSum.get("efSum").toString()));
                            rStatEventOrgMPO.setEventMeasurementRatioAccrued(Float.parseFloat(value));
                        }
                    } else {
                        rStatEventOrgMPO.setEventMeasurementRatioAccrued(0.00f);
                    }
                    rStatEventOrgMPOS.add(rStatEventOrgMPO);
                });
            }

        });
        if (!CollectionUtils.isEmpty(rStatEventOrgMPOS)) {
            rStatEventOrgMPOService.saveOrUpdateBatchByMultiId(rStatEventOrgMPOS, 500);
        }
    }

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

        //计算季度的开始日期和结束日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int quarter = (calendar.get(Calendar.MONTH) / 3) + 1; // 计算日期所在的季度
        calendar.set(Calendar.MONTH, (quarter - 1) * 3); // 设置季度开始的月份
        calendar.set(Calendar.DAY_OF_MONTH, 1); // 设置季度开始的日期
        String startTime = sdf.format(calendar.getTime()); // 季度开始日期
        calendar.add(Calendar.MONTH, 2); // 添加两个月，使其到季度结束的月份
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); // 设置季度结束的日期 - 月份最后一天
        String endTime = sdf.format(calendar.getTime()); // 季度结束日期

        /*获取暂态统计指标*/
        List<DictData> eventStatis = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.EVENT_STATIS.getCode()).getData();
        Map<String, DictData> eventStatisMap = eventStatis.stream().collect(Collectors.toMap(DictData::getCode, dictData -> dictData));

        /*获取数据类型*/
        List<DictData> dataTypeList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.DATA_TYPE.getCode()).getData();
        Map<String, DictData> dataTypeMap = dataTypeList.stream().collect(Collectors.toMap(DictData::getCode, dictData -> dictData));

        data.forEach(deptGetChildrenMoreDTO -> {

            //主网月表
            List<RStatEventOrgDVO> zwMList = rStatEventOrgMPOMapper.selectQlist(startTime, endTime,
                    deptGetChildrenMoreDTO.getUnitId(), dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId());

            //配网月表
            List<RStatEventOrgDVO> pwMList = rStatEventOrgMPOMapper.selectQlist(startTime, endTime,
                    deptGetChildrenMoreDTO.getUnitId(), dataTypeMap.get(DicDataEnum.DISTRIBUTION_POINT.getCode()).getId());

            //主网天表
            List<RStatEventOrgDVO> zwDList = rStatEventOrgDPOMapper.selectQlist(startTime, endTime,
                    deptGetChildrenMoreDTO.getUnitId(), dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId());

            //配网天表
            List<RStatEventOrgDVO> pwDList = rStatEventOrgDPOMapper.selectQlist(startTime, endTime,
                    deptGetChildrenMoreDTO.getUnitId(), dataTypeMap.get(DicDataEnum.DISTRIBUTION_POINT.getCode()).getId());

            //主网r_stat_event_q
            List<Map<String, Object>> zwEmaSum = rStatEventQPOMapper.selectQlist(startTime, endTime,
                    deptGetChildrenMoreDTO.getUnitId(), dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId());

            //配网r_stat_event_q
            List<Map<String, Object>> pwEmaSum = rStatEventQPOMapper.selectQlist(startTime, endTime,
                    deptGetChildrenMoreDTO.getUnitId(), dataTypeMap.get(DicDataEnum.DISTRIBUTION_POINT.getCode()).getId());

            //主网r_stat_org_q
            Map<String, Object> zwEfSum = rStatOrgQMapper.selectQlist(startTime, endTime,
                    deptGetChildrenMoreDTO.getUnitId(), dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId());

            //配网r_stat_org_q
            Map<String, Object> pwEfSum = rStatOrgQMapper.selectQlist(startTime, endTime,
                    deptGetChildrenMoreDTO.getUnitId(), dataTypeMap.get(DicDataEnum.DISTRIBUTION_POINT.getCode()).getId());

            //主网
            if (!CollectionUtils.isEmpty(zwMList) && !CollectionUtils.isEmpty(zwDList)) {

                for (RStatEventOrgDVO rStatEventOrgDVO : zwMList) {
                    for (RStatEventOrgDVO statEventOrgDVO : zwDList) {
                        if (Objects.equals(rStatEventOrgDVO.getEventType(), statEventOrgDVO.getEventType())) {
                            rStatEventOrgDVO.setEcSum(statEventOrgDVO.getEcSum());
                            rStatEventOrgDVO.setEmSum(statEventOrgDVO.getEmSum());
                        }
                    }
                }

                //转map
                Map<String, Integer> emaMap = new HashMap<>();
                if (!CollectionUtils.isEmpty(zwEmaSum)) {
                    for (Map<String, Object> map : zwEmaSum) {
                        emaMap.put(map.get("eventType").toString(), Integer.valueOf(map.get("emaSum").toString()));
                    }
                } else {
                    emaMap.put(eventStatisMap.get(DicDataEnum.VOLTAGE_DIP.getCode()).getId(), 0);
                    emaMap.put(eventStatisMap.get(DicDataEnum.VOLTAGE_RISE.getCode()).getId(), 0);
                    emaMap.put(eventStatisMap.get(DicDataEnum.SHORT_INTERRUPTIONS.getCode()).getId(), 0);
                }

                //循环
                zwMList.forEach(obj -> {
                    RStatEventOrgQPO rStatEventOrgQPO = new RStatEventOrgQPO();
                    rStatEventOrgQPO.setOrgNo(obj.getOrgNo());
                    rStatEventOrgQPO.setDataDate(localDate);
                    rStatEventOrgQPO.setEventType(obj.getEventType());
                    rStatEventOrgQPO.setEventMeasurementAverage(obj.getEmrAvg());
                    rStatEventOrgQPO.setEventCount(obj.getEcMSum());
                    rStatEventOrgQPO.setEventMeasurementRatioAverage(obj.getEmrAvg());
                    rStatEventOrgQPO.setDataType(dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId());

                    if (emaMap.get(obj.getEventType()) == 0) {
                        rStatEventOrgQPO.setEventMeasurementAccrued(0);
                    } else {
                        rStatEventOrgQPO.setEventMeasurementAccrued(emaMap.get(obj.getEventType()));
                    }
                    if (obj.getEmSum() == 0) {
                        rStatEventOrgQPO.setEventFreq(0.00f);
                    } else {
                        String value = df.format(obj.getEcSum().floatValue() / obj.getEmSum());
                        rStatEventOrgQPO.setEventFreq(Float.parseFloat(value));
                    }
                    if (!CollectionUtils.isEmpty(zwEfSum)) {
                        if (Integer.parseInt(zwEfSum.get("efSum").toString()) == 0) {
                            rStatEventOrgQPO.setEventMeasurementRatioAccrued(0.00f);
                        } else {
                            String value = df.format(rStatEventOrgQPO.getEventMeasurementAccrued() / Float.parseFloat(zwEfSum.get("efSum").toString()));
                            rStatEventOrgQPO.setEventMeasurementRatioAccrued(Float.parseFloat(value));
                        }
                    } else {
                        rStatEventOrgQPO.setEventMeasurementRatioAccrued(0.00f);
                    }
                    rStatEventOrgQPOS.add(rStatEventOrgQPO);
                });
            }

            //配网
            if (!CollectionUtils.isEmpty(pwMList) && !CollectionUtils.isEmpty(pwDList)) {

                for (RStatEventOrgDVO rStatEventOrgDVO : pwMList) {
                    for (RStatEventOrgDVO statEventOrgDVO : pwDList) {
                        if (Objects.equals(rStatEventOrgDVO.getEventType(), statEventOrgDVO.getEventType())) {
                            rStatEventOrgDVO.setEcSum(statEventOrgDVO.getEcSum());
                            rStatEventOrgDVO.setEmSum(statEventOrgDVO.getEmSum());
                        }
                    }
                }

                //转map
                Map<String, Integer> emaMap = new HashMap<>();
                if (!CollectionUtils.isEmpty(pwEmaSum)) {
                    for (Map<String, Object> map : pwEmaSum) {
                        emaMap.put(map.get("eventType").toString(), Integer.valueOf(map.get("emaSum").toString()));
                    }
                } else {
                    emaMap.put(eventStatisMap.get(DicDataEnum.VOLTAGE_DIP.getCode()).getId(), 0);
                    emaMap.put(eventStatisMap.get(DicDataEnum.VOLTAGE_RISE.getCode()).getId(), 0);
                    emaMap.put(eventStatisMap.get(DicDataEnum.SHORT_INTERRUPTIONS.getCode()).getId(), 0);
                }

                //循环
                pwMList.forEach(obj -> {
                    RStatEventOrgQPO rStatEventOrgQPO = new RStatEventOrgQPO();
                    rStatEventOrgQPO.setOrgNo(obj.getOrgNo());
                    rStatEventOrgQPO.setDataDate(localDate);
                    rStatEventOrgQPO.setEventType(obj.getEventType());
                    rStatEventOrgQPO.setEventMeasurementAverage(obj.getEmrAvg());
                    rStatEventOrgQPO.setEventCount(obj.getEcMSum());
                    rStatEventOrgQPO.setEventMeasurementRatioAverage(obj.getEmrAvg());
                    rStatEventOrgQPO.setDataType(dataTypeMap.get(DicDataEnum.DISTRIBUTION_POINT.getCode()).getId());

                    if (emaMap.get(obj.getEventType()) == 0) {
                        rStatEventOrgQPO.setEventMeasurementAccrued(0);
                    } else {
                        rStatEventOrgQPO.setEventMeasurementAccrued(emaMap.get(obj.getEventType()));
                    }
                    if (obj.getEmSum() == 0) {
                        rStatEventOrgQPO.setEventFreq(0.00f);
                    } else {
                        String value = df.format(obj.getEcSum() / obj.getEmSum().floatValue());
                        rStatEventOrgQPO.setEventFreq(Float.parseFloat(value));
                    }
                    if (!CollectionUtils.isEmpty(pwEfSum)) {
                        if (Integer.parseInt(pwEfSum.get("efSum").toString()) == 0) {
                            rStatEventOrgQPO.setEventMeasurementRatioAccrued(0.00f);
                        } else {
                            String value = df.format(rStatEventOrgQPO.getEventMeasurementAccrued() / Float.parseFloat(pwEfSum.get("efSum").toString()));
                            rStatEventOrgQPO.setEventMeasurementRatioAccrued(Float.parseFloat(value));
                        }
                    } else {
                        rStatEventOrgQPO.setEventMeasurementRatioAccrued(0.00f);
                    }
                    rStatEventOrgQPOS.add(rStatEventOrgQPO);
                });
            }

        });
        if (!CollectionUtils.isEmpty(rStatEventOrgQPOS)) {
            rStatEventOrgQPOService.saveOrUpdateBatchByMultiId(rStatEventOrgQPOS, 500);
        }
    }

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

        //计算年度的开始日期和结束日期
        Calendar calendar = Calendar.getInstance();  // 获取Calendar实例
        calendar.setTime(date);  // 设置Calendar对象的时间为传入的时间
        int year = calendar.get(Calendar.YEAR);  // 获取本年度年份
        // 获取本年度的第一天
        Calendar firstDayOfYear = Calendar.getInstance();
        firstDayOfYear.set(Calendar.YEAR, year);
        firstDayOfYear.set(Calendar.MONTH, Calendar.JANUARY);
        firstDayOfYear.set(Calendar.DAY_OF_MONTH, 1);
        // 获取本年度的最后一天
        Calendar lastDayOfYear = Calendar.getInstance();
        lastDayOfYear.set(Calendar.YEAR, year);
        lastDayOfYear.set(Calendar.MONTH, Calendar.DECEMBER);
        lastDayOfYear.set(Calendar.DAY_OF_MONTH, 31);

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String startTime = formatter.format(firstDayOfYear.getTime());
        String endTime = formatter.format(lastDayOfYear.getTime());

        /*获取暂态统计指标*/
        List<DictData> eventStatis = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.EVENT_STATIS.getCode()).getData();
        Map<String, DictData> eventStatisMap = eventStatis.stream().collect(Collectors.toMap(DictData::getCode, dictData -> dictData));

        /*获取数据类型*/
        List<DictData> dataTypeList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.DATA_TYPE.getCode()).getData();
        Map<String, DictData> dataTypeMap = dataTypeList.stream().collect(Collectors.toMap(DictData::getCode, dictData -> dictData));

        data.forEach(deptGetChildrenMoreDTO -> {

            //主网月表
            List<RStatEventOrgDVO> zwMList = rStatEventOrgMPOMapper.selectQlist(startTime, endTime,
                    deptGetChildrenMoreDTO.getUnitId(), dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId());

            //配网月表
            List<RStatEventOrgDVO> pwMList = rStatEventOrgMPOMapper.selectQlist(startTime, endTime,
                    deptGetChildrenMoreDTO.getUnitId(), dataTypeMap.get(DicDataEnum.DISTRIBUTION_POINT.getCode()).getId());

            //主网天表
            List<RStatEventOrgDVO> zwDList = rStatEventOrgDPOMapper.selectQlist(startTime, endTime,
                    deptGetChildrenMoreDTO.getUnitId(), dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId());

            //配网天表
            List<RStatEventOrgDVO> pwDList = rStatEventOrgDPOMapper.selectQlist(startTime, endTime,
                    deptGetChildrenMoreDTO.getUnitId(), dataTypeMap.get(DicDataEnum.DISTRIBUTION_POINT.getCode()).getId());

            //主网r_stat_event_y
            List<Map<String, Object>> zwEmaSum = rStatEventYPOMapper.selectYlist(startTime, endTime,
                    deptGetChildrenMoreDTO.getUnitId(), dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId());

            //配网r_stat_event_y
            List<Map<String, Object>> pwEmaSum = rStatEventYPOMapper.selectYlist(startTime, endTime,
                    deptGetChildrenMoreDTO.getUnitId(), dataTypeMap.get(DicDataEnum.DISTRIBUTION_POINT.getCode()).getId());

            //主网r_stat_org_y
            Map<String, Object> zwEfSum = rStatOrgYMapper.selectYlist(startTime, endTime,
                    deptGetChildrenMoreDTO.getUnitId(), dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId());

            //配网r_stat_org_y
            Map<String, Object> pwEfSum = rStatOrgYMapper.selectYlist(startTime, endTime,
                    deptGetChildrenMoreDTO.getUnitId(), dataTypeMap.get(DicDataEnum.DISTRIBUTION_POINT.getCode()).getId());

            //主网
            if (!CollectionUtils.isEmpty(zwMList) && !CollectionUtils.isEmpty(zwDList)) {

                for (RStatEventOrgDVO rStatEventOrgDVO : zwMList) {
                    for (RStatEventOrgDVO statEventOrgDVO : zwDList) {
                        if (Objects.equals(rStatEventOrgDVO.getEventType(), statEventOrgDVO.getEventType())) {
                            rStatEventOrgDVO.setEcSum(statEventOrgDVO.getEcSum());
                            rStatEventOrgDVO.setEmSum(statEventOrgDVO.getEmSum());
                        }
                    }
                }

                //转map
                Map<String, Integer> emaMap = new HashMap<>();
                if (!CollectionUtils.isEmpty(zwEmaSum)) {
                    for (Map<String, Object> map : zwEmaSum) {
                        emaMap.put(map.get("eventType").toString(), Integer.valueOf(map.get("emaSum").toString()));
                    }
                } else {
                    emaMap.put(eventStatisMap.get(DicDataEnum.VOLTAGE_DIP.getCode()).getId(), 0);
                    emaMap.put(eventStatisMap.get(DicDataEnum.VOLTAGE_RISE.getCode()).getId(), 0);
                    emaMap.put(eventStatisMap.get(DicDataEnum.SHORT_INTERRUPTIONS.getCode()).getId(), 0);
                }

                //循环
                zwMList.forEach(obj -> {
                    RStatEventOrgYPO rStatEventOrgYPO = new RStatEventOrgYPO();
                    rStatEventOrgYPO.setOrgNo(obj.getOrgNo());
                    rStatEventOrgYPO.setDataDate(localDate);
                    rStatEventOrgYPO.setEventType(obj.getEventType());
                    rStatEventOrgYPO.setEventMeasurementAverage(obj.getEmrAvg());
                    rStatEventOrgYPO.setEventCount(obj.getEcMSum());
                    rStatEventOrgYPO.setEventMeasurementRatioAverage(obj.getEmrAvg());
                    rStatEventOrgYPO.setDataType(dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId());

                    if (emaMap.get(obj.getEventType()) == 0) {
                        rStatEventOrgYPO.setEventMeasurementAccrued(0);
                    } else {
                        rStatEventOrgYPO.setEventMeasurementAccrued(emaMap.get(obj.getEventType()));
                    }
                    if (obj.getEmSum() == 0) {
                        rStatEventOrgYPO.setEventFreq(0.00f);
                    } else {
                        String value = df.format(obj.getEcSum().floatValue() / obj.getEmSum());
                        rStatEventOrgYPO.setEventFreq(Float.parseFloat(value));
                    }
                    if (!CollectionUtils.isEmpty(zwEfSum)) {
                        if (Integer.parseInt(zwEfSum.get("efSum").toString()) == 0) {
                            rStatEventOrgYPO.setEventMeasurementRatioAccrued(0.00f);
                        } else {
                            String value = df.format(rStatEventOrgYPO.getEventMeasurementAccrued() / Float.parseFloat(zwEfSum.get("efSum").toString()));
                            rStatEventOrgYPO.setEventMeasurementRatioAccrued(Float.parseFloat(value));
                        }
                    } else {
                        rStatEventOrgYPO.setEventMeasurementRatioAccrued(0.00f);
                    }
                    rStatEventOrgYPOS.add(rStatEventOrgYPO);
                });
            }

            //配网
            if (!CollectionUtils.isEmpty(pwMList) && !CollectionUtils.isEmpty(pwDList)) {

                for (RStatEventOrgDVO rStatEventOrgDVO : pwMList) {
                    for (RStatEventOrgDVO statEventOrgDVO : pwDList) {
                        if (Objects.equals(rStatEventOrgDVO.getEventType(), statEventOrgDVO.getEventType())) {
                            rStatEventOrgDVO.setEcSum(statEventOrgDVO.getEcSum());
                            rStatEventOrgDVO.setEmSum(statEventOrgDVO.getEmSum());
                        }
                    }
                }

                //转map
                Map<String, Integer> emaMap = new HashMap<>();
                if (!CollectionUtils.isEmpty(pwEmaSum)) {
                    for (Map<String, Object> map : pwEmaSum) {
                        emaMap.put(map.get("eventType").toString(), Integer.valueOf(map.get("emaSum").toString()));
                    }
                } else {
                    emaMap.put(eventStatisMap.get(DicDataEnum.VOLTAGE_DIP.getCode()).getId(), 0);
                    emaMap.put(eventStatisMap.get(DicDataEnum.VOLTAGE_RISE.getCode()).getId(), 0);
                    emaMap.put(eventStatisMap.get(DicDataEnum.SHORT_INTERRUPTIONS.getCode()).getId(), 0);
                }

                //循环
                pwMList.forEach(obj -> {
                    RStatEventOrgYPO rStatEventOrgYPO = new RStatEventOrgYPO();
                    rStatEventOrgYPO.setOrgNo(obj.getOrgNo());
                    rStatEventOrgYPO.setDataDate(localDate);
                    rStatEventOrgYPO.setEventType(obj.getEventType());
                    rStatEventOrgYPO.setEventMeasurementAverage(obj.getEmrAvg());
                    rStatEventOrgYPO.setEventCount(obj.getEcMSum());
                    rStatEventOrgYPO.setEventMeasurementRatioAverage(obj.getEmrAvg());
                    rStatEventOrgYPO.setDataType(dataTypeMap.get(DicDataEnum.DISTRIBUTION_POINT.getCode()).getId());

                    if (emaMap.get(obj.getEventType()) == 0) {
                        rStatEventOrgYPO.setEventMeasurementAccrued(0);
                    } else {
                        rStatEventOrgYPO.setEventMeasurementAccrued(emaMap.get(obj.getEventType()));
                    }
                    if (obj.getEmSum() == 0) {
                        rStatEventOrgYPO.setEventFreq(0.00f);
                    } else {
                        String value = df.format(obj.getEcSum() / obj.getEmSum().floatValue());
                        rStatEventOrgYPO.setEventFreq(Float.parseFloat(value));
                    }
                    if (!CollectionUtils.isEmpty(pwEfSum)) {
                        if (Integer.parseInt(pwEfSum.get("efSum").toString()) == 0) {
                            rStatEventOrgYPO.setEventMeasurementRatioAccrued(0.00f);
                        } else {
                            String value = df.format(rStatEventOrgYPO.getEventMeasurementAccrued() / Float.parseFloat(pwEfSum.get("efSum").toString()));
                            rStatEventOrgYPO.setEventMeasurementRatioAccrued(Float.parseFloat(value));
                        }
                    } else {
                        rStatEventOrgYPO.setEventMeasurementRatioAccrued(0.00f);
                    }
                    rStatEventOrgYPOS.add(rStatEventOrgYPO);
                });
            }

        });
        if (!CollectionUtils.isEmpty(rStatEventOrgYPOS)) {
            rStatEventOrgYPOService.saveOrUpdateBatchByMultiId(rStatEventOrgYPOS, 500);
        }
    }

}
