From 28842c90b1d4ce8c17bb1bfe8e6f2ff46043f2de Mon Sep 17 00:00:00 2001 From: sivdead <923396178@qq.com> Date: Mon, 1 Sep 2025 11:32:20 +0800 Subject: [PATCH] =?UTF-8?q?feat(service):=20=E5=9C=A8=20CommentService=20?= =?UTF-8?q?=E4=B8=AD=E6=B7=BB=E5=8A=A0=E9=80=BB=E8=BE=91=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E8=AF=84=E8=AE=BA=E6=97=B6=E9=87=8D=E6=96=B0=E8=AE=A1=E7=AE=97?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=A7=AF=E5=88=86=E7=9A=84=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=EF=BC=8C=E5=B9=B6=E5=9C=A8=20PointService=20=E4=B8=AD=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E7=94=A8=E6=88=B7=E7=A7=AF=E5=88=86=E7=9A=84=E9=87=8D?= =?UTF-8?q?=E6=96=B0=E8=AE=A1=E7=AE=97=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/openisle/service/CommentService.java | 49 +++++++++++++++---- .../com/openisle/service/PointService.java | 28 +++++++++++ 2 files changed, 68 insertions(+), 9 deletions(-) diff --git a/backend/src/main/java/com/openisle/service/CommentService.java b/backend/src/main/java/com/openisle/service/CommentService.java index e37ded111..1076aa5e2 100644 --- a/backend/src/main/java/com/openisle/service/CommentService.java +++ b/backend/src/main/java/com/openisle/service/CommentService.java @@ -4,6 +4,7 @@ import com.openisle.model.Comment; import com.openisle.model.Post; import com.openisle.model.User; import com.openisle.model.NotificationType; +import com.openisle.model.PointHistory; import com.openisle.model.CommentSort; import com.openisle.repository.CommentRepository; import com.openisle.repository.PostRepository; @@ -14,6 +15,7 @@ import com.openisle.repository.NotificationRepository; import com.openisle.repository.PointHistoryRepository; import com.openisle.service.NotificationService; import com.openisle.service.SubscriptionService; +import com.openisle.service.PointService; import com.openisle.model.Role; import com.openisle.exception.RateLimitException; import lombok.RequiredArgsConstructor; @@ -21,6 +23,9 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import java.util.List; +import java.util.Set; +import java.util.HashSet; +import java.util.stream.Collectors; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.transaction.annotation.Transactional; @@ -39,6 +44,7 @@ public class CommentService { private final CommentSubscriptionRepository commentSubscriptionRepository; private final NotificationRepository notificationRepository; private final PointHistoryRepository pointHistoryRepository; + private final PointService pointService; private final ImageUploader imageUploader; @Transactional @@ -65,16 +71,19 @@ public class CommentService { log.debug("Comment {} saved for post {}", comment.getId(), postId); imageUploader.addReferences(imageUploader.extractUrls(content)); if (!author.getId().equals(post.getAuthor().getId())) { - notificationService.createNotification(post.getAuthor(), NotificationType.COMMENT_REPLY, post, comment, null, null, null, null); + notificationService.createNotification(post.getAuthor(), NotificationType.COMMENT_REPLY, post, comment, + null, null, null, null); } for (User u : subscriptionService.getPostSubscribers(postId)) { if (!u.getId().equals(author.getId())) { - notificationService.createNotification(u, NotificationType.POST_UPDATED, post, comment, null, null, null, null); + notificationService.createNotification(u, NotificationType.POST_UPDATED, post, comment, null, null, + null, null); } } for (User u : subscriptionService.getSubscribers(author.getUsername())) { if (!u.getId().equals(author.getId())) { - notificationService.createNotification(u, NotificationType.USER_ACTIVITY, post, comment, null, null, null, null); + notificationService.createNotification(u, NotificationType.USER_ACTIVITY, post, comment, null, null, + null, null); } } notificationService.notifyMentions(content, author, post, comment); @@ -111,21 +120,25 @@ public class CommentService { log.debug("Reply {} saved for parent {}", comment.getId(), parentId); imageUploader.addReferences(imageUploader.extractUrls(content)); if (!author.getId().equals(parent.getAuthor().getId())) { - notificationService.createNotification(parent.getAuthor(), NotificationType.COMMENT_REPLY, parent.getPost(), comment, null, null, null, null); + notificationService.createNotification(parent.getAuthor(), NotificationType.COMMENT_REPLY, parent.getPost(), + comment, null, null, null, null); } for (User u : subscriptionService.getCommentSubscribers(parentId)) { if (!u.getId().equals(author.getId())) { - notificationService.createNotification(u, NotificationType.COMMENT_REPLY, parent.getPost(), comment, null, null, null, null); + notificationService.createNotification(u, NotificationType.COMMENT_REPLY, parent.getPost(), comment, + null, null, null, null); } } for (User u : subscriptionService.getPostSubscribers(parent.getPost().getId())) { if (!u.getId().equals(author.getId())) { - notificationService.createNotification(u, NotificationType.POST_UPDATED, parent.getPost(), comment, null, null, null, null); + notificationService.createNotification(u, NotificationType.POST_UPDATED, parent.getPost(), comment, + null, null, null, null); } } for (User u : subscriptionService.getSubscribers(author.getUsername())) { if (!u.getId().equals(author.getId())) { - notificationService.createNotification(u, NotificationType.USER_ACTIVITY, parent.getPost(), comment, null, null, null, null); + notificationService.createNotification(u, NotificationType.USER_ACTIVITY, parent.getPost(), comment, + null, null, null, null); } } notificationService.notifyMentions(content, author, parent.getPost(), comment); @@ -237,15 +250,33 @@ public class CommentService { for (Comment c : replies) { deleteCommentCascade(c); } - // 逻辑删除相关的积分历史记录 - pointHistoryRepository.findByComment(comment).forEach(pointHistoryRepository::delete); + + // 逻辑删除相关的积分历史记录,并收集受影响的用户 + List pointHistories = pointHistoryRepository.findByComment(comment); + // 收集需要重新计算积分的用户 + Set usersToRecalculate = pointHistories.stream().map(PointHistory::getUser).collect(Collectors.toSet()); + // 删除其他相关数据 reactionRepository.findByComment(comment).forEach(reactionRepository::delete); commentSubscriptionRepository.findByComment(comment).forEach(commentSubscriptionRepository::delete); notificationRepository.deleteAll(notificationRepository.findByComment(comment)); imageUploader.removeReferences(imageUploader.extractUrls(comment.getContent())); + // 逻辑删除评论 commentRepository.delete(comment); + // 删除积分历史 + pointHistoryRepository.deleteAll(pointHistories); + + // 重新计算受影响用户的积分 + if (!usersToRecalculate.isEmpty()) { + for (User user : usersToRecalculate) { + int newPoints = pointService.recalculateUserPoints(user); + user.setPoint(newPoints); + log.debug("Recalculated points for user {}: {}", user.getUsername(), newPoints); + } + userRepository.saveAll(usersToRecalculate); + } + log.debug("deleteCommentCascade removed comment {}", comment.getId()); } diff --git a/backend/src/main/java/com/openisle/service/PointService.java b/backend/src/main/java/com/openisle/service/PointService.java index 4bfe410af..677fd0a0d 100644 --- a/backend/src/main/java/com/openisle/service/PointService.java +++ b/backend/src/main/java/com/openisle/service/PointService.java @@ -219,4 +219,32 @@ public class PointService { return result; } + /** + * 重新计算用户的积分总数 + * 通过累加所有积分历史记录来重新计算用户的当前积分 + */ + public int recalculateUserPoints(User user) { + // 获取用户所有的积分历史记录(由于@Where注解,已删除的记录会被自动过滤) + List histories = pointHistoryRepository.findByUserOrderByIdDesc(user); + + int totalPoints = 0; + for (PointHistory history : histories) { + totalPoints += history.getAmount(); + } + + // 更新用户积分 + user.setPoint(totalPoints); + userRepository.save(user); + + return totalPoints; + } + + /** + * 重新计算用户的积分总数(通过用户名) + */ + public int recalculateUserPoints(String userName) { + User user = userRepository.findByUsername(userName).orElseThrow(); + return recalculateUserPoints(user); + } + }