package cn.cecep.talroad.service.impl.analysis;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import cn.cecep.talroad.domain.SEnvGasMonHour;
import cn.cecep.talroad.domain.SEnvWaterMonHour;
import cn.cecep.talroad.domain.analysis.SRaMultiSource;
import cn.cecep.talroad.domain.analysis.SRaWaterPermittedOnline;
import cn.cecep.talroad.mapper.analysis.SRaMultiSourceMapper;
import cn.cecep.talroad.service.IPcOutletService;
import cn.cecep.talroad.service.ISEnvGasMonHourService;
import cn.cecep.talroad.service.ISEnvWaterMonHourService;
import cn.cecep.talroad.service.analysis.ISRaMultiSourceService;
import cn.cecep.talroad.vo.query.analysis.SRaMultiSourceQuery;
import cn.cecep.talroad.vo.query.analysis.SRaWaterPermittedOnlineQuery;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;




/**
 * 多源排放量汇总Service业务层处理
 *
 * @author szhpt
 * @date 2023-06-03*/
@Service
public class SRaMultiSourceServiceImpl extends ServiceImpl<SRaMultiSourceMapper, SRaMultiSource> implements ISRaMultiSourceService
{
    @Autowired
    private SRaMultiSourceMapper sRaMultiSourceMapper;
    @Autowired
    private IPcOutletService iPcOutletService;
    @Autowired
    private ISEnvWaterMonHourService isEnvWaterMonHourService;
    @Autowired
    private ISEnvGasMonHourService isEnvGasMonHourService;
    /**
     * 查询多源排放量汇总列表
     *
     * @param lambdaQueryWrapper 多源排放量汇总
     * @return 多源排放量汇总
     */
    @Override
    public List<SRaWaterPermittedOnline> selectSRaMultiSourceList(SRaWaterPermittedOnlineQuery lambdaQueryWrapper)
    {
        if(StringUtils.isBlank(lambdaQueryWrapper.getInvalidDataCalculated())){
            lambdaQueryWrapper.setInvalidDataCalculated("1");
        }
        Page<SRaWaterPermittedOnline> page = new Page<>(1, 999999);

        List<SRaWaterPermittedOnline> sRaWaterPermittedOnlines= iPcOutletService.selectBySource(page,lambdaQueryWrapper);
        List<String> stringList=new ArrayList<>();
        sRaWaterPermittedOnlines.forEach(item->{
            stringList.add(item.getFactoryId());
        });
        lambdaQueryWrapper.setMonitors(stringList);
        /*-------------------------------------在线排放量-------------------------------------------------*/
        lambdaQueryWrapper.setType("1");
        List<SEnvGasMonHour> gasMonHours=isEnvGasMonHourService.selectSRaWaterSourceListBy(lambdaQueryWrapper);
        //处理修约量，如果为空则填充排放量
        gasMonHours.forEach(item->{
            if(StringUtils.isEmpty(item.getSo2CorrectEmission())){
                item.setSo2CorrectEmission(item.getSo2Emission());
            }
            if(StringUtils.isEmpty(item.getNoxCorrectEmission())){
                item.setNoxCorrectEmission(item.getNoxEmission());
            }
            if(StringUtils.isEmpty(item.getVocsCorrectEmission())){
                item.setVocsCorrectEmission(item.getVocsEmission());
            }
            if(StringUtils.isEmpty(item.getSmokeDustCorrectEmission())){
                item.setSmokeDustCorrectEmission(item.getPmEmission());
            }
        });
        Map<String, List<SEnvGasMonHour>> stringListMap = gasMonHours.stream().collect(Collectors.groupingBy( SEnvGasMonHour::getFactoryId));
        sRaWaterPermittedOnlines.forEach(item->{
            if(stringListMap.containsKey(item.getFactoryId())){
                List<SEnvGasMonHour> sEnvWaterMonHours = stringListMap.get(item.getFactoryId());
                if(lambdaQueryWrapper.getInvalidDataCalculated().equals("1")){
                    item.setSo2(NumberUtil.round(sEnvWaterMonHours.stream().filter(o ->{
                        return dataFlagConversion(o.getSo2CorrectEmission(),lambdaQueryWrapper.getDataFlag(),o.getSo2Flag(), o.getDataFlag());
                    }).mapToDouble(op->new BigDecimal(op.getSo2CorrectEmission()).doubleValue()).sum(),4));
                    item.setNox(NumberUtil.round(sEnvWaterMonHours.stream().filter(o ->{
                        return dataFlagConversion(o.getNoxCorrectEmission(),lambdaQueryWrapper.getDataFlag(),o.getNoxFlag(), o.getDataFlag());
                    }).mapToDouble(op->new BigDecimal(op.getNoxCorrectEmission()).doubleValue()).sum(),4));
                    item.setVocs(NumberUtil.round(sEnvWaterMonHours.stream().filter(o ->{
                        return dataFlagConversion(o.getVocsCorrectEmission(),lambdaQueryWrapper.getDataFlag(),o.getVocsFlag(), o.getDataFlag());
                    }).mapToDouble(op->new BigDecimal(op.getVocsCorrectEmission()).doubleValue()).sum(),4));
                    item.setPm(NumberUtil.round(sEnvWaterMonHours.stream().filter(o ->{
                        return dataFlagConversion(o.getSmokeDustCorrectEmission(),lambdaQueryWrapper.getDataFlag(),o.getPmFlag(), o.getDataFlag());
                    }).mapToDouble(op->new BigDecimal(op.getSmokeDustCorrectEmission()).doubleValue()).sum(),4));
                }else {
                    item.setSo2(NumberUtil.round(sEnvWaterMonHours.stream().filter(o ->{
                        return dataFlagConversion(o.getSo2Emission(),lambdaQueryWrapper.getDataFlag(),o.getSo2Flag(), o.getDataFlag());
                    }).mapToDouble(op->new BigDecimal(op.getSo2Emission()).doubleValue()).sum(),4));
                    item.setNox(NumberUtil.round(sEnvWaterMonHours.stream().filter(o ->{
                        return dataFlagConversion(o.getNoxEmission(),lambdaQueryWrapper.getDataFlag(),o.getNoxFlag(), o.getDataFlag());
                    }).mapToDouble(op->new BigDecimal(op.getNoxEmission()).doubleValue()).sum(),4));
                    item.setVocs(NumberUtil.round(sEnvWaterMonHours.stream().filter(o ->{
                        return dataFlagConversion(o.getVocsEmission(),lambdaQueryWrapper.getDataFlag(),o.getVocsFlag(), o.getDataFlag());
                    }).mapToDouble(op->new BigDecimal(op.getVocsEmission()).doubleValue()).sum(),4));
                    item.setPm(NumberUtil.round(sEnvWaterMonHours.stream().filter(o ->{
                        return dataFlagConversion(o.getPmEmission(),lambdaQueryWrapper.getDataFlag(),o.getPmFlag(), o.getDataFlag());
                    }).mapToDouble(op->new BigDecimal(op.getPmEmission()).doubleValue()).sum(),4));
                }
            }
        });

        List<SEnvWaterMonHour> waterMonHours=isEnvWaterMonHourService.selectSRaWaterSourceListBy(lambdaQueryWrapper);
        //处理修约量，如果为空则填充排放量
        waterMonHours.forEach(item->{
            if(StringUtils.isEmpty(item.getAmmoniaCorrectEmission())){
                item.setAmmoniaCorrectEmission(item.getAmmoniaNitrogenEmission());
            }
            if(StringUtils.isEmpty(item.getCodCorrectEmission())){
                item.setCodCorrectEmission(item.getCodEmission());
            }
            if(StringUtils.isEmpty(item.getNitrogenCorrectEmission())){
                item.setNitrogenCorrectEmission(item.getNitrogenConcentrationEmission());
            }
            if(StringUtils.isEmpty(item.getPhosphorusCorrectEmission())){
                item.setPhosphorusCorrectEmission(item.getPhosphorusConcentrationEmission());
            }
        });
        Map<String, List<SEnvWaterMonHour>> stringListMaps = waterMonHours.stream().collect(Collectors.groupingBy( SEnvWaterMonHour::getFactoryId));
        sRaWaterPermittedOnlines.forEach(item->{
            if(stringListMaps.containsKey(item.getFactoryId())){
                List<SEnvWaterMonHour> sEnvWaterMonHours = stringListMaps.get(item.getFactoryId());
                if(lambdaQueryWrapper.getInvalidDataCalculated().equals("1")){
                    item.setAmmonia(NumberUtil.round(sEnvWaterMonHours.stream().filter(o ->{
                        return dataFlagConversion(o.getAmmoniaCorrectEmission(),lambdaQueryWrapper.getDataFlag(),o.getAmmoniaNitrogenFlag(), o.getDataFlag());
                    }).mapToDouble(op->new BigDecimal(op.getAmmoniaCorrectEmission()).doubleValue()).sum(),4));
                    item.setCod(NumberUtil.round(sEnvWaterMonHours.stream().filter(o ->{
                        return dataFlagConversion(o.getCodCorrectEmission(),lambdaQueryWrapper.getDataFlag(),o.getCodFlag(), o.getDataFlag());
                    }).mapToDouble(op->new BigDecimal(op.getCodCorrectEmission()).doubleValue()).sum(),4));
                    item.setNitrogen(NumberUtil.round(sEnvWaterMonHours.stream().filter(o ->{
                        return dataFlagConversion(o.getNitrogenCorrectEmission(),lambdaQueryWrapper.getDataFlag(),o.getNitrogenConcentrationFlag(), o.getDataFlag());
                    }).mapToDouble(op->new BigDecimal(op.getNitrogenCorrectEmission()).doubleValue()).sum(),4));
                    item.setPhosphorus(NumberUtil.round(sEnvWaterMonHours.stream().filter(o ->{
                        return dataFlagConversion(o.getPhosphorusCorrectEmission(),lambdaQueryWrapper.getDataFlag(),o.getPhosphorusConcentrationFlag(), o.getDataFlag());
                    }).mapToDouble(op->new BigDecimal(op.getPhosphorusCorrectEmission()).doubleValue()).sum(),4));
                }else {
                    item.setAmmonia(NumberUtil.round(sEnvWaterMonHours.stream().filter(o ->{
                        return dataFlagConversion(o.getAmmoniaNitrogenEmission(),lambdaQueryWrapper.getDataFlag(),o.getAmmoniaNitrogenFlag(), o.getDataFlag());
                    }).mapToDouble(op->new BigDecimal(op.getAmmoniaNitrogenEmission()).doubleValue()).sum(),4));
                    item.setCod(NumberUtil.round(sEnvWaterMonHours.stream().filter(o ->{
                        return dataFlagConversion(o.getCodEmission(),lambdaQueryWrapper.getDataFlag(),o.getCodFlag(), o.getDataFlag());
                    }).mapToDouble(op->new BigDecimal(op.getCodEmission()).doubleValue()).sum(),4));
                    item.setNitrogen(NumberUtil.round(sEnvWaterMonHours.stream().filter(o ->{
                        return dataFlagConversion(o.getNitrogenConcentrationEmission(),lambdaQueryWrapper.getDataFlag(),o.getNitrogenConcentrationFlag(), o.getDataFlag());
                    }).mapToDouble(op->new BigDecimal(op.getNitrogenConcentrationEmission()).doubleValue()).sum(),4));
                    item.setPhosphorus(NumberUtil.round(sEnvWaterMonHours.stream().filter(o ->{
                        return dataFlagConversion(o.getPhosphorusConcentrationEmission(),lambdaQueryWrapper.getDataFlag(),o.getPhosphorusConcentrationFlag(), o.getDataFlag());
                    }).mapToDouble(op->new BigDecimal(op.getPhosphorusConcentrationEmission()).doubleValue()).sum(),4));
                }
            }
        });
        /*-------------------------------------许可排放量-------------------------------------------------*/
        lambdaQueryWrapper.setType("2");
        List<SEnvGasMonHour> gasMonHoursPermit=new ArrayList<>();
        //自动监测
        gasMonHoursPermit.addAll(isEnvGasMonHourService.selectSRaWaterSourceListBy(lambdaQueryWrapper));
        //手工检测
        gasMonHoursPermit.addAll(isEnvGasMonHourService.selectManualSRaWaterSourceListBy(lambdaQueryWrapper));
        //处理修约量，如果为空则填充排放量
        gasMonHoursPermit.forEach(item->{
            if(StringUtils.isEmpty(item.getSo2CorrectEmission())){
                item.setSo2CorrectEmission(item.getSo2Emission());
            }
            if(StringUtils.isEmpty(item.getNoxCorrectEmission())){
                item.setNoxCorrectEmission(item.getNoxEmission());
            }
            if(StringUtils.isEmpty(item.getVocsCorrectEmission())){
                item.setVocsCorrectEmission(item.getVocsEmission());
            }
            if(StringUtils.isEmpty(item.getSmokeDustCorrectEmission())){
                item.setSmokeDustCorrectEmission(item.getPmEmission());
            }
        });
        /**/
        Map<String, Map<String,List<SEnvGasMonHour>>> stringListMapPermit = gasMonHoursPermit.stream().collect(Collectors.groupingBy( SEnvGasMonHour::getFactoryId,Collectors.groupingBy(SEnvGasMonHour::getOutletId)));
        Set<String> setOutletIds =new HashSet<String>();
        stringListMapPermit.forEach((key,value)->{setOutletIds.addAll(value.keySet());});
        List<SRaWaterPermittedOnline> gasMonHours1=isEnvGasMonHourService.selectSRaWaterSourceListByPoll(setOutletIds);
        Map<String, List<SRaWaterPermittedOnline>> stringListMapsPermit = gasMonHours1.stream().collect(Collectors.groupingBy( SRaWaterPermittedOnline::getMonitorId));
        /**/
        sRaWaterPermittedOnlines.forEach(item->{
            if(stringListMapPermit.containsKey(item.getFactoryId())){
                /**/
                Map<String,List<SEnvGasMonHour>> sEnvWaterMonHour = stringListMapPermit.get(item.getFactoryId());
                sEnvWaterMonHour.forEach((key,sEnvWaterMonHours)->{
                    /**/
                    String  sourcePermissionPoll =stringListMapsPermit.get(key).get(0).getSourcePermissionPoll();
                    if(lambdaQueryWrapper.getInvalidDataCalculated().equals("1")){
                        if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&sourcePermissionPoll.contains("A21026"))) {
                            if(ObjectUtil.isNull(item.getSo2Amendments())){
                                item.setSo2Amendments(new BigDecimal(0));
                            }
                            item.setSo2Amendments(item.getSo2Amendments().add(NumberUtil.round(sEnvWaterMonHours.stream().filter(o -> {
                                return dataFlagConversion(o.getSo2CorrectEmission(), lambdaQueryWrapper.getDataFlag(), o.getSo2Flag(), o.getDataFlag());
                            }).mapToDouble(op -> new BigDecimal(op.getSo2CorrectEmission()).doubleValue()).sum(), 4)));
                        }
                        if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&sourcePermissionPoll.contains("A21002"))) {
                            if(ObjectUtil.isNull(item.getNoxAmendments())){
                                item.setNoxAmendments(new BigDecimal(0));
                            }
                            item.setNoxAmendments(item.getNoxAmendments().add(NumberUtil.round(sEnvWaterMonHours.stream().filter(o -> {
                                return dataFlagConversion(o.getNoxCorrectEmission(), lambdaQueryWrapper.getDataFlag(), o.getNoxFlag(), o.getDataFlag());
                            }).mapToDouble(op -> new BigDecimal(op.getNoxCorrectEmission()).doubleValue()).sum(), 4)));
                        }
                        if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&sourcePermissionPoll.contains("A99901"))) {
                            if(ObjectUtil.isNull(item.getVocsAmendments())){
                                item.setVocsAmendments(new BigDecimal(0));
                            }
                            item.setVocsAmendments(item.getVocsAmendments().add(NumberUtil.round(sEnvWaterMonHours.stream().filter(o -> {
                                return dataFlagConversion(o.getVocsCorrectEmission(), lambdaQueryWrapper.getDataFlag(), o.getVocsFlag(), o.getDataFlag());
                            }).mapToDouble(op -> new BigDecimal(op.getVocsCorrectEmission()).doubleValue()).sum(), 4)));
                        }
                        if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&sourcePermissionPoll.contains("A99911"))) {
                            if(ObjectUtil.isNull(item.getPmAmendments())){
                                item.setPmAmendments(new BigDecimal(0));
                            }
                            item.setPmAmendments(item.getPmAmendments().add(NumberUtil.round(sEnvWaterMonHours.stream().filter(o -> {
                                return dataFlagConversion(o.getSmokeDustCorrectEmission(), lambdaQueryWrapper.getDataFlag(), o.getPmFlag(), o.getDataFlag());
                            }).mapToDouble(op -> new BigDecimal(op.getSmokeDustCorrectEmission()).doubleValue()).sum(), 4)));
                        }
                    }else {
                        if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&sourcePermissionPoll.contains("A21026"))) {
                            if(ObjectUtil.isNull(item.getSo2Amendments())){
                                item.setSo2Amendments(new BigDecimal(0));
                            }
                            item.setSo2Amendments(item.getSo2Amendments().add(NumberUtil.round(sEnvWaterMonHours.stream().filter(o -> {
                                return dataFlagConversion(o.getSo2Emission(), lambdaQueryWrapper.getDataFlag(), o.getSo2Flag(), o.getDataFlag());
                            }).mapToDouble(op -> new BigDecimal(op.getSo2Emission()).doubleValue()).sum(), 4)));
                        }
                        if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&sourcePermissionPoll.contains("A21002"))) {
                            if(ObjectUtil.isNull(item.getNoxAmendments())){
                                item.setNoxAmendments(new BigDecimal(0));
                            }
                            item.setNoxAmendments(item.getNoxAmendments().add(NumberUtil.round(sEnvWaterMonHours.stream().filter(o -> {
                                return dataFlagConversion(o.getNoxEmission(), lambdaQueryWrapper.getDataFlag(), o.getNoxFlag(), o.getDataFlag());
                            }).mapToDouble(op -> new BigDecimal(op.getNoxEmission()).doubleValue()).sum(), 4)));
                        }
                        if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&sourcePermissionPoll.contains("A99901"))) {
                            if(ObjectUtil.isNull(item.getVocsAmendments())){
                                item.setVocsAmendments(new BigDecimal(0));
                            }
                            item.setVocsAmendments(item.getVocsAmendments().add(NumberUtil.round(sEnvWaterMonHours.stream().filter(o -> {
                                return dataFlagConversion(o.getVocsEmission(), lambdaQueryWrapper.getDataFlag(), o.getVocsFlag(), o.getDataFlag());
                            }).mapToDouble(op -> new BigDecimal(op.getVocsEmission()).doubleValue()).sum(), 4)));
                        }
                        if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&sourcePermissionPoll.contains("A99911"))) {
                            if(ObjectUtil.isNull(item.getPmAmendments())){
                                item.setPmAmendments(new BigDecimal(0));
                            }
                            item.setPmAmendments(item.getPmAmendments().add(NumberUtil.round(sEnvWaterMonHours.stream().filter(o -> {
                                return dataFlagConversion(o.getPmEmission(), lambdaQueryWrapper.getDataFlag(), o.getPmFlag(), o.getDataFlag());
                            }).mapToDouble(op -> new BigDecimal(op.getPmEmission()).doubleValue()).sum(), 4)));
                        }
                    }
                });
            }
        });
        List<SEnvWaterMonHour> waterMonHoursPermit =new ArrayList<>();
        //自动监测
        waterMonHoursPermit.addAll(isEnvWaterMonHourService.selectSRaWaterSourceListBy(lambdaQueryWrapper));
        //手工检测
        waterMonHoursPermit.addAll(isEnvWaterMonHourService.selectManualSRaWaterSourceListBy(lambdaQueryWrapper));
        //处理修约量，如果为空则填充排放量
        waterMonHoursPermit.forEach(item->{
            if(StringUtils.isEmpty(item.getAmmoniaCorrectEmission())){
                item.setAmmoniaCorrectEmission(item.getAmmoniaNitrogenEmission());
            }
            if(StringUtils.isEmpty(item.getCodCorrectEmission())){
                item.setCodCorrectEmission(item.getCodEmission());
            }
            if(StringUtils.isEmpty(item.getNitrogenCorrectEmission())){
                item.setNitrogenCorrectEmission(item.getNitrogenConcentrationEmission());
            }
            if(StringUtils.isEmpty(item.getPhosphorusCorrectEmission())){
                item.setPhosphorusCorrectEmission(item.getPhosphorusConcentrationEmission());
            }
        });
        /**/
        Map<String, Map<String,List<SEnvWaterMonHour>>> stringListWaterMapsPermits = waterMonHoursPermit.stream().collect(Collectors.groupingBy( SEnvWaterMonHour::getFactoryId,Collectors.groupingBy(SEnvWaterMonHour::getOutletId)));
        Set<String> setOutletIdWaters =new HashSet<String>();
        stringListWaterMapsPermits.forEach((key,value)->{setOutletIdWaters.addAll(value.keySet());});
        List<SRaWaterPermittedOnline> waterMonHours1=isEnvGasMonHourService.selectSRaWaterSourceListByPoll(setOutletIdWaters);
        Map<String, List<SRaWaterPermittedOnline>> stringListWaterMapsPermit = waterMonHours1.stream().collect(Collectors.groupingBy( SRaWaterPermittedOnline::getMonitorId));
        /**/
//        Map<String, List<SEnvWaterMonHour>> stringListMapsPermit = waterMonHoursPermit.stream().collect(Collectors.groupingBy( SEnvWaterMonHour::getFactoryId));
        sRaWaterPermittedOnlines.forEach(item->{
            if(stringListWaterMapsPermits.containsKey(item.getFactoryId())){
                Map<String,List<SEnvWaterMonHour>> sEnvWaterMonHouras = stringListWaterMapsPermits.get(item.getFactoryId());
                sEnvWaterMonHouras.forEach((key,sEnvWaterMonHours)->{
                    String  sourcePermissionPoll =stringListWaterMapsPermit.get(key).get(0).getSourcePermissionPoll();
                    if(lambdaQueryWrapper.getInvalidDataCalculated().equals("1")){
                        if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&sourcePermissionPoll.contains("W21003"))) {
                            if(ObjectUtil.isNull(item.getAmmoniaAmendments())){
                                item.setAmmoniaAmendments(new BigDecimal(0));
                            }
                            item.setAmmoniaAmendments(item.getAmmoniaAmendments().add(NumberUtil.round(sEnvWaterMonHours.stream().filter(o -> {
                                return dataFlagConversion(o.getAmmoniaCorrectEmission(), lambdaQueryWrapper.getDataFlag(), o.getAmmoniaNitrogenFlag(), o.getDataFlag());
                            }).mapToDouble(op -> new BigDecimal(op.getAmmoniaCorrectEmission()).doubleValue()).sum(), 4)));
                        }
                        if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&sourcePermissionPoll.contains("W01018"))) {
                            if(ObjectUtil.isNull(item.getCodAmendments())){
                                item.setCodAmendments(new BigDecimal(0));
                            }
                            item.setCodAmendments(item.getCodAmendments().add(NumberUtil.round(sEnvWaterMonHours.stream().filter(o -> {
                                return dataFlagConversion(o.getCodCorrectEmission(), lambdaQueryWrapper.getDataFlag(), o.getCodFlag(), o.getDataFlag());
                            }).mapToDouble(op -> new BigDecimal(op.getCodCorrectEmission()).doubleValue()).sum(), 4)));
                        }
                        if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&sourcePermissionPoll.contains("W21001"))) {
                            if(ObjectUtil.isNull(item.getNitrogenAmendments())){
                                item.setNitrogenAmendments(new BigDecimal(0));
                            }
                            item.setNitrogenAmendments(item.getNitrogenAmendments().add(NumberUtil.round(sEnvWaterMonHours.stream().filter(o -> {
                                return dataFlagConversion(o.getNitrogenCorrectEmission(), lambdaQueryWrapper.getDataFlag(), o.getNitrogenConcentrationFlag(), o.getDataFlag());
                            }).mapToDouble(op -> new BigDecimal(op.getNitrogenCorrectEmission()).doubleValue()).sum(), 4)));
                        }
                        if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&sourcePermissionPoll.contains("W21011"))) {
                            if(ObjectUtil.isNull(item.getPhosphorusAmendments())){
                                item.setPhosphorusAmendments(new BigDecimal(0));
                            }
                            item.setPhosphorusAmendments(item.getPhosphorusAmendments().add(NumberUtil.round(sEnvWaterMonHours.stream().filter(o -> {
                                return dataFlagConversion(o.getPhosphorusCorrectEmission(), lambdaQueryWrapper.getDataFlag(), o.getPhosphorusConcentrationFlag(), o.getDataFlag());
                            }).mapToDouble(op -> new BigDecimal(op.getPhosphorusCorrectEmission()).doubleValue()).sum(), 4)));
                        }
                    }else {
                        if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&sourcePermissionPoll.contains("W21003"))) {
                            if(ObjectUtil.isNull(item.getAmmoniaAmendments())){
                                item.setAmmoniaAmendments(new BigDecimal(0));
                            }
                            item.setAmmoniaAmendments(NumberUtil.round(sEnvWaterMonHours.stream().filter(o -> {
                                return dataFlagConversion(o.getAmmoniaNitrogenEmission(), lambdaQueryWrapper.getDataFlag(), o.getAmmoniaNitrogenFlag(), o.getDataFlag());
                            }).mapToDouble(op -> new BigDecimal(op.getAmmoniaNitrogenEmission()).doubleValue()).sum(), 4));
                        }
                        if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&sourcePermissionPoll.contains("W01018"))) {
                            if(ObjectUtil.isNull(item.getCodAmendments())){
                                item.setCodAmendments(new BigDecimal(0));
                            }
                            item.setCodAmendments(NumberUtil.round(sEnvWaterMonHours.stream().filter(o ->{
                                return dataFlagConversion(o.getCodEmission(),lambdaQueryWrapper.getDataFlag(),o.getCodFlag(), o.getDataFlag());
                            }).mapToDouble(op->new BigDecimal(op.getCodEmission()).doubleValue()).sum(),4));
                        }
                        if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&sourcePermissionPoll.contains("W21001"))) {
                            if(ObjectUtil.isNull(item.getNitrogenAmendments())){
                                item.setNitrogenAmendments(new BigDecimal(0));
                            }
                            item.setNitrogenAmendments(NumberUtil.round(sEnvWaterMonHours.stream().filter(o -> {
                                return dataFlagConversion(o.getNitrogenConcentrationEmission(), lambdaQueryWrapper.getDataFlag(), o.getNitrogenConcentrationFlag(), o.getDataFlag());
                            }).mapToDouble(op -> new BigDecimal(op.getNitrogenConcentrationEmission()).doubleValue()).sum(), 4));
                        }
                        if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&sourcePermissionPoll.contains("W21011"))) {
                            if(ObjectUtil.isNull(item.getPhosphorusAmendments())){
                                item.setPhosphorusAmendments(new BigDecimal(0));
                            }
                            item.setPhosphorusAmendments(NumberUtil.round(sEnvWaterMonHours.stream().filter(o -> {
                                return dataFlagConversion(o.getPhosphorusConcentrationEmission(), lambdaQueryWrapper.getDataFlag(), o.getPhosphorusConcentrationFlag(), o.getDataFlag());
                            }).mapToDouble(op -> new BigDecimal(op.getPhosphorusConcentrationEmission()).doubleValue()).sum(), 4));
                        }
                    }
                });
            }
        });
        return sRaWaterPermittedOnlines;
    }

    @Override
    public IPage<SRaWaterPermittedOnline> selectSRaMultiSourceListBy(SRaWaterPermittedOnlineQuery lambdaQueryWrapper) {
        if(StringUtils.isBlank(lambdaQueryWrapper.getInvalidDataCalculated())){
            lambdaQueryWrapper.setInvalidDataCalculated("1");
        }
        Page<SRaWaterPermittedOnline> page = new Page<>(lambdaQueryWrapper.getPageNum(), lambdaQueryWrapper.getPageSize());

        List<SRaWaterPermittedOnline> sRaWaterPermittedOnlines= iPcOutletService.selectBySource(page,lambdaQueryWrapper);
        List<String> stringList=new ArrayList<>();
        sRaWaterPermittedOnlines.forEach(item->{
            stringList.add(item.getFactoryId());
        });
        lambdaQueryWrapper.setMonitors(stringList);
        /*-------------------------------------在线排放量-------------------------------------------------*/
        lambdaQueryWrapper.setType("1");
        List<SEnvGasMonHour> gasMonHours=isEnvGasMonHourService.selectSRaWaterSourceListBy(lambdaQueryWrapper);
        //处理修约量，如果为空则填充排放量
        gasMonHours.forEach(item->{
            if(StringUtils.isEmpty(item.getSo2CorrectEmission())){
                item.setSo2CorrectEmission(item.getSo2Emission());
            }
            if(StringUtils.isEmpty(item.getNoxCorrectEmission())){
                item.setNoxCorrectEmission(item.getNoxEmission());
            }
            if(StringUtils.isEmpty(item.getVocsCorrectEmission())){
                item.setVocsCorrectEmission(item.getVocsEmission());
            }
            if(StringUtils.isEmpty(item.getSmokeDustCorrectEmission())){
                item.setSmokeDustCorrectEmission(item.getPmEmission());
            }
        });
        Map<String, List<SEnvGasMonHour>> stringListMap = gasMonHours.stream().collect(Collectors.groupingBy( SEnvGasMonHour::getFactoryId));
        sRaWaterPermittedOnlines.forEach(item->{
            if(stringListMap.containsKey(item.getFactoryId())){
                List<SEnvGasMonHour> sEnvWaterMonHours = stringListMap.get(item.getFactoryId());
                if(lambdaQueryWrapper.getInvalidDataCalculated().equals("1")){
                    item.setSo2(NumberUtil.round(sEnvWaterMonHours.stream().filter(o ->{
                        return dataFlagConversion(o.getSo2CorrectEmission(),lambdaQueryWrapper.getDataFlag(),o.getSo2Flag(), o.getDataFlag());
                    }).mapToDouble(op->new BigDecimal(op.getSo2CorrectEmission()).doubleValue()).sum(),4));
                    item.setNox(NumberUtil.round(sEnvWaterMonHours.stream().filter(o ->{
                        return dataFlagConversion(o.getNoxCorrectEmission(),lambdaQueryWrapper.getDataFlag(),o.getNoxFlag(), o.getDataFlag());
                    }).mapToDouble(op->new BigDecimal(op.getNoxCorrectEmission()).doubleValue()).sum(),4));
                    item.setVocs(NumberUtil.round(sEnvWaterMonHours.stream().filter(o ->{
                        return dataFlagConversion(o.getVocsCorrectEmission(),lambdaQueryWrapper.getDataFlag(),o.getVocsFlag(), o.getDataFlag());
                    }).mapToDouble(op->new BigDecimal(op.getVocsCorrectEmission()).doubleValue()).sum(),4));
                    item.setPm(NumberUtil.round(sEnvWaterMonHours.stream().filter(o ->{
                        return dataFlagConversion(o.getSmokeDustCorrectEmission(),lambdaQueryWrapper.getDataFlag(),o.getPmFlag(), o.getDataFlag());
                    }).mapToDouble(op->new BigDecimal(op.getSmokeDustCorrectEmission()).doubleValue()).sum(),4));
                }else {
                    item.setSo2(NumberUtil.round(sEnvWaterMonHours.stream().filter(o ->{
                        return dataFlagConversion(o.getSo2Emission(),lambdaQueryWrapper.getDataFlag(),o.getSo2Flag(), o.getDataFlag());
                    }).mapToDouble(op->new BigDecimal(op.getSo2Emission()).doubleValue()).sum(),4));
                    item.setNox(NumberUtil.round(sEnvWaterMonHours.stream().filter(o ->{
                        return dataFlagConversion(o.getNoxEmission(),lambdaQueryWrapper.getDataFlag(),o.getNoxFlag(), o.getDataFlag());
                    }).mapToDouble(op->new BigDecimal(op.getNoxEmission()).doubleValue()).sum(),4));
                    item.setVocs(NumberUtil.round(sEnvWaterMonHours.stream().filter(o ->{
                        return dataFlagConversion(o.getVocsEmission(),lambdaQueryWrapper.getDataFlag(),o.getVocsFlag(), o.getDataFlag());
                    }).mapToDouble(op->new BigDecimal(op.getVocsEmission()).doubleValue()).sum(),4));
                    item.setPm(NumberUtil.round(sEnvWaterMonHours.stream().filter(o ->{
                        return dataFlagConversion(o.getPmEmission(),lambdaQueryWrapper.getDataFlag(),o.getPmFlag(), o.getDataFlag());
                    }).mapToDouble(op->new BigDecimal(op.getPmEmission()).doubleValue()).sum(),4));
                }
            }
        });

        List<SEnvWaterMonHour> waterMonHours=isEnvWaterMonHourService.selectSRaWaterSourceListBy(lambdaQueryWrapper);
        //处理修约量，如果为空则填充排放量
        waterMonHours.forEach(item->{
            if(StringUtils.isEmpty(item.getAmmoniaCorrectEmission())){
                item.setAmmoniaCorrectEmission(item.getAmmoniaNitrogenEmission());
            }
            if(StringUtils.isEmpty(item.getCodCorrectEmission())){
                item.setCodCorrectEmission(item.getCodEmission());
            }
            if(StringUtils.isEmpty(item.getNitrogenCorrectEmission())){
                item.setNitrogenCorrectEmission(item.getNitrogenConcentrationEmission());
            }
            if(StringUtils.isEmpty(item.getPhosphorusCorrectEmission())){
                item.setPhosphorusCorrectEmission(item.getPhosphorusConcentrationEmission());
            }
        });
        Map<String, List<SEnvWaterMonHour>> stringListMaps = waterMonHours.stream().collect(Collectors.groupingBy( SEnvWaterMonHour::getFactoryId));
        sRaWaterPermittedOnlines.forEach(item->{
            if(stringListMaps.containsKey(item.getFactoryId())){
                List<SEnvWaterMonHour> sEnvWaterMonHours = stringListMaps.get(item.getFactoryId());
                if(lambdaQueryWrapper.getInvalidDataCalculated().equals("1")){
                    item.setAmmonia(NumberUtil.round(sEnvWaterMonHours.stream().filter(o ->{
                        return dataFlagConversion(o.getAmmoniaCorrectEmission(),lambdaQueryWrapper.getDataFlag(),o.getAmmoniaNitrogenFlag(), o.getDataFlag());
                    }).mapToDouble(op->new BigDecimal(op.getAmmoniaCorrectEmission()).doubleValue()).sum(),4));
                    item.setCod(NumberUtil.round(sEnvWaterMonHours.stream().filter(o ->{
                        return dataFlagConversion(o.getCodCorrectEmission(),lambdaQueryWrapper.getDataFlag(),o.getCodFlag(), o.getDataFlag());
                    }).mapToDouble(op->new BigDecimal(op.getCodCorrectEmission()).doubleValue()).sum(),4));
                    item.setNitrogen(NumberUtil.round(sEnvWaterMonHours.stream().filter(o ->{
                        return dataFlagConversion(o.getNitrogenCorrectEmission(),lambdaQueryWrapper.getDataFlag(),o.getNitrogenConcentrationFlag(), o.getDataFlag());
                    }).mapToDouble(op->new BigDecimal(op.getNitrogenCorrectEmission()).doubleValue()).sum(),4));
                    item.setPhosphorus(NumberUtil.round(sEnvWaterMonHours.stream().filter(o ->{
                        return dataFlagConversion(o.getPhosphorusCorrectEmission(),lambdaQueryWrapper.getDataFlag(),o.getPhosphorusConcentrationFlag(), o.getDataFlag());
                    }).mapToDouble(op->new BigDecimal(op.getPhosphorusCorrectEmission()).doubleValue()).sum(),4));
                }else {
                    item.setAmmonia(NumberUtil.round(sEnvWaterMonHours.stream().filter(o ->{
                        return dataFlagConversion(o.getAmmoniaNitrogenEmission(),lambdaQueryWrapper.getDataFlag(),o.getAmmoniaNitrogenFlag(), o.getDataFlag());
                    }).mapToDouble(op->new BigDecimal(op.getAmmoniaNitrogenEmission()).doubleValue()).sum(),4));
                    item.setCod(NumberUtil.round(sEnvWaterMonHours.stream().filter(o ->{
                        return dataFlagConversion(o.getCodEmission(),lambdaQueryWrapper.getDataFlag(),o.getCodFlag(), o.getDataFlag());
                    }).mapToDouble(op->new BigDecimal(op.getCodEmission()).doubleValue()).sum(),4));
                    item.setNitrogen(NumberUtil.round(sEnvWaterMonHours.stream().filter(o ->{
                        return dataFlagConversion(o.getNitrogenConcentrationEmission(),lambdaQueryWrapper.getDataFlag(),o.getNitrogenConcentrationFlag(), o.getDataFlag());
                    }).mapToDouble(op->new BigDecimal(op.getNitrogenConcentrationEmission()).doubleValue()).sum(),4));
                    item.setPhosphorus(NumberUtil.round(sEnvWaterMonHours.stream().filter(o ->{
                        return dataFlagConversion(o.getPhosphorusConcentrationEmission(),lambdaQueryWrapper.getDataFlag(),o.getPhosphorusConcentrationFlag(), o.getDataFlag());
                    }).mapToDouble(op->new BigDecimal(op.getPhosphorusConcentrationEmission()).doubleValue()).sum(),4));
                }
            }
        });
        /*-------------------------------------许可排放量-------------------------------------------------*/
        lambdaQueryWrapper.setType("2");
        List<SEnvGasMonHour> gasMonHoursPermit=new ArrayList<>();
        //自动监测
        gasMonHoursPermit.addAll(isEnvGasMonHourService.selectSRaWaterSourceListBy(lambdaQueryWrapper));
        //手工检测
        gasMonHoursPermit.addAll(isEnvGasMonHourService.selectManualSRaWaterSourceListBy(lambdaQueryWrapper));
        //处理修约量，如果为空则填充排放量
        gasMonHoursPermit.forEach(item->{
            if(StringUtils.isEmpty(item.getSo2CorrectEmission())){
                item.setSo2CorrectEmission(item.getSo2Emission());
            }
            if(StringUtils.isEmpty(item.getNoxCorrectEmission())){
                item.setNoxCorrectEmission(item.getNoxEmission());
            }
            if(StringUtils.isEmpty(item.getVocsCorrectEmission())){
                item.setVocsCorrectEmission(item.getVocsEmission());
            }
            if(StringUtils.isEmpty(item.getSmokeDustCorrectEmission())){
                item.setSmokeDustCorrectEmission(item.getPmEmission());
            }
        });
        /**/
        Map<String, Map<String,List<SEnvGasMonHour>>> stringListMapPermit = gasMonHoursPermit.stream().collect(Collectors.groupingBy( SEnvGasMonHour::getFactoryId,Collectors.groupingBy(SEnvGasMonHour::getOutletId)));
        Set<String> setOutletIds =new HashSet<String>();
        stringListMapPermit.forEach((key,value)->{setOutletIds.addAll(value.keySet());});
        List<SRaWaterPermittedOnline> gasMonHours1=isEnvGasMonHourService.selectSRaWaterSourceListByPoll(setOutletIds);
        Map<String, List<SRaWaterPermittedOnline>> stringListMapsPermit = gasMonHours1.stream().collect(Collectors.groupingBy( SRaWaterPermittedOnline::getMonitorId));
        /**/
        sRaWaterPermittedOnlines.forEach(item->{
            if(stringListMapPermit.containsKey(item.getFactoryId())){
                /**/
                Map<String,List<SEnvGasMonHour>> sEnvWaterMonHour = stringListMapPermit.get(item.getFactoryId());
                sEnvWaterMonHour.forEach((key,sEnvWaterMonHours)->{
                    /**/
                   String  sourcePermissionPoll =stringListMapsPermit.get(key).get(0).getSourcePermissionPoll();
                    if(lambdaQueryWrapper.getInvalidDataCalculated().equals("1")){
                        if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&sourcePermissionPoll.contains("A21026"))) {
                            if(ObjectUtil.isNull(item.getSo2Amendments())){
                                item.setSo2Amendments(new BigDecimal(0));
                            }
                            item.setSo2Amendments(item.getSo2Amendments().add(NumberUtil.round(sEnvWaterMonHours.stream().filter(o -> {
                                return dataFlagConversion(o.getSo2CorrectEmission(), lambdaQueryWrapper.getDataFlag(), o.getSo2Flag(), o.getDataFlag());
                            }).mapToDouble(op -> new BigDecimal(op.getSo2CorrectEmission()).doubleValue()).sum(), 4)));
                        }
                        if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&sourcePermissionPoll.contains("A21002"))) {
                            if(ObjectUtil.isNull(item.getNoxAmendments())){
                                item.setNoxAmendments(new BigDecimal(0));
                            }
                            item.setNoxAmendments(item.getNoxAmendments().add(NumberUtil.round(sEnvWaterMonHours.stream().filter(o -> {
                                return dataFlagConversion(o.getNoxCorrectEmission(), lambdaQueryWrapper.getDataFlag(), o.getNoxFlag(), o.getDataFlag());
                            }).mapToDouble(op -> new BigDecimal(op.getNoxCorrectEmission()).doubleValue()).sum(), 4)));
                        }
                        if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&sourcePermissionPoll.contains("A99901"))) {
                            if(ObjectUtil.isNull(item.getVocsAmendments())){
                                item.setVocsAmendments(new BigDecimal(0));
                            }
                            item.setVocsAmendments(item.getVocsAmendments().add(NumberUtil.round(sEnvWaterMonHours.stream().filter(o -> {
                                return dataFlagConversion(o.getVocsCorrectEmission(), lambdaQueryWrapper.getDataFlag(), o.getVocsFlag(), o.getDataFlag());
                            }).mapToDouble(op -> new BigDecimal(op.getVocsCorrectEmission()).doubleValue()).sum(), 4)));
                        }
                        if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&sourcePermissionPoll.contains("A99911"))) {
                            if(ObjectUtil.isNull(item.getPmAmendments())){
                                item.setPmAmendments(new BigDecimal(0));
                            }
                            item.setPmAmendments(item.getPmAmendments().add(NumberUtil.round(sEnvWaterMonHours.stream().filter(o -> {
                                return dataFlagConversion(o.getSmokeDustCorrectEmission(), lambdaQueryWrapper.getDataFlag(), o.getPmFlag(), o.getDataFlag());
                            }).mapToDouble(op -> new BigDecimal(op.getSmokeDustCorrectEmission()).doubleValue()).sum(), 4)));
                        }
                    }else {
                        if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&sourcePermissionPoll.contains("A21026"))) {
                            if(ObjectUtil.isNull(item.getSo2Amendments())){
                                item.setSo2Amendments(new BigDecimal(0));
                            }
                            item.setSo2Amendments(item.getSo2Amendments().add(NumberUtil.round(sEnvWaterMonHours.stream().filter(o -> {
                                return dataFlagConversion(o.getSo2Emission(), lambdaQueryWrapper.getDataFlag(), o.getSo2Flag(), o.getDataFlag());
                            }).mapToDouble(op -> new BigDecimal(op.getSo2Emission()).doubleValue()).sum(), 4)));
                        }
                        if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&sourcePermissionPoll.contains("A21002"))) {
                            if(ObjectUtil.isNull(item.getNoxAmendments())){
                                item.setNoxAmendments(new BigDecimal(0));
                            }
                            item.setNoxAmendments(item.getNoxAmendments().add(NumberUtil.round(sEnvWaterMonHours.stream().filter(o -> {
                                return dataFlagConversion(o.getNoxEmission(), lambdaQueryWrapper.getDataFlag(), o.getNoxFlag(), o.getDataFlag());
                            }).mapToDouble(op -> new BigDecimal(op.getNoxEmission()).doubleValue()).sum(), 4)));
                        }
                        if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&sourcePermissionPoll.contains("A99901"))) {
                            if(ObjectUtil.isNull(item.getVocsAmendments())){
                                item.setVocsAmendments(new BigDecimal(0));
                            }
                            item.setVocsAmendments(item.getVocsAmendments().add(NumberUtil.round(sEnvWaterMonHours.stream().filter(o -> {
                                return dataFlagConversion(o.getVocsEmission(), lambdaQueryWrapper.getDataFlag(), o.getVocsFlag(), o.getDataFlag());
                            }).mapToDouble(op -> new BigDecimal(op.getVocsEmission()).doubleValue()).sum(), 4)));
                        }
                        if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&sourcePermissionPoll.contains("A99911"))) {
                            if(ObjectUtil.isNull(item.getPmAmendments())){
                                item.setPmAmendments(new BigDecimal(0));
                            }
                            item.setPmAmendments(item.getPmAmendments().add(NumberUtil.round(sEnvWaterMonHours.stream().filter(o -> {
                                return dataFlagConversion(o.getPmEmission(), lambdaQueryWrapper.getDataFlag(), o.getPmFlag(), o.getDataFlag());
                            }).mapToDouble(op -> new BigDecimal(op.getPmEmission()).doubleValue()).sum(), 4)));
                        }
                    }
                });
            }
        });
        List<SEnvWaterMonHour> waterMonHoursPermit=new ArrayList<>();
        //自动监测
        waterMonHoursPermit.addAll(isEnvWaterMonHourService.selectSRaWaterSourceListBy(lambdaQueryWrapper));
        //手工检测
        waterMonHoursPermit.addAll(isEnvWaterMonHourService.selectManualSRaWaterSourceListBy(lambdaQueryWrapper));

        //处理修约量，如果为空则填充排放量
        waterMonHoursPermit.forEach(item->{
            if(StringUtils.isEmpty(item.getAmmoniaCorrectEmission())){
                item.setAmmoniaCorrectEmission(item.getAmmoniaNitrogenEmission());
            }
            if(StringUtils.isEmpty(item.getCodCorrectEmission())){
                item.setCodCorrectEmission(item.getCodEmission());
            }
            if(StringUtils.isEmpty(item.getNitrogenCorrectEmission())){
                item.setNitrogenCorrectEmission(item.getNitrogenConcentrationEmission());
            }
            if(StringUtils.isEmpty(item.getPhosphorusCorrectEmission())){
                item.setPhosphorusCorrectEmission(item.getPhosphorusConcentrationEmission());
            }
        });
        /**/
        Map<String, Map<String,List<SEnvWaterMonHour>>> stringListWaterMapsPermits = waterMonHoursPermit.stream().collect(Collectors.groupingBy( SEnvWaterMonHour::getFactoryId,Collectors.groupingBy(SEnvWaterMonHour::getOutletId)));
        Set<String> setOutletIdWaters =new HashSet<String>();
        stringListWaterMapsPermits.forEach((key,value)->{setOutletIdWaters.addAll(value.keySet());});
        List<SRaWaterPermittedOnline> waterMonHours1=isEnvGasMonHourService.selectSRaWaterSourceListByPoll(setOutletIdWaters);
        Map<String, List<SRaWaterPermittedOnline>> stringListWaterMapsPermit = waterMonHours1.stream().collect(Collectors.groupingBy( SRaWaterPermittedOnline::getMonitorId));
        /**/
