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

import cn.hutool.core.collection.CollUtil;
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.harmonic.pojo.po.RMpTargetWarnDPO;
import com.njcn.harmonic.pojo.po.RStatOrgDPO;
import com.njcn.harmonic.pojo.po.RStatOrgMPO;
import com.njcn.prepare.harmonic.mapper.mysql.area.RMpTargetWarnDMapper;
import com.njcn.prepare.harmonic.mapper.mysql.line.*;
import com.njcn.prepare.harmonic.pojo.bo.CalculatedParam;
import com.njcn.prepare.harmonic.pojo.mysql.ROperatingMonitorDPO;
import com.njcn.prepare.harmonic.pojo.mysql.po.RStatOrgQPO;
import com.njcn.prepare.harmonic.pojo.mysql.po.RStatOrgYPO;
import com.njcn.prepare.harmonic.pojo.mysql.po.line.RMpPassRateMPO;
import com.njcn.prepare.harmonic.pojo.mysql.po.line.RMpPassRateQPO;
import com.njcn.prepare.harmonic.pojo.mysql.po.line.RMpPassRateYPO;
import com.njcn.prepare.harmonic.service.mysql.area.*;
import com.njcn.prepare.harmonic.utils.PublicUtil;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.enums.DicDataEnum;
import com.njcn.system.enums.DicDataTypeEnum;
import com.njcn.system.pojo.po.DictData;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

