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

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.njcn.device.biz.pojo.dto.DeptGetChildrenMoreDTO;
import com.njcn.device.biz.pojo.dto.LineDevGetDTO;
import com.njcn.harmonic.pojo.po.ROperatingIndexDPO;
import com.njcn.harmonic.pojo.po.ROperatingIndexMPO;
import com.njcn.harmonic.pojo.po.ROperatingIndexQPO;
import com.njcn.harmonic.pojo.po.ROperatingIndexYPO;
import com.njcn.prepare.harmonic.pojo.bo.CalculatedParam;
import com.njcn.prepare.harmonic.pojo.mysql.ROperatingMonitorDPO;
import com.njcn.prepare.harmonic.pojo.mysql.ROperatingMonitorMPO;
import com.njcn.prepare.harmonic.pojo.po.RDnOperatingIndexMPO;
import com.njcn.prepare.harmonic.pojo.po.RDnOperatingQPO;
import com.njcn.prepare.harmonic.pojo.po.RDnOperatingYPO;
import com.njcn.prepare.harmonic.service.mysql.area.ROperatingIndexDPOService;
import com.njcn.prepare.harmonic.service.mysql.area.ROperatingIndexMPOService;
import com.njcn.prepare.harmonic.service.mysql.area.ROperatingIndexQPOService;
import com.njcn.prepare.harmonic.service.mysql.area.ROperatingIndexYPOService;
import com.njcn.prepare.harmonic.service.mysql.line.ROperatingMonitorMService;
import com.njcn.prepare.harmonic.service.mysql.line.ROperatingMonitorService;
import com.njcn.prepare.harmonic.service.mysql.newalgorithm.RDnOperatingIndexMPOService;
import com.njcn.prepare.harmonic.service.mysql.newalgorithm.RDnOperatingQPOService;
import com.njcn.prepare.harmonic.service.mysql.newalgorithm.RDnOperatingYPOService;
import com.njcn.prepare.harmonic.service.mysql.newalgorithm.ROperatingIndexService;
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.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Description:
 * Date: 2023/10/7  14:37【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@RequiredArgsConstructor
@Service
public class ROperatingIndexServiceImpl implements ROperatingIndexService {

    private final ROperatingMonitorService rOperatingMonitorDService;
    private final ROperatingMonitorMService rOperatingMonitorMService;

    private final ROperatingIndexDPOService rOperatingIndexDPOService;
    private final ROperatingIndexMPOService rOperatingIndexMPOService;
    private final ROperatingIndexQPOService rOperatingIndexQPOService;
    private final ROperatingIndexYPOService rOperatingIndexYPOService;

    private final RDnOperatingIndexMPOService rDnOperatingIndexMPOService;
    private final RDnOperatingQPOService rDnOperatingQPOService;
    private final RDnOperatingYPOService rDnOperatingYPOService;
    private final
    DicDataFeignClient dicDataFeignClient;
    private final DecimalFormat df = new DecimalFormat("0.00");


    @Override
    public void handler(List<DeptGetChildrenMoreDTO> data, String dataDate, Integer type) {
        switch (type) {
            case 1:
                this.hanlderYear (data,  dataDate,  type);
                break;
            case 2:
                this.hanlderQtr (data,  dataDate,  type);
                break;
            case 3:
                this.hanlderM ( data,  dataDate,  type);
                break;
            default:
                break;
        }
    }

