diff --git a/luoo_music/src/main/java/com/luoo/music/client/UserClient.java b/luoo_music/src/main/java/com/luoo/music/client/UserClient.java index 70f9b8f..157cccc 100644 --- a/luoo_music/src/main/java/com/luoo/music/client/UserClient.java +++ b/luoo_music/src/main/java/com/luoo/music/client/UserClient.java @@ -1,6 +1,7 @@ package com.luoo.music.client; import api.Result; +import com.luoo.music.dto.response.UserInfoDTO; import com.luoo.music.pojo.UserInfo; import lombok.Data; import org.apache.commons.lang.StringUtils; @@ -42,4 +43,8 @@ public interface UserClient { private String id; private String loginname; } + + @GetMapping("/simple_user/user/{id}") + UserInfoDTO findByUserId(@PathVariable String id); + } diff --git a/luoo_music/src/main/java/com/luoo/music/dto/mapper/CommentMapper.java b/luoo_music/src/main/java/com/luoo/music/dto/mapper/CommentMapper.java index 9a53cad..a029272 100644 --- a/luoo_music/src/main/java/com/luoo/music/dto/mapper/CommentMapper.java +++ b/luoo_music/src/main/java/com/luoo/music/dto/mapper/CommentMapper.java @@ -2,8 +2,13 @@ package com.luoo.music.dto.mapper; import java.util.List; import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; +import com.luoo.music.client.UserClient; +import com.luoo.music.dto.response.UserInfoDTO; +import com.luoo.music.util.Constants; import org.apache.commons.lang.StringUtils; +import org.apache.tomcat.util.bcel.classfile.Constant; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.aggregation.Aggregation; import org.springframework.data.mongodb.core.aggregation.AggregationResults; @@ -89,4 +94,93 @@ public class CommentMapper { commentDTO.setTotalCommentReplyInt(total); return commentDTO; } + + public static CommentDTO getCommentDTO(String journalId, MongoTemplate mongoTemplate, CommentDao commentDao, RedisTemplate redisTemplate, UserClient userClient) { + /** + * 显示期刊点赞最多5条评论,该五条评论随机显示,每次进入刷新新,超过部分显示为“...”点击评论进入期刊详情页并自动定位到该评论,点击用户头像同上,若为空数据则显示“暂无评论,快去抢沙发吧! + */ + CommentDTO commentDTO=new CommentDTO(); +// List commentList = (List) redisTemplate.opsForValue().get("JOURNAL_TOP5_COMMENT___"+journalId); +// if (null!= commentList) { +// commentDTO.setCommentList(commentList); +// } else { +// commentList = commentDao.findTop5ByJournalIdOrderByThumbupCountDesc(journalId); +// commentDTO.setCommentList(commentList); +// redisTemplate.opsForValue().set("JOURNAL_TOP5_COMMENT___"+journalId,commentList,6, TimeUnit.HOURS); +// } + + List commentList =commentDao.findTop5ByJournalIdOrderByThumbupCountDesc(journalId); + if(commentList.size()>0){ + commentList= commentList.stream().map(comment -> { + UserInfoDTO userInfoDTO =userClient.findByUserId(comment.getUserId()); + if(null != userInfoDTO){ + comment.setNickName(userInfoDTO.getNickName()); + comment.setAvatar(Constants.RESOURCE_PREFIX+userInfoDTO.getAvatar()); + } + + return comment; + }).collect(Collectors.toList()); + } + + commentDTO.setCommentList(commentList); + /** + * 获取期刊总评论数 一级评论数加上父评论的总回复数 + */ + +// String totalString = (String) redisTemplate.opsForValue().get("JOURNAL_TATAL_COMMENT_COUNT__"+journalId); + String totalString = "0"; + +// if (StringUtils.isNotBlank(totalString)) { +// commentDTO.setTotalCommentReply(totalString); +// }else { +//// totalString = "0"; +//// Criteria criteria = Criteria.where("journalId").is(journalId); +//// Aggregation agg = Aggregation.newAggregation(Aggregation.match(criteria), // 匹配条件 +//// Aggregation.group().sum("commentCount").as("totalComment")); +//// AggregationResults results = mongoTemplate.aggregate(agg, "comment", TotalCommentVo.class); +//// TotalCommentVo totalCommentVo = results.getUniqueMappedResult(); +//// commentDTO.setTotalCommentReply("0"); +//// List list = commentDao.findByJournalId(journalId); +//// int total = 0; +//// if (null != list && list.size() > 0) { +//// total = list.size(); +//// } +//// if (null != totalCommentVo) { +//// total = total + totalCommentVo.getTotalComment(); +//// totalString = total +""; +//// if (total > 99) { +//// totalString = "99+"; +//// } +//// } +//// commentDTO.setTotalCommentReply(totalString); +//// commentDTO.setTotalCommentReplyInt(total); +//// redisTemplate.opsForValue().set("JOURNAL_TATAL_COMMENT_COUNT__"+journalId,totalString,6,TimeUnit.HOURS); +// } + + totalString = "0"; + Criteria criteria = Criteria.where("journalId").is(journalId); + Aggregation agg = Aggregation.newAggregation(Aggregation.match(criteria), // 匹配条件 + Aggregation.group().sum("commentCount").as("totalComment")); + AggregationResults results = mongoTemplate.aggregate(agg, "comment", TotalCommentVo.class); + TotalCommentVo totalCommentVo = results.getUniqueMappedResult(); + commentDTO.setTotalCommentReply("0"); + List list = commentDao.findByJournalId(journalId); + int total = 0; + if (null != list && list.size() > 0) { + total = list.size(); + } + if (null != totalCommentVo) { + total = total + totalCommentVo.getTotalComment(); + totalString = total +""; + if (total > 99) { + totalString = "99+"; + } + } + commentDTO.setTotalCommentReply(totalString); + commentDTO.setTotalCommentReplyInt(total); + return commentDTO; + } + + + } diff --git a/luoo_music/src/main/java/com/luoo/music/dto/mapper/JournalMapper.java b/luoo_music/src/main/java/com/luoo/music/dto/mapper/JournalMapper.java index 621a886..2bdae26 100644 --- a/luoo_music/src/main/java/com/luoo/music/dto/mapper/JournalMapper.java +++ b/luoo_music/src/main/java/com/luoo/music/dto/mapper/JournalMapper.java @@ -13,6 +13,7 @@ import java.util.Map; import java.util.Set; import java.util.stream.Collectors; +import com.luoo.music.client.UserClient; import org.springframework.core.io.ClassPathResource; import org.springframework.data.mongodb.core.MongoTemplate; import com.apifan.common.random.RandomSource; @@ -76,6 +77,18 @@ public class JournalMapper { return journalRespDTO; } + public static JournalRespDTO getJournalRespDTO(Journal journal, + MongoTemplate mongoTemplate, RedisTemplate redisTemplate, CommentDao commentDao, UserClient userClient) { + JournalRespDTO journalRespDTO = getJournalRespDTO(journal); + CommentDTO commentDTO = CommentMapper.getCommentDTO(journalRespDTO.getId(), mongoTemplate, commentDao, + redisTemplate,userClient); + journalRespDTO.setCommentList(commentDTO.getCommentList()); + journalRespDTO.setTotalCommentReply(commentDTO.getTotalCommentReply()); + journalRespDTO.setTotalCommentReplyInt(commentDTO.getTotalCommentReplyInt()); + + return journalRespDTO; + } + private static String getPublishDate(Journal journal) { LocalDateTime date = null == journal.getPubTime() ? journal.getCreateTime() : journal.getPubTime(); diff --git a/luoo_music/src/main/java/com/luoo/music/dto/response/UserInfoDTO.java b/luoo_music/src/main/java/com/luoo/music/dto/response/UserInfoDTO.java new file mode 100644 index 0000000..71bd2c4 --- /dev/null +++ b/luoo_music/src/main/java/com/luoo/music/dto/response/UserInfoDTO.java @@ -0,0 +1,129 @@ +package com.luoo.music.dto.response; + +import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.Data; +import org.springframework.format.annotation.DateTimeFormat; + +import javax.persistence.Id; +import java.util.Date; + + +@Data +public class UserInfoDTO { + + 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_music/src/main/java/com/luoo/music/service/JournalService.java b/luoo_music/src/main/java/com/luoo/music/service/JournalService.java index 3b402dd..7884b48 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/JournalService.java +++ b/luoo_music/src/main/java/com/luoo/music/service/JournalService.java @@ -9,6 +9,7 @@ import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; import api.Result; +import com.luoo.music.client.UserClient; import constants.Constants; import enums.JournalPublishEnum; import enums.JournalStatusEnum; @@ -70,6 +71,9 @@ public class JournalService { @Autowired private TagService tagService; + + @Autowired + private UserClient userClient; /** * 查询全部列表 @@ -373,7 +377,7 @@ public class JournalService { private List getJournalRespDTOWithComment(JournalQueryReq queryReq) { List pageList = queryPage(queryReq); - return pageList.stream().map(journal -> JournalMapper.getJournalRespDTO(journal,mongoTemplate,redisTemplate,commentDao)).collect(Collectors.toList()); + return pageList.stream().map(journal -> JournalMapper.getJournalRespDTO(journal,mongoTemplate,redisTemplate,commentDao,userClient)).collect(Collectors.toList()); } diff --git a/luoo_user/src/main/java/com/luoo/user/controller/SimpleUserController.java b/luoo_user/src/main/java/com/luoo/user/controller/SimpleUserController.java index 4c4aeb6..6c709c0 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/SimpleUserController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/SimpleUserController.java @@ -69,4 +69,12 @@ public class SimpleUserController { List idList=Arrays.stream(ids.split(",")).map(String::trim).collect(Collectors.toList()); return Result.success(userInfoService.getSimpleUserOrderByField(idList)); } + + + @GetMapping("/user/{id}") + public UserInfo findByUserId(@PathVariable String id) { + UserInfo userInfo = userInfoService.findById(id); + return userInfo; + } + }