# ScheduleHistoryAndForecastPostProcessor.java

    package cloud.wconnect.workperformance.businesstask.schedule;

    import cloud.wconnect.commonlib.businesstask.postprocessor.IReportMissedPostProcessor;
    import cloud.wconnect.commonlib.cons.BaseConstant;
    import cloud.wconnect.commonlib.cons.ReportImportResultCons;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.pojo.dto.ReportMissedDTO;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.workperformance.cons.BusinessEnum;
    import cloud.wconnect.workperformance.cons.EventSubjectEnum;
    import cloud.wconnect.workperformance.entity.schedule.ScheduleHistoryAndForecastImportLog;
    import cloud.wconnect.workperformance.service.schedule.IScheduleHistoryAndForecastImportLogService;
    import cloud.wconnect.workperformance.service.schedule.IScheduleWorkDayService;
    import cloud.wconnect.workperformance.service.schedule.impl.ScheduleWorkDayServiceImpl;
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;

    import java.util.Map;
    import java.util.Objects;

    /**
     * history and forecast 报告缺失后续逻辑处理
     *
     * @author yyt
     * 2023/7/13
     */
    @Component
    public class ScheduleHistoryAndForecastPostProcessor implements IReportMissedPostProcessor {

        @Autowired
        private IScheduleHistoryAndForecastImportLogService scheduleHistoryAndForecastImportLogService;
        @Autowired
        private IScheduleWorkDayService scheduleWorkDayService;

        /**
         * 业务名称
         */
        private final static String BUSINESS = BusinessEnum.SCHEDULE_HISTORY_AND_FORECAST.getBusiness();
        /**
         * subject
         */
        private final static String SUBJECT = EventSubjectEnum.HISTORY_AND_FORECAST_SERVICE_YESTERDAY_NINETY_DAY.getSubject();

        @Override
        public boolean supports(ReportMissedDTO reportMissedDTO) {
            return reportMissedDTO.getBusiness().equals(BUSINESS) && reportMissedDTO.getSubject().equals(SUBJECT);
        }

        @Override
        public void afterPostProcess(ReportMissedDTO reportMissedDTO) {
            // 获取数据处理范围
            PropertyDTO property = reportMissedDTO.getPropertyDTO();
            Map<String, Long> dateRange = scheduleWorkDayService.calculateValidDateRange(property);
            // 没有 报告缺失异常时，插入异常
            long dateNow = DateTimeUtils.dateNowTimestamp(property.getTimezone());
            Long startDate = dateRange.get(ScheduleWorkDayServiceImpl.START_DATE);
            Long endDate = dateRange.get(ScheduleWorkDayServiceImpl.END_DATE);
            ScheduleHistoryAndForecastImportLog exceptionImportLog =
                    scheduleHistoryAndForecastImportLogService.getOne(new QueryWrapper<ScheduleHistoryAndForecastImportLog>().lambda()
                            .eq(ScheduleHistoryAndForecastImportLog::getPropertyId, property.getId())
                            .eq(ScheduleHistoryAndForecastImportLog::getTheDate, dateNow)
                            .eq(ScheduleHistoryAndForecastImportLog::getFromDate, startDate)
                            .eq(ScheduleHistoryAndForecastImportLog::getToDate, endDate)
                            .eq(ScheduleHistoryAndForecastImportLog::getImportResult, ReportImportResultCons.IMPORT_RESULT_MISSED)
                            .eq(ScheduleHistoryAndForecastImportLog::getCreatedBy, BaseConstant.ZERO)
                            .last("limit 1"));
            if (Objects.nonNull(exceptionImportLog)) {
                return;
            }

            // 插入异常记录
            long now = DateTimeUtils.nowTimestamp();
            scheduleHistoryAndForecastImportLogService.save(new ScheduleHistoryAndForecastImportLog()
                    .setPropertyId(property.getId())
                    .setTheDate(dateNow)
                    .setFromDate(startDate)
                    .setToDate(endDate)
                    .setImportResult(ReportImportResultCons.IMPORT_RESULT_MISSED)
                    .setCreatedBy(0)
                    .setCreatedTime(now));
        }

    }