    @Override
    public void handleMonth(CalculatedParam<DeptGetChildrenMoreDTO> calculatedParam) {
        List<RDnOperatingIndexMPO> rdnOperatingIndexMPOList = new ArrayList<>();
        List<ROperatingIndexMPO> rOperatingIndexMPOList = new ArrayList<>();
        QueryWrapper<ROperatingMonitorDPO> queryWrapperD = new QueryWrapper<>();
        QueryWrapper<ROperatingMonitorMPO> queryWrapperM = new QueryWrapper<>();
        DateTime begin = DateUtil.beginOfMonth(DateUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN));
        DateTime end = DateUtil.endOfMonth(DateUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN));
        LocalDate localDate = LocalDateTimeUtil.parseDate( DateUtil.format(begin, DatePattern.NORM_DATE_PATTERN), DatePattern.NORM_DATE_PATTERN);        /*获取监测点类型类型*/
        /*获取监测点类型类型*/

        List<DictData> lineSortList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.LINE_SORT.getCode()).getData();
        Map<String, DictData> lineSortMap = lineSortList.stream().collect(Collectors.toMap(DictData::getCode, dictData -> dictData));


        for (DeptGetChildrenMoreDTO deptGetChildrenMoreDTO : calculatedParam.getIdList()) {
            if (!CollectionUtils.isEmpty(deptGetChildrenMoreDTO.getLineBaseList())) {
                List<String> collect = deptGetChildrenMoreDTO.getLineBaseList().stream().map(LineDevGetDTO::getPointId).collect(Collectors.toList());
                queryWrapperD.clear();
                queryWrapperM.clear();
                queryWrapperD.in("measurement_point_id", collect).
                        between("data_date",begin,end);

                queryWrapperM.in("measurement_point_id", collect).
                        between("data_date",begin,end);

                List<ROperatingMonitorDPO> rOperatingMonitorDPOList = rOperatingMonitorDService.list(queryWrapperD);
                List<ROperatingMonitorMPO> roperatingMonitorMPOList = rOperatingMonitorMService.list(queryWrapperM);
                ROperatingIndexMPO rOperatingIndexMPO = new ROperatingIndexMPO();
                rOperatingIndexMPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                rOperatingIndexMPO.setDataDate(localDate);
                List<String> collect1 = rOperatingMonitorDPOList.stream().filter(temp -> temp.getEffectiveMinuteCount() > 1).
                        map(ROperatingMonitorDPO::getMeasurementPointId).distinct().collect(Collectors.toList());

                List<ROperatingMonitorDPO> monitorList = rOperatingMonitorDPOList.stream().filter(temp -> temp.getEffectiveMinuteCount() > 1).collect(Collectors.toList());

                List<String> collect2 = rOperatingMonitorDPOList.stream().filter(temp -> temp.getDataIntegrityRate().doubleValue() > 0.95 && temp.getIsUnusual() == 1).
                        map(ROperatingMonitorDPO::getMeasurementPointId).distinct().collect(Collectors.toList());

                Map<String, List<ROperatingMonitorDPO>> collect3 = rOperatingMonitorDPOList.stream().collect(Collectors.groupingBy(ROperatingMonitorDPO::getDevId));

                Map<String, List<ROperatingMonitorDPO>> collect4 = monitorList.stream().collect(Collectors.groupingBy(ROperatingMonitorDPO::getDevId));

                rOperatingIndexMPO.setEffectiveAccessMeasurementCount(collect2.size());
                rOperatingIndexMPO.setMeasurementRunPoints(collect.size());
                rOperatingIndexMPO.setOnlineMeasurementCount(collect4.size());
                rOperatingIndexMPO.setTransitMeasurementPoints(collect1.size());
                rOperatingIndexMPO.setOnlineMeasurementPoints(collect3.size());
//                rOperatingIndexMPO.setShouldCount();

                List<String> collect5 = roperatingMonitorMPOList.stream().filter(temp -> temp.getIsUnusual() == 0).map(ROperatingMonitorMPO::getMeasurementPointId).distinct().collect(Collectors.toList());


                double v = roperatingMonitorMPOList.stream().mapToDouble(temp -> {
                    return temp.getIndexIntegrityRate().doubleValue();
                }).average().orElse(0.0);
                double v1 = roperatingMonitorMPOList.stream().mapToDouble(temp -> {
                    return temp.getDataRightRate().doubleValue();
                }).average().orElse(0.0);
                double v2 = roperatingMonitorMPOList.stream().mapToDouble(temp -> {
                    return temp.getDataIntegrityRate().doubleValue();
                }).average().orElse(0.0);
                rOperatingIndexMPO.setIndexIntegrityRate(v);
                rOperatingIndexMPO.setDataIntegrityRate(v2);
                rOperatingIndexMPO.setIsUnusual(collect5.size());
                rOperatingIndexMPO.setDataRightRate(v1);
                rOperatingIndexMPOList.add(rOperatingIndexMPO);

            }
            if (!CollectionUtils.isEmpty(deptGetChildrenMoreDTO.getPwMonitorIds())) {
                if (!CollectionUtils.isEmpty(deptGetChildrenMoreDTO.getPwMonitorIds())) {
                    Map<Integer, List<LineDevGetDTO>> collect3 = deptGetChildrenMoreDTO.getPwMonitorIds().stream().collect(Collectors.groupingBy(LineDevGetDTO::getLineType));

                    collect3.forEach((k,v)->{
                        List<String> collect = v.stream().map(LineDevGetDTO::getPointId).collect(Collectors.toList());
                        queryWrapperD.clear();
                        queryWrapperM.clear();
                        queryWrapperD.in("measurement_point_id", collect).
                                between("data_date",begin,end);

                        queryWrapperM.in("measurement_point_id", collect).
                                between("data_date",begin,end);

                        List<ROperatingMonitorDPO> rOperatingMonitorDPOList = rOperatingMonitorDService.list(queryWrapperD);
                        List<ROperatingMonitorMPO> roperatingMonitorMPOList = rOperatingMonitorMService.list(queryWrapperM);
                        RDnOperatingIndexMPO rDnOperatingIndexMPO = new RDnOperatingIndexMPO();
                        rDnOperatingIndexMPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                        rDnOperatingIndexMPO.setDataDate(localDate);
                        List<String> collect1 = rOperatingMonitorDPOList.stream().filter(temp -> temp.getEffectiveMinuteCount() > 1).
                                map(ROperatingMonitorDPO::getMeasurementPointId).distinct().collect(Collectors.toList());

                        List<ROperatingMonitorDPO> monitorList = rOperatingMonitorDPOList.stream().filter(temp -> temp.getEffectiveMinuteCount() > 1).collect(Collectors.toList());

                        List<String> collect2 = rOperatingMonitorDPOList.stream().filter(temp -> temp.getDataIntegrityRate().doubleValue() > 0.95 && temp.getIsUnusual() == 1).
                                map(ROperatingMonitorDPO::getMeasurementPointId).distinct().collect(Collectors.toList());


                        rDnOperatingIndexMPO.setEffectiveAccessMeasurementCount(collect2.size());
                        rDnOperatingIndexMPO.setMeasurementRunPoints(collect.size());
                        rDnOperatingIndexMPO.setTransitMeasurementPoints(collect1.size());
                        if (collect.size()==0){
                            rDnOperatingIndexMPO.setEffectiveAccessRate(0.00);

                        }
                        else {
                            double rate = Double.parseDouble(df.format((collect2.size()) / Double.valueOf(collect.size())));
                            rDnOperatingIndexMPO.setEffectiveAccessRate(rate);

                        }

                        List<String> collect5 = roperatingMonitorMPOList.stream().filter(temp -> temp.getIsUnusual() == 0).map(ROperatingMonitorMPO::getMeasurementPointId).distinct().collect(Collectors.toList());


                        double integrity = roperatingMonitorMPOList.stream().mapToDouble(temp -> {
                            return temp.getIndexIntegrityRate().doubleValue();
                        }).average().orElse(0.0);
                        double v1 = roperatingMonitorMPOList.stream().mapToDouble(temp -> {
                            return temp.getDataRightRate().doubleValue();
                        }).average().orElse(0.0);
                        double v2 = roperatingMonitorMPOList.stream().mapToDouble(temp -> {
                            return temp.getDataIntegrityRate().doubleValue();
                        }).average().orElse(0.0);
                        rDnOperatingIndexMPO.setMeasurementPointType(getmeasurementType(k, lineSortMap));
                        rDnOperatingIndexMPO.setIndexIntegrityRate(integrity);
                        rDnOperatingIndexMPO.setDataIntegrityRate(v2);
                        rDnOperatingIndexMPO.setIsUnusual(collect5.size());
                        rDnOperatingIndexMPO.setDataRightRate(v1);
                        rdnOperatingIndexMPOList.add(rDnOperatingIndexMPO);
                    });

                }

            }
        }

        if (!CollectionUtils.isEmpty(rdnOperatingIndexMPOList)){
            rDnOperatingIndexMPOService.saveOrUpdateBatchByMultiId(rdnOperatingIndexMPOList);
        }
        if (!CollectionUtils.isEmpty(rOperatingIndexMPOList)){
            rOperatingIndexMPOService.saveOrUpdateBatchByMultiId(rOperatingIndexMPOList);

        }
    }

    @Override
    public void handleQtr(CalculatedParam<DeptGetChildrenMoreDTO> calculatedParam) {
        List<RDnOperatingQPO> rdnOperatingIndexQPOList = new ArrayList<>();
        List<ROperatingIndexQPO> rOperatingIndexQPOList = new ArrayList<>();
        QueryWrapper<ROperatingMonitorDPO> queryWrapperD = new QueryWrapper<>();
        QueryWrapper<ROperatingMonitorMPO> queryWrapperM = new QueryWrapper<>();
        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);        /*获取监测点类型类型*/

        /*获取监测点类型类型*/

        List<DictData> lineSortList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.LINE_SORT.getCode()).getData();
        Map<String, DictData> lineSortMap = lineSortList.stream().collect(Collectors.toMap(DictData::getCode, dictData -> dictData));

        for (DeptGetChildrenMoreDTO deptGetChildrenMoreDTO : calculatedParam.getIdList()) {
            if (!CollectionUtils.isEmpty(deptGetChildrenMoreDTO.getLineBaseList())) {
                List<String> collect = deptGetChildrenMoreDTO.getLineBaseList().stream().map(LineDevGetDTO::getPointId).collect(Collectors.toList());
                queryWrapperD.clear();
                queryWrapperM.clear();
                queryWrapperD.in("measurement_point_id", collect).
                        between("data_date",begin,end);
                queryWrapperM.in("measurement_point_id", collect).
                        between("data_date",begin,end);
                List<ROperatingMonitorDPO> rOperatingMonitorDPOList = rOperatingMonitorDService.list(queryWrapperD);
                List<ROperatingMonitorMPO> roperatingMonitorMPOList = rOperatingMonitorMService.list(queryWrapperM);
                ROperatingIndexQPO rOperatingIndexQPO = new ROperatingIndexQPO();
                rOperatingIndexQPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                rOperatingIndexQPO.setDataDate(localDate);
                List<String> collect1 = rOperatingMonitorDPOList.stream().filter(temp -> temp.getEffectiveMinuteCount() > 1).
                        map(ROperatingMonitorDPO::getMeasurementPointId).distinct().collect(Collectors.toList());

                List<ROperatingMonitorDPO> monitorList = rOperatingMonitorDPOList.stream().filter(temp -> temp.getEffectiveMinuteCount() > 1).collect(Collectors.toList());

                List<String> collect2 = rOperatingMonitorDPOList.stream().filter(temp -> temp.getDataIntegrityRate().doubleValue() > 0.95 && temp.getIsUnusual() == 1).
                        map(ROperatingMonitorDPO::getMeasurementPointId).distinct().collect(Collectors.toList());

                Map<String, List<ROperatingMonitorDPO>> collect3 = rOperatingMonitorDPOList.stream().collect(Collectors.groupingBy(ROperatingMonitorDPO::getDevId));

                Map<String, List<ROperatingMonitorDPO>> collect4 = monitorList.stream().collect(Collectors.groupingBy(ROperatingMonitorDPO::getDevId));

                rOperatingIndexQPO.setEffectiveAccessMeasurementCount(collect2.size());
                rOperatingIndexQPO.setMeasurementRunPoints(collect.size());
                rOperatingIndexQPO.setOnlineMeasurementCount(collect4.size());
                rOperatingIndexQPO.setTransitMeasurementPoints(collect1.size());
                rOperatingIndexQPO.setOnlineMeasurementPoints(collect3.size());
//                rOperatingIndexMPO.setShouldCount();

                List<String> collect5 = roperatingMonitorMPOList.stream().filter(temp -> temp.getIsUnusual() == 0).map(ROperatingMonitorMPO::getMeasurementPointId).distinct().collect(Collectors.toList());


                double v = roperatingMonitorMPOList.stream().mapToDouble(temp -> {
                    return temp.getIndexIntegrityRate().doubleValue();
                }).average().orElse(0.0);
                double v1 = roperatingMonitorMPOList.stream().mapToDouble(temp -> {
                    return temp.getDataRightRate().doubleValue();
                }).average().orElse(0.0);
                double v2 = roperatingMonitorMPOList.stream().mapToDouble(temp -> {
                    return temp.getDataIntegrityRate().doubleValue();
                }).average().orElse(0.0);
                rOperatingIndexQPO.setIndexIntegrityRate(v);
                rOperatingIndexQPO.setDataIntegrityRate(v2);
                rOperatingIndexQPO.setIsUnusual(collect5.size());
                rOperatingIndexQPO.setDataRightRate(v1);
                rOperatingIndexQPOList.add(rOperatingIndexQPO);

            }
            if (!CollectionUtils.isEmpty(deptGetChildrenMoreDTO.getPwMonitorIds())) {
                if (!CollectionUtils.isEmpty(deptGetChildrenMoreDTO.getPwMonitorIds())) {
                    Map<Integer, List<LineDevGetDTO>> collect3 = deptGetChildrenMoreDTO.getPwMonitorIds().stream().collect(Collectors.groupingBy(LineDevGetDTO::getLineType));
                    collect3.forEach((k,m)->{
                        List<String> collect = m.stream().map(LineDevGetDTO::getPointId).collect(Collectors.toList());
                        queryWrapperD.clear();
                        queryWrapperM.clear();
                        queryWrapperD.in("measurement_point_id", collect).
                                between("data_date",begin,end);

                        queryWrapperM.in("measurement_point_id", collect).
                                between("data_date",begin,end);

                        List<ROperatingMonitorDPO> rOperatingMonitorDPOList = rOperatingMonitorDService.list(queryWrapperD);
                        List<ROperatingMonitorMPO> roperatingMonitorMPOList = rOperatingMonitorMService.list(queryWrapperM);
                        RDnOperatingQPO rDnOperatingQPO = new RDnOperatingQPO();
                        rDnOperatingQPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                        rDnOperatingQPO.setDataDate(localDate);
                        List<String> collect1 = rOperatingMonitorDPOList.stream().filter(temp -> temp.getEffectiveMinuteCount() > 1).
                                map(ROperatingMonitorDPO::getMeasurementPointId).distinct().collect(Collectors.toList());

                        List<ROperatingMonitorDPO> monitorList = rOperatingMonitorDPOList.stream().filter(temp -> temp.getEffectiveMinuteCount() > 1).collect(Collectors.toList());

                        List<String> collect2 = rOperatingMonitorDPOList.stream().filter(temp -> temp.getDataIntegrityRate().doubleValue() > 0.95 && temp.getIsUnusual() == 1).
                                map(ROperatingMonitorDPO::getMeasurementPointId).distinct().collect(Collectors.toList());


                        rDnOperatingQPO.setEffectiveAccessMeasurementCount(collect2.size());
                        rDnOperatingQPO.setMeasurementRunPoints(collect.size());
                        rDnOperatingQPO.setTransitMeasurementPoints(collect1.size());
                        if (collect.size()==0){
                            rDnOperatingQPO.setEffectiveAccessRate(0.00);

                        }
                        else {
                            double v = Double.parseDouble(df.format((collect2.size()) / Double.valueOf(collect.size())));
                            rDnOperatingQPO.setEffectiveAccessRate(v);

                        }
//                rOperatingIndexMPO.setShouldCount();

                        List<String> collect5 = roperatingMonitorMPOList.stream().filter(temp -> temp.getIsUnusual() == 0).map(ROperatingMonitorMPO::getMeasurementPointId).distinct().collect(Collectors.toList());


                        double v = roperatingMonitorMPOList.stream().mapToDouble(temp -> {
                            return temp.getIndexIntegrityRate().doubleValue();
                        }).average().orElse(0.0);
                        double v1 = roperatingMonitorMPOList.stream().mapToDouble(temp -> {
                            return temp.getDataRightRate().doubleValue();
                        }).average().orElse(0.0);
                        double v2 = roperatingMonitorMPOList.stream().mapToDouble(temp -> {
                            return temp.getDataIntegrityRate().doubleValue();
                        }).average().orElse(0.0);
                        rDnOperatingQPO.setMeasurementPointType(getmeasurementType(k, lineSortMap));
                        rDnOperatingQPO.setIndexIntegrityRate(v);
                        rDnOperatingQPO.setDataIntegrityRate(v2);
                        rDnOperatingQPO.setIsUnusual(collect5.size());
                        rDnOperatingQPO.setDataRightRate(v1);
                        rdnOperatingIndexQPOList.add(rDnOperatingQPO);
                    });

                }

            }
        }

        if (!CollectionUtils.isEmpty(rdnOperatingIndexQPOList)){
            rDnOperatingQPOService.saveOrUpdateBatchByMultiId(rdnOperatingIndexQPOList);
        }
        if (!CollectionUtils.isEmpty(rOperatingIndexQPOList)){
            rOperatingIndexQPOService.saveOrUpdateBatchByMultiId(rOperatingIndexQPOList);

        }
    }

    @Override
    public void handleYear(CalculatedParam<DeptGetChildrenMoreDTO> calculatedParam) {
        List<RDnOperatingYPO> rdnOperatingIndexYPOList = new ArrayList<>();
        List<ROperatingIndexYPO> rOperatingIndexYPOList = new ArrayList<>();
        QueryWrapper<ROperatingMonitorDPO> queryWrapperD = new QueryWrapper<>();
        QueryWrapper<ROperatingMonitorMPO> queryWrapperM = new QueryWrapper<>();
        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);        /*获取监测点类型类型*/


        /*获取监测点类型类型*/

        List<DictData> lineSortList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.LINE_SORT.getCode()).getData();
        Map<String, DictData> lineSortMap = lineSortList.stream().collect(Collectors.toMap(DictData::getCode, dictData -> dictData));

        for (DeptGetChildrenMoreDTO deptGetChildrenMoreDTO : calculatedParam.getIdList()) {
            if (!CollectionUtils.isEmpty(deptGetChildrenMoreDTO.getLineBaseList())) {
                List<String> collect = deptGetChildrenMoreDTO.getLineBaseList().stream().map(LineDevGetDTO::getPointId).collect(Collectors.toList());
                queryWrapperD.clear();
                queryWrapperM.clear();
                queryWrapperD.in("measurement_point_id", collect).
                        between("data_date",begin,end);
                queryWrapperM.in("measurement_point_id", collect).
                        between("data_date",begin,end);
                List<ROperatingMonitorDPO> rOperatingMonitorDPOList = rOperatingMonitorDService.list(queryWrapperD);
                List<ROperatingMonitorMPO> roperatingMonitorMPOList = rOperatingMonitorMService.list(queryWrapperM);
                ROperatingIndexYPO rOperatingIndexYPO = new ROperatingIndexYPO();
                rOperatingIndexYPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                rOperatingIndexYPO.setDataDate(localDate);
                List<String> collect1 = rOperatingMonitorDPOList.stream().filter(temp -> temp.getEffectiveMinuteCount() > 1).
                        map(ROperatingMonitorDPO::getMeasurementPointId).distinct().collect(Collectors.toList());

                List<ROperatingMonitorDPO> monitorList = rOperatingMonitorDPOList.stream().filter(temp -> temp.getEffectiveMinuteCount() > 1).collect(Collectors.toList());

                List<String> collect2 = rOperatingMonitorDPOList.stream().filter(temp -> temp.getDataIntegrityRate().doubleValue() > 0.95 && temp.getIsUnusual() == 1).
                        map(ROperatingMonitorDPO::getMeasurementPointId).distinct().collect(Collectors.toList());

                Map<String, List<ROperatingMonitorDPO>> collect3 = rOperatingMonitorDPOList.stream().collect(Collectors.groupingBy(ROperatingMonitorDPO::getDevId));

                Map<String, List<ROperatingMonitorDPO>> collect4 = monitorList.stream().collect(Collectors.groupingBy(ROperatingMonitorDPO::getDevId));

                rOperatingIndexYPO.setEffectiveAccessMeasurementCount(collect2.size());
                rOperatingIndexYPO.setMeasurementRunPoints(collect.size());
                rOperatingIndexYPO.setOnlineMeasurementCount(collect4.size());
                rOperatingIndexYPO.setTransitMeasurementPoints(collect1.size());
                rOperatingIndexYPO.setOnlineMeasurementPoints(collect3.size());
//                rOperatingIndexMPO.setShouldCount();

                List<String> collect5 = roperatingMonitorMPOList.stream().filter(temp -> temp.getIsUnusual() == 0).map(ROperatingMonitorMPO::getMeasurementPointId).distinct().collect(Collectors.toList());


                double v = roperatingMonitorMPOList.stream().mapToDouble(temp -> {
                    return temp.getIndexIntegrityRate().doubleValue();
                }).average().orElse(0.0);
                double v1 = roperatingMonitorMPOList.stream().mapToDouble(temp -> {
                    return temp.getDataRightRate().doubleValue();
                }).average().orElse(0.0);
                double v2 = roperatingMonitorMPOList.stream().mapToDouble(temp -> {
                    return temp.getDataIntegrityRate().doubleValue();
                }).average().orElse(0.0);
                rOperatingIndexYPO.setIndexIntegrityRate(v);
                rOperatingIndexYPO.setDataIntegrityRate(v2);
                rOperatingIndexYPO.setIsUnusual(collect5.size());
                rOperatingIndexYPO.setDataRightRate(v1);
                rOperatingIndexYPOList.add(rOperatingIndexYPO);

            }
            if (!CollectionUtils.isEmpty(deptGetChildrenMoreDTO.getPwMonitorIds())) {
                if (!CollectionUtils.isEmpty(deptGetChildrenMoreDTO.getPwMonitorIds())) {
                    Map<Integer, List<LineDevGetDTO>> collect3 = deptGetChildrenMoreDTO.getPwMonitorIds().stream().collect(Collectors.groupingBy(LineDevGetDTO::getLineType));
                    collect3.forEach((k,m)->{
                        List<String> collect = m.stream().map(LineDevGetDTO::getPointId).collect(Collectors.toList());
                        queryWrapperD.clear();
                        queryWrapperM.clear();
                        queryWrapperD.in("measurement_point_id", collect).
                                between("data_date",begin,end);

                        queryWrapperM.in("measurement_point_id", collect).
                                between("data_date",begin,end);

                        List<ROperatingMonitorDPO> rOperatingMonitorDPOList = rOperatingMonitorDService.list(queryWrapperD);
                        List<ROperatingMonitorMPO> roperatingMonitorMPOList = rOperatingMonitorMService.list(queryWrapperM);
                        RDnOperatingYPO rDnOperatingYPO = new RDnOperatingYPO();
                        rDnOperatingYPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                        rDnOperatingYPO.setDataDate(localDate);
                        List<String> collect1 = rOperatingMonitorDPOList.stream().filter(temp -> temp.getEffectiveMinuteCount() > 1).
                                map(ROperatingMonitorDPO::getMeasurementPointId).distinct().collect(Collectors.toList());

                        List<ROperatingMonitorDPO> monitorList = rOperatingMonitorDPOList.stream().filter(temp -> temp.getEffectiveMinuteCount() > 1).collect(Collectors.toList());

                        List<String> collect2 = rOperatingMonitorDPOList.stream().filter(temp -> temp.getDataIntegrityRate().doubleValue() > 0.95 && temp.getIsUnusual() == 1).
                                map(ROperatingMonitorDPO::getMeasurementPointId).distinct().collect(Collectors.toList());


                        rDnOperatingYPO.setEffectiveAccessMeasurementCount(collect2.size());
                        rDnOperatingYPO.setMeasurementRunPoints(collect.size());
                        rDnOperatingYPO.setTransitMeasurementPoints(collect1.size());
                        if (collect.size()==0){
                            rDnOperatingYPO.setEffectiveAccessRate(0.00);

                        }
                        else {
                            double v = Double.parseDouble(df.format((collect2.size()) / Double.valueOf(collect.size())));
                            rDnOperatingYPO.setEffectiveAccessRate(v);

                        }
//                rOperatingIndexMPO.setShouldCount();

                        List<String> collect5 = roperatingMonitorMPOList.stream().filter(temp -> temp.getIsUnusual() == 0).map(ROperatingMonitorMPO::getMeasurementPointId).distinct().collect(Collectors.toList());


                        double v = roperatingMonitorMPOList.stream().mapToDouble(temp -> {
                            return temp.getIndexIntegrityRate().doubleValue();
                        }).average().orElse(0.0);
                        double v1 = roperatingMonitorMPOList.stream().mapToDouble(temp -> {
                            return temp.getDataRightRate().doubleValue();
                        }).average().orElse(0.0);
                        double v2 = roperatingMonitorMPOList.stream().mapToDouble(temp -> {
                            return temp.getDataIntegrityRate().doubleValue();
                        }).average().orElse(0.0);
                        rDnOperatingYPO.setMeasurementPointType(getmeasurementType(k, lineSortMap));
                        rDnOperatingYPO.setIndexIntegrityRate(v);
                        rDnOperatingYPO.setDataIntegrityRate(v2);
                        rDnOperatingYPO.setIsUnusual(collect5.size());
                        rDnOperatingYPO.setDataRightRate(v1);
                        rdnOperatingIndexYPOList.add(rDnOperatingYPO);
                    });

                }

            }
        }

        if (!CollectionUtils.isEmpty(rdnOperatingIndexYPOList)){
            rDnOperatingYPOService.saveOrUpdateBatchByMultiId(rdnOperatingIndexYPOList);
        }
        if (!CollectionUtils.isEmpty(rOperatingIndexYPOList)){
            rOperatingIndexYPOService.saveOrUpdateBatchByMultiId(rOperatingIndexYPOList);

        }
    }

    @Override
    public void handleDay(CalculatedParam<DeptGetChildrenMoreDTO> calculatedParam) {
        List<ROperatingIndexDPO> rdnOperatingIndexDPOList = new ArrayList<>();
        QueryWrapper<ROperatingMonitorDPO> queryWrapperD = new QueryWrapper<>();
        DateTime begin = DateUtil.beginOfDay(DateUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN));
        DateTime end = DateUtil.endOfDay(DateUtil.parse(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN));
        LocalDate localDate = LocalDateTimeUtil.parseDate( DateUtil.format(begin, DatePattern.NORM_DATE_PATTERN), DatePattern.NORM_DATE_PATTERN);        /*获取监测点类型类型*/
        /*获取监测点类型类型*/

        List<DictData> lineSortList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.LINE_SORT.getCode()).getData();
        Map<String, DictData> lineSortMap = lineSortList.stream().collect(Collectors.toMap(DictData::getCode, dictData -> dictData));


        for (DeptGetChildrenMoreDTO deptGetChildrenMoreDTO : calculatedParam.getIdList()) {
            if (!CollectionUtils.isEmpty(deptGetChildrenMoreDTO.getLineBaseList())) {
                List<String> collect = deptGetChildrenMoreDTO.getLineBaseList().stream().map(LineDevGetDTO::getPointId).collect(Collectors.toList());
                queryWrapperD.clear();
                queryWrapperD.in("measurement_point_id", collect).
                        between("data_date",begin,end);


                List<ROperatingMonitorDPO> rOperatingMonitorDPOList = rOperatingMonitorDService.list(queryWrapperD);
                ROperatingIndexDPO rOperatingIndexDPO = new ROperatingIndexDPO();
                rOperatingIndexDPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                rOperatingIndexDPO.setDataDate(localDate);
                List<String> collect1 = rOperatingMonitorDPOList.stream().filter(temp -> temp.getEffectiveMinuteCount() > 1).
                        map(ROperatingMonitorDPO::getMeasurementPointId).distinct().collect(Collectors.toList());

                List<ROperatingMonitorDPO> monitorList = rOperatingMonitorDPOList.stream().filter(temp -> temp.getEffectiveMinuteCount() > 1).collect(Collectors.toList());

                List<String> collect2 = rOperatingMonitorDPOList.stream().filter(temp -> temp.getDataIntegrityRate().doubleValue() > 0.95 && temp.getIsUnusual() == 1).
                        map(ROperatingMonitorDPO::getMeasurementPointId).distinct().collect(Collectors.toList());

                Map<String, List<ROperatingMonitorDPO>> collect3 = rOperatingMonitorDPOList.stream().collect(Collectors.groupingBy(ROperatingMonitorDPO::getDevId));

                Map<String, List<ROperatingMonitorDPO>> collect4 = monitorList.stream().collect(Collectors.groupingBy(ROperatingMonitorDPO::getDevId));

                rOperatingIndexDPO.setEffectiveAccessMeasurementCount(collect2.size());
                rOperatingIndexDPO.setMeasurementRunPoints(collect.size());
                rOperatingIndexDPO.setOnlineMeasurementCount(collect4.size());
                rOperatingIndexDPO.setTransitMeasurementPoints(collect1.size());
                rOperatingIndexDPO.setOnlineMeasurementPoints(collect3.size());
//                rOperatingIndexMPO.setShouldCount();

                List<String> collect5 = rOperatingMonitorDPOList.stream().filter(temp -> temp.getIsUnusual() == 0).map(ROperatingMonitorDPO::getMeasurementPointId).distinct().collect(Collectors.toList());


                double v = rOperatingMonitorDPOList.stream().mapToDouble(temp -> {
                    return temp.getIndexIntegrityRate().doubleValue();
                }).average().orElse(0.0);
                double v1 = rOperatingMonitorDPOList.stream().mapToDouble(temp -> {
                    return temp.getDataRightRate().doubleValue();
                }).average().orElse(0.0);
                double v2 = rOperatingMonitorDPOList.stream().mapToDouble(temp -> {
                    return temp.getDataIntegrityRate().doubleValue();
                }).average().orElse(0.0);
                rOperatingIndexDPO.setIndexIntegrityRate(v);
                rOperatingIndexDPO.setDataIntegrityRate(v2);
                rOperatingIndexDPO.setIsUnusual(collect5.size());
                rOperatingIndexDPO.setDataRightRate(v1);
                rdnOperatingIndexDPOList.add(rOperatingIndexDPO);

            }
//            if (!CollectionUtils.isEmpty(deptGetChildrenMoreDTO.getPwMonitorIds())) {
//                if (!CollectionUtils.isEmpty(deptGetChildrenMoreDTO.getPwMonitorIds())) {
//                    Map<Integer, List<LineDevGetDTO>> collect3 = deptGetChildrenMoreDTO.getPwMonitorIds().stream().collect(Collectors.groupingBy(LineDevGetDTO::getLineType));
//
//                    collect3.forEach((k,v)->{
//                        List<String> collect = v.stream().map(LineDevGetDTO::getPointId).collect(Collectors.toList());
//                        queryWrapperD.clear();
//                        queryWrapperD.in("measurement_point_id", collect).
//                                between("data_date",begin,end);
//
//
//                        List<ROperatingMonitorDPO> rOperatingMonitorDPOList = rOperatingMonitorDService.list(queryWrapperD);
//                        ROperatingIndexDPO rOperatingIndexDPO = new ROperatingIndexDPO();
//                        rOperatingIndexDPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
//                        rOperatingIndexDPO.setDataDate(localDate);
//                        List<String> collect1 = rOperatingMonitorDPOList.stream().filter(temp -> temp.getEffectiveMinuteCount() > 1).
//                                map(ROperatingMonitorDPO::getMeasurementPointId).distinct().collect(Collectors.toList());
//
//                        List<ROperatingMonitorDPO> monitorList = rOperatingMonitorDPOList.stream().filter(temp -> temp.getEffectiveMinuteCount() > 1).collect(Collectors.toList());
//
//                        List<String> collect2 = rOperatingMonitorDPOList.stream().filter(temp -> temp.getDataIntegrityRate().doubleValue() > 0.95 && temp.getIsUnusual() == 1).
//                                map(ROperatingMonitorDPO::getMeasurementPointId).distinct().collect(Collectors.toList());
//
//
//                        rOperatingIndexDPO.setEffectiveAccessMeasurementCount(collect2.size());
//                        rOperatingIndexDPO.setMeasurementRunPoints(collect.size());
//                        rOperatingIndexDPO.setTransitMeasurementPoints(collect1.size());
//
//
//                        List<String> collect5 = rOperatingMonitorDPOList.stream().filter(temp -> temp.getIsUnusual() == 0).map(ROperatingMonitorDPO::getMeasurementPointId).distinct().collect(Collectors.toList());
//
//
//                        double integrity = rOperatingMonitorDPOList.stream().mapToDouble(temp -> {
//                            return temp.getIndexIntegrityRate().doubleValue();
//                        }).average().orElse(0.0);
//                        double v1 = rOperatingMonitorDPOList.stream().mapToDouble(temp -> {
//                            return temp.getDataRightRate().doubleValue();
//                        }).average().orElse(0.0);
//                        double v2 = rOperatingMonitorDPOList.stream().mapToDouble(temp -> {
//                            return temp.getDataIntegrityRate().doubleValue();
//                        }).average().orElse(0.0);
//                        rOperatingIndexDPO.setIndexIntegrityRate(integrity);
//                        rOperatingIndexDPO.setDataIntegrityRate(v2);
//                        rOperatingIndexDPO.setIsUnusual(collect5.size());
//                        rOperatingIndexDPO.setDataRightRate(v1);
//                        rdnOperatingIndexMPOList.add(rDnOperatingIndexMPO);
//                    });
//
//                }
//
//            }
        }


        if (!CollectionUtils.isEmpty(rdnOperatingIndexDPOList)){
            rOperatingIndexDPOService.saveOrUpdateBatchByMultiId(rdnOperatingIndexDPOList);

        }

    }

    @Deprecated
    private void hanlderYear(List<DeptGetChildrenMoreDTO> data, String dataDate, Integer type) {

        List<RDnOperatingYPO> rdnOperatingIndexYPOList = new ArrayList<>();
        List<ROperatingIndexYPO> rOperatingIndexYPOList = new ArrayList<>();
        QueryWrapper<ROperatingMonitorDPO> queryWrapperD = new QueryWrapper<>();
        QueryWrapper<ROperatingMonitorMPO> queryWrapperM = new QueryWrapper<>();
        Date date = DateUtil.parse(dataDate);
        /*获取监测点类型类型*/

        List<DictData> lineSortList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.LINE_SORT.getCode()).getData();
        Map<String, DictData> lineSortMap = lineSortList.stream().collect(Collectors.toMap(DictData::getCode, dictData -> dictData));

        for (DeptGetChildrenMoreDTO deptGetChildrenMoreDTO : data) {
            if (!CollectionUtils.isEmpty(deptGetChildrenMoreDTO.getLineBaseList())) {
                List<String> collect = deptGetChildrenMoreDTO.getLineBaseList().stream().map(LineDevGetDTO::getPointId).collect(Collectors.toList());
                queryWrapperD.clear();
                queryWrapperM.clear();
                queryWrapperD.in("measurement_point_id", collect).
                        eq("YEAR( data_date)", dataDate.substring(0,4));
                queryWrapperM.in("measurement_point_id", collect).
                        eq("YEAR( data_date)", dataDate.substring(0,4));
                List<ROperatingMonitorDPO> rOperatingMonitorDPOList = rOperatingMonitorDService.list(queryWrapperD);
                List<ROperatingMonitorMPO> roperatingMonitorMPOList = rOperatingMonitorMService.list(queryWrapperM);
                ROperatingIndexYPO rOperatingIndexYPO = new ROperatingIndexYPO();
                rOperatingIndexYPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                rOperatingIndexYPO.setDataDate(date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
                List<String> collect1 = rOperatingMonitorDPOList.stream().filter(temp -> temp.getEffectiveMinuteCount() > 1).
                        map(ROperatingMonitorDPO::getMeasurementPointId).distinct().collect(Collectors.toList());

                List<ROperatingMonitorDPO> monitorList = rOperatingMonitorDPOList.stream().filter(temp -> temp.getEffectiveMinuteCount() > 1).collect(Collectors.toList());

                List<String> collect2 = rOperatingMonitorDPOList.stream().filter(temp -> temp.getDataIntegrityRate().doubleValue() > 0.95 && temp.getIsUnusual() == 1).
                        map(ROperatingMonitorDPO::getMeasurementPointId).distinct().collect(Collectors.toList());

                Map<String, List<ROperatingMonitorDPO>> collect3 = rOperatingMonitorDPOList.stream().collect(Collectors.groupingBy(ROperatingMonitorDPO::getDevId));

                Map<String, List<ROperatingMonitorDPO>> collect4 = monitorList.stream().collect(Collectors.groupingBy(ROperatingMonitorDPO::getDevId));

                rOperatingIndexYPO.setEffectiveAccessMeasurementCount(collect2.size());
                rOperatingIndexYPO.setMeasurementRunPoints(collect.size());
                rOperatingIndexYPO.setOnlineMeasurementCount(collect4.size());
                rOperatingIndexYPO.setTransitMeasurementPoints(collect1.size());
                rOperatingIndexYPO.setOnlineMeasurementPoints(collect3.size());
//                rOperatingIndexMPO.setShouldCount();

                List<String> collect5 = roperatingMonitorMPOList.stream().filter(temp -> temp.getIsUnusual() == 0).map(ROperatingMonitorMPO::getMeasurementPointId).distinct().collect(Collectors.toList());


                double v = roperatingMonitorMPOList.stream().mapToDouble(temp -> {
                    return temp.getIndexIntegrityRate().doubleValue();
                }).average().orElse(0.0);
                double v1 = roperatingMonitorMPOList.stream().mapToDouble(temp -> {
                    return temp.getDataRightRate().doubleValue();
                }).average().orElse(0.0);
                double v2 = roperatingMonitorMPOList.stream().mapToDouble(temp -> {
                    return temp.getDataIntegrityRate().doubleValue();
                }).average().orElse(0.0);
                rOperatingIndexYPO.setIndexIntegrityRate(v);
                rOperatingIndexYPO.setDataIntegrityRate(v2);
                rOperatingIndexYPO.setIsUnusual(collect5.size());
                rOperatingIndexYPO.setDataRightRate(v1);
                rOperatingIndexYPOList.add(rOperatingIndexYPO);

            }
            if (!CollectionUtils.isEmpty(deptGetChildrenMoreDTO.getPwMonitorIds())) {
                if (!CollectionUtils.isEmpty(deptGetChildrenMoreDTO.getPwMonitorIds())) {
                    Map<Integer, List<LineDevGetDTO>> collect3 = deptGetChildrenMoreDTO.getPwMonitorIds().stream().collect(Collectors.groupingBy(LineDevGetDTO::getLineType));
                    collect3.forEach((k,m)->{
                        List<String> collect = m.stream().map(LineDevGetDTO::getPointId).collect(Collectors.toList());
                        queryWrapperD.clear();
                        queryWrapperM.clear();
                        queryWrapperD.in("measurement_point_id", collect).
                                eq("DATE_FORMAT( data_date ,'%Y-%m')", dataDate.substring(0, 7));

                        queryWrapperM.in("measurement_point_id", collect).
                                eq("DATE_FORMAT( data_date ,'%Y-%m')", dataDate.substring(0, 7));

                        List<ROperatingMonitorDPO> rOperatingMonitorDPOList = rOperatingMonitorDService.list(queryWrapperD);
                        List<ROperatingMonitorMPO> roperatingMonitorMPOList = rOperatingMonitorMService.list(queryWrapperM);
                        RDnOperatingYPO rDnOperatingYPO = new RDnOperatingYPO();
                        rDnOperatingYPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                        rDnOperatingYPO.setDataDate(date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
                        List<String> collect1 = rOperatingMonitorDPOList.stream().filter(temp -> temp.getEffectiveMinuteCount() > 1).
                                map(ROperatingMonitorDPO::getMeasurementPointId).distinct().collect(Collectors.toList());

                        List<ROperatingMonitorDPO> monitorList = rOperatingMonitorDPOList.stream().filter(temp -> temp.getEffectiveMinuteCount() > 1).collect(Collectors.toList());

                        List<String> collect2 = rOperatingMonitorDPOList.stream().filter(temp -> temp.getDataIntegrityRate().doubleValue() > 0.95 && temp.getIsUnusual() == 1).
                                map(ROperatingMonitorDPO::getMeasurementPointId).distinct().collect(Collectors.toList());


                        rDnOperatingYPO.setEffectiveAccessMeasurementCount(collect2.size());
                        rDnOperatingYPO.setMeasurementRunPoints(collect.size());
                        rDnOperatingYPO.setTransitMeasurementPoints(collect1.size());
                        if (collect.size()==0){
                            rDnOperatingYPO.setEffectiveAccessRate(0.00);

                        }
                        else {
                            double v = Double.parseDouble(df.format((collect2.size()) / Double.valueOf(collect.size())));
                            rDnOperatingYPO.setEffectiveAccessRate(v);

                        }
//                rOperatingIndexMPO.setShouldCount();

                        List<String> collect5 = roperatingMonitorMPOList.stream().filter(temp -> temp.getIsUnusual() == 0).map(ROperatingMonitorMPO::getMeasurementPointId).distinct().collect(Collectors.toList());


                        double v = roperatingMonitorMPOList.stream().mapToDouble(temp -> {
                            return temp.getIndexIntegrityRate().doubleValue();
                        }).average().orElse(0.0);
                        double v1 = roperatingMonitorMPOList.stream().mapToDouble(temp -> {
                            return temp.getDataRightRate().doubleValue();
                        }).average().orElse(0.0);
                        double v2 = roperatingMonitorMPOList.stream().mapToDouble(temp -> {
                            return temp.getDataIntegrityRate().doubleValue();
                        }).average().orElse(0.0);
                        rDnOperatingYPO.setMeasurementPointType(getmeasurementType(k, lineSortMap));
                        rDnOperatingYPO.setIndexIntegrityRate(v);
                        rDnOperatingYPO.setDataIntegrityRate(v2);
                        rDnOperatingYPO.setIsUnusual(collect5.size());
                        rDnOperatingYPO.setDataRightRate(v1);
                        rdnOperatingIndexYPOList.add(rDnOperatingYPO);
                    });

                }

            }
        }

        if (!CollectionUtils.isEmpty(rdnOperatingIndexYPOList)){
            rDnOperatingYPOService.saveOrUpdateBatchByMultiId(rdnOperatingIndexYPOList);
        }
        if (!CollectionUtils.isEmpty(rOperatingIndexYPOList)){
            rOperatingIndexYPOService.saveOrUpdateBatchByMultiId(rOperatingIndexYPOList);

        }

    }
    @Deprecated
    private void hanlderQtr(List<DeptGetChildrenMoreDTO> data, String dataDate, Integer type) {
        List<RDnOperatingQPO> rdnOperatingIndexQPOList = new ArrayList<>();
        List<ROperatingIndexQPO> rOperatingIndexQPOList = new ArrayList<>();
        QueryWrapper<ROperatingMonitorDPO> queryWrapperD = new QueryWrapper<>();
        QueryWrapper<ROperatingMonitorMPO> queryWrapperM = new QueryWrapper<>();
        Date date = DateUtil.parse(dataDate);
        /*获取监测点类型类型*/

        List<DictData> lineSortList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.LINE_SORT.getCode()).getData();
        Map<String, DictData> lineSortMap = lineSortList.stream().collect(Collectors.toMap(DictData::getCode, dictData -> dictData));

        for (DeptGetChildrenMoreDTO deptGetChildrenMoreDTO : data) {
            if (!CollectionUtils.isEmpty(deptGetChildrenMoreDTO.getLineBaseList())) {
                List<String> collect = deptGetChildrenMoreDTO.getLineBaseList().stream().map(LineDevGetDTO::getPointId).collect(Collectors.toList());
                queryWrapperD.clear();
                queryWrapperM.clear();
                queryWrapperD.in("measurement_point_id", collect).
                        eq("QUARTER( data_date)", PublicUtil.getNowQuarter (dataDate));
                queryWrapperM.in("measurement_point_id", collect).
                        eq("QUARTER( data_date)", PublicUtil.getNowQuarter (dataDate));
                List<ROperatingMonitorDPO> rOperatingMonitorDPOList = rOperatingMonitorDService.list(queryWrapperD);
                List<ROperatingMonitorMPO> roperatingMonitorMPOList = rOperatingMonitorMService.list(queryWrapperM);
                ROperatingIndexQPO rOperatingIndexQPO = new ROperatingIndexQPO();
                rOperatingIndexQPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                rOperatingIndexQPO.setDataDate(date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
                List<String> collect1 = rOperatingMonitorDPOList.stream().filter(temp -> temp.getEffectiveMinuteCount() > 1).
                        map(ROperatingMonitorDPO::getMeasurementPointId).distinct().collect(Collectors.toList());

                List<ROperatingMonitorDPO> monitorList = rOperatingMonitorDPOList.stream().filter(temp -> temp.getEffectiveMinuteCount() > 1).collect(Collectors.toList());

                List<String> collect2 = rOperatingMonitorDPOList.stream().filter(temp -> temp.getDataIntegrityRate().doubleValue() > 0.95 && temp.getIsUnusual() == 1).
                        map(ROperatingMonitorDPO::getMeasurementPointId).distinct().collect(Collectors.toList());

                Map<String, List<ROperatingMonitorDPO>> collect3 = rOperatingMonitorDPOList.stream().collect(Collectors.groupingBy(ROperatingMonitorDPO::getDevId));

                Map<String, List<ROperatingMonitorDPO>> collect4 = monitorList.stream().collect(Collectors.groupingBy(ROperatingMonitorDPO::getDevId));

                rOperatingIndexQPO.setEffectiveAccessMeasurementCount(collect2.size());
                rOperatingIndexQPO.setMeasurementRunPoints(collect.size());
                rOperatingIndexQPO.setOnlineMeasurementCount(collect4.size());
                rOperatingIndexQPO.setTransitMeasurementPoints(collect1.size());
                rOperatingIndexQPO.setOnlineMeasurementPoints(collect3.size());
//                rOperatingIndexMPO.setShouldCount();

                List<String> collect5 = roperatingMonitorMPOList.stream().filter(temp -> temp.getIsUnusual() == 0).map(ROperatingMonitorMPO::getMeasurementPointId).distinct().collect(Collectors.toList());


                double v = roperatingMonitorMPOList.stream().mapToDouble(temp -> {
                    return temp.getIndexIntegrityRate().doubleValue();
                }).average().orElse(0.0);
                double v1 = roperatingMonitorMPOList.stream().mapToDouble(temp -> {
                    return temp.getDataRightRate().doubleValue();
                }).average().orElse(0.0);
                double v2 = roperatingMonitorMPOList.stream().mapToDouble(temp -> {
                    return temp.getDataIntegrityRate().doubleValue();
                }).average().orElse(0.0);
                rOperatingIndexQPO.setIndexIntegrityRate(v);
                rOperatingIndexQPO.setDataIntegrityRate(v2);
                rOperatingIndexQPO.setIsUnusual(collect5.size());
                rOperatingIndexQPO.setDataRightRate(v1);
                rOperatingIndexQPOList.add(rOperatingIndexQPO);

            }
            if (!CollectionUtils.isEmpty(deptGetChildrenMoreDTO.getPwMonitorIds())) {
                if (!CollectionUtils.isEmpty(deptGetChildrenMoreDTO.getPwMonitorIds())) {
                    Map<Integer, List<LineDevGetDTO>> collect3 = deptGetChildrenMoreDTO.getPwMonitorIds().stream().collect(Collectors.groupingBy(LineDevGetDTO::getLineType));
                    collect3.forEach((k,m)->{
                        List<String> collect = m.stream().map(LineDevGetDTO::getPointId).collect(Collectors.toList());
                        queryWrapperD.clear();
                        queryWrapperM.clear();
                        queryWrapperD.in("measurement_point_id", collect).
                                eq("DATE_FORMAT( data_date ,'%Y-%m')", dataDate.substring(0, 7));

                        queryWrapperM.in("measurement_point_id", collect).
                                eq("DATE_FORMAT( data_date ,'%Y-%m')", dataDate.substring(0, 7));

                        List<ROperatingMonitorDPO> rOperatingMonitorDPOList = rOperatingMonitorDService.list(queryWrapperD);
                        List<ROperatingMonitorMPO> roperatingMonitorMPOList = rOperatingMonitorMService.list(queryWrapperM);
                        RDnOperatingQPO rDnOperatingQPO = new RDnOperatingQPO();
                        rDnOperatingQPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                        rDnOperatingQPO.setDataDate(date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
                        List<String> collect1 = rOperatingMonitorDPOList.stream().filter(temp -> temp.getEffectiveMinuteCount() > 1).
                                map(ROperatingMonitorDPO::getMeasurementPointId).distinct().collect(Collectors.toList());

                        List<ROperatingMonitorDPO> monitorList = rOperatingMonitorDPOList.stream().filter(temp -> temp.getEffectiveMinuteCount() > 1).collect(Collectors.toList());

                        List<String> collect2 = rOperatingMonitorDPOList.stream().filter(temp -> temp.getDataIntegrityRate().doubleValue() > 0.95 && temp.getIsUnusual() == 1).
                                map(ROperatingMonitorDPO::getMeasurementPointId).distinct().collect(Collectors.toList());


                        rDnOperatingQPO.setEffectiveAccessMeasurementCount(collect2.size());
                        rDnOperatingQPO.setMeasurementRunPoints(collect.size());
                        rDnOperatingQPO.setTransitMeasurementPoints(collect1.size());
                        if (collect.size()==0){
                            rDnOperatingQPO.setEffectiveAccessRate(0.00);

                        }
                        else {
                            double v = Double.parseDouble(df.format((collect2.size()) / Double.valueOf(collect.size())));
                            rDnOperatingQPO.setEffectiveAccessRate(v);

                        }
//                rOperatingIndexMPO.setShouldCount();

                        List<String> collect5 = roperatingMonitorMPOList.stream().filter(temp -> temp.getIsUnusual() == 0).map(ROperatingMonitorMPO::getMeasurementPointId).distinct().collect(Collectors.toList());


                        double v = roperatingMonitorMPOList.stream().mapToDouble(temp -> {
                            return temp.getIndexIntegrityRate().doubleValue();
                        }).average().orElse(0.0);
                        double v1 = roperatingMonitorMPOList.stream().mapToDouble(temp -> {
                            return temp.getDataRightRate().doubleValue();
                        }).average().orElse(0.0);
                        double v2 = roperatingMonitorMPOList.stream().mapToDouble(temp -> {
                            return temp.getDataIntegrityRate().doubleValue();
                        }).average().orElse(0.0);
                        rDnOperatingQPO.setMeasurementPointType(getmeasurementType(k, lineSortMap));
                        rDnOperatingQPO.setIndexIntegrityRate(v);
                        rDnOperatingQPO.setDataIntegrityRate(v2);
                        rDnOperatingQPO.setIsUnusual(collect5.size());
                        rDnOperatingQPO.setDataRightRate(v1);
                        rdnOperatingIndexQPOList.add(rDnOperatingQPO);
                    });

                }

            }
        }

        if (!CollectionUtils.isEmpty(rdnOperatingIndexQPOList)){
            rDnOperatingQPOService.saveOrUpdateBatchByMultiId(rdnOperatingIndexQPOList);
        }
        if (!CollectionUtils.isEmpty(rOperatingIndexQPOList)){
            rOperatingIndexQPOService.saveOrUpdateBatchByMultiId(rOperatingIndexQPOList);

        }
        
    }
    @Deprecated
    private void hanlderM(List<DeptGetChildrenMoreDTO> data, String dataDate, Integer type) {
        List<RDnOperatingIndexMPO> rdnOperatingIndexMPOList = new ArrayList<>();
        List<ROperatingIndexMPO> rOperatingIndexMPOList = new ArrayList<>();
        QueryWrapper<ROperatingMonitorDPO> queryWrapperD = new QueryWrapper<>();
        QueryWrapper<ROperatingMonitorMPO> queryWrapperM = new QueryWrapper<>();
        Date date = DateUtil.parse(dataDate);
        /*获取监测点类型类型*/

        List<DictData> lineSortList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.LINE_SORT.getCode()).getData();
        Map<String, DictData> lineSortMap = lineSortList.stream().collect(Collectors.toMap(DictData::getCode, dictData -> dictData));

        for (DeptGetChildrenMoreDTO deptGetChildrenMoreDTO : data) {
            if (!CollectionUtils.isEmpty(deptGetChildrenMoreDTO.getLineBaseList())) {
                List<String> collect = deptGetChildrenMoreDTO.getLineBaseList().stream().map(LineDevGetDTO::getPointId).collect(Collectors.toList());
                queryWrapperD.clear();
                queryWrapperM.clear();
                queryWrapperD.in("measurement_point_id", collect).
                        eq("DATE_FORMAT( data_date ,'%Y-%m')", dataDate.substring(0, 7));

                queryWrapperM.in("measurement_point_id", collect).
                        eq("DATE_FORMAT( data_date ,'%Y-%m')", dataDate.substring(0, 7));

                List<ROperatingMonitorDPO> rOperatingMonitorDPOList = rOperatingMonitorDService.list(queryWrapperD);
                List<ROperatingMonitorMPO> roperatingMonitorMPOList = rOperatingMonitorMService.list(queryWrapperM);
                ROperatingIndexMPO rOperatingIndexMPO = new ROperatingIndexMPO();
                rOperatingIndexMPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                rOperatingIndexMPO.setDataDate(date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
                List<String> collect1 = rOperatingMonitorDPOList.stream().filter(temp -> temp.getEffectiveMinuteCount() > 1).
                        map(ROperatingMonitorDPO::getMeasurementPointId).distinct().collect(Collectors.toList());

                List<ROperatingMonitorDPO> monitorList = rOperatingMonitorDPOList.stream().filter(temp -> temp.getEffectiveMinuteCount() > 1).collect(Collectors.toList());

                List<String> collect2 = rOperatingMonitorDPOList.stream().filter(temp -> temp.getDataIntegrityRate().doubleValue() > 0.95 && temp.getIsUnusual() == 1).
                        map(ROperatingMonitorDPO::getMeasurementPointId).distinct().collect(Collectors.toList());

                Map<String, List<ROperatingMonitorDPO>> collect3 = rOperatingMonitorDPOList.stream().collect(Collectors.groupingBy(ROperatingMonitorDPO::getDevId));

                Map<String, List<ROperatingMonitorDPO>> collect4 = monitorList.stream().collect(Collectors.groupingBy(ROperatingMonitorDPO::getDevId));

                rOperatingIndexMPO.setEffectiveAccessMeasurementCount(collect2.size());
                rOperatingIndexMPO.setMeasurementRunPoints(collect.size());
                rOperatingIndexMPO.setOnlineMeasurementCount(collect4.size());
                rOperatingIndexMPO.setTransitMeasurementPoints(collect1.size());
                rOperatingIndexMPO.setOnlineMeasurementPoints(collect3.size());
//                rOperatingIndexMPO.setShouldCount();

                List<String> collect5 = roperatingMonitorMPOList.stream().filter(temp -> temp.getIsUnusual() == 0).map(ROperatingMonitorMPO::getMeasurementPointId).distinct().collect(Collectors.toList());


                double v = roperatingMonitorMPOList.stream().mapToDouble(temp -> {
                    return temp.getIndexIntegrityRate().doubleValue();
                }).average().orElse(0.0);
                double v1 = roperatingMonitorMPOList.stream().mapToDouble(temp -> {
                    return temp.getDataRightRate().doubleValue();
                }).average().orElse(0.0);
                double v2 = roperatingMonitorMPOList.stream().mapToDouble(temp -> {
                    return temp.getDataIntegrityRate().doubleValue();
                }).average().orElse(0.0);
                rOperatingIndexMPO.setIndexIntegrityRate(v);
                rOperatingIndexMPO.setDataIntegrityRate(v2);
                rOperatingIndexMPO.setIsUnusual(collect5.size());
                rOperatingIndexMPO.setDataRightRate(v1);
                rOperatingIndexMPOList.add(rOperatingIndexMPO);

            }
            if (!CollectionUtils.isEmpty(deptGetChildrenMoreDTO.getPwMonitorIds())) {
                if (!CollectionUtils.isEmpty(deptGetChildrenMoreDTO.getPwMonitorIds())) {
                    Map<Integer, List<LineDevGetDTO>> collect3 = deptGetChildrenMoreDTO.getPwMonitorIds().stream().collect(Collectors.groupingBy(LineDevGetDTO::getLineType));

                    collect3.forEach((k,v)->{
                        List<String> collect = v.stream().map(LineDevGetDTO::getPointId).collect(Collectors.toList());
                        queryWrapperD.clear();
                        queryWrapperM.clear();
                        queryWrapperD.in("measurement_point_id", collect).
                                eq("DATE_FORMAT( data_date ,'%Y-%m')", dataDate.substring(0, 7));

                        queryWrapperM.in("measurement_point_id", collect).
                                eq("DATE_FORMAT( data_date ,'%Y-%m')", dataDate.substring(0, 7));

                        List<ROperatingMonitorDPO> rOperatingMonitorDPOList = rOperatingMonitorDService.list(queryWrapperD);
                        List<ROperatingMonitorMPO> roperatingMonitorMPOList = rOperatingMonitorMService.list(queryWrapperM);
                        RDnOperatingIndexMPO rDnOperatingIndexMPO = new RDnOperatingIndexMPO();
                        rDnOperatingIndexMPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                        rDnOperatingIndexMPO.setDataDate(date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
                        List<String> collect1 = rOperatingMonitorDPOList.stream().filter(temp -> temp.getEffectiveMinuteCount() > 1).
                                map(ROperatingMonitorDPO::getMeasurementPointId).distinct().collect(Collectors.toList());

                        List<ROperatingMonitorDPO> monitorList = rOperatingMonitorDPOList.stream().filter(temp -> temp.getEffectiveMinuteCount() > 1).collect(Collectors.toList());

                        List<String> collect2 = rOperatingMonitorDPOList.stream().filter(temp -> temp.getDataIntegrityRate().doubleValue() > 0.95 && temp.getIsUnusual() == 1).
                                map(ROperatingMonitorDPO::getMeasurementPointId).distinct().collect(Collectors.toList());


                        rDnOperatingIndexMPO.setEffectiveAccessMeasurementCount(collect2.size());
                        rDnOperatingIndexMPO.setMeasurementRunPoints(collect.size());
                        rDnOperatingIndexMPO.setTransitMeasurementPoints(collect1.size());
                        if (collect.size()==0){
                            rDnOperatingIndexMPO.setEffectiveAccessRate(0.00);

                        }
                        else {
                            double rate = Double.parseDouble(df.format((collect2.size()) / Double.valueOf(collect.size())));
                            rDnOperatingIndexMPO.setEffectiveAccessRate(rate);

                        }
//                rOperatingIndexMPO.setShouldCount();

                        List<String> collect5 = roperatingMonitorMPOList.stream().filter(temp -> temp.getIsUnusual() == 0).map(ROperatingMonitorMPO::getMeasurementPointId).distinct().collect(Collectors.toList());


                        double integrity = roperatingMonitorMPOList.stream().mapToDouble(temp -> {
                            return temp.getIndexIntegrityRate().doubleValue();
                        }).average().orElse(0.0);
                        double v1 = roperatingMonitorMPOList.stream().mapToDouble(temp -> {
                            return temp.getDataRightRate().doubleValue();
                        }).average().orElse(0.0);
                        double v2 = roperatingMonitorMPOList.stream().mapToDouble(temp -> {
                            return temp.getDataIntegrityRate().doubleValue();
                        }).average().orElse(0.0);
                        rDnOperatingIndexMPO.setMeasurementPointType(getmeasurementType(k, lineSortMap));
                        rDnOperatingIndexMPO.setIndexIntegrityRate(integrity);
                        rDnOperatingIndexMPO.setDataIntegrityRate(v2);
                        rDnOperatingIndexMPO.setIsUnusual(collect5.size());
                        rDnOperatingIndexMPO.setDataRightRate(v1);
                        rdnOperatingIndexMPOList.add(rDnOperatingIndexMPO);
                    });

                }

            }
        }

        if (!CollectionUtils.isEmpty(rdnOperatingIndexMPOList)){
            rDnOperatingIndexMPOService.saveOrUpdateBatchByMultiId(rdnOperatingIndexMPOList);
        }
        if (!CollectionUtils.isEmpty(rOperatingIndexMPOList)){
            rOperatingIndexMPOService.saveOrUpdateBatchByMultiId(rOperatingIndexMPOList);

        }
    }

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