Merge remote-tracking branch 'origin/main'

main
wangqing 10 months ago
commit 4b62a7003d

@ -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<List<SimpleUser>> findUserByIds(List<String> ids);
}

@ -24,8 +24,6 @@ public class BaseExceptionHandler {
@ExceptionHandler(value = AuthorityLoginException.class)
@ResponseBody
public Result<String> 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);
}
}

@ -28,7 +28,7 @@ public class CommentController extends BaseController{
private RedisTemplate redisTemplate;
@GetMapping("/findByVolid/{volid}")
public Result<List<Comment>> findByVolid(@PathVariable String volid){
public Result<List<CommentVo>> findByVolid(@PathVariable String volid){
return Result.success(commentService.findByVolid(volid));
}

@ -22,5 +22,5 @@ public interface CommentService {
void thumbup(String commentId);
List<Comment> findByVolid(String volid);
List<CommentVo> findByVolid(String volid);
}

@ -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<Comment> comments = mongoTemplate.find(query, Comment.class);
List<String> userIds = comments.stream().map(Comment::getUserId).collect(Collectors.toList());
Result<List<SimpleUser>> userByIds = userClient.findUserByIds(userIds);
Map<String, SimpleUser> userIdAndInfoMap = userByIds.getData().stream().collect(Collectors.toMap(SimpleUser::getUserId, Function.identity()));
List<CommentVo> commentVos = comments.stream().map(Comment::convertVo).collect(Collectors.toList());
List<String> targetComments = comments.stream().map(Comment::getTargetId).collect(Collectors.toList());
// List<String> targetComments = comments.stream().map(Comment::getTargetId).collect(Collectors.toList());
Map<String, CommentVo> 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<Comment> findByVolid(String volid) {
return commentDao.findByArticleId(volid);
public List<CommentVo> findByVolid(String volid) {
List<Comment> byArticleId = commentDao.findByArticleId(volid);
if (ObjectUtil.isEmpty(byArticleId)) {
return new ArrayList<>();
}
List<CommentVo> commentVos = byArticleId.stream().map(Comment::convertVo).collect(Collectors.toList());
List<String> userIds = byArticleId.stream().map(Comment::getUserId).collect(Collectors.toList());
Result<List<SimpleUser>> userByIds = userClient.findUserByIds(userIds);
Map<String, SimpleUser> 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;
}
}

@ -0,0 +1,11 @@
package client.vo;
import lombok.Data;
@Data
public class SimpleUser {
private String userId;
private String nickName;
}

@ -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<List<SimpleUser>> findUserByIds(@RequestBody List<String> ids) {
List<SimpleUser> userByIds = userService.findUserByIds(ids);
return Result.success(userByIds);
}
}

@ -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<User,String>,JpaSpecificationExec
public User findByLoginname(String loginname);
public long countByNickname(String nickName);
@Query("SELECT * FROM tb_user m WHERE m.id IN ?1")
List<User> findUserByIds(List<String> ids);
}

@ -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;
}
}

@ -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<SimpleUser> findUserByIds(List<String> ids) {
List<User> userByIds = userDao.findUserByIds(ids);
if (CollectionUtils.isEmpty(userByIds)){
return new ArrayList<>();
}
return userByIds.stream().map(User::converSimpleUser).collect(Collectors.toList());
}
}

Loading…
Cancel
Save