# RevenueHistoryAndForecastYesterdayAndNinetyTask.java

    package cloud.wconnect.rateshop.businesstask.revenue;

    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.PropertyDTO;
    import cloud.wconnect.commonlib.pojo.dto.internationalization.InternationalizationDateTimeDTO;
    import cloud.wconnect.commonlib.pojo.dto.scheduler.JobParameterDTO;
    import cloud.wconnect.commonlib.redis.PropertyI18nRedisService;
    import cloud.wconnect.commonlib.service.PropertyInternalService;
    import cloud.wconnect.commonlib.util.InternationalizationUtils;
    import cloud.wconnect.commonlib.util.JobParameterUtils;
    import cloud.wconnect.commonlib.util.JsonUtils;
    import cloud.wconnect.rateshop.cons.BusinessEnum;
    import cloud.wconnect.rateshop.cons.EventSubjectEnum;
    import cloud.wconnect.rateshop.entity.revenue.RevenueSchedulerJob;
    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.scheduling.annotation.Async;
    import org.springframework.stereotype.Service;

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

    /**
     * 检测History and forecast -1,90 days 报告缺失
     *
     * @author lichen
     * @date 2024/3/26
     */
    @Slf4j
    @Service
    public class RevenueHistoryAndForecastYesterdayAndNinetyTask {

        @Autowired
        private CheckReportMissedService checkReportMissedService;
        @Autowired
        private PackageNotifyDTOUtil packageNotifyDTOUtil;
        @Autowired
        private ObjectMapper objectMapper;
        @Autowired
        private PropertyI18nRedisService propertyI18nRedisService;
        @Autowired
        private PropertyInternalService propertyInternalService;

        private static final String SUBJECT = EventSubjectEnum.HISTORY_AND_FORECAST_SERVICE_YESTERDAY_NINETY_DAY.getSubject();
        private static final String BUSINESS = BusinessEnum.REVENUE.getBusiness();

        /**
         * 检查报告缺失
         *
         * @param schedulerJob 定时任务
         * @author lichen
         * @date 2024/03/26
         */
        @Async
        public void checkReportMissing(RevenueSchedulerJob schedulerJob) {
            log.info("RevenueHistoryAndForecastYesterdayAndNinetyTask.checkReportMissing start...");

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

            //报告缺失日期
            InternationalizationDateTimeDTO dateTime = propertyDTO.getInternationalization().getDateTime();
            String missDate = "";
            String parameters = schedulerJob.getParameters();
            if (StringUtils.isNotBlank(parameters) && !parameters.equals("{}")) {
                JobParameterDTO jobParameterDTO = JsonUtils.convertStringToObject(objectMapper, parameters, JobParameterDTO.class);
                Map<String, Long> dataRange = JobParameterUtils.parseDateRange(jobParameterDTO.getDateRange(), jobParameterDTO.getDateRangeUnit(), propertyDTO.getTimezone(), null);
                String startDate = InternationalizationUtils.formatDateTime(dataRange.get(JobParameterDTO.START_DATE),
                        dateTime.getLongDate(), StringUtils.EMPTY, propertyDTO.getTimezone(), propertyDTO.getLanguage());
                String endDate = InternationalizationUtils.formatDateTime(dataRange.get(JobParameterDTO.END_DATE),
                        dateTime.getLongDate(), StringUtils.EMPTY, propertyDTO.getTimezone(), propertyDTO.getLanguage());
                missDate = startDate + " ~ " + endDate;
            }

            // 获取资产配置的 PMS 报告名称
            String reportName = propertyInternalService.getPmsReportConfig(propertyDTO.getId(),
                    propertyDTO.getPmsType(), PmsReportEnum.HISTORY_AND_FORECAST.getSymbol()).getReportName() + " yesterday to next 90 days";
            CheckReportMissedDTO check = CheckReportMissedDTO.builder()
                    .subject(SUBJECT)
                    .business(BUSINESS)
                    .propertyDTO(propertyDTO)
                    .ruleClazzList(Collections.singletonList(MissedRuleByThreshold.class))
                    .notifyDTO(packageNotifyDTOUtil.getNotifyDTO(propertyDTO, missDate, reportName, schedulerJob.getCategory()))
                    .thresholdDTO(packageNotifyDTOUtil.getThresholdDTO(schedulerJob))
                    .build();
            checkReportMissedService.doCheck(check);

            log.info("RevenueHistoryAndForecastYesterdayAndNinetyTask.checkReportMissing end...");
        }

    }