123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753 |
- package com.zhentao.moment.service.impl;
- import com.aliyun.oss.OSSClient;
- import com.aliyun.oss.model.ObjectMetadata;
- import com.aliyun.oss.model.PutObjectRequest;
- 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.zhentao.moment.domain.MomentComments;
- import com.zhentao.moment.domain.MomentLikes;
- import com.zhentao.moment.domain.UserMoments;
- 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.service.UserMomentsService;
- import com.zhentao.moment.mapper.UserMomentsMapper;
- import com.zhentao.osspicture.OssConfig;
- import com.zhentao.osspicture.OssUtil;
- import com.zhentao.tool.TokenUtils;
- import com.zhentao.user.domain.UserLogin;
- import com.zhentao.user.mapper.UserLoginMapper;
- import com.zhentao.userRelationships.domain.UserRelationships;
- import com.zhentao.userRelationships.mapper.UserRelationshipsMapper;
- 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.text.SimpleDateFormat;
- import java.util.*;
- import java.util.concurrent.CompletableFuture;
- import java.util.stream.Collectors;
- import java.util.stream.Stream;
- /**
- * @author 86159
- * @description 针对表【user_moments(朋友圈动态表)】的数据库操作Service实现
- * @createDate 2025-06-04 11:56:59
- */
- @Service
- @Slf4j
- public class UserMomentsServiceImpl extends ServiceImpl<UserMomentsMapper, UserMoments>
- implements UserMomentsService{
- @Autowired
- private UserMomentsMapper userMomentsMapper;
- @Autowired
- private UserLoginMapper userLoginMapper;
- @Autowired
- private MomentLikesMapper momentLikesMapper;
- @Autowired
- private OssUtil ossUtil;
- @Autowired
- private OssConfig ossConfig;
- @Autowired
- private UserRelationshipsMapper userRelationshipsMapper;
- @Autowired
- private MomentCommentsMapper momentCommentsMapper;
- // 发送朋友圈
- @Override
- public Result sendMonment(String token,MonmentDto monmentDto) {
- if (monmentDto.getContent()==null){
- return Result.error(400,"请输入内容");
- }
- if (monmentDto.getContentType()==null){
- return Result.error(400,"内容类型不能为空");
- }
- try {
- UserMoments userMoments=new UserMoments();
- userMoments.setMomentId(SnowflakeUtil.nextId());
- String userId = TokenUtils.getUserIdFromToken(token);
- userMoments.setUserId(Long.valueOf(userId));
- String filter = SensitiveWordFilter.filter(monmentDto.getContent());
- userMoments.setContent(filter);
- Integer typeCode = monmentDto.getContentType();
- 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,"只有图片类型支持上传多张");
- // }
- switch (type){
- case IMAGE:
- for (MultipartFile file : monmentDto.getFiles()){
- if (file.isEmpty()){
- return Result.error(400,"图片不能为空");
- }
- validateImageContent(file);
- }
- List<String> imageUrls = new ArrayList<>();
- List<String> imageNames = new ArrayList<>();
- Long totalSize = 0L;
- String imageDir = ossConfig.getUpload().getImage().getDir();
- for (MultipartFile file : monmentDto.getFiles()){
- String imageFileName = userId + "/" + System.currentTimeMillis() +
- getFileExtension(file.getOriginalFilename());
- String ossFilePath = imageDir + imageFileName;
- // 上传OSS
- String url = uploadToOss(file, ossFilePath);
- imageUrls.add(url);
- imageNames.add(file.getOriginalFilename());
- totalSize+=file.getSize();
- }
- String urlJson = "[" + imageUrls.stream()
- .map(url -> "\"" + url.replace("\"", "\\\"") + "\"") // 确保URL中的双引号被转义
- .collect(Collectors.joining(",")) + "]";
- userMoments.setUrl(urlJson);
- String nameJson = "[" + imageNames.stream()
- .map(name -> "\"" + name.replace("\"", "\\\"") + "\"")
- .collect(Collectors.joining(",")) + "]";
- userMoments.setFilename(nameJson);
- userMoments.setFilesize(totalSize);
- break;
- case VIDEO:
- MultipartFile videoFile = monmentDto.getFiles().get(0);
- if (videoFile.isEmpty()){
- return Result.error(400,"视频不能为空");
- }
- validateVideoContent(videoFile);
- String videoDir = ossConfig.getUpload().getVideo().getDir();
- String videoFileName = userId + "/" + System.currentTimeMillis() +
- getFileExtension(videoFile.getOriginalFilename());
- String ossFilePath = videoDir + videoFileName;
- String url = uploadToOss(videoFile, ossFilePath);
- userMoments.setUrl(url);
- userMoments.setFilename(videoFile.getOriginalFilename());
- userMoments.setFilesize(videoFile.getSize());
- break;
- default:
- return Result.error(400,"内容类型错误");
- }
- }
- 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());
- map.put("ossFilePath",userMoments.getUrl());
- return Result.OK(map,"发送成功");
- }catch (Exception e){
- e.printStackTrace();
- return Result.error(500,"发送失败");
- }
- }
- // 删除朋友圈
- @Override
- 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.ERR(200,"删除成功");
- }
- }
- // 查询个人信息
- @Override
- public Result userinfo(String token) {
- String userId = TokenUtils.getUserIdFromToken(token);
- if (userId==null){
- return Result.error(400,"没有userid");
- }
- UserLogin userLogin = userLoginMapper.selectById(userId);
- return Result.OK(userLogin,"查询成功");
- }
- // 查询我的朋友圈
- @Override
- 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);
- 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) {
- System.err.println("赞"+monmentDto.getMomentId());
- 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.OK(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,"查询成功");
- }
- // 更改朋友圈背景图
- @Override
- 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, "用户信息不存在");
- }
- // 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
- * */
- private String uploadToOss(MultipartFile file, String ossFilePath) throws IOException {
- OSSClient ossClient = new OSSClient(ossConfig.getEndpoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret());
- String bucketName = ossConfig.getBucketName();
- // 创建PutObject请求
- PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, ossFilePath,
- file.getInputStream(), null);
- // 设置对象元信息
- ObjectMetadata metadata = new ObjectMetadata();
- metadata.setContentType(file.getContentType());
- metadata.setContentLength(file.getSize());
- putObjectRequest.setMetadata(metadata);
- // 上传文件
- ossClient.putObject(putObjectRequest);
- // 构建访问URL
- String endpoint = ossConfig.getEndpoint();
- String cleanEndpoint = endpoint.replaceAll("^https://", "");
- String domain = "https://" + bucketName + "." + cleanEndpoint;
- return domain + "/" + ossFilePath;
- }
- /**
- * 获取文件扩展名
- */
- private String getFileExtension(String fileName) {
- if (fileName == null || !fileName.contains(".")) {
- return "";
- }
- return fileName.substring(fileName.lastIndexOf("."));
- }
- /**
- * 验证图片内容
- */
- private void validateImageContent(MultipartFile file) throws Exception {
- if (file == null || file.isEmpty()) {
- throw new Exception("图片不能为空");
- }
- String contentType = file.getContentType();
- if (!contentType.startsWith("image/")) {
- throw new Exception("请上传图片格式文件");
- }
- // 验证图片大小(例如限制为10MB)
- if (file.getSize() > ossConfig.getUpload().getImage().getMaxSize()) {
- throw new Exception("图片大小不能超过" +
- (ossConfig.getUpload().getImage().getMaxSize() / (1024 * 1024)) + "MB");
- }
- }
- /**
- * 验证视频内容
- */
- private void validateVideoContent(MultipartFile file) throws Exception {
- if (file == null || file.isEmpty()) {
- throw new Exception("视频不能为空");
- }
- String contentType = file.getContentType();
- if (!contentType.startsWith("video/")) {
- throw new Exception("请上传视频格式文件");
- }
- // 验证视频大小(例如限制为50MB)
- if (file.getSize() > ossConfig.getUpload().getVideo().getMaxSize()) {
- throw new Exception("视频大小不能超过" +
- (ossConfig.getUpload().getVideo().getMaxSize() / (1024 * 1024)) + "MB");
- }
- }
- }
|