# WeeklyMonthlySendNotifyServiceImpl.java

    package cloud.wconnect.rateshop.service.weeklymonthlyrevenue.impl;

    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.redis.PropertyBusinessDateRedisService;
    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.weeklymonthlyrevenue.MonthlyRevenueNotifyTypeEnum;
    import cloud.wconnect.rateshop.cons.weeklymonthlyrevenue.MonthlyRevenueReportDateRangeEnum;
    import cloud.wconnect.rateshop.entity.weeklymonthlyrevenue.WeeklyMonthlyRevenueEmailConfig;
    import cloud.wconnect.rateshop.service.weeklymonthlyrevenue.IWeeklyMonthlyRevenueEmailConfigService;
    import cloud.wconnect.rateshop.service.weeklymonthlyrevenue.IWeeklyMonthlyRevenueEmailTargetService;
    import cloud.wconnect.rateshop.service.weeklymonthlyrevenue.IWeeklyMonthlyRevenueReportService;
    import cloud.wconnect.rateshop.service.weeklymonthlyrevenue.IWeeklyMonthlySendNotifyService;
    import com.alibaba.fastjson.JSONObject;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.google.common.collect.Lists;
    import org.apache.commons.collections.CollectionUtils;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Service;
    import org.thymeleaf.TemplateEngine;
    import org.thymeleaf.context.Context;

    import jakarta.annotation.Resource;

    import java.util.List;
    import java.util.Objects;

    /**
     * <p>
     * Weekly Monthly Revenue Send Notify 服务实现类
     * </p>
     *
     * @author zc
     * @since 2024-05-16
     */
    @Service
    @DS("rate_shop")
    public class WeeklyMonthlySendNotifyServiceImpl implements IWeeklyMonthlySendNotifyService {

        @Resource
        private IWeeklyMonthlyRevenueEmailConfigService emailConfigService;
        @Resource
        private IWeeklyMonthlyRevenueEmailTargetService emailTargetService;
        @Resource
        private LineMessageUtil lineMessageUtil;
        @Resource
        private I18nMessageUtils i18nMessageUtils;
        @Resource
        private TemplateEngine templateEngine;
        @Resource
        private BeanMapper beanMapper;
        @Resource
        private IWeeklyMonthlyRevenueReportService revenueReportService;
        @Resource
        private PropertyBusinessDateRedisService propertyBusinessDateRedisService;
        @Value("${here.link.monthly-revenue}")
        private String link;

        /**
         * 获取通知配置DTO
         *
         * @param propertyDTO 资产
         * @param reportName  报告名称
         * @param notifyType  通知类型
         * @return NotifyDTO
         * @author zc
         * @date 2024/11/8
         */
        @Override
        public NotifyDTO getNotifyDTO(PropertyDTO propertyDTO, String reportName, int notifyType) {
            // 获取邮件配置
            WeeklyMonthlyRevenueEmailConfig emailConfig = emailConfigService.getEmailConfig(propertyDTO.getId(), notifyType);
            if (Objects.isNull(emailConfig)) {
                return null;
            }
            if (!emailConfig.getStatus()) {
                return null;
            }
            List<NotifyIdentityDTO> identityDTOList = emailTargetService.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();
            String functionName = i18nMessageUtils.message(FunctionCons.WEEKLY_MONTHLY_REVENUE, propertyDTO.getLanguage());
            String defaultSubject = i18nMessageUtils.message(MonthlyRevenueNotifyTypeEnum.getDefaultSubjectI18nCode(notifyType),
                    propertyDTO.getLanguage(), functionName, propertyDTO.getName());
            String subject = StringUtils.isNotBlank(emailConfig.getSubject()) ? emailConfig.getSubject() : defaultSubject;

            String missDate = getMissDate(propertyDTO, notifyType);

            // 邮件通知内容
            Context context = new Context();
            context.setVariable("propertyName", propertyDTO.getName());
            context.setVariable("functionName", functionName);
            context.setVariable("reportName", reportName);
            context.setVariable("reportDate", missDate);
            context.setVariable("hereLink", hereLink);
            String processContent = templateEngine.process(EmailUtil.getMissReportTemplateName(propertyDTO.getLanguage()), context);
            String emailContent = EmailUtil.getFullEmailContent(defaultSubject, processContent, propertyDTO.getLanguage());

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

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

        /**
         * 获取缺失日期
         *
         * @param propertyDTO 资产DTO
         * @param notifyType  通知类型
         * @return java.lang.String
         * @author lichen
         * @date 2025/12/16
         */
        private String getMissDate(PropertyDTO propertyDTO, int notifyType) {
            String language = propertyDTO.getLanguage();
            String pattern = propertyDTO.getInternationalization().getDateTime().getLongDate();

            // 获取资产业务日期
            long businessDate = propertyBusinessDateRedisService.getEffectiveBusinessDateOrDefault(propertyDTO.getId(), propertyDTO.getTimezone());

            List<Long> dateRange = revenueReportService.getReportDateRangeByCategory(businessDate, propertyDTO.getTimezone(),
                    MonthlyRevenueReportDateRangeEnum.getDateRange(notifyType));

            String startDateStr = InternationalizationUtils.formatYearMonthDate(dateRange.get(0), pattern, language);
            String endDateStr = InternationalizationUtils.formatYearMonthDate(dateRange.get(1), pattern, language);
            return startDateStr + " ~ " + endDateStr;
        }
    }