From 8f8b90abb21ef5225957190de0e0f2a80792f8c2 Mon Sep 17 00:00:00 2001 From: dengqichen Date: Thu, 13 Nov 2025 19:10:53 +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 --- .../delegate/NotificationNodeDelegate.java | 73 ++++++++++++------- .../NotificationInputMapping.java | 3 + 2 files changed, 50 insertions(+), 26 deletions(-) diff --git a/backend/src/main/java/com/qqchen/deploy/backend/workflow/delegate/NotificationNodeDelegate.java b/backend/src/main/java/com/qqchen/deploy/backend/workflow/delegate/NotificationNodeDelegate.java index 12f406fc..48ea3870 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/workflow/delegate/NotificationNodeDelegate.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/workflow/delegate/NotificationNodeDelegate.java @@ -1,12 +1,12 @@ package com.qqchen.deploy.backend.workflow.delegate; import com.qqchen.deploy.backend.notification.dto.EmailSendNotificationRequest; +import com.qqchen.deploy.backend.notification.dto.SendNotificationRequest; import com.qqchen.deploy.backend.notification.dto.WeworkSendNotificationRequest; import com.qqchen.deploy.backend.notification.entity.NotificationChannel; -import com.qqchen.deploy.backend.notification.enums.NotificationChannelTypeEnum; import com.qqchen.deploy.backend.notification.enums.WeworkMessageTypeEnum; import com.qqchen.deploy.backend.notification.repository.INotificationChannelRepository; -import com.qqchen.deploy.backend.notification.service.INotificationSendService; +import com.qqchen.deploy.backend.notification.service.INotificationService; import com.qqchen.deploy.backend.workflow.dto.inputmapping.NotificationInputMapping; import com.qqchen.deploy.backend.workflow.dto.outputs.NotificationOutputs; import jakarta.annotation.Resource; @@ -29,58 +29,79 @@ import java.util.Map; public class NotificationNodeDelegate extends BaseNodeDelegate { @Resource - private INotificationSendService notificationSendService; - + private INotificationService notificationService; + @Resource private INotificationChannelRepository notificationChannelRepository; @Override protected void executeInternal(DelegateExecution execution, Map configs, NotificationInputMapping input) { - if (input.getChannelId() == null || StringUtils.isEmpty(input.getTitle()) || StringUtils.isEmpty(input.getContent())) { - logError(String.format("Notification delegate parameter verification failed %s %s %s", input.getChannelId(), input.getTitle(), input.getContent())); + // 1. 参数校验 + if (input.getChannelId() == null || input.getNotificationTemplateId() == null) { + logWarn(String.format("Notification delegate parameter verification failed - channelId: %s, templateId: %s", input.getChannelId(), input.getNotificationTemplateId())); return; } try { - // 1. 查询渠道信息 + // 2. 查询渠道信息(仅用于确定渠道类型) NotificationChannel channel = notificationChannelRepository.findById(input.getChannelId()) .orElseThrow(() -> new RuntimeException("通知渠道不存在: " + input.getChannelId())); - - // 2. 根据渠道类型构建对应的请求对象 + + // 3. 构建SendNotificationRequest + SendNotificationRequest request = new SendNotificationRequest(); + request.setNotificationTemplateId(input.getNotificationTemplateId()); + request.setTemplateParams(execution.getVariables()); + + // 4. 根据渠道类型创建基础的sendRequest(具体配置由NotificationService处理) switch (channel.getChannelType()) { case WEWORK -> { WeworkSendNotificationRequest weworkRequest = new WeworkSendNotificationRequest(); weworkRequest.setChannelId(input.getChannelId()); - weworkRequest.setContent(buildWeworkContent(input.getTitle(), input.getContent())); - weworkRequest.setMessageType(WeworkMessageTypeEnum.TEXT); - notificationSendService.send(weworkRequest); + // 其他配置(消息类型等)由NotificationService根据模板配置自动设置 + request.setSendRequest(weworkRequest); } case EMAIL -> { EmailSendNotificationRequest emailRequest = new EmailSendNotificationRequest(); emailRequest.setChannelId(input.getChannelId()); - emailRequest.setSubject(input.getTitle()); - emailRequest.setContent(input.getContent()); - // 这里需要设置收件人,但工作流中没有提供,需要从其他地方获取 - // 暂时使用一个默认值,实际应该从工作流变量或配置中获取 - emailRequest.setToReceivers(Arrays.asList("admin@company.com")); - notificationSendService.send(emailRequest); + // 收件人从工作流变量获取 + emailRequest.setToReceivers(getEmailReceivers(execution, configs)); + // 其他配置(HTML格式等)由NotificationService根据模板配置自动设置 + request.setSendRequest(emailRequest); } default -> throw new RuntimeException("不支持的渠道类型: " + channel.getChannelType()); } - - log.info("工作流通知发送成功 - 渠道ID: {}, 类型: {}", input.getChannelId(), channel.getChannelType()); + + // 5. 发送通知(NotificationService会处理模板渲染和详细配置) + notificationService.send(request); + + log.info("工作流通知发送成功 - 渠道ID: {}, 模板ID: {}", + input.getChannelId(), input.getNotificationTemplateId()); } catch (Exception e) { logError("工作流通知发送失败: " + e.getMessage()); throw new RuntimeException("通知发送失败", e); } } - + + /** - * 构建企业微信消息内容(标题+内容) + * 获取邮件收件人列表 + * 从工作流变量或配置中获取,如果没有则使用默认值 */ - private String buildWeworkContent(String title, String content) { - if (StringUtils.isNotEmpty(title)) { - return title + "\n\n" + content; + private java.util.List getEmailReceivers(DelegateExecution execution, Map configs) { + // 1. 优先从工作流变量中获取 + Object receiversVar = execution.getVariable("emailReceivers"); + if (receiversVar instanceof java.util.List) { + return (java.util.List) receiversVar; } - return content; + + // 2. 从配置中获取 + if (configs != null && configs.containsKey("emailReceivers")) { + Object receiversConfig = configs.get("emailReceivers"); + if (receiversConfig instanceof java.util.List) { + return (java.util.List) receiversConfig; + } + } + + // 3. 使用默认收件人 + return Arrays.asList("admin@company.com"); } } diff --git a/backend/src/main/java/com/qqchen/deploy/backend/workflow/dto/inputmapping/NotificationInputMapping.java b/backend/src/main/java/com/qqchen/deploy/backend/workflow/dto/inputmapping/NotificationInputMapping.java index 06d9ddcb..1d9708c8 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/workflow/dto/inputmapping/NotificationInputMapping.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/workflow/dto/inputmapping/NotificationInputMapping.java @@ -4,6 +4,8 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import lombok.Data; import jakarta.validation.constraints.NotNull; +import java.util.Map; + /** * 通知节点输入映射 * @@ -24,5 +26,6 @@ public class NotificationInputMapping extends BaseNodeInputMapping { * 通知模板ID */ private Long notificationTemplateId; + }