feat: 🎸 评论点赞通知

This commit is contained in:
GoCo 2025-09-27 10:50:51 +08:00
parent 237a4f10a8
commit 718f84b8a4
6 changed files with 448 additions and 116 deletions

View File

@ -56,6 +56,7 @@ public class MybatisInterceptor implements Interceptor {
if (sysUser != null) {
// 登录人账号
field.setAccessible(true);
// field.set(parameter, sysUser.getId());
field.set(parameter, sysUser.getUsername());
field.setAccessible(false);
}

View File

@ -1,51 +1,41 @@
package org.jeecg.modules.aiol.controller;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.query.QueryRuleEnum;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.config.shiro.IgnoreAuth;
import org.jeecg.modules.aiol.dto.CommentWithUserInfo;
import org.jeecg.modules.aiol.entity.AiolComment;
import org.jeecg.modules.aiol.service.IAiolCommentService;
import org.jeecg.modules.aiol.entity.AiolCourse;
import org.jeecg.modules.aiol.entity.AiolDiscussion;
import org.jeecg.modules.aiol.service.IAiolCourseService;
import org.jeecg.modules.aiol.service.IAiolDiscussionService;
import org.jeecg.modules.aiol.utils.MessageNotificationUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.constant.CommonConstant;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import java.util.Date;
/**
* @Description: 评论
@ -60,6 +50,15 @@ import java.util.Date;
public class AiolCommentController extends JeecgController<AiolComment, IAiolCommentService> {
@Autowired
private IAiolCommentService aiolCommentService;
@Autowired
private MessageNotificationUtil messageNotificationUtil;
@Autowired
private IAiolCourseService aiolCourseService;
@Autowired
private IAiolDiscussionService aiolDiscussionService;
/**
* 分页列表查询
@ -95,21 +94,35 @@ public class AiolCommentController extends JeecgController<AiolComment, IAiolCom
@RequiresPermissions("aiol:aiol_comment:add")
@PostMapping(value = "/add")
public Result<String> add(@RequestBody AiolComment aiolComment, HttpServletRequest request) {
// 1. 获取当前登录用户信息
String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
String username = JwtUtil.getUsername(token);
LoginUser sysUser = sysBaseApi.getUserByName(username);
try {
// 1. 获取当前登录用户信息
String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
String username = JwtUtil.getUsername(token);
LoginUser sysUser = sysBaseApi.getUserByName(username);
if (sysUser == null) {
return Result.error("用户未登录或登录已过期");
if (sysUser == null) {
return Result.error("用户未登录或登录已过期");
}
aiolComment.setUserId(sysUser.getId()); // 用户ID
aiolComment.setLikeCount(0);
// 2. 保存评论
aiolCommentService.save(aiolComment);
// 3. 发送评论通知
sendCommentNotification(aiolComment, sysUser);
log.info("用户 {} 成功添加评论评论ID: {}, 目标类型: {}, 目标ID: {}",
username, aiolComment.getId(), aiolComment.getTargetType(), aiolComment.getTargetId());
return Result.OK(aiolComment.getId());
} catch (Exception e) {
log.error("添加评论失败: targetType={}, targetId={}, error={}",
aiolComment.getTargetType(), aiolComment.getTargetId(), e.getMessage(), e);
return Result.error("添加评论失败: " + e.getMessage());
}
aiolComment.setUserId(sysUser.getId()); // 用户ID
aiolComment.setLikeCount(0);
aiolCommentService.save(aiolComment);
return Result.OK("添加成功!");
}
/**
@ -299,14 +312,47 @@ public class AiolCommentController extends JeecgController<AiolComment, IAiolCom
* @param request HTTP请求对象
* @return
*/
@AutoLog(value = "评论-新增课程评论")
@Operation(summary = "新增课程评论", description = "新增课程评论target_type默认为courseuser_id通过token自动获取")
@PostMapping(value = "/course/{courseId}/add")
public Result<String> addCourseComment(
@PathVariable("courseId") String courseId,
@RequestBody AiolComment aiolComment,
HttpServletRequest request) {
// @AutoLog(value = "评论-新增课程评论")
// @Operation(summary = "新增课程评论", description = "新增课程评论target_type默认为courseuser_id通过token自动获取")
// @PostMapping(value = "/course/{courseId}/add")
// public Result<String> addCourseComment(
// @PathVariable("courseId") String courseId,
// @RequestBody AiolComment aiolComment,
// HttpServletRequest request) {
// try {
// // 1. 获取当前登录用户信息
// String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
// String username = JwtUtil.getUsername(token);
// LoginUser sysUser = sysBaseApi.getUserByName(username);
// if (sysUser == null) {
// return Result.error("用户未登录或登录已过期");
// }
// // 2. 设置评论基本信息
// aiolComment.setTargetType("course");
// aiolComment.setTargetId(courseId); // 课程ID
// aiolComment.setUserId(sysUser.getId()); // 用户ID
// // 3. 保存评论
// aiolCommentService.save(aiolComment);
// log.info("用户 {} 成功添加课程评论课程ID: {}, 评论ID: {}",
// username, courseId, aiolComment.getId());
// return Result.OK("评论添加成功!");
// } catch (Exception e) {
// log.error("添加课程评论失败: courseId={}, error={}", courseId, e.getMessage(), e);
// return Result.error("评论添加失败: " + e.getMessage());
// }
// }
@AutoLog(value = "评论-点赞")
@Operation(summary = "点赞评论", description = "为指定评论点赞增加like_count字段值并发送通知")
@GetMapping(value = "/like/{commentId}")
public Result<String> likeComment(@PathVariable("commentId") String commentId, HttpServletRequest request) {
try {
// 1. 获取当前登录用户信息
String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
@ -317,38 +363,13 @@ public class AiolCommentController extends JeecgController<AiolComment, IAiolCom
return Result.error("用户未登录或登录已过期");
}
// 2. 设置评论基本信息
aiolComment.setTargetType("course");
aiolComment.setTargetId(courseId); // 课程ID
aiolComment.setUserId(sysUser.getId()); // 用户ID
// 3. 保存评论
aiolCommentService.save(aiolComment);
log.info("用户 {} 成功添加课程评论课程ID: {}, 评论ID: {}",
username, courseId, aiolComment.getId());
return Result.OK("评论添加成功!");
} catch (Exception e) {
log.error("添加课程评论失败: courseId={}, error={}", courseId, e.getMessage(), e);
return Result.error("评论添加失败: " + e.getMessage());
}
}
@AutoLog(value = "评论-点赞")
@Operation(summary = "点赞评论", description = "为指定评论点赞增加like_count字段值")
@GetMapping(value = "/like/{commentId}")
@IgnoreAuth
public Result<String> likeComment(@PathVariable("commentId") String commentId) {
try {
// 1. 查询评论是否存在
// 2. 查询评论是否存在
AiolComment comment = aiolCommentService.getById(commentId);
if (comment == null) {
return Result.error("评论不存在");
}
// 2. 更新点赞数
// 3. 更新点赞数
Integer currentLikeCount = comment.getLikeCount() != null ? comment.getLikeCount() : 0;
comment.setLikeCount(currentLikeCount + 1);
@ -357,7 +378,10 @@ public class AiolCommentController extends JeecgController<AiolComment, IAiolCom
return Result.error("点赞失败");
}
log.info("评论点赞成功: commentId={}, 当前点赞数={}", commentId, comment.getLikeCount());
// 4. 发送点赞通知
sendLikeNotification(comment, sysUser);
log.info("用户 {} 评论点赞成功: commentId={}, 当前点赞数={}", username, commentId, comment.getLikeCount());
return Result.OK("点赞成功!");
} catch (Exception e) {
@ -422,4 +446,119 @@ public class AiolCommentController extends JeecgController<AiolComment, IAiolCom
return Result.error("取消置顶失败: " + e.getMessage());
}
}
/**
* 发送评论通知
*
* @param aiolComment 评论对象
* @param sysUser 当前用户
*/
private void sendCommentNotification(AiolComment aiolComment, LoginUser sysUser) {
try {
String targetType = aiolComment.getTargetType();
String targetId = aiolComment.getTargetId();
// 确定接收通知的用户和实体标题
String toUserId = null;
String entityTitle = "未知实体";
if ("course".equals(targetType)) {
// 课程评论通知课程创建者
AiolCourse course = aiolCourseService.getById(targetId);
if (course != null) {
toUserId = course.getCreateBy(); // 课程创建者
entityTitle = course.getName(); // 课程名称
}
} else if ("discussion".equals(targetType)) {
// 讨论评论通知讨论创建者
AiolDiscussion discussion = aiolDiscussionService.getById(targetId);
if (discussion != null) {
toUserId = discussion.getCreateBy(); // 讨论创建者
entityTitle = discussion.getTitle(); // 讨论标题
}
} else if ("comment".equals(targetType)) {
// 回复评论通知被回复的评论作者
AiolComment parentComment = aiolCommentService.getById(targetId);
if (parentComment != null) {
toUserId = parentComment.getUserId(); // 被回复评论的作者
entityTitle = "评论回复"; // 回复类型
}
}
// 不给自己发通知
if (toUserId != null && !toUserId.equals(sysUser.getId()) && !toUserId.equals(sysUser.getUsername())) {
// 生成当前时间格式yyyy-MM-dd HH:mm:ss
String actionTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
// 调用消息通知工具类发送评论通知
messageNotificationUtil.sendCommentNotification(
toUserId, // 接收用户ID
sysUser.getId(), // 发送用户ID
sysUser.getId(), // 发送者ID
sysUser.getUsername(), // 发送者用户名
Long.valueOf(aiolComment.getId()), // 评论ID
aiolComment.getContent(), // 评论内容
targetType, // 实体类型
targetId, // 实体ID
entityTitle, // 实体标题
actionTime // 操作时间
);
log.info("评论通知发送成功: 评论者={}, 接收者={}, 目标类型={}, 实体标题={}",
sysUser.getUsername(), toUserId, targetType, entityTitle);
} else {
log.debug("跳过通知发送: 自己评论自己的内容或未找到接收者, 评论者={}, 接收者={}",
sysUser.getId(), toUserId);
}
} catch (Exception e) {
// 通知发送失败不影响评论的正常添加只记录错误日志
log.error("发送评论通知失败: commentId={}, targetType={}, targetId={}, error={}",
aiolComment.getId(), aiolComment.getTargetType(), aiolComment.getTargetId(), e.getMessage(), e);
}
}
/**
* 发送点赞通知
*
* @param comment 被点赞的评论对象
* @param sysUser 点赞用户
*/
private void sendLikeNotification(AiolComment comment, LoginUser sysUser) {
try {
// 获取评论作者ID
String toUserId = comment.getUserId();
String entityTitle = "评论";
// 不给自己发通知
if (toUserId != null && !toUserId.equals(sysUser.getId()) && !toUserId.equals(sysUser.getUsername())) {
// 生成当前时间格式yyyy-MM-dd HH:mm:ss
String actionTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
// 调用消息通知工具类发送点赞通知
messageNotificationUtil.sendLikeNotification(
toUserId, // 接收用户ID评论作者
sysUser.getId(), // 发送用户ID点赞者
sysUser.getId(), // 发送者ID
sysUser.getUsername(), // 发送者用户名
"comment", // 实体类型
comment.getId(), // 实体ID评论ID
entityTitle, // 实体标题
"like", // 操作类型
actionTime // 操作时间
);
log.info("点赞通知发送成功: 点赞者={}, 接收者={}, 评论ID={}",
sysUser.getUsername(), toUserId, comment.getId());
} else {
log.debug("跳过点赞通知发送: 自己点赞自己的评论, 点赞者={}, 接收者={}",
sysUser.getId(), toUserId);
}
} catch (Exception e) {
// 通知发送失败不影响点赞的正常操作只记录错误日志
log.error("发送点赞通知失败: commentId={}, error={}",
comment.getId(), e.getMessage(), e);
}
}
}

