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

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.njcn.device.biz.commApi.CommTerminalGeneralClient;
import com.njcn.device.biz.pojo.dto.LineDevGetBandDTO;
import com.njcn.device.biz.pojo.dto.LineDevGetDTO;
import com.njcn.prepare.harmonic.mapper.mysql.area.RMpTargetWarnDMapper;
import com.njcn.prepare.harmonic.mapper.mysql.line.*;
import com.njcn.prepare.harmonic.mapper.mysql.newalgorithm.RStatSubstationDPOMapper;
import com.njcn.prepare.harmonic.mapper.mysql.newalgorithm.RStatSubstationMPOMapper;
import com.njcn.prepare.harmonic.pojo.bo.CalculatedParam;
import com.njcn.prepare.harmonic.pojo.po.RStatSubstationDPO;
import com.njcn.prepare.harmonic.pojo.po.RStatSubstationMPO;
import com.njcn.prepare.harmonic.pojo.po.RStatSubstationQPO;
import com.njcn.prepare.harmonic.pojo.po.RStatSubstationYPO;
import com.njcn.prepare.harmonic.service.mysql.newalgorithm.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.ListUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 变电站指标统计
 * </p>
 *
 * @author zbj
 * @since 2023-06-19
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class RStatSubstationServiceImpl implements RStatSubstationService {

    private final CommTerminalGeneralClient commTerminalGeneralClient;

    private final RMpTargetWarnDMapper rMpTargetWarnDMapper;

    private final RMpPartHarmonicDetailDMapper rMpPartHarmonicDetailDMapper;

    private final RMpPartHarmonicDetailMMapper rMpPartHarmonicDetailMMapper;

    private final RMpSurplusHarmonicDetailDMapper mpSurplusHarmonicDetailDMapper;

    private final RMpSurplusHarmonicDetailMMapper mpSurplusHarmonicDetailMMapper;

    private final RMpEventDetailDMapper rMpEventDetailDMapper;

    private final RStatSubstationDPOService rStatSubstationDPOService;

    private final RStatSubstationMPOService rStatSubstationMPOService;

    private final RStatSubstationQPOService rStatSubstationQPOService;

    private final RStatSubstationYPOService rStatSubstationYPOService;

    private final RStatSubstationDPOMapper rStatSubstationDPOMapper;

    private final RStatSubstationMPOMapper rStatSubstationMPOMapper;


    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void handlerD(CalculatedParam calculatedParam) {
        log.info(LocalDateTime.now()+"===>变电站指标统计日表算法开始");
        List<RStatSubstationDPO> rStatSubstationDPOS = new ArrayList<>();
        String beginDay = LocalDateTimeUtil.format(LocalDateTimeUtil.beginOfDay(LocalDateTimeUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN)),DatePattern.NORM_DATETIME_PATTERN);
        String endDay = LocalDateTimeUtil.format(LocalDateTimeUtil.endOfDay(LocalDateTimeUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN)),DatePattern.NORM_DATETIME_PATTERN);
        //变电站id
        List<String> subIds = calculatedParam.getIdList();
        //以尺寸100分片,查询数据
        List<List<String>> pendingIds =  ListUtils.partition(subIds,100);
        for (List<String> pendingId : pendingIds) {
            pendingId.forEach(z -> {
                //获取监测点对象集合
                LineDevGetBandDTO lineDevGetDTOs = commTerminalGeneralClient.substationGetLine(z).getData();
                if (Objects.nonNull(lineDevGetDTOs.getZwList()) || Objects.nonNull(lineDevGetDTOs.getPwList())) {
                    //获取监测点id集合
                    List<String> lineIds = new ArrayList<>();
                    List<String> pLineIds = new ArrayList<>();
                    if (Objects.nonNull(lineDevGetDTOs.getZwList())) {
                        lineIds = lineDevGetDTOs.getZwList().stream().map(LineDevGetDTO::getPointId).collect(Collectors.toList());
                    }
                    if (Objects.nonNull(lineDevGetDTOs.getPwList())) {
                        pLineIds = lineDevGetDTOs.getPwList().stream().map(LineDevGetDTO::getPointId).collect(Collectors.toList());
                        lineIds.addAll(pLineIds);
                    }
                    //判空
                    if (!CollectionUtils.isEmpty(lineIds)) {
                        int sw = 0, sa = 0, in = 0, result = 0;
                        //r_mp_target_warn_d 表中字段求和
                        Map<String, Object> warnSum = rMpTargetWarnDMapper.getSumDate(beginDay, endDay, lineIds);
                        //r_mp_part_harmonic_detail_d 表中字段求和
                        Map<String, Object> partSum = rMpPartHarmonicDetailDMapper.getPartSum(beginDay, endDay, lineIds);
                        //r_mp_surplus_harmonic_detail_d 表中字段求和
                        Map<String, Object> surplusSum = mpSurplusHarmonicDetailDMapper.getSurplusSum(beginDay, endDay, lineIds);
                        //r_mp_event_detail_d 表中字段求和及计数
                        Map<String, Object> eventDetailSumAndCount = rMpEventDetailDMapper.getSumAndCount(beginDay, endDay, lineIds);

                        RStatSubstationDPO rStatSubstationDPO = new RStatSubstationDPO();
                        rStatSubstationDPO.setDataDate(LocalDateTimeUtil.parseDate(calculatedParam.getDataDate(),DatePattern.NORM_DATE_FORMATTER));
                        rStatSubstationDPO.setSubstationId(z);

                        if (!CollectionUtils.isEmpty(warnSum)) {
                            rStatSubstationDPO.setEffectiveMeasurementCount(warnSum.containsKey("iECount") ?
                                    Integer.parseInt(warnSum.get("iECount").toString()) : 0);
                        } else {
                            rStatSubstationDPO.setEffectiveMeasurementCount(0);
                        }

                        if (!CollectionUtils.isEmpty(partSum)) {
                            rStatSubstationDPO.setHarmonicCount(partSum.containsKey("isHarmonicCount") ?
                                    Integer.parseInt(partSum.get("isHarmonicCount").toString()) : 0);
                            rStatSubstationDPO.setFreqCount(partSum.containsKey("isFreqCount") ?
                                    Integer.parseInt(partSum.get("isFreqCount").toString()) : 0);
                            rStatSubstationDPO.setVDevCount(partSum.containsKey("isVDevCount") ?
                                    Integer.parseInt(partSum.get("isVDevCount").toString()) : 0);
                            rStatSubstationDPO.setVCount(partSum.containsKey("isVCount") ?
                                    Integer.parseInt(partSum.get("isVCount").toString()) : 0);
                            rStatSubstationDPO.setICount(partSum.containsKey("isICount") ?
                                    Integer.parseInt(partSum.get("isICount").toString()) : 0);
                            rStatSubstationDPO.setUnbalanceCount(partSum.containsKey("isUnbalanceCount") ?
                                    Integer.parseInt(partSum.get("isUnbalanceCount").toString()) : 0);
                            rStatSubstationDPO.setINegCount(partSum.containsKey("isINegCount") ?
                                    Integer.parseInt(partSum.get("isINegCount").toString()) : 0);
                        } else {
                            rStatSubstationDPO.setHarmonicCount(0);
                            rStatSubstationDPO.setFreqCount(0);
                            rStatSubstationDPO.setVDevCount(0);
                            rStatSubstationDPO.setVCount(0);
                            rStatSubstationDPO.setICount(0);
                            rStatSubstationDPO.setUnbalanceCount(0);
                            rStatSubstationDPO.setINegCount(0);
                        }

                        if (!CollectionUtils.isEmpty(surplusSum)) {
                            rStatSubstationDPO.setFlickerCount(surplusSum.containsKey("isFlickerCount") ?
                                    Integer.parseInt(surplusSum.get("isFlickerCount").toString()) : 0);
                            rStatSubstationDPO.setInuharmCount(surplusSum.containsKey("isInuharmCount") ?
                                    Integer.parseInt(surplusSum.get("isInuharmCount").toString()) : 0);
                        } else {
                            rStatSubstationDPO.setFlickerCount(0);
                            rStatSubstationDPO.setInuharmCount(0);
                        }
                        if (!CollectionUtils.isEmpty(eventDetailSumAndCount)) {
                            rStatSubstationDPO.setEventMeasurementCount(eventDetailSumAndCount.containsKey("resultCount") ?
                                    Integer.parseInt(eventDetailSumAndCount.get("resultCount").toString()) : 0);

                            if (eventDetailSumAndCount.containsKey("swellTimesSum")) {
                                sw = Integer.parseInt(eventDetailSumAndCount.get("swellTimesSum").toString());
                            }
                            if (eventDetailSumAndCount.containsKey("sagTimesSum")) {
                                sa = Integer.parseInt(eventDetailSumAndCount.get("sagTimesSum").toString());
                            }
                            if (eventDetailSumAndCount.containsKey("interruptTimesSum")) {
                                in = Integer.parseInt(eventDetailSumAndCount.get("interruptTimesSum").toString());
                            }
                            result = sw + sa + in;
                            rStatSubstationDPO.setEventCount(result);

                            rStatSubstationDPO.setSwellMeasurementCount(eventDetailSumAndCount.containsKey("swellTimesCount") ?
                                    Integer.parseInt(eventDetailSumAndCount.get("swellTimesCount").toString()) : 0);
                            rStatSubstationDPO.setSwellCount(sw);
                            rStatSubstationDPO.setSagMeasurementCount(eventDetailSumAndCount.containsKey("sagTimesCount") ?
                                    Integer.parseInt(eventDetailSumAndCount.get("sagTimesCount").toString()) : 0);
                            rStatSubstationDPO.setSagCount(sa);
                            rStatSubstationDPO.setInterruptMeasurementCount(eventDetailSumAndCount.containsKey("interruptTimesCount") ?
                                    Integer.parseInt(eventDetailSumAndCount.get("interruptTimesCount").toString()) : 0);
                            rStatSubstationDPO.setInterruptCount(in);
                        } else {
                            rStatSubstationDPO.setEventMeasurementCount(0);
                            rStatSubstationDPO.setEventCount(0);
                            rStatSubstationDPO.setSwellMeasurementCount(0);
                            rStatSubstationDPO.setSwellCount(0);
                            rStatSubstationDPO.setSagMeasurementCount(0);
                            rStatSubstationDPO.setSagCount(0);
                            rStatSubstationDPO.setInterruptMeasurementCount(0);
                            rStatSubstationDPO.setInterruptCount(0);
                        }
                        rStatSubstationDPOS.add(rStatSubstationDPO);
                    }
                }
            });
        }
        if (!CollectionUtils.isEmpty(rStatSubstationDPOS)) {
            rStatSubstationDPOService.saveOrUpdateBatchByMultiId(rStatSubstationDPOS, 100);
        }
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void handlerM(CalculatedParam calculatedParam) {
        log.info(LocalDateTime.now()+"===>变电站指标统计月表算法开始");
        List<RStatSubstationMPO> rStatSubstationMPOS = new ArrayList<>();

        String beginMonth = DateUtil.beginOfMonth(DateUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN)).toString(DatePattern.NORM_DATE_PATTERN);
        String endMonth = DateUtil.endOfMonth(DateUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN)).toString(DatePattern.NORM_DATE_PATTERN);
        //变电站id
        List<String> subIds = calculatedParam.getIdList();
        //以尺寸100分片,查询数据
        List<List<String>> pendingIds =  ListUtils.partition(subIds,100);
        for (List<String> pendingId : pendingIds) {
            pendingId.forEach(z -> {
                //获取监测点对象集合
                LineDevGetBandDTO lineDevGetDTOs = commTerminalGeneralClient.substationGetLine(z).getData();
                if (Objects.nonNull(lineDevGetDTOs.getZwList()) || Objects.nonNull(lineDevGetDTOs.getPwList())) {
                    //获取监测点id集合
                    List<String> lineIds = new ArrayList<>();
                    List<String> pLineIds = new ArrayList<>();
                    if (Objects.nonNull(lineDevGetDTOs.getZwList())) {
                        lineIds = lineDevGetDTOs.getZwList().stream().map(LineDevGetDTO::getPointId).collect(Collectors.toList());
                    }
                    if (Objects.nonNull(lineDevGetDTOs.getPwList())) {
                        pLineIds = lineDevGetDTOs.getPwList().stream().map(LineDevGetDTO::getPointId).collect(Collectors.toList());
                        lineIds.addAll(pLineIds);
                    }
                    if (!CollectionUtils.isEmpty(lineIds)) {
                        //r_mp_target_warn_d 表中字段求和
                        Map<String, Object> warnSum = rMpTargetWarnDMapper.getSumDate(beginMonth, endMonth, lineIds);
                        //r_mp_part_harmonic_detail_m 表中字段求和
                        Map<String, Object> partSum = rMpPartHarmonicDetailMMapper.getPartSum(beginMonth, endMonth, lineIds);
                        //r_mp_surplus_harmonic_detail_m 表中字段求和
                        Map<String, Object> surplusSum = mpSurplusHarmonicDetailMMapper.getSurplusSum(beginMonth, endMonth, lineIds);
                        //r_stat_substation_d 表中字段求和及计数
                        Map<String, Object> eventDetailSumAndCount = rStatSubstationDPOMapper.getSumAndCount(beginMonth, endMonth, z);

                        RStatSubstationMPO rStatSubstationMPO = new RStatSubstationMPO();
                        rStatSubstationMPO.setDataDate(LocalDateTimeUtil.parseDate(beginMonth,DatePattern.NORM_DATE_FORMATTER));
                        rStatSubstationMPO.setSubstationId(z);

                        if (!CollectionUtils.isEmpty(warnSum)) {
                            rStatSubstationMPO.setEffectiveMeasurementAccrued(warnSum.containsKey("iECount") ?
                                    Integer.parseInt(warnSum.get("iECount").toString()) : 0);

                        } else {
                            rStatSubstationMPO.setEffectiveMeasurementAccrued(0);
                        }

                        if (!CollectionUtils.isEmpty(partSum)) {
                            rStatSubstationMPO.setHarmonicCount(partSum.containsKey("measurementOverDayCount") ?
                                    Integer.parseInt(partSum.get("measurementOverDayCount").toString()) : 0);

                            rStatSubstationMPO.setFreqCount(partSum.containsKey("freqOverDayCount") ?
                                    Integer.parseInt(partSum.get("freqOverDayCount").toString()) : 0);

                            rStatSubstationMPO.setVDevCount(partSum.containsKey("vDevOverDayCount") ?
                                    Integer.parseInt(partSum.get("vDevOverDayCount").toString()) : 0);

                            rStatSubstationMPO.setVCount(partSum.containsKey("vOverDayCount") ?
                                    Integer.parseInt(partSum.get("vOverDayCount").toString()) : 0);

                            rStatSubstationMPO.setICount(partSum.containsKey("iOverDayCount") ?
                                    Integer.parseInt(partSum.get("iOverDayCount").toString()) : 0);

                            rStatSubstationMPO.setUnbalanceCount(partSum.containsKey("unbalanceOverDayCount") ?
                                    Integer.parseInt(partSum.get("unbalanceOverDayCount").toString()) : 0);

                            rStatSubstationMPO.setINegCount(partSum.containsKey("iNegOverDayCount") ?
                                    Integer.parseInt(partSum.get("iNegOverDayCount").toString()) : 0);

                        } else {
                            rStatSubstationMPO.setHarmonicCount(0);
                            rStatSubstationMPO.setFreqCount(0);
                            rStatSubstationMPO.setVDevCount(0);
                            rStatSubstationMPO.setVCount(0);
                            rStatSubstationMPO.setICount(0);
                            rStatSubstationMPO.setUnbalanceCount(0);
                            rStatSubstationMPO.setINegCount(0);
                        }

                        if (!CollectionUtils.isEmpty(surplusSum)) {
                            rStatSubstationMPO.setFlickerCount(surplusSum.containsKey("flickerOverDayCount") ?
                                    Integer.parseInt(surplusSum.get("flickerOverDayCount").toString()) : 0);

                            rStatSubstationMPO.setInuharmCount(surplusSum.containsKey("inuharmOverDayCount") ?
                                    Integer.parseInt(surplusSum.get("inuharmOverDayCount").toString()) : 0);
                        } else {
                            rStatSubstationMPO.setFlickerCount(0);
                            rStatSubstationMPO.setInuharmCount(0);
                        }

                        if (!CollectionUtils.isEmpty(eventDetailSumAndCount)) {
                            rStatSubstationMPO.setEffectiveMeasurementAverage(eventDetailSumAndCount.containsKey("effectiveMeasurementCountAvg") ?
                                    Float.parseFloat(eventDetailSumAndCount.get("effectiveMeasurementCountAvg").toString()) : 0.0f);

                            rStatSubstationMPO.setHarmonicOverDay(eventDetailSumAndCount.containsKey("harmonicCountCount") ?
                                    Float.parseFloat(eventDetailSumAndCount.get("harmonicCountCount").toString()) : 0.0f);

                            rStatSubstationMPO.setFreqOverDay(eventDetailSumAndCount.containsKey("freqCountCount") ?
                                    Float.parseFloat(eventDetailSumAndCount.get("freqCountCount").toString()) : 0.0f);

                            rStatSubstationMPO.setVDevOverDay(eventDetailSumAndCount.containsKey("vDevCountCount") ?
                                    Float.parseFloat(eventDetailSumAndCount.get("vDevCountCount").toString()) : 0.0f);

                            rStatSubstationMPO.setVOverDay(eventDetailSumAndCount.containsKey("vCountCount") ?
                                    Float.parseFloat(eventDetailSumAndCount.get("vCountCount").toString()) : 0.0f);

                            rStatSubstationMPO.setIOverDay(eventDetailSumAndCount.containsKey("iCountCount") ?
                                    Float.parseFloat(eventDetailSumAndCount.get("iCountCount").toString()) : 0.0f);

                            rStatSubstationMPO.setUnbalanceOverDay(eventDetailSumAndCount.containsKey("unbalanceCountCount") ?
                                    Float.parseFloat(eventDetailSumAndCount.get("unbalanceCountCount").toString()) : 0.0f);

                            rStatSubstationMPO.setINegOverDay(eventDetailSumAndCount.containsKey("iNegCountCount") ?
                                    Float.parseFloat(eventDetailSumAndCount.get("iNegCountCount").toString()) : 0.0f);

                            rStatSubstationMPO.setFlickerOverDay(eventDetailSumAndCount.containsKey("flickerCountCount") ?
                                    Float.parseFloat(eventDetailSumAndCount.get("flickerCountCount").toString()) : 0.0f);

                            rStatSubstationMPO.setInuharmOverDay(eventDetailSumAndCount.containsKey("inuharmCountCount") ?
                                    Float.parseFloat(eventDetailSumAndCount.get("inuharmCountCount").toString()) : 0.0f);

                            rStatSubstationMPO.setEventCount(eventDetailSumAndCount.containsKey("eventCountSum") ?
                                    Integer.parseInt(eventDetailSumAndCount.get("eventCountSum").toString()) : 0);

                            rStatSubstationMPO.setEventFreq(eventDetailSumAndCount.containsKey("eventCountFrequency") ?
                                    Float.parseFloat(eventDetailSumAndCount.get("eventCountFrequency").toString()) : 0.0f);

                            rStatSubstationMPO.setSagCount(eventDetailSumAndCount.containsKey("sagCountSum") ?
                                    Integer.parseInt(eventDetailSumAndCount.get("sagCountSum").toString()) : 0);

                            rStatSubstationMPO.setSagFreq(eventDetailSumAndCount.containsKey("sagCountFrequency") ?
                                    Float.parseFloat(eventDetailSumAndCount.get("sagCountFrequency").toString()) : 0.0f);

                            rStatSubstationMPO.setSwellCount(eventDetailSumAndCount.containsKey("swellCountSum") ?
                                    Integer.parseInt(eventDetailSumAndCount.get("swellCountSum").toString()) : 0);

                            rStatSubstationMPO.setSwellFreq(eventDetailSumAndCount.containsKey("swellCountFrequency") ?
                                    Float.parseFloat(eventDetailSumAndCount.get("swellCountFrequency").toString()) : 0.0f);

                            rStatSubstationMPO.setInterruptCount(eventDetailSumAndCount.containsKey("interruptCountSum") ?
                                    Integer.parseInt(eventDetailSumAndCount.get("interruptCountSum").toString()) : 0);

                            rStatSubstationMPO.setInterruptFreq(eventDetailSumAndCount.containsKey("interruptCountFrequency") ?
                                    Float.parseFloat(eventDetailSumAndCount.get("interruptCountFrequency").toString()) : 0.0f);

                        } else {
                            rStatSubstationMPO.setEffectiveMeasurementAverage(0.0f);
                            rStatSubstationMPO.setHarmonicOverDay(0.0f);
                            rStatSubstationMPO.setFreqOverDay(0.0f);
                            rStatSubstationMPO.setVDevOverDay(0.0f);
                            rStatSubstationMPO.setVOverDay(0.0f);
                            rStatSubstationMPO.setIOverDay(0.0f);
                            rStatSubstationMPO.setUnbalanceOverDay(0.0f);
                            rStatSubstationMPO.setINegOverDay(0.0f);
                            rStatSubstationMPO.setFlickerOverDay(0.0f);
                            rStatSubstationMPO.setInuharmOverDay(0.0f);
                            rStatSubstationMPO.setEventCount(0);
                            rStatSubstationMPO.setEventFreq(0.0f);
                            rStatSubstationMPO.setSagCount(0);
                            rStatSubstationMPO.setSagFreq(0.0f);
                            rStatSubstationMPO.setSwellCount(0);
                            rStatSubstationMPO.setSwellFreq(0.0f);
                            rStatSubstationMPO.setInterruptCount(0);
                            rStatSubstationMPO.setInterruptFreq(0.0f);
                        }
                        rStatSubstationMPOS.add(rStatSubstationMPO);
                    }
                }
            });
        }
        if (!CollectionUtils.isEmpty(rStatSubstationMPOS)) {
            rStatSubstationMPOService.saveOrUpdateBatchByMultiId(rStatSubstationMPOS, 500);
        }
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void handlerQ(CalculatedParam calculatedParam) {
        log.info(LocalDateTime.now()+"===>变电站指标统计季表算法开始");
        DecimalFormat df = new DecimalFormat("0.00");
        List<RStatSubstationQPO> rStatSubstationQPOS = new ArrayList<>();
        String beginQuarter = DateUtil.beginOfQuarter(DateUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN)).toString(DatePattern.NORM_DATE_PATTERN);
        String endQuarter = DateUtil.endOfQuarter(DateUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN)).toString(DatePattern.NORM_DATE_PATTERN);
        //变电站id
        List<String> subIds = calculatedParam.getIdList();
        //以尺寸100分片,查询数据
        List<List<String>> pendingIds =  ListUtils.partition(subIds,100);
        for (List<String> pendingId : pendingIds) {
            pendingId.forEach(z -> {
                //获取监测点对象集合
                LineDevGetBandDTO lineDevGetDTOs = commTerminalGeneralClient.substationGetLine(z).getData();
                if (Objects.nonNull(lineDevGetDTOs.getZwList()) || Objects.nonNull(lineDevGetDTOs.getPwList())) {
                    //获取监测点id集合
                    List<String> lineIds = new ArrayList<>();
                    List<String> pLineIds = new ArrayList<>();
                    if (Objects.nonNull(lineDevGetDTOs.getZwList())) {
                        lineIds = lineDevGetDTOs.getZwList().stream().map(LineDevGetDTO::getPointId).collect(Collectors.toList());
                    }
                    if (Objects.nonNull(lineDevGetDTOs.getPwList())) {
                        pLineIds = lineDevGetDTOs.getPwList().stream().map(LineDevGetDTO::getPointId).collect(Collectors.toList());
                        lineIds.addAll(pLineIds);
                    }
                    //判空
                    if (!CollectionUtils.isEmpty(lineIds)) {
                        //r_mp_target_warn_d 表中字段求和
                        Map<String, Object> warnSum = rMpTargetWarnDMapper.getSumDate(beginQuarter, endQuarter, lineIds);
                        //r_stat_substation_d 表中字段求和及计数
                        Map<String, Object> eventDetailSumAndCountD = rStatSubstationDPOMapper.getSumAndCountQ(beginQuarter, endQuarter, z);
                        //r_stat_substation_m 表中字段求和及计数
                        Map<String, Object> eventDetailSumAndCountM = rStatSubstationMPOMapper.getSumAndCount(beginQuarter, endQuarter, z);

                        RStatSubstationQPO rStatSubstationQPO = new RStatSubstationQPO();
                        rStatSubstationQPO.setDataDate(LocalDateTimeUtil.parseDate(beginQuarter,DatePattern.NORM_DATE_FORMATTER));
                        rStatSubstationQPO.setSubstationId(z);
                        if (!CollectionUtils.isEmpty(warnSum)) {
                            rStatSubstationQPO.setEffectiveMeasurementAccrued(warnSum.containsKey("iECount") ?
                                    Integer.parseInt(warnSum.get("iECount").toString()) : 0);
                        } else {
                            rStatSubstationQPO.setEffectiveMeasurementAccrued(0);
                        }
                        if (!CollectionUtils.isEmpty(eventDetailSumAndCountM)) {

                            rStatSubstationQPO.setEffectiveMeasurementAverage(eventDetailSumAndCountM.containsKey("effectiveMeasurementCountAvg") ?
                                    Float.parseFloat(eventDetailSumAndCountM.get("effectiveMeasurementCountAvg").toString()) : 0.0f);

                            rStatSubstationQPO.setHarmonicAverageOverDay(eventDetailSumAndCountM.containsKey("harmonicAverageOverDay") ?
                                    Float.parseFloat(eventDetailSumAndCountM.get("harmonicAverageOverDay").toString()) : 0.0f);

                            rStatSubstationQPO.setFreqAverageOverDay(eventDetailSumAndCountM.containsKey("freqAverageOverDay") ?
                                    Float.parseFloat(eventDetailSumAndCountM.get("freqAverageOverDay").toString()) : 0.0f);

                            rStatSubstationQPO.setVDevAverageOverDay(eventDetailSumAndCountM.containsKey("vDevAverageOverDay") ?
                                    Float.parseFloat(eventDetailSumAndCountM.get("vDevAverageOverDay").toString()) : 0.0f);

                            rStatSubstationQPO.setVAverageOverDay(eventDetailSumAndCountM.containsKey("vAverageOverDay") ?
                                    Float.parseFloat(eventDetailSumAndCountM.get("vAverageOverDay").toString()) : 0.0f);

                            rStatSubstationQPO.setIAverageOverDay(eventDetailSumAndCountM.containsKey("iAverageOverDay") ?
                                    Float.parseFloat(eventDetailSumAndCountM.get("iAverageOverDay").toString()) : 0.0f);

                            rStatSubstationQPO.setUnbalanceAverageOverDay(eventDetailSumAndCountM.containsKey("unbalanceAverageOverDay") ?
                                    Float.parseFloat(eventDetailSumAndCountM.get("unbalanceAverageOverDay").toString()) : 0.0f);

                            rStatSubstationQPO.setINegAverageOverDay(eventDetailSumAndCountM.containsKey("iNegAverageOverDay") ?
                                    Float.parseFloat(eventDetailSumAndCountM.get("iNegAverageOverDay").toString()) : 0.0f);

                            rStatSubstationQPO.setFlickerAverageOverDay(eventDetailSumAndCountM.containsKey("flickerAverageOverDay") ?
                                    Float.parseFloat(eventDetailSumAndCountM.get("flickerAverageOverDay").toString()) : 0.0f);

                            rStatSubstationQPO.setInuharmAverageOverDay(eventDetailSumAndCountM.containsKey("inuharmAverageOverDay") ?
                                    Float.parseFloat(eventDetailSumAndCountM.get("inuharmAverageOverDay").toString()) : 0.0f);

                            rStatSubstationQPO.setEventCount(eventDetailSumAndCountM.containsKey("eventCountSum") ?
                                    Integer.parseInt(eventDetailSumAndCountM.get("eventCountSum").toString()) : 0);

                            if (!Objects.equals("0", eventDetailSumAndCountD.get("eventMeasurementCountSum").toString())) {
                                String value = df.format(Float.parseFloat(eventDetailSumAndCountM.get("eventCountSum").toString())
                                        / Float.parseFloat(eventDetailSumAndCountD.get("eventMeasurementCountSum").toString()));
                                rStatSubstationQPO.setEventFreq(Float.parseFloat(value));
                            } else {
                                rStatSubstationQPO.setEventFreq(0.0f);
                            }

                            rStatSubstationQPO.setSagCount(eventDetailSumAndCountM.containsKey("sagCountSum") ?
                                    Integer.parseInt(eventDetailSumAndCountM.get("sagCountSum").toString()) : 0);

                            if (!Objects.equals("0", eventDetailSumAndCountD.get("sagMeasurementCountSum").toString())) {
                                String value = df.format(Float.parseFloat(eventDetailSumAndCountM.get("sagCountSum").toString())
                                        / Float.parseFloat(eventDetailSumAndCountD.get("sagMeasurementCountSum").toString()));
                                rStatSubstationQPO.setSagFreq(Float.parseFloat(value));
                            } else {
                                rStatSubstationQPO.setSagFreq(0.0f);
                            }

                            rStatSubstationQPO.setSwellCount(eventDetailSumAndCountM.containsKey("swellCountSum") ?
                                    Integer.parseInt(eventDetailSumAndCountM.get("swellCountSum").toString()) : 0);

                            if (!Objects.equals("0", eventDetailSumAndCountD.get("swellMeasurementCountSum").toString())) {
                                String value = df.format(Float.parseFloat(eventDetailSumAndCountM.get("swellCountSum").toString())
                                        / Float.parseFloat(eventDetailSumAndCountD.get("swellMeasurementCountSum").toString()));
                                rStatSubstationQPO.setSwellFreq(Float.parseFloat(value));
                            } else {
                                rStatSubstationQPO.setSwellFreq(0.0f);
                            }

                            rStatSubstationQPO.setInterruptCount(eventDetailSumAndCountM.containsKey("interruptCountSum") ?
                                    Integer.parseInt(eventDetailSumAndCountM.get("interruptCountSum").toString()) : 0);

                            if (!Objects.equals("0", eventDetailSumAndCountD.get("interruptMeasurementCountSum").toString())) {
                                String value = df.format(Float.parseFloat(eventDetailSumAndCountM.get("interruptCountSum").toString())
                                        / Float.parseFloat(eventDetailSumAndCountD.get("interruptMeasurementCountSum").toString()));
                                rStatSubstationQPO.setInterruptFreq(Float.parseFloat(value));
                            } else {
                                rStatSubstationQPO.setInterruptFreq(0.0f);
                            }
                        } else {
                            rStatSubstationQPO.setEffectiveMeasurementAverage(0.0f);
                            rStatSubstationQPO.setHarmonicAverageOverDay(0.0f);
                            rStatSubstationQPO.setFreqAverageOverDay(0.0f);
                            rStatSubstationQPO.setVDevAverageOverDay(0.0f);
                            rStatSubstationQPO.setVAverageOverDay(0.0f);
                            rStatSubstationQPO.setIAverageOverDay(0.0f);
                            rStatSubstationQPO.setUnbalanceAverageOverDay(0.0f);
                            rStatSubstationQPO.setINegAverageOverDay(0.0f);
                            rStatSubstationQPO.setFlickerAverageOverDay(0.0f);
                            rStatSubstationQPO.setInuharmAverageOverDay(0.0f);
                            rStatSubstationQPO.setEventCount(0);
                            rStatSubstationQPO.setEventFreq(0.0f);
                            rStatSubstationQPO.setSagCount(0);
                            rStatSubstationQPO.setSagFreq(0.0f);
                            rStatSubstationQPO.setSwellCount(0);
                            rStatSubstationQPO.setSwellFreq(0.0f);
                            rStatSubstationQPO.setInterruptCount(0);
                            rStatSubstationQPO.setInterruptFreq(0.0f);
                        }
                        rStatSubstationQPOS.add(rStatSubstationQPO);
                    }
                }
            });
        }
        if (!CollectionUtils.isEmpty(rStatSubstationQPOS)) {
            rStatSubstationQPOService.saveOrUpdateBatchByMultiId(rStatSubstationQPOS, 100);
        }
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void handlerY(CalculatedParam calculatedParam) {
        log.info(LocalDateTime.now()+"===>变电站指标统计年表算法开始");
        DecimalFormat df = new DecimalFormat("0.00");
        List<RStatSubstationYPO> rStatSubstationYPOS = new ArrayList<>();
        String beginYear = DateUtil.beginOfYear(DateUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN)).toString(DatePattern.NORM_DATE_PATTERN);
        String endYear = DateUtil.endOfYear(DateUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN)).toString(DatePattern.NORM_DATE_PATTERN);
        //变电站id
        List<String> subIds = calculatedParam.getIdList();
        //以尺寸100分片,查询数据
        List<List<String>> pendingIds =  ListUtils.partition(subIds,100);
        for (List<String> pendingId : pendingIds) {
            pendingId.forEach(z -> {
                //获取监测点对象集合
                LineDevGetBandDTO lineDevGetDTOs = commTerminalGeneralClient.substationGetLine(z).getData();
                if (Objects.nonNull(lineDevGetDTOs.getZwList()) || Objects.nonNull(lineDevGetDTOs.getPwList())) {
                    //获取监测点id集合
                    List<String> lineIds = new ArrayList<>();
                    List<String> pLineIds = new ArrayList<>();
                    if (Objects.nonNull(lineDevGetDTOs.getZwList())) {
                        lineIds = lineDevGetDTOs.getZwList().stream().map(LineDevGetDTO::getPointId).collect(Collectors.toList());
                    }
                    if (Objects.nonNull(lineDevGetDTOs.getPwList())) {
                        pLineIds = lineDevGetDTOs.getPwList().stream().map(LineDevGetDTO::getPointId).collect(Collectors.toList());
                        lineIds.addAll(pLineIds);
                    }
                    //判空
                    if (!CollectionUtils.isEmpty(lineIds)) {
                        //r_mp_target_warn_d 表中字段求和
                        Map<String, Object> warnSum = rMpTargetWarnDMapper.getSumDate(beginYear, endYear, lineIds);
                        //r_stat_substation_d 表中字段求和及计数
                        Map<String, Object> eventDetailSumAndCountD = rStatSubstationDPOMapper.getSumAndCountQ(beginYear, endYear, z);
                        //r_stat_substation_m 表中字段求和及计数
                        Map<String, Object> eventDetailSumAndCountM = rStatSubstationMPOMapper.getSumAndCount(beginYear, endYear, z);

                        RStatSubstationYPO rStatSubstationYPO = new RStatSubstationYPO();
                        rStatSubstationYPO.setDataDate(LocalDateTimeUtil.parseDate(beginYear,DatePattern.NORM_DATE_FORMATTER));
                        rStatSubstationYPO.setSubstationId(z);
                        if (!CollectionUtils.isEmpty(warnSum)) {
                            rStatSubstationYPO.setEffectiveMeasurementAccrued(warnSum.containsKey("iECount") ?
                                    Integer.parseInt(warnSum.get("iECount").toString()) : 0);
                        } else {
                            rStatSubstationYPO.setEffectiveMeasurementAccrued(0);
                        }
                        if (!CollectionUtils.isEmpty(eventDetailSumAndCountM)) {

                            rStatSubstationYPO.setEffectiveMeasurementAverage(eventDetailSumAndCountM.containsKey("effectiveMeasurementCountAvg") ?
                                    Float.parseFloat(eventDetailSumAndCountM.get("effectiveMeasurementCountAvg").toString()) : 0.0f);

                            rStatSubstationYPO.setHarmonicAverageOverDay(eventDetailSumAndCountM.containsKey("harmonicAverageOverDay") ?
                                    Float.parseFloat(eventDetailSumAndCountM.get("harmonicAverageOverDay").toString()) : 0.0f);

                            rStatSubstationYPO.setFreqAverageOverDay(eventDetailSumAndCountM.containsKey("freqAverageOverDay") ?
                                    Float.parseFloat(eventDetailSumAndCountM.get("freqAverageOverDay").toString()) : 0.0f);

                            rStatSubstationYPO.setVDevAverageOverDay(eventDetailSumAndCountM.containsKey("vDevAverageOverDay") ?
                                    Float.parseFloat(eventDetailSumAndCountM.get("vDevAverageOverDay").toString()) : 0.0f);

                            rStatSubstationYPO.setVAverageOverDay(eventDetailSumAndCountM.containsKey("vAverageOverDay") ?
                                    Float.parseFloat(eventDetailSumAndCountM.get("vAverageOverDay").toString()) : 0.0f);

                            rStatSubstationYPO.setIAverageOverDay(eventDetailSumAndCountM.containsKey("iAverageOverDay") ?
                                    Float.parseFloat(eventDetailSumAndCountM.get("iAverageOverDay").toString()) : 0.0f);

                            rStatSubstationYPO.setUnbalanceAverageOverDay(eventDetailSumAndCountM.containsKey("unbalanceAverageOverDay") ?
                                    Float.parseFloat(eventDetailSumAndCountM.get("unbalanceAverageOverDay").toString()) : 0.0f);

                            rStatSubstationYPO.setINegAverageOverDay(eventDetailSumAndCountM.containsKey("iNegAverageOverDay") ?
                                    Float.parseFloat(eventDetailSumAndCountM.get("iNegAverageOverDay").toString()) : 0.0f);

                            rStatSubstationYPO.setFlickerAverageOverDay(eventDetailSumAndCountM.containsKey("flickerAverageOverDay") ?
                                    Float.parseFloat(eventDetailSumAndCountM.get("flickerAverageOverDay").toString()) : 0.0f);

                            rStatSubstationYPO.setInuharmAverageOverDay(eventDetailSumAndCountM.containsKey("inuharmAverageOverDay") ?
                                    Float.parseFloat(eventDetailSumAndCountM.get("inuharmAverageOverDay").toString()) : 0.0f);

                            rStatSubstationYPO.setEventCount(eventDetailSumAndCountM.containsKey("eventCountSum") ?
                                    Integer.parseInt(eventDetailSumAndCountM.get("eventCountSum").toString()) : 0);

                            if (!Objects.equals("0", eventDetailSumAndCountD.get("eventMeasurementCountSum").toString())) {
                                String value = df.format(Float.parseFloat(eventDetailSumAndCountM.get("eventCountSum").toString())
                                        / Float.parseFloat(eventDetailSumAndCountD.get("eventMeasurementCountSum").toString()));
                                rStatSubstationYPO.setEventFreq(Float.parseFloat(value));
                            } else {
                                rStatSubstationYPO.setEventFreq(0.0f);
                            }

                            rStatSubstationYPO.setSagCount(eventDetailSumAndCountM.containsKey("sagCountSum") ?
                                    Integer.parseInt(eventDetailSumAndCountM.get("sagCountSum").toString()) : 0);

                            if (!Objects.equals("0", eventDetailSumAndCountD.get("sagMeasurementCountSum").toString())) {
                                String value = df.format(Float.parseFloat(eventDetailSumAndCountM.get("sagCountSum").toString())
                                        / Float.parseFloat(eventDetailSumAndCountD.get("sagMeasurementCountSum").toString()));
                                rStatSubstationYPO.setSagFreq(Float.parseFloat(value));
                            } else {
                                rStatSubstationYPO.setSagFreq(0.0f);
                            }

                            rStatSubstationYPO.setSwellCount(eventDetailSumAndCountM.containsKey("swellCountSum") ?
                                    Integer.parseInt(eventDetailSumAndCountM.get("swellCountSum").toString()) : 0);

                            if (!Objects.equals("0", eventDetailSumAndCountD.get("swellMeasurementCountSum").toString())) {
                                String value = df.format(Float.parseFloat(eventDetailSumAndCountM.get("swellCountSum").toString())
                                        / Float.parseFloat(eventDetailSumAndCountD.get("swellMeasurementCountSum").toString()));
                                rStatSubstationYPO.setSwellFreq(Float.parseFloat(value));
                            } else {
                                rStatSubstationYPO.setSwellFreq(0.0f);
                            }

                            rStatSubstationYPO.setInterruptCount(eventDetailSumAndCountM.containsKey("interruptCountSum") ?
                                    Integer.parseInt(eventDetailSumAndCountM.get("interruptCountSum").toString()) : 0);

                            if (!Objects.equals("0", eventDetailSumAndCountD.get("interruptMeasurementCountSum").toString())) {
                                String value = df.format(Float.parseFloat(eventDetailSumAndCountM.get("interruptCountSum").toString())
                                        / Float.parseFloat(eventDetailSumAndCountD.get("interruptMeasurementCountSum").toString()));
                                rStatSubstationYPO.setInterruptFreq(Float.parseFloat(value));
                            } else {
                                rStatSubstationYPO.setInterruptFreq(0.0f);
                            }
                        } else {
                            rStatSubstationYPO.setEffectiveMeasurementAverage(0.0f);
                            rStatSubstationYPO.setHarmonicAverageOverDay(0.0f);
                            rStatSubstationYPO.setFreqAverageOverDay(0.0f);
                            rStatSubstationYPO.setVDevAverageOverDay(0.0f);
                            rStatSubstationYPO.setVAverageOverDay(0.0f);
                            rStatSubstationYPO.setIAverageOverDay(0.0f);
                            rStatSubstationYPO.setUnbalanceAverageOverDay(0.0f);
                            rStatSubstationYPO.setINegAverageOverDay(0.0f);
                            rStatSubstationYPO.setFlickerAverageOverDay(0.0f);
                            rStatSubstationYPO.setInuharmAverageOverDay(0.0f);
                            rStatSubstationYPO.setEventCount(0);
                            rStatSubstationYPO.setEventFreq(0.0f);
                            rStatSubstationYPO.setSagCount(0);
                            rStatSubstationYPO.setSagFreq(0.0f);
                            rStatSubstationYPO.setSwellCount(0);
                            rStatSubstationYPO.setSwellFreq(0.0f);
                            rStatSubstationYPO.setInterruptCount(0);
                            rStatSubstationYPO.setInterruptFreq(0.0f);
                        }
                        rStatSubstationYPOS.add(rStatSubstationYPO);
                    }
                }
            });
        }
        if (!CollectionUtils.isEmpty(rStatSubstationYPOS)) {
            rStatSubstationYPOService.saveOrUpdateBatchByMultiId(rStatSubstationYPOS, 100);
        }
    }


}
