注册登录,用户等级,社交,动态,新手任务

Change-Id: I1d3183526517fb3c0dab665e0e7547eefa5c9d76
diff --git a/src/main/java/com/example/myproject/service/SeedCommentService.java b/src/main/java/com/example/myproject/service/SeedCommentService.java
new file mode 100644
index 0000000..530b164
--- /dev/null
+++ b/src/main/java/com/example/myproject/service/SeedCommentService.java
@@ -0,0 +1,158 @@
+package com.example.myproject.service;
+
+import com.example.myproject.entity.SeedComment;
+import com.example.myproject.entity.SeedCommentLikes;
+import com.example.myproject.repository.SeedCommentLikesRepository;
+import com.example.myproject.repository.SeedCommentRepository;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.*;
+
+@Service
+public class SeedCommentService {
+
+    @Autowired
+    private SeedCommentRepository seedCommentRepository;
+
+    @Autowired
+    private SeedCommentLikesRepository seedCommentLikesRepository;
+
+    public String publishComment(int seedId, SeedComment seedComment) {
+        try {
+            // 如果是回复评论,检查父评论是否存在
+            if (seedComment.getComCommentId() != 0) {
+                // 查找父评论是否存在
+                boolean parentExists = seedCommentRepository.existsById(seedComment.getComCommentId());
+                if (!parentExists) {
+                    return "{ \"status\": \"error\", \"message\": \"父评论不存在\" }";
+                }
+            }
+
+            // 设置评论的种子ID
+            seedComment.setSeedId(seedId);
+            seedComment.setCommentTime(new Date());
+            seedComment.setLikesCount(0);  // 默认点赞数为0
+            seedComment.setReplyCount(0);  // 默认回复数为0
+
+            // 如果没有父评论,com_comment_id 默认为0
+            if (seedComment.getComCommentId() == 0) {
+                seedComment.setComCommentId(0);
+            }
+
+            // 保存评论到数据库
+            seedCommentRepository.save(seedComment);
+
+            // 返回成功信息
+            return "{ \"status\": \"success\", \"message\": \"评论发布成功\" }";
+        } catch (Exception e) {
+            // 处理异常,返回失败信息
+            return "{ \"status\": \"error\", \"message\": \"评论发布失败\" }";
+        }
+    }
+
+    public Map<String, Object> getAllCommentsForSeed(int seedId) {
+        // 获取该种子的所有评论(顶级评论)
+        List<SeedComment> allComments = seedCommentRepository.findBySeedId(seedId);
+        List<Map<String, Object>> responseComments = new ArrayList<>();
+
+        for (SeedComment comment : allComments) {
+            Map<String, Object> commentData = new HashMap<>();
+            commentData.put("comment_id", comment.getCommentId());
+            commentData.put("user_id", comment.getUserId());
+            commentData.put("username", "User" + comment.getUserId());  // 通过用户ID查询用户名
+            commentData.put("content", comment.getContent());
+            commentData.put("parent_comment_id", comment.getComCommentId());
+
+            // 获取该评论的所有回复
+            List<Map<String, Object>> replies = getRepliesForComment(comment.getCommentId());
+            commentData.put("replies", replies);
+
+            responseComments.add(commentData);
+        }
+
+        // 返回最终结果
+        Map<String, Object> result = new LinkedHashMap<>();
+        result.put("status", "success");
+        result.put("comments", responseComments);
+
+        return result;
+    }
+
+    // 获取特定评论的所有回复
+    private List<Map<String, Object>> getRepliesForComment(long parentCommentId) {
+        List<SeedComment> replies = seedCommentRepository.findByComCommentId(parentCommentId);
+        List<Map<String, Object>> replyResponses = new ArrayList<>();
+
+        for (SeedComment reply : replies) {
+            Map<String, Object> replyData = new LinkedHashMap<>();
+            replyData.put("comment_id", reply.getCommentId());
+            replyData.put("user_id", reply.getUserId());
+            replyData.put("username", "User" + reply.getUserId());  // 通过用户ID查询用户名
+            replyData.put("content", reply.getContent());
+            replyData.put("parent_comment_id", reply.getComCommentId());
+            replyResponses.add(replyData);
+        }
+
+        return replyResponses;
+    }
+
+    // 点赞切换逻辑
+    public Map<String, Object> toggleLike(Long commentId, Long userId) {
+        // 查找是否有该评论
+        Optional<SeedComment> seedCommentOptional = seedCommentRepository.findById(commentId);
+        if (!seedCommentOptional.isPresent()) {
+            return Map.of("status", "error", "message", "评论不存在");
+        }
+
+        SeedComment seedComment = seedCommentOptional.get();
+
+        // 查找该用户是否已点赞
+        Optional<SeedCommentLikes> commentLikeOptional = seedCommentLikesRepository.findByCommentIdAndUserId(commentId, userId);
+
+        // 如果用户未点赞,添加新的点赞记录
+        if (commentLikeOptional.isEmpty()) {
+            SeedCommentLikes newLike = new SeedCommentLikes();
+            newLike.setCommentId(commentId);
+            newLike.setUserId(userId);
+            newLike.setIsLiked(true);
+
+            // 更新 SeedComment 表中的点赞数
+            seedComment.setLikesCount(seedComment.getLikesCount() + 1);
+            seedCommentRepository.save(seedComment);
+
+            // 保存点赞记录
+            seedCommentLikesRepository.save(newLike);
+
+            return Map.of("status", "success", "message", "操作成功", "liked", "true");
+        } else {
+            SeedCommentLikes commentLike = commentLikeOptional.get();
+
+            // 如果已经点赞,取消点赞
+            if (commentLike.getIsLiked()) {
+                commentLike.setIsLiked(false);
+
+                // 更新 SeedComment 表中的点赞数
+                seedComment.setLikesCount(seedComment.getLikesCount() - 1);
+                seedCommentRepository.save(seedComment);
+
+                // 更新点赞记录
+                seedCommentLikesRepository.save(commentLike);
+
+                return Map.of("status", "success", "message", "操作成功", "liked", "false");
+            } else {
+                // 如果未点赞,进行点赞
+                commentLike.setIsLiked(true);
+
+                // 更新 SeedComment 表中的点赞数
+                seedComment.setLikesCount(seedComment.getLikesCount() + 1);
+                seedCommentRepository.save(seedComment);
+
+                // 更新点赞记录
+                seedCommentLikesRepository.save(commentLike);
+
+                return Map.of("status", "success", "message", "操作成功", "liked", "true");
+            }
+        }
+    }
+}