//        Map<String, List<SEnvWaterMonHour>> stringListMapsPermit = waterMonHoursPermit.stream().collect(Collectors.groupingBy( SEnvWaterMonHour::getFactoryId));
        sRaWaterPermittedOnlines.forEach(item->{
            if(stringListWaterMapsPermits.containsKey(item.getFactoryId())){
                Map<String,List<SEnvWaterMonHour>> sEnvWaterMonHouras = stringListWaterMapsPermits.get(item.getFactoryId());
                sEnvWaterMonHouras.forEach((key,sEnvWaterMonHours)->{
                    String  sourcePermissionPoll =stringListWaterMapsPermit.get(key).get(0).getSourcePermissionPoll();
                    if(lambdaQueryWrapper.getInvalidDataCalculated().equals("1")){
                        if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&sourcePermissionPoll.contains("W21003"))) {
                            if(ObjectUtil.isNull(item.getAmmoniaAmendments())){
                                item.setAmmoniaAmendments(new BigDecimal(0));
                            }
                            item.setAmmoniaAmendments(item.getAmmoniaAmendments().add(NumberUtil.round(sEnvWaterMonHours.stream().filter(o -> {
                                return dataFlagConversion(o.getAmmoniaCorrectEmission(), lambdaQueryWrapper.getDataFlag(), o.getAmmoniaNitrogenFlag(), o.getDataFlag());
                            }).mapToDouble(op -> new BigDecimal(op.getAmmoniaCorrectEmission()).doubleValue()).sum(), 4)));
                        }
                        if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&sourcePermissionPoll.contains("W01018"))) {
                            if(ObjectUtil.isNull(item.getCodAmendments())){
                                item.setCodAmendments(new BigDecimal(0));
                            }
                            item.setCodAmendments(item.getCodAmendments().add(NumberUtil.round(sEnvWaterMonHours.stream().filter(o -> {
                                return dataFlagConversion(o.getCodCorrectEmission(), lambdaQueryWrapper.getDataFlag(), o.getCodFlag(), o.getDataFlag());
                            }).mapToDouble(op -> new BigDecimal(op.getCodCorrectEmission()).doubleValue()).sum(), 4)));
                        }
                        if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&sourcePermissionPoll.contains("W21001"))) {
                            if(ObjectUtil.isNull(item.getNitrogenAmendments())){
                                item.setNitrogenAmendments(new BigDecimal(0));
                            }
                            item.setNitrogenAmendments(item.getNitrogenAmendments().add(NumberUtil.round(sEnvWaterMonHours.stream().filter(o -> {
                                return dataFlagConversion(o.getNitrogenCorrectEmission(), lambdaQueryWrapper.getDataFlag(), o.getNitrogenConcentrationFlag(), o.getDataFlag());
                            }).mapToDouble(op -> new BigDecimal(op.getNitrogenCorrectEmission()).doubleValue()).sum(), 4)));
                        }
                        if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&sourcePermissionPoll.contains("W21011"))) {
                            if(ObjectUtil.isNull(item.getPhosphorusAmendments())){
                                item.setPhosphorusAmendments(new BigDecimal(0));
                            }
                            item.setPhosphorusAmendments(item.getPhosphorusAmendments().add(NumberUtil.round(sEnvWaterMonHours.stream().filter(o -> {
                                return dataFlagConversion(o.getPhosphorusCorrectEmission(), lambdaQueryWrapper.getDataFlag(), o.getPhosphorusConcentrationFlag(), o.getDataFlag());
                            }).mapToDouble(op -> new BigDecimal(op.getPhosphorusCorrectEmission()).doubleValue()).sum(), 4)));
                        }
                    }else {
                        if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&sourcePermissionPoll.contains("W21003"))) {
                            if(ObjectUtil.isNull(item.getAmmoniaAmendments())){
                                item.setAmmoniaAmendments(new BigDecimal(0));
                            }
                            item.setAmmoniaAmendments(NumberUtil.round(sEnvWaterMonHours.stream().filter(o -> {
                                return dataFlagConversion(o.getAmmoniaNitrogenEmission(), lambdaQueryWrapper.getDataFlag(), o.getAmmoniaNitrogenFlag(), o.getDataFlag());
                            }).mapToDouble(op -> new BigDecimal(op.getAmmoniaNitrogenEmission()).doubleValue()).sum(), 4));
                        }
                        if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&sourcePermissionPoll.contains("W01018"))) {
                            if(ObjectUtil.isNull(item.getCodAmendments())){
                                item.setCodAmendments(new BigDecimal(0));
                            }
                            item.setCodAmendments(NumberUtil.round(sEnvWaterMonHours.stream().filter(o ->{
                                return dataFlagConversion(o.getCodEmission(),lambdaQueryWrapper.getDataFlag(),o.getCodFlag(), o.getDataFlag());
                            }).mapToDouble(op->new BigDecimal(op.getCodEmission()).doubleValue()).sum(),4));
                        }
                        if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&sourcePermissionPoll.contains("W21001"))) {
                            if(ObjectUtil.isNull(item.getNitrogenAmendments())){
                                item.setNitrogenAmendments(new BigDecimal(0));
                            }
                            item.setNitrogenAmendments(NumberUtil.round(sEnvWaterMonHours.stream().filter(o -> {
                                return dataFlagConversion(o.getNitrogenConcentrationEmission(), lambdaQueryWrapper.getDataFlag(), o.getNitrogenConcentrationFlag(), o.getDataFlag());
                            }).mapToDouble(op -> new BigDecimal(op.getNitrogenConcentrationEmission()).doubleValue()).sum(), 4));
                        }
                        if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&sourcePermissionPoll.contains("W21011"))) {
                            if(ObjectUtil.isNull(item.getPhosphorusAmendments())){
                                item.setPhosphorusAmendments(new BigDecimal(0));
                            }
                            item.setPhosphorusAmendments(NumberUtil.round(sEnvWaterMonHours.stream().filter(o -> {
                                return dataFlagConversion(o.getPhosphorusConcentrationEmission(), lambdaQueryWrapper.getDataFlag(), o.getPhosphorusConcentrationFlag(), o.getDataFlag());
                            }).mapToDouble(op -> new BigDecimal(op.getPhosphorusConcentrationEmission()).doubleValue()).sum(), 4));
                        }
                    }
                });
            }
        });
        page.setRecords(sRaWaterPermittedOnlines);
        return page;
    }
    public boolean dataFlagConversion(String value, String dataFlag, String valueFlag, String flag){
        if(StringUtils.isBlank(value)){
            return false;
        }
        if(StringUtils.isBlank(dataFlag)){
            return true;
        }
        if(StringUtils.isBlank(flag)&&StringUtils.isBlank(valueFlag)){
            return true;
        }
        if(StringUtils.isNotBlank(flag)){
            if(!dataFlag.contains(flag.trim())){
                return true;
            }
        }
        if(StringUtils.isNotBlank(valueFlag)){
            if(!dataFlag.contains(valueFlag.trim())){
                return true;
            }
        }
        return false;
    }
    @Override
    public IPage<SRaMultiSource> selectSRaMultiSourceListDetailsBy(SRaMultiSourceQuery lambdaQueryWrapper) {
        if(StringUtils.isBlank(lambdaQueryWrapper.getInvalidDataCalculated())){
            lambdaQueryWrapper.setInvalidDataCalculated("0");
        }
        if(StringUtils.isNotBlank(lambdaQueryWrapper.getDataFlag())){
            lambdaQueryWrapper.setDataFlagArr(Arrays.asList(lambdaQueryWrapper.getDataFlag().split(",")));
        }
        Page<SRaMultiSource> page = new Page<>(lambdaQueryWrapper.getPageNum(), lambdaQueryWrapper.getPageSize());
        List<SRaMultiSource> list=sRaMultiSourceMapper.selectSRaMultiSourceListDetailsBy(page,lambdaQueryWrapper);
        page.setRecords(list);
        return page;
    }

    @Override
    public List<SRaMultiSource> selectSRaMultiSourceListDetails(SRaMultiSourceQuery lambdaQueryWrapper) {
        if(StringUtils.isBlank(lambdaQueryWrapper.getInvalidDataCalculated())){
            lambdaQueryWrapper.setInvalidDataCalculated("0");
        }
        if(StringUtils.isNotBlank(lambdaQueryWrapper.getDataFlag())){
            lambdaQueryWrapper.setDataFlagArr(Arrays.asList(lambdaQueryWrapper.getDataFlag().split(",")));
        }
        Page<SRaMultiSource> page = new Page<>(1, 999999);
        List<SRaMultiSource> list=sRaMultiSourceMapper.selectSRaMultiSourceListDetailsBy(page,lambdaQueryWrapper);
        return list;
    }

    @Override
    public String pullMonitorNameOptions(SRaMultiSourceQuery lambdaQueryWrapper) {
        return sRaMultiSourceMapper.pullMonitorNameOptions(lambdaQueryWrapper);
    }
}
