From bebfd9d12276d6dc4b967753652ffddc472d081d Mon Sep 17 00:00:00 2001 From: lyp <255258@gongniu.com> Date: Sun, 21 Jan 2024 19:34:25 +0800 Subject: [PATCH 1/2] 1. add findUserByIds rpc --- .../com/luoo/comment/client/UserClient.java | 7 +++ .../comment/config/BaseExceptionHandler.java | 4 +- .../src/main/java/client/vo/SimpleUser.java | 11 +++++ .../controller/UserCollectController.java | 29 +++++++----- .../main/java/com/luoo/user/dao/UserDao.java | 8 +++- .../main/java/com/luoo/user/pojo/User.java | 15 +++++-- .../com/luoo/user/service/UserService.java | 44 +++++++++++-------- 7 files changed, 79 insertions(+), 39 deletions(-) create mode 100644 luoo_common/src/main/java/client/vo/SimpleUser.java diff --git a/luoo_comment/src/main/java/com/luoo/comment/client/UserClient.java b/luoo_comment/src/main/java/com/luoo/comment/client/UserClient.java index 8506208..5e416d3 100644 --- a/luoo_comment/src/main/java/com/luoo/comment/client/UserClient.java +++ b/luoo_comment/src/main/java/com/luoo/comment/client/UserClient.java @@ -1,11 +1,18 @@ package com.luoo.comment.client; +import api.Result; +import client.vo.SimpleUser; import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; + +import java.util.List; @FeignClient("luoo-user") public interface UserClient { //todo 根据用户 IDs 获取用户昵称集合 + @GetMapping("/findUserByIds") + Result> findUserByIds(List ids); } diff --git a/luoo_comment/src/main/java/com/luoo/comment/config/BaseExceptionHandler.java b/luoo_comment/src/main/java/com/luoo/comment/config/BaseExceptionHandler.java index 580fa70..0036d98 100644 --- a/luoo_comment/src/main/java/com/luoo/comment/config/BaseExceptionHandler.java +++ b/luoo_comment/src/main/java/com/luoo/comment/config/BaseExceptionHandler.java @@ -24,8 +24,6 @@ public class BaseExceptionHandler { @ExceptionHandler(value = AuthorityLoginException.class) @ResponseBody public Result error(AuthorityLoginException e) { - log.info("业务错误:{}", e.getMessage()); - StatusCode statusCode = null == e.getCodeEnum() ? StatusCode.MUSIC_COMMON_FAILED : e.getCodeEnum(); - return Result.failed(statusCode, e.getMessage()); + return Result.unauthorized(null); } } diff --git a/luoo_common/src/main/java/client/vo/SimpleUser.java b/luoo_common/src/main/java/client/vo/SimpleUser.java new file mode 100644 index 0000000..2198356 --- /dev/null +++ b/luoo_common/src/main/java/client/vo/SimpleUser.java @@ -0,0 +1,11 @@ +package client.vo; + +import lombok.Data; + +@Data +public class SimpleUser { + + private String userId; + + private String nickName; +} diff --git a/luoo_user/src/main/java/com/luoo/user/controller/UserCollectController.java b/luoo_user/src/main/java/com/luoo/user/controller/UserCollectController.java index bbc37a0..4b78db0 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/UserCollectController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/UserCollectController.java @@ -1,27 +1,22 @@ package com.luoo.user.controller; -import org.springframework.beans.BeanUtils; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestHeader; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; - -import com.luoo.user.service.UserCollectService; - import annotation.GlobalInterceptor; import annotation.VerifyParam; import api.Result; +import client.vo.SimpleUser; +import com.luoo.user.service.UserCollectService; +import com.luoo.user.service.UserService; import dto.UserLoginDto; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; import util.JwtUtil; +import java.util.List; + @Api(tags = "UserCollectController") @RestController @RequestMapping("/userCollect") @@ -30,6 +25,8 @@ public class UserCollectController { private JwtUtil jwtUtil; @Autowired private UserCollectService userCollectService; + @Autowired + private UserService userService; @ApiOperation(value = "1.收藏/喜欢") @ApiImplicitParams({ @ApiImplicitParam(name = "objectId", value = "收藏/喜欢的id,此处为歌曲/期刊id", required = true), @@ -57,4 +54,12 @@ public class UserCollectController { collectType); return Result.success(); } + + @ApiOperation(value = "根据 IDs 获取用户简要信息") + @GetMapping("/findUserByIds") + public Result> findUserByIds(@RequestBody List ids) { + List userByIds = userService.findUserByIds(ids); + return Result.success(userByIds); + } + } diff --git a/luoo_user/src/main/java/com/luoo/user/dao/UserDao.java b/luoo_user/src/main/java/com/luoo/user/dao/UserDao.java index 5c6b5a3..c35d279 100644 --- a/luoo_user/src/main/java/com/luoo/user/dao/UserDao.java +++ b/luoo_user/src/main/java/com/luoo/user/dao/UserDao.java @@ -1,12 +1,13 @@ package com.luoo.user.dao; +import com.luoo.user.pojo.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; - -import com.luoo.user.pojo.User; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; +import java.util.List; + /** * 数据访问接口 * @author Administrator @@ -28,4 +29,7 @@ public interface UserDao extends JpaRepository,JpaSpecificationExec public User findByLoginname(String loginname); public long countByNickname(String nickName); + + @Query("SELECT * FROM tb_user m WHERE m.id IN ?1") + List findUserByIds(List ids); } diff --git a/luoo_user/src/main/java/com/luoo/user/pojo/User.java b/luoo_user/src/main/java/com/luoo/user/pojo/User.java index 01b98e8..36f33af 100644 --- a/luoo_user/src/main/java/com/luoo/user/pojo/User.java +++ b/luoo_user/src/main/java/com/luoo/user/pojo/User.java @@ -1,12 +1,12 @@ package com.luoo.user.pojo; -import javax.persistence.Entity; -import javax.persistence.Id; -import javax.persistence.Table; - +import client.vo.SimpleUser; import lombok.Getter; import lombok.Setter; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; import java.io.Serializable; /** * 实体类 @@ -40,4 +40,11 @@ public class User implements Serializable{ private Integer followcount;//关注数 //private String deviceId;//设备id //private String deviceBrand;//设备品牌 + + public SimpleUser converSimpleUser(){ + SimpleUser simpleUser = new SimpleUser(); + simpleUser.setUserId(this.getId()); + simpleUser.setNickName(this.nickname); + return simpleUser; + } } diff --git a/luoo_user/src/main/java/com/luoo/user/service/UserService.java b/luoo_user/src/main/java/com/luoo/user/service/UserService.java index 1aae65f..f89a4ea 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/UserService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/UserService.java @@ -1,14 +1,12 @@ package com.luoo.user.service; -import java.util.*; -import java.util.concurrent.TimeUnit; - -import javax.persistence.criteria.CriteriaBuilder; -import javax.persistence.criteria.CriteriaQuery; -import javax.persistence.criteria.Predicate; -import javax.persistence.criteria.Root; -import javax.servlet.http.HttpServletRequest; - +import client.vo.SimpleUser; +import com.luoo.user.dao.UserDao; +import com.luoo.user.dao.UserInfoDao; +import com.luoo.user.pojo.User; +import com.luoo.user.util.NickNameUtil; +import constants.Constants; +import dto.UserLoginDto; import org.apache.commons.lang3.RandomStringUtils; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; @@ -18,19 +16,20 @@ import org.springframework.data.jpa.domain.Specification; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.stereotype.Service; - import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.CollectionUtils; import util.IdWorker; - -import constants.Constants; -import com.luoo.user.dao.UserDao; -import com.luoo.user.dao.UserInfoDao; -import com.luoo.user.pojo.User; -import com.luoo.user.util.NickNameUtil; - -import dto.UserLoginDto; import util.JwtUtil; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Predicate; +import javax.persistence.criteria.Root; +import javax.servlet.http.HttpServletRequest; +import java.util.*; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + /** * 服务层 * @@ -299,4 +298,13 @@ public class UserService { public long countByNickName(String nickName) { return userDao.countByNickname(nickName); } + + + public List findUserByIds(List ids) { + List userByIds = userDao.findUserByIds(ids); + if (CollectionUtils.isEmpty(userByIds)){ + return new ArrayList<>(); + } + return userByIds.stream().map(User::converSimpleUser).collect(Collectors.toList()); + } } From 446c3f4196fcf350ac9e285245f8e3c206a89a2c Mon Sep 17 00:00:00 2001 From: lyp <255258@gongniu.com> Date: Sun, 21 Jan 2024 19:46:13 +0800 Subject: [PATCH 2/2] 1. handle findByVolid interface --- .../comment/controller/CommentController.java | 2 +- .../luoo/comment/service/CommentService.java | 2 +- .../service/impl/CommentServiceImpl.java | 48 +++++++++++++++++-- 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java b/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java index c0e72cf..068b61f 100644 --- a/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java +++ b/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java @@ -28,7 +28,7 @@ public class CommentController extends BaseController{ private RedisTemplate redisTemplate; @GetMapping("/findByVolid/{volid}") - public Result> findByVolid(@PathVariable String volid){ + public Result> findByVolid(@PathVariable String volid){ return Result.success(commentService.findByVolid(volid)); } diff --git a/luoo_comment/src/main/java/com/luoo/comment/service/CommentService.java b/luoo_comment/src/main/java/com/luoo/comment/service/CommentService.java index 7b371ad..d17d6e6 100644 --- a/luoo_comment/src/main/java/com/luoo/comment/service/CommentService.java +++ b/luoo_comment/src/main/java/com/luoo/comment/service/CommentService.java @@ -22,5 +22,5 @@ public interface CommentService { void thumbup(String commentId); - List findByVolid(String volid); + List findByVolid(String volid); } diff --git a/luoo_comment/src/main/java/com/luoo/comment/service/impl/CommentServiceImpl.java b/luoo_comment/src/main/java/com/luoo/comment/service/impl/CommentServiceImpl.java index 233dbd2..cdbfc4f 100644 --- a/luoo_comment/src/main/java/com/luoo/comment/service/impl/CommentServiceImpl.java +++ b/luoo_comment/src/main/java/com/luoo/comment/service/impl/CommentServiceImpl.java @@ -1,7 +1,10 @@ package com.luoo.comment.service.impl; +import api.Result; +import client.vo.SimpleUser; import cn.hutool.core.util.ObjectUtil; +import com.luoo.comment.client.UserClient; import com.luoo.comment.dao.CommentDao; import com.luoo.comment.enums.CommentStatusEnum; import com.luoo.comment.pojo.Comment; @@ -18,6 +21,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import util.IdWorker; +import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; @@ -28,6 +32,9 @@ import java.util.stream.Collectors; @Transactional public class CommentServiceImpl implements CommentService { + @Autowired + private UserClient userClient; + @Autowired private CommentDao commentDao; @@ -100,9 +107,15 @@ public class CommentServiceImpl implements CommentService { query.with(pageable); List comments = mongoTemplate.find(query, Comment.class); + List userIds = comments.stream().map(Comment::getUserId).collect(Collectors.toList()); + + Result> userByIds = userClient.findUserByIds(userIds); + + Map userIdAndInfoMap = userByIds.getData().stream().collect(Collectors.toMap(SimpleUser::getUserId, Function.identity())); + List commentVos = comments.stream().map(Comment::convertVo).collect(Collectors.toList()); - List targetComments = comments.stream().map(Comment::getTargetId).collect(Collectors.toList()); +// List targetComments = comments.stream().map(Comment::getTargetId).collect(Collectors.toList()); Map idCommentMap = commentVos.stream().collect(Collectors.toMap(CommentVo::getId, Function.identity())); @@ -111,6 +124,14 @@ public class CommentServiceImpl implements CommentService { // boolean isChange = targetComments.removeAll(idCommentMap.keySet()); for (CommentVo commentVo : commentVos) { + + // 处理昵称 + SimpleUser simpleUser = userIdAndInfoMap.get(commentVo.getUserId()); + + if (ObjectUtil.isNotEmpty(simpleUser)) { + commentVo.setNickName(simpleUser.getNickName()); + } + // 处理叠楼评论回复 if (commentVo.getLevel() > 2 && ObjectUtil.isNotEmpty(commentVo.getTargetId())) { CommentVo targetCommentVo = idCommentMap.get(commentVo.getTargetId()); if (targetCommentVo != null) { @@ -130,7 +151,28 @@ public class CommentServiceImpl implements CommentService { mongoTemplate.updateFirst(query,update,"spit"); } - public List findByVolid(String volid) { - return commentDao.findByArticleId(volid); + public List findByVolid(String volid) { + List byArticleId = commentDao.findByArticleId(volid); + + if (ObjectUtil.isEmpty(byArticleId)) { + return new ArrayList<>(); + } + + List commentVos = byArticleId.stream().map(Comment::convertVo).collect(Collectors.toList()); + + List userIds = byArticleId.stream().map(Comment::getUserId).collect(Collectors.toList()); + Result> userByIds = userClient.findUserByIds(userIds); + + Map userIdAndInfoMap = userByIds.getData().stream().collect(Collectors.toMap(SimpleUser::getUserId, Function.identity())); + + for (CommentVo commentVo : commentVos) { + // 处理昵称 + SimpleUser simpleUser = userIdAndInfoMap.get(commentVo.getUserId()); + + if (ObjectUtil.isNotEmpty(simpleUser)) { + commentVo.setNickName(simpleUser.getNickName()); + } + } + return commentVos; } }