|
@@ -7,9 +7,16 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.zhentao.moment.config.OssConfig;
|
|
|
import com.zhentao.moment.config.OssProperties;
|
|
|
+import com.zhentao.moment.domain.MomentComments;
|
|
|
+import com.zhentao.moment.domain.MomentLikes;
|
|
|
import com.zhentao.moment.domain.UserMoments;
|
|
|
+import com.zhentao.moment.domain.UserRelationships;
|
|
|
+import com.zhentao.moment.dto.CommentsDto;
|
|
|
import com.zhentao.moment.dto.MonmentDto;
|
|
|
import com.zhentao.moment.enums.ContentTypeEnum;
|
|
|
+import com.zhentao.moment.mapper.MomentCommentsMapper;
|
|
|
+import com.zhentao.moment.mapper.MomentLikesMapper;
|
|
|
+import com.zhentao.moment.mapper.UserRelationshipsMapper;
|
|
|
import com.zhentao.moment.service.UserMomentsService;
|
|
|
import com.zhentao.moment.mapper.UserMomentsMapper;
|
|
|
import com.zhentao.tool.TokenUtils;
|
|
@@ -19,15 +26,16 @@ import com.zhentao.utils.SensitiveWordFilter;
|
|
|
import com.zhentao.utils.SnowflakeUtil;
|
|
|
import com.zhentao.vo.Result;
|
|
|
import io.jsonwebtoken.Claims;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
@@ -36,6 +44,7 @@ import java.util.stream.Collectors;
|
|
|
* @createDate 2025-06-04 11:56:59
|
|
|
*/
|
|
|
@Service
|
|
|
+@Slf4j
|
|
|
public class UserMomentsServiceImpl extends ServiceImpl<UserMomentsMapper, UserMoments>
|
|
|
implements UserMomentsService{
|
|
|
@Autowired
|
|
@@ -43,10 +52,16 @@ public class UserMomentsServiceImpl extends ServiceImpl<UserMomentsMapper, UserM
|
|
|
@Autowired
|
|
|
private UserLoginMapper userLoginMapper;
|
|
|
@Autowired
|
|
|
-
|
|
|
+ private MomentLikesMapper momentLikesMapper;
|
|
|
+ @Autowired
|
|
|
private OSSClient ossClient;
|
|
|
@Autowired
|
|
|
private OssProperties ossProperties;
|
|
|
+ @Autowired
|
|
|
+ private UserRelationshipsMapper userRelationshipsMapper;
|
|
|
+ @Autowired
|
|
|
+ private MomentCommentsMapper momentCommentsMapper;
|
|
|
+ // 发送朋友圈
|
|
|
@Override
|
|
|
public Result sendMonment(String token,MonmentDto monmentDto) {
|
|
|
if (monmentDto.getContent()==null){
|
|
@@ -63,11 +78,14 @@ public class UserMomentsServiceImpl extends ServiceImpl<UserMomentsMapper, UserM
|
|
|
String filter = SensitiveWordFilter.filter(monmentDto.getContent());
|
|
|
userMoments.setContent(filter);
|
|
|
Integer typeCode = monmentDto.getContentType();
|
|
|
- ContentTypeEnum type = ContentTypeEnum.getByCode(typeCode);
|
|
|
+ System.err.println(typeCode);
|
|
|
+ ContentTypeEnum type = ContentTypeEnum.getByCodeOrThrow(typeCode);
|
|
|
+ userMoments.setContentType(typeCode);
|
|
|
if (monmentDto.getFiles()!=null && !monmentDto.getFiles().isEmpty()){
|
|
|
- if (type!=ContentTypeEnum.IMAGE){
|
|
|
- return Result.error(400,"只有图片类型支持上传多张");
|
|
|
- }
|
|
|
+// if (type!=ContentTypeEnum.IMAGE){
|
|
|
+// return Result.error(400,"只有图片类型支持上传多张");
|
|
|
+// }
|
|
|
+
|
|
|
switch (type){
|
|
|
case IMAGE:
|
|
|
for (MultipartFile file : monmentDto.getFiles()){
|
|
@@ -120,15 +138,16 @@ public class UserMomentsServiceImpl extends ServiceImpl<UserMomentsMapper, UserM
|
|
|
break;
|
|
|
default:
|
|
|
return Result.error(400,"内容类型错误");
|
|
|
+ }
|
|
|
}
|
|
|
-// 上传文件到OSS
|
|
|
-
|
|
|
- }else if (type!=ContentTypeEnum.TEXT){
|
|
|
+ else if (type!=ContentTypeEnum.TEXT){
|
|
|
return Result.ERR(null,"类型上传不正确");
|
|
|
}
|
|
|
//// userMoments.setContentType();
|
|
|
// userMoments.setContent(monmentDto.getContent());
|
|
|
userMoments.setVisibility(monmentDto.getVisible());
|
|
|
+ userMoments.setLocation(monmentDto.getLocation());
|
|
|
+ userMoments.setMomentId(SnowflakeUtil.nextId());
|
|
|
userMomentsMapper.insert(userMoments);
|
|
|
Map<String,Object> map=new HashMap<>();
|
|
|
map.put("monmentId",userMoments.getMomentId());
|
|
@@ -140,12 +159,26 @@ public class UserMomentsServiceImpl extends ServiceImpl<UserMomentsMapper, UserM
|
|
|
}
|
|
|
|
|
|
}
|
|
|
-// 删除朋友圈
|
|
|
+ // 删除朋友圈
|
|
|
@Override
|
|
|
- public Result deleteMonment(MonmentDto monmentDto) {
|
|
|
- return null;
|
|
|
+ public Result deleteMonment(String token,MonmentDto monmentDto) {
|
|
|
+ if (monmentDto.getMomentId()==null){
|
|
|
+ return Result.error(400,"朋友圈id不能为空");
|
|
|
+ }
|
|
|
+ Long userId = Long.valueOf(TokenUtils.getUserIdFromToken(token));
|
|
|
+ UserMoments userMoments = userMomentsMapper.selectById(monmentDto.getMomentId());
|
|
|
+ if (userMoments.getMomentId()==null){
|
|
|
+ return Result.error(400,"没有此朋友圈");
|
|
|
+ }else{
|
|
|
+ if (!userId.equals(userMoments.getUserId())){
|
|
|
+ return Result.error(400,"没有权限");
|
|
|
+ }
|
|
|
+ userMomentsMapper.deleteById(monmentDto.getMomentId());
|
|
|
+ return Result.OKK(200,"删除成功");
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
-//查询个人信息
|
|
|
+ // 查询个人信息
|
|
|
@Override
|
|
|
public Result userinfo(String token) {
|
|
|
String userId = TokenUtils.getUserIdFromToken(token);
|
|
@@ -155,18 +188,237 @@ public class UserMomentsServiceImpl extends ServiceImpl<UserMomentsMapper, UserM
|
|
|
UserLogin userLogin = userLoginMapper.selectById(userId);
|
|
|
return Result.OK(userLogin,"查询成功");
|
|
|
}
|
|
|
-
|
|
|
+ // 查询我的朋友圈
|
|
|
@Override
|
|
|
- public List<UserMoments> getMyMonment(Long userId) {
|
|
|
+ public Result getMyMonment(Long userId) {
|
|
|
QueryWrapper<UserMoments> queryWrapper=new QueryWrapper<>();
|
|
|
queryWrapper.eq("user_id",userId);
|
|
|
+ queryWrapper.orderByDesc("created_at");
|
|
|
+ UserLogin userLogin = userLoginMapper.selectById(userId);
|
|
|
List<UserMoments> userMoments = userMomentsMapper.selectList(queryWrapper);
|
|
|
- return userMoments;
|
|
|
+ Map<String,Object> map=new HashMap<>();
|
|
|
+ map.put("userMoments",userMoments);
|
|
|
+ map.put("userLogin",userLogin);
|
|
|
+ return Result.OK(map,"查询成功");
|
|
|
+ }
|
|
|
+ // 查询好友的朋友圈
|
|
|
+ @Override
|
|
|
+ public Result getFriendMonment(Long userId) {
|
|
|
+ log.info("查询好友朋友圈,用户ID: {}", userId);
|
|
|
+
|
|
|
+ // 构建查询条件,只查询user_id=userId的好友关系
|
|
|
+ QueryWrapper<UserRelationships> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("user_id", userId)
|
|
|
+ .eq("status", 1)
|
|
|
+ .eq("is_blacklist", 0)
|
|
|
+ .eq("is_del", 0)
|
|
|
+ .eq("is_moments", 0)
|
|
|
+ .orderByDesc("created_at");
|
|
|
+
|
|
|
+ // 查询好友关系
|
|
|
+ List<UserRelationships> userRelationships = userRelationshipsMapper.selectList(queryWrapper);
|
|
|
+ log.info("查询到的好友关系数量: {}", userRelationships.size());
|
|
|
+
|
|
|
+ // 调试:打印查询到的每条好友关系
|
|
|
+ for (UserRelationships rel : userRelationships) {
|
|
|
+ log.info("好友关系: user_id={}, friend_id={}, status={}, is_blacklist={}, is_del={}, is_moments={}",
|
|
|
+ rel.getUserId(), rel.getFriendId(), rel.getStatus(),
|
|
|
+ rel.getIsBlacklist(), rel.getIsDel(), rel.getIsMoments());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 提取好友ID(单向查询:直接从friend_id获取)
|
|
|
+ List<Long> friendIds = userRelationships.stream()
|
|
|
+ .map(UserRelationships::getFriendId)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ // 处理无好友情况
|
|
|
+ if (friendIds.isEmpty()) {
|
|
|
+ log.warn("没有找到好友,返回空结果");
|
|
|
+ return Result.OK(new ArrayList<>(), "没有好友的朋友圈");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查询好友的朋友圈
|
|
|
+ QueryWrapper<UserMoments> momentQuery = new QueryWrapper<>();
|
|
|
+ momentQuery.in("user_id", friendIds)
|
|
|
+ .ne("visibility",2)
|
|
|
+ .orderByDesc("created_at");
|
|
|
+ List<UserMoments> userMoments = userMomentsMapper.selectList(momentQuery);
|
|
|
+
|
|
|
+ // 关联用户信息
|
|
|
+ // 1. 提取朋友圈中所有不同的userId
|
|
|
+ Set<Long> userIdsInMoments = userMoments.stream()
|
|
|
+ .map(UserMoments::getUserId)
|
|
|
+ .collect(Collectors.toSet());
|
|
|
+
|
|
|
+ // 2. 根据userId列表查询用户信息
|
|
|
+ if (userIdsInMoments.isEmpty()) {
|
|
|
+ log.warn("好友没有发布任何朋友圈");
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("userMoments", Collections.emptyList());
|
|
|
+ map.put("userList", Collections.emptyList());
|
|
|
+ return Result.OK(map, "查询成功");
|
|
|
+ }
|
|
|
+
|
|
|
+ QueryWrapper<UserLogin> userQuery = new QueryWrapper<>();
|
|
|
+ userQuery.in("id", userIdsInMoments);
|
|
|
+ List<UserLogin> users = userLoginMapper.selectList(userQuery);
|
|
|
+
|
|
|
+ // 3. 将用户信息转换为Map,根据userId查找
|
|
|
+ Map<Long, UserLogin> userMap = users.stream()
|
|
|
+ .collect(Collectors.toMap(UserLogin::getId, user -> user));
|
|
|
+
|
|
|
+ // 4. 包含用户信息的列表
|
|
|
+ List<Map<String, Object>> userList = userMoments.stream()
|
|
|
+ .map(moment -> {
|
|
|
+ Map<String, Object> momentMap = new HashMap<>();
|
|
|
+ BeanUtils.copyProperties(moment, momentMap);
|
|
|
+
|
|
|
+ UserLogin user = userMap.get(moment.getUserId());
|
|
|
+ if (user != null) {
|
|
|
+ momentMap.put("nickName", user.getNickName());
|
|
|
+ momentMap.put("avatar", user.getAvatar());
|
|
|
+ }
|
|
|
+ return momentMap;
|
|
|
+ })
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ log.info("查询到的朋友圈数量: {}", userMoments.size());
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("userMoments", userMoments);
|
|
|
+ map.put("userList", userList);
|
|
|
+ return Result.OK(map, "查询成功");
|
|
|
}
|
|
|
+ // 点赞朋友圈
|
|
|
+ @Override
|
|
|
+ public Result likeMonment(Long uid, MonmentDto monmentDto) {
|
|
|
|
|
|
- // **
|
|
|
-// * 上传文件到OSS
|
|
|
-// */
|
|
|
+ if (monmentDto.getMomentId()==null){
|
|
|
+ return Result.error(400,"请选择要点赞的朋友圈");
|
|
|
+ }
|
|
|
+ QueryWrapper<MomentLikes> queryWrapper=new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("moment_id",monmentDto.getMomentId());
|
|
|
+ queryWrapper.eq("user_id",uid);
|
|
|
+ // 查询是否已点赞
|
|
|
+ MomentLikes momentLikes = momentLikesMapper.selectOne(queryWrapper);
|
|
|
+ if (momentLikes==null){
|
|
|
+ // 没有点赞
|
|
|
+ MomentLikes momentLikes1=new MomentLikes();
|
|
|
+ momentLikes1.setLikeId(SnowflakeUtil.nextId());
|
|
|
+ momentLikes1.setMomentId(Long.valueOf(monmentDto.getMomentId()));
|
|
|
+ momentLikes1.setUserId(uid);
|
|
|
+ momentLikesMapper.insert(momentLikes1);
|
|
|
+ return Result.OKK(200,"点赞成功");
|
|
|
+ }else{
|
|
|
+ // 已点赞
|
|
|
+ momentLikesMapper.delete(queryWrapper);
|
|
|
+ return Result.error(400,"取消点赞");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 评论朋友圈
|
|
|
+ @Override
|
|
|
+ public Result commentsMonment(Long userId, CommentsDto commentsDto) {
|
|
|
+ System.err.println(commentsDto.getMomentId());
|
|
|
+ MomentComments momentComments = new MomentComments();
|
|
|
+ momentComments.setUserId(userId);
|
|
|
+ momentComments.setCommentId(SnowflakeUtil.nextId());
|
|
|
+ UserMoments userMoments = userMomentsMapper.selectById(commentsDto.getMomentId());
|
|
|
+ System.err.println(userMoments);
|
|
|
+ if (userMoments==null){
|
|
|
+ return Result.error(400,"朋友圈不存在");
|
|
|
+ }
|
|
|
+ if (commentsDto.getContent()==null){
|
|
|
+ return Result.error(400,"评论不能为空");
|
|
|
+ }
|
|
|
+ commentsDto.setReplyTo(Long.valueOf(commentsDto.getMomentId()));
|
|
|
+ commentsDto.setReplyToUser(userMoments.getUserId());
|
|
|
+ momentComments.setMomentId(Long.valueOf(commentsDto.getMomentId()));
|
|
|
+ momentComments.setContent(commentsDto.getContent());
|
|
|
+ momentComments.setReplyToUser(commentsDto.getReplyToUser());
|
|
|
+ momentComments.setReplyTo(commentsDto.getReplyTo());
|
|
|
+ momentCommentsMapper.insert(momentComments);
|
|
|
+ return Result.OK(200,"评论成功");
|
|
|
+ }
|
|
|
+ // 查询点赞列表
|
|
|
+ @Override
|
|
|
+ public Result likeList(Long uid, MonmentDto monmentDto) {
|
|
|
+ if (monmentDto.getMomentId()==null){
|
|
|
+ return Result.error(400,"朋友圈id不能为空");
|
|
|
+ }
|
|
|
+ QueryWrapper<MomentLikes> queryWrapper=new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("moment_id",monmentDto.getMomentId());
|
|
|
+ List<MomentLikes> momentLikes = momentLikesMapper.selectList(queryWrapper);
|
|
|
+// 如果没有点赞
|
|
|
+ if (CollectionUtils.isEmpty(momentLikes)){
|
|
|
+ Map<String,Object> map = new HashMap<>();
|
|
|
+ map.put("likeList", Collections.emptyList());
|
|
|
+ map.put("userList", Collections.emptyList());
|
|
|
+ map.put("total", 0);
|
|
|
+ return Result.OK(map, "查询成功,暂无点赞数据");
|
|
|
+ }
|
|
|
+ // 提取去重后的用户ID列表
|
|
|
+ List<Long> userIds = momentLikes.stream()
|
|
|
+ .map(MomentLikes::getUserId)
|
|
|
+ .distinct()
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ List<Long> userLikes=new ArrayList<>();
|
|
|
+ for (MomentLikes like:momentLikes){
|
|
|
+ userLikes.add(like.getUserId());
|
|
|
+ }
|
|
|
+ // 查询用户信息(需恢复此功能)
|
|
|
+ List<UserLogin> userLogins = userLoginMapper.selectBatchIds(userIds);
|
|
|
+ Map<Long, UserLogin> userMap = userLogins.stream()
|
|
|
+ .collect(Collectors.toMap(UserLogin::getId, user -> user));
|
|
|
+
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("likeList",momentLikes);
|
|
|
+ map.put("userList",userLogins);
|
|
|
+ return Result.OK(map,"查询成功");
|
|
|
+ }
|
|
|
+ // 查询评论列表
|
|
|
+ @Override
|
|
|
+ public Result commentList(Long valueOf, CommentsDto commentsDto) {
|
|
|
+ QueryWrapper<MomentComments> queryWrapper=new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("moment_id",commentsDto.getMomentId());
|
|
|
+ queryWrapper.orderByDesc("created_at");
|
|
|
+ List<MomentComments> momentComments = momentCommentsMapper.selectList(queryWrapper);
|
|
|
+// 查询朋友圈发布者信息
|
|
|
+ UserMoments userMoments = userMomentsMapper.selectById(commentsDto.getMomentId());
|
|
|
+ Long publisherId = null;
|
|
|
+ if (userMoments!=null){
|
|
|
+ publisherId = userMoments.getUserId();
|
|
|
+ }
|
|
|
+ List<Long> replyToUserIds=new ArrayList<>(); //存储每条评论的replyToUser
|
|
|
+ List<Long> replyToMomentIds=new ArrayList<>(); //存储每条评论的朋友圈id
|
|
|
+ List<String> userUsernames=new ArrayList<>();
|
|
|
+ for (MomentComments comment:momentComments){
|
|
|
+ UserLogin user = userLoginMapper.selectById(comment.getUserId());
|
|
|
+ if (user!=null){
|
|
|
+ userUsernames.add(user.getUserUsername());
|
|
|
+ }
|
|
|
+ // 设置replyTo和replyToUser
|
|
|
+ if (comment.getReplyTo() == null) {
|
|
|
+ // 如果当前评论没有回复目标(直接评论朋友圈)
|
|
|
+ replyToMomentIds.add(comment.getMomentId()); // replyTo设为当前朋友圈ID
|
|
|
+ replyToUserIds.add(publisherId); // replyToUser设为朋友圈发布者ID
|
|
|
+ } else {
|
|
|
+ // 如果当前评论是回复其他评论
|
|
|
+ replyToMomentIds.add(comment.getReplyTo()); // replyTo设为被回复的评论ID
|
|
|
+ replyToUserIds.add(comment.getReplyToUser()); // replyToUser设为被回复的用户ID
|
|
|
+ }
|
|
|
+ }
|
|
|
+ commentsDto.setUserUsernames(userUsernames);
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("commentList",momentComments);
|
|
|
+ map.put("usernames",userUsernames);
|
|
|
+ map.put("replyToUserIds", replyToUserIds); // 添加replyToUser
|
|
|
+ map.put("replyToMomentIds", replyToMomentIds); // 添加replyTo
|
|
|
+ return Result.OK(map,"查询成功");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传到oss
|
|
|
+ * */
|
|
|
private String uploadToOss(MultipartFile file, String ossFilePath) throws IOException {
|
|
|
String bucketName = ossProperties.getBucketName();
|
|
|
|
|
@@ -218,8 +470,6 @@ public class UserMomentsServiceImpl extends ServiceImpl<UserMomentsMapper, UserM
|
|
|
throw new Exception("图片大小不能超过" +
|
|
|
(ossProperties.getUpload().getImage().getMaxSize() / (1024 * 1024)) + "MB");
|
|
|
}
|
|
|
-
|
|
|
- // 可以添加更多图片验证逻辑,如图片尺寸、格式等
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -241,7 +491,6 @@ public class UserMomentsServiceImpl extends ServiceImpl<UserMomentsMapper, UserM
|
|
|
(ossProperties.getUpload().getVideo().getMaxSize() / (1024 * 1024)) + "MB");
|
|
|
}
|
|
|
|
|
|
- // 可以添加更多视频验证逻辑,如视频时长、分辨率等
|
|
|
}
|
|
|
}
|
|
|
|