22301138 | 1c35910 | 2025-06-03 15:19:59 +0800 | [diff] [blame^] | 1 | package com.example.myproject.service; |
| 2 | |
| 3 | import com.example.myproject.entity.SeedComment; |
| 4 | import com.example.myproject.entity.SeedCommentLikes; |
| 5 | import com.example.myproject.repository.SeedCommentLikesRepository; |
| 6 | import com.example.myproject.repository.SeedCommentRepository; |
| 7 | import org.springframework.beans.factory.annotation.Autowired; |
| 8 | import org.springframework.stereotype.Service; |
| 9 | |
| 10 | import java.util.*; |
| 11 | |
| 12 | @Service |
| 13 | public class SeedCommentService { |
| 14 | |
| 15 | @Autowired |
| 16 | private SeedCommentRepository seedCommentRepository; |
| 17 | |
| 18 | @Autowired |
| 19 | private SeedCommentLikesRepository seedCommentLikesRepository; |
| 20 | |
| 21 | public String publishComment(int seedId, SeedComment seedComment) { |
| 22 | try { |
| 23 | // 如果是回复评论,检查父评论是否存在 |
| 24 | if (seedComment.getComCommentId() != 0) { |
| 25 | // 查找父评论是否存在 |
| 26 | boolean parentExists = seedCommentRepository.existsById(seedComment.getComCommentId()); |
| 27 | if (!parentExists) { |
| 28 | return "{ \"status\": \"error\", \"message\": \"父评论不存在\" }"; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | // 设置评论的种子ID |
| 33 | seedComment.setSeedId(seedId); |
| 34 | seedComment.setCommentTime(new Date()); |
| 35 | seedComment.setLikesCount(0); // 默认点赞数为0 |
| 36 | seedComment.setReplyCount(0); // 默认回复数为0 |
| 37 | |
| 38 | // 如果没有父评论,com_comment_id 默认为0 |
| 39 | if (seedComment.getComCommentId() == 0) { |
| 40 | seedComment.setComCommentId(0); |
| 41 | } |
| 42 | |
| 43 | // 保存评论到数据库 |
| 44 | seedCommentRepository.save(seedComment); |
| 45 | |
| 46 | // 返回成功信息 |
| 47 | return "{ \"status\": \"success\", \"message\": \"评论发布成功\" }"; |
| 48 | } catch (Exception e) { |
| 49 | // 处理异常,返回失败信息 |
| 50 | return "{ \"status\": \"error\", \"message\": \"评论发布失败\" }"; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | public Map<String, Object> getAllCommentsForSeed(int seedId) { |
| 55 | // 获取该种子的所有评论(顶级评论) |
| 56 | List<SeedComment> allComments = seedCommentRepository.findBySeedId(seedId); |
| 57 | List<Map<String, Object>> responseComments = new ArrayList<>(); |
| 58 | |
| 59 | for (SeedComment comment : allComments) { |
| 60 | Map<String, Object> commentData = new HashMap<>(); |
| 61 | commentData.put("comment_id", comment.getCommentId()); |
| 62 | commentData.put("user_id", comment.getUserId()); |
| 63 | commentData.put("username", "User" + comment.getUserId()); // 通过用户ID查询用户名 |
| 64 | commentData.put("content", comment.getContent()); |
| 65 | commentData.put("parent_comment_id", comment.getComCommentId()); |
| 66 | |
| 67 | // 获取该评论的所有回复 |
| 68 | List<Map<String, Object>> replies = getRepliesForComment(comment.getCommentId()); |
| 69 | commentData.put("replies", replies); |
| 70 | |
| 71 | responseComments.add(commentData); |
| 72 | } |
| 73 | |
| 74 | // 返回最终结果 |
| 75 | Map<String, Object> result = new LinkedHashMap<>(); |
| 76 | result.put("status", "success"); |
| 77 | result.put("comments", responseComments); |
| 78 | |
| 79 | return result; |
| 80 | } |
| 81 | |
| 82 | // 获取特定评论的所有回复 |
| 83 | private List<Map<String, Object>> getRepliesForComment(long parentCommentId) { |
| 84 | List<SeedComment> replies = seedCommentRepository.findByComCommentId(parentCommentId); |
| 85 | List<Map<String, Object>> replyResponses = new ArrayList<>(); |
| 86 | |
| 87 | for (SeedComment reply : replies) { |
| 88 | Map<String, Object> replyData = new LinkedHashMap<>(); |
| 89 | replyData.put("comment_id", reply.getCommentId()); |
| 90 | replyData.put("user_id", reply.getUserId()); |
| 91 | replyData.put("username", "User" + reply.getUserId()); // 通过用户ID查询用户名 |
| 92 | replyData.put("content", reply.getContent()); |
| 93 | replyData.put("parent_comment_id", reply.getComCommentId()); |
| 94 | replyResponses.add(replyData); |
| 95 | } |
| 96 | |
| 97 | return replyResponses; |
| 98 | } |
| 99 | |
| 100 | // 点赞切换逻辑 |
| 101 | public Map<String, Object> toggleLike(Long commentId, Long userId) { |
| 102 | // 查找是否有该评论 |
| 103 | Optional<SeedComment> seedCommentOptional = seedCommentRepository.findById(commentId); |
| 104 | if (!seedCommentOptional.isPresent()) { |
| 105 | return Map.of("status", "error", "message", "评论不存在"); |
| 106 | } |
| 107 | |
| 108 | SeedComment seedComment = seedCommentOptional.get(); |
| 109 | |
| 110 | // 查找该用户是否已点赞 |
| 111 | Optional<SeedCommentLikes> commentLikeOptional = seedCommentLikesRepository.findByCommentIdAndUserId(commentId, userId); |
| 112 | |
| 113 | // 如果用户未点赞,添加新的点赞记录 |
| 114 | if (commentLikeOptional.isEmpty()) { |
| 115 | SeedCommentLikes newLike = new SeedCommentLikes(); |
| 116 | newLike.setCommentId(commentId); |
| 117 | newLike.setUserId(userId); |
| 118 | newLike.setIsLiked(true); |
| 119 | |
| 120 | // 更新 SeedComment 表中的点赞数 |
| 121 | seedComment.setLikesCount(seedComment.getLikesCount() + 1); |
| 122 | seedCommentRepository.save(seedComment); |
| 123 | |
| 124 | // 保存点赞记录 |
| 125 | seedCommentLikesRepository.save(newLike); |
| 126 | |
| 127 | return Map.of("status", "success", "message", "操作成功", "liked", "true"); |
| 128 | } else { |
| 129 | SeedCommentLikes commentLike = commentLikeOptional.get(); |
| 130 | |
| 131 | // 如果已经点赞,取消点赞 |
| 132 | if (commentLike.getIsLiked()) { |
| 133 | commentLike.setIsLiked(false); |
| 134 | |
| 135 | // 更新 SeedComment 表中的点赞数 |
| 136 | seedComment.setLikesCount(seedComment.getLikesCount() - 1); |
| 137 | seedCommentRepository.save(seedComment); |
| 138 | |
| 139 | // 更新点赞记录 |
| 140 | seedCommentLikesRepository.save(commentLike); |
| 141 | |
| 142 | return Map.of("status", "success", "message", "操作成功", "liked", "false"); |
| 143 | } else { |
| 144 | // 如果未点赞,进行点赞 |
| 145 | commentLike.setIsLiked(true); |
| 146 | |
| 147 | // 更新 SeedComment 表中的点赞数 |
| 148 | seedComment.setLikesCount(seedComment.getLikesCount() + 1); |
| 149 | seedCommentRepository.save(seedComment); |
| 150 | |
| 151 | // 更新点赞记录 |
| 152 | seedCommentLikesRepository.save(commentLike); |
| 153 | |
| 154 | return Map.of("status", "success", "message", "操作成功", "liked", "true"); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | } |