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.harmonic.pojo.po.RStatOrgMPO;
import com.njcn.prepare.harmonic.mapper.mysql.newalgorithm.*;
import com.njcn.prepare.harmonic.pojo.bo.CalculatedParam;
import com.njcn.prepare.harmonic.pojo.mysql.po.RStatOrgQPO;
import com.njcn.prepare.harmonic.pojo.mysql.po.RStatOrgYPO;
import com.njcn.prepare.harmonic.pojo.po.*;
import com.njcn.prepare.harmonic.service.mysql.newalgorithm.*;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.enums.DicDataEnum;
import com.njcn.system.enums.DicDataTypeEnum;
import com.njcn.system.pojo.po.DictData;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

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

    private final DicDataFeignClient dicDataFeignClient;

    private final RStatEventDPOMapper rStatEventDPOMapper;

    private final RStatEventMPOMapper rStatEventMPOMapper;

    private final RStatEventQPOMapper rStatEventQPOMapper;

    private final RStatEventYPOMapper rStatEventYPOMapper;

    private final RStatEventOrgDPOService rStatEventOrgDPOService;

    private final RStatEventOrgMPOService rStatEventOrgMPOService;

    private final RStatEventOrgQPOService rStatEventOrgQPOService;

    private final RStatEventOrgYPOService rStatEventOrgYPOService;


    @Override
    public void handleDay(CalculatedParam<DeptGetChildrenMoreDTO> calculatedParam) {
        List<RStatEventOrgDPO> rStatEventOrgDPOS = new ArrayList<>();
        DecimalFormat df = new DecimalFormat("0.00000");

        LocalDate localDate = LocalDateTimeUtil.parseDate(calculatedParam.getDataDate(), DatePattern.NORM_DATE_PATTERN);
        QueryWrapper<RStatEventDPO> queryWrapper = new QueryWrapper<>();

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

        calculatedParam.getIdList().forEach(deptGetChildrenMoreDTO -> {
            int sumEffectiveMeasurement =0;
//            List<RStatOrgDVO> rStatOrgDVOS = rStatOrgDMapper.listAll( LocalDateTimeUtil.format(localDate,DatePattern.NORM_DATE_PATTERN), deptGetChildrenMoreDTO.getUnitId());
//            if(CollectionUtils.isEmpty(rStatOrgDVOS)){
//                sumEffectiveMeasurement=0;
//            }else {
//                sumEffectiveMeasurement = rStatOrgDVOS.stream().mapToInt(RStatOrgDVO::getEffectiveMeasurement).sum();
//            }
//            int finalSumEffectiveMeasurement = sumEffectiveMeasurement;
            dataTypeMap.forEach((k, v)->{
                eventStatis.forEach(dictData -> {
                    if (Objects.equals(dictData.getCode(), DicDataEnum.DISTURBANCE.getCode())
                            || Objects.equals(dictData.getCode(), DicDataEnum.OTHER.getCode())
                            || Objects.equals(dictData.getCode(), DicDataEnum.RECORDING_WAVE.getCode())
                            || Objects.equals(dictData.getCode(), DicDataEnum.TOTAL_INDICATORS.getCode())) {
                        return;
                    }
                    queryWrapper.clear();
                    queryWrapper.
                            eq("org_no",deptGetChildrenMoreDTO.getUnitId()).
                            eq("data_type",v.getId()).
                            eq("event_type",dictData.getId()).
                            eq("data_date",localDate);
                    List<RStatEventDPO> list = rStatEventDPOMapper.selectList(queryWrapper);
                    int sum_event_measurement =0;
                    int sum_event_count =0;

                    if(CollectionUtils.isEmpty(list)){
                        sum_event_measurement=0;
                        sum_event_count =0;
                    }else {
                        sum_event_measurement = list.stream().mapToInt(RStatEventDPO::getEventMeasurement).sum();
                        sum_event_count = list.stream().mapToInt(RStatEventDPO::getEventCount).sum();

                    }
                    RStatEventOrgDPO rStatEventOrgDPO = new RStatEventOrgDPO();
                    rStatEventOrgDPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                    rStatEventOrgDPO.setDataDate(localDate);
                    rStatEventOrgDPO.setEventType(dictData.getId());
                    rStatEventOrgDPO.setEventMeasurement(sum_event_measurement);
                    rStatEventOrgDPO.setEventCount(sum_event_count);
                    rStatEventOrgDPO.setDataType(v.getId());
                    if (deptGetChildrenMoreDTO.getLineBaseList().size() == 0) {
                        rStatEventOrgDPO.setEventMeasurementRatio(0.0000f);
                    } else {
                        String value = df.format(((float)sum_event_measurement)/ deptGetChildrenMoreDTO.getLineBaseList().size());
                        rStatEventOrgDPO.setEventMeasurementRatio(Float.parseFloat(value));
                    }

                    rStatEventOrgDPOS.add(rStatEventOrgDPO);
                });
            });
        });
        if (!CollectionUtils.isEmpty(rStatEventOrgDPOS)) {
            rStatEventOrgDPOService.saveOrUpdateBatchByMultiId(rStatEventOrgDPOS, 500);
        }

    }

    @Override
    public void handleMonth(CalculatedParam<DeptGetChildrenMoreDTO> calculatedParam) {
        List<RStatEventOrgMPO> rStatEventOrgMPOS = new ArrayList<>();

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

        QueryWrapper<RStatOrgMPO> rStatOrgMPOQueryWrapper = new QueryWrapper<>();
        QueryWrapper<RStatEventMPO> queryWrapper = new QueryWrapper<>();
        QueryWrapper<RStatEventOrgDPO> queryWrapper1 = new QueryWrapper<>();


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

        calculatedParam.getIdList().forEach(deptGetChildrenMoreDTO -> {
//            int sumEffectiveMeasurement =0;
//            rStatOrgMPOQueryWrapper.clear();
//            rStatOrgMPOQueryWrapper.select("IFNULL(effective_measurement_accrued,0) effective_measurement_accrued").
//                    eq("org_no",deptGetChildrenMoreDTO.getUnitId()).
//                    between("data_date",begin,end);
//            List<RStatOrgMPO> rStatOrgMPOS = rStatOrgMMapper.selectList(rStatOrgMPOQueryWrapper);
//            if(CollectionUtils.isEmpty(rStatOrgMPOS)){
//                sumEffectiveMeasurement=0;
//            }else {
//                sumEffectiveMeasurement = rStatOrgMPOS.stream().mapToInt(RStatOrgMPO::getEffectiveMeasurementAccrued).sum();
//            }
//            int finalSumEffectiveMeasurement = sumEffectiveMeasurement;

            dataTypeMap.forEach((k, v)->{
                eventStatis.forEach(dictData -> {
                    if (Objects.equals(dictData.getCode(), DicDataEnum.DISTURBANCE.getCode())
                            || Objects.equals(dictData.getCode(), DicDataEnum.OTHER.getCode())
                            || Objects.equals(dictData.getCode(), DicDataEnum.RECORDING_WAVE.getCode())
                            || Objects.equals(dictData.getCode(), DicDataEnum.TOTAL_INDICATORS.getCode())) {
                        return;
                    }
                    queryWrapper.clear();
                    queryWrapper.
                            eq("org_no",deptGetChildrenMoreDTO.getUnitId()).
                            eq("data_type",v.getId()).
                            eq("event_type",dictData.getId()).
                            between("data_date",begin,end);
                    List<RStatEventMPO> list = rStatEventMPOMapper.selectList(queryWrapper);
                    int sum_event_measurement =0;
                    int sum_event_count =0;

                    if(CollectionUtils.isEmpty(list)){
                        sum_event_measurement=0;
                        sum_event_count =0;
                    }else {
                        sum_event_measurement = list.stream().mapToInt(RStatEventMPO::getEventMeasurementAccrued).sum();
                        sum_event_count = list.stream().mapToInt(RStatEventMPO::getEventCount).sum();

                    }
                    queryWrapper1.clear();
                    queryWrapper1.
                            eq("org_no",deptGetChildrenMoreDTO.getUnitId()).
                            eq("data_type",v.getId()).
                            eq("event_type",dictData.getId()).
                            between("data_date",begin,end);
                    List<RStatEventOrgDPO> rStatEventOrgDPOList = rStatEventOrgDPOService.list(queryWrapper1);
                    int event_count_all = rStatEventOrgDPOList.stream().mapToInt(RStatEventOrgDPO::getEventCount).sum();
                    int event_measurement_all = rStatEventOrgDPOList.stream().mapToInt(RStatEventOrgDPO::getEventMeasurement).sum();
                    double event_measurement_avg = rStatEventOrgDPOList.stream().mapToInt(RStatEventOrgDPO::getEventMeasurement).average().getAsDouble();

                    double asDouble = rStatEventOrgDPOList.stream().mapToDouble(RStatEventOrgDPO::getEventMeasurementRatio).average().getAsDouble();


                    RStatEventOrgMPO rStatEventOrgMPO = new RStatEventOrgMPO();
                    rStatEventOrgMPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                    rStatEventOrgMPO.setDataDate(localDate);
                    rStatEventOrgMPO.setEventType(dictData.getId());

                    rStatEventOrgMPO.setDataType(v.getId());

                    rStatEventOrgMPO.setEventMeasurementAverage(Float.parseFloat( df.format(event_measurement_avg)));
                    rStatEventOrgMPO.setEventMeasurementAccrued(sum_event_measurement);

                    if (event_measurement_all == 0) {
                        rStatEventOrgMPO.setEventFreq(0.00f);
                    } else {
                        String value = df.format(((float)event_count_all)/ event_measurement_all);
                        rStatEventOrgMPO.setEventFreq(Float.parseFloat(value));
                    }
                    rStatEventOrgMPO.setEventCount(event_count_all);

                    rStatEventOrgMPO.setEventMeasurementRatioAverage(Float.parseFloat( df.format(asDouble)));

                    if (deptGetChildrenMoreDTO.getLineBaseList().size() == 0) {
                        rStatEventOrgMPO.setEventMeasurementRatioAccrued(0.00f);
                    } else {
                        String value = df.format(((float)sum_event_measurement)/ deptGetChildrenMoreDTO.getLineBaseList().size());
                        rStatEventOrgMPO.setEventMeasurementRatioAccrued(Float.parseFloat(value));
                    }

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

    }

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

        List<RStatEventOrgQPO> rStatEventOrgQPOS = new ArrayList<>();
        DecimalFormat df = new DecimalFormat("0.00000");

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

        QueryWrapper<RStatOrgQPO> rStatOrgQPOQueryWrapper = new QueryWrapper<>();
        QueryWrapper<RStatEventQPO> queryWrapper = new QueryWrapper<>();
        QueryWrapper<RStatEventOrgDPO> queryWrapper1 = new QueryWrapper<>();
        QueryWrapper<RStatEventOrgMPO> queryWrapper2 = new QueryWrapper<>();


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

        calculatedParam.getIdList().forEach(deptGetChildrenMoreDTO -> {
//            int sumEffectiveMeasurement =0;
//            rStatOrgQPOQueryWrapper.clear();
//            rStatOrgQPOQueryWrapper.select("IFNULL(effective_measurement_accrued,0) effective_measurement_accrued").
//                    eq("org_no",deptGetChildrenMoreDTO.getUnitId()).
//                    between("data_date",begin,end);
//            List<RStatOrgQPO> rStatOrgQPOS = rStatOrgQMapper.selectList(rStatOrgQPOQueryWrapper);
//            if(CollectionUtils.isEmpty(rStatOrgQPOS)){
//                sumEffectiveMeasurement=0;
//            }else {
//                sumEffectiveMeasurement = rStatOrgQPOS.stream().mapToInt(RStatOrgQPO::getEffectiveMeasurementAccrued).sum();
//            }
//            int finalSumEffectiveMeasurement = sumEffectiveMeasurement;

            dataTypeMap.forEach((k, v)->{
                eventStatis.forEach(dictData -> {
                    if (Objects.equals(dictData.getCode(), DicDataEnum.DISTURBANCE.getCode())
                            || Objects.equals(dictData.getCode(), DicDataEnum.OTHER.getCode())
                            || Objects.equals(dictData.getCode(), DicDataEnum.RECORDING_WAVE.getCode())
                            || Objects.equals(dictData.getCode(), DicDataEnum.TOTAL_INDICATORS.getCode())) {
                        return;
                    }
                    queryWrapper.clear();
                    queryWrapper.
                            eq("org_no",deptGetChildrenMoreDTO.getUnitId()).
                            eq("data_type",v.getId()).
                            eq("event_type",dictData.getId()).
                            between("data_date",begin,end);
                    List<RStatEventQPO> list = rStatEventQPOMapper.selectList(queryWrapper);
                    int sum_event_measurement =0;
                    int sum_event_count =0;

                    if(CollectionUtils.isEmpty(list)){
                        sum_event_measurement=0;
                        sum_event_count =0;
                    }else {
                        sum_event_measurement = list.stream().mapToInt(RStatEventQPO::getEventMeasurementAccrued).sum();
                        sum_event_count = list.stream().mapToInt(RStatEventQPO::getEventCount).sum();

                    }
                    queryWrapper1.clear();
                    queryWrapper1.
                            eq("org_no",deptGetChildrenMoreDTO.getUnitId()).
                            eq("data_type",v.getId()).
                            eq("event_type",dictData.getId()).
                            between("data_date",begin,end);
                    List<RStatEventOrgDPO> rStatEventOrgDPOList = rStatEventOrgDPOService.list(queryWrapper1);
                    int event_count_all = rStatEventOrgDPOList.stream().mapToInt(RStatEventOrgDPO::getEventCount).sum();
                    int event_measurement_all = rStatEventOrgDPOList.stream().mapToInt(RStatEventOrgDPO::getEventMeasurement).sum();
//                    double event_measurement_avg = rStatEventOrgDPOList.stream().mapToInt(RStatEventOrgDPO::getEventMeasurement).average().getAsDouble();
//
//                    double asDouble = rStatEventOrgDPOList.stream().mapToDouble(RStatEventOrgDPO::getEventMeasurementRatio).average().getAsDouble();

                    queryWrapper2.clear();
                    queryWrapper2.
                            eq("org_no",deptGetChildrenMoreDTO.getUnitId()).
                            eq("data_type",v.getId()).
                            eq("event_type",dictData.getId()).
                            between("data_date",begin,end);
                    List<RStatEventOrgMPO> rStatEventOrgMPOList = rStatEventOrgMPOService.list(queryWrapper2);

                    double event_measurement_average = rStatEventOrgMPOList.stream().mapToDouble(RStatEventOrgMPO::getEventMeasurementAverage).average().getAsDouble();
                    double event_measurement_ratio_average = rStatEventOrgMPOList.stream().mapToDouble(RStatEventOrgMPO::getEventMeasurementRatioAverage).average().getAsDouble();

                    int event_count = rStatEventOrgMPOList.stream().mapToInt(RStatEventOrgMPO::getEventCount).sum();

                    RStatEventOrgQPO rStatEventOrgQPO = new RStatEventOrgQPO();
                    rStatEventOrgQPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                    rStatEventOrgQPO.setDataDate(localDate);
                    rStatEventOrgQPO.setEventType(dictData.getId());
                    rStatEventOrgQPO.setDataType(v.getId());
                    rStatEventOrgQPO.setEventMeasurementAverage(Float.parseFloat( df.format(event_measurement_average)));
                    rStatEventOrgQPO.setEventMeasurementAccrued(sum_event_measurement);
                    rStatEventOrgQPO.setEventCount(event_count);
                    rStatEventOrgQPO.setEventMeasurementRatioAverage(Float.parseFloat( df.format(event_measurement_ratio_average)));
                    if (deptGetChildrenMoreDTO.getLineBaseList().size() == 0) {
                        rStatEventOrgQPO.setEventMeasurementRatioAccrued(0.00f);
                    } else {
                        String value = df.format(((float)sum_event_measurement)/ deptGetChildrenMoreDTO.getLineBaseList().size() );
                        rStatEventOrgQPO.setEventMeasurementRatioAccrued(Float.parseFloat(value));
                    }
                    if (event_measurement_all == 0) {
                        rStatEventOrgQPO.setEventFreq(0.00f);
                    } else {
                        String value = df.format(((float)event_count_all)/ event_measurement_all);
                        rStatEventOrgQPO.setEventFreq(Float.parseFloat(value));
                    }


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

    }

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

        List<RStatEventOrgYPO> rStatEventOrgYPOS = new ArrayList<>();
        DecimalFormat df = new DecimalFormat("0.00");

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

        QueryWrapper<RStatOrgYPO> rStatOrgYPOQueryWrapper = new QueryWrapper<>();
        QueryWrapper<RStatEventYPO> queryWrapper = new QueryWrapper<>();
        QueryWrapper<RStatEventOrgDPO> queryWrapper1 = new QueryWrapper<>();
        QueryWrapper<RStatEventOrgMPO> queryWrapper2 = new QueryWrapper<>();


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

        calculatedParam.getIdList().forEach(deptGetChildrenMoreDTO -> {
//            int sumEffectiveMeasurement =0;
//            rStatOrgYPOQueryWrapper.clear();
//            rStatOrgYPOQueryWrapper.select("IFNULL(effective_measurement_accrued,0) effective_measurement_accrued").
//                    eq("org_no",deptGetChildrenMoreDTO.getUnitId()).
//                    between("data_date",begin,end);
//            List<RStatOrgYPO> rStatOrgYPOS = rStatOrgYMapper.selectList(rStatOrgYPOQueryWrapper);
//            if(CollectionUtils.isEmpty(rStatOrgYPOS)){
//                sumEffectiveMeasurement=0;
//            }else {
//                sumEffectiveMeasurement = rStatOrgYPOS.stream().mapToInt(RStatOrgYPO::getEffectiveMeasurementAccrued).sum();
//            }
//            int finalSumEffectiveMeasurement = sumEffectiveMeasurement;

            dataTypeMap.forEach((k, v)->{
                eventStatis.forEach(dictData -> {
                    if (Objects.equals(dictData.getCode(), DicDataEnum.DISTURBANCE.getCode())
                            || Objects.equals(dictData.getCode(), DicDataEnum.OTHER.getCode())
                            || Objects.equals(dictData.getCode(), DicDataEnum.RECORDING_WAVE.getCode())
                            || Objects.equals(dictData.getCode(), DicDataEnum.TOTAL_INDICATORS.getCode())) {
                        return;
                    }
                    queryWrapper.clear();
                    queryWrapper.
                            eq("org_no",deptGetChildrenMoreDTO.getUnitId()).
                            eq("data_type",v.getId()).
                            eq("event_type",dictData.getId()).
                            between("data_date",begin,end);
                    List<RStatEventYPO> list = rStatEventYPOMapper.selectList(queryWrapper);
                    int sum_event_measurement =0;
                    int sum_event_count =0;

                    if(CollectionUtils.isEmpty(list)){
                        sum_event_measurement=0;
                        sum_event_count =0;
                    }else {
                        sum_event_measurement = list.stream().mapToInt(RStatEventYPO::getEventMeasurementAccrued).sum();
                        sum_event_count = list.stream().mapToInt(RStatEventYPO::getEventCount).sum();

                    }
                    queryWrapper1.clear();
                    queryWrapper1.
                            eq("org_no",deptGetChildrenMoreDTO.getUnitId()).
                            eq("data_type",v.getId()).
                            eq("event_type",dictData.getId()).
                            between("data_date",begin,end);
                    List<RStatEventOrgDPO> rStatEventOrgDPOList = rStatEventOrgDPOService.list(queryWrapper1);
                    int event_count_all = rStatEventOrgDPOList.stream().mapToInt(RStatEventOrgDPO::getEventCount).sum();
                    int event_measurement_all = rStatEventOrgDPOList.stream().mapToInt(RStatEventOrgDPO::getEventMeasurement).sum();
                    double event_measurement_avg = rStatEventOrgDPOList.stream().mapToInt(RStatEventOrgDPO::getEventMeasurement).average().getAsDouble();

                    double asDouble = rStatEventOrgDPOList.stream().mapToDouble(RStatEventOrgDPO::getEventMeasurementRatio).average().getAsDouble();

                    queryWrapper2.clear();
                    queryWrapper2.
                            eq("org_no",deptGetChildrenMoreDTO.getUnitId()).
                            eq("data_type",v.getId()).
                            eq("event_type",dictData.getId()).
                            between("data_date",begin,end);
                    List<RStatEventOrgMPO> rStatEventOrgMPOList = rStatEventOrgMPOService.list(queryWrapper2);

                    double event_measurement_average = rStatEventOrgMPOList.stream().mapToDouble(RStatEventOrgMPO::getEventMeasurementAverage).average().getAsDouble();
                    double event_measurement_ratio_average = rStatEventOrgMPOList.stream().mapToDouble(RStatEventOrgMPO::getEventMeasurementRatioAverage).average().getAsDouble();

                    int event_count = rStatEventOrgMPOList.stream().mapToInt(RStatEventOrgMPO::getEventCount).sum();

                    RStatEventOrgYPO rStatEventOrgYPO = new RStatEventOrgYPO();
                    rStatEventOrgYPO.setOrgNo(deptGetChildrenMoreDTO.getUnitId());
                    rStatEventOrgYPO.setDataDate(localDate);
                    rStatEventOrgYPO.setEventType(dictData.getId());
                    rStatEventOrgYPO.setDataType(v.getId());
                    rStatEventOrgYPO.setEventMeasurementAverage(Float.parseFloat( df.format(event_measurement_average)));
                    rStatEventOrgYPO.setEventMeasurementAccrued(sum_event_measurement);
                    rStatEventOrgYPO.setEventCount(event_count);
                    rStatEventOrgYPO.setEventMeasurementRatioAverage(Float.parseFloat( df.format(event_measurement_ratio_average)));
                    if (deptGetChildrenMoreDTO.getLineBaseList().size()  == 0) {
                        rStatEventOrgYPO.setEventMeasurementRatioAccrued(0.00f);
                    } else {
                        String value = df.format(((float)sum_event_measurement)/ deptGetChildrenMoreDTO.getLineBaseList().size() );
                        rStatEventOrgYPO.setEventMeasurementRatioAccrued(Float.parseFloat(value));
                    }
                    if (event_measurement_all == 0) {
                        rStatEventOrgYPO.setEventFreq(0.00f);
                    } else {
                        String value = df.format(((float)event_count_all)/ event_measurement_all);
                        rStatEventOrgYPO.setEventFreq(Float.parseFloat(value));
                    }


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

    }



}
