/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.xrtygis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.tofly.common.core.util.StringUtil;
import com.tofly.common.oauth.util.SecurityUtils;
import com.tofly.xrtygis.config.DictConfig;
import com.tofly.xrtygis.custom.util.DateTimeUtil;
import com.tofly.xrtygis.em.Constant;
import com.tofly.xrtygis.entity.*;
import com.tofly.xrtygis.entity.dto.CustomizeDto;
import com.tofly.xrtygis.entity.vo.DmaCalculatePramVo;
import com.tofly.xrtygis.entity.vo.DmaMinWaterSupplyVo;
import com.tofly.xrtygis.mapper.IndicatorCorrectionMapper;
import com.tofly.xrtygis.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;


/**
 * 指标修正记录表
 *
 * @author admin
 * @date Thu Apr 28 00:00:00 CST 2022
 */
@Service("indicatorCorrectionService")
public class IndicatorCorrectionServiceImpl extends ServiceImpl<IndicatorCorrectionMapper, IndicatorCorrection> implements IndicatorCorrectionService {

    @Autowired
    private DmaregionService dmaregionService;
    @Autowired
    private DmaUnfilledWaterService dmaUnfilledWaterService;
    @Autowired
    private DmaStatisticalService dmaStatisticalService;
    @Autowired
    private TotalStatisticalService totalStatisticalService;
    @Autowired
    private CustomizeService customizeService;
    @Autowired
    private DictConfig dictConfig;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean leakageRateCalculate(Date reviseDate) {
        // 统计值存储
        Map<Long, DmaStatistical> dmaStatisticalMap = new HashMap<>(12);

        List<Long> dmaIdList = dmaregionService.list(new QueryWrapper<Dmaregion>().lambda().select(Dmaregion::getId)).stream().map(Dmaregion::getId).collect(Collectors.toList());
        // 当月开始时间 当月结束时间
        LocalDateTime time = DateTimeUtil.fromLong2LocalDateTime(reviseDate.getTime());
        Date startDate = DateTimeUtil.localDateToDate(DateTimeUtil.getFirstDayOfCurrentSpecifiedMonth(time));
        Date localDateToDate = DateTimeUtil.localDateToDate(DateTimeUtil.getLastDayOfCurrentSpecifiedMonth(time));
        Date endDate = DateTimeUtil.convertLDTToDate(DateTimeUtil.getAssignTodayEndTime(DateTimeUtil.fromLong2LocalDateTime(localDateToDate.getTime())));
        Date totalUserDate = DateTimeUtil.convertLDTToDate(time.plusMonths(1));
        Map<Long, BigDecimal> userWaterMap = baseMapper.getDmaUserWaterConsumption(DateTimeUtil.getLocalDate(startDate,DateTimeUtil.MONTHS_YEARS_FORMAT)).stream().collect(Collectors.toMap(DmaCalculatePramVo::getDmaId, DmaCalculatePramVo::getUserWaterConsumption, (key1, key2) -> key2));
        // 获取当前月份
        String yearMonth = DateTimeUtil.getLocalDate(reviseDate, DateTimeUtil.MONTHS_FORMAT);
        // 获取当前月份(最大数据) 设备供水量
        List<DmaCalculatePramVo> supplyList = baseMapper.getDmaWaterSupply(yearMonth);
        // 获取当前月份(最小数据) 设备供水量
        List<DmaCalculatePramVo> lodTotalWaterSupply = baseMapper.getLostMonthWaterSupply(yearMonth);
        // 处理供水量逻辑
        Map<Long, BigDecimal> waterSupplyMap = getWaterSupplyMap(supplyList, lodTotalWaterSupply);
        // 获取对应DMA分区的 远传供水量(远传表) 当月开始时间 当月结束时间
        Map<Long, BigDecimal> remoteWaterMap = baseMapper.getRemoteWaterSupply(startDate, endDate).stream().collect(Collectors.toMap(DmaCalculatePramVo::getDmaId, DmaCalculatePramVo::getRemoteWaterSupply, (key1, key2) -> key2));
        // 获取对应DMA分区的 未上盘水量(对应的DMA分区 每月只对应 一条未上盘水量)
        Map<Long, DmaUnfilledWater> unfilledWaterMap = dmaUnfilledWaterService.list(new QueryWrapper<DmaUnfilledWater>().lambda()
                .ge(DmaUnfilledWater::getWriteDate, startDate)
                .le(DmaUnfilledWater::getWriteDate, endDate)).stream().collect(Collectors.toMap(DmaUnfilledWater::getDmaId, m -> m));
        // 获取对应DMA分区的 DN75（含）以上管道长度 总长 km
        Map<Long, BigDecimal> dnLengthMap = baseMapper.getDNLength().stream().collect(Collectors.toMap(DmaCalculatePramVo::getDmaId, DmaCalculatePramVo::getDNLength, (key1, key2) -> key2));
        // 获取对应DMA分区的 年平均压力
        String year = DateTimeUtil.getLocalDate(reviseDate, DateTimeUtil.YEAR);
        Map<Long, BigDecimal> yearPressureMap = baseMapper.getDmaYearPressure(year).stream().collect(Collectors.toMap(DmaCalculatePramVo::getDmaId, DmaCalculatePramVo::getYearPressure, (key1, key2) -> key2));
        // 获取对应DMA分区的 漏点处理信息 处漏数量 漏点数量
        List<DmaCalculatePramVo> leakageGdInfo = baseMapper.getLeakageGdInfo(startDate, endDate);
        Map<Long, BigDecimal> leakageOverValMap = leakageGdInfo.stream().collect(Collectors.toMap(DmaCalculatePramVo::getDmaId, DmaCalculatePramVo::getLeakageOverVal, (key1, key2) -> key2));
        Map<Long, BigDecimal> leakageValMap = leakageGdInfo.stream().collect(Collectors.toMap(DmaCalculatePramVo::getDmaId, DmaCalculatePramVo::getLeakageVal, (key1, key2) -> key2));
        // 巡检效率相关统计
        BigDecimal totalPlanPipeLength = baseMapper.getTotalPlanPipeLength(startDate, endDate);
        BigDecimal planTotalDistanceLength = baseMapper.getPlanTotalDistanceLength(startDate, endDate);
        dmaIdList.forEach(dmaId -> {
            DmaStatistical vo = new DmaStatistical();
            // 计算修正前漏损率
            BigDecimal waterSupply = waterSupplyMap.get(dmaId) == null ? BigDecimal.ZERO : waterSupplyMap.get(dmaId);
            BigDecimal remoteWater = remoteWaterMap.get(dmaId) == null ? BigDecimal.ZERO : remoteWaterMap.get(dmaId);
            // 供水量 (设备+远传)
            BigDecimal gsl = waterSupply.add(remoteWater);
            // 售水量
            BigDecimal userWater = userWaterMap.get(dmaId) == null ? BigDecimal.ZERO : userWaterMap.get(dmaId);
            DmaUnfilledWater dmaUnfilledWater = unfilledWaterMap.get(dmaId) == null ? new DmaUnfilledWater() : unfilledWaterMap.get(dmaId);
            BigDecimal freeWaterVolume = dmaUnfilledWater.getFreeWaterVolume() == null ? BigDecimal.ZERO : dmaUnfilledWater.getFreeWaterVolume();
            BigDecimal outflowWaterVolume = dmaUnfilledWater.getOutflowWaterVolume() == null ? BigDecimal.ZERO : dmaUnfilledWater.getOutflowWaterVolume();
            BigDecimal lossWaterVolume = dmaUnfilledWater.getLossWaterVolume() == null ? BigDecimal.ZERO : dmaUnfilledWater.getLossWaterVolume();
            BigDecimal otherWaterVolume = dmaUnfilledWater.getOtherWaterVolume() == null ? BigDecimal.ZERO : dmaUnfilledWater.getOtherWaterVolume();
            // 计费用水量/计费计量用水量 从营销来
//            BigDecimal billingWaterVolume = dmaUnfilledWater.getBillingWaterVolume() == null ? BigDecimal.ZERO : dmaUnfilledWater.getBillingWaterVolume();

            //            BigDecimal measureVolume = dmaUnfilledWater.getMeasureVolume() == null ? BigDecimal.ZERO : dmaUnfilledWater.getMeasureVolume();
            BigDecimal notMeasureVolume = dmaUnfilledWater.getNotMeasureVolume() == null ? BigDecimal.ZERO : dmaUnfilledWater.getNotMeasureVolume();
            BigDecimal freeMeasureVolume = dmaUnfilledWater.getFreeMeasureVolume() == null ? BigDecimal.ZERO : dmaUnfilledWater.getFreeMeasureVolume();
            BigDecimal notFreeMeasureVolume = dmaUnfilledWater.getNotFreeMeasureVolume() == null ? BigDecimal.ZERO : dmaUnfilledWater.getNotFreeMeasureVolume();
            BigDecimal obviousOutflowVolume = dmaUnfilledWater.getObviousOutflowVolume() == null ? BigDecimal.ZERO : dmaUnfilledWater.getObviousOutflowVolume();
            BigDecimal darkOutflowVolume = dmaUnfilledWater.getDarkOutflowVolume() == null ? BigDecimal.ZERO : dmaUnfilledWater.getDarkOutflowVolume();
            BigDecimal backgroundOutflowVolume = dmaUnfilledWater.getBackgroundOutflowVolume() == null ? BigDecimal.ZERO : dmaUnfilledWater.getBackgroundOutflowVolume();
            BigDecimal otherOutflowVolume = dmaUnfilledWater.getOtherOutflowVolume() == null ? BigDecimal.ZERO : dmaUnfilledWater.getOtherOutflowVolume();
            BigDecimal userLossVolume = dmaUnfilledWater.getUserLossVolume() == null ? BigDecimal.ZERO : dmaUnfilledWater.getUserLossVolume();
            BigDecimal notUserLossVolume = dmaUnfilledWater.getNotUserLossVolume() == null ? BigDecimal.ZERO : dmaUnfilledWater.getNotUserLossVolume();

            // 修正前漏损率=（供水量-注册用户用水量）/供水量 (修正后同步修改该字段值)
            // 注册用户用水量 = 计费计量用水量+计费未计量用水量+免费用水量(免费计量用水量+免费未计量用水量)
            BigDecimal zcysl = userWater.add(notMeasureVolume).add(freeWaterVolume);
            // 产销差（%）=【（总供水量-计费计量用水量-计费未计量用水量）/总供水量】×100
            BigDecimal poorProductionRate = BigDecimal.ZERO;
            BigDecimal leakageRate = BigDecimal.ZERO;
            if (BigDecimal.ZERO.compareTo(gsl) != 0) {
                leakageRate = gsl.subtract(zcysl).divide(gsl, 4, BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(100));

                poorProductionRate = gsl.subtract(userWater).subtract(notMeasureVolume).divide(gsl, 4, BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(100));
            }
            // DMA分区的 DN75（含）以上管道长度 总长 km
            BigDecimal dnLengthVal = dnLengthMap.get(dmaId) == null ? BigDecimal.ZERO : dnLengthMap.get(dmaId);
            // DMA分区的 年平均压力
            BigDecimal yearPressureVal = yearPressureMap.get(dmaId) == null ? BigDecimal.ZERO : yearPressureMap.get(dmaId);
            // 漏点相关数量
            BigDecimal leakageOverVal = leakageOverValMap.get(dmaId) == null ? BigDecimal.ZERO : leakageOverValMap.get(dmaId);
            BigDecimal leakageVal = leakageValMap.get(dmaId) == null ? BigDecimal.ZERO : leakageValMap.get(dmaId);
            // 巡检效率相关统计
            vo.setTotalPlanPipeLength(totalPlanPipeLength);
            vo.setPlanTotalDistanceLength(planTotalDistanceLength);
            vo.setDmaId(dmaId);
            vo.setStatisticalDate(reviseDate);
            vo.setBillingWaterVolume(userWater.add(notMeasureVolume));
            vo.setFreeWaterVolume(freeWaterVolume);
            vo.setOutflowWaterVolume(outflowWaterVolume);
            vo.setLossWaterVolume(lossWaterVolume);
            vo.setOtherWaterVolume(otherWaterVolume);
            vo.setRemoteWater(remoteWater);
            vo.setMeasureVolume(userWater);
            vo.setNotMeasureVolume(notMeasureVolume);
            vo.setFreeMeasureVolume(freeMeasureVolume);
            vo.setNotFreeMeasureVolume(notFreeMeasureVolume);
            vo.setObviousOutflowVolume(obviousOutflowVolume);
            vo.setDarkOutflowVolume(darkOutflowVolume);
            vo.setBackgroundOutflowVolume(backgroundOutflowVolume);
            vo.setOtherOutflowVolume(otherOutflowVolume);
            vo.setUserLossVolume(userLossVolume);
            vo.setNotUserLossVolume(notUserLossVolume);
            vo.setWaterSupply(gsl);
            vo.setLeakageRate(leakageRate);
            vo.setPoorProductionRate(poorProductionRate);
            vo.setUserWater(userWater);
            vo.setLeakageOverVal(leakageOverVal);
            vo.setLeakageVal(leakageVal);
            vo.setDnLengthVal(dnLengthVal);
            vo.setYearPressureVal(yearPressureVal);
            vo.setStatisticType(1);
            dmaStatisticalMap.put(dmaId, vo);
        });
        // 对应DMA分区 统计时间 相同 则更新 其余为新增
        if (dmaIdList.size() > 0) {
            Map<Long, Long> collect = dmaStatisticalService.list(new QueryWrapper<DmaStatistical>().lambda()
                    .in(DmaStatistical::getDmaId, dmaIdList)
                    .eq(DmaStatistical::getStatisticalDate, reviseDate)).stream()
                    .collect(Collectors.toMap(DmaStatistical::getDmaId, DmaStatistical::getId, (key1, key2) -> key2));
            collect.forEach((k, v) -> {
                DmaStatistical dmaStatistical = dmaStatisticalMap.get(k);
                if (null != dmaStatistical) {
                    dmaStatistical.setId(v);
                }
            });
            if (dmaStatisticalMap.size() > 0) {
                Collection<DmaStatistical> dmaStatisticalList=dmaStatisticalMap.values();
                dmaStatisticalList.forEach(item->{
                    dmaStatisticalService.saveOrUpdate(item);
                });

               // dmaStatisticalService.saveOrUpdateBatch(dmaStatisticalMap.values());
                this.saveIndicatorCorrection(dmaStatisticalMap.values());
                this.saveCustomize(dmaStatisticalMap.values(),reviseDate);
            }
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean leakageRateCalculateDay(Date reviseDate) {
        // 统计值存储
        Map<Long, DmaStatistical> dmaStatisticalMap = new HashMap<>(12);

        List<Long> dmaIdList = dmaregionService.list(new QueryWrapper<Dmaregion>().lambda().select(Dmaregion::getId)).stream().map(Dmaregion::getId).collect(Collectors.toList());
        // 当天开始时间 当天结束时间
        LocalDate time = DateTimeUtil.UDateToLocalDate(reviseDate);
        Date startDate = DateTimeUtil.convertLDTToDate(DateTimeUtil.getAssignTodayBeginDate(time));
        Date endDate = DateTimeUtil.convertLDTToDate(DateTimeUtil.getAssignTodayEndDate(time));
        Map<Long, BigDecimal> userWaterMap = baseMapper.getDmaUserWaterConsumptionDay(DateTimeUtil.getLocalDate(startDate,DateTimeUtil.DAY_FORMAT)).stream().collect(Collectors.toMap(DmaCalculatePramVo::getDmaId, DmaCalculatePramVo::getUserWaterConsumption, (key1, key2) -> key2));

        // 获取当前日期
        String yearMonthDay = DateTimeUtil.getLocalDate(reviseDate, DateTimeUtil.DAY_FORMAT);
        // 获取当前日期(最大数据) 设备供水量
        List<DmaCalculatePramVo> supplyList = baseMapper.getDmaWaterSupplyDay(yearMonthDay,
                dictConfig.getMomentFlowAllIndex());
        // 获取当前日期(最小数据) 设备供水量
        List<DmaCalculatePramVo> lodTotalWaterSupply = baseMapper.getLostMonthWaterSupplyDay(yearMonthDay,
                dictConfig.getMomentFlowAllIndex());
        // 获取每个分区最小瞬时流量
        List<DmaMinWaterSupplyVo> minWaterSupplyVos = baseMapper.getDmaInstantaneousMinWaterSupply(yearMonthDay,
                dictConfig.getNightFlowAllIndex());
        Map<Long, BigDecimal> minWaterSupply = minWaterSupplyVos.stream().collect(Collectors.toMap(DmaMinWaterSupplyVo::getDmaId, DmaMinWaterSupplyVo::getMinWaterSupply,(key1, key2) -> key2));

        Map<Long, BigDecimal> waterSupplyMap = supplyList.stream().collect(Collectors.toMap(DmaCalculatePramVo::getDmaId, DmaCalculatePramVo::getWaterSupply,(key1, key2) -> key2));


        // 处理供水量逻辑
      //  Map<Long, BigDecimal> waterSupplyMap = getWaterSupplyMap(supplyList, lodTotalWaterSupply);
        // 总供水量
        BigDecimal totalWaterSupply = waterSupplyMap.values().stream().filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
        // 获取对应DMA分区的 远传供水量(远传表) 当日开始时间 当日结束时间
        Map<Long, BigDecimal> remoteWaterMap = baseMapper.getRemoteWaterSupply(startDate, endDate).stream().collect(Collectors.toMap(DmaCalculatePramVo::getDmaId, DmaCalculatePramVo::getRemoteWaterSupply, (key1, key2) -> key2));
        // 获取对应DMA分区的 未上盘水量(对应的DMA分区 每月只对应 一条未上盘水量)
        Map<Long, DmaUnfilledWater> unfilledWaterMap = dmaUnfilledWaterService.list(new QueryWrapper<DmaUnfilledWater>().lambda()
                .ge(DmaUnfilledWater::getWriteDate, startDate)
                .le(DmaUnfilledWater::getWriteDate, endDate)).stream().collect(Collectors.toMap(DmaUnfilledWater::getDmaId, m -> m));
        // 获取对应DMA分区的 DN75（含）以上管道长度 总长 km
        Map<Long, BigDecimal> dnLengthMap = baseMapper.getDNLength().stream().collect(Collectors.toMap(DmaCalculatePramVo::getDmaId, DmaCalculatePramVo::getDNLength, (key1, key2) -> key2));
        // 获取对应DMA分区的 年平均压力
        String year = DateTimeUtil.getLocalDate(reviseDate, DateTimeUtil.YEAR);
        Map<Long, BigDecimal> yearPressureMap = baseMapper.getDmaYearPressure(year).stream().collect(Collectors.toMap(DmaCalculatePramVo::getDmaId, DmaCalculatePramVo::getYearPressure, (key1, key2) -> key2));

        dmaIdList.forEach(dmaId -> {
            DmaStatistical vo = new DmaStatistical();
            // 计算修正前漏损率
            BigDecimal waterSupply = waterSupplyMap.get(dmaId) == null ? BigDecimal.ZERO : waterSupplyMap.get(dmaId);
            BigDecimal remoteWater = remoteWaterMap.get(dmaId) == null ? BigDecimal.ZERO : remoteWaterMap.get(dmaId);
            // 供水量 (设备+远传)
            BigDecimal gsl = waterSupply.add(remoteWater);
            // 售水量 绵阳师院等于供水量
          BigDecimal userWater = userWaterMap.get(dmaId) == null ? BigDecimal.ZERO : userWaterMap.get(dmaId);
            //   BigDecimal userWater = gsl;
            // 未上盘水量 = 免费用水量+漏失水量+计量漏失水量+其他损失水量+计费用水量
            BigDecimal wspsl = BigDecimal.ZERO;
            DmaUnfilledWater dmaUnfilledWater = unfilledWaterMap.get(dmaId) == null ? new DmaUnfilledWater() : unfilledWaterMap.get(dmaId);
            BigDecimal freeWaterVolume = dmaUnfilledWater.getFreeWaterVolume() == null ? BigDecimal.ZERO : dmaUnfilledWater.getFreeWaterVolume();
            BigDecimal outflowWaterVolume = dmaUnfilledWater.getOutflowWaterVolume() == null ? BigDecimal.ZERO : dmaUnfilledWater.getOutflowWaterVolume();
            BigDecimal lossWaterVolume = dmaUnfilledWater.getLossWaterVolume() == null ? BigDecimal.ZERO : dmaUnfilledWater.getLossWaterVolume();
            BigDecimal otherWaterVolume = dmaUnfilledWater.getOtherWaterVolume() == null ? BigDecimal.ZERO : dmaUnfilledWater.getOtherWaterVolume();
            BigDecimal billingWaterVolume = dmaUnfilledWater.getBillingWaterVolume() == null ? BigDecimal.ZERO : dmaUnfilledWater.getBillingWaterVolume();
            wspsl = wspsl.add(freeWaterVolume).add(outflowWaterVolume).add(lossWaterVolume).add(otherWaterVolume).add(billingWaterVolume);
            // 修正前漏损率=（供水量-售水量-未上盘水量）/供水量 (修正后同步修改该字段值)
            // 产销差（%）=【（总供水量-售水量）/总供水量】×100
            BigDecimal poorProductionRate = BigDecimal.ZERO;
            BigDecimal leakageRate = BigDecimal.ZERO;
            if (BigDecimal.ZERO.compareTo(gsl) != 0) {
                leakageRate = gsl.subtract(userWater).subtract(wspsl).divide(gsl, 4, BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(100));

                poorProductionRate = gsl.subtract(userWater).divide(gsl, 4, BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(100));
            }
            // DMA分区的 DN75（含）以上管道长度 总长 km
            BigDecimal dnLengthVal = dnLengthMap.get(dmaId) == null ? BigDecimal.ZERO : dnLengthMap.get(dmaId);
            // DMA分区的 年平均压力
            BigDecimal yearPressureVal = yearPressureMap.get(dmaId) == null ? BigDecimal.ZERO : yearPressureMap.get(dmaId);
            vo.setDmaId(dmaId);
            vo.setStatisticalDate(reviseDate);
            //vo.setBillingWaterVolume(billingWaterVolume);
            vo.setBillingWaterVolume(userWater);
            vo.setFreeWaterVolume(freeWaterVolume);

            vo.setOutflowWaterVolume(outflowWaterVolume);
            vo.setLossWaterVolume(lossWaterVolume);
            vo.setOtherWaterVolume(otherWaterVolume);
            vo.setRemoteWater(remoteWater);
            vo.setWaterSupply(gsl);
            vo.setLeakageRate(leakageRate);
            vo.setPoorProductionRate(poorProductionRate);
            vo.setUserWater(userWater);
            vo.setDnLengthVal(dnLengthVal);
            vo.setYearPressureVal(yearPressureVal);
            vo.setStatisticType(0);
            if(null!= minWaterSupply && minWaterSupply.size()>0){
                vo.setMinWaterSupply(minWaterSupply.get(dmaId));
            }
            if(totalWaterSupply.compareTo(BigDecimal.ZERO)>0){
                vo.setWaterSupplyRate(gsl.divide(totalWaterSupply,4,BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(100)));
            }
            dmaStatisticalMap.put(dmaId, vo);

          //  dmaStatisticalNewMapper.insert(vo);

        });
        // 对应DMA分区 统计时间 相同 则更新 其余为新增
        if (dmaIdList.size() > 0) {
            Map<Long, Long> collect = dmaStatisticalService.list(new QueryWrapper<DmaStatistical>().lambda()
                            .in(DmaStatistical::getDmaId, dmaIdList)
                            .eq(DmaStatistical::getStatisticalDate, reviseDate)).stream()
                    .collect(Collectors.toMap(DmaStatistical::getDmaId, DmaStatistical::getId, (key1, key2) -> key2));
            collect.forEach((k, v) -> {
                DmaStatistical dmaStatistical = dmaStatisticalMap.get(k);
                if (null != dmaStatistical) {
                    dmaStatistical.setId(v);
                }
            });
            if (dmaStatisticalMap.size() > 0) {
                Collection<DmaStatistical> dmaStatisticalList=dmaStatisticalMap.values();
                dmaStatisticalList.forEach(item->{
                    dmaStatisticalService.saveOrUpdate(item);
                });


               // dmaStatisticalService.saveOrUpdateBatch(dmaStatisticalMap.values());
                this.saveIndicatorCorrection(dmaStatisticalMap.values());
                this.saveCustomize(dmaStatisticalMap.values(),reviseDate);
            }
        }
        return true;
    }

    @Override
    public Boolean totalCalculate(Date reviseDate) {
        TotalStatistical statistical = new TotalStatistical();
        // 获取当前月份 用户售水量
        LocalDateTime time = DateTimeUtil.fromLong2LocalDateTime(reviseDate.getTime());
        Date startDate = DateTimeUtil.localDateToDate(DateTimeUtil.getFirstDayOfCurrentSpecifiedMonth(time));
        Date localDateToDate = DateTimeUtil.localDateToDate(DateTimeUtil.getLastDayOfCurrentSpecifiedMonth(time));
        Date totalUserDate = DateTimeUtil.convertLDTToDate(time.plusMonths(1));
        Date endDate = DateTimeUtil.convertLDTToDate(DateTimeUtil.getAssignTodayEndTime(DateTimeUtil.fromLong2LocalDateTime(localDateToDate.getTime())));
        BigDecimal mapperTotalUserWaterConsumption = baseMapper.getTotalUserWaterConsumption(DateTimeUtil.getLocalDate(startDate,DateTimeUtil.MONTHS_YEARS_FORMAT));
        BigDecimal totalUserWaterConsumption = mapperTotalUserWaterConsumption == null ? BigDecimal.ZERO : mapperTotalUserWaterConsumption;
        // 获取当前月份 设备供水量
        String newYearMonth = DateTimeUtil.getLocalDate(reviseDate, DateTimeUtil.MONTHS_FORMAT);
        List<DmaCalculatePramVo> newTotalWaterSupply = baseMapper.getTotalWaterSupply(newYearMonth);
        // 获取当前月份scada统计最小值 设备供水量

        List<DmaCalculatePramVo> lodTotalWaterSupply = baseMapper.getLostMonthTotalWaterSupply(newYearMonth);
        // 处理供水量逻辑
        BigDecimal bigDecimal = getMapperTotalWaterSupply(newTotalWaterSupply, lodTotalWaterSupply);
        BigDecimal totalWaterSupply = bigDecimal == null ? BigDecimal.ZERO : bigDecimal;

        // 获取当前月份1级分区 远传供水量(远传表) 当月开始时间 当月结束时间
        BigDecimal mapperTotalRemoteWaterSupply = baseMapper.getTotalRemoteWaterSupply(startDate, endDate);
        BigDecimal totalRemoteWaterSupply = mapperTotalRemoteWaterSupply == null ? BigDecimal.ZERO : mapperTotalRemoteWaterSupply;
        // 漏点相关数量
        DmaCalculatePramVo totalLeakageGdInfo = baseMapper.getTotalLeakageGdInfo(startDate, endDate);
        BigDecimal leakageVal = totalLeakageGdInfo.getLeakageVal();
        BigDecimal leakageOverVal = totalLeakageGdInfo.getLeakageOverVal();
        // 获取当前月份1级分区 未上盘水量(对应的DMA分区 每月只对应 一条未上盘水量)
        List<DmaUnfilledWater> list = dmaUnfilledWaterService.list(new QueryWrapper<DmaUnfilledWater>().lambda()
                .ge(DmaUnfilledWater::getWriteDate, startDate)
                .le(DmaUnfilledWater::getWriteDate, endDate));
        // 未上盘水量 = 免费用水量+漏失水量+计量漏失水量+其他损失水量+计费用水量
        if (null != list && list.size() > 0) {
            BigDecimal wspsl = BigDecimal.ZERO;
            BigDecimal freeWaterVolume = list.stream().map(DmaUnfilledWater::getFreeWaterVolume).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal outflowWaterVolume = list.stream().map(DmaUnfilledWater::getOutflowWaterVolume).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal lossWaterVolume = list.stream().map(DmaUnfilledWater::getLossWaterVolume).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal otherWaterVolume = list.stream().map(DmaUnfilledWater::getOtherWaterVolume).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
//            BigDecimal billingWaterVolume = list.stream().map(DmaUnfilledWater::getBillingWaterVolume).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);

            wspsl = wspsl.add(freeWaterVolume).add(outflowWaterVolume).add(lossWaterVolume).add(otherWaterVolume);
            BigDecimal notMeasureVolume = list.stream().map(DmaUnfilledWater::getNotMeasureVolume).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal freeMeasureVolume = list.stream().map(DmaUnfilledWater::getFreeMeasureVolume).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal notFreeMeasureVolume = list.stream().map(DmaUnfilledWater::getNotFreeMeasureVolume).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal obviousOutflowVolume = list.stream().map(DmaUnfilledWater::getObviousOutflowVolume).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal darkOutflowVolume = list.stream().map(DmaUnfilledWater::getDarkOutflowVolume).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal backgroundOutflowVolume = list.stream().map(DmaUnfilledWater::getBackgroundOutflowVolume).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal otherOutflowVolume = list.stream().map(DmaUnfilledWater::getOtherOutflowVolume).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal userLossVolume = list.stream().map(DmaUnfilledWater::getUserLossVolume).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal notUserLossVolume = list.stream().map(DmaUnfilledWater::getNotUserLossVolume).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);

            statistical.setNotMeasureVolume(notMeasureVolume);
            statistical.setFreeMeasureVolume(freeMeasureVolume);
            statistical.setNotFreeMeasureVolume(notFreeMeasureVolume);
            statistical.setObviousOutflowVolume(obviousOutflowVolume);
            statistical.setDarkOutflowVolume(darkOutflowVolume);
            statistical.setBackgroundOutflowVolume(backgroundOutflowVolume);
            statistical.setOtherOutflowVolume(otherOutflowVolume);
            statistical.setUserLossVolume(userLossVolume);
            statistical.setNotUserLossVolume(notUserLossVolume);
            statistical.setFreeWaterVolume(freeWaterVolume);
            statistical.setOutflowWaterVolume(outflowWaterVolume);
            statistical.setLossWaterVolume(lossWaterVolume);
            statistical.setOtherWaterVolume(otherWaterVolume);
            statistical.setUnfilledWater(wspsl);
            statistical.setBillingWaterVolume(totalUserWaterConsumption.add(notUserLossVolume));
        } else {
            statistical.setUnfilledWater(BigDecimal.ZERO);
        }
        statistical.setStatisticalDate(startDate);
        statistical.setUserWater(totalUserWaterConsumption);
        statistical.setWaterSupply(totalWaterSupply);
        statistical.setRemoteWater(totalRemoteWaterSupply);
        statistical.setLeakageVal(leakageVal);
        statistical.setLeakageOverVal(leakageOverVal);
        // 供水量 (设备+远传)
        BigDecimal gsl = totalWaterSupply.add(totalRemoteWaterSupply);
        // 修正前漏损率=（供水量-注册用户用水量）/供水量 (修正后同步修改该字段值)
        // 注册用户用水量 = 售水量+计费未计量用水量+免费用水量(免费计量用水量+免费未计量用水量)
        BigDecimal zcysl = totalUserWaterConsumption.add(statistical.getNotMeasureVolume()==null?BigDecimal.ZERO:statistical.getNotMeasureVolume()).add(statistical.getFreeWaterVolume()==null?BigDecimal.ZERO:statistical.getFreeWaterVolume());
        // 产销差（%）=【（总供水量-售水量-计费未计量用水量）/总供水量】×100
        BigDecimal poorProductionRate = BigDecimal.ZERO;
        BigDecimal leakageRate = BigDecimal.ZERO;
        if (BigDecimal.ZERO.compareTo(gsl) != 0) {
            leakageRate = gsl.subtract(zcysl).divide(gsl, 4, BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(100));

            poorProductionRate = gsl.subtract(totalUserWaterConsumption).subtract(statistical.getNotMeasureVolume()==null?BigDecimal.ZERO:statistical.getNotMeasureVolume()).divide(gsl, 4, BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(100));
        }
        statistical.setLeakageRate(leakageRate);
        statistical.setPoorProductionRate(poorProductionRate);
        // 巡检效率相关统计
        BigDecimal totalPlanPipeLength = baseMapper.getTotalPlanPipeLength(startDate, endDate);
        BigDecimal planTotalDistanceLength = baseMapper.getPlanTotalDistanceLength(startDate, endDate);
        // 巡检效率相关统计
        statistical.setTotalPlanPipeLength(totalPlanPipeLength);
        statistical.setPlanTotalDistanceLength(planTotalDistanceLength);
        // 对应 统计时间 相同 则更新 其余为新增
        List<TotalStatistical> totalStatisticals = totalStatisticalService.list(new QueryWrapper<TotalStatistical>().lambda()
                .eq(TotalStatistical::getStatisticalDate, startDate));
        if (null != totalStatisticals && totalStatisticals.size() > 0) {
            statistical.setId(totalStatisticals.get(0).getId());
        }

        return totalStatisticalService.saveOrUpdate(statistical);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveInfo(IndicatorCorrection indicatorCorrection, String flag) {
        Dmaregion dmaregion = dmaregionService.getById(indicatorCorrection.getDmaId());
        Integer partitionLevel = dmaregion.getPartitionLevel();
        indicatorCorrection.setDmaCode(dmaregion.getDmaCode());
        indicatorCorrection.setDmaName(dmaregion.getDmaName());
        indicatorCorrection.setDmaLevel(partitionLevel);
        // 判断当前月份是否存在,存在则更新,不存在则新增
        Long dmaId = indicatorCorrection.getDmaId();
        Date reviseDate = indicatorCorrection.getReviseDate();
        if(null== SecurityUtils.getUser()){
            indicatorCorrection.setCreateUser(0L);
            indicatorCorrection.setUpdateUser(0L);
        }
        IndicatorCorrection one = this.getOne(new QueryWrapper<IndicatorCorrection>().lambda().eq(IndicatorCorrection::getDmaId, dmaId).eq(IndicatorCorrection::getReviseDate, reviseDate));
        if (null != one) {
            indicatorCorrection.setId(one.getId());
        }

        List<DmaStatistical> list = dmaStatisticalService.list(new QueryWrapper<DmaStatistical>().lambda().eq(DmaStatistical::getStatisticalDate, reviseDate).eq(DmaStatistical::getDmaId, dmaId));
        if (null != list && list.size() > 0) {
            DmaStatistical dmaStatistical = list.get(0);
            BigDecimal r1 = getR1(indicatorCorrection.getJmcbZgsBl(), dmaStatistical);
            BigDecimal waterSupply = dmaStatistical.getWaterSupply();
            if (waterSupply.compareTo(BigDecimal.ZERO) > 0) {
                indicatorCorrection.setPipeLength(dmaStatistical.getDnLengthVal().divide(waterSupply, 4, BigDecimal.ROUND_HALF_UP));
            }

            BigDecimal r2 = getR2(dmaStatistical);
            BigDecimal r3 = getR3(dmaStatistical.getYearPressureVal());
            BigDecimal leakageRate = dmaStatistical.getLeakageRate();
            // 漏损率=修正前漏损率-(R1+R2+R3+R4)
            BigDecimal subtrahend = r1.add(r2).add(r3);
            BigDecimal correctLeakage = leakageRate.subtract(subtrahend);
            indicatorCorrection.setJmcbXzz(r1);
            indicatorCorrection.setDwgsXzz(r2);
            indicatorCorrection.setNpjylXzz(r3);
            indicatorCorrection.setDifferenceLeak(subtrahend);
            indicatorCorrection.setBeforLeak(leakageRate);
            if (StringUtil.isEmpty(flag)) {
                indicatorCorrection.setAfterLeak(correctLeakage);
            }

            indicatorCorrection.setDnLengthVal(dmaStatistical.getDnLengthVal());
            indicatorCorrection.setWaterSupply(waterSupply);
            if (StringUtil.isEmpty(flag)) {
                // 修改统计表各自DMA分区漏损率
                dmaStatistical.setLeakageRate(correctLeakage);
                dmaStatisticalService.updateById(dmaStatistical);
                // 判断是否为1级分区 若为一级分区修改总统计指标表漏损率
                if (1 == partitionLevel) {
                    TotalStatistical totalStatistical = totalStatisticalService.getOne(new QueryWrapper<TotalStatistical>().lambda().eq(TotalStatistical::getStatisticalDate, reviseDate));
                    if (null != totalStatistical) {
                        // 获取统计指标表中所有一级分区的漏损率 求和(除当前修正dma分区漏损率)
                        List<DmaStatistical> statisticalList = dmaStatisticalService.list(new QueryWrapper<DmaStatistical>().lambda()
                                .eq(DmaStatistical::getStatisticalDate, reviseDate)
                                .ne(DmaStatistical::getDmaId, dmaId));
                        if (null != statisticalList && statisticalList.size() > 0) {
                            BigDecimal leakTotal = statisticalList.stream()
                                    .map(DmaStatistical::getLeakageRate).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
                            BigDecimal add = leakTotal.add(correctLeakage);
                            BigDecimal leakNew = add.divide(BigDecimal.valueOf(statisticalList.size() + 1), 2, BigDecimal.ROUND_HALF_UP);
                            totalStatistical.setLeakageRate(leakNew);
                        } else {
                            totalStatistical.setLeakageRate(correctLeakage);
                        }
                        totalStatisticalService.updateById(totalStatistical);
                    }
                }
            }

        }

        return this.saveOrUpdate(indicatorCorrection);
    }


    /**
     * 处理每个分区的供水量计算
     *
     * @param supplyList
     * @param lodTotalWaterSupply
     * @return
     */
    private Map<Long, BigDecimal> getWaterSupplyMap(List<DmaCalculatePramVo> supplyList, List<DmaCalculatePramVo> lodTotalWaterSupply) {
        Map<Long, BigDecimal> map = new HashMap<>(12);
        // dma分组
        Map<Long, List<DmaCalculatePramVo>> collect = supplyList.stream().collect(Collectors.groupingBy(DmaCalculatePramVo::getDmaId));
        // 上月dma分组
        Map<Long, List<DmaCalculatePramVo>> lodCollect = lodTotalWaterSupply.stream().collect(Collectors.groupingBy(DmaCalculatePramVo::getDmaId));
        collect.forEach((k, v) -> {

            // 存储 返回供水量/入口供水量/出口供水量
            AtomicReference<BigDecimal> value = new AtomicReference<>(BigDecimal.ZERO);
            AtomicReference<BigDecimal> enterValue = new AtomicReference<>(BigDecimal.ZERO);
            AtomicReference<BigDecimal> outValue = new AtomicReference<>(BigDecimal.ZERO);

            List<DmaCalculatePramVo> lodList = lodCollect.get(k) == null ? new ArrayList<>() : lodCollect.get(k);
            // 按照出入口分组
            Map<String, List<DmaCalculatePramVo>> newIsOutList = v.stream().collect(Collectors.groupingBy(DmaCalculatePramVo::getIsOut));
            Map<String, List<DmaCalculatePramVo>> lodIsOutList = lodList.stream().collect(Collectors.groupingBy(DmaCalculatePramVo::getIsOut));
            // 本月 与 上月 入口/出口 相减
            if (null != newIsOutList) {
                newIsOutList.forEach((z, m) -> {
                    List<DmaCalculatePramVo> dmaCalculatePramVos = lodIsOutList.get(z) == null ? new ArrayList<>() : lodIsOutList.get(z);
                    BigDecimal bigDecimal1 = m.stream().map(DmaCalculatePramVo::getWaterSupply).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
                    BigDecimal reduce = bigDecimal1 == null ? BigDecimal.ZERO : bigDecimal1;
                    BigDecimal bigDecimal2 = dmaCalculatePramVos.stream().map(DmaCalculatePramVo::getWaterSupply).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
                    BigDecimal decimal = bigDecimal2 == null ? BigDecimal.ZERO : bigDecimal2;
                    if ("0".equals(z)) {
                        enterValue.set(enterValue.get().add(reduce).subtract(decimal));
                    } else {
                        outValue.set(outValue.get().add(reduce).subtract(decimal));
                    }
                });
            }

            // 当前月内出入口相减
            value.set(enterValue.get().subtract(outValue.get()));
            map.put(k, value.get());
        });
        return map;
    }

    /**
     * 处理所有供水量计算
     *
     * @param supplyList
     * @param lodTotalWaterSupply
     * @return
     */
    private BigDecimal getMapperTotalWaterSupply(List<DmaCalculatePramVo> supplyList, List<DmaCalculatePramVo> lodTotalWaterSupply) {
        // 存储 返回供水量/入口供水量/出口供水量
        AtomicReference<BigDecimal> value = new AtomicReference<>(BigDecimal.ZERO);
        AtomicReference<BigDecimal> enterValue = new AtomicReference<>(BigDecimal.ZERO);
        AtomicReference<BigDecimal> outValue = new AtomicReference<>(BigDecimal.ZERO);

        // 按照出入口分组
        Map<String, List<DmaCalculatePramVo>> newIsOutList = supplyList.stream().collect(Collectors.groupingBy(DmaCalculatePramVo::getIsOut));
        Map<String, List<DmaCalculatePramVo>> lodIsOutList = lodTotalWaterSupply.stream().collect(Collectors.groupingBy(DmaCalculatePramVo::getIsOut));
        // 本月 与 上月 入口/出口 相减
        if (null != newIsOutList) {
            newIsOutList.forEach((z, m) -> {
                List<DmaCalculatePramVo> dmaCalculatePramVos = lodIsOutList.get(z) == null ? new ArrayList<>() : lodIsOutList.get(z);
                BigDecimal bigDecimal1 = m.stream().map(DmaCalculatePramVo::getWaterSupply).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
                BigDecimal reduce = bigDecimal1 == null ? BigDecimal.ZERO : bigDecimal1;
                BigDecimal bigDecimal2 = dmaCalculatePramVos.stream().map(DmaCalculatePramVo::getWaterSupply).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
                BigDecimal decimal = bigDecimal2 == null ? BigDecimal.ZERO : bigDecimal2;
                if ("0".equals(z)) {
                    enterValue.set(enterValue.get().add(reduce).subtract(decimal));
                } else {
                    outValue.set(outValue.get().add(reduce).subtract(decimal));
                }
            });
        }

        // 当前月内出入口相减
        value.set(enterValue.get().subtract(outValue.get()));


        return value.get();
    }

    /**
     * ①居民抄表到户水量的修正值应按下式计算：
     * R1＝0．08r×100％（2.9
     * 式中：R1——居民抄表到户水量的修正值（％）
     * r——居民抄表到户水量占总供水量比例。
     *
     * @param jmcbZgsBl      居民抄表到户水量的修正值
     * @param dmaStatistical 对应月份统计的指标参数对象
     * @return
     */
    private BigDecimal getR1(BigDecimal jmcbZgsBl, DmaStatistical dmaStatistical) {
        BigDecimal r1 = BigDecimal.ZERO;
        // 判断前端是否传入 居民抄表到户水量占总供水量比例
        if (null == jmcbZgsBl) {
            // 用户用水量 供水量获取
            BigDecimal userWater = dmaStatistical.getUserWater();
            BigDecimal waterSupply = dmaStatistical.getWaterSupply();
            if (userWater.compareTo(BigDecimal.ZERO) > 0 && waterSupply.compareTo(BigDecimal.ZERO)>0) {
                BigDecimal r = userWater.divide(waterSupply, 4, BigDecimal.ROUND_HALF_UP);
                r1 = r.multiply(BigDecimal.valueOf(0.08)).multiply(BigDecimal.valueOf(100));
            }

        } else {
            r1 = BigDecimal.valueOf(0.08).multiply(jmcbZgsBl).multiply(BigDecimal.valueOf(100)).setScale(2, BigDecimal.ROUND_HALF_UP);
        }
        return r1;
    }

    /**
     * 获取R2计算值
     * ②单位供水量管长的修正值应按下列公式计算：
     * R2＝0．99（A—0．0693）×100％（2.9.8-2）
     * （2.9.8-3）
     * 式中：R2——单位供水量管长的修正值（％）；
     * A——单位供水量管长（km／万m3）；
     * L——DN75（含）以上管道长度（km）;
     * Qs——总供水量（m3）
     * 当R2值大于3％时，应取3％；当R2值小于-3％时，应取-3％
     *
     * @param dmaStatistical 对应月份统计的指标参数对象
     * @return
     */
    private BigDecimal getR2(DmaStatistical dmaStatistical) {
        BigDecimal r2 = BigDecimal.ZERO;
        // 管网长度 供水量 获取
        BigDecimal dnLength = dmaStatistical.getDnLengthVal();
        BigDecimal waterSupply = dmaStatistical.getWaterSupply();
        if (waterSupply.compareTo(BigDecimal.ZERO) > 0) {
            r2 = dnLength.divide(waterSupply, 4, BigDecimal.ROUND_HALF_UP)
                    .subtract(BigDecimal.valueOf(0.0693))
                    .multiply(BigDecimal.valueOf(0.99))
                    .multiply(BigDecimal.valueOf(100));
        }
        BigDecimal up = new BigDecimal(3);
        BigDecimal dow = new BigDecimal(-3);
        if (r2.compareTo(up) > 0) {
            r2 = up;
        }
        if (dow.compareTo(r2) > 0) {
            r2 = dow;
        }
        return r2;
    }

    /**
     * ③年平均出厂压力修正
     * 年平均出厂压力大于0.35MPa且小于或等于0.55MPa时，修正值应为0.5％；
     * 年平均出厂压力大于0.55MPa且小于或等于0.75MPa时，修正值应为1％；
     * 年平均出厂压力大于0.75MPa时，修正值应为2％。
     *
     * @param dmaYearPressure 年平均压力
     * @return
     */
    private BigDecimal getR3(BigDecimal dmaYearPressure) {
        //   ③年平均出厂压力修正
        //  年平均出厂压力大于0.35MPa且小于或等于0.55MPa时，修正值应为0.5％；
        //  年平均出厂压力大于0.55MPa且小于或等于0.75MPa时，修正值应为1％；
        //  年平均出厂压力大于0.75MPa时，修正值应为2％。
        BigDecimal r3 = BigDecimal.ZERO;
        if (dmaYearPressure.compareTo(BigDecimal.valueOf(0.75)) > 0) {
            r3 = BigDecimal.valueOf(2);
        }
        if (dmaYearPressure.compareTo(BigDecimal.valueOf(0.55)) > 0 && dmaYearPressure.compareTo(BigDecimal.valueOf(0.75)) <= 0) {
            r3 = BigDecimal.valueOf(1);
        }
        if (dmaYearPressure.compareTo(BigDecimal.valueOf(0.35)) > 0 && dmaYearPressure.compareTo(BigDecimal.valueOf(0.55)) <= 0) {
            r3 = BigDecimal.valueOf(0.5);
        }
        return r3;
    }

    /**
     * 每日统计指标->触发国标指标修正批量新增/修改
     *
     * @param dmaStatisticalList
     */
    public void saveIndicatorCorrection(Collection<DmaStatistical> dmaStatisticalList) {
        dmaStatisticalList.forEach(info -> {
            IndicatorCorrection correction = new IndicatorCorrection();
            Long dmaId = info.getDmaId();
            Date statisticalDate = info.getStatisticalDate();
            correction.setDmaId(dmaId);
            correction.setReviseDate(statisticalDate);
            saveInfo(correction,"1");
        });
    }

    /**
     * 每日统计指标->触发自定义指标修正批量新增/修改
     * @param dmaStatisticalList
     * @param reviseDate
     */
    public void saveCustomize(Collection<DmaStatistical> dmaStatisticalList,Date reviseDate){
        CustomizeDto dto = new CustomizeDto();
        StringBuilder stringBuilder = new StringBuilder("");
        dmaStatisticalList.forEach(info->{
            stringBuilder.append(info.getDmaId()).append(Constant.CSV);
        });
        dto.setDmaIds(stringBuilder.deleteCharAt(stringBuilder.length() - 1).toString());
        dto.setMonths(DateTimeUtil.getLocalDate(reviseDate,DateTimeUtil.DAY_FORMAT));
        dto.setLeakCoefficient(BigDecimal.ONE);
        dto.setPoorProductionCoefficient(BigDecimal.ONE);
        customizeService.saveInfo(dto,"1");
    }

}
