# SvaScheduleHoursTask.java

    package cloud.wconnect.workperformance.businesstask.sva.actualscheduledhours;

    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.workperformance.cons.BusinessEnum;
    import cloud.wconnect.workperformance.cons.EventSubjectEnum;
    import cloud.wconnect.workperformance.entity.sva.SvaScheduler;
    import cloud.wconnect.workperformance.service.sva.scheduleactual.ISvaSendNotifyService;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import jakarta.annotation.Resource;
    import lombok.extern.slf4j.Slf4j;
    import org.apache.commons.lang3.StringUtils;
    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;

    /**
     * 定时任务：SVA Schedule Hours 是否存在数据缺失
     *
     * @author yyt
     * @date 2025/03/26
     */
    @Slf4j
    @Component
    public class SvaScheduleHoursTask {
        @Resource
        private CheckReportMissedService checkReportMissedService;
        @Resource
        private PropertyI18nRedisService propertyI18nRedisService;
        @Resource
        private ISvaSendNotifyService sendNotifyService;
        @Resource
        private ObjectMapper objectMapper;

        @Value("${default.timezone}")
        private String defaultTimezone;

        @Value("${here.link.sva.av-hours}")
        private String link;

        private final static String BUSINESS = BusinessEnum.SVA_AV_HOURS.getBusiness();
        private final static String SUBJECT = EventSubjectEnum.ATTENDANCE_AV_SCHEDULE_HOURS.getSubject();

        /**
         * 检查今天的报告数据是否缺失
         *
         * @param schedulerJob 调度数据
         * @author yyt
         * @date 2025/03/26
         */
        @Async
        public void reportMissingCheck(SvaScheduler schedulerJob) {
            log.info("SvaScheduleHoursTask.reportMissingCheck start...");

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

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

            // 获取通知配置
            String reportName = "Schedule Hours Report";
            String missDate = getMissDate(propertyDTO, jobParameterDTO);
            String hereLink = link + "?tenant=" + propertyDTO.getTenantSymbol() + "&property=" + propertyDTO.getAbbr();
            NotifyDTO notifyDTO = sendNotifyService.getNotifyDTO(propertyDTO, reportName, missDate, hereLink, 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("SvaScheduleHoursTask.reportMissingCheck end...");
        }

        /**
         * 获取报告缺失日期
         *
         * @param propertyDTO     资产
         * @param jobParameterDTO 调度任务参数DTO
         * @return 缺失日期
         * @author yyt
         * @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);
            String startDateStr = InternationalizationUtils.formatDateTime(dateRange.get(JobParameterDTO.START_DATE),
                    pattern, StringUtils.EMPTY, timezone, language);
            String endDateStr = InternationalizationUtils.formatDateTime(dateRange.get(JobParameterDTO.END_DATE),
                    pattern, StringUtils.EMPTY, timezone, language);
            return startDateStr + " ~ " + endDateStr;
        }
    }