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

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.cecep.talroad.domain.SEnvGasMonHour;
import cn.cecep.talroad.domain.analysis.SRaGasPermittedOnline;
import cn.cecep.talroad.domain.analysis.SRaWaterPermittedOnline;
import cn.cecep.talroad.mapper.analysis.SRaGasPermittedOnlineMapper;
import cn.cecep.talroad.service.IPcOutletService;
import cn.cecep.talroad.service.ISEnvGasMonHourService;
import cn.cecep.talroad.service.analysis.ISRaGasPermittedOnlineService;
import cn.cecep.talroad.vo.query.analysis.SRaWaterPermittedOnlineQuery;
import cn.hutool.core.util.NumberUtil;
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-02*/
@Service
public class SRaGasPermittedOnlineServiceImpl extends ServiceImpl<SRaGasPermittedOnlineMapper, SRaGasPermittedOnline> implements ISRaGasPermittedOnlineService
{
    @Autowired
    private SRaGasPermittedOnlineMapper sRaGasPermittedOnlineMapper;
    @Autowired
    private ISEnvGasMonHourService isEnvGasMonHourService;
    @Autowired
    private IPcOutletService iPcOutletService;
    /**
     * 查询在线排放量废气明细列表
     *
     * @return 在线排放量废气明细
     */
    @Override
    public List<SRaWaterPermittedOnline> selectSRaGasPermittedOnlineList(SRaWaterPermittedOnlineQuery lambdaQueryWrapper)
    {
        if(StringUtils.isBlank(lambdaQueryWrapper.getInvalidDataCalculated())){
            lambdaQueryWrapper.setInvalidDataCalculated("1");
        }
        //废气
        lambdaQueryWrapper.setPollutantOutletType("1");
        Page<SRaWaterPermittedOnline> page = new Page<>(1, 999999);

        List<SRaWaterPermittedOnline> sRaWaterPermittedOnlines= iPcOutletService.selectByOnlineAndSource(page,lambdaQueryWrapper);
        List<String> stringList=new ArrayList<>();
        sRaWaterPermittedOnlines.forEach(item->{
            if(StringUtils.isBlank(item.getSourcePermissionPoll())){
                item.setSourcePermissionPoll("");
            }
            stringList.add(item.getMonitorId());
        });
        lambdaQueryWrapper.setMonitors(stringList);
        List<SEnvGasMonHour> list=new ArrayList<>();
        //自动监测数据
        list.addAll(isEnvGasMonHourService.selectSRaWaterPermittedOnlineListBy(lambdaQueryWrapper));
        if(lambdaQueryWrapper.getType().equals("2")){
            //手工检测数据
            list.addAll(isEnvGasMonHourService.selectManualSRaWaterPermittedOnlineListBy(lambdaQueryWrapper));
        }
        //处理修约量，如果为空则填充排放量
        list.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 = list.stream().collect(Collectors.groupingBy( SEnvGasMonHour::getOutletId));
        sRaWaterPermittedOnlines.forEach(item->{
            if(stringListMap.containsKey(item.getMonitorId())){
                List<SEnvGasMonHour> sEnvWaterMonHours = stringListMap.get(item.getMonitorId());
                if(lambdaQueryWrapper.getInvalidDataCalculated().equals("1")){
                    if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&item.getSourcePermissionPoll().contains("A21026"))){
                        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));
                    }
                    if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&item.getSourcePermissionPoll().contains("A21002"))){
                        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));
                    }
                    if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&item.getSourcePermissionPoll().contains("A99901"))){
                        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));
                    }
                    if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&item.getSourcePermissionPoll().contains("A99911"))){
                        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 {
                    if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&item.getSourcePermissionPoll().contains("A21026"))) {
                        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));
                    }
                    if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&item.getSourcePermissionPoll().contains("A21002"))) {
                        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));
                    }
                    if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&item.getSourcePermissionPoll().contains("A99901"))) {
                        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));
                    }
                    if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&item.getSourcePermissionPoll().contains("A99911"))) {
                        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));
                    }
                }
            }
        });
        return sRaWaterPermittedOnlines;
    }

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

        List<SRaWaterPermittedOnline> sRaWaterPermittedOnlines= iPcOutletService.selectByOnlineAndSource(page,lambdaQueryWrapper);
        List<String> stringList=new ArrayList<>();
        sRaWaterPermittedOnlines.forEach(item->{
            if(StringUtils.isBlank(item.getSourcePermissionPoll())){
                item.setSourcePermissionPoll("");
            }
            stringList.add(item.getMonitorId());
        });
        lambdaQueryWrapper.setMonitors(stringList);
        List<SEnvGasMonHour> list=new ArrayList<>();
        //自动监测数据
        list.addAll(isEnvGasMonHourService.selectSRaWaterPermittedOnlineListBy(lambdaQueryWrapper));
        if(lambdaQueryWrapper.getType().equals("2")){
            //手工检测数据
            list.addAll(isEnvGasMonHourService.selectManualSRaWaterPermittedOnlineListBy(lambdaQueryWrapper));
        }
        //处理修约量，如果为空则填充排放量
        list.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 = list.stream().collect(Collectors.groupingBy( SEnvGasMonHour::getOutletId));
        sRaWaterPermittedOnlines.forEach(item->{
            if(stringListMap.containsKey(item.getMonitorId())){
                List<SEnvGasMonHour> sEnvWaterMonHours = stringListMap.get(item.getMonitorId());
                if(lambdaQueryWrapper.getInvalidDataCalculated().equals("1")){
                    if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&item.getSourcePermissionPoll().contains("A21026"))){
                        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));
                    }
                    if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&item.getSourcePermissionPoll().contains("A21002"))){
                        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));
                    }
                    if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&item.getSourcePermissionPoll().contains("A99901"))){
                        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));
                    }
                    if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&item.getSourcePermissionPoll().contains("A99911"))){
                        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 {
                    if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&item.getSourcePermissionPoll().contains("A21026"))) {
                        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));
                    }
                    if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&item.getSourcePermissionPoll().contains("A21002"))) {
                        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));
                    }
                    if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&item.getSourcePermissionPoll().contains("A99901"))) {
                        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));
                    }
                    if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&item.getSourcePermissionPoll().contains("A99911"))) {
                        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));
                    }
                }
            }
        });
        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 List<Map<String, Object>> pullMonitorNameOptions(String factoryName) {

        return sRaGasPermittedOnlineMapper.pullMonitorNameOptions(factoryName);
    }
}
