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

import cn.cecep.talroad.common.core.utils.DateUtils;
import cn.cecep.talroad.domain.AmProblemFi;
import cn.cecep.talroad.domain.BMainPollDis;
import cn.cecep.talroad.domain.BMainPollDis.PwxkzType;
import cn.cecep.talroad.domain.BasThresholdConfig;
import cn.cecep.talroad.enums.ProblemTypeEnum;
import cn.cecep.talroad.enums.ThresHoldConfigEnum;
import cn.cecep.talroad.service.IAmProblemFiService;
import cn.cecep.talroad.service.IBMainPollDisService;
import cn.cecep.talroad.service.IBasThresholdConfigService;
import cn.cecep.talroad.service.analysis.IDisPermitService;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Auther: cjh
 * @Date: 2023-06-09 16:05
 * @Class: IDisPermitServiceImpl
 * @Deseription:
 * @Version V1.0
 */
@Service
public class IDisPermitServiceImpl implements IDisPermitService {

    @Autowired
    private IBMainPollDisService ibMainPollDisService;
    @Autowired
    private IBasThresholdConfigService basThresholdConfigService;
    @Autowired
    private IAmProblemFiService amProblemFiService;


    @Override
    public void doDisPermitWarn() {
        List<AmProblemFi> alarmList = new ArrayList<>();
        BasThresholdConfig config = basThresholdConfigService.selectByParamCode(ThresHoldConfigEnum.X01.getCode());
        if(config!=null) {
            Date curr = new Date();
            String currDay = DateUtils.parseDateToStr("yyyy-MM-dd", curr);
            String deadLineDay = DateUtils.parseDateToStr("yyyy-MM-dd", DateUtils.addDateDays(curr, config.getThresholdValue().intValue()));

            List<BMainPollDis> list = ibMainPollDisService.selectDisPermitData(PwxkzType.TYPE_3.code, currDay, deadLineDay);
            list.forEach(d ->{
                StringBuffer sb = new StringBuffer();
                sb.append("贵单位排污许可证（许可证编号：").append(d.getPollDisLicenseCode()).append("）将于").append(d.getDeadlineTime());
                sb.append("届满，请及时登录全国排污许可证管理信息平台进行排污许可证延续。");
                doSaveAlarmData(alarmList, d,ProblemTypeEnum.PERMIT_WARN, sb);
            });
            if(alarmList!=null&&alarmList.size()>0) {
                amProblemFiService.saveBatch(alarmList);
            }
        }
    }


    @Override
    public void doDisPermitExceed() {
        List<AmProblemFi> alarmList = new ArrayList<>();
        BasThresholdConfig config = basThresholdConfigService.selectByParamCode(ThresHoldConfigEnum.X02.getCode());
        if(config!=null) {
            Date curr = new Date();
            String currDay = DateUtils.parseDateToStr("yyyy-MM-dd", curr);
            String deadLineDay = DateUtils.parseDateToStr("yyyy-MM-dd", DateUtils.addDateDays(curr, config.getThresholdValue().intValue()));

            List<BMainPollDis> list = ibMainPollDisService.selectDisPermitData(PwxkzType.TYPE_4.code, currDay, deadLineDay);
            list.forEach(d ->{
                StringBuffer sb = new StringBuffer();
                sb.append("贵单位排污许可证（整改通知书编号：").append(d.getPollDisLicenseCode()).append("）将于").append(d.getDeadlineTime());
                sb.append("届满，请及时登录全国排污许可证管理信息平台在排污许可证申请模块进行排污许可证整改后申请。");
                doSaveAlarmData(alarmList, d,ProblemTypeEnum.PERMIT_WARN, sb);
            });

            if(alarmList!=null&&alarmList.size()>0) {
                amProblemFiService.saveBatch(alarmList);
            }
        }

    }


    /**
     * 保存预警预报数据
     * @param alarmList
     * @param d
     * @param sb
     */
    private void doSaveAlarmData(List<AmProblemFi> alarmList, BMainPollDis d,ProblemTypeEnum problemTypeEnum, StringBuffer sb) {
        AmProblemFi problemFi = new AmProblemFi();
        problemFi.setProblemSources("1");
        problemFi.setFactoryId(d.getFactoryId());
        problemFi.setProblemDetails(sb.toString());
        problemFi.setType(1);
        problemFi.setProblemType(problemTypeEnum.getCode());
        problemFi.setProblemTreatmentState("1");
        problemFi.setCreateTime(new Date());
        alarmList.add(problemFi);
    }
}
