|
|
@@ -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;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
}
|