# RateShopPackageNotifyDTOUtil.java

    package cloud.wconnect.rateshop.businesstask.rateshop;

    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.line.ReportMissedMsgParm;
    import cloud.wconnect.commonlib.util.EmailUtil;
    import cloud.wconnect.commonlib.util.I18nMessageUtils;
    import cloud.wconnect.commonlib.util.InternationalizationUtils;
    import cloud.wconnect.commonlib.util.LineMessageUtil;
    import cloud.wconnect.rateshop.cons.FunctionPointEnum;
    import cloud.wconnect.rateshop.cons.rateshop.RateShopNotifyConfigTypeEnum;
    import cloud.wconnect.rateshop.entity.rateshop.RateShopEmailConfig;
    import cloud.wconnect.rateshop.entity.rateshop.RateShopEmailTarget;
    import cloud.wconnect.rateshop.service.rateshop.IRateShopEmailConfigService;
    import cloud.wconnect.rateshop.service.rateshop.IRateShopEmailTargetService;
    import com.alibaba.fastjson.JSONObject;
    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.Service;
    import org.thymeleaf.TemplateEngine;
    import org.thymeleaf.context.Context;

    import java.text.MessageFormat;
    import java.util.List;
    import java.util.Objects;

    /**
     * Service：封装邮件通知业务
     *
     * @author: yyt
     * 2026-03-10
     */
    @Service
    public class RateShopPackageNotifyDTOUtil {

        @Autowired
        private IRateShopEmailConfigService emailConfigService;
        @Autowired
        private IRateShopEmailTargetService emailTargetService;
        @Autowired
        private LineMessageUtil lineMessageUtil;
        @Autowired
        private I18nMessageUtils i18nMessageUtils;
        @Autowired
        private BeanMapper beanMapper;
        @Autowired
        private TemplateEngine templateEngine;

        @Value("${here.link.rate-shop}")
        private String hereLink;

        /**
         * 获取日期区间字符串
         *
         * @param propertyDTO 资产
         * @param startDate   开始日期
         * @param endDate     结束日期
         * @return 日期区间字符串
         * @author yyt
         * @date 2024/1/11
         */
        public String getDateRangeStr(PropertyDTO propertyDTO, Long startDate, Long endDate) {
            String timezone = propertyDTO.getTimezone();
            String language = propertyDTO.getLanguage();
            String longDate = propertyDTO.getInternationalization().getDateTime().getLongDate();

            String startDateStr = InternationalizationUtils.formatDateTime(startDate, longDate, StringUtils.EMPTY,
                    timezone, language);
            String endDateStr = InternationalizationUtils.formatDateTime(endDate, longDate, StringUtils.EMPTY,
                    timezone, language);

            return startDateStr + " ~ " + endDateStr;
        }

        /**
         * 获取邮件通知DTO
         *
         * @param propertyDTO 资产
         * @param missDate    缺失日期
         * @param reportName  报告名称
         * @param category    类别
         * @return 邮件通知DTO
         * @author yyt
         * @date 2024/1/11
         */
        public NotifyDTO getNotifyDTO(PropertyDTO propertyDTO, String missDate, String reportName, Integer category) {
            //查询邮件配置
            RateShopEmailConfig emailConfig = emailConfigService.getByPropertyIdAndType(propertyDTO.getId(), category);
            if (Objects.isNull(emailConfig)) return null;
            if (!emailConfig.getStatus()) return null;

            List<RateShopEmailTarget> emailTargets = emailTargetService.listByConfigIds(Lists.newArrayList(emailConfig.getId()));
            if (CollectionUtils.isEmpty(emailTargets)) return null;

            //获取国际化配置
            String language = propertyDTO.getLanguage();
            String functionName = i18nMessageUtils.message(FunctionCons.RATE_SHOP_FUNCTION, language);
            //通知主题
            String defaultSubjectI18nCode = RateShopNotifyConfigTypeEnum.getDefaultSubjectI18nCode(category);
            String defaultSubject = i18nMessageUtils.message(defaultSubjectI18nCode, language, functionName, propertyDTO.getName());
            //自定义主体优先，其次默认主题
            String subject = StringUtils.isNotBlank(emailConfig.getSubject()) ? emailConfig.getSubject() : defaultSubject;

            //构建NotifyDTO
            NotifyDTO notifyDTO = new NotifyDTO();
            notifyDTO.setPropertyId(propertyDTO.getId());
            notifyDTO.setFunctionPoint(FunctionPointEnum.RATE_SHOP_PMS_REPORT_MISSED.getFunctionPoint());
            notifyDTO.setSubject(subject);
            notifyDTO.setIdentityList(beanMapper.mapList(emailTargets, NotifyIdentityDTO.class));

            //跳转链接
            String linkUrl = MessageFormat.format(hereLink + "?property={0}&tenant={1}", propertyDTO.getAbbr(), propertyDTO.getTenantSymbol());
            Context context = new Context();
            context.setVariable("propertyName", propertyDTO.getName());
            context.setVariable("hereLink", linkUrl);
            context.setVariable("functionName", functionName);
            context.setVariable("reportName", reportName);
            context.setVariable("reportDate", missDate);
            //获取邮件body
            String mailBody = templateEngine.process(EmailUtil.getMissReportTemplateName(language), context);
            //设置邮件通知内容
            notifyDTO.setEmailContent(EmailUtil.getFullEmailContent(defaultSubject, mailBody, language));

            //line通知内容
            ReportMissedMsgParm reportMissedMsgParm = ReportMissedMsgParm.builder()
                    .propertyId(propertyDTO.getId())
                    .propertyName(propertyDTO.getName())
                    .functionName(functionName)
                    .reportName(reportName)
                    .missDate(missDate)
                    .build();
            JSONObject lineMsg = lineMessageUtil.generateReportMissedMsg(reportMissedMsgParm);
            notifyDTO.setLineContent(Lists.newArrayList(lineMsg));
            return notifyDTO;
        }

    }