# GmwrSendNotifyServiceImpl.java

    package cloud.wconnect.workperformance.service.gmwr.impl;

    import cloud.wconnect.commonlib.cons.BaseConstant;
    import cloud.wconnect.commonlib.cons.EmailConfigCons;
    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.UserDTO;
    import cloud.wconnect.commonlib.pojo.dto.internationalization.InternationalizationDateTimeDTO;
    import cloud.wconnect.commonlib.pojo.dto.line.PublicFlexMsgParm;
    import cloud.wconnect.commonlib.service.NotifyInternalService;
    import cloud.wconnect.commonlib.service.RoleInternalService;
    import cloud.wconnect.commonlib.util.*;
    import cloud.wconnect.workperformance.cons.FunctionPointEnum;
    import cloud.wconnect.workperformance.cons.gmwr.GmwrI18nConstant;
    import cloud.wconnect.workperformance.cons.gmwr.GmwrNotifyTypeEnum;
    import cloud.wconnect.workperformance.cons.gmwr.GmwrStatusEnum;
    import cloud.wconnect.workperformance.entity.gmwr.GmwrEmailConfig;
    import cloud.wconnect.workperformance.entity.gmwr.GmwrWeeks;
    import cloud.wconnect.workperformance.service.gmwr.IGmwrEmailConfigService;
    import cloud.wconnect.workperformance.service.gmwr.IGmwrSendNotifyService;
    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.time.LocalDate;
    import java.util.List;
    import java.util.Objects;

    /**
     * Service: Send Notify
     *
     * @author zc
     * @date 2024/10/31
     */
    @Service
    @DS("work_performance")
    public class GmwrSendNotifyServiceImpl implements IGmwrSendNotifyService {
        @Resource
        private IGmwrEmailConfigService emailConfigService;
        @Resource
        private RoleInternalService roleInternalService;
        @Resource
        private NotifyInternalService notifyInternalService;
        @Resource
        private I18nMessageUtils i18nMessageUtils;
        @Resource
        private LineMessageUtil lineMessageUtil;
        @Resource
        private TemplateEngine templateEngine;

        @Value("${here.link.gm-weekly-report}")
        private String link;

        /**
         * 发送通知 For Draft
         *
         * @param propertyDTO 资产
         * @param gmwrWeeks   周报数据
         * @author zc
         * @date 2025/2/5
         */
        @Override
        public void sendNotificationForDraft(PropertyDTO propertyDTO, GmwrWeeks gmwrWeeks) {
            if (GmwrStatusEnum.DRAFT.getStatus() != gmwrWeeks.getStatus()) {
                return;
            }
            // 获取邮件配置
            GmwrEmailConfig emailConfig = emailConfigService.getEmailConfig(propertyDTO.getId(), GmwrNotifyTypeEnum.DRAFT_DATA_PROCESS.getType());
            if (Objects.isNull(emailConfig)) {
                return;
            }
            // 获取通知人
            List<UserDTO> userDTOList = roleInternalService.getUsers(propertyDTO.getTenantId(), propertyDTO.getId(),
                    BaseConstant.GM_WEEKLY_REPORT_ADMIN_ROLE, BaseConstant.GM_WEEKLY_REPORT_GM_ROLE);
            sendNotification(propertyDTO, gmwrWeeks, userDTOList, emailConfig);
        }

        /**
         * 发送通知 For Complete
         *
         * @param propertyDTO 资产
         * @param gmwrWeeks   周报数据
         * @author zc
         * @date 2024/10/31
         */
        @Override
        public void sendNotificationForComplete(PropertyDTO propertyDTO, GmwrWeeks gmwrWeeks) {
            if (GmwrStatusEnum.COMPLETE.getStatus() != gmwrWeeks.getStatus()) {
                return;
            }
            // 获取通知人
            List<UserDTO> userDTOList = roleInternalService.getUsers(propertyDTO.getTenantId(), propertyDTO.getId(),
                    BaseConstant.GM_WEEKLY_REPORT_REVIEW_ROLE);
            sendNotification(propertyDTO, gmwrWeeks, userDTOList, new GmwrEmailConfig());
        }

        /**
         * 发送通知
         *
         * @param propertyDTO 资产
         * @param gmwrWeeks   周报数据
         * @param userDTOList 通知人集合
         * @param emailConfig 邮件配置
         * @author zc
         * @date 2025/2/5
         */
        private void sendNotification(PropertyDTO propertyDTO, GmwrWeeks gmwrWeeks, List<UserDTO> userDTOList, GmwrEmailConfig emailConfig) {
            if (CollectionUtils.isEmpty(userDTOList)) {
                return;
            }
            List<NotifyIdentityDTO> notifyIdentityDTOList = userDTOList.stream().map(item ->
                    NotifyIdentityDTO.builder()
                            .source(EmailConfigCons.SOURCE_USER)
                            .identityId(item.getId())
                            .targetType(EmailConfigCons.TARGET_TYPE_TO)
                            .build()).toList();

            String hereLink = link + "?tenant=" + propertyDTO.getTenantSymbol() + "&property=" + propertyDTO.getAbbr() + "&id=" + gmwrWeeks.getId();
            String functionName = i18nMessageUtils.message(FunctionCons.GM_WEEKLY_REPORT, propertyDTO.getLanguage());
            String statusStr = i18nMessageUtils.message(GmwrStatusEnum.getI18nCode(gmwrWeeks.getStatus()), propertyDTO.getLanguage());

            InternationalizationDateTimeDTO dateTimeDTO = propertyDTO.getInternationalization().getDateTime();
            LocalDate weekStartLocalDate = DateTimeUtils.strToLocalDate(gmwrWeeks.getWeek().toString(), BaseConstant.DATE_FORMAT_YYYYMMDD);
            String dateRangeStr = "(" + InternationalizationUtils.formatLocalDate(weekStartLocalDate,
                    dateTimeDTO.getLongDate(), propertyDTO.getLanguage()) + " ~ " +
                    InternationalizationUtils.formatLocalDate(weekStartLocalDate.plusDays(6),
                            dateTimeDTO.getLongDate(), propertyDTO.getLanguage()) + ")";

            String yearMonthStr = InternationalizationUtils.formatLocalDate(weekStartLocalDate, dateTimeDTO.getShortDate(), propertyDTO.getLanguage());
            String weekIndexStr = i18nMessageUtils.message(GmwrI18nConstant.WEEK_INDEX, propertyDTO.getLanguage(), DateTimeUtils.getWeekIndexOfMonth(weekStartLocalDate));
            String monthStr = InternationalizationUtils.formatLocalDate(weekStartLocalDate, BaseConstant.DATE_FORMAT_MMMM, propertyDTO.getLanguage());
            String defaultSubject = i18nMessageUtils.message(GmwrNotifyTypeEnum.DRAFT_DATA_PROCESS.getDefaultSubjectI18nCode(),
                    propertyDTO.getLanguage(), functionName, statusStr, yearMonthStr, weekIndexStr, 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("status", statusStr);
            context.setVariable("month", monthStr);
            context.setVariable("weekIndex", weekIndexStr);
            context.setVariable("dateRange", dateRangeStr);
            String processContent = templateEngine.process("gmwr/gmwr-data-handle_" + propertyDTO.getLanguage(), context);
            String emailContent = EmailUtil.getFullEmailContent(defaultSubject, processContent, propertyDTO.getLanguage());

            // LINE 通知内容
            JSONObject lineContent = lineMessageUtil.generatePublicFlexMsg(PublicFlexMsgParm.builder()
                    .propertyId(propertyDTO.getId())
                    .propertyName(propertyDTO.getName())
                    .functionName(functionName)
                    .title(statusStr + " - " + yearMonthStr + " " + weekIndexStr)
                    .link(hereLink)
                    .businessMap(null)
                    .build());

            NotifyDTO notifyDTO = NotifyDTO.builder()
                    .propertyId(propertyDTO.getId())
                    .subject(subject)
                    .emailContent(emailContent)
                    .identityList(notifyIdentityDTOList)
                    .lineContent(Lists.newArrayList(lineContent))
                    .functionPoint(FunctionPointEnum.GM_WEEKLY_REPORT_NOTIFY_OPERATOR.getFunctionPoint())
                    .build();

            // 发送通知
            notifyInternalService.sendNotify(notifyDTO);
        }
    }