View File

@ -1,8 +1,5 @@
package org.jeecg.modules.aiol.controller;
import org.jeecg.common.api.dto.message.MessageDTO;
import org.jeecg.common.constant.enums.MessageTypeEnum;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@ -12,7 +9,6 @@ import org.jeecg.common.api.vo.Result;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.GetMapping;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
@ -20,9 +16,10 @@ import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.jeecg.modules.system.model.AnnouncementSendModel;
import org.jeecg.modules.system.service.ISysAnnouncementSendService;
import org.jeecg.modules.system.service.ISysAnnouncementService;
import org.jeecg.modules.system.entity.SysAnnouncementSend;
import org.jeecg.modules.system.entity.SysAnnouncement;
import org.jeecg.modules.system.service.ISysAnnouncementService;
import org.jeecg.modules.aiol.utils.MessageNotificationUtil;
import org.jeecg.common.constant.CommonConstant;
import org.apache.commons.lang3.StringUtils;
@ -36,42 +33,43 @@ import lombok.extern.slf4j.Slf4j;
@RequestMapping("/aiol/message")
@Slf4j
public class AiolMessageController {
@Autowired
private ISysBaseAPI sysBaseApi;
@Autowired
private ISysAnnouncementSendService sysAnnouncementSendService;
@Autowired
private ISysAnnouncementService sysAnnouncementService;
@Autowired
private MessageNotificationUtil messageNotificationUtil;
@GetMapping("/test")
public void test() {
// //推送消息
// MessageDTO messageDTO = new MessageDTO();
// messageDTO.setToAll(false);
// messageDTO.setToUser("202121200873");
// messageDTO.setTitle("评论和@");
// messageDTO.setCategory("3");
// //推送消息类型
// messageDTO.setType(MessageTypeEnum.XT.getType());
// messageDTO.setIsMarkdown(false);
// messageDTO.setFromUser("202121200874");
// messageDTO.setContent("{\"sender\":{\"id\":\"1966804797404344321\",\"username\":\"小明\"},\"comment\":{\"id\":456,\"content\":\"老师讲得真棒![user:789:李四] 快来看这个课程!\"},\"entity\":{\"type\":\"course\",\"id\":\"1954463468539371522\",\"title\":\"python语言基础与应用\"},\"actionTime\":\"2025-09-16T15:30:00Z\"}");
// sysBaseApi.sendTemplateMessage(messageDTO);
// 测试评论通知
messageNotificationUtil.sendCommentNotification(
"202121200873", // 接收用户ID
"202121200874", // 发送用户ID
"1966804797404344321", // 发送者ID
"小明", // 发送者用户名
456L, // 评论ID
"老师讲得真棒![user:789:李四] 快来看这个课程!", // 评论内容
"course", // 实体类型
"1954463468539371522", // 实体ID
"python语言基础与应用", // 实体标题
"2025-09-16T15:30:00Z" // 操作时间
);
//推送消息
MessageDTO messageDTO = new MessageDTO();
messageDTO.setToAll(false);
messageDTO.setToUser("202121200873");
messageDTO.setTitle("赞和收藏");
messageDTO.setCategory("4");
//推送消息类型
messageDTO.setType(MessageTypeEnum.XT.getType());
messageDTO.setIsMarkdown(false);
messageDTO.setFromUser("202121200874");
messageDTO.setContent("{\"sender\":{\"id\":\"202121200874\",\"username\":\"202121200874\"},\"entity\":{\"type\":\"course\",\"id\":\"1954463468539371522\",\"title\":\"python语言基础与应用\"},\"action\":\"like\",\"actionTime\":\"2025-09-16T15:35:00Z\"}");
sysBaseApi.sendTemplateMessage(messageDTO);
// 测试点赞通知
messageNotificationUtil.sendLikeNotification(
"202121200873", // 接收用户ID
"202121200874", // 发送用户ID
"202121200874", // 发送者ID
"202121200874", // 发送者用户名
"course", // 实体类型
"1954463468539371522", // 实体ID
"python语言基础与应用", // 实体标题
"like", // 操作类型
"2025-09-16T15:35:00Z" // 操作时间
);
}
@GetMapping("/comments_at")
@ -165,15 +163,16 @@ public class AiolMessageController {
try {
LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
String userId = sysUser.getId();
Map<String, Object> result = new HashMap<>();
// 1. 先查询用户所有未读消息的annt_id
QueryWrapper<SysAnnouncementSend> unreadWrapper = new QueryWrapper<>();
unreadWrapper.eq("user_id", userId)
.eq("read_flag", 0);
.or().eq("user_id", sysUser.getUsername())
.eq("read_flag", 0);
List<SysAnnouncementSend> unreadMessages = sysAnnouncementSendService.list(unreadWrapper);
if (unreadMessages.isEmpty()) {
// 如果没有未读消息返回0
result.put("commentsUnreadCount", 0);
@ -182,22 +181,22 @@ public class AiolMessageController {
result.put("totalUnreadCount", 0);
return Result.OK(result);
}
// 2. 提取所有annt_id
List<String> anntIds = unreadMessages.stream()
.map(SysAnnouncementSend::getAnntId)
.collect(java.util.stream.Collectors.toList());
// 3. 根据annt_id查询对应的消息详情获取msg_category
QueryWrapper<SysAnnouncement> announcementWrapper = new QueryWrapper<>();
announcementWrapper.in("id", anntIds);
List<SysAnnouncement> announcements = sysAnnouncementService.list(announcementWrapper);
// 4. 按msg_category分类统计
long commentsUnreadCount = 0; // msg_category=3
long likesUnreadCount = 0; // msg_category=4
long systemUnreadCount = 0; // msg_category=2
long commentsUnreadCount = 0; // msg_category=3
long likesUnreadCount = 0; // msg_category=4
long systemUnreadCount = 0; // msg_category=2
for (SysAnnouncement announcement : announcements) {
String msgCategory = announcement.getMsgCategory();
if ("3".equals(msgCategory)) {
@ -208,15 +207,15 @@ public class AiolMessageController {
systemUnreadCount++;
}
}
// 5. 计算总未读数量
long totalUnreadCount = commentsUnreadCount + likesUnreadCount + systemUnreadCount;
result.put("commentsUnreadCount", commentsUnreadCount);
result.put("likesUnreadCount", likesUnreadCount);
result.put("systemUnreadCount", systemUnreadCount);
result.put("totalUnreadCount", totalUnreadCount);
return Result.OK(result);
} catch (Exception e) {
return Result.error("查询未读消息数量失败: " + e.getMessage());
@ -236,23 +235,23 @@ public class AiolMessageController {
result.error500("消息ID不能为空");
return result;
}
// 构建更新条件根据sendId和userId更新单条消息
LambdaUpdateWrapper<SysAnnouncementSend> updateWrapper = new UpdateWrapper<SysAnnouncementSend>().lambda();
updateWrapper.set(SysAnnouncementSend::getReadFlag, CommonConstant.HAS_READ_FLAG);
updateWrapper.set(SysAnnouncementSend::getReadTime, new Date());
updateWrapper.eq(SysAnnouncementSend::getId, sendId);
SysAnnouncementSend announcementSend = new SysAnnouncementSend();
boolean updateResult = sysAnnouncementSendService.update(announcementSend, updateWrapper);
if (updateResult) {
result.setSuccess(true);
result.setMessage("消息已标记为已读");
} else {
result.error500("标记已读失败,可能消息不存在或已被标记");
}
return result;
} catch (Exception e) {
log.error("标记单条消息已读失败: " + e.getMessage(), e);

View File

@ -0,0 +1,191 @@
package org.jeecg.modules.aiol.utils;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.modules.message.websocket.WebSocket;
import org.jeecg.modules.system.entity.SysAnnouncement;
import org.jeecg.modules.system.entity.SysAnnouncementSend;
import org.jeecg.modules.system.mapper.SysAnnouncementMapper;
import org.jeecg.modules.system.mapper.SysAnnouncementSendMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* 消息通知工具类
* 用于发送评论通知和点赞通知避免循环依赖问题
*
* @Author: jeecg-boot
* @Date: 2025-01-17
* @Version: V1.0
*/
@Component
@Slf4j
public class MessageNotificationUtil {
@Autowired
private SysAnnouncementMapper sysAnnouncementMapper;
@Autowired
private SysAnnouncementSendMapper sysAnnouncementSendMapper;
@Autowired
private WebSocket webSocket;
/**
* 发送评论通知
*
* @param toUser 接收用户ID
* @param fromUser 发送用户ID
* @param senderId 发送者ID
* @param senderUsername 发送者用户名
* @param commentId 评论ID
* @param commentContent 评论内容
* @param entityType 实体类型
* @param entityId 实体ID
* @param entityTitle 实体标题
* @param actionTime 操作时间
*/
public void sendCommentNotification(String toUser, String fromUser, String senderId, String senderUsername,
Long commentId, String commentContent, String entityType, String entityId,
String entityTitle, String actionTime) {
try {
log.info("发送评论通知 - 接收者: {}, 发送者: {}, 评论ID: {}", toUser, fromUser, commentId);
// 创建评论和@消息的JSON内容
JSONObject contentJson = new JSONObject();
// 发送者信息
JSONObject sender = new JSONObject();
sender.put("id", senderId);
sender.put("username", senderUsername);
contentJson.put("sender", sender);
// 评论信息
JSONObject comment = new JSONObject();
comment.put("id", commentId);
comment.put("content", commentContent);
contentJson.put("comment", comment);
// 实体信息
JSONObject entity = new JSONObject();
entity.put("type", entityType);
entity.put("id", entityId);
entity.put("title", entityTitle);
contentJson.put("entity", entity);
// 操作时间
contentJson.put("actionTime", actionTime);
// 1. 创建系统公告
SysAnnouncement announcement = new SysAnnouncement();
announcement.setTitile("评论和@");
announcement.setMsgContent(contentJson.toJSONString());
announcement.setSender(senderUsername);
announcement.setPriority(CommonConstant.PRIORITY_L);
announcement.setMsgType(CommonConstant.MSG_TYPE_UESR);
announcement.setMsgCategory("3");
announcement.setSendStatus(CommonConstant.HAS_SEND);
announcement.setSendTime(new Date());
announcement.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
announcement.setUserIds(toUser + ",");
// 保存公告
sysAnnouncementMapper.insert(announcement);
// 2. 创建用户通告阅读标记记录
SysAnnouncementSend announcementSend = new SysAnnouncementSend();
announcementSend.setAnntId(announcement.getId());
announcementSend.setUserId(toUser);
announcementSend.setReadFlag(CommonConstant.NO_READ_FLAG);
announcementSend.setReadTime(new Date());
sysAnnouncementSendMapper.insert(announcementSend);
log.info("评论通知发送成功 - 接收者: {}, 内容: {}", toUser, contentJson.toJSONString());
} catch (Exception e) {
log.error("发送评论通知失败 - 接收者: {}, 错误: {}", toUser, e.getMessage(), e);
}
}
/**
* 发送点赞通知
*
* @param toUser 接收用户ID
* @param fromUser 发送用户ID
* @param senderId 发送者ID
* @param senderUsername 发送者用户名
* @param entityType 实体类型
* @param entityId 实体ID
* @param entityTitle 实体标题
* @param action 操作类型like/collect等
* @param actionTime 操作时间
*/
public void sendLikeNotification(String toUser, String fromUser, String senderId, String senderUsername,
String entityType, String entityId, String entityTitle, String action, String actionTime) {
try {
log.info("发送点赞通知 - 接收者: {}, 发送者: {}, 操作: {}", toUser, fromUser, action);
// 创建赞和收藏消息的JSON内容
JSONObject contentJson = new JSONObject();
// 发送者信息
JSONObject sender = new JSONObject();
sender.put("id", senderId);
sender.put("username", senderUsername);
contentJson.put("sender", sender);
// 实体信息
JSONObject entity = new JSONObject();
entity.put("type", entityType);
entity.put("id", entityId);
entity.put("title", entityTitle);
contentJson.put("entity", entity);
// 操作类型和时间
contentJson.put("action", action);
contentJson.put("actionTime", actionTime);
// 1. 创建系统公告
SysAnnouncement announcement = new SysAnnouncement();
announcement.setTitile("赞和收藏");
announcement.setMsgContent(contentJson.toJSONString());
announcement.setSender(senderUsername);
announcement.setPriority(CommonConstant.PRIORITY_L);
announcement.setMsgType(CommonConstant.MSG_TYPE_UESR);
announcement.setMsgCategory("4");
announcement.setSendStatus(CommonConstant.HAS_SEND);
announcement.setSendTime(new Date());
announcement.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
announcement.setUserIds(toUser + ",");
// 保存公告
sysAnnouncementMapper.insert(announcement);
// 2. 创建用户通告阅读标记记录
SysAnnouncementSend announcementSend = new SysAnnouncementSend();
announcementSend.setAnntId(announcement.getId());
announcementSend.setUserId(toUser);
announcementSend.setReadFlag(CommonConstant.NO_READ_FLAG);
announcementSend.setReadTime(new Date());
sysAnnouncementSendMapper.insert(announcementSend);
// 3. 发送WebSocket通知
try {
JSONObject obj = new JSONObject();
obj.put("cmd", "user");
obj.put("userId", toUser);
obj.put("msgId", announcement.getId());
obj.put("msgTxt", announcement.getTitile());
webSocket.pushMessage(toUser, obj.toJSONString());
} catch (Exception we) {
log.warn("WebSocket通知发送失败: {}", we.getMessage());
}
log.info("点赞通知发送成功 - 接收者: {}, 内容: {}", toUser, contentJson.toJSONString());
} catch (Exception e) {
log.error("发送点赞通知失败 - 接收者: {}, 错误: {}", toUser, e.getMessage(), e);
}
}
}

View File

@ -98,4 +98,5 @@ public interface ISysAnnouncementService extends IService<SysAnnouncement> {
* @param count
*/
void updateVisitsNum(String id, int count);
}

View File

@ -251,4 +251,5 @@ public class SysAnnouncementServiceImpl extends ServiceImpl<SysAnnouncementMappe
}
}
}