Pārlūkot izejas kodu

feat(messages): 增加消息发送人字段并优化消息发送逻辑

- 在 MessagesInfo 模型中添加 sendName 字段,用于存储消息发送人名称
- 修改 MessagesController 中的 add 方法,获取并使用发送人信息
- 更新 MessagesMapper.xml,支持插入 sendName 字段
- 重构 MessagesServiceImpl 中的 insertByBo 方法,简化消息发送逻辑
- 优化 NotificationUtils 中的 sendToAll 方法,增加日志记录并处理异常
fugui001 5 mēneši atpakaļ
vecāks
revīzija
ac68347baf

+ 1 - 0
ruoyi-modules/ruoyi-system/src/main/java/org/dromara/business/controller/MessagesController.java

@@ -76,6 +76,7 @@ public class MessagesController extends BaseController {
     @RepeatSubmit()
     @PostMapping()
     public R<Void> add(@Validated(AddGroup.class) @RequestBody MessagesBo bo) {
+        String content = bo.getContent();
         return toAjax(messagesService.insertByBo(bo));
     }
 

+ 4 - 0
ruoyi-modules/ruoyi-system/src/main/java/org/dromara/business/domain/MessagesInfo.java

@@ -64,5 +64,9 @@ public class MessagesInfo {
      */
     private String status;
 
+    /**
+     * 发送人
+     */
+    private String sendName;
 
 }

+ 1 - 3
ruoyi-modules/ruoyi-system/src/main/java/org/dromara/business/service/impl/MessagesServiceImpl.java

@@ -115,9 +115,7 @@ public class MessagesServiceImpl implements IMessagesService {
     public Boolean insertByBo(MessagesBo bo) {
         MessagesInfo add = MapstructUtils.convert(bo, MessagesInfo.class);
         validEntityBeforeSave(add);
-        Long userId = LoginHelper.getUserId();
-        add.setSenderId(userId);
-        boolean flag=notificationUtils.sendToAll(bo.getTitle(),add.getContent(),add.getSenderId(), MessageTypeEnum.SYSTEM.getCode());
+        boolean flag=notificationUtils.sendToAll(bo.getTitle(),add.getContent(),LoginHelper.getUserId(), LoginHelper.getUsername(),MessageTypeEnum.SYSTEM.getCode());
         if (flag) {
             bo.setId(add.getId());
         }

+ 25 - 9
ruoyi-modules/ruoyi-system/src/main/java/org/dromara/business/utils/NotificationUtils.java

@@ -10,16 +10,18 @@ import org.dromara.business.domain.vo.UserVo;
 import org.dromara.business.mapper.MessageReceiversMapper;
 import org.dromara.business.mapper.MessagesMapper;
 import org.dromara.business.mapper.UserMapper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
-import org.springframework.transaction.annotation.Transactional;
-
 import java.util.List;
 import java.util.stream.Collectors;
 
 @Component
 public class NotificationUtils {
 
+    private static final Logger log = LoggerFactory.getLogger(NotificationUtils.class);
+
     @Autowired
     MessagesMapper messagesInfoMapper;
 
@@ -32,8 +34,10 @@ public class NotificationUtils {
     /**
      * 发送给所有用户
      */
-    public Boolean sendToAll(String title, String content, Long senderId, String messageType) {
+    public Boolean sendToAll(String title, String content, Long senderId, String sendName, String messageType) {
         try {
+            log.info("开始发送广播消息给所有用户,标题:{}", title);
+
             // 1. 插入主消息(receiver_type = ALL)
             MessagesInfo bo = new MessagesInfo();
             bo.setTitle(title);
@@ -42,21 +46,30 @@ public class NotificationUtils {
             bo.setReceiverType(ReceiverTypeEnum.ALL.getCode());
             bo.setMessageType(messageType);
             bo.setStatus(MessageStatusEnum.SENT.getCode());
+            bo.setSendName(sendName);
 
+            log.debug("插入主消息内容:{}", bo);
             messagesInfoMapper.insertMessage(bo);
             Long messageId = bo.getId();
 
             if (messageId == null) {
-                throw new RuntimeException("消息插入失败,无法获取消息ID");
+                log.error("消息插入失败,无法获取消息ID");
+                return false;
             }
 
+            log.info("消息插入成功,消息ID:{}", messageId);
+
             // 2. 获取所有用户列表
+            log.debug("开始查询所有用户列表...");
             List<UserVo> users = userMapper.selectUserList(new QueryWrapper<User>());
 
             if (users.isEmpty()) {
+                log.warn("当前系统中没有可用用户,消息未发送");
                 return true; // 没有用户也视为发送成功
             }
 
+            log.info("共找到 {} 个用户,准备生成接收记录", users.size());
+
             // 3. 批量插入 message_receivers 记录
             List<MessageReceivers> receiverList = users.stream()
                 .map(user -> {
@@ -66,14 +79,17 @@ public class NotificationUtils {
                     return receiver;
                 })
                 .collect(Collectors.toList());
-            messageReceiversMapper.batchInsertMessageReceiver(receiverList);
+
+            log.debug("准备批量插入 {} 条接收记录", receiverList.size());
+            int rows = messageReceiversMapper.batchInsertMessageReceiver(receiverList);
+            log.info("批量插入接收记录完成,影响行数:{}", rows);
+
+            log.info("广播消息发送完成,标题:{}", title);
             return true;
+
         } catch (Exception e) {
-            e.printStackTrace();
-            // 可以记录日志
+            log.error("发送广播消息过程中发生异常", e);
             return false;
         }
     }
-
-
 }

+ 2 - 0
ruoyi-modules/ruoyi-system/src/main/resources/mapper/business/MessagesMapper.xml

@@ -67,6 +67,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             <if test="receiverIds != null and receiverIds != ''">receiver_ids,</if>
             <if test="messageType != null and messageType != ''">message_type,</if>
             <if test="status != null and status != ''">status,</if>
+            <if test="sendName != null and sendName != ''">send_name,</if>
         </trim>
         VALUES
         <trim prefix="(" suffix=")" suffixOverrides=",">
@@ -77,6 +78,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             <if test="receiverIds != null and receiverIds != ''">#{receiverIds},</if>
             <if test="messageType != null and messageType != ''">#{messageType},</if>
             <if test="status != null and status != ''">#{status},</if>
+            <if test="sendName != null and sendName != ''">#{sendName},</if>
         </trim>
     </insert>