# PackageNotifyDTOUtil.java

    package cloud.wconnect.rateshop.businesstask.revenue;

    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.cons.CommonFieldI18nConstant;
    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.PublicFlexMsgParm;
    import cloud.wconnect.commonlib.pojo.dto.line.ReportMissedMsgParm;
    import cloud.wconnect.commonlib.pojo.dto.scheduler.JobParameterDTO;
    import cloud.wconnect.commonlib.util.*;
    import cloud.wconnect.rateshop.cons.FunctionPointEnum;
    import cloud.wconnect.rateshop.cons.revenue.DailyRevenueNotifyTypeEnum;
    import cloud.wconnect.rateshop.entity.revenue.RevenueEmailConfig;
    import cloud.wconnect.rateshop.entity.revenue.RevenueEmailTarget;
    import cloud.wconnect.rateshop.entity.revenue.RevenueSchedulerJob;
    import cloud.wconnect.rateshop.pojo.vo.revenue.OccJumpAlertVO;
    import cloud.wconnect.rateshop.service.revenue.IRevenueEmailConfigService;
    import cloud.wconnect.rateshop.service.revenue.IRevenueEmailTargetService;
    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.LinkedHashMap;
    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 ObjectMapper objectMapper;
        @Autowired
        private IRevenueEmailConfigService revenueEmailConfigService;
        @Autowired
        private IRevenueEmailTargetService revenueEmailTargetService;

        @Value("${default.timezone}")
        private String defaultTimezone;
        @Autowired
        private TemplateEngine templateEngine;
        @Value("${here.link.revenue}")
        private String link;

        /**
         * 获取通知配置DTO
         *
         * @param propertyDTO 资产集合
         * @param missDate    报告缺失时间
         * @param reportName  报告名称
         * @param category    分类
         * @return 通知配置
         * @author lichen
         * @date 2023/12/14
         */
        public NotifyDTO getNotifyDTO(PropertyDTO propertyDTO, String missDate, String reportName, Integer category) {
            // 查询邮件配置
            Integer propertyId = propertyDTO.getId();
            RevenueEmailConfig emailConfig = revenueEmailConfigService.getEmailConfig(propertyId, category, true);
            if (Objects.isNull(emailConfig)) {
                return null;
            }
            List<RevenueEmailTarget> targets = revenueEmailTargetService.listByConfigId(emailConfig.getId());
            if (CollectionUtils.isEmpty(targets)) {
                return null;
            }

            String propertyName = propertyDTO.getName();
            String language = propertyDTO.getLanguage();
            String defaultSubject = revenueEmailConfigService.getDefaultSubject(propertyId, category);

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

            // 功能名称
            String functionName = i18nMessageUtils.message(FunctionCons.REVENUE, language);

            // 设置邮件通知内容
            Context context = new Context();
            context.setVariable("propertyName", propertyName);
            context.setVariable("hereLink", link + "?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;
        }

        /**
         * 获取 Occ Jump Alert 通知DTO
         *
         * @param propertyDTO        资产
         * @param notifyType         通知类型
         * @param occJumpAlertVOList Occ Jump Alert 数据
         * @param lastMoment         比对目标时刻
         * @param nowMoment          当前目标时刻
         * @param thresholdMin       过滤阈值，最小值
         * @param thresholdMax       过滤阈值，最大值
         * @return NotifyDTO
         * @author zc
         * @date 2024/4/18
         */
        public NotifyDTO getNotifyDTOForOccJumpAlert(PropertyDTO propertyDTO, int notifyType, List<OccJumpAlertVO> occJumpAlertVOList,
                                                     String lastMoment, String nowMoment, String thresholdMin, String thresholdMax) {
            // 查询邮件配置
            RevenueEmailConfig emailConfig = revenueEmailConfigService.getEmailConfig(propertyDTO.getId(), notifyType, true);
            if (Objects.isNull(emailConfig)) {
                return null;
            }
            List<NotifyIdentityDTO> identityDTOList = revenueEmailTargetService.listByConfigId(emailConfig.getId()).stream()
                    .map(item -> beanMapper.map(item, NotifyIdentityDTO.class)).toList();
            if (CollectionUtils.isEmpty(identityDTOList)) {
                return null;
            }

            String hereLink = link + "?tenant=" + propertyDTO.getTenantSymbol() + "&property=" + propertyDTO.getAbbr()
                    + "&date=" + DateTimeUtils.dateNow(propertyDTO.getTimezone()).toEpochSecond();
            String functionName = i18nMessageUtils.message(FunctionCons.REVENUE, propertyDTO.getLanguage());
            String defaultSubject = i18nMessageUtils.message(DailyRevenueNotifyTypeEnum.getDefaultSubjectI18nCode(notifyType),
                    propertyDTO.getLanguage(), functionName, nowMoment, lastMoment, propertyDTO.getName());
            String subject = StringUtils.isNotBlank(emailConfig.getSubject()) ? emailConfig.getSubject() : defaultSubject;

            // 设置邮件通知内容
            Context context = new Context();
            context.setVariable("propertyName", propertyDTO.getName());
            context.setVariable("functionName", functionName);
            context.setVariable("hereLink", hereLink);
            context.setVariable("thresholdMin", thresholdMin);
            context.setVariable("thresholdMax", thresholdMax);
            context.setVariable("lastMoment", lastMoment);
            context.setVariable("nowMoment", nowMoment);
            context.setVariable("occAlerts", occJumpAlertVOList);
            String processContent = templateEngine.process("dailyrevenue/daily-revenue-occ-jump-alert_" + propertyDTO.getLanguage(), context);
            String emailContent = EmailUtil.getFullEmailContent(defaultSubject, processContent, propertyDTO.getLanguage());

            // LINE 通知内容
            LinkedHashMap<String, String> businessMap = new LinkedHashMap<>();
            businessMap.put(i18nMessageUtils.message(CommonFieldI18nConstant.TOTAL, propertyDTO.getLanguage()), String.valueOf(occJumpAlertVOList.size()));
            JSONObject lineMsg = lineMessageUtil.generatePublicFlexMsg(PublicFlexMsgParm.builder()
                    .propertyId(propertyDTO.getId())
                    .propertyName(propertyDTO.getName())
                    .functionName(functionName)
                    .title("Occ jump alert - Now: " + nowMoment + " VS Last: " + lastMoment)
                    .link(hereLink)
                    .businessMap(businessMap)
                    .build());

            return NotifyDTO.builder()
                    .propertyId(propertyDTO.getId())
                    .subject(subject)
                    .emailContent(emailContent)
                    .lineContent(Lists.newArrayList(lineMsg))
                    .identityList(identityDTOList)
                    .functionPoint(FunctionPointEnum.REVENUE_OCC_JUMP_ALERT.getFunctionPoint())
                    .build();
        }

        /**
         * 获取日期区间字符串
         *
         * @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;
        }

        /**
         * 获取资产配置的阈值
         *
         * @param schedulerJob 定时任务
         * @return 阈值
         * @author yyt
         * @date 2024/3/16
         **/
        public ThresholdDTO getThresholdDTO(RevenueSchedulerJob schedulerJob) {
            JobParameterDTO jobParameterDTO = JsonUtils.convertStringToObject(objectMapper, schedulerJob.getParameters(), JobParameterDTO.class);
            return new ThresholdDTO(jobParameterDTO.getThreshold(), TimeUnit.MINUTES, defaultTimezone);
        }

    }