From d1feac665060b512f416b62f46db4e147e7962a7 Mon Sep 17 00:00:00 2001 From: wangqing <408564126@qq.com> Date: Fri, 1 Mar 2024 16:10:04 +0800 Subject: [PATCH] fix: comment with realtime user info --- luoo_comment/pom.xml | 9 +- .../comment/controller/CommentController.java | 42 ++++-- .../com/luoo/comment/dao/UserInfoDao.java | 36 +++++ .../com/luoo/comment/pojo/CommentResp.java | 2 + .../java/com/luoo/comment/pojo/UserInfo.java | 135 ++++++++++++++++++ .../luoo/comment/service/UserInfoService.java | 38 +++++ .../luoo/user/controller/MyController.java | 20 ++- 7 files changed, 270 insertions(+), 12 deletions(-) create mode 100644 luoo_comment/src/main/java/com/luoo/comment/dao/UserInfoDao.java create mode 100644 luoo_comment/src/main/java/com/luoo/comment/pojo/UserInfo.java create mode 100644 luoo_comment/src/main/java/com/luoo/comment/service/UserInfoService.java diff --git a/luoo_comment/pom.xml b/luoo_comment/pom.xml index b92079d..2cbbacf 100644 --- a/luoo_comment/pom.xml +++ b/luoo_comment/pom.xml @@ -27,7 +27,14 @@ mongodb-plus-spring-boot-starter 1.0.0.RELEASE - + + org.springframework.boot + spring-boot-starter-data-jpa + + + mysql + mysql-connector-java + org.springframework.boot spring-boot-starter-data-redis 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 b79a084..6099c64 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 @@ -8,6 +8,7 @@ import com.luoo.comment.pojo.*; import com.luoo.comment.service.CommentService; import com.luoo.comment.service.ComplaintService; import com.luoo.comment.service.LikeService; +import com.luoo.comment.service.UserInfoService; import com.luoo.comment.util.IpUtil; import constants.Constants; import controller.BaseController; @@ -18,6 +19,7 @@ import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; +import org.apache.commons.lang.StringUtils; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -66,6 +68,9 @@ public class CommentController extends BaseController { @Autowired private ComplaintService complaintService; + @Autowired + private UserInfoService userInfoService; + @Autowired private IdWorker idWorker; @@ -112,12 +117,12 @@ public class CommentController extends BaseController { UserLoginDto userLoginDto = getUserLoginDto(authorization); if (null == userLoginDto) { Page pageList = commentService.findByJournalId(journalId,page,size); - List list =pageList.stream().map(x->getCommentResp(x,"")).collect(Collectors.toList()); + List list =pageList.stream().parallel().map(x->getCommentResp(x,"")).collect(Collectors.toList()); return Result.success(new PageResult(Long.valueOf(list.size()),list)); } String userId = userLoginDto.getUserId(); Page pageList = commentService.findHotByJournalId(journalId,page,size); - List list =pageList.stream().map(x->getCommentResp(x,userId)).collect(Collectors.toList()); + List list =pageList.stream().parallel().map(x->getCommentResp(x,userId)).collect(Collectors.toList()); return Result.success(new PageResult(Long.valueOf(list.size()),list)); } @@ -131,12 +136,12 @@ public class CommentController extends BaseController { UserLoginDto userLoginDto = getUserLoginDto(authorization); if (null == userLoginDto) { Page pageList = commentService.findByJournalId(journalId,page,size); - List list =pageList.stream().map(x->getCommentResp(x,"")).collect(Collectors.toList()); + List list =pageList.stream().parallel().map(x->getCommentResp(x,"")).collect(Collectors.toList()); return Result.success(new PageResult(Long.valueOf(list.size()),list)); } String userId = userLoginDto.getUserId(); Page pageList = commentService.findNewByJournalId(journalId,page,size); - List list =pageList.stream().map(x->getCommentResp(x,userId)).collect(Collectors.toList()); + List list =pageList.stream().parallel().map(x->getCommentResp(x,userId)).collect(Collectors.toList()); return Result.success(new PageResult(Long.valueOf(list.size()),list)); } @@ -148,9 +153,21 @@ public class CommentController extends BaseController { // 创建查询对象并应用查询条件 Query query = new Query(criteria); boolean isExists = mongoTemplate.exists(query, PublicationLike.class); - comment.setHaveThumbup(isExists); //是否已点赞 + comment.setHaveThumbup(isExists); // + + UserInfo commentUserInfo = userInfoService.findById(comment.getUserId()); + comment.setNickName(commentUserInfo.getNickName()); + comment.setAvatar(Constants.RESOURCE_PREFIX+commentUserInfo.getAvatar()); + if(StringUtils.isNotEmpty(comment.getReplyToUserId())){ + UserInfo replyToUserInfo = userInfoService.findById(comment.getReplyToUserId()); + comment.setReplyToNickname(replyToUserInfo.getNickName()); + } CommentResp commentResp = new CommentResp(); BeanUtils.copyProperties(comment,commentResp); + if(comment.getCommentCount()>0){ + Comment topComment = commentService.findByParentId(comment.get_id(),1,1).getContent().get(0); + commentResp.setTopChildrenComment(topComment); + } commentResp.setThumbupCountString(comment.getThumbupCount()+""); commentResp.setThumbupCountInt(comment.getThumbupCount()); if(comment.getThumbupCount()>999) { @@ -167,8 +184,15 @@ public class CommentController extends BaseController { @GetMapping("/{commentId}") - public Result findById(@PathVariable String commentId){ - return Result.success(commentService.findById(commentId)); + public Result findById(@PathVariable String commentId,@RequestHeader(value = "Authorization", required = false) String authorization){ + //验证是否登录,并且拿到ID + UserLoginDto userLoginDto = getUserLoginDto(authorization); + Comment comment = commentService.findById(commentId); + if (null == userLoginDto) { + CommentResp commentResp = getCommentResp(comment,""); + return Result.success(commentResp); + } + return Result.success(getCommentResp(comment,userLoginDto.getUserId())); } @@ -229,12 +253,12 @@ public class CommentController extends BaseController { UserLoginDto userLoginDto = getUserLoginDto(authorization); if (null == userLoginDto) { Page pageList = commentService.findByParentId(parentId,page,size); - List list =pageList.stream().map(x->getCommentResp(x,"")).collect(Collectors.toList()); + List list =pageList.stream().parallel().map(x->getCommentResp(x,"")).collect(Collectors.toList()); return Result.success(new PageResult(Long.valueOf(list.size()),list)); } String userId = userLoginDto.getUserId(); Page pageList = commentService.findByParentId(parentId,page,size); - List list =pageList.stream().map(x->getCommentResp(x,userId)).collect(Collectors.toList()); + List list =pageList.stream().parallel().map(x->getCommentResp(x,userId)).collect(Collectors.toList()); return Result.success(new PageResult(Long.valueOf(list.size()),list)); } diff --git a/luoo_comment/src/main/java/com/luoo/comment/dao/UserInfoDao.java b/luoo_comment/src/main/java/com/luoo/comment/dao/UserInfoDao.java new file mode 100644 index 0000000..97f4216 --- /dev/null +++ b/luoo_comment/src/main/java/com/luoo/comment/dao/UserInfoDao.java @@ -0,0 +1,36 @@ +package com.luoo.comment.dao; + +import client.vo.SimpleUser; +import com.luoo.comment.pojo.UserInfo; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; + +import java.util.List; + +public interface UserInfoDao extends JpaRepository, JpaSpecificationExecutor { + public UserInfo findByMobile(String mobile); + + @Modifying + @Query(value = "update tb_user_info set follow_count=follow_count+? where id = ?", nativeQuery = true) + void updatefollowcount(int x, String appUserInfoId); + + @Modifying + @Query(value = "update tb_user_info set fans_count=fans_count+? where id = ?", nativeQuery = true) + void updatefanscount(int x, String friendid); + + public long countByNickName(String nickName); + + @Query(value = "select * from tb_user_info where id in ?1 order by field(id,?1)", nativeQuery = true) + public List orderByField(List idList); + + @Query(value = "select new client.vo.SimpleUser(id, nickName) from UserInfo where id in ?1 order by field(id,?1)") + public List getSimpleUserOrderByField(List idList); + + @Query(value = "select * from tb_user_info where id = ?1", nativeQuery = true) + public UserInfo getById(String id); + + @Query(value = "select * from tb_user_info where badges like '%1%' ", nativeQuery = true) + public List getThanks(); +} diff --git a/luoo_comment/src/main/java/com/luoo/comment/pojo/CommentResp.java b/luoo_comment/src/main/java/com/luoo/comment/pojo/CommentResp.java index 3e60906..d3ab8fd 100644 --- a/luoo_comment/src/main/java/com/luoo/comment/pojo/CommentResp.java +++ b/luoo_comment/src/main/java/com/luoo/comment/pojo/CommentResp.java @@ -57,4 +57,6 @@ public class CommentResp implements Serializable { private String replyToUserId; private String replyToNickname; + + private Comment topChildrenComment; } diff --git a/luoo_comment/src/main/java/com/luoo/comment/pojo/UserInfo.java b/luoo_comment/src/main/java/com/luoo/comment/pojo/UserInfo.java new file mode 100644 index 0000000..4b1cb47 --- /dev/null +++ b/luoo_comment/src/main/java/com/luoo/comment/pojo/UserInfo.java @@ -0,0 +1,135 @@ +package com.luoo.comment.pojo; + +import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.Getter; +import lombok.Setter; +import org.springframework.format.annotation.DateTimeFormat; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import java.io.Serializable; +import java.util.Date; + + +@Getter +@Setter +@Entity +@Table(name="tb_user_info") +public class UserInfo implements Serializable { + private static final long serialVersionUID = 1L; + + /** + * 用户ID + */ + @Id + private String id; + + /** + * 手机号码 + */ + private String mobile; + + /** + * 昵称 + */ + private String nickName; + + /** + * 头像 + */ + private String avatar; + /** + * 个性签名 + */ + private String signature; + + /** + * 密码 + */ + private String password; + /** + * 邮箱 + */ + private String email; + /** + * 性别 0:男 1:女 2:保密 + */ + private Integer sex; + /** + * 出生年月日 + */ + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private Date birthday; + /** + * 粉丝数 + */ + private int fansCount; + /** + * 关注数 + */ + private int followCount; + /** + * 获赞数 + */ + private int thumbUpCount; + /** + * 喜欢歌曲数 + */ + private int songCount; + /** + * 收藏期刊数 + */ + private int journalCount; + /** + * 获得评论数 + */ + private int commentReplyCount; + /** + * 创建时间 + */ + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private Date joinTime; + + /** + * 修改时间 + */ + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private Date modifyTime; + + /** + * 最后登录时间 + */ + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private Date lastLoginTime; + + /** + * 最后使用的设备ID + */ + private String lastUseDeviceId; + + /** + * 手机品牌 + */ + private String lastUseDeviceBrand; + + /** + * 最后登录IP + */ + private String lastLoginIp; + + /** + * 0:禁用 1:正常 + */ + private int status; + /** + * 0:禁用 1:正常 + */ + private int onlineStatus; + + private String badges; +} diff --git a/luoo_comment/src/main/java/com/luoo/comment/service/UserInfoService.java b/luoo_comment/src/main/java/com/luoo/comment/service/UserInfoService.java new file mode 100644 index 0000000..e4ecbda --- /dev/null +++ b/luoo_comment/src/main/java/com/luoo/comment/service/UserInfoService.java @@ -0,0 +1,38 @@ +package com.luoo.comment.service; + + +import com.luoo.comment.dao.UserInfoDao; +import com.luoo.comment.pojo.UserInfo; + +import org.springframework.beans.factory.annotation.Autowired; + +import org.springframework.stereotype.Service; + + +/** + * 服务层 + * + * @author Administrator + * + */ +@Service +public class UserInfoService { + @Autowired + private UserInfoDao userInfoDao; + + + + + /** + * 根据ID查询实体 + * + * @param id + * @return + */ + public UserInfo findById(String id) { + return userInfoDao.getById(id); + } + + + +} diff --git a/luoo_user/src/main/java/com/luoo/user/controller/MyController.java b/luoo_user/src/main/java/com/luoo/user/controller/MyController.java index e2baf45..f18cfd4 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/MyController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/MyController.java @@ -336,12 +336,19 @@ public class MyController extends BaseController { String userId = userLoginDto.getUserId(); Page pageList = myService.getMyThumbupList(userId,page,size); + List list = pageList.stream().parallel().map(x->getPublicationLike(x)).collect(Collectors.toList()); - return Result.success(new PageResult(pageList.getTotalElements(),pageList.getContent())); + return Result.success(new PageResult(Long.valueOf(list.size()),list)); } + private PublicationLike getPublicationLike(PublicationLike publicationLike) { + UserInfo userInfo = userInfoService.findById(publicationLike.getUserId()); + publicationLike.setNickName(userInfo.getNickName()); + publicationLike.setAvatar(Constants.RESOURCE_PREFIX+userInfo.getAvatar()); + return publicationLike; + } // 查询我收到的评论 @ApiOperation(value = "10.查询我收到的评论分页列表", notes = "游客无法获取") @GetMapping("/myCommentReplyList/{page}/{size}") @@ -354,7 +361,16 @@ public class MyController extends BaseController { } String userId = userLoginDto.getUserId(); Page pageList = myService.getMyCommentReplyList(userId,page,size); - return Result.success(new PageResult(pageList.getTotalElements(),pageList.getContent())); + List list = pageList.stream().parallel().map(x->getCommentHis(x)).collect(Collectors.toList()); + return Result.success(new PageResult(Long.valueOf(list.size()),list)); + } + + private CommentHis getCommentHis(CommentHis commentHis) { + + UserInfo userInfo = userInfoService.findById(commentHis.getCommenterId()); + commentHis.setNickName(userInfo.getNickName()); + commentHis.setCommenterAvatar(Constants.RESOURCE_PREFIX+userInfo.getAvatar()); + return commentHis; } // 查询我收到的评论