From 3f7a38cad1f1c2cb2b0857faaa0fb08a0afac314 Mon Sep 17 00:00:00 2001 From: Gary Date: Sat, 16 Mar 2024 23:15:55 +0800 Subject: [PATCH] 1.remove redundant commment method --- .../music/controller/JournalController.java | 12 --- .../luoo/music/dto/mapper/CommentMapper.java | 91 +++++-------------- .../luoo/music/dto/mapper/JournalMapper.java | 22 ----- .../luoo/music/service/JournalService.java | 6 +- 4 files changed, 27 insertions(+), 104 deletions(-) diff --git a/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java b/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java index 5551d87..de0c1dc 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java @@ -246,18 +246,6 @@ public class JournalController { private boolean isLatest10(String journalNo) { return journalService.isLatest10ByJournalNo(journalNo); } - @ApiOperation(value = "3.2 根据期刊id查询评论信息") - @GetMapping("/comment/{id}") - @GlobalInterceptor - public Result findCommentById( - @RequestHeader(value = "Authorization", required = false) String authorization, - @PathVariable @VerifyParam(required = true) String id) { - UserLoginDto user = jwtUtil.getUserLoginDto(authorization); - if (null == user && !journalService.isLatest10ByJournalId(id)) { - return Result.unauthorized(null); - } - return Result.success(CommentMapper.getCommentDTO(id,mongoTemplate,commentDao,redisTemplate)); - } @ApiOperation(value = "4.获取期刊筛选条件") @GetMapping("/filter") 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 2268bdf..8c220dd 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 @@ -1,15 +1,12 @@ 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 constants.Constants; import net.oschina.j2cache.CacheChannel; -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; @@ -22,79 +19,41 @@ import com.luoo.music.dto.response.TotalCommentVo; import com.luoo.music.pojo.Comment; public class CommentMapper { - public static CommentDTO getCommentDTO(String journalId, MongoTemplate mongoTemplate, CommentDao commentDao,RedisTemplate redisTemplate) { - /** - * 显示期刊点赞最多5条评论,该五条评论随机显示,每次进入刷新新,超过部分显示为“...”点击评论进入期刊详情页并自动定位到该评论,点击用户头像同上,若为空数据则显示“暂无评论,快去抢沙发吧! - */ - CommentDTO commentDTO=new CommentDTO(); - - List commentList =commentDao.findTop2ByJournalIdOrderByThumbupCountDesc(journalId); - commentDTO.setCommentList(commentList); - /** - * 获取期刊总评论数 一级评论数加上父评论的总回复数 - */ - - String totalString = "0"; - - - 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+"; - } + public static List getList(String journalId, CommentDao commentDao, UserClient userClient) { + List commentList = commentDao.findTop2ByJournalIdOrderByThumbupCountDesc(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.setTotalCommentReply(totalString); - commentDTO.setTotalCommentReplyInt(total); - return commentDTO; + return commentList; } - public static List getList(String journalId,CommentDao commentDao,UserClient userClient){ - List commentList =commentDao.findTop2ByJournalIdOrderByThumbupCountDesc(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()); - } - return commentList; - } - - public static CommentDTO getCommentDTO(String journalId, MongoTemplate mongoTemplate, CommentDao commentDao, RedisTemplate redisTemplate, UserClient userClient, CacheChannel cacheChannel) { + public static CommentDTO getCommentDTO(String journalId, MongoTemplate mongoTemplate, CommentDao commentDao, + RedisTemplate redisTemplate, UserClient userClient, CacheChannel cacheChannel) { /** * 显示期刊点赞最多5条评论,该五条评论随机显示,每次进入刷新新,超过部分显示为“...”点击评论进入期刊详情页并自动定位到该评论,点击用户头像同上,若为空数据则显示“暂无评论,快去抢沙发吧! */ - CommentDTO commentDTO=new CommentDTO(); - - - - - - commentDTO.setCommentList((List) cacheChannel.get(Constants.J2CACHE_REGION_JOURNAL_HOT_COMMENTS,journalId, key-> CommentMapper.getList(journalId,commentDao,userClient),false).getValue()); + CommentDTO commentDTO = new CommentDTO(); + + commentDTO + .setCommentList( + (List) cacheChannel + .get(Constants.J2CACHE_REGION_JOURNAL_HOT_COMMENTS, journalId, + key -> CommentMapper.getList(journalId, commentDao, userClient), false) + .getValue()); /** * 获取期刊总评论数 一级评论数加上父评论的总回复数 */ String totalString = "0"; - totalString = "0"; Criteria criteria = Criteria.where("journalId").is(journalId); Aggregation agg = Aggregation.newAggregation(Aggregation.match(criteria), // 匹配条件 @@ -109,7 +68,7 @@ public class CommentMapper { } if (null != totalCommentVo) { total = total + totalCommentVo.getTotalComment(); - totalString = total +""; + totalString = total + ""; if (total > 99) { totalString = "99+"; } @@ -119,6 +78,4 @@ public class CommentMapper { 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 e698fad..da1f428 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 @@ -55,29 +55,7 @@ public class JournalMapper { return journalRespDTO; } - public static JournalRespDTO getJournalRespDTO(Journal journal, Set journalCollectSet, - MongoTemplate mongoTemplate, RedisTemplate redisTemplate, CommentDao commentDao) { - JournalRespDTO journalRespDTO = getJournalRespDTO(journal, journalCollectSet); - CommentDTO commentDTO = CommentMapper.getCommentDTO(journalRespDTO.getId(), mongoTemplate, commentDao, - redisTemplate); - journalRespDTO.setCommentList(commentDTO.getCommentList()); - journalRespDTO.setTotalCommentReply(commentDTO.getTotalCommentReply()); - journalRespDTO.setTotalCommentReplyInt(commentDTO.getTotalCommentReplyInt()); - return journalRespDTO; - } - - public static JournalRespDTO getJournalRespDTO(Journal journal, - MongoTemplate mongoTemplate, RedisTemplate redisTemplate, CommentDao commentDao) { - JournalRespDTO journalRespDTO = getJournalRespDTO(journal); - CommentDTO commentDTO = CommentMapper.getCommentDTO(journalRespDTO.getId(), mongoTemplate, commentDao, - redisTemplate); - journalRespDTO.setCommentList(commentDTO.getCommentList()); - journalRespDTO.setTotalCommentReply(commentDTO.getTotalCommentReply()); - journalRespDTO.setTotalCommentReplyInt(commentDTO.getTotalCommentReplyInt()); - - return journalRespDTO; - } public static JournalRespDTO getJournalRespDTO(Journal journal, MongoTemplate mongoTemplate, RedisTemplate redisTemplate, CommentDao commentDao, UserClient userClient, CacheChannel cacheChannel) { JournalRespDTO journalRespDTO = getJournalRespDTO(journal); 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 e1b4a9c..f3331f0 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 @@ -362,7 +362,7 @@ public class JournalService { private void updateComment(JournalRespDTO journalRespDTO) { CommentDTO commentDTO = CommentMapper.getCommentDTO(journalRespDTO.getId(), mongoTemplate, commentDao, - redisTemplate); + redisTemplate,userClient,cacheChannel); journalRespDTO.setCommentList(commentDTO.getCommentList()); journalRespDTO.setTotalCommentReply(commentDTO.getTotalCommentReply()); journalRespDTO.setTotalCommentReplyInt(commentDTO.getTotalCommentReplyInt()); @@ -391,7 +391,7 @@ public class JournalService { if(null==journal) { return null; } - return JournalMapper.getJournalRespDTO(journal,mongoTemplate,redisTemplate,commentDao); + return JournalMapper.getJournalRespDTO(journal,mongoTemplate,redisTemplate,commentDao,userClient,cacheChannel); } public JournalRespDTO queryJournalByJournalNo(String journalNo) { @@ -403,7 +403,7 @@ public class JournalService { if(null==journal) { return null; } - return JournalMapper.getJournalRespDTO(journal,mongoTemplate,redisTemplate,commentDao); + return JournalMapper.getJournalRespDTO(journal,mongoTemplate,redisTemplate,commentDao,userClient,cacheChannel); } public JournalFilterDTO getJournalFilterDTO() {