# TaPortalServiceTask.java

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

    import cloud.wconnect.commonlib.cons.BaseConstant;
    import cloud.wconnect.commonlib.cons.ReportImportResultCons;
    import cloud.wconnect.commonlib.pojo.dto.NotifyDTO;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.pojo.dto.scheduler.JobParameterDTO;
    import cloud.wconnect.commonlib.redis.PropertyI18nRedisService;
    import cloud.wconnect.commonlib.service.NotifyInternalService;
    import cloud.wconnect.commonlib.util.InternationalizationUtils;
    import cloud.wconnect.commonlib.util.JobParameterUtils;
    import cloud.wconnect.commonlib.util.JsonUtils;
    import cloud.wconnect.finance.cons.ta.TaReportTypeCons;
    import cloud.wconnect.finance.entity.ta.TaScheduler;
    import cloud.wconnect.finance.service.ta.ITaImportLogService;
    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.scheduling.annotation.Async;
    import org.springframework.stereotype.Component;

    import jakarta.annotation.Resource;
    import java.util.Collections;
    import java.util.Map;
    import java.util.Objects;

    /**
     * 定时任务：TA - 检查 Portal 报告文件是否按时导入
     *
     * @author zc
     * @date 2023/07/28
     */
    @Slf4j
    @Component
    public class TaPortalServiceTask {
        @Resource
        private ITaImportLogService importLogService;
        @Resource
        private ITaSendNotifyService sendNotifyService;
        @Resource
        private PropertyI18nRedisService propertyI18nRedisService;
        @Resource
        private NotifyInternalService notifyInternalService;
        @Resource
        private ObjectMapper objectMapper;

        /**
         * 检查是否存在报告数据缺失
         *
         * <p>
         * 检查报告缺失：每天上午 10:30:00
         * </p>
         *
         * @author zc
         * @date 2023/7/28
         */
        @Async
        public void reportMissingCheck(TaScheduler schedulerJob) {
            log.info("TaPortalServiceTask.reportMissingCheck start...");

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

            JobParameterDTO jobParameterDTO = JsonUtils.convertStringToObject(objectMapper,
                    schedulerJob.getParameters(), JobParameterDTO.class);
            // 获取报告日期范围
            Map<String, Long> dateRange = JobParameterUtils.parseDateRange(jobParameterDTO.getDateRange(),
                    jobParameterDTO.getDateRangeUnit(), propertyDTO.getTimezone(), null);

            // 根据日期范围检查是否有数据
            long dataDate = dateRange.get(JobParameterDTO.START_DATE);
            if (checkHasImportLog(propertyDTO, dataDate)) {
                log.info("TaPortalServiceTask.reportMissingCheck end... data has been entered");
                return;
            }

            // 记录报告缺失日志
            importLogService.add(propertyDTO, dataDate, TaReportTypeCons.PORTAL,
                    ReportImportResultCons.IMPORT_RESULT_MISSED, BaseConstant.ZERO, null);

            // 获取通知配置
            String reportName = "Portal";
            String missDateStr = InternationalizationUtils.formatDateTime(dataDate,
                    propertyDTO.getInternationalization().getDateTime().getLongDate(),
                    StringUtils.EMPTY, propertyDTO.getTimezone(), propertyDTO.getLanguage());

            // 获取通知配置
            NotifyDTO notify = sendNotifyService.getNotifyDTO(propertyDTO, reportName, missDateStr, schedulerJob.getCategory());
            if (Objects.isNull(notify)) {
                log.info("TaPortalServiceTask.reportMissingCheck end... notifyDTO is null");
                return;
            }

            // 发送通知
            notifyInternalService.sendNotify(notify);

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

        /**
         * 校验是否生成日志
         *
         * @param property 资产
         * @param dataDate 数据日期
         * @return true：已存在日志；false：无日志
         * @author zc
         * @date 2023/7/28
         */
        private boolean checkHasImportLog(PropertyDTO property, long dataDate) {
            long count = importLogService.count(property.getTenantId(), property.getId(),
                    TaReportTypeCons.PORTAL, dataDate, Collections.emptyList());
            return count > BaseConstant.LONG_ZERO;
        }
    }