From 749e0469f954c6eb82f6da68399e5c18da28bf9c Mon Sep 17 00:00:00 2001 From: Gary Date: Tue, 6 Feb 2024 16:45:46 +0800 Subject: [PATCH] 1.add comment dto --- .../music/controller/JournalController.java | 18 +++++++ .../luoo/music/controller/SongController.java | 2 +- .../java/com/luoo/music/dao/JournalDao.java | 5 +- .../luoo/music/dto/mapper/CommentMapper.java | 45 +++++++++++++++++ .../luoo/music/dto/mapper/JournalMapper.java | 48 ++++--------------- .../luoo/music/dto/response/CommentDTO.java | 16 +++++++ .../luoo/music/service/JournalService.java | 9 +++- 7 files changed, 99 insertions(+), 44 deletions(-) create mode 100644 luoo_music/src/main/java/com/luoo/music/dto/mapper/CommentMapper.java create mode 100644 luoo_music/src/main/java/com/luoo/music/dto/response/CommentDTO.java 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 1e2571e..4b5cf48 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 @@ -7,8 +7,10 @@ import java.util.Set; import java.util.stream.Collectors; import com.luoo.music.dao.CommentDao; +import com.luoo.music.dto.mapper.CommentMapper; import com.luoo.music.dto.mapper.JournalMapper; import com.luoo.music.dto.request.JournalQueryReq; +import com.luoo.music.dto.response.CommentDTO; import com.luoo.music.dto.response.JournalFilterDTO; import com.luoo.music.dto.response.JournalRespDTO; import com.luoo.music.dto.response.TagDTO; @@ -113,6 +115,9 @@ public class JournalController { @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); + } Journal journal = journalService.findById(id); JournalRespDTO journalRespDTO=JournalMapper.getJournalRespDTO(journal, Collections.emptySet(),mongoTemplate,commentDao); boolean isCollect=null == user ? false:userCollectInfoService.isCollect(user.getUserId(),id, CollectTypeEnum.JOURNAL); @@ -120,6 +125,19 @@ public class JournalController { return Result.success(journalRespDTO); } + @ApiOperation(value = "3.1 根据期刊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)); + } + @ApiOperation(value = "4.获取期刊筛选条件") @GetMapping("/filter") @GlobalInterceptor(checkAppUserLogin = true) diff --git a/luoo_music/src/main/java/com/luoo/music/controller/SongController.java b/luoo_music/src/main/java/com/luoo/music/controller/SongController.java index ae9bdb2..3c13b90 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/SongController.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/SongController.java @@ -74,7 +74,7 @@ public class SongController { } private boolean isLatest10(String journalNo) { - return journalService.isLatest10(journalNo); + return journalService.isLatest10ByJournalNo(journalNo); } @ApiOperation(value = "2.查询收藏歌曲信息") diff --git a/luoo_music/src/main/java/com/luoo/music/dao/JournalDao.java b/luoo_music/src/main/java/com/luoo/music/dao/JournalDao.java index f46a7e5..5e377b1 100644 --- a/luoo_music/src/main/java/com/luoo/music/dao/JournalDao.java +++ b/luoo_music/src/main/java/com/luoo/music/dao/JournalDao.java @@ -44,7 +44,10 @@ public interface JournalDao extends JpaRepository,JpaSpecificati List orderByField(List objectIds); @Query(value = "select 1 from tb_journal where ?1 in (select journal_no_tmp.journal_no from (select * from tb_journal order by ABS(journal_no) desc limit 10) as journal_no_tmp) limit 1", nativeQuery = true) - Integer isLatest10(String journalNo); + Integer isLatest10ByJournalNo(String journalNo); + + @Query(value = "select 1 from tb_journal where ?1 in (select journal_no_tmp.id from (select * from tb_journal order by ABS(journal_no) desc limit 10) as journal_no_tmp) limit 1", nativeQuery = true) + Integer isLatest10ByJournalId(String journalId); @Modifying @Transactional 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 new file mode 100644 index 0000000..559ad45 --- /dev/null +++ b/luoo_music/src/main/java/com/luoo/music/dto/mapper/CommentMapper.java @@ -0,0 +1,45 @@ +package com.luoo.music.dto.mapper; + +import java.util.List; + +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.aggregation.Aggregation; +import org.springframework.data.mongodb.core.aggregation.AggregationResults; +import org.springframework.data.mongodb.core.query.Criteria; + +import com.luoo.music.dao.CommentDao; +import com.luoo.music.dto.response.CommentDTO; +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) { + CommentDTO commentDTO=new CommentDTO(); + List commentList = commentDao.findTop5ByJournalIdOrderByThumbupCountDesc(journalId); + commentDTO.setCommentList(commentList); + + /** + * 获取期刊总评论数 一级评论数加上父评论的总回复数 + */ + 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(); + commentDTO.setTotalCommentReply(total + ""); + if (total > 99) { + commentDTO.setTotalCommentReply("99+"); + } + } + + 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 0a3304c..b964019 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 @@ -15,16 +15,11 @@ import java.util.stream.Collectors; import org.springframework.core.io.ClassPathResource; import org.springframework.data.mongodb.core.MongoTemplate; -import org.springframework.data.mongodb.core.aggregation.Aggregation; -import org.springframework.data.mongodb.core.aggregation.AggregationResults; -import org.springframework.data.mongodb.core.query.Criteria; - import com.apifan.common.random.RandomSource; import com.apifan.common.random.entity.Poem; import com.luoo.music.dao.CommentDao; +import com.luoo.music.dto.response.CommentDTO; import com.luoo.music.dto.response.JournalRespDTO; -import com.luoo.music.dto.response.TotalCommentVo; -import com.luoo.music.pojo.Comment; import com.luoo.music.pojo.Journal; import constants.Constants; import enums.DateTimePatternEnum; @@ -56,9 +51,9 @@ public class JournalMapper { } public static JournalRespDTO getJournalRespDTO(Journal journal) { - return getJournalRespDTO(journal,Collections.emptySet()); + return getJournalRespDTO(journal, Collections.emptySet()); } - + public static JournalRespDTO getJournalRespDTO(Journal journal, Set journalCollectSet) { JournalRespDTO journalRespDTO = new JournalRespDTO(); journalRespDTO.setId(journal.getId()); @@ -82,40 +77,13 @@ public class JournalMapper { } return journalRespDTO; } + public static JournalRespDTO getJournalRespDTO(Journal journal, Set journalCollectSet, MongoTemplate mongoTemplate, CommentDao commentDao) { - JournalRespDTO journalRespDTO = getJournalRespDTO(journal,journalCollectSet); - - /** - * 显示期刊点赞最多5条评论,该五条评论随机显示,每次进入刷新新,超过部分显示为“...”点击评论进入期刊详情页并自动定位到该评论,点击用户头像同上,若为空数据则显示“暂无评论,快去抢沙发吧! - */ - - List commentList = commentDao.findTop5ByJournalIdOrderByThumbupCountDesc(journal.getId()); - - journalRespDTO.setCommentList(commentList); - - /** - * 获取期刊总评论数 一级评论数加上父评论的总回复数 - */ - Criteria criteria = Criteria.where("journalId").is(journal.getId()); - 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(); - journalRespDTO.setTotalCommentReply("0"); - List list = commentDao.findByJournalId(journal.getId()); - int total = 0; - if (null != list && list.size() > 0) { - total = list.size(); - } - if (null != totalCommentVo) { - total = total + totalCommentVo.getTotalComment(); - journalRespDTO.setTotalCommentReply(total + ""); - if (total > 99) { - journalRespDTO.setTotalCommentReply("99+"); - } - } - + JournalRespDTO journalRespDTO = getJournalRespDTO(journal, journalCollectSet); + CommentDTO commentDTO = CommentMapper.getCommentDTO(journalRespDTO.getId(), mongoTemplate, commentDao); + journalRespDTO.setCommentList(commentDTO.getCommentList()); + journalRespDTO.setTotalCommentReply(commentDTO.getTotalCommentReply()); return journalRespDTO; } diff --git a/luoo_music/src/main/java/com/luoo/music/dto/response/CommentDTO.java b/luoo_music/src/main/java/com/luoo/music/dto/response/CommentDTO.java new file mode 100644 index 0000000..619b4c4 --- /dev/null +++ b/luoo_music/src/main/java/com/luoo/music/dto/response/CommentDTO.java @@ -0,0 +1,16 @@ +package com.luoo.music.dto.response; + +import java.util.List; + +import com.luoo.music.pojo.Comment; + +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +@Data +public class CommentDTO { + @ApiModelProperty(value = "期刊评论top5") + private List commentList; + + @ApiModelProperty(value = "期刊总评论数,大于99,显示99+") + private String totalCommentReply; +} 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 1816eaa..eeda372 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 @@ -213,8 +213,8 @@ public class JournalService { - public boolean isLatest10(String journalNo) { - return null!=journalDao.isLatest10(journalNo); + public boolean isLatest10ByJournalNo(String journalNo) { + return null!=journalDao.isLatest10ByJournalNo(journalNo); } public List orderByField(List objectIds) { @@ -281,4 +281,9 @@ public class JournalService { // TODO Auto-generated method stub return Collections.emptyList(); } + + + public boolean isLatest10ByJournalId(String journalId) { + return null!=journalDao.isLatest10ByJournalId(journalId); + } }