# PackageNotifyDTOUtil.java

    package cloud.wconnect.workperformance.businesstask.schedule;

    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.cons.FunctionCons;
    import cloud.wconnect.commonlib.pojo.dto.NotifyDTO;
    import cloud.wconnect.commonlib.pojo.dto.NotifyIdentityDTO;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.pojo.dto.ThresholdDTO;
    import cloud.wconnect.commonlib.pojo.dto.line.ReportMissedMsgParm;
    import cloud.wconnect.commonlib.pojo.dto.scheduler.JobParameterDTO;
    import cloud.wconnect.commonlib.util.EmailUtil;
    import cloud.wconnect.commonlib.util.I18nMessageUtils;
    import cloud.wconnect.commonlib.util.JsonUtils;
    import cloud.wconnect.commonlib.util.LineMessageUtil;
    import cloud.wconnect.workperformance.cons.FunctionPointEnum;
    import cloud.wconnect.workperformance.entity.schedule.ScheduleEmailConfig;
    import cloud.wconnect.workperformance.entity.schedule.ScheduleEmailTarget;
    import cloud.wconnect.workperformance.entity.schedule.ScheduleSchedulerJob;
    import cloud.wconnect.workperformance.service.schedule.IScheduleEmailConfigService;
    import cloud.wconnect.workperformance.service.schedule.IScheduleEmailTargetService;
    import com.alibaba.fastjson.JSONObject;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.google.common.collect.Lists;
    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.stereotype.Component;
    import org.thymeleaf.TemplateEngine;
    import org.thymeleaf.context.Context;

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

    /**
     * 组装通知DTO 工具
     *
     * @author lichen
     * @date 2023/12/14
     */
    @Component
    public class PackageNotifyDTOUtil {

        @Autowired
        private LineMessageUtil lineMessageUtil;
        @Autowired
        private I18nMessageUtils i18nMessageUtils;
        @Autowired
        private BeanMapper beanMapper;
        @Autowired
        private IScheduleEmailConfigService scheduleEmailConfigService;
        @Autowired
        private IScheduleEmailTargetService scheduleEmailTargetService;
        @Autowired
        private ObjectMapper objectMapper;
        @Autowired
        private TemplateEngine templateEngine;

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

        @Value("${here.link.schedule}")
        private String hereLink;

        /**
         * 获取通知配置DTO
         *
         * @param propertyDTO 资产
         * @param missDate    报告缺失时间
         * @param reportName  报告名称
         * @param category    分类
         * @return 通知配置
         * @author lichen
         * @date 2023/12/15
         */
        public NotifyDTO getNotifyDTO(PropertyDTO propertyDTO, String missDate, String reportName, int category) {
            Integer propertyId = propertyDTO.getId();
            String propertyName = propertyDTO.getName();
            String language = propertyDTO.getLanguage();

            // 查询邮件配置
            ScheduleEmailConfig emailConfig = scheduleEmailConfigService.getEmailConfig(propertyId, category, true);
            if (Objects.isNull(emailConfig)) {
                return null;
            }
            List<ScheduleEmailTarget> emailTargets = scheduleEmailTargetService.list(emailConfig.getId());
            if (CollectionUtils.isEmpty(emailTargets)) {
                return null;
            }

            String defaultSubject = scheduleEmailConfigService.getDefaultSubject(propertyId, category);

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

            String functionName = i18nMessageUtils.message(FunctionCons.SCHEDULE, language);

            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));

            //line通知内容
            JSONObject lineMsg = lineMessageUtil.generateReportMissedMsg(ReportMissedMsgParm.builder()
                    .propertyId(propertyId)
                    .propertyName(propertyName)
                    .functionName(functionName)
                    .reportName(reportName)
                    .missDate(missDate)
                    .build());

            notifyDTO.setLineContent(Lists.newArrayList(lineMsg));
            return notifyDTO;
        }

        /**
         * 获取报告检查缺失的阈值
         *
         * @param schedulerJob 定时任务
         * @return 检查缺失的阈值
         * @author yyt
         * @date 2024/3/15
         **/
        ThresholdDTO getThresholdDTO(ScheduleSchedulerJob schedulerJob) {
            // 校验规则所需的参数
            JobParameterDTO jobParameterDTO = JsonUtils.convertStringToObject(objectMapper,
                    schedulerJob.getParameters(), JobParameterDTO.class);
            return new ThresholdDTO(jobParameterDTO.getThreshold(), TimeUnit.MINUTES, defaultTimezone);
        }

    }