From 74170cd19c08549a5ab14172a79f84e6778e6e45 Mon Sep 17 00:00:00 2001 From: lixuhuilll <676824363@qq.com> Date: Wed, 3 Jan 2024 17:27:28 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E8=AF=84=E8=AE=BA=E5=93=8D=E5=BA=94?= =?UTF-8?q?=E7=82=B9=E8=B8=A9=E6=95=B0=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../utils/converter/CommentConverter.java | 10 +++++---- .../response/comments/CommentsInfo.java | 3 ++- .../response/comments/SubCommentsInfo.java | 3 ++- .../repository/entity/CommentsArea.java | 2 ++ .../backend/service/CommentsAreaService.java | 22 ++++++++++++++----- 5 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/main/java/plus/maa/backend/common/utils/converter/CommentConverter.java b/src/main/java/plus/maa/backend/common/utils/converter/CommentConverter.java index ba412411..553d99aa 100644 --- a/src/main/java/plus/maa/backend/common/utils/converter/CommentConverter.java +++ b/src/main/java/plus/maa/backend/common/utils/converter/CommentConverter.java @@ -15,16 +15,18 @@ @Mapper(componentModel = "spring") public interface CommentConverter { - @Mapping(target = "like", source = "likeCount") + @Mapping(target = "like", source = "commentsArea.likeCount") + @Mapping(target = "dislike", source = "commentsArea.dislikeCount") @Mapping(target = "uploader", source = "maaUser.userName") @Mapping(target = "commentId", source = "id") @Mapping(target = "subCommentsInfos", ignore = true) - CommentsInfo toCommentsInfo(CommentsArea commentsArea, String id, int likeCount, MaaUser maaUser); + CommentsInfo toCommentsInfo(CommentsArea commentsArea, String id, MaaUser maaUser); - @Mapping(target = "like", source = "likeCount") + @Mapping(target = "like", source = "commentsArea.likeCount") + @Mapping(target = "dislike", source = "commentsArea.dislikeCount") @Mapping(target = "uploader", source = "maaUser.userName") @Mapping(target = "commentId", source = "id") @Mapping(target = "deleted", source = "delete") - SubCommentsInfo toSubCommentsInfo(CommentsArea commentsArea, String id, int likeCount, MaaUser maaUser, boolean delete); + SubCommentsInfo toSubCommentsInfo(CommentsArea commentsArea, String id, MaaUser maaUser, boolean delete); } diff --git a/src/main/java/plus/maa/backend/controller/response/comments/CommentsInfo.java b/src/main/java/plus/maa/backend/controller/response/comments/CommentsInfo.java index f0a8e9be..e41a3193 100644 --- a/src/main/java/plus/maa/backend/controller/response/comments/CommentsInfo.java +++ b/src/main/java/plus/maa/backend/controller/response/comments/CommentsInfo.java @@ -26,7 +26,8 @@ public class CommentsInfo { //评论内容 private String message; private LocalDateTime uploadTime; - private int like; + private long like; + private long dislike; private boolean topping; private List subCommentsInfos = new ArrayList<>(); } diff --git a/src/main/java/plus/maa/backend/controller/response/comments/SubCommentsInfo.java b/src/main/java/plus/maa/backend/controller/response/comments/SubCommentsInfo.java index 5265ce76..ebe24c51 100644 --- a/src/main/java/plus/maa/backend/controller/response/comments/SubCommentsInfo.java +++ b/src/main/java/plus/maa/backend/controller/response/comments/SubCommentsInfo.java @@ -24,7 +24,8 @@ public class SubCommentsInfo { //评论内容 private String message; private LocalDateTime uploadTime; - private int like; + private long like; + private long dislike; private String fromCommentId; private String mainCommentId; private boolean deleted; diff --git a/src/main/java/plus/maa/backend/repository/entity/CommentsArea.java b/src/main/java/plus/maa/backend/repository/entity/CommentsArea.java index f16b3c0d..6d1a37c1 100644 --- a/src/main/java/plus/maa/backend/repository/entity/CommentsArea.java +++ b/src/main/java/plus/maa/backend/repository/entity/CommentsArea.java @@ -35,6 +35,8 @@ public class CommentsArea implements Serializable { private long likeCount; + private long dislikeCount; + private LocalDateTime uploadTime = LocalDateTime.now(); // 是否将该评论置顶 diff --git a/src/main/java/plus/maa/backend/service/CommentsAreaService.java b/src/main/java/plus/maa/backend/service/CommentsAreaService.java index d5bc14c2..52bda58e 100644 --- a/src/main/java/plus/maa/backend/service/CommentsAreaService.java +++ b/src/main/java/plus/maa/backend/service/CommentsAreaService.java @@ -193,7 +193,9 @@ public void rates(String userId, CommentsRatingDTO commentsRatingDTO) { CommentsArea commentsArea = findCommentsById(commentsRatingDTO.getCommentId()); - long change; + long likeCountChange; + long dislikeCountChange; + Optional ratingOptional = ratingRepository.findByTypeAndKeyAndUserId(Rating.KeyType.COMMENT, commentsArea.getId(), userId); // 判断该用户是否存在评分 if (ratingOptional.isPresent()) { @@ -204,8 +206,10 @@ public void rates(String userId, CommentsRatingDTO commentsRatingDTO) { ratingOptional.get().setRateTime(LocalDateTime.now()); RatingType newRatingType = ratingRepository.save(ratingOptional.get()).getRating(); // 更新评分后更新评论的点赞数 - change = newRatingType == RatingType.LIKE ? 1 : + likeCountChange = newRatingType == RatingType.LIKE ? 1 : (oldRatingType != RatingType.LIKE ? 0 : -1); + dislikeCountChange = newRatingType == RatingType.DISLIKE ? 1 : + (oldRatingType != RatingType.DISLIKE ? 0 : -1); } else { // 如果评分未发生变化则结束 return; @@ -220,15 +224,23 @@ public void rates(String userId, CommentsRatingDTO commentsRatingDTO) { .setRateTime(LocalDateTime.now()); ratingRepository.insert(newRating); - change = newRating.getRating() == RatingType.LIKE ? 1 : 0; + likeCountChange = newRating.getRating() == RatingType.LIKE ? 1 : 0; + dislikeCountChange = newRating.getRating() == RatingType.DISLIKE ? 1 : 0; } // 点赞数不需要在高并发下特别精准,大概就行,但是也得避免特别离谱的数字 - long likeCount = commentsArea.getLikeCount() + change; + long likeCount = commentsArea.getLikeCount() + likeCountChange; if (likeCount < 0) { likeCount = 0; } + + long dislikeCount = commentsArea.getDislikeCount() + dislikeCountChange; + if (dislikeCount < 0) { + dislikeCount = 0; + } + commentsArea.setLikeCount(likeCount); + commentsArea.setDislikeCount(dislikeCount); commentsAreaRepository.save(commentsArea); } @@ -329,7 +341,6 @@ public CommentsAreaInfo queriesCommentsArea(CommentsQueriesDTO request) { .toCommentsInfo( mainComment , mainComment.getId() - , (int) mainComment.getLikeCount() , maaUserMap.getOrDefault( mainComment.getUploaderId() , UNKNOWN_USER @@ -344,7 +355,6 @@ public CommentsAreaInfo queriesCommentsArea(CommentsQueriesDTO request) { .toSubCommentsInfo( subComment , subComment.getId() - , (int) subComment.getLikeCount() //填充评论用户名 , maaUserMap.getOrDefault( subComment.getUploaderId(), From 0504e3ecb415b4f9283c90f3a77922bdacd01719 Mon Sep 17 00:00:00 2001 From: lixuhuilll <676824363@qq.com> Date: Wed, 3 Jan 2024 17:39:36 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=87=8F=E5=B0=91=E8=BD=AC=E6=8D=A2?= =?UTF-8?q?=E5=99=A8=E4=B8=AD=E4=B8=8D=E5=BF=85=E8=A6=81=E7=9A=84=E5=8F=82?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/utils/converter/CommentConverter.java | 10 +++++----- .../plus/maa/backend/service/CommentsAreaService.java | 3 --- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/main/java/plus/maa/backend/common/utils/converter/CommentConverter.java b/src/main/java/plus/maa/backend/common/utils/converter/CommentConverter.java index 553d99aa..0f88940b 100644 --- a/src/main/java/plus/maa/backend/common/utils/converter/CommentConverter.java +++ b/src/main/java/plus/maa/backend/common/utils/converter/CommentConverter.java @@ -18,15 +18,15 @@ public interface CommentConverter { @Mapping(target = "like", source = "commentsArea.likeCount") @Mapping(target = "dislike", source = "commentsArea.dislikeCount") @Mapping(target = "uploader", source = "maaUser.userName") - @Mapping(target = "commentId", source = "id") + @Mapping(target = "commentId", source = "commentsArea.id") @Mapping(target = "subCommentsInfos", ignore = true) - CommentsInfo toCommentsInfo(CommentsArea commentsArea, String id, MaaUser maaUser); + CommentsInfo toCommentsInfo(CommentsArea commentsArea, MaaUser maaUser); @Mapping(target = "like", source = "commentsArea.likeCount") @Mapping(target = "dislike", source = "commentsArea.dislikeCount") @Mapping(target = "uploader", source = "maaUser.userName") - @Mapping(target = "commentId", source = "id") - @Mapping(target = "deleted", source = "delete") - SubCommentsInfo toSubCommentsInfo(CommentsArea commentsArea, String id, MaaUser maaUser, boolean delete); + @Mapping(target = "commentId", source = "commentsArea.id") + @Mapping(target = "deleted", source = "commentsArea.delete") + SubCommentsInfo toSubCommentsInfo(CommentsArea commentsArea, MaaUser maaUser); } diff --git a/src/main/java/plus/maa/backend/service/CommentsAreaService.java b/src/main/java/plus/maa/backend/service/CommentsAreaService.java index 52bda58e..16ac8d5d 100644 --- a/src/main/java/plus/maa/backend/service/CommentsAreaService.java +++ b/src/main/java/plus/maa/backend/service/CommentsAreaService.java @@ -340,7 +340,6 @@ public CommentsAreaInfo queriesCommentsArea(CommentsQueriesDTO request) { commentConverter .toCommentsInfo( mainComment - , mainComment.getId() , maaUserMap.getOrDefault( mainComment.getUploaderId() , UNKNOWN_USER @@ -354,13 +353,11 @@ public CommentsAreaInfo queriesCommentsArea(CommentsQueriesDTO request) { commentConverter .toSubCommentsInfo( subComment - , subComment.getId() //填充评论用户名 , maaUserMap.getOrDefault( subComment.getUploaderId(), UNKNOWN_USER ) - , subComment.isDelete() ) ).toList();