JinGe | fe5140c | 2025-06-06 20:07:42 +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 | // } |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 276 | package com.example.myproject.service; |
| 277 | |
| 278 | import com.example.myproject.entity.*; |
| 279 | import com.example.myproject.repository.*; |
| 280 | import org.springframework.beans.factory.annotation.Autowired; |
| 281 | import org.springframework.stereotype.Service; |
| 282 | import org.springframework.web.multipart.MultipartFile; |
| 283 | |
| 284 | import java.io.IOException; |
| 285 | import java.nio.file.Files; |
| 286 | import java.nio.file.Path; |
| 287 | import java.nio.file.Paths; |
| 288 | import java.util.*; |
| 289 | |
| 290 | @Service |
| 291 | public class DynamicService { |
| 292 | |
| 293 | @Autowired |
| 294 | private UserDynamicRepository userDynamicRepository; |
| 295 | |
| 296 | @Autowired |
| 297 | private UserRepository userRepository; |
| 298 | |
| 299 | @Autowired |
| 300 | private DynamicCommentRepository dynamicCommentRepository; |
| 301 | |
| 302 | @Autowired |
| 303 | private DynamicLikesRepository dynamicLikesRepository; |
| 304 | |
| 305 | @Autowired |
| 306 | private FriendRelationRepository friendRelationRepository; |
| 307 | |
| 308 | private static final String IMAGE_DIR = "uploads/post/"; |
| 309 | |
| 310 | // 创建好友动态 |
| 311 | public Map<String, Object> createDynamic(Long userId, String title, String content, MultipartFile[] imageFiles) { |
| 312 | // 查找用户信息 |
| 313 | Users user = userRepository.findById(userId) |
| 314 | .orElseThrow(() -> new RuntimeException("User not found")); |
| 315 | |
| 316 | // 创建新的动态 |
| 317 | UserDynamic userDynamic = new UserDynamic(); |
| 318 | userDynamic.setUserId(userId); |
| 319 | userDynamic.setTitle(title != null ? title : "默认标题"); |
| 320 | userDynamic.setContent(content); |
| 321 | userDynamic.setTime(new Date()); |
| 322 | userDynamic.setLikesCount(0); |
| 323 | userDynamic.setCommentsCount(0); |
| 324 | |
| 325 | // 处理图片上传 |
| 326 | StringBuilder imageUrlsBuilder = new StringBuilder(); |
| 327 | if (imageFiles != null && imageFiles.length > 0) { |
| 328 | for (int i = 0; i < imageFiles.length; i++) { |
| 329 | if (i > 0) { |
| 330 | imageUrlsBuilder.append(","); |
| 331 | } |
| 332 | try { |
| 333 | String imageUrl = saveImage(imageFiles[i]); |
| 334 | imageUrlsBuilder.append(imageUrl); |
| 335 | } catch (IOException e) { |
| 336 | throw new RuntimeException("Image upload failed: " + e.getMessage()); |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | // 将多个图片 URL 拼接 |
| 342 | userDynamic.setImageUrl(imageUrlsBuilder.toString()); |
| 343 | |
| 344 | // 保存数据库 |
| 345 | UserDynamic savedUserDynamic = userDynamicRepository.save(userDynamic); |
| 346 | |
| 347 | // 返回结果 |
| 348 | Map<String, Object> response = new HashMap<>(); |
| 349 | response.put("dynamicId", savedUserDynamic.getDynamicId()); |
| 350 | response.put("message", "动态创建成功"); |
| 351 | return response; |
| 352 | } |
| 353 | |
| 354 | // 保存图片并返回图片的 URL |
| 355 | public String saveImage(MultipartFile imageFile) throws IOException { |
| 356 | String fileName = imageFile.getOriginalFilename(); |
| 357 | Path path = Paths.get(IMAGE_DIR + fileName); |
| 358 | Files.createDirectories(path.getParent()); |
| 359 | Files.write(path, imageFile.getBytes()); |
| 360 | return "/images/" + fileName; |
| 361 | } |
| 362 | |
| 363 | // 删除动态 |
| 364 | public void deleteDynamic(Long dynamicId) { |
| 365 | if (userDynamicRepository.existsById(dynamicId)) { |
| 366 | userDynamicRepository.deleteById(dynamicId); |
| 367 | } else { |
| 368 | throw new RuntimeException("Dynamic with id " + dynamicId + " not found"); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | //好友动态评论 |
JinGe | fe5140c | 2025-06-06 20:07:42 +0800 | [diff] [blame] | 373 | public Map<String, Object> addComment(Long userId, Long dynamicId, String commentContent, Long parentCommentId) { |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 374 | Users user = userRepository.findById(userId) |
| 375 | .orElseThrow(() -> new RuntimeException("User not found")); |
| 376 | |
| 377 | UserDynamic dynamic = userDynamicRepository.findById(dynamicId) |
| 378 | .orElseThrow(() -> new RuntimeException("Dynamic not found")); |
| 379 | |
| 380 | DynamicComment comment = new DynamicComment(); |
| 381 | comment.setDynamicId(dynamicId); |
| 382 | comment.setUserId(userId); |
| 383 | comment.setCommentContent(commentContent); |
| 384 | comment.setCommentTime(new Date()); |
JinGe | fe5140c | 2025-06-06 20:07:42 +0800 | [diff] [blame] | 385 | comment.setParentCommentId(parentCommentId); // 设置父评论ID |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 386 | |
| 387 | // 保存评论 |
| 388 | DynamicComment savedComment = dynamicCommentRepository.save(comment); |
| 389 | |
| 390 | // 返回成功消息 |
| 391 | Map<String, Object> response = new HashMap<>(); |
| 392 | response.put("comment_id", savedComment.getCommentId()); |
JinGe | fe5140c | 2025-06-06 20:07:42 +0800 | [diff] [blame] | 393 | response.put("parent_comment_id", savedComment.getParentCommentId()); |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 394 | response.put("message", "评论成功"); |
| 395 | return response; |
| 396 | } |
| 397 | |
JinGe | fe5140c | 2025-06-06 20:07:42 +0800 | [diff] [blame] | 398 | |
22301138 | 5e9c35a | 2025-06-04 15:52:45 +0800 | [diff] [blame] | 399 | //返回某个好友的所有动态 |
| 400 | public Map<String, Object> getAllUserDynamics(Long userId) { |
| 401 | // 查找用户的所有动态 |
| 402 | List<UserDynamic> userDynamics = userDynamicRepository.findByUserId(userId); |
| 403 | Map<String, Object> postData = new LinkedHashMap<>(); |
| 404 | List<Map<String, Object>> dynamicList = new ArrayList<>(); |
| 405 | |
| 406 | // 遍历动态,获取点赞和评论 |
| 407 | for (UserDynamic dynamic : userDynamics) { |
| 408 | Map<String, Object> dynamicData = new LinkedHashMap<>(); |
| 409 | dynamicData.put("dynamic_id", dynamic.getDynamicId()); |
| 410 | dynamicData.put("user_id", dynamic.getUserId()); |
| 411 | |
| 412 | Users user = userRepository.findById(dynamic.getUserId()).orElse(null); |
| 413 | if (user != null) { |
| 414 | dynamicData.put("username", user.getUsername()); |
| 415 | dynamicData.put("avatar_url", user.getAvatarUrl()); |
| 416 | } else { |
| 417 | dynamicData.put("username", "Unknown"); |
| 418 | dynamicData.put("avatar_url", "http://example.com/default-avatar.jpg"); |
| 419 | } |
| 420 | |
| 421 | dynamicData.put("title", dynamic.getTitle()); |
| 422 | dynamicData.put("content", dynamic.getContent()); |
| 423 | dynamicData.put("images", dynamic.getImageUrl()); |
| 424 | dynamicData.put("time", dynamic.getTime()); |
| 425 | |
| 426 | // 获取点赞 |
| 427 | List<DynamicLikes> likes = dynamicLikesRepository.findByDynamicId(dynamic.getDynamicId()); |
| 428 | List<Map<String, Object>> likeList = new ArrayList<>(); |
| 429 | for (DynamicLikes like : likes) { |
| 430 | Map<String, Object> likeData = new HashMap<>(); |
| 431 | likeData.put("user_id", like.getUserId()); |
| 432 | |
| 433 | // 获取点赞用户的用户名 |
| 434 | Users likeUser = userRepository.findById(like.getUserId()).orElse(null); |
| 435 | if (likeUser != null) { |
| 436 | likeData.put("username", likeUser.getUsername()); |
| 437 | } else { |
| 438 | likeData.put("username", "Unknown"); |
| 439 | } |
| 440 | |
| 441 | likeList.add(likeData); |
| 442 | } |
| 443 | dynamicData.put("likes", likeList); |
| 444 | |
| 445 | // 获取评论 |
| 446 | List<DynamicComment> comments = dynamicCommentRepository.findByDynamicId(dynamic.getDynamicId()); |
| 447 | List<Map<String, Object>> commentList = new ArrayList<>(); |
| 448 | for (DynamicComment comment : comments) { |
| 449 | Map<String, Object> commentData = new HashMap<>(); |
| 450 | commentData.put("comment_id", comment.getCommentId()); |
| 451 | commentData.put("user_id", comment.getUserId()); |
| 452 | |
| 453 | // 获取评论用户的用户名 |
| 454 | Users commentUser = userRepository.findById(comment.getUserId()).orElse(null); |
| 455 | if (commentUser != null) { |
| 456 | commentData.put("username", commentUser.getUsername()); |
| 457 | } else { |
| 458 | commentData.put("username", "Unknown"); |
| 459 | } |
| 460 | |
| 461 | commentData.put("content", comment.getCommentContent()); |
| 462 | commentData.put("time", comment.getCommentTime()); |
| 463 | commentList.add(commentData); |
| 464 | } |
| 465 | dynamicData.put("comments", commentList); |
| 466 | |
| 467 | dynamicList.add(dynamicData); |
| 468 | } |
| 469 | |
| 470 | Map<String, Object> response = new HashMap<>(); |
| 471 | response.put("dynamic", dynamicList); |
| 472 | return response; |
| 473 | } |
| 474 | |
| 475 | //获取当前所有好友的id |
| 476 | public List<Long> getAllFriendIds(Long userId) { |
| 477 | List<FriendRelation> friendRelations = friendRelationRepository.findByUserId(userId); |
| 478 | List<Long> friendIds = new ArrayList<>(); |
| 479 | |
| 480 | for (FriendRelation relation : friendRelations) { |
| 481 | // 添加好友ID |
| 482 | if (!friendIds.contains(relation.getFriendId())) { |
| 483 | friendIds.add(relation.getFriendId()); |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | return friendIds; |
| 488 | } |
| 489 | |
| 490 | public Map<String, Object> getAllUserAndFriendsDynamics(List<Long> userIds) { |
| 491 | List<Map<String, Object>> allDynamics = new ArrayList<>(); |
| 492 | |
| 493 | // 遍历所有用户获取他们的动态 |
| 494 | for (Long userId : userIds) { |
| 495 | Map<String, Object> userDynamics = getAllUserDynamics(userId); |
| 496 | if (userDynamics != null && !userDynamics.isEmpty()) { |
| 497 | allDynamics.addAll((List<Map<String, Object>>) userDynamics.get("dynamic")); |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | // 返回所有动态的合集 |
| 502 | Map<String, Object> response = new HashMap<>(); |
| 503 | response.put("dynamic", allDynamics); |
| 504 | return response; |
| 505 | } |
| 506 | |
| 507 | //点赞动态 |
| 508 | public boolean likeDynamic(Long userId, Long dynamicId) { |
| 509 | // 检查该用户是否已经点赞过该动态 |
| 510 | DynamicLikes existingLike = dynamicLikesRepository.findByUserIdAndDynamicId(userId, dynamicId); |
| 511 | if (existingLike != null) { |
| 512 | return false; |
| 513 | } |
| 514 | |
| 515 | // 新增点赞记录 |
| 516 | DynamicLikes newLike = new DynamicLikes(); |
| 517 | newLike.setUserId(userId); |
| 518 | newLike.setDynamicId(dynamicId); |
| 519 | newLike.setLikeTime(new java.util.Date()); |
| 520 | dynamicLikesRepository.save(newLike); |
| 521 | |
| 522 | // 更新动态表的点赞数 |
| 523 | UserDynamic dynamic = userDynamicRepository.findById(dynamicId).orElse(null); |
| 524 | if (dynamic != null) { |
| 525 | dynamic.setLikesCount(dynamic.getLikesCount() + 1); |
| 526 | userDynamicRepository.save(dynamic); |
| 527 | } |
| 528 | |
| 529 | return true; |
| 530 | } |
| 531 | |
| 532 | //取消点赞 |
| 533 | public boolean unlikeDynamic(Long userId, Long dynamicId) { |
| 534 | // 查找该用户的点赞记录 |
| 535 | DynamicLikes existingLike = dynamicLikesRepository.findByUserIdAndDynamicId(userId, dynamicId); |
| 536 | if (existingLike == null) { |
| 537 | return false; |
| 538 | } |
| 539 | |
| 540 | // 删除点赞记录 |
| 541 | dynamicLikesRepository.delete(existingLike); |
| 542 | |
| 543 | // 更新动态表的点赞数 |
| 544 | UserDynamic dynamic = userDynamicRepository.findById(dynamicId).orElse(null); |
| 545 | if (dynamic != null) { |
| 546 | dynamic.setLikesCount(dynamic.getLikesCount() - 1); |
| 547 | userDynamicRepository.save(dynamic); |
| 548 | } |
| 549 | |
| 550 | return true; |
| 551 | } |
| 552 | |
| 553 | } |