22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 1 | package com.example.myproject.service; |
| 2 | |
| 3 | import com.example.myproject.entity.Comments; |
| 4 | import com.example.myproject.entity.Post; |
| 5 | import com.example.myproject.entity.Users; |
| 6 | import com.example.myproject.repository.CommentRepository; |
| 7 | import com.example.myproject.repository.PostRepository; |
| 8 | import com.example.myproject.repository.UserRepository; |
| 9 | import org.springframework.beans.factory.annotation.Autowired; |
| 10 | import org.springframework.stereotype.Service; |
| 11 | |
| 12 | import java.util.ArrayList; |
| 13 | import java.util.LinkedHashMap; |
| 14 | import java.util.List; |
| 15 | import java.util.Map; |
| 16 | import java.util.stream.Collectors; |
| 17 | |
| 18 | @Service |
| 19 | public class CommentService { |
| 20 | |
| 21 | @Autowired |
| 22 | private CommentRepository commentRepository; |
| 23 | |
| 24 | @Autowired |
| 25 | private PostRepository postRepository; |
| 26 | |
| 27 | @Autowired |
| 28 | private UserRepository userRepository; |
| 29 | |
| 30 | // 添加评论 |
| 31 | public void addComment(Long postId, Comments comment) { |
| 32 | try { |
| 33 | // 查找帖子 |
| 34 | Post post = postRepository.findById(postId).orElseThrow(() -> { |
| 35 | throw new RuntimeException("Post with ID " + postId + " not found"); |
| 36 | }); |
| 37 | |
| 38 | // 查找用户 |
| 39 | Users user = userRepository.findById(comment.getUserId()).orElseThrow(() -> { |
| 40 | throw new RuntimeException("User with ID " + comment.getUserId() + " not found"); |
| 41 | }); |
| 42 | |
| 43 | // 设置评论的相关属性 |
| 44 | comment.setPost(post); |
| 45 | comment.setUserId(user.getUserId()); |
| 46 | comment.setCommentTime(new java.util.Date()); |
| 47 | |
| 48 | if (comment.getParentComment() != null && comment.getParentComment() != 0) { |
| 49 | // 查找父评论 |
| 50 | Comments parentComment = commentRepository.findById(comment.getParentComment()) |
| 51 | .orElseThrow(() -> new RuntimeException("Parent comment with ID " + comment.getParentComment() + " not found")); |
| 52 | comment.setParentComment(parentComment.getCommentId()); // 设置父评论 ID |
| 53 | } else { |
| 54 | comment.setParentComment(0L); // 设置为 0,表示没有父评论 |
| 55 | } |
| 56 | |
| 57 | // 保存评论 |
| 58 | commentRepository.save(comment); |
| 59 | } catch (Exception e) { |
| 60 | // 捕获任何异常并打印堆栈信息 |
| 61 | e.printStackTrace(); |
| 62 | throw new RuntimeException("An error occurred while adding the comment: " + e.getMessage()); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // 获取指定帖子的所有评论 |
| 67 | public List<Map<String, Object>> getCommentsByPostId(Long postId) { |
| 68 | List<Comments> comments = commentRepository.findAll() |
| 69 | .stream() |
| 70 | .filter(comment -> comment.getPost().getPostNo().equals(postId)) // 通过 post 的 postNo 过滤评论 |
| 71 | .collect(Collectors.toList()); |
| 72 | |
| 73 | if (comments.isEmpty()) { |
| 74 | return new ArrayList<>(); |
| 75 | } |
| 76 | |
| 77 | List<Map<String, Object>> result = new ArrayList<>(); |
| 78 | for (Comments comment : comments) { |
| 79 | Map<String, Object> commentData = new LinkedHashMap<>(); |
| 80 | commentData.put("commentId", comment.getCommentId()); |
| 81 | commentData.put("post_id", comment.getPost().getPostNo()); |
| 82 | commentData.put("userId", comment.getUserId()); |
| 83 | |
| 84 | Users user = userRepository.findById(comment.getUserId()).orElse(null); |
| 85 | commentData.put("nickname", user != null ? user.getUsername() : null); |
| 86 | commentData.put("avatar_url", user != null ? user.getAvatarUrl() : null); |
| 87 | |
| 88 | commentData.put("content", comment.getContent()); |
| 89 | commentData.put("isAnonymous", comment.getIsAnonymous()); |
| 90 | commentData.put("likesCount", comment.getLikesCount()); |
| 91 | commentData.put("replyCount", comment.getReplyCount()); |
| 92 | commentData.put("commentTime", comment.getCommentTime()); |
| 93 | commentData.put("parentComment", comment.getParentComment()); |
| 94 | |
| 95 | Map<String, Object> parentCommentData = getParentCommentData(comment.getParentComment()); |
| 96 | commentData.put("parentCommentData", parentCommentData); |
| 97 | |
| 98 | result.add(commentData); |
| 99 | } |
| 100 | |
| 101 | return result; |
| 102 | } |
| 103 | |
| 104 | // 递归查找父评论的内容 |
| 105 | private Map<String, Object> getParentCommentData(Long parentCommentId) { |
| 106 | Map<String, Object> parentCommentData = new LinkedHashMap<>(); |
| 107 | if (parentCommentId != null && parentCommentId != 0) { |
| 108 | // 查找父评论 |
| 109 | Comments parentComment = commentRepository.findById(parentCommentId).orElse(null); |
| 110 | if (parentComment != null) { |
| 111 | parentCommentData.put("commentId", parentComment.getCommentId()); |
| 112 | parentCommentData.put("content", parentComment.getContent()); |
| 113 | parentCommentData.put("userId", parentComment.getUserId()); |
| 114 | |
| 115 | Users user = userRepository.findById(parentComment.getUserId()).orElse(null); |
| 116 | parentCommentData.put("nickname", user != null ? user.getUsername() : null); |
| 117 | parentCommentData.put("avatar_url", user != null ? user.getAvatarUrl() : null); |
| 118 | |
| 119 | Map<String, Object> grandParentData = getParentCommentData(parentComment.getParentComment()); |
| 120 | parentCommentData.put("parentCommentData", grandParentData); |
| 121 | } |
| 122 | } |
| 123 | return parentCommentData; |
| 124 | } |
| 125 | } |