增加构建通知

This commit is contained in:
dengqichen 2025-11-13 19:10:53 +08:00
parent 7a8c7f7762
commit 8f8b90abb2
2 changed files with 50 additions and 26 deletions

View File

@ -1,12 +1,12 @@
package com.qqchen.deploy.backend.workflow.delegate; package com.qqchen.deploy.backend.workflow.delegate;
import com.qqchen.deploy.backend.notification.dto.EmailSendNotificationRequest; 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.dto.WeworkSendNotificationRequest;
import com.qqchen.deploy.backend.notification.entity.NotificationChannel; 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.enums.WeworkMessageTypeEnum;
import com.qqchen.deploy.backend.notification.repository.INotificationChannelRepository; 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.inputmapping.NotificationInputMapping;
import com.qqchen.deploy.backend.workflow.dto.outputs.NotificationOutputs; import com.qqchen.deploy.backend.workflow.dto.outputs.NotificationOutputs;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
@ -29,58 +29,79 @@ import java.util.Map;
public class NotificationNodeDelegate extends BaseNodeDelegate<NotificationInputMapping, NotificationOutputs> { public class NotificationNodeDelegate extends BaseNodeDelegate<NotificationInputMapping, NotificationOutputs> {
@Resource @Resource
private INotificationSendService notificationSendService; private INotificationService notificationService;
@Resource @Resource
private INotificationChannelRepository notificationChannelRepository; private INotificationChannelRepository notificationChannelRepository;
@Override @Override
protected void executeInternal(DelegateExecution execution, Map<String, Object> configs, NotificationInputMapping input) { protected void executeInternal(DelegateExecution execution, Map<String, Object> configs, NotificationInputMapping input) {
if (input.getChannelId() == null || StringUtils.isEmpty(input.getTitle()) || StringUtils.isEmpty(input.getContent())) { // 1. 参数校验
logError(String.format("Notification delegate parameter verification failed %s %s %s", input.getChannelId(), input.getTitle(), input.getContent())); if (input.getChannelId() == null || input.getNotificationTemplateId() == null) {
logWarn(String.format("Notification delegate parameter verification failed - channelId: %s, templateId: %s", input.getChannelId(), input.getNotificationTemplateId()));
return; return;
} }
try { try {
// 1. 查询渠道信息 // 2. 查询渠道信息仅用于确定渠道类型
NotificationChannel channel = notificationChannelRepository.findById(input.getChannelId()) NotificationChannel channel = notificationChannelRepository.findById(input.getChannelId())
.orElseThrow(() -> new RuntimeException("通知渠道不存在: " + 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()) { switch (channel.getChannelType()) {
case WEWORK -> { case WEWORK -> {
WeworkSendNotificationRequest weworkRequest = new WeworkSendNotificationRequest(); WeworkSendNotificationRequest weworkRequest = new WeworkSendNotificationRequest();
weworkRequest.setChannelId(input.getChannelId()); weworkRequest.setChannelId(input.getChannelId());
weworkRequest.setContent(buildWeworkContent(input.getTitle(), input.getContent())); // 其他配置消息类型等由NotificationService根据模板配置自动设置
weworkRequest.setMessageType(WeworkMessageTypeEnum.TEXT); request.setSendRequest(weworkRequest);
notificationSendService.send(weworkRequest);
} }
case EMAIL -> { case EMAIL -> {
EmailSendNotificationRequest emailRequest = new EmailSendNotificationRequest(); EmailSendNotificationRequest emailRequest = new EmailSendNotificationRequest();
emailRequest.setChannelId(input.getChannelId()); emailRequest.setChannelId(input.getChannelId());
emailRequest.setSubject(input.getTitle()); // 收件人从工作流变量获取
emailRequest.setContent(input.getContent()); emailRequest.setToReceivers(getEmailReceivers(execution, configs));
// 这里需要设置收件人但工作流中没有提供需要从其他地方获取 // 其他配置HTML格式等由NotificationService根据模板配置自动设置
// 暂时使用一个默认值实际应该从工作流变量或配置中获取 request.setSendRequest(emailRequest);
emailRequest.setToReceivers(Arrays.asList("admin@company.com"));
notificationSendService.send(emailRequest);
} }
default -> throw new RuntimeException("不支持的渠道类型: " + channel.getChannelType()); 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) { } catch (Exception e) {
logError("工作流通知发送失败: " + e.getMessage()); logError("工作流通知发送失败: " + e.getMessage());
throw new RuntimeException("通知发送失败", e); throw new RuntimeException("通知发送失败", e);
} }
} }
/** /**
* 构建企业微信消息内容标题+内容 * 获取邮件收件人列表
* 从工作流变量或配置中获取如果没有则使用默认值
*/ */
private String buildWeworkContent(String title, String content) { private java.util.List<String> getEmailReceivers(DelegateExecution execution, Map<String, Object> configs) {
if (StringUtils.isNotEmpty(title)) { // 1. 优先从工作流变量中获取
return title + "\n\n" + content; Object receiversVar = execution.getVariable("emailReceivers");
if (receiversVar instanceof java.util.List) {
return (java.util.List<String>) 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<String>) receiversConfig;
}
}
// 3. 使用默认收件人
return Arrays.asList("admin@company.com");
} }
} }

View File

@ -4,6 +4,8 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data; import lombok.Data;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
import java.util.Map;
/** /**
* 通知节点输入映射 * 通知节点输入映射
* *
@ -24,5 +26,6 @@ public class NotificationInputMapping extends BaseNodeInputMapping {
* 通知模板ID * 通知模板ID
*/ */
private Long notificationTemplateId; private Long notificationTemplateId;
} }