package cn.cecep.talroad.data.analyse.task.service;

import cn.cecep.talroad.common.core.utils.DateUtils;
import cn.cecep.talroad.data.analyse.task.real.concentration.GasConcentrationEnum;
import cn.cecep.talroad.data.analyse.task.real.concentration.WaterConcentrationEnum;
import cn.cecep.talroad.domain.SEnvGasMonHour;
import cn.cecep.talroad.domain.SEnvWaterMonDay;
import cn.cecep.talroad.domain.analysis.SRaEmissionsMonitoringVariance;
import cn.cecep.talroad.domain.analysis.SRaLowMonitoringGas;
import cn.cecep.talroad.domain.analysis.SRaLowMonitoringWater;
import cn.cecep.talroad.domain.analysis.SRaSelfMonitoringData;
import cn.cecep.talroad.mapper.analysis.SRaLowMonitoringGasMapper;
import cn.cecep.talroad.mapper.analysis.SRaLowMonitoringWaterMapper;
import cn.cecep.talroad.mapper.analysis.SRaSelfMonitoringDataMapper;
import cn.cecep.talroad.service.ISEnvGasMonHourService;
import cn.cecep.talroad.service.ISEnvWaterMonDayService;
import cn.cecep.talroad.service.analysis.ISRaEmissionsMonitoringVarianceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/***
 *
 * @Title:
 * @Author: xuxin
 * @CreateTime: 2023-07-03 14:47
 * @Description
 *
 **/
@Service
public class ConcentrationTaskService implements TaskService {

    @Autowired
    private SRaLowMonitoringGasMapper sRaLowMonitoringGasMapper;//执法监测废气
    @Autowired
    private SRaLowMonitoringWaterMapper sRaLowMonitoringWaterMapper;// 执法监测废水
    @Autowired
    private SRaSelfMonitoringDataMapper sRaSelfMonitoringDataMapper;
    @Autowired
    private ISEnvGasMonHourService isEnvGasMonHourService;
    @Autowired
    private ISEnvWaterMonDayService isEnvWaterMonDayService;

    @Autowired
    private ISRaEmissionsMonitoringVarianceService isRaEmissionsMonitoringVarianceService;


    @Override
    public Object mainProcess(Object dto) {
        Date lastDay = DateUtils.addDateDays(new Date(), -1);
        Date lastDayBegin = DateUtils.parseFormatDate("yyyy-MM-dd 00:00:00", lastDay);
        Date lastDayEnd = DateUtils.parseFormatDate("yyyy-MM-dd 23:59:59", lastDay);
        //废气监测数据差异分析
        doProcessGas(lastDayBegin, lastDayEnd);
        //废水监测数据差异分析
        doProcessWater(lastDayBegin, lastDayEnd);
        return null;
    }

    /**
     * 废水监测数据差异分析
     *
     * @param lastDayBegin
     * @param lastDayEnd
     */
    private void doProcessWater(Date lastDayBegin, Date lastDayEnd) {
        List<SRaEmissionsMonitoringVariance> resultList = new ArrayList<>();
        List<SRaSelfMonitoringData> waterList = sRaSelfMonitoringDataMapper.selectLastDayWaterData(lastDayBegin, lastDayEnd);
        if(waterList!=null && waterList.size()>0){
            waterList.forEach(water->{
                SRaEmissionsMonitoringVariance emission = new SRaEmissionsMonitoringVariance();
                BigDecimal lowRate = new BigDecimal(0);
                BigDecimal bVal = new BigDecimal(0);
                List<SEnvWaterMonDay> waterHourList = isEnvWaterMonDayService.selectHourDataByTime(water.getFactoryId(), water.getMonitorId(), water.getMonitorTime());
                List<SRaLowMonitoringWater> lowWaterList = sRaLowMonitoringWaterMapper.selectHourDataByTime(water.getFactoryId(), water.getMonitorId(), water.getPollutantsCode(), water.getMonitorTime());
                if(waterHourList.size()>0||lowWaterList.size()>0){
                    SEnvWaterMonDay hourData = null;
                    if(waterHourList.size()>0){
                        hourData = waterHourList.get(0);
                        if(WaterConcentrationEnum.cod_concentration.getPollutantsCode().equals(water.getPollutantsCode())){
                            bVal = BigDecimal.valueOf(Double.valueOf(hourData.getCodConcentration()));
                        }else if(WaterConcentrationEnum.nitrogen_concentration.getPollutantsCode().equals(water.getPollutantsCode())){
                            bVal = BigDecimal.valueOf(Double.valueOf(hourData.getNitrogenConcentration()));
                        }else if(WaterConcentrationEnum.phosphorus_concentration.getPollutantsCode().equals(water.getPollutantsCode())){
                            bVal = BigDecimal.valueOf(Double.valueOf(hourData.getPhosphorusConcentration()));
                        }
                    }
                    SRaLowMonitoringWater lowMonitoringWater = null;
                    if(lowWaterList.size()>0){
                        lowMonitoringWater = lowWaterList.get(0);
                        // 执法监测污染物浓度
                        lowRate = bVal.subtract(BigDecimal.valueOf(Double.valueOf(lowMonitoringWater.getConcentration()))).divide(bVal,2, RoundingMode.HALF_UP).abs();
                    }

                    if(hourData!=null) {
                        BigDecimal hourRate = bVal.subtract(BigDecimal.valueOf(Double.valueOf(water.getActualConcentration()))).divide(bVal,2, RoundingMode.HALF_UP).abs();
                        emission.setFactoryId(water.getFactoryId());
                        emission.setStationId(water.getMonitorId());
                        emission.setStationTime(water.getMonitorTime());
                        emission.setPollId(water.getPollutantsCode());
                        emission.setConcentrationPm(Double.valueOf(water.getActualConcentration()));
                        emission.setConcentrationEtl(Double.valueOf(lowMonitoringWater.getConcentration()));
                        emission.setConcentrationOnline(bVal.doubleValue());
                        if(lowRate.compareTo(hourRate)>0) {
                            emission.setDifferenceRatio(lowRate.doubleValue());
                        }else{
                            emission.setDifferenceRatio(hourRate.doubleValue());
                        }
                        emission.setCreateTime(new Date());
                        emission.setUpdateTime(new Date());
                        resultList.add(emission);
                    }
                }
            });
        }

        if(resultList!=null&&resultList.size()>0){
            isRaEmissionsMonitoringVarianceService.saveBatch(resultList);
        }
    }

