# ScheduleHistoryAndForecastTask.java

    package cloud.wconnect.workperformance.businesstask.schedule;

    import cloud.wconnect.commonlib.businesstask.CheckReportMissedService;
    import cloud.wconnect.commonlib.businesstask.rule.MissedRuleByThreshold;
    import cloud.wconnect.commonlib.cons.PmsReportEnum;
    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.internationalization.InternationalizationDateTimeDTO;
    import cloud.wconnect.commonlib.redis.PropertyI18nRedisService;
    import cloud.wconnect.commonlib.service.PropertyInternalService;
    import cloud.wconnect.commonlib.util.InternationalizationUtils;
    import cloud.wconnect.workperformance.cons.BusinessEnum;
    import cloud.wconnect.workperformance.cons.EventSubjectEnum;
    import cloud.wconnect.workperformance.entity.schedule.ScheduleSchedulerJob;
    import cloud.wconnect.workperformance.service.schedule.IScheduleWorkDayService;
    import cloud.wconnect.workperformance.service.schedule.impl.ScheduleWorkDayServiceImpl;
    import lombok.extern.slf4j.Slf4j;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.scheduling.annotation.Async;
    import org.springframework.stereotype.Component;

    import java.util.Collections;
    import java.util.Map;

    /**
     * Schedule History And Forecast Task
     *
     * @author yyt
     * 2023/7/5
     */
    @Slf4j
    @Component
    public class ScheduleHistoryAndForecastTask {

        @Autowired
        private PropertyI18nRedisService propertyI18nRedisService;
        @Autowired
        private PropertyInternalService propertyInternalService;
        @Autowired
        private CheckReportMissedService checkReportMissedService;
        @Autowired
        private PackageNotifyDTOUtil packageNotifyDTOUtil;
        @Autowired
        private IScheduleWorkDayService scheduleWorkDayService;

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

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

        /**
         * 检查是否存在数据缺失
         *
         * @param schedulerJob 定时任务
         * @author yyt
         * @date 2023/7/5
         */
        @Async
        public void reportMissCheck(ScheduleSchedulerJob schedulerJob) {
            log.info("ScheduleHistoryAndForecastTask.reportMissCheck start...");

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

            // 获取报告缺失日期
            Map<String, Long> dateRange = scheduleWorkDayService.calculateValidDateRange(property);
            Long startDate = dateRange.get(ScheduleWorkDayServiceImpl.START_DATE);
            Long endDate = dateRange.get(ScheduleWorkDayServiceImpl.END_DATE);
            String timezone = property.getTimezone();
            String language = property.getLanguage();
            InternationalizationDateTimeDTO dateTime = property.getInternationalization().getDateTime();
            String startDateStr = InternationalizationUtils.formatDateTime(startDate, dateTime.getLongDate(),
                    StringUtils.EMPTY, timezone, language);
            String endDateStr = InternationalizationUtils.formatDateTime(endDate, dateTime.getLongDate(),
                    StringUtils.EMPTY, timezone, language);
            String missDate = startDateStr + " ~ " + endDateStr;

            // 获取资产配置的 PMS 报告名称
            String reportName = propertyInternalService.getPmsReportConfig(propertyId,
                    property.getPmsType(), PmsReportEnum.HISTORY_AND_FORECAST.getSymbol()).getReportName();
            NotifyDTO notifyDTO = packageNotifyDTOUtil.getNotifyDTO(property, missDate, reportName,
                    schedulerJob.getCategory());

            CheckReportMissedDTO check = CheckReportMissedDTO.builder()
                    .subject(SUBJECT)
                    .business(BUSINESS)
                    .propertyDTO(property)
                    .ruleClazzList(Collections.singletonList(MissedRuleByThreshold.class))
                    .notifyDTO(notifyDTO)
                    .thresholdDTO(packageNotifyDTOUtil.getThresholdDTO(schedulerJob))
                    .build();
            checkReportMissedService.doCheck(check);

            log.info("ScheduleHistoryAndForecastTask.reportMissCheck end...");
        }

    }