From 5205c6aaa107e018578ad10bb40fe07b1a5614b8 Mon Sep 17 00:00:00 2001 From: dengqichen Date: Thu, 13 Nov 2025 11:15:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=9E=84=E5=BB=BA=E9=80=9A?= =?UTF-8?q?=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../adapter/impl/EmailChannelAdapter.java | 58 +++++++++---------- .../INotificationTemplateRepository.java | 23 +++++--- .../service/INotificationTemplateService.java | 2 + .../impl/NotificationChannelServiceImpl.java | 3 +- .../service/impl/NotificationServiceImpl.java | 2 +- .../impl/NotificationTemplateServiceImpl.java | 11 ++++ 6 files changed, 57 insertions(+), 42 deletions(-) diff --git a/backend/src/main/java/com/qqchen/deploy/backend/notification/adapter/impl/EmailChannelAdapter.java b/backend/src/main/java/com/qqchen/deploy/backend/notification/adapter/impl/EmailChannelAdapter.java index 41a60ac4..d2584886 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/notification/adapter/impl/EmailChannelAdapter.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/notification/adapter/impl/EmailChannelAdapter.java @@ -25,63 +25,61 @@ import java.util.Properties; @Slf4j @Component public class EmailChannelAdapter implements INotificationChannelAdapter { - - private final ObjectMapper objectMapper = new ObjectMapper(); - + @Override public void send(EmailNotificationConfig emailConfig, EmailSendNotificationRequest request) throws Exception { // 1. 校验配置 validateEmailConfig(emailConfig); - + // 2. 创建JavaMailSender JavaMailSenderImpl mailSender = createMailSender(emailConfig); - + // 3. 校验收件人 if (CollectionUtils.isEmpty(request.getToReceivers())) { throw new IllegalArgumentException("收件人列表不能为空"); } - + // 4. 构建邮件 MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8"); - + // 设置发件人 if (emailConfig.getFromName() != null && !emailConfig.getFromName().isEmpty()) { helper.setFrom(new InternetAddress(emailConfig.getFrom(), emailConfig.getFromName(), "UTF-8")); } else { helper.setFrom(emailConfig.getFrom()); } - + // 设置收件人 helper.setTo(request.getToReceivers().toArray(new String[0])); - + // 设置抄送 if (!CollectionUtils.isEmpty(request.getCcReceivers())) { helper.setCc(request.getCcReceivers().toArray(new String[0])); } - + // 设置密送 if (!CollectionUtils.isEmpty(request.getBccReceivers())) { helper.setBcc(request.getBccReceivers().toArray(new String[0])); } - + // 设置主题 helper.setSubject(request.getSubject()); - + // 设置内容(支持HTML) helper.setText(request.getContent(), request.getIsHtml() != null ? request.getIsHtml() : false); - + // 5. 发送邮件 log.info("发送邮件通知 - 收件人: {}, 主题: {}", request.getToReceivers(), request.getSubject()); mailSender.send(mimeMessage); log.info("邮件通知发送成功"); } - + @Override public NotificationChannelTypeEnum supportedType() { return NotificationChannelTypeEnum.EMAIL; } - + @Override public String validateConfig(EmailNotificationConfig emailConfig) { try { @@ -91,18 +89,18 @@ public class EmailChannelAdapter implements INotificationChannelAdapter { - + /** * 根据编码查找模板 */ Optional findByCode(String code); - + /** * 根据编码和启用状态查找模板 */ Optional findByCodeAndEnabled(String code, Boolean enabled); - + + + @Query("SELECT t FROM NotificationTemplate t WHERE t.id = :id AND t.enabled = :enabled AND t.deleted = false") + Optional findByIdAndEnabled(@Param("id") Long id, @Param("enabled") Boolean enabled); + + /** * 根据渠道类型查找模板 */ List findByChannelTypeAndEnabled(NotificationChannelTypeEnum channelType, Boolean enabled); - + /** * 检查编码是否存在(排除指定ID) */ @Query("SELECT COUNT(t) > 0 FROM NotificationTemplate t WHERE t.code = :code AND t.id != :id") boolean existsByCodeAndIdNot(@Param("code") String code, @Param("id") Long id); - + /** * 分页查询模板 */ @Query("SELECT t FROM NotificationTemplate t WHERE " + - "(:name IS NULL OR t.name LIKE %:name%) AND " + - "(:code IS NULL OR t.code LIKE %:code%) AND " + - "(:channelType IS NULL OR t.channelType = :channelType) AND " + - "(:enabled IS NULL OR t.enabled = :enabled)") + "(:name IS NULL OR t.name LIKE %:name%) AND " + + "(:code IS NULL OR t.code LIKE %:code%) AND " + + "(:channelType IS NULL OR t.channelType = :channelType) AND " + + "(:enabled IS NULL OR t.enabled = :enabled)") Page findByConditions( @Param("name") String name, @Param("code") String code, diff --git a/backend/src/main/java/com/qqchen/deploy/backend/notification/service/INotificationTemplateService.java b/backend/src/main/java/com/qqchen/deploy/backend/notification/service/INotificationTemplateService.java index a535f699..4e303603 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/notification/service/INotificationTemplateService.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/notification/service/INotificationTemplateService.java @@ -26,6 +26,8 @@ public interface INotificationTemplateService extends IBaseService params); + String renderById(Long id, Map params); + /** * 根据编码获取模板 diff --git a/backend/src/main/java/com/qqchen/deploy/backend/notification/service/impl/NotificationChannelServiceImpl.java b/backend/src/main/java/com/qqchen/deploy/backend/notification/service/impl/NotificationChannelServiceImpl.java index f90ee514..75b444fa 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/notification/service/impl/NotificationChannelServiceImpl.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/notification/service/impl/NotificationChannelServiceImpl.java @@ -143,8 +143,7 @@ public class NotificationChannelServiceImpl extends BaseServiceImpl params) { + NotificationTemplate template = getTemplateById(id); + return processTemplate(template.getContentTemplate(), params); + } + @Override public NotificationTemplateDTO getByCode(String code) { @@ -92,6 +98,11 @@ public class NotificationTemplateServiceImpl extends BaseServiceImpl new BusinessException(ResponseCode.NOTIFICATION_TEMPLATE_NOT_FOUND)); } + private NotificationTemplate getTemplateById(Long id) { + return notificationTemplateRepository.findByIdAndEnabled(id, true) + .orElseThrow(() -> new BusinessException(ResponseCode.NOTIFICATION_TEMPLATE_NOT_FOUND)); + } + /** * FreeMarker模板处理