22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 1 | package com.example.myproject.service; |
| 2 | |
| 3 | import com.example.myproject.entity.*; |
| 4 | import com.example.myproject.repository.*; |
| 5 | import org.springframework.beans.factory.annotation.Autowired; |
| 6 | import org.springframework.stereotype.Service; |
| 7 | import org.springframework.web.multipart.MultipartFile; |
| 8 | |
| 9 | import java.io.IOException; |
| 10 | import java.nio.file.Files; |
| 11 | import java.nio.file.Path; |
| 12 | import java.nio.file.Paths; |
| 13 | import java.util.*; |
| 14 | |
| 15 | @Service |
| 16 | public class DynamicService { |
| 17 | |
| 18 | @Autowired |
| 19 | private UserDynamicRepository userDynamicRepository; |
| 20 | |
| 21 | @Autowired |
| 22 | private UserRepository userRepository; |
| 23 | |
| 24 | @Autowired |
| 25 | private DynamicCommentRepository dynamicCommentRepository; |
| 26 | |
| 27 | @Autowired |
| 28 | private DynamicLikesRepository dynamicLikesRepository; |
| 29 | |
| 30 | @Autowired |
| 31 | private FriendRelationRepository friendRelationRepository; |
| 32 | |
| 33 | private static final String IMAGE_DIR = "uploads/post/"; |
| 34 | |
| 35 | // 创建好友动态 |
| 36 | public Map<String, Object> createDynamic(Long userId, String title, String content, MultipartFile[] imageFiles) { |
| 37 | // 查找用户信息 |
| 38 | Users user = userRepository.findById(userId) |
| 39 | .orElseThrow(() -> new RuntimeException("User not found")); |
| 40 | |
| 41 | // 创建新的动态 |
| 42 | UserDynamic userDynamic = new UserDynamic(); |
| 43 | userDynamic.setUserId(userId); |
| 44 | userDynamic.setTitle(title != null ? title : "默认标题"); |
| 45 | userDynamic.setContent(content); |
| 46 | userDynamic.setTime(new Date()); |
| 47 | userDynamic.setLikesCount(0); |
| 48 | userDynamic.setCommentsCount(0); |
| 49 | |
| 50 | // 处理图片上传 |
| 51 | StringBuilder imageUrlsBuilder = new StringBuilder(); |
| 52 | if (imageFiles != null && imageFiles.length > 0) { |
| 53 | for (int i = 0; i < imageFiles.length; i++) { |
| 54 | if (i > 0) { |
| 55 | imageUrlsBuilder.append(","); |
| 56 | } |
| 57 | try { |
| 58 | String imageUrl = saveImage(imageFiles[i]); |
| 59 | imageUrlsBuilder.append(imageUrl); |
| 60 | } catch (IOException e) { |
| 61 | throw new RuntimeException("Image upload failed: " + e.getMessage()); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // 将多个图片 URL 拼接 |
| 67 | userDynamic.setImageUrl(imageUrlsBuilder.toString()); |
| 68 | |
| 69 | // 保存数据库 |
| 70 | UserDynamic savedUserDynamic = userDynamicRepository.save(userDynamic); |
| 71 | |
| 72 | // 返回结果 |
| 73 | Map<String, Object> response = new HashMap<>(); |
| 74 | response.put("dynamicId", savedUserDynamic.getDynamicId()); |
| 75 | response.put("message", "动态创建成功"); |
| 76 | return response; |
| 77 | } |
| 78 | |
| 79 | // 保存图片并返回图片的 URL |
| 80 | public String saveImage(MultipartFile imageFile) throws IOException { |
| 81 | String fileName = imageFile.getOriginalFilename(); |
| 82 | Path path = Paths.get(IMAGE_DIR + fileName); |
| 83 | Files.createDirectories(path.getParent()); |
| 84 | Files.write(path, imageFile.getBytes()); |
| 85 | return "/images/" + fileName; |
| 86 | } |
| 87 | |
| 88 | // 删除动态 |
| 89 | public void deleteDynamic(Long dynamicId) { |
| 90 | if (userDynamicRepository.existsById(dynamicId)) { |
| 91 | userDynamicRepository.deleteById(dynamicId); |
| 92 | } else { |
| 93 | throw new RuntimeException("Dynamic with id " + dynamicId + " not found"); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | //好友动态评论 |
| 98 | public Map<String, Object> addComment(Long userId, Long dynamicId, String commentContent) { |
| 99 | Users user = userRepository.findById(userId) |
| 100 | .orElseThrow(() -> new RuntimeException("User not found")); |
| 101 | |
| 102 | UserDynamic dynamic = userDynamicRepository.findById(dynamicId) |
| 103 | .orElseThrow(() -> new RuntimeException("Dynamic not found")); |
| 104 | |
| 105 | DynamicComment comment = new DynamicComment(); |
| 106 | comment.setDynamicId(dynamicId); |
| 107 | comment.setUserId(userId); |
| 108 | comment.setCommentContent(commentContent); |
| 109 | comment.setCommentTime(new Date()); |
| 110 | |
| 111 | // 保存评论 |
| 112 | DynamicComment savedComment = dynamicCommentRepository.save(comment); |
| 113 | |
| 114 | // 返回成功消息 |
| 115 | Map<String, Object> response = new HashMap<>(); |
| 116 | response.put("comment_id", savedComment.getCommentId()); |
| 117 | response.put("message", "评论成功"); |
| 118 | return response; |
| 119 | } |
| 120 | |
| 121 | //返回某个好友的所有动态 |
| 122 | public Map<String, Object> getAllUserDynamics(Long userId) { |
| 123 | // 查找用户的所有动态 |
| 124 | List<UserDynamic> userDynamics = userDynamicRepository.findByUserId(userId); |
| 125 | Map<String, Object> postData = new LinkedHashMap<>(); |
| 126 | List<Map<String, Object>> dynamicList = new ArrayList<>(); |
| 127 | |
| 128 | // 遍历动态,获取点赞和评论 |
| 129 | for (UserDynamic dynamic : userDynamics) { |
| 130 | Map<String, Object> dynamicData = new LinkedHashMap<>(); |
| 131 | dynamicData.put("dynamic_id", dynamic.getDynamicId()); |
| 132 | dynamicData.put("user_id", dynamic.getUserId()); |
| 133 | |
| 134 | Users user = userRepository.findById(dynamic.getUserId()).orElse(null); |
| 135 | if (user != null) { |
| 136 | dynamicData.put("username", user.getUsername()); |
| 137 | dynamicData.put("avatar_url", user.getAvatarUrl()); |
| 138 | } else { |
| 139 | dynamicData.put("username", "Unknown"); |
| 140 | dynamicData.put("avatar_url", "http://example.com/default-avatar.jpg"); |
| 141 | } |
| 142 | |
| 143 | dynamicData.put("title", dynamic.getTitle()); |
| 144 | dynamicData.put("content", dynamic.getContent()); |
| 145 | dynamicData.put("images", dynamic.getImageUrl()); |
| 146 | dynamicData.put("time", dynamic.getTime()); |
| 147 | |
| 148 | // 获取点赞 |
| 149 | List<DynamicLikes> likes = dynamicLikesRepository.findByDynamicId(dynamic.getDynamicId()); |
| 150 | List<Map<String, Object>> likeList = new ArrayList<>(); |
| 151 | for (DynamicLikes like : likes) { |
| 152 | Map<String, Object> likeData = new HashMap<>(); |
| 153 | likeData.put("user_id", like.getUserId()); |
| 154 | |
| 155 | // 获取点赞用户的用户名 |
| 156 | Users likeUser = userRepository.findById(like.getUserId()).orElse(null); |
| 157 | if (likeUser != null) { |
| 158 | likeData.put("username", likeUser.getUsername()); |
| 159 | } else { |
| 160 | likeData.put("username", "Unknown"); |
| 161 | } |
| 162 | |
| 163 | likeList.add(likeData); |
| 164 | } |
| 165 | dynamicData.put("likes", likeList); |
| 166 | |
| 167 | // 获取评论 |
| 168 | List<DynamicComment> comments = dynamicCommentRepository.findByDynamicId(dynamic.getDynamicId()); |
| 169 | List<Map<String, Object>> commentList = new ArrayList<>(); |
| 170 | for (DynamicComment comment : comments) { |
| 171 | Map<String, Object> commentData = new HashMap<>(); |
| 172 | commentData.put("comment_id", comment.getCommentId()); |
| 173 | commentData.put("user_id", comment.getUserId()); |
| 174 | |
| 175 | // 获取评论用户的用户名 |
| 176 | Users commentUser = userRepository.findById(comment.getUserId()).orElse(null); |
| 177 | if (commentUser != null) { |
| 178 | commentData.put("username", commentUser.getUsername()); |
| 179 | } else { |
| 180 | commentData.put("username", "Unknown"); |
| 181 | } |
| 182 | |
| 183 | commentData.put("content", comment.getCommentContent()); |
| 184 | commentData.put("time", comment.getCommentTime()); |
| 185 | commentList.add(commentData); |
| 186 | } |
| 187 | dynamicData.put("comments", commentList); |
| 188 | |
| 189 | dynamicList.add(dynamicData); |
| 190 | } |
| 191 | |
| 192 | Map<String, Object> response = new HashMap<>(); |
| 193 | response.put("dynamic", dynamicList); |
| 194 | return response; |
| 195 | } |
| 196 | |
| 197 | //获取当前所有好友的id |
| 198 | public List<Long> getAllFriendIds(Long userId) { |
| 199 | List<FriendRelation> friendRelations = friendRelationRepository.findByUserId(userId); |
| 200 | List<Long> friendIds = new ArrayList<>(); |
| 201 | |
| 202 | for (FriendRelation relation : friendRelations) { |
| 203 | // 添加好友ID |
| 204 | if (!friendIds.contains(relation.getFriendId())) { |
| 205 | friendIds.add(relation.getFriendId()); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | return friendIds; |
| 210 | } |
| 211 | |
| 212 | public Map<String, Object> getAllUserAndFriendsDynamics(List<Long> userIds) { |
| 213 | List<Map<String, Object>> allDynamics = new ArrayList<>(); |
| 214 | |
| 215 | // 遍历所有用户获取他们的动态 |
| 216 | for (Long userId : userIds) { |
| 217 | Map<String, Object> userDynamics = getAllUserDynamics(userId); |
| 218 | if (userDynamics != null && !userDynamics.isEmpty()) { |
| 219 | allDynamics.addAll((List<Map<String, Object>>) userDynamics.get("dynamic")); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | // 返回所有动态的合集 |
| 224 | Map<String, Object> response = new HashMap<>(); |
| 225 | response.put("dynamic", allDynamics); |
| 226 | return response; |
| 227 | } |
| 228 | |
| 229 | //点赞动态 |
| 230 | public boolean likeDynamic(Long userId, Long dynamicId) { |
| 231 | // 检查该用户是否已经点赞过该动态 |
| 232 | DynamicLikes existingLike = dynamicLikesRepository.findByUserIdAndDynamicId(userId, dynamicId); |
| 233 | if (existingLike != null) { |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | // 新增点赞记录 |
| 238 | DynamicLikes newLike = new DynamicLikes(); |
| 239 | newLike.setUserId(userId); |
| 240 | newLike.setDynamicId(dynamicId); |
| 241 | newLike.setLikeTime(new java.util.Date()); |
| 242 | dynamicLikesRepository.save(newLike); |
| 243 | |
| 244 | // 更新动态表的点赞数 |
| 245 | UserDynamic dynamic = userDynamicRepository.findById(dynamicId).orElse(null); |
| 246 | if (dynamic != null) { |
| 247 | dynamic.setLikesCount(dynamic.getLikesCount() + 1); |
| 248 | userDynamicRepository.save(dynamic); |
| 249 | } |
| 250 | |
| 251 | return true; |
| 252 | } |
| 253 | |
| 254 | //取消点赞 |
| 255 | public boolean unlikeDynamic(Long userId, Long dynamicId) { |
| 256 | // 查找该用户的点赞记录 |
| 257 | DynamicLikes existingLike = dynamicLikesRepository.findByUserIdAndDynamicId(userId, dynamicId); |
| 258 | if (existingLike == null) { |
| 259 | return false; |
| 260 | } |
| 261 | |
| 262 | // 删除点赞记录 |
| 263 | dynamicLikesRepository.delete(existingLike); |
| 264 | |
| 265 | // 更新动态表的点赞数 |
| 266 | UserDynamic dynamic = userDynamicRepository.findById(dynamicId).orElse(null); |
| 267 | if (dynamic != null) { |
| 268 | dynamic.setLikesCount(dynamic.getLikesCount() - 1); |
| 269 | userDynamicRepository.save(dynamic); |
| 270 | } |
| 271 | |
| 272 | return true; |
| 273 | } |
| 274 | |
| 275 | } |