From 27f29826bcff850553db1cb0d26fba9736f9830b Mon Sep 17 00:00:00 2001 From: forely <1605769034@qq.com> Date: Tue, 3 Jun 2025 22:00:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E8=AF=84=E8=AE=BA=E5=AF=B9?= =?UTF-8?q?=E5=BA=94=E7=9A=84=E6=95=B0=E9=87=8F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../luojia-island/.vscode/launch.json | 22 +++++++++++++ .../post/service/impl/CommentServiceImpl.java | 32 +++++++++++++++---- 2 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 珞珈岛-项目相关文件/luojia-island/.vscode/launch.json diff --git a/珞珈岛-项目相关文件/luojia-island/.vscode/launch.json b/珞珈岛-项目相关文件/luojia-island/.vscode/launch.json new file mode 100644 index 0000000..6f5b8c4 --- /dev/null +++ b/珞珈岛-项目相关文件/luojia-island/.vscode/launch.json @@ -0,0 +1,22 @@ +{ + // 使用 IntelliSense 了解相关属性。 + // 悬停以查看现有属性的描述。 + // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Launch Edge", + "request": "launch", + "type": "msedge", + "url": "http://localhost:8080", + "webRoot": "${workspaceFolder}" + }, + { + "type": "msedge", + "request": "launch", + "name": "针对 localhost 启动 Edge", + "url": "http://localhost:8080", + "webRoot": "${workspaceFolder}" + } + ] +} \ No newline at end of file diff --git a/珞珈岛-项目相关文件/luojia-island/service/src/main/java/com/luojia_channel/modules/post/service/impl/CommentServiceImpl.java b/珞珈岛-项目相关文件/luojia-island/service/src/main/java/com/luojia_channel/modules/post/service/impl/CommentServiceImpl.java index 764d485..26cf113 100644 --- a/珞珈岛-项目相关文件/luojia-island/service/src/main/java/com/luojia_channel/modules/post/service/impl/CommentServiceImpl.java +++ b/珞珈岛-项目相关文件/luojia-island/service/src/main/java/com/luojia_channel/modules/post/service/impl/CommentServiceImpl.java @@ -185,22 +185,42 @@ public class CommentServiceImpl extends ServiceImpl impl @Transactional(rollbackFor = Exception.class) public void deleteComment(Long id) { validatePostUtil.validateCommentOwnership(id); - LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(Comment.class) - .eq(Comment::getTopId, id); - int delete = commentMapper.delete(queryWrapper); - if(delete <= 0) { - throw new PostException("删除评论失败"); - } Comment comment = commentMapper.selectById(id); if(comment.getId().equals(comment.getTopId())) { + LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(Comment.class) + .eq(Comment::getTopId, id); + int delete = commentMapper.delete(queryWrapper); + if(delete <= 0) { + throw new PostException("删除根评论失败"); + } + Post post = postMapper.selectById(comment.getPostId()); + post.setCommentCount(post.getCommentCount() - delete); + postMapper.updateById(post); + redisUtil.zRemove("post:comment_by_time:" + comment.getPostId(), comment.getId()); redisUtil.zRemove("post:comment_by_hot:" + comment.getPostId(), comment.getId()); redisUtil.delete("comment:reply_by_time:" + comment.getTopId()); redisUtil.delete("comment:reply_by_hot:" + comment.getTopId()); }else{ + int delete = commentMapper.deleteById(id); + if(delete <= 0){ + throw new PostException("删除评论失败"); + } + Post post = postMapper.selectById(comment.getPostId()); + post.setCommentCount(post.getCommentCount() - delete); + postMapper.updateById(post); + + Comment parentComment = commentMapper.selectById(comment.getParentCommentId()); + Comment topComment = commentMapper.selectById(comment.getTopId()); + parentComment.setReplyCount(parentComment.getReplyCount() - delete); + topComment.setReplyCount(topComment.getReplyCount() - delete); + updateById(parentComment); + updateById(topComment); + redisUtil.zRemove("comment:reply_by_time:" + comment.getTopId(), comment.getId()); redisUtil.zRemove("comment:reply_by_hot:" + comment.getTopId(), comment.getId()); } + // TODO 如果根评论删除,那么其他评论怎么办,目前做法是删除其下所有的子评论 }