/**
 * Description:
 * Date: 2023/5/26  10:50【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@RequiredArgsConstructor
public class RStatOrgServiceImpl implements RStatOrgService {

    private final RStatOrgDService rStatOrgDService;
    private final RStatOrgMService rStatOrgMService;
    private final RStatOrgQPOService rStatOrgQService;
    private final RStatOrgYPOService rStatOrgYService;

    private final DicDataFeignClient dicDataFeignClient;

    private final ROperatingMonitorDMapper rOperatingMonitorDMapper;
    private final RMpTargetWarnDMapper rMpTargetWarnDMapper;
    private final RMpEventDetailDMapper rMpEventDetailDMapper;
    private final RMpPassRateMMapper rMpPassRateMMapper;
    private final RMpPassRateQMapper rMpPassRateQMapper;
    private final RMpPassRateYMapper rMpPassRateYMapper;


    DecimalFormat df = new DecimalFormat("0.00");


    @Override
    public void handleDay(CalculatedParam<DeptGetChildrenMoreDTO> calculatedParam) {
        List<RStatOrgDPO> rStatOrgDPOList = new ArrayList<>();


        LocalDate localDate = LocalDateTimeUtil.parseDate(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN);        /*获取监测点类型类型*/
        /*获取数据类型*/

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

        QueryWrapper<ROperatingMonitorDPO> rOperatingMonitorDPOQueryWrapper = new QueryWrapper<>();
        QueryWrapper<RMpTargetWarnDPO> rmpTargetWarnDPOQueryWrapper = new QueryWrapper<>();
        QueryWrapper<RMpEventDetailD> rMpEventDetailDPOQueryWrapper = new QueryWrapper<>();


        for (DeptGetChildrenMoreDTO deptGetChildrenMoreDTO : calculatedParam.getIdList()) {
            if (!CollectionUtils.isEmpty(deptGetChildrenMoreDTO.getLineBaseList())) {
                List<LineDevGetDTO> lineBaseList = deptGetChildrenMoreDTO.getLineBaseList();
                Map<String, List<LineDevGetDTO>> collect = lineBaseList.stream().collect(Collectors.groupingBy(LineDevGetDTO::getLineTag));
                collect.forEach((k, v)->{

                    List<String> collect1 = v.stream().map(LineDevGetDTO::getPointId).collect(Collectors.toList());
                    RStatOrgDPO rStatOrgDPO = new RStatOrgDPO();
                    rStatOrgDPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                    rStatOrgDPO.setDataDate(localDate);
                    rStatOrgDPO.setDataType(dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId());
                    rStatOrgDPO.setMeasurementTypeClass(k);
                    rStatOrgDPO.setAllCount(v.size());
                    /*2、接入有效监测点数*/
                    rOperatingMonitorDPOQueryWrapper.clear();
                    rOperatingMonitorDPOQueryWrapper.select("1").
                            in("measurement_point_id",collect1).
                            gt("effective_minute_count",0).
                            eq("data_date",localDate);
                    Integer integer = rOperatingMonitorDMapper.selectCount(rOperatingMonitorDPOQueryWrapper);
                    rStatOrgDPO.setEffectiveMeasurement(integer);
                    /*3、稳态超标监测点数*/
                    rmpTargetWarnDPOQueryWrapper.clear();
                    rmpTargetWarnDPOQueryWrapper.select("1").
                            in("measurement_point_id",collect1).
                            eq("is_harmonic",1).
                            eq("data_date",localDate);
                    Integer integer1 = rMpTargetWarnDMapper.selectCount(rmpTargetWarnDPOQueryWrapper);
                    rStatOrgDPO.setHarmonicMeasurement(integer1);
                    /*4、发生暂态的监测点数,5、暂态指标发生次数*/
                    rMpEventDetailDPOQueryWrapper.clear();
                    rMpEventDetailDPOQueryWrapper.
                            in("measurement_point_id", collect1).
                            and(i-> i.gt("swell_times",0).or().gt("sag_times",0).or().gt("interrupt_times",0)).
                            eq("data_date",localDate);
                    List<RMpEventDetailD> rMpEventDetailDS = rMpEventDetailDMapper.selectList(rMpEventDetailDPOQueryWrapper);
                    Integer eventCount =0;
                    int sum = rMpEventDetailDS.stream().mapToInt(RMpEventDetailD::getInterruptTimes).sum();
                    int sum1 = rMpEventDetailDS.stream().mapToInt(RMpEventDetailD::getSagTimes).sum();
                    int sum2 = rMpEventDetailDS.stream().mapToInt(RMpEventDetailD::getSwellTimes).sum();

                    rStatOrgDPO.setEventCount(sum+sum1+sum2);
                    rStatOrgDPO.setEventMeasurement(rMpEventDetailDS.size());
                    /*todo这个指标不重要，后续根据配置来*/
                    rStatOrgDPO.setShouldCount(8);
                    /*7、告警监测点数*/
                    rOperatingMonitorDPOQueryWrapper.clear();
                    rOperatingMonitorDPOQueryWrapper.select("1").
                            in("measurement_point_id",collect1).
                            eq("is_unusual",1).
                            eq("data_date",localDate);
                    Integer integer2 = rOperatingMonitorDMapper.selectCount(rOperatingMonitorDPOQueryWrapper);
                    rStatOrgDPO.setWarnCount(integer2);
                    rStatOrgDPOList.add(rStatOrgDPO);
                });

            }


            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());
                    RStatOrgDPO rStatOrgDPO = new RStatOrgDPO();
                    rStatOrgDPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                    rStatOrgDPO.setDataDate(localDate);
                    rStatOrgDPO.setDataType(dataTypeMap.get(DicDataEnum.DISTRIBUTION_POINT.getCode()).getId());
                    rStatOrgDPO.setMeasurementTypeClass(getmeasurementType(k,lineSortMap));
                    rStatOrgDPO.setAllCount(v.size());
                    /*2、接入有效监测点数*/
                    rOperatingMonitorDPOQueryWrapper.clear();
                    rOperatingMonitorDPOQueryWrapper.select("1").
                            in("measurement_point_id",collect1).
                            gt("effective_minute_count",0).
                            eq("data_date",localDate);
                    Integer integer = rOperatingMonitorDMapper.selectCount(rOperatingMonitorDPOQueryWrapper);
                    rStatOrgDPO.setEffectiveMeasurement(integer);
                    /*3、稳态超标监测点数*/
                    rmpTargetWarnDPOQueryWrapper.clear();
                    rmpTargetWarnDPOQueryWrapper.select("1").
                            in("measurement_point_id",collect1).
                            eq("is_harmonic",1).
                            eq("data_date",localDate);
                    Integer integer1 = rMpTargetWarnDMapper.selectCount(rmpTargetWarnDPOQueryWrapper);
                    rStatOrgDPO.setHarmonicMeasurement(integer1);
                    /*4、发生暂态的监测点数,5、暂态指标发生次数*/
                    rMpEventDetailDPOQueryWrapper.clear();
                    rMpEventDetailDPOQueryWrapper.
                            in("measurement_point_id", collect1).
                            and(i-> i.gt("swell_times",0).or().gt("sag_times",0).or().gt("interrupt_times",0)).
                            eq("data_date",localDate);
                    List<RMpEventDetailD> rMpEventDetailDS = rMpEventDetailDMapper.selectList(rMpEventDetailDPOQueryWrapper);
                    Integer eventCount =0;
                    int sum = rMpEventDetailDS.stream().mapToInt(RMpEventDetailD::getInterruptTimes).sum();
                    int sum1 = rMpEventDetailDS.stream().mapToInt(RMpEventDetailD::getSagTimes).sum();
                    int sum2 = rMpEventDetailDS.stream().mapToInt(RMpEventDetailD::getSwellTimes).sum();

                    rStatOrgDPO.setEventCount(sum+sum1+sum2);
                    rStatOrgDPO.setEventMeasurement(rMpEventDetailDS.size());
                    /*todo这个指标不重要，后续根据配置来*/
                    rStatOrgDPO.setShouldCount(8);
                    /*7、告警监测点数*/
                    rOperatingMonitorDPOQueryWrapper.clear();
                    rOperatingMonitorDPOQueryWrapper.select("1").
                            in("measurement_point_id",collect1).
                            eq("is_unusual",1).
                            eq("data_date",localDate);
                    Integer integer2 = rOperatingMonitorDMapper.selectCount(rOperatingMonitorDPOQueryWrapper);
                    rStatOrgDPO.setWarnCount(integer2);
                    rStatOrgDPOList.add(rStatOrgDPO);
                });

            }


        }
        if (!CollectionUtils.isEmpty(rStatOrgDPOList)){
            rStatOrgDService.saveOrUpdateBatchByMultiId(rStatOrgDPOList,500);
        }

    }

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

        List<RStatOrgMPO> rStatOrgMPOList = new ArrayList<>();


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

        int days = localDate.lengthOfMonth();

        /*获取数据类型*/

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


        QueryWrapper<ROperatingMonitorDPO> rOperatingMonitorDPOQueryWrapper = new QueryWrapper<>();
        QueryWrapper<RMpTargetWarnDPO> rmpTargetWarnDPOQueryWrapper = new QueryWrapper<>();
        QueryWrapper<RMpEventDetailD> rMpEventDetailDPOQueryWrapper = new QueryWrapper<>();
        QueryWrapper<RMpPassRateMPO> rMpPassRateMPOQueryWrapper = new QueryWrapper<>();

        for (DeptGetChildrenMoreDTO deptGetChildrenMoreDTO : calculatedParam.getIdList()) {
            if (!CollectionUtils.isEmpty(deptGetChildrenMoreDTO.getLineBaseList())) {
                List<LineDevGetDTO> lineBaseList = deptGetChildrenMoreDTO.getLineBaseList();
                Map<String, List<LineDevGetDTO>> collect = lineBaseList.stream().collect(Collectors.groupingBy(LineDevGetDTO::getLineTag));
                collect.forEach((k, v)->{

                    List<String> collect1 = v.stream().map(LineDevGetDTO::getPointId).collect(Collectors.toList());
                    RStatOrgMPO rStatOrgMPO = new RStatOrgMPO();
                    rStatOrgMPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                    rStatOrgMPO.setDataDate(localDate);
                    rStatOrgMPO.setDataType(dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId());
                    rStatOrgMPO.setMeasurementTypeClass(k);
                    rStatOrgMPO.setAllCount(v.size());
                    /*2、2、日均有效接入监测点数2、日均有效接入监测点数*/
                    rOperatingMonitorDPOQueryWrapper.clear();
                    rOperatingMonitorDPOQueryWrapper.select("measurement_point_id").
                            in("measurement_point_id",collect1).
                            gt("effective_minute_count",0).
                            between("data_date",begin,end);
                    List<ROperatingMonitorDPO> rOperatingMonitorDPOS = rOperatingMonitorDMapper.selectList(rOperatingMonitorDPOQueryWrapper);
                    String value = df.format((float)rOperatingMonitorDPOS.size()/ days);

                    rStatOrgMPO.setEffectiveMeasurementAverage(Double.parseDouble(value));
                    /*3、3、累计有效接入监测点数*/
                    long count = rOperatingMonitorDPOS.stream().map(ROperatingMonitorDPO::getMeasurementPointId).distinct().count();
                    rStatOrgMPO.setEffectiveMeasurementAccrued(Integer.valueOf(count+""));

                    /*4、日均稳态超标监测点数*/
                    rmpTargetWarnDPOQueryWrapper.clear();
                    rmpTargetWarnDPOQueryWrapper.select("measurement_point_id").
                            in("measurement_point_id",collect1).
                            eq("is_harmonic",1).
                            between("data_date",begin,end);
                    List<RMpTargetWarnDPO> rMpTargetWarnDPOS = rMpTargetWarnDMapper.selectList(rmpTargetWarnDPOQueryWrapper);
                    String value1 = df.format((float)rMpTargetWarnDPOS.size()/ days);
                    rStatOrgMPO.setHarmonicMeasurementAverage(Double.parseDouble(value1));

                    /* 5、累计稳态超标监测点数*/
                    long count1 = rMpTargetWarnDPOS.stream().map(RMpTargetWarnDPO::getMeasurementPointId).distinct().count();

                    rStatOrgMPO.setHarmonicMeasurementAccrued(Integer.valueOf(count1+""));

                    /*6、日均稳态超标监测点数占比*/
                    String value2 = df.format(rStatOrgMPO.getHarmonicMeasurementAccrued()*100.0 / rStatOrgMPO.getAllCount());
                    rStatOrgMPO.setHarmonicMeasurementRatioAverage(Double.parseDouble(value2));

                    /*7、累计超标天数*/
                    rStatOrgMPO.setOverDayAccrued(rMpTargetWarnDPOS.size());
                    /*8、频率偏差合格率,9、电压偏差合格率,10、闪变合格率*/
                    rMpPassRateMPOQueryWrapper.clear();
                    rMpPassRateMPOQueryWrapper.select("avg(freq_pass_rate) as freq_pass_rate","avg(v_dev_pass_rate) as v_dev_pass_rate","avg(flicker_pass_rate) as flicker_pass_rate").
                            in("measurement_point_id",collect1).
                            between("data_date",begin,end);
                    RMpPassRateMPO rMpPassRateMPO = rMpPassRateMMapper.selectOne(rMpPassRateMPOQueryWrapper);
                    //todo 这边有点问题,每日监测点信息从公共接口获取，当日监测点应该都一样，不存在表数据不一致的情况
                    if (!Objects.isNull(rMpPassRateMPO)){
                    rStatOrgMPO.setFreqPassRate(rMpPassRateMPO.getFreqPassRate());
                    rStatOrgMPO.setFlickerPassRate(rMpPassRateMPO.getFlickerPassRate());
                    rStatOrgMPO.setVDevPassRate(rMpPassRateMPO.getVDevPassRate());
                    }

                    /*11、日均监测到暂态指标的监测点数*/
                    rMpEventDetailDPOQueryWrapper.clear();
                    rMpEventDetailDPOQueryWrapper.
                            in("measurement_point_id", collect1).
                            and(i-> i.gt("swell_times",0).or().gt("sag_times",0).or().gt("interrupt_times",0)).
                            between("data_date",begin,end);
                    List<RMpEventDetailD> rMpEventDetailDS = rMpEventDetailDMapper.selectList(rMpEventDetailDPOQueryWrapper);
                    Integer eventCount =0;
                    int sum = rMpEventDetailDS.stream().mapToInt(RMpEventDetailD::getInterruptTimes).sum();
                    int sum1 = rMpEventDetailDS.stream().mapToInt(RMpEventDetailD::getSagTimes).sum();
                    int sum2 = rMpEventDetailDS.stream().mapToInt(RMpEventDetailD::getSwellTimes).sum();
                    String value3 = df.format((float) rMpEventDetailDS.size()/ days);
                    rStatOrgMPO.setEventMeasurementAverage(Double.valueOf(value3));
                    /*12、累计监测到暂态指标的监测点数*/
                    long count2 = rMpEventDetailDS.stream().map(RMpEventDetailD::getMeasurementPointId).distinct().count();

                    rStatOrgMPO.setEventMeasurementAccrued(Integer.valueOf(count2+""));
                    /*13、日均暂态超标监测点数占比*/
                    String value4 = df.format(rStatOrgMPO.getEventMeasurementAverage()*100 / rStatOrgMPO.getAllCount());
                    rStatOrgMPO.setEventMeasurementRatioAverage(Double.valueOf(value4));

                    /*15、暂态指标发生频次*/
                    rStatOrgMPO.setEventCount(sum+sum1+sum2);

                    /*14、暂态指标发生频次*/
                    long count3 = rMpEventDetailDS.stream().map(RMpEventDetailD::getMeasurementPointId).distinct().count();
                    if(count3==0){
                        rStatOrgMPO.setEventFreq(0.00);

                    }else {
                        String value5 = df.format((float)rStatOrgMPO.getEventCount() / count3);

                        rStatOrgMPO.setEventFreq(Double.valueOf(value5));
                    }




                    /*todo这个指标不重要，后续根据配置来*/
                    /*17、告警监测点数*/
                    rStatOrgMPO.setShouldCount(8);
                    rOperatingMonitorDPOQueryWrapper.clear();
                    rOperatingMonitorDPOQueryWrapper.select("distinct(measurement_point_id)").
                            in("measurement_point_id",collect1).
                            eq("is_unusual",1).
                            between("data_date",begin,end);
                    Integer integer2 = rOperatingMonitorDMapper.selectCount(rOperatingMonitorDPOQueryWrapper);
                    rStatOrgMPO.setWarnCount(integer2);
                    rStatOrgMPOList.add(rStatOrgMPO);
                });

            }
            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());
                    RStatOrgMPO rStatOrgMPO = new RStatOrgMPO();
                    rStatOrgMPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                    rStatOrgMPO.setDataDate(localDate);
                    rStatOrgMPO.setDataType(dataTypeMap.get(DicDataEnum.DISTRIBUTION_POINT.getCode()).getId());
                    rStatOrgMPO.setMeasurementTypeClass(getmeasurementType(k,lineSortMap));
                    rStatOrgMPO.setAllCount(v.size());
                    /*2、2、日均有效接入监测点数2、日均有效接入监测点数*/
                    rOperatingMonitorDPOQueryWrapper.clear();
                    rOperatingMonitorDPOQueryWrapper.select("measurement_point_id").
                            in("measurement_point_id",collect1).
                            gt("effective_minute_count",0).
                            between("data_date",begin,end);
                    List<ROperatingMonitorDPO> rOperatingMonitorDPOS = rOperatingMonitorDMapper.selectList(rOperatingMonitorDPOQueryWrapper);
                    String value = df.format((float)rOperatingMonitorDPOS.size()/ days);

                    rStatOrgMPO.setEffectiveMeasurementAverage(Double.parseDouble(value));
                    /*3、3、累计有效接入监测点数*/
                    long count = rOperatingMonitorDPOS.stream().map(ROperatingMonitorDPO::getMeasurementPointId).distinct().count();
                    rStatOrgMPO.setEffectiveMeasurementAccrued(Integer.valueOf(count+""));

                    /*4、日均稳态超标监测点数*/
                    rmpTargetWarnDPOQueryWrapper.clear();
                    rmpTargetWarnDPOQueryWrapper.select("measurement_point_id").
                            in("measurement_point_id",collect1).
                            eq("is_harmonic",1).
                            between("data_date",begin,end);
                    List<RMpTargetWarnDPO> rMpTargetWarnDPOS = rMpTargetWarnDMapper.selectList(rmpTargetWarnDPOQueryWrapper);
                    String value1 = df.format((float)rMpTargetWarnDPOS.size()/ days);
                    rStatOrgMPO.setHarmonicMeasurementAverage(Double.parseDouble(value1));

                    /* 5、累计稳态超标监测点数*/
                    long count1 = rMpTargetWarnDPOS.stream().map(RMpTargetWarnDPO::getMeasurementPointId).distinct().count();

                    rStatOrgMPO.setHarmonicMeasurementAccrued(Integer.valueOf(count1+""));

                    /*6、日均稳态超标监测点数占比*/
                    String value2 = df.format(rStatOrgMPO.getHarmonicMeasurementAccrued()*100.0 / rStatOrgMPO.getAllCount());
                    rStatOrgMPO.setHarmonicMeasurementRatioAverage(Double.parseDouble(value2));

                    /*7、累计超标天数*/
                    rStatOrgMPO.setOverDayAccrued(rMpTargetWarnDPOS.size());
                    /*8、频率偏差合格率,9、电压偏差合格率,10、闪变合格率*/
                    rMpPassRateMPOQueryWrapper.clear();
                    rMpPassRateMPOQueryWrapper.select("avg(freq_pass_rate) as freq_pass_rate","avg(v_dev_pass_rate) as v_dev_pass_rate","avg(flicker_pass_rate) as flicker_pass_rate").
                            in("measurement_point_id",collect1).
                            between("data_date",begin,end);
                    RMpPassRateMPO rMpPassRateMPO = rMpPassRateMMapper.selectOne(rMpPassRateMPOQueryWrapper);
                    rStatOrgMPO.setFreqPassRate(rMpPassRateMPO.getFreqPassRate());
                    rStatOrgMPO.setFlickerPassRate(rMpPassRateMPO.getFlickerPassRate());
                    rStatOrgMPO.setVDevPassRate(rMpPassRateMPO.getVDevPassRate());



                    /*11、日均监测到暂态指标的监测点数*/
                    rMpEventDetailDPOQueryWrapper.clear();
                    rMpEventDetailDPOQueryWrapper.
                            in("measurement_point_id", collect1).
                            and(i-> i.gt("swell_times",0).or().gt("sag_times",0).or().gt("interrupt_times",0)).
                            between("data_date",begin,end);
                    List<RMpEventDetailD> rMpEventDetailDS = rMpEventDetailDMapper.selectList(rMpEventDetailDPOQueryWrapper);
                    Integer eventCount =0;
                    int sum = rMpEventDetailDS.stream().mapToInt(RMpEventDetailD::getInterruptTimes).sum();
                    int sum1 = rMpEventDetailDS.stream().mapToInt(RMpEventDetailD::getSagTimes).sum();
                    int sum2 = rMpEventDetailDS.stream().mapToInt(RMpEventDetailD::getSwellTimes).sum();
                    String value3 = df.format((float) rMpEventDetailDS.size()/ days);
                    rStatOrgMPO.setEventMeasurementAverage(Double.valueOf(value3));
                    /*12、累计监测到暂态指标的监测点数*/
                    long count2 = rMpEventDetailDS.stream().map(RMpEventDetailD::getMeasurementPointId).distinct().count();

                    rStatOrgMPO.setEventMeasurementAccrued(Integer.valueOf(count2+""));
                    /*13、日均暂态超标监测点数占比*/
                    String value4 = df.format(rStatOrgMPO.getEventMeasurementAverage() *100/ rStatOrgMPO.getAllCount());
                    rStatOrgMPO.setEventMeasurementRatioAverage(Double.valueOf(value4));

                    /*15、暂态指标发生频次*/
                    rStatOrgMPO.setEventCount(sum+sum1+sum2);

                    /*14、暂态指标发生频次*/
                    long count3 = rMpEventDetailDS.stream().map(RMpEventDetailD::getMeasurementPointId).distinct().count();
                    if(count3==0){
                        rStatOrgMPO.setEventFreq(0.00);

                    }else{
                        String value5 = df.format((float)rStatOrgMPO.getEventCount() / count3);

                        rStatOrgMPO.setEventFreq(Double.valueOf(value5));

                    }



                    /*todo这个指标不重要，后续根据配置来*/
                    /*17、告警监测点数*/
                    rStatOrgMPO.setShouldCount(8);
                    rOperatingMonitorDPOQueryWrapper.clear();
                    rOperatingMonitorDPOQueryWrapper.select("distinct(measurement_point_id)").
                            in("measurement_point_id",collect1).
                            eq("is_unusual",1).
                            between("data_date",begin,end);
                    Integer integer2 = rOperatingMonitorDMapper.selectCount(rOperatingMonitorDPOQueryWrapper);
                    rStatOrgMPO.setWarnCount(integer2);

                    rStatOrgMPOList.add(rStatOrgMPO);
                });

            }
        }
        if (!CollectionUtils.isEmpty(rStatOrgMPOList)){
            rStatOrgMService.saveOrUpdateBatchByMultiId(rStatOrgMPOList,500);
        }

    }

    @Override
    public void handleQtr(CalculatedParam<DeptGetChildrenMoreDTO> calculatedParam) {
        List<RStatOrgQPO> rStatOrgQPOList = new ArrayList<>();

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

        int days = PublicUtil.getQtrDays(localDate);
        /*获取数据类型*/

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

        QueryWrapper<ROperatingMonitorDPO> rOperatingMonitorDPOQueryWrapper = new QueryWrapper<>();
        QueryWrapper<RMpTargetWarnDPO> rmpTargetWarnDPOQueryWrapper = new QueryWrapper<>();
        QueryWrapper<RMpEventDetailD> rMpEventDetailDPOQueryWrapper = new QueryWrapper<>();
        QueryWrapper<RMpPassRateQPO> rMpPassRateQPOQueryWrapper = new QueryWrapper<>();



        for (DeptGetChildrenMoreDTO deptGetChildrenMoreDTO : calculatedParam.getIdList()) {

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

                    List<String> collect1 = v.stream().map(LineDevGetDTO::getPointId).collect(Collectors.toList());
                    RStatOrgQPO rStatOrgQPO = new RStatOrgQPO();
                    rStatOrgQPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                    rStatOrgQPO.setDataDate(localDate);
                    rStatOrgQPO.setDataType(dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId());
                    rStatOrgQPO.setMeasurementTypeClass(k);
                    rStatOrgQPO.setAllCount(v.size());
                    /*2、2、日均有效接入监测点数2、日均有效接入监测点数*/
                    rOperatingMonitorDPOQueryWrapper.clear();
                    rOperatingMonitorDPOQueryWrapper.select("measurement_point_id").
                            in("measurement_point_id",collect1).
                            gt("effective_minute_count",0).
                            between("data_date",begin,end);
                    List<ROperatingMonitorDPO> rOperatingMonitorDPOS = rOperatingMonitorDMapper.selectList(rOperatingMonitorDPOQueryWrapper);
                    String value = df.format((float)rOperatingMonitorDPOS.size()/ days);

                    rStatOrgQPO.setEffectiveMeasurementAverage(Double.parseDouble(value));
                    /*3、3、累计有效接入监测点数*/
                    List<String> monitorIds = rOperatingMonitorDPOS.stream().map(ROperatingMonitorDPO::getMeasurementPointId).distinct().collect(Collectors.toList());
                    rStatOrgQPO.setEffectiveMeasurementAccrued(monitorIds.size());

                    /*4、日均稳态超标监测点数 TODO 用有效监测点*/
                    List<RMpTargetWarnDPO> rMpTargetWarnDPOS=new ArrayList<>();
                    if(CollUtil.isNotEmpty(monitorIds)){
                        rmpTargetWarnDPOQueryWrapper.clear();
                        rmpTargetWarnDPOQueryWrapper.select("measurement_point_id").
                                in("measurement_point_id",monitorIds).
                                eq("is_harmonic",1).
                                between("data_date",begin,end);
                         rMpTargetWarnDPOS.addAll(rMpTargetWarnDMapper.selectList(rmpTargetWarnDPOQueryWrapper));
                    }
                    String value1 = df.format((float)rMpTargetWarnDPOS.size()/ days);
                    rStatOrgQPO.setHarmonicMeasurementAverage(Double.parseDouble(value1));

                    /* 5、累计稳态超标监测点数*/
                    long count1 = rMpTargetWarnDPOS.stream().map(RMpTargetWarnDPO::getMeasurementPointId).distinct().count();

                    rStatOrgQPO.setHarmonicMeasurementAccrued(Integer.valueOf(count1+""));

                    /*6、日均稳态超标监测点数占比*/
                    String value2 = df.format(rStatOrgQPO.getHarmonicMeasurementAccrued()*100.0 / rStatOrgQPO.getAllCount());
                    rStatOrgQPO.setHarmonicMeasurementRatioAverage(Double.parseDouble(value2));

                    /*7、累计超标天数*/
                    if(count1==0){
                        rStatOrgQPO.setAverageOverDay(0.00);

                    }else {
                        String format = df.format((float)rMpTargetWarnDPOS.size()/ count1);

                        rStatOrgQPO.setAverageOverDay(Double.parseDouble(format));
                    }

                    /*8、频率偏差合格率,9、电压偏差合格率,10、闪变合格率*/
                    rMpPassRateQPOQueryWrapper.clear();
                    rMpPassRateQPOQueryWrapper.select("avg(freq_pass_rate) as freq_pass_rate","avg(v_dev_pass_rate) as v_dev_pass_rate","avg(flicker_pass_rate) as flicker_pass_rate").
                            in("measurement_point_id",collect1).
                            between("data_date",begin,end);
                    RMpPassRateQPO rMpPassRateQPO = rMpPassRateQMapper.selectOne(rMpPassRateQPOQueryWrapper);
                    rStatOrgQPO.setFreqPassRate(rMpPassRateQPO.getFreqPassRate());
                    rStatOrgQPO.setFlickerPassRate(rMpPassRateQPO.getFlickerPassRate());
                    rStatOrgQPO.setVDevPassRate(rMpPassRateQPO.getVDevPassRate());



                    /*11、日均监测到暂态指标的监测点数*/
                    rMpEventDetailDPOQueryWrapper.clear();
                    rMpEventDetailDPOQueryWrapper.
                            in("measurement_point_id", collect1).
                            and(i-> i.gt("swell_times",0).or().gt("sag_times",0).or().gt("interrupt_times",0)).
                            between("data_date",begin,end);
                    List<RMpEventDetailD> rMpEventDetailDS = rMpEventDetailDMapper.selectList(rMpEventDetailDPOQueryWrapper);
                    Integer eventCount =0;
                    int sum = rMpEventDetailDS.stream().mapToInt(RMpEventDetailD::getInterruptTimes).sum();
                    int sum1 = rMpEventDetailDS.stream().mapToInt(RMpEventDetailD::getSagTimes).sum();
                    int sum2 = rMpEventDetailDS.stream().mapToInt(RMpEventDetailD::getSwellTimes).sum();
                    String value3 = df.format((float) rMpEventDetailDS.size()/ days);
                    rStatOrgQPO.setEventMeasurementAverage(Double.valueOf(value3));
                    /*12、累计监测到暂态指标的监测点数*/
                    long count2 = rMpEventDetailDS.stream().map(RMpEventDetailD::getMeasurementPointId).distinct().count();

                    rStatOrgQPO.setEventMeasurementAccrued(Integer.valueOf(count2+""));
                    /*13、日均暂态超标监测点数占比*/
                    String value4 = df.format(rStatOrgQPO.getEventMeasurementAverage()*100.0 / rStatOrgQPO.getAllCount());
                    rStatOrgQPO.setEventMeasurementRatioAverage(Double.valueOf(value4));

                    /*15、暂态指标发生频次*/
                    rStatOrgQPO.setEventCount(sum+sum1+sum2);

                    /*14、暂态指标发生频次*/
                    long count3 = rMpEventDetailDS.stream().map(RMpEventDetailD::getMeasurementPointId).distinct().count();
                    if(count3==0){
                        rStatOrgQPO.setEventFreq(0.00);

                    }else {
                        String value5 = df.format((float)rStatOrgQPO.getEventCount() / count3);

                        rStatOrgQPO.setEventFreq(Double.valueOf(value5));
                    }




                    /*todo这个指标不重要，后续根据配置来*/
                    /*17、告警监测点数*/
                    rStatOrgQPO.setShouldCount(8);
                    rOperatingMonitorDPOQueryWrapper.clear();
                    rOperatingMonitorDPOQueryWrapper.select("distinct(measurement_point_id)").
                            in("measurement_point_id",collect1).
                            eq("is_unusual",1).
                            between("data_date",begin,end);
                    Integer integer2 = rOperatingMonitorDMapper.selectCount(rOperatingMonitorDPOQueryWrapper);
                    rStatOrgQPO.setWarnCount(integer2);

                    rStatOrgQPOList.add(rStatOrgQPO);
                });

            }
            /*配网*/
            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());
                    RStatOrgQPO rStatOrgQPO = new RStatOrgQPO();
                    rStatOrgQPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                    rStatOrgQPO.setDataDate(localDate);
                    rStatOrgQPO.setDataType(dataTypeMap.get(DicDataEnum.DISTRIBUTION_POINT.getCode()).getId());
                    rStatOrgQPO.setMeasurementTypeClass(getmeasurementType(k,lineSortMap));
                    rStatOrgQPO.setAllCount(v.size());
                    /*2、2、日均有效接入监测点数2、日均有效接入监测点数*/
                    rOperatingMonitorDPOQueryWrapper.clear();
                    rOperatingMonitorDPOQueryWrapper.select("measurement_point_id").
                            in("measurement_point_id",collect1).
                            gt("effective_minute_count",0).
                            between("data_date",begin,end);
                    List<ROperatingMonitorDPO> rOperatingMonitorDPOS = rOperatingMonitorDMapper.selectList(rOperatingMonitorDPOQueryWrapper);
                    String value = df.format((float)rOperatingMonitorDPOS.size()/ days);

                    rStatOrgQPO.setEffectiveMeasurementAverage(Double.parseDouble(value));
                    /*3、3、累计有效接入监测点数*/
                    long count = rOperatingMonitorDPOS.stream().map(ROperatingMonitorDPO::getMeasurementPointId).distinct().count();
                    rStatOrgQPO.setEffectiveMeasurementAccrued(Integer.valueOf(count+""));

                    /*4、日均稳态超标监测点数*/
                    rmpTargetWarnDPOQueryWrapper.clear();
                    rmpTargetWarnDPOQueryWrapper.select("measurement_point_id").
                            in("measurement_point_id",collect1).
                            eq("is_harmonic",1).
                            between("data_date",begin,end);
                    List<RMpTargetWarnDPO> rMpTargetWarnDPOS = rMpTargetWarnDMapper.selectList(rmpTargetWarnDPOQueryWrapper);
                    String value1 = df.format((float)rMpTargetWarnDPOS.size()/ days);
                    rStatOrgQPO.setHarmonicMeasurementAverage(Double.parseDouble(value1));

                    /* 5、累计稳态超标监测点数*/
                    long count1 = rMpTargetWarnDPOS.stream().map(RMpTargetWarnDPO::getMeasurementPointId).distinct().count();

                    rStatOrgQPO.setHarmonicMeasurementAccrued(Integer.valueOf(count1+""));

                    /*6、日均稳态超标监测点数占比*/
                    String value2 = df.format(rStatOrgQPO.getHarmonicMeasurementAccrued()*100.0 / rStatOrgQPO.getAllCount());
                    rStatOrgQPO.setHarmonicMeasurementRatioAverage(Double.parseDouble(value2));

                    /*7、累计超标天数*/
                    if(count1==0) {
                        rStatOrgQPO.setAverageOverDay(0.00);

                    }else{
                        String format = df.format((float)rMpTargetWarnDPOS.size()/ count1);

                        rStatOrgQPO.setAverageOverDay(Double.parseDouble(format));
                    }

                    /*8、频率偏差合格率,9、电压偏差合格率,10、闪变合格率*/
                    rMpPassRateQPOQueryWrapper.clear();
                    rMpPassRateQPOQueryWrapper.select("avg(freq_pass_rate) as freq_pass_rate","avg(v_dev_pass_rate) as v_dev_pass_rate","avg(flicker_pass_rate) as flicker_pass_rate").
                            in("measurement_point_id",collect1).
                            between("data_date",begin,end);
                    RMpPassRateQPO rMpPassRateQPO = rMpPassRateQMapper.selectOne(rMpPassRateQPOQueryWrapper);
                    rStatOrgQPO.setFreqPassRate(rMpPassRateQPO.getFreqPassRate());
                    rStatOrgQPO.setFlickerPassRate(rMpPassRateQPO.getFlickerPassRate());
                    rStatOrgQPO.setVDevPassRate(rMpPassRateQPO.getVDevPassRate());



                    /*11、日均监测到暂态指标的监测点数*/
                    rMpEventDetailDPOQueryWrapper.clear();
                    rMpEventDetailDPOQueryWrapper.
                            in("measurement_point_id", collect1).
                            and(i-> i.gt("swell_times",0).or().gt("sag_times",0).or().gt("interrupt_times",0)).
                            between("data_date",begin,end);
                    List<RMpEventDetailD> rMpEventDetailDS = rMpEventDetailDMapper.selectList(rMpEventDetailDPOQueryWrapper);
                    Integer eventCount =0;
                    int sum = rMpEventDetailDS.stream().mapToInt(RMpEventDetailD::getInterruptTimes).sum();
                    int sum1 = rMpEventDetailDS.stream().mapToInt(RMpEventDetailD::getSagTimes).sum();
                    int sum2 = rMpEventDetailDS.stream().mapToInt(RMpEventDetailD::getSwellTimes).sum();
                    String value3 = df.format((float) rMpEventDetailDS.size()/ days);
                    rStatOrgQPO.setEventMeasurementAverage(Double.valueOf(value3));
                    /*12、累计监测到暂态指标的监测点数*/
                    long count2 = rMpEventDetailDS.stream().map(RMpEventDetailD::getMeasurementPointId).distinct().count();

                    rStatOrgQPO.setEventMeasurementAccrued(Integer.valueOf(count2+""));
                    /*13、日均暂态超标监测点数占比*/
                    String value4 = df.format(rStatOrgQPO.getEventMeasurementAverage()*100.0 / rStatOrgQPO.getAllCount());
                    rStatOrgQPO.setEventMeasurementRatioAverage(Double.valueOf(value4));

                    /*15、暂态指标发生频次*/
                    rStatOrgQPO.setEventCount(sum+sum1+sum2);

                    /*14、暂态指标发生频次*/
                    long count3 = rMpEventDetailDS.stream().map(RMpEventDetailD::getMeasurementPointId).distinct().count();

                    if(count3==0){
                        rStatOrgQPO.setEventFreq(0.00);

                    }else{
                        String value5 = df.format((float)rStatOrgQPO.getEventCount() / count3);

                        rStatOrgQPO.setEventFreq(Double.valueOf(value5));
                    }




                    /*todo这个指标不重要，后续根据配置来*/
                    /*17、告警监测点数*/
                    rStatOrgQPO.setShouldCount(8);
                    rOperatingMonitorDPOQueryWrapper.clear();
                    rOperatingMonitorDPOQueryWrapper.select("distinct(measurement_point_id)").
                            in("measurement_point_id",collect1).
                            eq("is_unusual",1).
                            between("data_date",begin,end);
                    Integer integer2 = rOperatingMonitorDMapper.selectCount(rOperatingMonitorDPOQueryWrapper);
                    rStatOrgQPO.setWarnCount(integer2);

                    rStatOrgQPOList.add(rStatOrgQPO);
                });

            }

        };
        if (!CollectionUtils.isEmpty(rStatOrgQPOList)){
            rStatOrgQService.saveOrUpdateBatchByMultiId(rStatOrgQPOList,500);
        }

    }

    @Override
    public void handleYear(CalculatedParam<DeptGetChildrenMoreDTO> calculatedParam) {
        List<RStatOrgYPO> rStatOrgYList = new ArrayList<>();

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


        int days = localDate.lengthOfYear();
        /*获取数据类型*/

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

        QueryWrapper<ROperatingMonitorDPO> rOperatingMonitorDPOQueryWrapper = new QueryWrapper<>();
        QueryWrapper<RMpTargetWarnDPO> rmpTargetWarnDPOQueryWrapper = new QueryWrapper<>();
        QueryWrapper<RMpEventDetailD> rMpEventDetailDPOQueryWrapper = new QueryWrapper<>();
        QueryWrapper<RMpPassRateYPO> rMpPassRateYPOQueryWrapper = new QueryWrapper<>();



        for (DeptGetChildrenMoreDTO deptGetChildrenMoreDTO : calculatedParam.getIdList()) {

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

                    List<String> collect1 = v.stream().map(LineDevGetDTO::getPointId).collect(Collectors.toList());
                    RStatOrgYPO rStatOrgYPO = new RStatOrgYPO();
                    rStatOrgYPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                    rStatOrgYPO.setDataDate(localDate);
                    rStatOrgYPO.setDataType(dataTypeMap.get(DicDataEnum.MAINNET_POINT.getCode()).getId());
                    rStatOrgYPO.setMeasurementTypeClass(k);
                    rStatOrgYPO.setAllCount(v.size());
                    /*2、2、日均有效接入监测点数2、日均有效接入监测点数*/
                    rOperatingMonitorDPOQueryWrapper.clear();
                    rOperatingMonitorDPOQueryWrapper.select("measurement_point_id").
                            in("measurement_point_id",collect1).
                            gt("effective_minute_count",0).
                            between("data_date",begin,end);
                    List<ROperatingMonitorDPO> rOperatingMonitorDPOS = rOperatingMonitorDMapper.selectList(rOperatingMonitorDPOQueryWrapper);
                    String value = df.format((float)rOperatingMonitorDPOS.size()/ days);

                    rStatOrgYPO.setEffectiveMeasurementAverage(Double.valueOf(value));
                    /*3、3、累计有效接入监测点数*/
                    long count = rOperatingMonitorDPOS.stream().map(ROperatingMonitorDPO::getMeasurementPointId).distinct().count();
                    rStatOrgYPO.setEffectiveMeasurementAccrued(Integer.valueOf(count+""));

                    /*4、日均稳态超标监测点数*/
                    rmpTargetWarnDPOQueryWrapper.clear();
                    rmpTargetWarnDPOQueryWrapper.select("measurement_point_id").
                            in("measurement_point_id",collect1).
                            eq("is_harmonic",1).
                            between("data_date",begin,end);
                    List<RMpTargetWarnDPO> rMpTargetWarnDPOS = rMpTargetWarnDMapper.selectList(rmpTargetWarnDPOQueryWrapper);
                    String value1 = df.format((float)rMpTargetWarnDPOS.size()/ days);
                    rStatOrgYPO.setOverLimitMeasurementAverage(Double.parseDouble(value1));

                    /* 5、累计稳态超标监测点数*/
                    long count1 = rMpTargetWarnDPOS.stream().map(RMpTargetWarnDPO::getMeasurementPointId).distinct().count();

                    rStatOrgYPO.setOverLimitMeasurementAccrued(Integer.valueOf(count1+""));

                    /*6、日均稳态超标监测点数占比*/

                    String value2 = df.format(rStatOrgYPO.getOverLimitMeasurementAccrued()*100.0 / rStatOrgYPO.getAllCount());
                    rStatOrgYPO.setHarmonicMeasurementRatioAverage(Double.parseDouble(value2));

                    /*7、稳态指标平均超标天数*/
                    if(count1==0){
                        rStatOrgYPO.setAverageOverDay(0.00);
                    }else{
                        String format = df.format((float)rMpTargetWarnDPOS.size()/ count1);
                        rStatOrgYPO.setAverageOverDay(Double.parseDouble(format));


                    }

                    /*8、频率偏差合格率,9、电压偏差合格率,10、闪变合格率*/
                    rMpPassRateYPOQueryWrapper.clear();
                    rMpPassRateYPOQueryWrapper.select("avg(freq_pass_rate) as freq_pass_rate","avg(v_dev_pass_rate) as v_dev_pass_rate","avg(flicker_pass_rate) as flicker_pass_rate").
                            in("measurement_point_id",collect1).
                            between("data_date",begin,end);
                    RMpPassRateYPO rMpPassRateYPO = rMpPassRateYMapper.selectOne(rMpPassRateYPOQueryWrapper);
                    rStatOrgYPO.setFreqPassRate(rMpPassRateYPO.getFreqPassRate());
                    rStatOrgYPO.setFlickerPassRate(rMpPassRateYPO.getFlickerPassRate());
                    rStatOrgYPO.setVDevPassRate(rMpPassRateYPO.getVDevPassRate());



                    /*11、日均监测到暂态指标的监测点数*/
                    rMpEventDetailDPOQueryWrapper.clear();
                    rMpEventDetailDPOQueryWrapper.
                            in("measurement_point_id", collect1).
                            and(i-> i.gt("swell_times",0).or().gt("sag_times",0).or().gt("interrupt_times",0)).
                            between("data_date",begin,end);
                    List<RMpEventDetailD> rMpEventDetailDS = rMpEventDetailDMapper.selectList(rMpEventDetailDPOQueryWrapper);
                    Integer eventCount =0;
                    int sum = rMpEventDetailDS.stream().mapToInt(RMpEventDetailD::getInterruptTimes).sum();
                    int sum1 = rMpEventDetailDS.stream().mapToInt(RMpEventDetailD::getSagTimes).sum();
                    int sum2 = rMpEventDetailDS.stream().mapToInt(RMpEventDetailD::getSwellTimes).sum();
                    String value3 = df.format((float) rMpEventDetailDS.size()/ days);
                    rStatOrgYPO.setEventMeasurementAverage(Double.valueOf(value3));
                    /*12、累计监测到暂态指标的监测点数*/
                    long count2 = rMpEventDetailDS.stream().map(RMpEventDetailD::getMeasurementPointId).distinct().count();

                    rStatOrgYPO.setEventMeasurementAccrued(Integer.valueOf(count2+""));
                    /*13、日均暂态超标监测点数占比*/
                    String value4 = df.format(rStatOrgYPO.getEventMeasurementAverage()*100.0 / rStatOrgYPO.getAllCount());
                    rStatOrgYPO.setEventMeasurementRatioAverage(Double.valueOf(value4));

                    /*15、暂态指标发生频次*/
                    rStatOrgYPO.setEventCount(sum+sum1+sum2);

                    /*14、暂态指标发生频次*/
                    long count3 = rMpEventDetailDS.stream().map(RMpEventDetailD::getMeasurementPointId).distinct().count();
                    if(count3==0){
                        rStatOrgYPO.setEventFreq(0.00);
                    }else{
                        String value5 = df.format((float)rStatOrgYPO.getEventCount() / count3);

                        rStatOrgYPO.setEventFreq(Double.valueOf(value5));
                    }




                    /*todo这个指标不重要，后续根据配置来*/
                    /*17、告警监测点数*/
                    rStatOrgYPO.setShouldCount(8);
                    rOperatingMonitorDPOQueryWrapper.clear();
                    rOperatingMonitorDPOQueryWrapper.select("distinct(measurement_point_id)").
                            in("measurement_point_id",collect1).
                            eq("is_unusual",1).
                            between("data_date",begin,end);
                    Integer integer2 = rOperatingMonitorDMapper.selectCount(rOperatingMonitorDPOQueryWrapper);
                    rStatOrgYPO.setWarnCount(integer2);

                    rStatOrgYList.add(rStatOrgYPO);
                });

            }
            /*配网*/
            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());
                    RStatOrgYPO rStatOrgYPO = new RStatOrgYPO();
                    rStatOrgYPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                    rStatOrgYPO.setDataDate(localDate);
                    rStatOrgYPO.setDataType(dataTypeMap.get(DicDataEnum.DISTRIBUTION_POINT.getCode()).getId());
                    rStatOrgYPO.setMeasurementTypeClass(getmeasurementType(k,lineSortMap));
                    rStatOrgYPO.setAllCount(v.size());
                    /*2、2、日均有效接入监测点数2、日均有效接入监测点数*/
                    rOperatingMonitorDPOQueryWrapper.clear();
                    rOperatingMonitorDPOQueryWrapper.select("measurement_point_id").
                            in("measurement_point_id",collect1).
                            gt("effective_minute_count",0).
                            between("data_date",begin,end);
                    List<ROperatingMonitorDPO> rOperatingMonitorDPOS = rOperatingMonitorDMapper.selectList(rOperatingMonitorDPOQueryWrapper);
                    String value = df.format((float)rOperatingMonitorDPOS.size()/ days);

                    rStatOrgYPO.setEffectiveMeasurementAverage(Double.valueOf(value));
                    /*3、3、累计有效接入监测点数*/
                    long count = rOperatingMonitorDPOS.stream().map(ROperatingMonitorDPO::getMeasurementPointId).distinct().count();
                    rStatOrgYPO.setEffectiveMeasurementAccrued(Integer.valueOf(count+""));

                    /*4、日均稳态超标监测点数*/
                    rmpTargetWarnDPOQueryWrapper.clear();
                    rmpTargetWarnDPOQueryWrapper.select("measurement_point_id").
                            in("measurement_point_id",collect1).
                            eq("is_harmonic",1).
                            between("data_date",begin,end);
                    List<RMpTargetWarnDPO> rMpTargetWarnDPOS = rMpTargetWarnDMapper.selectList(rmpTargetWarnDPOQueryWrapper);
                    String value1 = df.format((float)rMpTargetWarnDPOS.size()/ days);
                    rStatOrgYPO.setOverLimitMeasurementAverage(Double.parseDouble(value1));

                    /* 5、累计稳态超标监测点数*/
                    long count1 = rMpTargetWarnDPOS.stream().map(RMpTargetWarnDPO::getMeasurementPointId).distinct().count();

                    rStatOrgYPO.setOverLimitMeasurementAccrued(Integer.valueOf(count1+""));

                    /*6、日均稳态超标监测点数占比*/
                    String value2 = df.format(rStatOrgYPO.getOverLimitMeasurementAccrued()*100.0 / rStatOrgYPO.getAllCount());
                    rStatOrgYPO.setHarmonicMeasurementRatioAverage(Double.parseDouble(value2));

                    /*7、稳态指标平均超标天数*/
                    if(count1==0){
                        rStatOrgYPO.setAverageOverDay(0.00);

                    }else {
                        String format = df.format((float)rMpTargetWarnDPOS.size()/ count1);

                        rStatOrgYPO.setAverageOverDay(Double.parseDouble(format));
                    }

                    /*8、频率偏差合格率,9、电压偏差合格率,10、闪变合格率*/
                    rMpPassRateYPOQueryWrapper.clear();
                    rMpPassRateYPOQueryWrapper.select("avg(freq_pass_rate) as freq_pass_rate","avg(v_dev_pass_rate) as v_dev_pass_rate","avg(flicker_pass_rate) as flicker_pass_rate").
                            in("measurement_point_id",collect1).
                            between("data_date",begin,end);
                    RMpPassRateYPO rMpPassRateYPO = rMpPassRateYMapper.selectOne(rMpPassRateYPOQueryWrapper);
                    rStatOrgYPO.setFreqPassRate(rMpPassRateYPO.getFreqPassRate());
                    rStatOrgYPO.setFlickerPassRate(rMpPassRateYPO.getFlickerPassRate());
                    rStatOrgYPO.setVDevPassRate(rMpPassRateYPO.getVDevPassRate());



                    /*11、日均监测到暂态指标的监测点数*/
                    rMpEventDetailDPOQueryWrapper.clear();
                    rMpEventDetailDPOQueryWrapper.
                            in("measurement_point_id", collect1).
                            and(i-> i.gt("swell_times",0).or().gt("sag_times",0).or().gt("interrupt_times",0)).
                            between("data_date",begin,end);
                    List<RMpEventDetailD> rMpEventDetailDS = rMpEventDetailDMapper.selectList(rMpEventDetailDPOQueryWrapper);
                    Integer eventCount =0;
                    int sum = rMpEventDetailDS.stream().mapToInt(RMpEventDetailD::getInterruptTimes).sum();
                    int sum1 = rMpEventDetailDS.stream().mapToInt(RMpEventDetailD::getSagTimes).sum();
                    int sum2 = rMpEventDetailDS.stream().mapToInt(RMpEventDetailD::getSwellTimes).sum();
                    String value3 = df.format((float) rMpEventDetailDS.size()/ days);
                    rStatOrgYPO.setEventMeasurementAverage(Double.valueOf(value3));
                    /*12、累计监测到暂态指标的监测点数*/
                    long count2 = rMpEventDetailDS.stream().map(RMpEventDetailD::getMeasurementPointId).distinct().count();

                    rStatOrgYPO.setEventMeasurementAccrued(Integer.valueOf(count2+""));
                    /*13、日均暂态超标监测点数占比*/
                    String value4 = df.format(rStatOrgYPO.getEventMeasurementAverage()*100.0 / rStatOrgYPO.getAllCount());
                    rStatOrgYPO.setEventMeasurementRatioAverage(Double.valueOf(value4));

                    /*15、暂态指标发生频次*/
                    rStatOrgYPO.setEventCount(sum+sum1+sum2);

                    /*14、暂态指标发生频次*/

                    long count3 = rMpEventDetailDS.stream().map(RMpEventDetailD::getMeasurementPointId).distinct().count();
                    if(count3==0){
                        rStatOrgYPO.setEventFreq(0.00);

                    }else {
                        String value5 = df.format((float)rStatOrgYPO.getEventCount() / count3);

                        rStatOrgYPO.setEventFreq(Double.valueOf(value5));
                    }




                    /*todo这个指标不重要，后续根据配置来*/
                    /*17、告警监测点数*/
                    rStatOrgYPO.setShouldCount(8);
                    rOperatingMonitorDPOQueryWrapper.clear();
                    rOperatingMonitorDPOQueryWrapper.select("distinct(measurement_point_id)").
                            in("measurement_point_id",collect1).
                            eq("is_unusual",1).
                            between("data_date",begin,end);
                    Integer integer2 = rOperatingMonitorDMapper.selectCount(rOperatingMonitorDPOQueryWrapper);
                    rStatOrgYPO.setWarnCount(integer2);

                    rStatOrgYList.add(rStatOrgYPO);
                });

            }

        };
        if (!CollectionUtils.isEmpty(rStatOrgYList)){
            rStatOrgYService.saveOrUpdateBatchByMultiId(rStatOrgYList,500);
        }
    }


    private String getmeasurementType(Integer k, Map<String, DictData> lineSortMap) {
        String value ="";
        switch (k) {
            case 1:
                value =lineSortMap.get(DicDataEnum.ONE_LINE.getCode()).getId();
                break;
            case 2:
                value =lineSortMap.get(DicDataEnum.TWO_LINE.getCode()).getId();
                break;
            case 3:
                value =lineSortMap.get(DicDataEnum.THREE_LINE.getCode()).getId();
                break;
            default:
                break;
        }
        return value;
    }

}
