增加构建通知

This commit is contained in:
dengqichen 2025-11-13 11:15:50 +08:00
parent 8b6b4652ce
commit 5205c6aaa1
6 changed files with 57 additions and 42 deletions

View File

@ -26,8 +26,6 @@ import java.util.Properties;
@Component
public class EmailChannelAdapter implements INotificationChannelAdapter<EmailNotificationConfig, EmailSendNotificationRequest> {
private final ObjectMapper objectMapper = new ObjectMapper();
@Override
public void send(EmailNotificationConfig emailConfig, EmailSendNotificationRequest request) throws Exception {
// 1. 校验配置

View File

@ -31,6 +31,11 @@ public interface INotificationTemplateRepository extends IBaseRepository<Notific
*/
Optional<NotificationTemplate> findByCodeAndEnabled(String code, Boolean enabled);
@Query("SELECT t FROM NotificationTemplate t WHERE t.id = :id AND t.enabled = :enabled AND t.deleted = false")
Optional<NotificationTemplate> findByIdAndEnabled(@Param("id") Long id, @Param("enabled") Boolean enabled);
/**
* 根据渠道类型查找模板
*/

View File

@ -26,6 +26,8 @@ public interface INotificationTemplateService extends IBaseService<NotificationT
String renderByCode(String templateCode, Map<String, Object> params);
String renderById(Long id, Map<String, Object> params);
/**
* 根据编码获取模板

View File

@ -143,8 +143,7 @@ public class NotificationChannelServiceImpl extends BaseServiceImpl<Notification
// 6. 发送通知
try {
log.info("发送通知 - 渠道ID: {}, 渠道类型: {}, 内容: {}",
channel.getId(), channel.getChannelType(), request.getContent());
log.info("发送通知 - 渠道ID: {}, 渠道类型: {}, 内容: {}", channel.getId(), channel.getChannelType(), request.getContent());
adapter.send(config, request);

View File

@ -71,7 +71,7 @@ public class NotificationServiceImpl implements INotificationService {
}
// 5. 渲染模板内容
String content = notificationTemplateService.renderByCode(template.getCode(), request.getParams());
String content = notificationTemplateService.renderTemplate(template.getContentTemplate(), request.getParams());
// 6. 根据渠道类型发送通知
switch (template.getChannelType()) {

View File

@ -69,6 +69,12 @@ public class NotificationTemplateServiceImpl extends BaseServiceImpl<Notificatio
return processTemplate(template.getContentTemplate(), params);
}
@Override
public String renderById(Long id, Map<String, Object> 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<Notificatio
.orElseThrow(() -> 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模板处理