功能:新增模板消息占位符替换和最终内容保存
- Add replaceTemplatePlaceholders method in MessageService to replace ${key} placeholders
- Update SmsQueueListener to detect 模板消息s and perform 占位符替换
- Add updateFinalContent method in PushLogMapper to save final content
功能:将消息 DTO 模板编码字段改为可选
- Print final content to 日志s for both template and non-模板消息s
- Save final content to database for all valid messages
This commit is contained in:
@@ -3,6 +3,8 @@ package com.xiaowang.messagehub.listener;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.rabbitmq.client.Channel;
|
||||
import com.xiaowang.messagehub.dto.MessageDTO;
|
||||
import com.xiaowang.messagehub.entity.PushTemplateDO;
|
||||
import com.xiaowang.messagehub.mapper.PushLogMapper;
|
||||
import com.xiaowang.messagehub.service.MessageService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -10,6 +12,7 @@ import org.springframework.amqp.core.Message;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@@ -30,6 +33,9 @@ public class SmsQueueListener {
|
||||
@Autowired
|
||||
private MessageService messageService;
|
||||
|
||||
@Autowired
|
||||
private PushLogMapper pushLogMapper;
|
||||
|
||||
@RabbitListener(queues = "smsQueue")
|
||||
public void handleSmsMessage(Message message, Channel channel) {
|
||||
String messageId = message.getMessageProperties().getMessageId();
|
||||
@@ -48,14 +54,51 @@ public class SmsQueueListener {
|
||||
|
||||
boolean isValid = messageService.validateAndSaveLog(messageDTO, messageId, sendTime);
|
||||
|
||||
long deliveryTag = message.getMessageProperties().getDeliveryTag();
|
||||
channel.basicAck(deliveryTag, false);
|
||||
|
||||
if (isValid) {
|
||||
logger.info("消息处理成功,消息ID: {}", messageId);
|
||||
String finalContent = null;
|
||||
String templateCode = messageDTO.getTemplateCode();
|
||||
|
||||
if (StringUtils.hasText(templateCode)) {
|
||||
PushTemplateDO template = messageService.getTemplateByCode(templateCode);
|
||||
|
||||
if (template != null && StringUtils.hasText(template.getContentPattern())) {
|
||||
logger.info("检测到模板消息,模板编码: {}", templateCode);
|
||||
logger.info("模板内容模式: {}", template.getContentPattern());
|
||||
|
||||
String contentPattern = template.getContentPattern();
|
||||
finalContent = messageService.replaceTemplatePlaceholders(contentPattern, messageDTO.getTemplateParams());
|
||||
|
||||
logger.info("模板参数: {}", messageDTO.getTemplateParams());
|
||||
logger.info("替换后最终内容: {}", finalContent);
|
||||
} else {
|
||||
logger.info("非模板消息或模板不存在/未启用,不进行占位符替换");
|
||||
logger.info("模板编码: {}, 模板存在: {}", templateCode, template != null);
|
||||
}
|
||||
} else {
|
||||
logger.info("未指定模板编码,非模板消息,不进行占位符替换");
|
||||
}
|
||||
|
||||
if (finalContent == null) {
|
||||
if (messageDTO.getTemplateParams() != null && messageDTO.getTemplateParams().containsKey("message")) {
|
||||
finalContent = messageDTO.getTemplateParams().get("message").toString();
|
||||
} else {
|
||||
finalContent = "非模板消息,无最终内容";
|
||||
}
|
||||
}
|
||||
|
||||
pushLogMapper.updateFinalContent(messageId, finalContent);
|
||||
|
||||
logger.info("========================================");
|
||||
logger.info("消息处理完成");
|
||||
logger.info("消息ID: {}", messageId);
|
||||
logger.info("最终内容: {}", finalContent);
|
||||
logger.info("========================================");
|
||||
} else {
|
||||
logger.warn("消息校验失败,消息ID: {}", messageId);
|
||||
}
|
||||
|
||||
long deliveryTag = message.getMessageProperties().getDeliveryTag();
|
||||
channel.basicAck(deliveryTag, false);
|
||||
} catch (Exception e) {
|
||||
logger.error("处理短信消息时发生异常,消息ID: {}", messageId, e);
|
||||
|
||||
|
||||
@@ -42,6 +42,9 @@ public interface PushLogMapper extends BaseMapper<PushLogDO> {
|
||||
@Update("UPDATE push_log SET status = #{status}, retry_count = retry_count + 1, fail_reason = #{failReason} WHERE id = #{id}")
|
||||
int updateRetryStatus(@Param("id") String id, @Param("status") Integer status, @Param("failReason") String failReason);
|
||||
|
||||
@Update("UPDATE push_log SET final_content = #{finalContent} WHERE id = #{id}")
|
||||
int updateFinalContent(@Param("id") String id, @Param("finalContent") String finalContent);
|
||||
|
||||
@Update("UPDATE push_log SET send_time = #{sendTime} WHERE id = #{id}")
|
||||
int updateSendTime(@Param("id") String id, @Param("sendTime") LocalDateTime sendTime);
|
||||
|
||||
|
||||
@@ -10,10 +10,12 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validator;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@@ -85,4 +87,23 @@ public class MessageService {
|
||||
pushLog.setFailReason(failReason);
|
||||
pushLogMapper.updateById(pushLog);
|
||||
}
|
||||
|
||||
public String replaceTemplatePlaceholders(String contentPattern, Map<String, Object> templateParams) {
|
||||
if (!StringUtils.hasText(contentPattern)) {
|
||||
return contentPattern;
|
||||
}
|
||||
|
||||
if (templateParams == null || templateParams.isEmpty()) {
|
||||
return contentPattern;
|
||||
}
|
||||
|
||||
String result = contentPattern;
|
||||
for (Map.Entry<String, Object> entry : templateParams.entrySet()) {
|
||||
String placeholder = "${" + entry.getKey() + "}";
|
||||
String value = entry.getValue() != null ? entry.getValue().toString() : "";
|
||||
result = result.replace(placeholder, value);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user