From fdee4dc2b4b00d3644d3e329cb468764c13cba34 Mon Sep 17 00:00:00 2001
From: Hanserwei <2628273921@qq.com>
Date: Sat, 8 Nov 2025 11:07:50 +0800
Subject: [PATCH] =?UTF-8?q?feat(comment):=20=E5=AE=9E=E7=8E=B0=E8=AF=84?=
=?UTF-8?q?=E8=AE=BA=E5=88=86=E9=A1=B5=E6=9F=A5=E8=AF=A2=E5=8A=9F=E8=83=BD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 新增评论分页查询接口与实现逻辑
- 支持查询一级评论及其最早回复的二级评论
- 支持从KV服务批量获取评论内容
- 支持从用户服务批量获取用户信息并组装- 新增评论热度字段用于排序
- 修改MyBatis代码生成配置至comment模块
- 调整评论表结构,优化字段定义
- 完善相关DTO、VO及Mapper文件
- 添加HTTP客户端测试用例
---
.idea/MyBatisCodeHelperDatasource.xml | 18 +-
han-note-comment/han-note-comment-biz/pom.xml | 5 +
.../biz/consumer/Comment2DBConsumer.java | 5 +-
.../biz/controller/CommentController.java | 9 +
.../biz/domain/dataobject/CommentDO.java | 21 +-
.../biz/domain/dataobject/NoteCountDO.java | 50 +++++
.../biz/domain/mapper/CommentDOMapper.java | 21 +-
.../biz/domain/mapper/NoteCountDOMapper.java | 17 ++
.../biz/model/vo/FindCommentItemRspVO.java | 64 ++++++
.../model/vo/FindCommentPageListReqVO.java | 20 ++
.../comment/biz/rpc/KeyValueRpcService.java | 27 +++
.../comment/biz/rpc/UserRpcService.java | 45 ++++
.../comment/biz/service/CommentService.java | 11 +
.../biz/service/impl/CommentServiceImpl.java | 192 ++++++++++++++++++
.../resources/mapperxml/CommentDOMapper.xml | 43 +++-
.../resources/mapperxml/NoteCountDOMapper.xml | 23 +++
.../biz/domain/mapper/NoteCountDOMapper.java | 1 -
.../hannote/kv/api/KeyValueFeignApi.java | 11 +-
http-client/gateApi.http | 9 +
19 files changed, 565 insertions(+), 27 deletions(-)
create mode 100644 han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/domain/dataobject/NoteCountDO.java
create mode 100644 han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/domain/mapper/NoteCountDOMapper.java
create mode 100644 han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/model/vo/FindCommentItemRspVO.java
create mode 100644 han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/model/vo/FindCommentPageListReqVO.java
create mode 100644 han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/rpc/UserRpcService.java
create mode 100644 han-note-comment/han-note-comment-biz/src/main/resources/mapperxml/NoteCountDOMapper.xml
diff --git a/.idea/MyBatisCodeHelperDatasource.xml b/.idea/MyBatisCodeHelperDatasource.xml
index 78337bc..bafaba1 100644
--- a/.idea/MyBatisCodeHelperDatasource.xml
+++ b/.idea/MyBatisCodeHelperDatasource.xml
@@ -9,11 +9,11 @@
-
-
-
-
-
+
+
+
+
+
@@ -94,7 +94,7 @@
-
+
diff --git a/han-note-comment/han-note-comment-biz/pom.xml b/han-note-comment/han-note-comment-biz/pom.xml
index 2673d36..9281cc6 100644
--- a/han-note-comment/han-note-comment-biz/pom.xml
+++ b/han-note-comment/han-note-comment-biz/pom.xml
@@ -125,6 +125,11 @@
spring-boot-starter-data-redis
+
+ com.hanserwei
+ han-note-user-api
+
+
diff --git a/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/consumer/Comment2DBConsumer.java b/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/consumer/Comment2DBConsumer.java
index 9f569dd..a5a407f 100644
--- a/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/consumer/Comment2DBConsumer.java
+++ b/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/consumer/Comment2DBConsumer.java
@@ -183,7 +183,10 @@ public class Comment2DBConsumer {
.toList();
if (CollUtil.isNotEmpty(commentContentNotEmptyBOS)) {
// 批量存入评论内容
- keyValueRpcService.batchSaveCommentContent(commentContentNotEmptyBOS);
+ boolean result = keyValueRpcService.batchSaveCommentContent(commentContentNotEmptyBOS);
+ if (!result) {
+ throw new RuntimeException("批量保存评论内容失败");
+ }
}
return count;
diff --git a/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/controller/CommentController.java b/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/controller/CommentController.java
index 671e17f..f5ec963 100644
--- a/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/controller/CommentController.java
+++ b/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/controller/CommentController.java
@@ -1,7 +1,10 @@
package com.hanserwei.hannote.comment.biz.controller;
import com.hanserwei.framework.biz.operationlog.aspect.ApiOperationLog;
+import com.hanserwei.framework.common.response.PageResponse;
import com.hanserwei.framework.common.response.Response;
+import com.hanserwei.hannote.comment.biz.model.vo.FindCommentItemRspVO;
+import com.hanserwei.hannote.comment.biz.model.vo.FindCommentPageListReqVO;
import com.hanserwei.hannote.comment.biz.model.vo.PublishCommentReqVO;
import com.hanserwei.hannote.comment.biz.service.CommentService;
import jakarta.annotation.Resource;
@@ -26,4 +29,10 @@ public class CommentController {
return commentService.publishComment(publishCommentReqVO);
}
+ @PostMapping("/list")
+ @ApiOperationLog(description = "评论分页查询")
+ public PageResponse findCommentPageList(@Validated @RequestBody FindCommentPageListReqVO findCommentPageListReqVO) {
+ return commentService.findCommentPageList(findCommentPageListReqVO);
+ }
+
}
\ No newline at end of file
diff --git a/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/domain/dataobject/CommentDO.java b/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/domain/dataobject/CommentDO.java
index cb451d4..6ac1db6 100644
--- a/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/domain/dataobject/CommentDO.java
+++ b/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/domain/dataobject/CommentDO.java
@@ -9,6 +9,7 @@ import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
+import java.math.BigDecimal;
import java.time.LocalDateTime;
/**
@@ -104,12 +105,6 @@ public class CommentDO {
@TableField(value = "create_time")
private LocalDateTime createTime;
- /**
- * 下级评论总数
- */
- @TableField(value = "child_comment_total")
- private Long childCommentTotal;
-
/**
* 更新时间
*/
@@ -117,7 +112,19 @@ public class CommentDO {
private LocalDateTime updateTime;
/**
- * 一级评论的第一个回复的评论ID
+ * 二级评论总数(只有一级评论才需要统计)
+ */
+ @TableField(value = "child_comment_total")
+ private Long childCommentTotal;
+
+ /**
+ * 评论热度
+ */
+ @TableField(value = "heat")
+ private BigDecimal heat;
+
+ /**
+ * 最早回复的评论ID (只有一级评论需要)
*/
@TableField(value = "first_reply_comment_id")
private Long firstReplyCommentId;
diff --git a/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/domain/dataobject/NoteCountDO.java b/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/domain/dataobject/NoteCountDO.java
new file mode 100644
index 0000000..c654d91
--- /dev/null
+++ b/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/domain/dataobject/NoteCountDO.java
@@ -0,0 +1,50 @@
+package com.hanserwei.hannote.comment.biz.domain.dataobject;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+/**
+ * 笔记计数表
+ */
+@Data
+@Builder
+@AllArgsConstructor
+@NoArgsConstructor
+@TableName(value = "t_note_count")
+public class NoteCountDO {
+ /**
+ * 主键ID
+ */
+ @TableId(value = "id", type = IdType.ASSIGN_ID)
+ private Long id;
+
+ /**
+ * 笔记ID
+ */
+ @TableField(value = "note_id")
+ private Long noteId;
+
+ /**
+ * 获得点赞总数
+ */
+ @TableField(value = "like_total")
+ private Long likeTotal;
+
+ /**
+ * 获得收藏总数
+ */
+ @TableField(value = "collect_total")
+ private Long collectTotal;
+
+ /**
+ * 被评论总数
+ */
+ @TableField(value = "comment_total")
+ private Long commentTotal;
+}
\ No newline at end of file
diff --git a/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/domain/mapper/CommentDOMapper.java b/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/domain/mapper/CommentDOMapper.java
index fc73840..d1e5327 100644
--- a/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/domain/mapper/CommentDOMapper.java
+++ b/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/domain/mapper/CommentDOMapper.java
@@ -11,7 +11,6 @@ import java.util.List;
@Mapper
public interface CommentDOMapper extends BaseMapper {
-
/**
* 根据评论 ID 批量查询
*
@@ -55,4 +54,24 @@ public interface CommentDOMapper extends BaseMapper {
*/
int updateFirstReplyCommentIdByPrimaryKey(@Param("firstReplyCommentId") Long firstReplyCommentId,
@Param("id") Long id);
+
+ /**
+ * 查询评论分页数据
+ *
+ * @param noteId 笔记 ID
+ * @param offset 偏移量
+ * @param pageSize 页大小
+ * @return 评论分页数据
+ */
+ List selectPageList(@Param("noteId") Long noteId,
+ @Param("offset") long offset,
+ @Param("pageSize") long pageSize);
+
+ /**
+ * 批量查询二级评论
+ *
+ * @param commentIds 评论 ID 列表
+ * @return 二级评论
+ */
+ List selectTwoLevelCommentByIds(@Param("commentIds") List commentIds);
}
\ No newline at end of file
diff --git a/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/domain/mapper/NoteCountDOMapper.java b/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/domain/mapper/NoteCountDOMapper.java
new file mode 100644
index 0000000..045f2e4
--- /dev/null
+++ b/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/domain/mapper/NoteCountDOMapper.java
@@ -0,0 +1,17 @@
+package com.hanserwei.hannote.comment.biz.domain.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.hanserwei.hannote.comment.biz.domain.dataobject.NoteCountDO;
+import org.apache.ibatis.annotations.Mapper;
+
+@Mapper
+public interface NoteCountDOMapper extends BaseMapper {
+
+ /**
+ * 查询笔记评论总数
+ *
+ * @param noteId 笔记ID
+ * @return 笔记评论总数
+ */
+ Long selectCommentTotalByNoteId(Long noteId);
+}
\ No newline at end of file
diff --git a/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/model/vo/FindCommentItemRspVO.java b/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/model/vo/FindCommentItemRspVO.java
new file mode 100644
index 0000000..ed542ec
--- /dev/null
+++ b/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/model/vo/FindCommentItemRspVO.java
@@ -0,0 +1,64 @@
+package com.hanserwei.hannote.comment.biz.model.vo;
+
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+@Builder
+public class FindCommentItemRspVO {
+
+ /**
+ * 评论 ID
+ */
+ private Long commentId;
+
+ /**
+ * 发布者用户 ID
+ */
+ private Long userId;
+
+ /**
+ * 头像
+ */
+ private String avatar;
+
+ /**
+ * 昵称
+ */
+ private String nickname;
+
+ /**
+ * 评论内容
+ */
+ private String content;
+
+ /**
+ * 评论内容
+ */
+ private String imageUrl;
+
+ /**
+ * 发布时间
+ */
+ private String createTime;
+
+ /**
+ * 被点赞数
+ */
+ private Long likeTotal;
+
+ /**
+ * 二级评论总数
+ */
+ private Long childCommentTotal;
+
+ /**
+ * 最早回复的评论
+ */
+ private FindCommentItemRspVO firstReplyComment;
+
+}
\ No newline at end of file
diff --git a/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/model/vo/FindCommentPageListReqVO.java b/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/model/vo/FindCommentPageListReqVO.java
new file mode 100644
index 0000000..419f867
--- /dev/null
+++ b/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/model/vo/FindCommentPageListReqVO.java
@@ -0,0 +1,20 @@
+package com.hanserwei.hannote.comment.biz.model.vo;
+
+import jakarta.validation.constraints.NotNull;
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+@Builder
+public class FindCommentPageListReqVO {
+
+ @NotNull(message = "笔记 ID 不能为空")
+ private Long noteId;
+
+ @NotNull(message = "页码不能为空")
+ private Integer pageNo = 1;
+}
\ No newline at end of file
diff --git a/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/rpc/KeyValueRpcService.java b/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/rpc/KeyValueRpcService.java
index d5aa10d..cb6cd33 100644
--- a/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/rpc/KeyValueRpcService.java
+++ b/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/rpc/KeyValueRpcService.java
@@ -1,16 +1,21 @@
package com.hanserwei.hannote.comment.biz.rpc;
+import cn.hutool.core.collection.CollUtil;
import com.google.common.collect.Lists;
import com.hanserwei.framework.common.constant.DateConstants;
import com.hanserwei.framework.common.response.Response;
import com.hanserwei.hannote.comment.biz.model.bo.CommentBO;
import com.hanserwei.hannote.kv.api.KeyValueFeignApi;
import com.hanserwei.hannote.kv.dto.req.BatchAddCommentContentReqDTO;
+import com.hanserwei.hannote.kv.dto.req.BatchFindCommentContentReqDTO;
import com.hanserwei.hannote.kv.dto.req.CommentContentReqDTO;
+import com.hanserwei.hannote.kv.dto.req.FindCommentContentReqDTO;
+import com.hanserwei.hannote.kv.dto.resp.FindCommentContentRspDTO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;
import java.util.List;
+import java.util.Objects;
@Component
public class KeyValueRpcService {
@@ -54,4 +59,26 @@ public class KeyValueRpcService {
return true;
}
+ /**
+ * 批量查询评论内容
+ *
+ * @param noteId 笔记ID
+ * @param findCommentContentReqDTOS 查询参数
+ * @return 批量查询结果
+ */
+ public List batchFindCommentContent(Long noteId, List findCommentContentReqDTOS) {
+ BatchFindCommentContentReqDTO bathFindCommentContentReqDTO = BatchFindCommentContentReqDTO.builder()
+ .noteId(noteId)
+ .commentContentKeys(findCommentContentReqDTOS)
+ .build();
+
+ Response> response = keyValueFeignApi.batchFindCommentContent(bathFindCommentContentReqDTO);
+
+ if (!response.isSuccess() || Objects.isNull(response.getData()) || CollUtil.isEmpty(response.getData())) {
+ return null;
+ }
+
+ return response.getData();
+ }
+
}
\ No newline at end of file
diff --git a/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/rpc/UserRpcService.java b/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/rpc/UserRpcService.java
new file mode 100644
index 0000000..3c13022
--- /dev/null
+++ b/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/rpc/UserRpcService.java
@@ -0,0 +1,45 @@
+package com.hanserwei.hannote.comment.biz.rpc;
+
+import cn.hutool.core.collection.CollUtil;
+import com.hanserwei.framework.common.response.Response;
+import com.hanserwei.hannote.user.api.UserFeignApi;
+import com.hanserwei.hannote.user.dto.req.FindUsersByIdsReqDTO;
+import com.hanserwei.hannote.user.dto.resp.FindUserByIdRspDTO;
+import jakarta.annotation.Resource;
+import org.springframework.stereotype.Component;
+
+import java.util.List;
+import java.util.Objects;
+import java.util.stream.Collectors;
+
+@Component
+public class UserRpcService {
+
+ @Resource
+ private UserFeignApi userFeignApi;
+
+ /**
+ * 批量查询用户信息
+ *
+ * @param userIds 用户 ID集合
+ * @return 用户信息集合
+ */
+ public List findByIds(List userIds) {
+ if (CollUtil.isEmpty(userIds)) {
+ return null;
+ }
+
+ FindUsersByIdsReqDTO findUsersByIdsReqDTO = new FindUsersByIdsReqDTO();
+ // 去重, 并设置用户 ID 集合
+ findUsersByIdsReqDTO.setIds(userIds.stream().distinct().collect(Collectors.toList()));
+
+ Response> response = userFeignApi.findByIds(findUsersByIdsReqDTO);
+
+ if (!response.isSuccess() || Objects.isNull(response.getData()) || CollUtil.isEmpty(response.getData())) {
+ return null;
+ }
+
+ return response.getData();
+ }
+
+}
\ No newline at end of file
diff --git a/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/service/CommentService.java b/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/service/CommentService.java
index bdb2a89..c808945 100644
--- a/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/service/CommentService.java
+++ b/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/service/CommentService.java
@@ -1,8 +1,11 @@
package com.hanserwei.hannote.comment.biz.service;
import com.baomidou.mybatisplus.extension.service.IService;
+import com.hanserwei.framework.common.response.PageResponse;
import com.hanserwei.framework.common.response.Response;
import com.hanserwei.hannote.comment.biz.domain.dataobject.CommentDO;
+import com.hanserwei.hannote.comment.biz.model.vo.FindCommentItemRspVO;
+import com.hanserwei.hannote.comment.biz.model.vo.FindCommentPageListReqVO;
import com.hanserwei.hannote.comment.biz.model.vo.PublishCommentReqVO;
public interface CommentService extends IService {
@@ -13,4 +16,12 @@ public interface CommentService extends IService {
* @return 响应
*/
Response> publishComment(PublishCommentReqVO publishCommentReqVO);
+
+ /**
+ * 评论列表分页查询
+ *
+ * @param findCommentPageListReqVO 评论列表分页查询参数
+ * @return 响应
+ */
+ PageResponse findCommentPageList(FindCommentPageListReqVO findCommentPageListReqVO);
}
diff --git a/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/service/impl/CommentServiceImpl.java b/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/service/impl/CommentServiceImpl.java
index c72c231..e933cb8 100644
--- a/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/service/impl/CommentServiceImpl.java
+++ b/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/service/impl/CommentServiceImpl.java
@@ -1,24 +1,41 @@
package com.hanserwei.hannote.comment.biz.service.impl;
+import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
import com.hanserwei.framework.biz.context.holder.LoginUserContextHolder;
+import com.hanserwei.framework.common.constant.DateConstants;
+import com.hanserwei.framework.common.response.PageResponse;
import com.hanserwei.framework.common.response.Response;
+import com.hanserwei.framework.common.utils.DateUtils;
import com.hanserwei.framework.common.utils.JsonUtils;
import com.hanserwei.hannote.comment.biz.constants.MQConstants;
import com.hanserwei.hannote.comment.biz.domain.dataobject.CommentDO;
import com.hanserwei.hannote.comment.biz.domain.mapper.CommentDOMapper;
+import com.hanserwei.hannote.comment.biz.domain.mapper.NoteCountDOMapper;
import com.hanserwei.hannote.comment.biz.model.dto.PublishCommentMqDTO;
+import com.hanserwei.hannote.comment.biz.model.vo.FindCommentItemRspVO;
+import com.hanserwei.hannote.comment.biz.model.vo.FindCommentPageListReqVO;
import com.hanserwei.hannote.comment.biz.model.vo.PublishCommentReqVO;
import com.hanserwei.hannote.comment.biz.retry.SendMqRetryHelper;
import com.hanserwei.hannote.comment.biz.rpc.DistributedIdGeneratorRpcService;
+import com.hanserwei.hannote.comment.biz.rpc.KeyValueRpcService;
+import com.hanserwei.hannote.comment.biz.rpc.UserRpcService;
import com.hanserwei.hannote.comment.biz.service.CommentService;
+import com.hanserwei.hannote.kv.dto.req.FindCommentContentReqDTO;
+import com.hanserwei.hannote.kv.dto.resp.FindCommentContentRspDTO;
+import com.hanserwei.hannote.user.dto.resp.FindUserByIdRspDTO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.stream.Collectors;
@Service
@Slf4j
@@ -28,6 +45,14 @@ public class CommentServiceImpl extends ServiceImpl
private SendMqRetryHelper sendMqRetryHelper;
@Resource
private DistributedIdGeneratorRpcService distributedIdGeneratorRpcService;
+ @Resource
+ private NoteCountDOMapper noteCountDOMapper;
+ @Resource
+ private CommentDOMapper commentDOMapper;
+ @Resource
+ private KeyValueRpcService keyValueRpcService;
+ @Resource
+ private UserRpcService userRpcService;
@Override
public Response> publishComment(PublishCommentReqVO publishCommentReqVO) {
@@ -61,4 +86,171 @@ public class CommentServiceImpl extends ServiceImpl
sendMqRetryHelper.asyncSend(MQConstants.TOPIC_PUBLISH_COMMENT, JsonUtils.toJsonString(publishCommentMqDTO));
return Response.success();
}
+
+ /**
+ * 设置评论内容
+ *
+ * @param commentUuidAndContentMap 评论内容
+ * @param commentDO 评论DO
+ * @param firstReplyCommentRspVO 一级评论
+ */
+ private static void setCommentContent(Map commentUuidAndContentMap, CommentDO commentDO, FindCommentItemRspVO firstReplyCommentRspVO) {
+ if (CollUtil.isNotEmpty(commentUuidAndContentMap)) {
+ String contentUuid = commentDO.getContentUuid();
+ if (StringUtils.isNotBlank(contentUuid)) {
+ firstReplyCommentRspVO.setContent(commentUuidAndContentMap.get(contentUuid));
+ }
+ }
+ }
+
+ /**
+ * 设置用户信息
+ *
+ * @param userIdAndDTOMap 用户信息
+ * @param userId 用户ID
+ * @param oneLevelCommentRspVO 一级评论
+ */
+ private static void setUserInfo(Map userIdAndDTOMap, Long userId, FindCommentItemRspVO oneLevelCommentRspVO) {
+ FindUserByIdRspDTO findUserByIdRspDTO = userIdAndDTOMap.get(userId);
+ if (Objects.nonNull(findUserByIdRspDTO)) {
+ oneLevelCommentRspVO.setAvatar(findUserByIdRspDTO.getAvatar());
+ oneLevelCommentRspVO.setNickname(findUserByIdRspDTO.getNickName());
+ }
+ }
+
+ @Override
+ public PageResponse findCommentPageList(FindCommentPageListReqVO findCommentPageListReqVO) {
+ // 笔记ID
+ Long noteId = findCommentPageListReqVO.getNoteId();
+ //当前页码
+ Integer pageNo = findCommentPageListReqVO.getPageNo();
+ // 每页展示一级评论数量
+ int pageSize = 10;
+
+ // TODO: 先从缓存中查询
+
+ // 查询评论总数
+ Long count = noteCountDOMapper.selectCommentTotalByNoteId(noteId);
+
+ if (Objects.isNull(count)) {
+ return PageResponse.success(null, pageNo, pageSize);
+ }
+
+ // 分页返回参数
+ List commentRspVOS = null;
+
+ // 若评论总数大于0
+ if (count > 0) {
+ commentRspVOS = Lists.newArrayList();
+ // 计算分页查询的offset
+ long offset = PageResponse.getOffset(pageNo, pageSize);
+ //查询一级评论
+ List oneLevelCommentIds = commentDOMapper.selectPageList(noteId, offset, pageSize);
+ // 过滤出所有最早回复的二级评论ID
+ List twoLevelCommentIds = oneLevelCommentIds.stream()
+ .map(CommentDO::getFirstReplyCommentId)
+ .filter(e -> e != 0)
+ .toList();
+ // 查询二级评论
+ Map commentIdAndDOMap = null;
+ List twoLevelCommentDOS = null;
+ if (CollUtil.isNotEmpty(twoLevelCommentIds)) {
+ twoLevelCommentDOS = commentDOMapper.selectTwoLevelCommentByIds(twoLevelCommentIds);
+ // 转Map方便后续数据拼接
+ commentIdAndDOMap = twoLevelCommentDOS.stream()
+ .collect(Collectors.toMap(CommentDO::getId, e -> e));
+ }
+
+ // 调用KV服务需要的入参
+ List findCommentContentReqDTOS = Lists.newArrayList();
+ // 调用用户服务需要的入参
+ List userIds = Lists.newArrayList();
+
+ // 一二级评论合并到一起
+ List allCommentDOS = Lists.newArrayList();
+ CollUtil.addAll(allCommentDOS, oneLevelCommentIds);
+ CollUtil.addAll(allCommentDOS, twoLevelCommentDOS);
+
+ // 循环提取RPC需要的入参数据
+ allCommentDOS.forEach(commentDO -> {
+ // 构建KV服务批量查询评论内容的入参
+ boolean isContentEmpty = commentDO.getIsContentEmpty();
+ if (!isContentEmpty) {
+ FindCommentContentReqDTO findCommentContentReqDTO = FindCommentContentReqDTO.builder()
+ .contentId(commentDO.getContentUuid())
+ .yearMonth(DateConstants.DATE_FORMAT_Y_M.format(commentDO.getCreateTime()))
+ .build();
+ findCommentContentReqDTOS.add(findCommentContentReqDTO);
+ }
+
+ // 构建用户服务批量查询用户信息的入参
+ userIds.add(commentDO.getUserId());
+ });
+
+ // RPC: 调用KV服务批量查询评论内容
+ List findCommentContentRspDTOS = keyValueRpcService.batchFindCommentContent(noteId, findCommentContentReqDTOS);
+ // DTO转Map方便后续数据拼接
+ Map commentUuidAndContentMap = null;
+ if (CollUtil.isNotEmpty(findCommentContentRspDTOS)) {
+ commentUuidAndContentMap = findCommentContentRspDTOS.stream()
+ .collect(Collectors.toMap(FindCommentContentRspDTO::getContentId, FindCommentContentRspDTO::getContent));
+ }
+
+ // RPC: 调用用户服务批量查询用户信息
+ List findUserByIdRspDTOS = userRpcService.findByIds(userIds);
+ // DTO转Map方便后续数据拼接
+ Map userIdAndDTOMap = null;
+ if (CollUtil.isNotEmpty(findUserByIdRspDTOS)) {
+ userIdAndDTOMap = findUserByIdRspDTOS.stream()
+ .collect(Collectors.toMap(FindUserByIdRspDTO::getId, e -> e));
+ }
+
+ // DO转VO组装一二级评论数据
+ for (CommentDO commentDO : oneLevelCommentIds) {
+ // 一级评论
+ Long userId = commentDO.getUserId();
+ FindCommentItemRspVO oneLevelCommentRspVO = FindCommentItemRspVO.builder()
+ .userId(userId)
+ .commentId(commentDO.getId())
+ .imageUrl(commentDO.getImageUrl())
+ .createTime(DateUtils.formatRelativeTime(commentDO.getCreateTime()))
+ .likeTotal(commentDO.getLikeTotal())
+ .childCommentTotal(commentDO.getChildCommentTotal())
+ .build();
+ // 用户信息
+ if (userIdAndDTOMap != null) {
+ setUserInfo(userIdAndDTOMap, userId, oneLevelCommentRspVO);
+ }
+ // 笔记内容
+ setCommentContent(commentUuidAndContentMap, commentDO, oneLevelCommentRspVO);
+
+ // 二级评论
+ Long firstReplyCommentId = commentDO.getFirstReplyCommentId();
+ if (CollUtil.isNotEmpty(commentIdAndDOMap)) {
+ CommentDO firstReplyCommentDO = commentIdAndDOMap.get(firstReplyCommentId);
+ if (Objects.nonNull(firstReplyCommentDO)) {
+ Long firstReplyCommentUserId = firstReplyCommentDO.getUserId();
+ FindCommentItemRspVO firstReplyCommentRspVO = FindCommentItemRspVO.builder()
+ .userId(firstReplyCommentDO.getUserId())
+ .commentId(firstReplyCommentDO.getId())
+ .imageUrl(firstReplyCommentDO.getImageUrl())
+ .createTime(DateUtils.formatRelativeTime(firstReplyCommentDO.getCreateTime()))
+ .likeTotal(firstReplyCommentDO.getLikeTotal())
+ .build();
+ if (userIdAndDTOMap != null) {
+ setUserInfo(userIdAndDTOMap, firstReplyCommentUserId, firstReplyCommentRspVO);
+ }
+
+ // 用户信息
+ oneLevelCommentRspVO.setFirstReplyComment(firstReplyCommentRspVO);
+ // 笔记内容
+ setCommentContent(commentUuidAndContentMap, firstReplyCommentDO, firstReplyCommentRspVO);
+ }
+ }
+ commentRspVOS.add(oneLevelCommentRspVO);
+ }
+ // TODO 后续逻辑
+ }
+ return PageResponse.success(commentRspVOS, pageNo, count, pageSize);
+ }
}
diff --git a/han-note-comment/han-note-comment-biz/src/main/resources/mapperxml/CommentDOMapper.xml b/han-note-comment/han-note-comment-biz/src/main/resources/mapperxml/CommentDOMapper.xml
index 9cf11d3..8834a21 100644
--- a/han-note-comment/han-note-comment-biz/src/main/resources/mapperxml/CommentDOMapper.xml
+++ b/han-note-comment/han-note-comment-biz/src/main/resources/mapperxml/CommentDOMapper.xml
@@ -20,6 +20,7 @@
+
@@ -40,10 +41,11 @@
create_time,
update_time,
child_comment_total,
+ heat,
first_reply_comment_id
-