    /**
     * 废气监测数据差异分析
     *
     * @param lastDayBegin
     * @param lastDayEnd
     */
    private void doProcessGas(Date lastDayBegin, Date lastDayEnd) {
        List<SRaEmissionsMonitoringVariance> resultList = new ArrayList<>();
        List<SRaSelfMonitoringData> gasList = sRaSelfMonitoringDataMapper.selectLastDayGasData(lastDayBegin, lastDayEnd);
        if (gasList != null && gasList.size() > 0) {
            gasList.forEach(self -> {
                SRaEmissionsMonitoringVariance emission = new SRaEmissionsMonitoringVariance();
                BigDecimal lowRate = new BigDecimal(0);
                BigDecimal bVal = new BigDecimal(0);
                List<SEnvGasMonHour> gasHourList = isEnvGasMonHourService.selectHourDataByTime(self.getFactoryId(), self.getMonitorId(), self.getMonitorTime());
                List<SRaLowMonitoringGas> lowGasList = sRaLowMonitoringGasMapper.selectHourDataByTime(self.getFactoryId(), self.getMonitorId(), self.getPollutantsCode(), self.getMonitorTime());
                if(gasHourList.size()>0||lowGasList.size()>0){
                    SEnvGasMonHour hourData = null;
                    if(gasHourList.size()>0){
                        hourData = gasHourList.get(0);
                        if(GasConcentrationEnum.so2.getPollutantsCode().equals(self.getPollutantsCode())){
                            bVal = BigDecimal.valueOf(Double.valueOf(hourData.getSo2()));
                        }else if(GasConcentrationEnum.pm.getPollutantsCode().equals(self.getPollutantsCode())){
                            bVal = BigDecimal.valueOf(Double.valueOf(hourData.getPm()));
                        }else if(GasConcentrationEnum.nox.getPollutantsCode().equals(self.getPollutantsCode())){
                            bVal = BigDecimal.valueOf(Double.valueOf(hourData.getNox()));
                        }else if(GasConcentrationEnum.vocs.getPollutantsCode().equals(self.getPollutantsCode())){
                            bVal = BigDecimal.valueOf(Double.valueOf(hourData.getVocs()));
                        }
                    }
                    SRaLowMonitoringGas lowMonitoringGas = null;
                    if(lowGasList.size()>0){
                        lowMonitoringGas = lowGasList.get(0);
                        // 执法监测污染物浓度
                        lowRate = bVal.subtract(BigDecimal.valueOf(Double.valueOf(lowMonitoringGas.getActualConcentration()))).divide(bVal,2, RoundingMode.HALF_UP).abs();
                    }
                    //
                    if(hourData!=null) {
                        BigDecimal hourRate = bVal.subtract(BigDecimal.valueOf(Double.valueOf(self.getActualConcentration()))).divide(bVal,2, RoundingMode.HALF_UP).abs();
                        emission.setFactoryId(self.getFactoryId());
                        emission.setStationId(self.getMonitorId());
                        emission.setStationTime(self.getMonitorTime());
                        emission.setPollId(self.getPollutantsCode());
                        emission.setConcentrationPm(Double.valueOf(self.getActualConcentration()));
                        emission.setConcentrationEtl(Double.valueOf(lowMonitoringGas.getActualConcentration()));
                        emission.setConcentrationOnline(bVal.doubleValue());
                        if(lowRate.compareTo(hourRate)>0) {
                            emission.setDifferenceRatio(lowRate.doubleValue());
                        }else{
                            emission.setDifferenceRatio(hourRate.doubleValue());
                        }
                        emission.setCreateTime(new Date());
                        emission.setUpdateTime(new Date());
                        resultList.add(emission);
                    }
                }
            });
        }

        if(resultList!=null&&resultList.size()>0){
            isRaEmissionsMonitoringVarianceService.saveBatch(resultList);
        }
    }

}
