package cn.cecep.talroad.data.analyse.task.execute.report.specifications.month;

import cn.cecep.talroad.domain.BMainActionReports;
import cn.cecep.talroad.domain.BMainPollDisEnforcement;
import cn.cecep.talroad.domain.analysis.SRaActionReportsAudit;
import cn.cecep.talroad.enums.EmlTypeEnums;
import cn.cecep.talroad.mapper.BMainActionReportsMapper;
import cn.cecep.talroad.mapper.BMainPollDisEnforcementMapper;
import cn.cecep.talroad.mapper.analysis.SRaActionReportsAuditMapper;
import cn.cecep.talroad.util.DateUtil;
import cn.hutool.core.util.IdUtil;
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;

/**
 * @author hehongyu
 * @description: 执行报告异常分析行为
 * @date 2023/8/6 19:34
 */
@Slf4j
@Service
public class AnomalyAnalysisService {
    @Autowired
    private BMainPollDisEnforcementMapper enforcementMapper;

    @Autowired
    private BMainActionReportsMapper reportsMapper;

    @Autowired
    private SRaActionReportsAuditMapper actionReportsAuditMapper;
    /**
     * 分析任务
     * 应提交企业数量：把所有需要填报这个报告类型的企业数量加总。
     * 已提交企业数量：目前已经提交个这个报告的企业数量加总。
     * 提交率：已提交企业数量/应提交企业数量
     */
    public void execute(EmlTypeEnums type){
        log.info("执行报告异常分析行为 类型 ：{}",type);
        Integer shouldSubmitCount = countShouldSubmit(type);
        //只有月报可能会跳过
        if (shouldSubmitCount == 0 && EmlTypeEnums.MONTH.equals(type)){
            log.info("执行报告异常分析行为 无应该提交的报表数据 ");
            return;
        }
        Integer alreadySubmitCount = countAlreadySubmit(type);
        BigDecimal rate = BigDecimal.ZERO;
        if (alreadySubmitCount != 0){
            rate = new BigDecimal(alreadySubmitCount).divide(new BigDecimal(shouldSubmitCount),BigDecimal.ROUND_FLOOR);
        }
        SRaActionReportsAudit sRaActionReportsAudit = new SRaActionReportsAudit();
        sRaActionReportsAudit.setId(IdUtil.simpleUUID());
        sRaActionReportsAudit.setAuditStatus(1L);
        sRaActionReportsAudit.setReportType(type.getSimple());
        sRaActionReportsAudit.setReportTime(getNowTime(type));
        sRaActionReportsAudit.setTotalEnterprises(shouldSubmitCount.longValue());
        sRaActionReportsAudit.setSubmittedEnterprises(alreadySubmitCount.longValue());
        sRaActionReportsAudit.setSubmissionRate(rate);
        //TODO 未提交企业清单  暂时置空
        sRaActionReportsAudit.setNonSubmittedEnterprisesUrl("");
        sRaActionReportsAudit.setCreateBy("Timed tasks");
        sRaActionReportsAudit.setUpdateBy("Timed tasks");
        sRaActionReportsAudit.setCreateTime(new Date());
        sRaActionReportsAudit.setUpdateTime(new Date());
        actionReportsAuditMapper.insert(sRaActionReportsAudit);
    }


    /**
     * 应该提交的企业个数
     */
    private Integer countShouldSubmit(EmlTypeEnums type){
        LambdaQueryWrapper<BMainPollDisEnforcement> queryWrapper = new QueryWrapper<BMainPollDisEnforcement>().lambda()
                .eq(BMainPollDisEnforcement::getEmlType, type.getName());
        Integer count = enforcementMapper.selectCount(queryWrapper);
        return count == null ? 0 : count;
    }

    /**
     * 应该已经提交的企业个数
     */
    private Integer countAlreadySubmit(EmlTypeEnums type){
        LambdaQueryWrapper<BMainActionReports> queryWrapper = new QueryWrapper<BMainActionReports>().lambda()
                .eq(BMainActionReports::getReportsType, type.getCode());
        Integer count = reportsMapper.selectCount(queryWrapper);
        return count == null ? 0 : count;
    }

    private String getNowTime(EmlTypeEnums type){
        if (EmlTypeEnums.YEAR.equals(type)){
            return DateUtil.getYear();
        }else if (EmlTypeEnums.MONTH.equals(type)){
            return DateUtil.getYearMonth();
        }else if (EmlTypeEnums.DAY.equals(type)){
            return DateUtil.getDay();
        }else{
            //季度：202301(第一季度)
            return DateUtil.getYear()+"0"+DateUtil.getQuarter(DateUtil.getMonth());
        }
    }
}
