调试交互

Change-Id: Ib9d7b8432cb622c7e28c842ab4dc4e156fcd6414
diff --git a/src/main/java/com/example/myproject/service/DynamicService.java b/src/main/java/com/example/myproject/service/DynamicService.java
index 853604d..9190d57 100644
--- a/src/main/java/com/example/myproject/service/DynamicService.java
+++ b/src/main/java/com/example/myproject/service/DynamicService.java
@@ -1,3 +1,278 @@
+// package com.example.myproject.service;
+
+// import com.example.myproject.entity.*;
+// import com.example.myproject.repository.*;
+// import org.springframework.beans.factory.annotation.Autowired;
+// import org.springframework.stereotype.Service;
+// import org.springframework.web.multipart.MultipartFile;
+
+// import java.io.IOException;
+// import java.nio.file.Files;
+// import java.nio.file.Path;
+// import java.nio.file.Paths;
+// import java.util.*;
+
+// @Service
+// public class DynamicService {
+
+//     @Autowired
+//     private UserDynamicRepository userDynamicRepository;
+
+//     @Autowired
+//     private UserRepository userRepository;
+
+//     @Autowired
+//     private DynamicCommentRepository dynamicCommentRepository;
+
+//     @Autowired
+//     private DynamicLikesRepository dynamicLikesRepository;
+
+//     @Autowired
+//     private FriendRelationRepository friendRelationRepository;
+
+//     private static final String IMAGE_DIR = "uploads/post/";
+
+//     // 创建好友动态
+//     public Map<String, Object> createDynamic(Long userId, String title, String content, MultipartFile[] imageFiles) {
+//         // 查找用户信息
+//         Users user = userRepository.findById(userId)
+//                 .orElseThrow(() -> new RuntimeException("User not found"));
+
+//         // 创建新的动态
+//         UserDynamic userDynamic = new UserDynamic();
+//         userDynamic.setUserId(userId);
+//         userDynamic.setTitle(title != null ? title : "默认标题");
+//         userDynamic.setContent(content);
+//         userDynamic.setTime(new Date());
+//         userDynamic.setLikesCount(0);
+//         userDynamic.setCommentsCount(0);
+
+//         // 处理图片上传
+//         StringBuilder imageUrlsBuilder = new StringBuilder();
+//         if (imageFiles != null && imageFiles.length > 0) {
+//             for (int i = 0; i < imageFiles.length; i++) {
+//                 if (i > 0) {
+//                     imageUrlsBuilder.append(",");
+//                 }
+//                 try {
+//                     String imageUrl = saveImage(imageFiles[i]);
+//                     imageUrlsBuilder.append(imageUrl);
+//                 } catch (IOException e) {
+//                     throw new RuntimeException("Image upload failed: " + e.getMessage());
+//                 }
+//             }
+//         }
+
+//         // 将多个图片 URL 拼接
+//         userDynamic.setImageUrl(imageUrlsBuilder.toString());
+
+//         // 保存数据库
+//         UserDynamic savedUserDynamic = userDynamicRepository.save(userDynamic);
+
+//         // 返回结果
+//         Map<String, Object> response = new HashMap<>();
+//         response.put("dynamicId", savedUserDynamic.getDynamicId());
+//         response.put("message", "动态创建成功");
+//         return response;
+//     }
+
+//     // 保存图片并返回图片的 URL
+//     public String saveImage(MultipartFile imageFile) throws IOException {
+//         String fileName = imageFile.getOriginalFilename();
+//         Path path = Paths.get(IMAGE_DIR + fileName);
+//         Files.createDirectories(path.getParent());
+//         Files.write(path, imageFile.getBytes());
+//         return "/images/" + fileName;
+//     }
+
+//     // 删除动态
+//     public void deleteDynamic(Long dynamicId) {
+//         if (userDynamicRepository.existsById(dynamicId)) {
+//             userDynamicRepository.deleteById(dynamicId);
+//         } else {
+//             throw new RuntimeException("Dynamic with id " + dynamicId + " not found");
+//         }
+//     }
+
+//     //好友动态评论
+//     public Map<String, Object> addComment(Long userId, Long dynamicId, String commentContent) {
+//         Users user = userRepository.findById(userId)
+//                 .orElseThrow(() -> new RuntimeException("User not found"));
+
+//         UserDynamic dynamic = userDynamicRepository.findById(dynamicId)
+//                 .orElseThrow(() -> new RuntimeException("Dynamic not found"));
+
+//         DynamicComment comment = new DynamicComment();
+//         comment.setDynamicId(dynamicId);
+//         comment.setUserId(userId);
+//         comment.setCommentContent(commentContent);
+//         comment.setCommentTime(new Date());
+
+//         // 保存评论
+//         DynamicComment savedComment = dynamicCommentRepository.save(comment);
+
+//         // 返回成功消息
+//         Map<String, Object> response = new HashMap<>();
+//         response.put("comment_id", savedComment.getCommentId());
+//         response.put("message", "评论成功");
+//         return response;
+//     }
+
+//     //返回某个好友的所有动态
+//     public Map<String, Object> getAllUserDynamics(Long userId) {
+//         // 查找用户的所有动态
+//         List<UserDynamic> userDynamics = userDynamicRepository.findByUserId(userId);
+//         Map<String, Object> postData = new LinkedHashMap<>();
+//         List<Map<String, Object>> dynamicList = new ArrayList<>();
+
+//         // 遍历动态,获取点赞和评论
+//         for (UserDynamic dynamic : userDynamics) {
+//             Map<String, Object> dynamicData = new LinkedHashMap<>();
+//             dynamicData.put("dynamic_id", dynamic.getDynamicId());
+//             dynamicData.put("user_id", dynamic.getUserId());
+
+//             Users user = userRepository.findById(dynamic.getUserId()).orElse(null);
+//             if (user != null) {
+//                 dynamicData.put("username", user.getUsername());
+//                 dynamicData.put("avatar_url", user.getAvatarUrl());
+//             } else {
+//                 dynamicData.put("username", "Unknown");
+//                 dynamicData.put("avatar_url", "http://example.com/default-avatar.jpg");
+//             }
+
+//             dynamicData.put("title", dynamic.getTitle());
+//             dynamicData.put("content", dynamic.getContent());
+//             dynamicData.put("images", dynamic.getImageUrl());
+//             dynamicData.put("time", dynamic.getTime());
+
+//             // 获取点赞
+//             List<DynamicLikes> likes = dynamicLikesRepository.findByDynamicId(dynamic.getDynamicId());
+//             List<Map<String, Object>> likeList = new ArrayList<>();
+//             for (DynamicLikes like : likes) {
+//                 Map<String, Object> likeData = new HashMap<>();
+//                 likeData.put("user_id", like.getUserId());
+
+//                 // 获取点赞用户的用户名
+//                 Users likeUser = userRepository.findById(like.getUserId()).orElse(null);
+//                 if (likeUser != null) {
+//                     likeData.put("username", likeUser.getUsername());
+//                 } else {
+//                     likeData.put("username", "Unknown");
+//                 }
+
+//                 likeList.add(likeData);
+//             }
+//             dynamicData.put("likes", likeList);
+
+//             // 获取评论
+//             List<DynamicComment> comments = dynamicCommentRepository.findByDynamicId(dynamic.getDynamicId());
+//             List<Map<String, Object>> commentList = new ArrayList<>();
+//             for (DynamicComment comment : comments) {
+//                 Map<String, Object> commentData = new HashMap<>();
+//                 commentData.put("comment_id", comment.getCommentId());
+//                 commentData.put("user_id", comment.getUserId());
+
+//                 // 获取评论用户的用户名
+//                 Users commentUser = userRepository.findById(comment.getUserId()).orElse(null);
+//                 if (commentUser != null) {
+//                     commentData.put("username", commentUser.getUsername());
+//                 } else {
+//                     commentData.put("username", "Unknown");
+//                 }
+
+//                 commentData.put("content", comment.getCommentContent());
+//                 commentData.put("time", comment.getCommentTime());
+//                 commentList.add(commentData);
+//             }
+//             dynamicData.put("comments", commentList);
+
+//             dynamicList.add(dynamicData);
+//         }
+
+//         Map<String, Object> response = new HashMap<>();
+//         response.put("dynamic", dynamicList);
+//         return response;
+//     }
+
+//     //获取当前所有好友的id
+//     public List<Long> getAllFriendIds(Long userId) {
+//         List<FriendRelation> friendRelations = friendRelationRepository.findByUserId(userId);
+//         List<Long> friendIds = new ArrayList<>();
+
+//         for (FriendRelation relation : friendRelations) {
+//             // 添加好友ID
+//             if (!friendIds.contains(relation.getFriendId())) {
+//                 friendIds.add(relation.getFriendId());
+//             }
+//         }
+
+//         return friendIds;
+//     }
+
+//     public Map<String, Object> getAllUserAndFriendsDynamics(List<Long> userIds) {
+//         List<Map<String, Object>> allDynamics = new ArrayList<>();
+
+//         // 遍历所有用户获取他们的动态
+//         for (Long userId : userIds) {
+//             Map<String, Object> userDynamics = getAllUserDynamics(userId);
+//             if (userDynamics != null && !userDynamics.isEmpty()) {
+//                 allDynamics.addAll((List<Map<String, Object>>) userDynamics.get("dynamic"));
+//             }
+//         }
+
+//         // 返回所有动态的合集
+//         Map<String, Object> response = new HashMap<>();
+//         response.put("dynamic", allDynamics);
+//         return response;
+//     }
+
+//     //点赞动态
+//     public boolean likeDynamic(Long userId, Long dynamicId) {
+//         // 检查该用户是否已经点赞过该动态
+//         DynamicLikes existingLike = dynamicLikesRepository.findByUserIdAndDynamicId(userId, dynamicId);
+//         if (existingLike != null) {
+//             return false;
+//         }
+
+//         // 新增点赞记录
+//         DynamicLikes newLike = new DynamicLikes();
+//         newLike.setUserId(userId);
+//         newLike.setDynamicId(dynamicId);
+//         newLike.setLikeTime(new java.util.Date());
+//         dynamicLikesRepository.save(newLike);
+
+//         // 更新动态表的点赞数
+//         UserDynamic dynamic = userDynamicRepository.findById(dynamicId).orElse(null);
+//         if (dynamic != null) {
+//             dynamic.setLikesCount(dynamic.getLikesCount() + 1);
+//             userDynamicRepository.save(dynamic);
+//         }
+
+//         return true;
+//     }
+
+//     //取消点赞
+//     public boolean unlikeDynamic(Long userId, Long dynamicId) {
+//         // 查找该用户的点赞记录
+//         DynamicLikes existingLike = dynamicLikesRepository.findByUserIdAndDynamicId(userId, dynamicId);
+//         if (existingLike == null) {
+//             return false;
+//         }
+
+//         // 删除点赞记录
+//         dynamicLikesRepository.delete(existingLike);
+
+//         // 更新动态表的点赞数
+//         UserDynamic dynamic = userDynamicRepository.findById(dynamicId).orElse(null);
+//         if (dynamic != null) {
+//             dynamic.setLikesCount(dynamic.getLikesCount() - 1);
+//             userDynamicRepository.save(dynamic);
+//         }
+
+//         return true;
+//     }
+
+// }
 package com.example.myproject.service;
 
 import com.example.myproject.entity.*;
@@ -95,7 +370,7 @@
     }
 
     //好友动态评论
-    public Map<String, Object> addComment(Long userId, Long dynamicId, String commentContent) {
+    public Map<String, Object> addComment(Long userId, Long dynamicId, String commentContent, Long parentCommentId) {
         Users user = userRepository.findById(userId)
                 .orElseThrow(() -> new RuntimeException("User not found"));
 
@@ -107,6 +382,7 @@
         comment.setUserId(userId);
         comment.setCommentContent(commentContent);
         comment.setCommentTime(new Date());
+        comment.setParentCommentId(parentCommentId);  // 设置父评论ID
 
         // 保存评论
         DynamicComment savedComment = dynamicCommentRepository.save(comment);
@@ -114,10 +390,12 @@
         // 返回成功消息
         Map<String, Object> response = new HashMap<>();
         response.put("comment_id", savedComment.getCommentId());
+        response.put("parent_comment_id", savedComment.getParentCommentId());
         response.put("message", "评论成功");
         return response;
     }
 
+
     //返回某个好友的所有动态
     public Map<String, Object> getAllUserDynamics(Long userId) {
         // 查找用户的所有动态