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.SEnvWaterMonHour;
import cn.cecep.talroad.domain.analysis.SRaWaterPermittedOnline;
import cn.cecep.talroad.mapper.analysis.SRaWaterPermittedOnlineMapper;
import cn.cecep.talroad.service.IPcOutletService;
import cn.cecep.talroad.service.ISEnvWaterMonHourService;
import cn.cecep.talroad.service.analysis.ISRaWaterPermittedOnlineService;
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 SRaWaterPermittedOnlineServiceImpl extends ServiceImpl<SRaWaterPermittedOnlineMapper, SRaWaterPermittedOnline> implements ISRaWaterPermittedOnlineService
{
    @Autowired
    private SRaWaterPermittedOnlineMapper sRaWaterPermittedOnlineMapper;
    @Autowired
    private IPcOutletService iPcOutletService;
    @Autowired
    private ISEnvWaterMonHourService isEnvWaterMonHourService;

    /**
     * 查询许可排放量废水明细列表
     *
     * @return 许可排放量废水明细
     */
    @Override
    public List<SRaWaterPermittedOnline> selectSRaWaterPermittedOnlineList(SRaWaterPermittedOnlineQuery lambdaQueryWrapper)
    {
        if(StringUtils.isBlank(lambdaQueryWrapper.getInvalidDataCalculated())){
            lambdaQueryWrapper.setInvalidDataCalculated("1");
        }
        lambdaQueryWrapper.setPollutantOutletType("2");
        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<SEnvWaterMonHour> list=new ArrayList<>();
        //自动监测数据
        list.addAll(isEnvWaterMonHourService.selectSRaWaterPermittedOnlineListBy(lambdaQueryWrapper));
        if(lambdaQueryWrapper.getType().equals("2")){
            //手工检测数据
            list.addAll(isEnvWaterMonHourService.selectManualSRaWaterPermittedOnlineListBy(lambdaQueryWrapper));
        }
        //处理修约量，如果为空则填充排放量
        list.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>> stringListMap = list.stream().collect(Collectors.groupingBy( SEnvWaterMonHour::getOutletId));
        sRaWaterPermittedOnlines.forEach(item->{
            if(stringListMap.containsKey(item.getMonitorId())){
                List<SEnvWaterMonHour> sEnvWaterMonHours = stringListMap.get(item.getMonitorId());
                if(lambdaQueryWrapper.getInvalidDataCalculated().equals("1")){
                    if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&item.getSourcePermissionPoll().contains("W21003"))) {
                        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));
                    }
                    if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&item.getSourcePermissionPoll().contains("W01018"))) {
                        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));
                    }
                    if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&item.getSourcePermissionPoll().contains("W21001"))) {
                        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));
                    }
                    if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&item.getSourcePermissionPoll().contains("W21011"))) {
                        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 {
                    if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&item.getSourcePermissionPoll().contains("W21003"))) {
                        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));
                    }
                    if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&item.getSourcePermissionPoll().contains("W01018"))) {
                        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));
                    }
                    if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&item.getSourcePermissionPoll().contains("W21001"))) {
                        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));
                    }
                    if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&item.getSourcePermissionPoll().contains("W21011"))) {
                        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));
                    }
                }
            }
        });
        return sRaWaterPermittedOnlines;
    }

    @Override
    public IPage<SRaWaterPermittedOnline> selectSRaWaterPermittedOnlineListBy(SRaWaterPermittedOnlineQuery lambdaQueryWrapper) {
        if(StringUtils.isBlank(lambdaQueryWrapper.getInvalidDataCalculated())){
            lambdaQueryWrapper.setInvalidDataCalculated("1");
        }
        lambdaQueryWrapper.setPollutantOutletType("2");
        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<SEnvWaterMonHour> list=new ArrayList<>();
        //自动监测数据
        list.addAll(isEnvWaterMonHourService.selectSRaWaterPermittedOnlineListBy(lambdaQueryWrapper));
        if(lambdaQueryWrapper.getType().equals("2")){
            //手工检测数据
            list.addAll(isEnvWaterMonHourService.selectManualSRaWaterPermittedOnlineListBy(lambdaQueryWrapper));
        }
        //处理修约量，如果为空则填充排放量
        list.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>> stringListMap = list.stream().collect(Collectors.groupingBy( SEnvWaterMonHour::getOutletId));
        sRaWaterPermittedOnlines.forEach(item->{
            if(stringListMap.containsKey(item.getMonitorId())){
                List<SEnvWaterMonHour> sEnvWaterMonHours = stringListMap.get(item.getMonitorId());
                if(lambdaQueryWrapper.getInvalidDataCalculated().equals("1")){
                    if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&item.getSourcePermissionPoll().contains("W21003"))) {
                        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));
                    }
                    if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&item.getSourcePermissionPoll().contains("W01018"))) {
                        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));
                    }
                    if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&item.getSourcePermissionPoll().contains("W21001"))) {
                        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));
                    }
                    if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&item.getSourcePermissionPoll().contains("W21011"))) {
                        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 {
                    if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&item.getSourcePermissionPoll().contains("W21003"))) {
                        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));
                    }
                    if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&item.getSourcePermissionPoll().contains("W01018"))) {
                        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));
                    }
                    if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&item.getSourcePermissionPoll().contains("W21001"))) {
                        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));
                    }
                    if(lambdaQueryWrapper.getType().equals("1")||(lambdaQueryWrapper.getType().equals("2")&&item.getSourcePermissionPoll().contains("W21011"))) {
                        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));
                    }
                }
            }
        });
        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 sRaWaterPermittedOnlineMapper.pullMonitorNameOptions(factoryName);
    }
}
