# JournalTask.java

    package cloud.wconnect.finance.businesstask.chargeback;

    import cloud.wconnect.commonlib.businesstask.CheckReportMissedService;
    import cloud.wconnect.commonlib.businesstask.rule.MissedRuleByThreshold;
    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.cons.FunctionCons;
    import cloud.wconnect.commonlib.cons.PmsReportEnum;
    import cloud.wconnect.commonlib.pojo.dto.*;
    import cloud.wconnect.commonlib.pojo.dto.line.ReportMissedMsgParm;
    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.*;
    import cloud.wconnect.finance.cons.BusinessEnum;
    import cloud.wconnect.finance.cons.EventSubjectEnum;
    import cloud.wconnect.finance.cons.FunctionPointEnum;
    import cloud.wconnect.finance.cons.chargeback.ChargebackNotifyEnum;
    import cloud.wconnect.finance.entity.chargeback.CbCaseEmailConfig;
    import cloud.wconnect.finance.entity.chargeback.CbCaseEmailTarget;
    import cloud.wconnect.finance.entity.chargeback.CbCaseSchedulerJob;
    import cloud.wconnect.finance.pojo.dto.chargeback.CbCaseSchedulerDTO;
    import cloud.wconnect.finance.service.chargeback.ICbCaseEmailConfigService;
    import cloud.wconnect.finance.service.chargeback.ICbCaseEmailTargetService;
    import cloud.wconnect.finance.service.chargeback.ICbCaseSchedulerJobService;
    import com.alibaba.fastjson.JSONObject;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.google.common.collect.Lists;
    import lombok.extern.slf4j.Slf4j;
    import org.apache.commons.collections.CollectionUtils;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.scheduling.annotation.Async;
    import org.springframework.stereotype.Service;
    import org.thymeleaf.TemplateEngine;
    import org.thymeleaf.context.Context;

    import java.util.Collections;
    import java.util.List;
    import java.util.Objects;
    import java.util.concurrent.TimeUnit;

    /**
     * Journal
     *
     * @author yyt
     * 2023/8/8
     */
    @Service
    @Slf4j
    public class JournalTask {

        @Autowired
        private PropertyInternalService propertyInternalService;
        @Autowired
        private PropertyI18nRedisService propertyI18nRedisService;
        @Value("${default.timezone}")
        private String defaultTimezone;
        @Autowired
        private CheckReportMissedService checkReportMissedService;
        @Autowired
        private ICbCaseEmailConfigService cbCaseEmailConfigService;
        @Autowired
        private ICbCaseEmailTargetService cbCaseEmailTargetService;
        @Autowired
        private LineMessageUtil lineMessageUtil;
        @Autowired
        private I18nMessageUtils i18nMessageUtils;
        @Autowired
        private BeanMapper beanMapper;
        @Autowired
        private ICbCaseSchedulerJobService schedulerJobService;
        @Autowired
        private ObjectMapper objectMapper;
        @Autowired
        private TemplateEngine templateEngine;

        @Value("${here.link.chargeback.case-list}")
        private String hereLink;

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

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

        /**
         * 检查报告缺失
         *
         * @param schedulerJob 定时任务
         * @author yyt
         * @date 2023/9/8
         */
        @Async
        public void checkJournalReportMissing(CbCaseSchedulerJob schedulerJob) {
            log.info("JournalTask.JournalReportMissCheck start...");

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

            //资产配置的阈值集合
            JobParameterDTO jobParameterDTO = JsonUtils.convertStringToObject(objectMapper, schedulerJob.getParameters(), JobParameterDTO.class);
            ThresholdDTO thresholdDTO = new ThresholdDTO(jobParameterDTO.getThreshold(), TimeUnit.MINUTES, defaultTimezone);

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

            log.info("JournalTask.JournalReportMissCheck end...");
        }

        /**
         * 获取通知配置 DTO
         *
         * @param propertyDTO 资产
         * @return 通知配置 DTO
         * @author yyt
         * @date 2024/3/18
         **/
        private NotifyDTO getNotifyDTO(PropertyDTO propertyDTO) {
            Integer propertyId = propertyDTO.getId();
            CbCaseEmailConfig emailConfig = cbCaseEmailConfigService.get(propertyId, ChargebackNotifyEnum.MISSING_REPORT_JOURNAL.getCode(), true);
            if (Objects.isNull(emailConfig)) {
                return null;
            }
            List<CbCaseEmailTarget> emailTargets = cbCaseEmailTargetService.list(emailConfig.getId());
            if (CollectionUtils.isEmpty(emailTargets)) {
                return null;
            }

            String defaultSubject = cbCaseEmailConfigService.getDefaultSubject(propertyId, ChargebackNotifyEnum.MISSING_REPORT_JOURNAL.getCode());

            NotifyDTO notifyDTO = new NotifyDTO();
            notifyDTO.setPropertyId(propertyId);
            notifyDTO.setFunctionPoint(FunctionPointEnum.CHARGEBACK_PMS_REPORT_MISSED.getFunctionPoint());
            notifyDTO.setSubject(StringUtils.isNotBlank(emailConfig.getSubject()) ? emailConfig.getSubject() : defaultSubject);
            notifyDTO.setIdentityList(beanMapper.mapList(emailTargets, NotifyIdentityDTO.class));

            // 报告缺失日期
            String timezone = propertyDTO.getTimezone();
            String language = propertyDTO.getLanguage();
            Long date = schedulerJobService.getDateRange(propertyDTO, CbCaseSchedulerDTO.MISSING_JOURNAL_REPORT);
            String missDate = InternationalizationUtils.formatDateTime(Objects.isNull(date) ? 0 : date,
                    propertyDTO.getInternationalization().getDateTime().getLongDate(), StringUtils.EMPTY,
                    timezone, language);
            String functionName = i18nMessageUtils.message(FunctionCons.CHARGE_BACK, language);

            // 获取资产配置的 PMS 报告名称
            String reportName = propertyInternalService.getPmsReportConfig(propertyId,
                    propertyDTO.getPmsType(), PmsReportEnum.JOURNAL_BY_CASHIER_AND_TRANSACTION_CODES.getSymbol()).getReportName();

            String propertyName = propertyDTO.getName();
            JSONObject lineMsg = lineMessageUtil.generateReportMissedMsg(ReportMissedMsgParm.builder()
                    .propertyId(propertyId)
                    .propertyName(propertyName)
                    .functionName(functionName)
                    .reportName(reportName)
                    .missDate(missDate)
                    .build());
            notifyDTO.setLineContent(Lists.newArrayList(lineMsg));

            Context context = new Context();
            context.setVariable("propertyName", propertyName);
            context.setVariable("hereLink", hereLink + "?property=" + propertyDTO.getAbbr() + "&tenant=" + propertyDTO.getTenantSymbol());
            context.setVariable("functionName", functionName);
            context.setVariable("reportName", reportName);
            context.setVariable("reportDate", missDate);
            String emailContent = templateEngine.process(EmailUtil.getMissReportTemplateName(language), context);
            notifyDTO.setEmailContent(EmailUtil.getFullEmailContent(defaultSubject, emailContent, language));

            return notifyDTO;
        }

    }