From 2b06ca0300a06b699c5176ebcf994a5a11d912ac Mon Sep 17 00:00:00 2001 From: Hanserwei <2628273921@qq.com> Date: Sat, 8 Nov 2025 09:54:34 +0800 Subject: [PATCH] =?UTF-8?q?feat(kv):=20=E6=96=B0=E5=A2=9E=E6=89=B9?= =?UTF-8?q?=E9=87=8F=E6=9F=A5=E8=AF=A2=E8=AF=84=E8=AE=BA=E5=86=85=E5=AE=B9?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 BatchFindCommentContentReqDTO 用于批量查询请求参数校验 - 新增 FindCommentContentReqDTO 和 FindCommentContentRspDTO 用于查询参数与响应封装 - 在 CommentContentController 中添加 /comment/content/batchFind 接口 - 实现 CommentContentRepository 的批量查询方法 - 在 CommentContentServiceImpl 中完成批量查询逻辑,包括参数解析与数据转换 - 更新 gateApi.http 添加批量查询接口测试用例 --- .../req/BatchFindCommentContentReqDTO.java | 29 ++++++++++++ .../kv/dto/req/FindCommentContentReqDTO.java | 21 +++++++++ .../kv/dto/resp/FindCommentContentRspDTO.java | 24 ++++++++++ .../controller/CommentContentController.java | 7 +++ .../repository/CommentContentRepository.java | 23 ++++++++++ .../kv/biz/service/CommentContentService.java | 15 +++++++ .../impl/CommentContentServiceImpl.java | 44 +++++++++++++++++++ http-client/gateApi.http | 20 ++++++++- 8 files changed, 182 insertions(+), 1 deletion(-) create mode 100644 han-note-kv/han-note-kv-api/src/main/java/com/hanserwei/hannote/kv/dto/req/BatchFindCommentContentReqDTO.java create mode 100644 han-note-kv/han-note-kv-api/src/main/java/com/hanserwei/hannote/kv/dto/req/FindCommentContentReqDTO.java create mode 100644 han-note-kv/han-note-kv-api/src/main/java/com/hanserwei/hannote/kv/dto/resp/FindCommentContentRspDTO.java create mode 100644 han-note-kv/han-note-kv-biz/src/main/java/com/hanserwei/hannote/kv/biz/domain/repository/CommentContentRepository.java diff --git a/han-note-kv/han-note-kv-api/src/main/java/com/hanserwei/hannote/kv/dto/req/BatchFindCommentContentReqDTO.java b/han-note-kv/han-note-kv-api/src/main/java/com/hanserwei/hannote/kv/dto/req/BatchFindCommentContentReqDTO.java new file mode 100644 index 0000000..bd1a17f --- /dev/null +++ b/han-note-kv/han-note-kv-api/src/main/java/com/hanserwei/hannote/kv/dto/req/BatchFindCommentContentReqDTO.java @@ -0,0 +1,29 @@ +package com.hanserwei.hannote.kv.dto.req; + +import jakarta.validation.Valid; +import jakarta.validation.constraints.NotEmpty; +import jakarta.validation.constraints.NotNull; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class BatchFindCommentContentReqDTO { + + /** + * 笔记 ID + */ + @NotNull(message = "评论 ID 不能为空") + private Long noteId; + + @NotEmpty(message = "评论内容 Key 集合") + @Valid // 指定集合中的 DTO 也需要进行参数校验 + private List commentContentKeys; + +} \ No newline at end of file diff --git a/han-note-kv/han-note-kv-api/src/main/java/com/hanserwei/hannote/kv/dto/req/FindCommentContentReqDTO.java b/han-note-kv/han-note-kv-api/src/main/java/com/hanserwei/hannote/kv/dto/req/FindCommentContentReqDTO.java new file mode 100644 index 0000000..964a9f5 --- /dev/null +++ b/han-note-kv/han-note-kv-api/src/main/java/com/hanserwei/hannote/kv/dto/req/FindCommentContentReqDTO.java @@ -0,0 +1,21 @@ +package com.hanserwei.hannote.kv.dto.req; + +import jakarta.validation.constraints.NotBlank; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class FindCommentContentReqDTO { + + @NotBlank(message = "发布年月不能为空") + private String yearMonth; + + @NotBlank(message = "评论正文 ID 不能为空") + private String contentId; + +} \ No newline at end of file diff --git a/han-note-kv/han-note-kv-api/src/main/java/com/hanserwei/hannote/kv/dto/resp/FindCommentContentRspDTO.java b/han-note-kv/han-note-kv-api/src/main/java/com/hanserwei/hannote/kv/dto/resp/FindCommentContentRspDTO.java new file mode 100644 index 0000000..e4db9d5 --- /dev/null +++ b/han-note-kv/han-note-kv-api/src/main/java/com/hanserwei/hannote/kv/dto/resp/FindCommentContentRspDTO.java @@ -0,0 +1,24 @@ +package com.hanserwei.hannote.kv.dto.resp; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class FindCommentContentRspDTO { + + /** + * 评论内容 UUID + */ + private String contentId; + + /** + * 评论内容 + */ + private String content; + +} \ No newline at end of file diff --git a/han-note-kv/han-note-kv-biz/src/main/java/com/hanserwei/hannote/kv/biz/controller/CommentContentController.java b/han-note-kv/han-note-kv-biz/src/main/java/com/hanserwei/hannote/kv/biz/controller/CommentContentController.java index a5b4f16..d91e3d0 100644 --- a/han-note-kv/han-note-kv-biz/src/main/java/com/hanserwei/hannote/kv/biz/controller/CommentContentController.java +++ b/han-note-kv/han-note-kv-biz/src/main/java/com/hanserwei/hannote/kv/biz/controller/CommentContentController.java @@ -4,6 +4,7 @@ import com.hanserwei.framework.biz.operationlog.aspect.ApiOperationLog; import com.hanserwei.framework.common.response.Response; import com.hanserwei.hannote.kv.biz.service.CommentContentService; import com.hanserwei.hannote.kv.dto.req.BatchAddCommentContentReqDTO; +import com.hanserwei.hannote.kv.dto.req.BatchFindCommentContentReqDTO; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.springframework.validation.annotation.Validated; @@ -26,4 +27,10 @@ public class CommentContentController { return commentContentService.batchAddCommentContent(batchAddCommentContentReqDTO); } + @PostMapping(value = "/comment/content/batchFind") + @ApiOperationLog(description = "批量查询评论内容") + public Response batchFindCommentContent(@Validated @RequestBody BatchFindCommentContentReqDTO batchFindCommentContentReqDTO) { + return commentContentService.batchFindCommentContent(batchFindCommentContentReqDTO); + } + } diff --git a/han-note-kv/han-note-kv-biz/src/main/java/com/hanserwei/hannote/kv/biz/domain/repository/CommentContentRepository.java b/han-note-kv/han-note-kv-biz/src/main/java/com/hanserwei/hannote/kv/biz/domain/repository/CommentContentRepository.java new file mode 100644 index 0000000..c37ef1a --- /dev/null +++ b/han-note-kv/han-note-kv-biz/src/main/java/com/hanserwei/hannote/kv/biz/domain/repository/CommentContentRepository.java @@ -0,0 +1,23 @@ +package com.hanserwei.hannote.kv.biz.domain.repository; + +import com.hanserwei.hannote.kv.biz.domain.dataobject.CommentContentDO; +import com.hanserwei.hannote.kv.biz.domain.dataobject.CommentContentPrimaryKey; +import org.springframework.data.cassandra.repository.CassandraRepository; + +import java.util.List; +import java.util.UUID; + +public interface CommentContentRepository extends CassandraRepository { + + /** + * 批量查询评论内容 + * + * @param noteId 笔记ID + * @param yearMonths 年月 + * @param contentIds 评论 ID列表 + * @return 评论内容 + */ + List findByPrimaryKeyNoteIdAndPrimaryKeyYearMonthInAndPrimaryKeyContentIdIn( + Long noteId, List yearMonths, List contentIds + ); +} \ No newline at end of file diff --git a/han-note-kv/han-note-kv-biz/src/main/java/com/hanserwei/hannote/kv/biz/service/CommentContentService.java b/han-note-kv/han-note-kv-biz/src/main/java/com/hanserwei/hannote/kv/biz/service/CommentContentService.java index ba8918b..7696c00 100644 --- a/han-note-kv/han-note-kv-biz/src/main/java/com/hanserwei/hannote/kv/biz/service/CommentContentService.java +++ b/han-note-kv/han-note-kv-biz/src/main/java/com/hanserwei/hannote/kv/biz/service/CommentContentService.java @@ -2,8 +2,23 @@ package com.hanserwei.hannote.kv.biz.service; import com.hanserwei.framework.common.response.Response; import com.hanserwei.hannote.kv.dto.req.BatchAddCommentContentReqDTO; +import com.hanserwei.hannote.kv.dto.req.BatchFindCommentContentReqDTO; public interface CommentContentService { + /** + * 批量添加评论内容 + * + * @param batchAddCommentContentReqDTO 批量添加评论内容请求参数 + * @return 批量添加结果 + */ Response batchAddCommentContent(BatchAddCommentContentReqDTO batchAddCommentContentReqDTO); + + /** + * 批量查询评论内容 + * + * @param batchFindCommentContentReqDTO 批量查询评论内容请求参数 + * @return 批量查询结果 + */ + Response batchFindCommentContent(BatchFindCommentContentReqDTO batchFindCommentContentReqDTO); } diff --git a/han-note-kv/han-note-kv-biz/src/main/java/com/hanserwei/hannote/kv/biz/service/impl/CommentContentServiceImpl.java b/han-note-kv/han-note-kv-biz/src/main/java/com/hanserwei/hannote/kv/biz/service/impl/CommentContentServiceImpl.java index a528167..9c1790c 100644 --- a/han-note-kv/han-note-kv-biz/src/main/java/com/hanserwei/hannote/kv/biz/service/impl/CommentContentServiceImpl.java +++ b/han-note-kv/han-note-kv-biz/src/main/java/com/hanserwei/hannote/kv/biz/service/impl/CommentContentServiceImpl.java @@ -1,12 +1,19 @@ package com.hanserwei.hannote.kv.biz.service.impl; +import cn.hutool.core.collection.CollUtil; +import com.google.common.collect.Lists; import com.hanserwei.framework.common.response.Response; import com.hanserwei.hannote.kv.biz.domain.dataobject.CommentContentDO; import com.hanserwei.hannote.kv.biz.domain.dataobject.CommentContentPrimaryKey; +import com.hanserwei.hannote.kv.biz.domain.repository.CommentContentRepository; import com.hanserwei.hannote.kv.biz.service.CommentContentService; 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 jakarta.validation.constraints.NotBlank; import org.springframework.data.cassandra.core.CassandraTemplate; import org.springframework.stereotype.Service; @@ -18,6 +25,8 @@ public class CommentContentServiceImpl implements CommentContentService { @Resource private CassandraTemplate cassandraTemplate; + @Resource + private CommentContentRepository commentContentRepository; @Override public Response batchAddCommentContent(BatchAddCommentContentReqDTO batchAddCommentContentReqDTO) { @@ -47,4 +56,39 @@ public class CommentContentServiceImpl implements CommentContentService { .execute(); return Response.success(); } + + @Override + public Response batchFindCommentContent(BatchFindCommentContentReqDTO batchFindCommentContentReqDTO) { + // 归属的笔记ID + Long noteId = batchFindCommentContentReqDTO.getNoteId(); + // 查询评论的发布年月、内容 UUID + List commentContentKeys = batchFindCommentContentReqDTO.getCommentContentKeys(); + + // 过滤出年月 + List<@NotBlank(message = "发布年月不能为空") String> yearMonths = commentContentKeys.stream() + .map(FindCommentContentReqDTO::getYearMonth) + .distinct() + .toList(); + // 过滤出内容 UUID + List contentIds = commentContentKeys.stream() + .map(r -> UUID.fromString(r.getContentId())) + .distinct() + .toList(); + // 批量查询 Cassandra + List commentContentDOS = commentContentRepository + .findByPrimaryKeyNoteIdAndPrimaryKeyYearMonthInAndPrimaryKeyContentIdIn(noteId, yearMonths, contentIds); + + // DO 转 DTO + List findCommentContentRspDTOS = Lists.newArrayList(); + if (CollUtil.isNotEmpty(commentContentDOS)) { + findCommentContentRspDTOS = commentContentDOS.stream() + .map(commentContentDO -> FindCommentContentRspDTO.builder() + .contentId(String.valueOf(commentContentDO.getPrimaryKey().getContentId())) + .content(commentContentDO.getContent()) + .build()) + .toList(); + } + + return Response.success(findCommentContentRspDTOS); + } } diff --git a/http-client/gateApi.http b/http-client/gateApi.http index 8378bd9..b87432c 100644 --- a/http-client/gateApi.http +++ b/http-client/gateApi.http @@ -328,4 +328,22 @@ Content-Type: application/json "content": "这是一条评论内容3" } ] -} \ No newline at end of file +} + +### 批量查询评论内容 +POST http://localhost:8084/kv/comment/content/batchFind +Content-Type: application/json + +{ + "noteId": 1862481582414102549, + "commentContentKeys": [ + { + "yearMonth": "2025-11", + "contentId": "a2537301-dc80-4fd6-ab2f-a9a908baebba" + }, + { + "yearMonth": "2025-11", + "contentId": "a66b06ce-bf45-4a21-a5f2-31e5fb5fb5eb" + } + ] +}