package cn.cecep.talroad.data.analyse.task.execute.report.excess;

import cn.cecep.talroad.data.analyse.task.execute.report.PollutionDischargeProcessService;
import cn.cecep.talroad.domain.*;
import cn.cecep.talroad.mapper.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
@Slf4j
public class AirExcessYearAvgProcessService implements PollutionDischargeProcessService {

    @Autowired
    //b_stat_table_zxbg_gas_emission
    private BStatTableZxbgGasEmissionMapper bStatTableZxbgGasEmissionMapper;

    @Autowired
    private BMainActionReportsMapper mainActionReportsMapper;

    @Autowired
    private AmProblemFiMapper amProblemFiMapper;

    @Autowired
    private PcFactoryMapper factoryMapper;

    @Autowired
    private PcOutletMapper pcOutletMapper;

    /**
     * 0：月报，1：季报，2：年报
     */
    public static final String zxbgType = "2";

    /**
     * 1：年报，2：季报，3：月报
     */

    public static final String reportsType = "1";

    /**
     * 污染物来源 1废气、2废水 3 视频 可为空
     */
    public static final String pollutantSource = "1";

    @Override
    public void process() {

        // todo ：缺少查询报告周期条件
        List<BMainActionReports> report = getReport();

        for (BMainActionReports bMainActionReports : report) {

            LambdaQueryWrapper<BStatTableZxbgGasEmission> bStatTableZxbgGasEmissionLambdaQueryWrapper = new QueryWrapper<BStatTableZxbgGasEmission>().lambda()
                    .eq(BStatTableZxbgGasEmission::getZxbgType, zxbgType)
                    .eq(BStatTableZxbgGasEmission::getZxbgPeriod, bMainActionReports.getReportsTime())
                    .eq(BStatTableZxbgGasEmission::getFactoryId, bMainActionReports.getFactoryId());
            List<BStatTableZxbgGasEmission> bStatTableZxbgPflWaters = bStatTableZxbgGasEmissionMapper.selectList(bStatTableZxbgGasEmissionLambdaQueryWrapper);

            for (BStatTableZxbgGasEmission bStatTableZxbgGasEmission : bStatTableZxbgPflWaters) {
                BigDecimal folds = new BigDecimal(bStatTableZxbgGasEmission.getFoldsAvgHour());
                BigDecimal emiss = new BigDecimal(bStatTableZxbgGasEmission.getEmissionConcentration());
                if (emiss.doubleValue()>folds.doubleValue()){
                    addAmProblemFi(bStatTableZxbgGasEmission);
                }

            }

        }


    }

    public void addAmProblemFi(BStatTableZxbgGasEmission bStatTableZxbgPflAir){

        PcFactory pcFactory = factoryMapper.selectById(bStatTableZxbgPflAir.getFactoryId());

        if (pcFactory==null){
            log.warn("企业id不存在 , 企业ID : {}",bStatTableZxbgPflAir.getFactoryId());
        }

        LambdaQueryWrapper<PcOutlet> eq = new QueryWrapper<PcOutlet>().lambda().eq(PcOutlet::getOutletCode, bStatTableZxbgPflAir.getOutletCode());
        PcOutlet pcOutlet = pcOutletMapper.selectOne(eq);

        String problemString = getProblemString(pcFactory.getFactoryName() , pcOutlet.getOutletCode() , pcOutlet.getOutletName(), bStatTableZxbgPflAir.getZxbgPeriod(), bStatTableZxbgPflAir.getPollutantName());

        AmProblemFi amProblemFi = new AmProblemFi();
        amProblemFi.setFactoryId(pcFactory.getId());
        amProblemFi.setAnalyseTime(new Date());
        amProblemFi.setType(1);
        amProblemFi.setPollutantSourceAi(pollutantSource);
        amProblemFi.setDetails(problemString);
        amProblemFiMapper.insert(amProblemFi);

    }

    /**
     * 执“企业名称”“执行报告周期（2013年执行报告年报/2013年执行报告第一季度季报。。。）”中“排口编号”“排口名称”“污染物名称”监测结果平均值超过许可浓度限值，存在超标排污行为。
     */
    public String getProblemString(String factoryName , String outletCode , String outletName , String zxbgPeriod , String pollutantName){
        return new StringBuilder()
                .append("企业名称").append(factoryName).append("(").append(zxbgPeriod).append(")")
                .append("中").append(outletCode).append(outletName).append(pollutantName).append("监测结果平均值超过许可浓度限值，存在超标排污行为。")
                .toString();
    }

    /**
     * 查询报告周期
     * todo ：缺少查询报告周期条件
     * @return
     */
    public List<BMainActionReports> getReport(){
        LambdaQueryWrapper<BMainActionReports> bMainActionReportsLambdaQueryWrapper = new QueryWrapper<BMainActionReports>().lambda().eq(BMainActionReports::getReportsType, reportsType);
        List<BMainActionReports> bMainActionReports = mainActionReportsMapper.selectList(bMainActionReportsLambdaQueryWrapper);
        return bMainActionReports;
    }

}
