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.AmProblemFiMapper;
import cn.cecep.talroad.mapper.BMainActionReportsMapper;
import cn.cecep.talroad.mapper.BStatTableZxbgPflWaterMapper;
import cn.cecep.talroad.mapper.PcFactoryMapper;
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;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class WaterExcessMonthProcessService implements PollutionDischargeProcessService {

    @Autowired
    private BStatTableZxbgPflWaterMapper statTableZxbgPflWaterMapper;

    @Autowired
    private BMainActionReportsMapper mainActionReportsMapper;

    @Autowired
    private AmProblemFiMapper amProblemFiMapper;

    @Autowired
    private PcFactoryMapper factoryMapper;

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

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

    public static final String reportsType = "3";

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

    @Override
    public void process() {

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

        for (BMainActionReports bMainActionReports : report) {

            LambdaQueryWrapper<BStatTableZxbgPflWater> bStatTableZxbgPflWaterLambdaQueryWrapper = new QueryWrapper<BStatTableZxbgPflWater>().lambda()
                    .eq(BStatTableZxbgPflWater::getZxbgType, zxbgType)
                    .eq(BStatTableZxbgPflWater::getZxbgPeriod, bMainActionReports.getReportsTime())
                    .eq(BStatTableZxbgPflWater::getFactoryId, bMainActionReports.getFactoryId());
            List<BStatTableZxbgPflWater> bStatTableZxbgPflWaters = statTableZxbgPflWaterMapper.selectList(bStatTableZxbgPflWaterLambdaQueryWrapper);

            for (BStatTableZxbgPflWater bStatTableZxbgPflWater : bStatTableZxbgPflWaters) {
                BigDecimal zxbgYear = new BigDecimal(bStatTableZxbgPflWater.getZxbgDischargeYear());
                BigDecimal zxbgActual = new BigDecimal(bStatTableZxbgPflWater.getZxbgDischargeActual());
                if (zxbgActual.doubleValue()>zxbgYear.doubleValue()){
                    //todo 添加报警信息
                }

            }

        }


    }

    public void addAmProblemFi(BStatTableZxbgPflAir bStatTableZxbgPflAir){

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

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

        String problemString = getProblemString(pcFactory.getFactoryName(), bStatTableZxbgPflAir.getZxbgPeriod(), bStatTableZxbgPflAir.getZxbgPollutant());

        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 zxbgPeriod , String pollutantName){
        return new StringBuilder()
                .append(factoryName).append("(").append(zxbgPeriod).append(")")
                .append("中").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;
    }
}
