|
@@ -430,6 +430,7 @@ public class UserMomentsServiceImpl extends ServiceImpl<UserMomentsMapper, UserM
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public Result allListPage(Long userId, MonmentDto monmentDto) {
|
|
public Result allListPage(Long userId, MonmentDto monmentDto) {
|
|
try {
|
|
try {
|
|
@@ -457,7 +458,7 @@ public class UserMomentsServiceImpl extends ServiceImpl<UserMomentsMapper, UserM
|
|
.map(UserRelationships::getFriendId)
|
|
.map(UserRelationships::getFriendId)
|
|
.collect(Collectors.toList());
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
- // 4. 合并查询当前用户和好友的动态(真正的分页实现)
|
|
|
|
|
|
+ // 4. 合并查询当前用户和好友的动态
|
|
Page<UserMoments> allMomentsPage = new Page<>(monmentDto.getCurrentPage(), monmentDto.getPageSize());
|
|
Page<UserMoments> allMomentsPage = new Page<>(monmentDto.getCurrentPage(), monmentDto.getPageSize());
|
|
QueryWrapper<UserMoments> allMomentQuery = new QueryWrapper<>();
|
|
QueryWrapper<UserMoments> allMomentQuery = new QueryWrapper<>();
|
|
|
|
|
|
@@ -485,61 +486,74 @@ public class UserMomentsServiceImpl extends ServiceImpl<UserMomentsMapper, UserM
|
|
userMomentsMapper.selectPage(allMomentsPage, allMomentQuery);
|
|
userMomentsMapper.selectPage(allMomentsPage, allMomentQuery);
|
|
List<UserMoments> allUserMoments = allMomentsPage.getRecords();
|
|
List<UserMoments> allUserMoments = allMomentsPage.getRecords();
|
|
|
|
|
|
- // 5. 提取所有动态相关用户ID
|
|
|
|
|
|
+ // 5. 提取所有动态相关用户ID(原有代码)
|
|
Set<Long> allUserIds = allUserMoments.stream()
|
|
Set<Long> allUserIds = allUserMoments.stream()
|
|
.map(UserMoments::getUserId)
|
|
.map(UserMoments::getUserId)
|
|
.collect(Collectors.toSet());
|
|
.collect(Collectors.toSet());
|
|
|
|
|
|
- // 6. 批量查询所有动态相关用户信息
|
|
|
|
|
|
+ // 6. 批量查询所有动态相关用户信息(原有代码)
|
|
QueryWrapper<UserLogin> userQuery = new QueryWrapper<>();
|
|
QueryWrapper<UserLogin> userQuery = new QueryWrapper<>();
|
|
userQuery.in("id", allUserIds);
|
|
userQuery.in("id", allUserIds);
|
|
List<UserLogin> users = userLoginMapper.selectList(userQuery);
|
|
List<UserLogin> users = userLoginMapper.selectList(userQuery);
|
|
Map<Long, UserLogin> userMap = users.stream()
|
|
Map<Long, UserLogin> userMap = users.stream()
|
|
.collect(Collectors.toMap(UserLogin::getId, user -> user));
|
|
.collect(Collectors.toMap(UserLogin::getId, user -> user));
|
|
|
|
|
|
- // 7. 处理动态数据,添加用户信息
|
|
|
|
- List<Map<String, Object>> momentList = allUserMoments.stream()
|
|
|
|
- .map(moment -> wrapMomentWithUser(moment, userMap))
|
|
|
|
- .collect(Collectors.toList());
|
|
|
|
-
|
|
|
|
- // 8. 提取所有动态ID,用于查询点赞和评论
|
|
|
|
|
|
+ // 7. 提取所有动态ID
|
|
List<Long> allMomentIds = allUserMoments.stream()
|
|
List<Long> allMomentIds = allUserMoments.stream()
|
|
.map(UserMoments::getMomentId)
|
|
.map(UserMoments::getMomentId)
|
|
.collect(Collectors.toList());
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
- if (allMomentIds.isEmpty()) {
|
|
|
|
- // 没有动态时直接返回
|
|
|
|
- Map<String, Object> resultMap = new HashMap<>();
|
|
|
|
- resultMap.put("userInfo", userLogin);
|
|
|
|
- resultMap.put("moments", momentList);
|
|
|
|
- resultMap.put("allLikesAndComments", new HashMap<>());
|
|
|
|
- resultMap.put("total", allMomentsPage.getTotal());
|
|
|
|
- resultMap.put("currentPage", allMomentsPage.getCurrent());
|
|
|
|
- resultMap.put("pageSize", allMomentsPage.getSize());
|
|
|
|
- resultMap.put("totalPages", allMomentsPage.getPages());
|
|
|
|
- return Result.OK(resultMap, "查询成功");
|
|
|
|
|
|
+ // 8. 批量查询当前用户对所有动态的点赞状态(性能优化点)
|
|
|
|
+ Map<Long, Boolean> likedStatusMap = new HashMap<>();
|
|
|
|
+ if (!allMomentIds.isEmpty()) {
|
|
|
|
+ QueryWrapper<MomentLikes> batchLikeQuery = new QueryWrapper<>();
|
|
|
|
+ batchLikeQuery.in("moment_id", allMomentIds)
|
|
|
|
+ .eq("user_id", userId);
|
|
|
|
+ List<MomentLikes> likedMoments = momentLikesMapper.selectList(batchLikeQuery);
|
|
|
|
+
|
|
|
|
+ // 将结果转换为 Map<momentId, isLiked>
|
|
|
|
+ for (Long momentId : allMomentIds) {
|
|
|
|
+ likedStatusMap.put(momentId, likedMoments.stream()
|
|
|
|
+ .anyMatch(like -> like.getMomentId().equals(momentId)));
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
- // 9. 批量查询所有动态的点赞和评论
|
|
|
|
- Map<Long, Object> allLikesAndComments = new HashMap<>();
|
|
|
|
- for (Long momentId : allMomentIds) {
|
|
|
|
- Map<String, Object> momentInfo = new HashMap<>();
|
|
|
|
|
|
+ // 9. 处理动态数据,添加用户信息和点赞状态(优化后)
|
|
|
|
+ List<Map<String, Object>> momentList = allUserMoments.stream()
|
|
|
|
+ .map(moment -> {
|
|
|
|
+ Map<String, Object> momentMap = wrapMomentWithUser(moment, userMap);
|
|
|
|
+ // 从批量查询结果中获取点赞状态,避免重复查询
|
|
|
|
+ momentMap.put("isLiked", likedStatusMap.getOrDefault(moment.getMomentId(), false));
|
|
|
|
+ return momentMap;
|
|
|
|
+ })
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
|
|
- // 查询点赞
|
|
|
|
|
|
+ // 10. 处理点赞和评论(原有代码,略作优化)
|
|
|
|
+ Map<Long, Object> allLikesAndComments = new HashMap<>();
|
|
|
|
+ if (!allMomentIds.isEmpty()) {
|
|
|
|
+ // 批量查询所有动态的点赞
|
|
|
|
+ Map<Long, List<MomentLikes>> likesMap = new HashMap<>();
|
|
QueryWrapper<MomentLikes> likeQuery = new QueryWrapper<>();
|
|
QueryWrapper<MomentLikes> likeQuery = new QueryWrapper<>();
|
|
- likeQuery.eq("moment_id", momentId);
|
|
|
|
- List<MomentLikes> likes = momentLikesMapper.selectList(likeQuery);
|
|
|
|
|
|
+ likeQuery.in("moment_id", allMomentIds);
|
|
|
|
+ List<MomentLikes> allLikes = momentLikesMapper.selectList(likeQuery);
|
|
|
|
+ for (MomentLikes like : allLikes) {
|
|
|
|
+ likesMap.computeIfAbsent(like.getMomentId(), k -> new ArrayList<>()).add(like);
|
|
|
|
+ }
|
|
|
|
|
|
- // 查询评论
|
|
|
|
|
|
+ // 批量查询所有动态的评论
|
|
|
|
+ Map<Long, List<MomentComments>> commentsMap = new HashMap<>();
|
|
QueryWrapper<MomentComments> commentQuery = new QueryWrapper<>();
|
|
QueryWrapper<MomentComments> commentQuery = new QueryWrapper<>();
|
|
- commentQuery.eq("moment_id", momentId)
|
|
|
|
|
|
+ commentQuery.in("moment_id", allMomentIds)
|
|
.orderByDesc("created_at");
|
|
.orderByDesc("created_at");
|
|
- List<MomentComments> comments = momentCommentsMapper.selectList(commentQuery);
|
|
|
|
|
|
+ List<MomentComments> allComments = momentCommentsMapper.selectList(commentQuery);
|
|
|
|
+ for (MomentComments comment : allComments) {
|
|
|
|
+ commentsMap.computeIfAbsent(comment.getMomentId(), k -> new ArrayList<>()).add(comment);
|
|
|
|
+ }
|
|
|
|
|
|
- // 提取点赞和评论涉及的用户ID
|
|
|
|
|
|
+ // 提取所有点赞和评论涉及的用户ID
|
|
Set<Long> likeCommentUserIds = new HashSet<>();
|
|
Set<Long> likeCommentUserIds = new HashSet<>();
|
|
- likes.forEach(like -> likeCommentUserIds.add(like.getUserId()));
|
|
|
|
- comments.forEach(comment -> {
|
|
|
|
|
|
+ allLikes.forEach(like -> likeCommentUserIds.add(like.getUserId()));
|
|
|
|
+ allComments.forEach(comment -> {
|
|
likeCommentUserIds.add(comment.getUserId());
|
|
likeCommentUserIds.add(comment.getUserId());
|
|
if (comment.getReplyToUser() != null) {
|
|
if (comment.getReplyToUser() != null) {
|
|
likeCommentUserIds.add(comment.getReplyToUser());
|
|
likeCommentUserIds.add(comment.getReplyToUser());
|
|
@@ -554,71 +568,77 @@ public class UserMomentsServiceImpl extends ServiceImpl<UserMomentsMapper, UserM
|
|
Map<Long, UserLogin> likeCommentUserMap = likeCommentUsers.stream()
|
|
Map<Long, UserLogin> likeCommentUserMap = likeCommentUsers.stream()
|
|
.collect(Collectors.toMap(UserLogin::getId, user -> user));
|
|
.collect(Collectors.toMap(UserLogin::getId, user -> user));
|
|
|
|
|
|
- // 处理点赞数据,统一使用字符串格式的时间
|
|
|
|
- List<Map<String, Object>> likeList = likes.stream()
|
|
|
|
- .map(like -> {
|
|
|
|
- Map<String, Object> likeInfo = new HashMap<>();
|
|
|
|
- likeInfo.put("id", like.getLikeId());
|
|
|
|
- likeInfo.put("userId", like.getUserId());
|
|
|
|
-
|
|
|
|
- if (like.getCreatedAt() != null) {
|
|
|
|
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
- likeInfo.put("createTime", sdf.format(like.getCreatedAt()));
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // 手动构建用户信息,只包含必要字段
|
|
|
|
- UserLogin user = likeCommentUserMap.get(like.getUserId());
|
|
|
|
- if (user != null) {
|
|
|
|
- Map<String, Object> userInfo = new HashMap<>();
|
|
|
|
- userInfo.put("id", user.getId());
|
|
|
|
- userInfo.put("nickName", user.getNickName());
|
|
|
|
- userInfo.put("avatar", user.getAvatar());
|
|
|
|
- userInfo.put("username", user.getUserUsername());
|
|
|
|
- likeInfo.put("userInfo", userInfo);
|
|
|
|
- }
|
|
|
|
- return likeInfo;
|
|
|
|
- })
|
|
|
|
- .collect(Collectors.toList());
|
|
|
|
-
|
|
|
|
- // 处理评论数据,统一使用字符串格式的时间
|
|
|
|
- List<Map<String, Object>> commentList = comments.stream()
|
|
|
|
- .map(comment -> {
|
|
|
|
- Map<String, Object> commentInfo = new HashMap<>();
|
|
|
|
- commentInfo.put("id", comment.getCommentId());
|
|
|
|
- commentInfo.put("userId", comment.getUserId());
|
|
|
|
- commentInfo.put("content", comment.getContent());
|
|
|
|
-
|
|
|
|
- // 统一转换为字符串格式
|
|
|
|
- if (comment.getCreatedAt() != null) {
|
|
|
|
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
- commentInfo.put("createdAt", sdf.format(comment.getCreatedAt()));
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- commentInfo.put("replyTo", comment.getReplyTo());
|
|
|
|
- commentInfo.put("replyToUser", comment.getReplyToUser());
|
|
|
|
-
|
|
|
|
- UserLogin user = likeCommentUserMap.get(comment.getUserId());
|
|
|
|
- if (user != null) {
|
|
|
|
- Map<String, Object> userInfo = new HashMap<>();
|
|
|
|
- userInfo.put("id", user.getId());
|
|
|
|
- userInfo.put("username", user.getUserUsername());
|
|
|
|
- userInfo.put("avatar", user.getAvatar());
|
|
|
|
- commentInfo.put("userInfo", userInfo);
|
|
|
|
- }
|
|
|
|
- return commentInfo;
|
|
|
|
- })
|
|
|
|
- .collect(Collectors.toList());
|
|
|
|
-
|
|
|
|
- // 封装点赞评论信息
|
|
|
|
- momentInfo.put("likeList", likeList);
|
|
|
|
- momentInfo.put("commentList", commentList);
|
|
|
|
- momentInfo.put("likeCount", likeList.size());
|
|
|
|
- momentInfo.put("commentCount", commentList.size());
|
|
|
|
-
|
|
|
|
- allLikesAndComments.put(momentId, momentInfo);
|
|
|
|
|
|
+ // 处理每个动态的点赞和评论信息
|
|
|
|
+ for (Long momentId : allMomentIds) {
|
|
|
|
+ Map<String, Object> momentInfo = new HashMap<>();
|
|
|
|
+
|
|
|
|
+ // 获取当前动态的点赞列表
|
|
|
|
+ List<MomentLikes> likes = likesMap.getOrDefault(momentId, Collections.emptyList());
|
|
|
|
+ List<Map<String, Object>> likeList = likes.stream()
|
|
|
|
+ .map(like -> {
|
|
|
|
+ Map<String, Object> likeInfo = new HashMap<>();
|
|
|
|
+ likeInfo.put("id", like.getLikeId());
|
|
|
|
+ likeInfo.put("userId", like.getUserId());
|
|
|
|
+
|
|
|
|
+ if (like.getCreatedAt() != null) {
|
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
+ likeInfo.put("createTime", sdf.format(like.getCreatedAt()));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 手动构建用户信息
|
|
|
|
+ UserLogin user = likeCommentUserMap.get(like.getUserId());
|
|
|
|
+ if (user != null) {
|
|
|
|
+ Map<String, Object> userInfo = new HashMap<>();
|
|
|
|
+ userInfo.put("id", user.getId().toString());
|
|
|
|
+ userInfo.put("nickName", user.getNickName());
|
|
|
|
+ userInfo.put("avatar", user.getAvatar());
|
|
|
|
+ userInfo.put("username", user.getUserUsername());
|
|
|
|
+ likeInfo.put("userInfo", userInfo);
|
|
|
|
+ }
|
|
|
|
+ return likeInfo;
|
|
|
|
+ })
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
+
|
|
|
|
+ // 获取当前动态的评论列表
|
|
|
|
+ List<MomentComments> comments = commentsMap.getOrDefault(momentId, Collections.emptyList());
|
|
|
|
+ List<Map<String, Object>> commentList = comments.stream()
|
|
|
|
+ .map(comment -> {
|
|
|
|
+ Map<String, Object> commentInfo = new HashMap<>();
|
|
|
|
+ commentInfo.put("id", comment.getCommentId());
|
|
|
|
+ commentInfo.put("userId", comment.getUserId());
|
|
|
|
+ commentInfo.put("content", comment.getContent());
|
|
|
|
+
|
|
|
|
+ if (comment.getCreatedAt() != null) {
|
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
+ commentInfo.put("createdAt", sdf.format(comment.getCreatedAt()));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ commentInfo.put("replyTo", comment.getReplyTo());
|
|
|
|
+ commentInfo.put("replyToUser", comment.getReplyToUser());
|
|
|
|
+
|
|
|
|
+ UserLogin user = likeCommentUserMap.get(comment.getUserId());
|
|
|
|
+ if (user != null) {
|
|
|
|
+ Map<String, Object> userInfo = new HashMap<>();
|
|
|
|
+ userInfo.put("id", user.getId().toString());
|
|
|
|
+ userInfo.put("username", user.getUserUsername());
|
|
|
|
+ userInfo.put("avatar", user.getAvatar());
|
|
|
|
+ commentInfo.put("userInfo", userInfo);
|
|
|
|
+ }
|
|
|
|
+ return commentInfo;
|
|
|
|
+ })
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
+
|
|
|
|
+ // 封装点赞评论信息
|
|
|
|
+ momentInfo.put("likeList", likeList);
|
|
|
|
+ momentInfo.put("commentList", commentList);
|
|
|
|
+ momentInfo.put("likeCount", likeList.size());
|
|
|
|
+ momentInfo.put("commentCount", commentList.size());
|
|
|
|
+
|
|
|
|
+ allLikesAndComments.put(momentId, momentInfo);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
- // 10. 封装最终结果
|
|
|
|
|
|
+ // 11. 封装最终结果(原有代码)
|
|
Map<String, Object> resultMap = new HashMap<>();
|
|
Map<String, Object> resultMap = new HashMap<>();
|
|
resultMap.put("userInfo", userLogin);
|
|
resultMap.put("userInfo", userLogin);
|
|
resultMap.put("moments", momentList);
|
|
resultMap.put("moments", momentList);
|