|
@@ -4,6 +4,7 @@ import com.aliyun.oss.OSSClient;
|
|
import com.aliyun.oss.model.ObjectMetadata;
|
|
import com.aliyun.oss.model.ObjectMetadata;
|
|
import com.aliyun.oss.model.PutObjectRequest;
|
|
import com.aliyun.oss.model.PutObjectRequest;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.zhentao.moment.domain.MomentComments;
|
|
import com.zhentao.moment.domain.MomentComments;
|
|
import com.zhentao.moment.domain.MomentLikes;
|
|
import com.zhentao.moment.domain.MomentLikes;
|
|
@@ -35,8 +36,11 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
import java.util.*;
|
|
import java.util.*;
|
|
|
|
+import java.util.concurrent.CompletableFuture;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
+import java.util.stream.Stream;
|
|
|
|
|
|
/**
|
|
/**
|
|
* @author 86159
|
|
* @author 86159
|
|
@@ -53,8 +57,8 @@ public class UserMomentsServiceImpl extends ServiceImpl<UserMomentsMapper, UserM
|
|
private UserLoginMapper userLoginMapper;
|
|
private UserLoginMapper userLoginMapper;
|
|
@Autowired
|
|
@Autowired
|
|
private MomentLikesMapper momentLikesMapper;
|
|
private MomentLikesMapper momentLikesMapper;
|
|
-// @Autowired
|
|
|
|
-// private OSSClient ossClient;
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ private OssUtil ossUtil;
|
|
@Autowired
|
|
@Autowired
|
|
private OssConfig ossConfig;
|
|
private OssConfig ossConfig;
|
|
@Autowired
|
|
@Autowired
|
|
@@ -291,7 +295,7 @@ public class UserMomentsServiceImpl extends ServiceImpl<UserMomentsMapper, UserM
|
|
// 点赞朋友圈
|
|
// 点赞朋友圈
|
|
@Override
|
|
@Override
|
|
public Result likeMonment(Long uid, MonmentDto monmentDto) {
|
|
public Result likeMonment(Long uid, MonmentDto monmentDto) {
|
|
-
|
|
|
|
|
|
+ System.err.println("赞"+monmentDto.getMomentId());
|
|
if (monmentDto.getMomentId()==null){
|
|
if (monmentDto.getMomentId()==null){
|
|
return Result.error(400,"请选择要点赞的朋友圈");
|
|
return Result.error(400,"请选择要点赞的朋友圈");
|
|
}
|
|
}
|
|
@@ -317,7 +321,7 @@ public class UserMomentsServiceImpl extends ServiceImpl<UserMomentsMapper, UserM
|
|
// 评论朋友圈
|
|
// 评论朋友圈
|
|
@Override
|
|
@Override
|
|
public Result commentsMonment(Long userId, CommentsDto commentsDto) {
|
|
public Result commentsMonment(Long userId, CommentsDto commentsDto) {
|
|
- System.err.println(commentsDto.getMomentId());
|
|
|
|
|
|
+ System.err.println("轮"+commentsDto.getMomentId());
|
|
MomentComments momentComments = new MomentComments();
|
|
MomentComments momentComments = new MomentComments();
|
|
momentComments.setUserId(userId);
|
|
momentComments.setUserId(userId);
|
|
momentComments.setCommentId(SnowflakeUtil.nextId());
|
|
momentComments.setCommentId(SnowflakeUtil.nextId());
|
|
@@ -416,12 +420,255 @@ public class UserMomentsServiceImpl extends ServiceImpl<UserMomentsMapper, UserM
|
|
}
|
|
}
|
|
// 更改朋友圈背景图
|
|
// 更改朋友圈背景图
|
|
@Override
|
|
@Override
|
|
- public Result updateBackground(Long valueOf, MultipartFile file) {
|
|
|
|
|
|
+ public Result updateBackground(Long userId, MultipartFile file) throws IOException {
|
|
|
|
+ String ossUrl = ossUtil.uploadFile(file);
|
|
|
|
+ UserLogin userLogin=new UserLogin();
|
|
|
|
+ userLogin.setId(userId);
|
|
|
|
+ userLogin.setLabelList(ossUrl);
|
|
|
|
+ userLoginMapper.updateById(userLogin);
|
|
|
|
+ return Result.OK(ossUrl,"修改成功");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public Result allListPage(Long userId, MonmentDto monmentDto) {
|
|
|
|
+ try {
|
|
|
|
+ // 1. 校验用户ID
|
|
|
|
+ if (userId == null) {
|
|
|
|
+ return Result.error(400, "用户ID不能为空");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 2. 查询个人信息
|
|
|
|
+ UserLogin userLogin = userLoginMapper.selectById(userId);
|
|
|
|
+ if (userLogin == null) {
|
|
|
|
+ return Result.error(404, "用户信息不存在");
|
|
|
|
+ }
|
|
|
|
|
|
- return null;
|
|
|
|
|
|
+ // 3. 查询好友关系
|
|
|
|
+ QueryWrapper<UserRelationships> friendRelQuery = new QueryWrapper<>();
|
|
|
|
+ friendRelQuery.eq("user_id", userId)
|
|
|
|
+ .eq("status", 1)
|
|
|
|
+ .eq("is_blacklist", 0)
|
|
|
|
+ .eq("is_del", 0)
|
|
|
|
+ .eq("is_moments", 0)
|
|
|
|
+ .orderByDesc("created_at");
|
|
|
|
+ List<UserRelationships> friendRelationships = userRelationshipsMapper.selectList(friendRelQuery);
|
|
|
|
+ List<Long> friendIds = friendRelationships.stream()
|
|
|
|
+ .map(UserRelationships::getFriendId)
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
+
|
|
|
|
+ // 4. 合并查询当前用户和好友的动态(真正的分页实现)
|
|
|
|
+ Page<UserMoments> allMomentsPage = new Page<>(monmentDto.getCurrentPage(), monmentDto.getPageSize());
|
|
|
|
+ QueryWrapper<UserMoments> allMomentQuery = new QueryWrapper<>();
|
|
|
|
+
|
|
|
|
+ // 如果有好友,查询自己和好友的动态;否则只查询自己的
|
|
|
|
+ if (!friendIds.isEmpty()) {
|
|
|
|
+ allMomentQuery.in("user_id", Stream.concat(
|
|
|
|
+ Stream.of(userId),
|
|
|
|
+ friendIds.stream()
|
|
|
|
+ ).collect(Collectors.toList()));
|
|
|
|
+ } else {
|
|
|
|
+ allMomentQuery.eq("user_id", userId);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 排除仅自己可见的动态(除了自己发布的)
|
|
|
|
+ allMomentQuery.and(wrapper ->
|
|
|
|
+ wrapper.eq("user_id", userId)
|
|
|
|
+ .or()
|
|
|
|
+ .ne("visibility", 2)
|
|
|
|
+ );
|
|
|
|
+
|
|
|
|
+ // 按创建时间降序排列
|
|
|
|
+ allMomentQuery.orderByDesc("created_at");
|
|
|
|
+
|
|
|
|
+ // 执行分页查询
|
|
|
|
+ userMomentsMapper.selectPage(allMomentsPage, allMomentQuery);
|
|
|
|
+ List<UserMoments> allUserMoments = allMomentsPage.getRecords();
|
|
|
|
+
|
|
|
|
+ // 5. 提取所有动态相关用户ID
|
|
|
|
+ Set<Long> allUserIds = allUserMoments.stream()
|
|
|
|
+ .map(UserMoments::getUserId)
|
|
|
|
+ .collect(Collectors.toSet());
|
|
|
|
+
|
|
|
|
+ // 6. 批量查询所有动态相关用户信息
|
|
|
|
+ QueryWrapper<UserLogin> userQuery = new QueryWrapper<>();
|
|
|
|
+ userQuery.in("id", allUserIds);
|
|
|
|
+ List<UserLogin> users = userLoginMapper.selectList(userQuery);
|
|
|
|
+ Map<Long, UserLogin> userMap = users.stream()
|
|
|
|
+ .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,用于查询点赞和评论
|
|
|
|
+ List<Long> allMomentIds = allUserMoments.stream()
|
|
|
|
+ .map(UserMoments::getMomentId)
|
|
|
|
+ .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, "查询成功");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 9. 批量查询所有动态的点赞和评论
|
|
|
|
+ Map<Long, Object> allLikesAndComments = new HashMap<>();
|
|
|
|
+ for (Long momentId : allMomentIds) {
|
|
|
|
+ Map<String, Object> momentInfo = new HashMap<>();
|
|
|
|
+
|
|
|
|
+ // 查询点赞
|
|
|
|
+ QueryWrapper<MomentLikes> likeQuery = new QueryWrapper<>();
|
|
|
|
+ likeQuery.eq("moment_id", momentId);
|
|
|
|
+ List<MomentLikes> likes = momentLikesMapper.selectList(likeQuery);
|
|
|
|
+
|
|
|
|
+ // 查询评论
|
|
|
|
+ QueryWrapper<MomentComments> commentQuery = new QueryWrapper<>();
|
|
|
|
+ commentQuery.eq("moment_id", momentId)
|
|
|
|
+ .orderByDesc("created_at");
|
|
|
|
+ List<MomentComments> comments = momentCommentsMapper.selectList(commentQuery);
|
|
|
|
+
|
|
|
|
+ // 提取点赞和评论涉及的用户ID
|
|
|
|
+ Set<Long> likeCommentUserIds = new HashSet<>();
|
|
|
|
+ likes.forEach(like -> likeCommentUserIds.add(like.getUserId()));
|
|
|
|
+ comments.forEach(comment -> {
|
|
|
|
+ likeCommentUserIds.add(comment.getUserId());
|
|
|
|
+ if (comment.getReplyToUser() != null) {
|
|
|
|
+ likeCommentUserIds.add(comment.getReplyToUser());
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ // 批量查询用户信息
|
|
|
|
+ List<UserLogin> likeCommentUsers = new ArrayList<>();
|
|
|
|
+ if (!likeCommentUserIds.isEmpty()) {
|
|
|
|
+ likeCommentUsers = userLoginMapper.selectBatchIds(likeCommentUserIds);
|
|
|
|
+ }
|
|
|
|
+ Map<Long, UserLogin> likeCommentUserMap = likeCommentUsers.stream()
|
|
|
|
+ .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);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 10. 封装最终结果
|
|
|
|
+ Map<String, Object> resultMap = new HashMap<>();
|
|
|
|
+ resultMap.put("userInfo", userLogin);
|
|
|
|
+ resultMap.put("moments", momentList);
|
|
|
|
+ resultMap.put("allLikesAndComments", allLikesAndComments);
|
|
|
|
+ resultMap.put("total", allMomentsPage.getTotal());
|
|
|
|
+ resultMap.put("currentPage", allMomentsPage.getCurrent());
|
|
|
|
+ resultMap.put("pageSize", allMomentsPage.getSize());
|
|
|
|
+ resultMap.put("totalPages", allMomentsPage.getPages());
|
|
|
|
+
|
|
|
|
+ return Result.OK(resultMap, "查询成功");
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("综合朋友圈查询异常: {}", e.getMessage(), e);
|
|
|
|
+ return Result.error(500, "查询失败");
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 封装动态数据并关联用户信息,确保包含momentId
|
|
|
|
+ */
|
|
|
|
+ private Map<String, Object> wrapMomentWithUser(UserMoments moment, Map<Long, UserLogin> userMap) {
|
|
|
|
+ Map<String, Object> momentMap = new HashMap<>();
|
|
|
|
+
|
|
|
|
+ // 手动添加所有需要的字段,确保momentId存在
|
|
|
|
+ momentMap.put("momentId", moment.getMomentId().toString());
|
|
|
|
+ momentMap.put("contentType", moment.getContentType());
|
|
|
|
+ momentMap.put("url", moment.getUrl());
|
|
|
|
+ momentMap.put("content", moment.getContent());
|
|
|
|
+ momentMap.put("location", moment.getLocation());
|
|
|
|
+ momentMap.put("visibility", moment.getVisibility());
|
|
|
|
+
|
|
|
|
+ // 转换日期为字符串格式
|
|
|
|
+ if (moment.getCreatedAt() != null) {
|
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
+ momentMap.put("createdAt", sdf.format(moment.getCreatedAt()));
|
|
|
|
+ }
|
|
|
|
|
|
|
|
+ // 手动添加用户信息,只包含必要字段
|
|
|
|
+ UserLogin user = userMap.get(moment.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());
|
|
|
|
+ momentMap.put("userInfo", userInfo);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ momentMap.put("isSelf", user != null && user.getId().equals(moment.getUserId()));
|
|
|
|
+ return momentMap;
|
|
|
|
+ }
|
|
/**
|
|
/**
|
|
* 上传到oss
|
|
* 上传到oss
|
|
* */
|
|
* */
|