# TaCreditCardServiceTask.java

    package cloud.wconnect.finance.businesstask.ta.creditcard;

    import cloud.wconnect.commonlib.businesstask.CheckReportMissedService;
    import cloud.wconnect.commonlib.businesstask.rule.MissedRuleByThreshold;
    import cloud.wconnect.commonlib.pojo.dto.CheckReportMissedDTO;
    import cloud.wconnect.commonlib.pojo.dto.NotifyDTO;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.pojo.dto.ThresholdDTO;
    import cloud.wconnect.commonlib.pojo.dto.scheduler.JobParameterDTO;
    import cloud.wconnect.commonlib.redis.PropertyI18nRedisService;
    import cloud.wconnect.commonlib.util.InternationalizationUtils;
    import cloud.wconnect.commonlib.util.JobParameterUtils;
    import cloud.wconnect.commonlib.util.JsonUtils;
    import cloud.wconnect.finance.cons.BusinessEnum;
    import cloud.wconnect.finance.cons.EventSubjectEnum;
    import cloud.wconnect.finance.entity.ta.TaScheduler;
    import cloud.wconnect.finance.service.ta.ITaSendNotifyService;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import lombok.extern.slf4j.Slf4j;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.scheduling.annotation.Async;
    import org.springframework.stereotype.Component;

    import java.util.List;
    import java.util.Map;
    import java.util.concurrent.TimeUnit;

    /**
     * 定时任务：TA - 检查 Credit Card 报告文件是否按时导入
     *
     * @author zc
     * @date 2023/07/28
     */
    @Slf4j
    @Component
    public class TaCreditCardServiceTask {

        @Autowired
        private ITaSendNotifyService sendNotifyService;
        @Autowired
        private PropertyI18nRedisService propertyI18nRedisService;
        @Autowired
        private CheckReportMissedService checkReportMissedService;
        @Autowired
        private ObjectMapper objectMapper;
        @Value("${default.timezone}")
        private String defaultTimezone;

        private static final String BUSINESS = BusinessEnum.TA.getBusiness();

        private final static String SUBJECT = EventSubjectEnum.CREDIT_CARD.getSubject();

        /**
         * 检查是否存在报告数据缺失
         *
         * <p>
         * 报告下发时间：每天上午 10:00:00
         * 检查报告缺失：每天上午 10:30:00
         * 阈值：阈值：23 小时 59 分钟
         * </p>
         *
         * @param schedulerJob 调度数据
         * @author zc
         * @date 2023/7/28
         */
        @Async
        public void reportMissingCheck(TaScheduler schedulerJob) {
            log.info("TaCreditCardServiceTask.reportMissingCheck start...");

            int propertyId = schedulerJob.getPropertyId();
            PropertyDTO propertyDTO = propertyI18nRedisService.get(propertyId);
            if (!propertyDTO.getStatus()) {
                log.warn("TaCreditCardServiceTask.reportMissingCheck end... the property is closed, no processing required");
                return;
            }

            JobParameterDTO jobParameterDTO = JsonUtils.convertStringToObject(objectMapper,
                    schedulerJob.getParameters(), JobParameterDTO.class);

            // 获取通知配置
            String reportName = "Credit Card";
            String missDateStr = getMissDate(propertyDTO, jobParameterDTO);
            NotifyDTO notifyDTO = sendNotifyService.getNotifyDTO(propertyDTO, reportName, missDateStr, schedulerJob.getCategory());

            // 获取资产配置的阈值
            ThresholdDTO thresholdDTO = new ThresholdDTO(jobParameterDTO.getThreshold(), TimeUnit.MINUTES, defaultTimezone);

            // 封装 CheckReportMissedDTO 对象
            CheckReportMissedDTO job = CheckReportMissedDTO.builder()
                    .business(BUSINESS)
                    .subject(SUBJECT)
                    .ruleClazzList(List.of(MissedRuleByThreshold.class))
                    .propertyDTO(propertyDTO)
                    .notifyDTO(notifyDTO)
                    .thresholdDTO(thresholdDTO)
                    .build();
            checkReportMissedService.doCheck(job);

            log.info("TaCreditCardServiceTask.reportMissingCheck end...");
        }

        /**
         * 获取报告缺失日期
         *
         * @param propertyDTO 资产
         * @param jobParameterDTO 调度任务参数DTO
         * @return 缺失日期
         * @author zc
         * @date 2024/2/29
         */
        private String getMissDate(PropertyDTO propertyDTO, JobParameterDTO jobParameterDTO) {
            String timezone = propertyDTO.getTimezone();
            String language = propertyDTO.getLanguage();
            String pattern = propertyDTO.getInternationalization().getDateTime().getLongDate();

            // 解析日期表达式
            Map<String, Long> dateRange = JobParameterUtils.parseDateRange(jobParameterDTO.getDateRange(),
                    jobParameterDTO.getDateRangeUnit(), propertyDTO.getTimezone(), null);
            return InternationalizationUtils.formatDateTime(dateRange.get(JobParameterDTO.START_DATE),
                    pattern, StringUtils.EMPTY, timezone, language);
        }
    }