From 8167f62b08ac89361e38deb5d1fcd79193c93c51 Mon Sep 17 00:00:00 2001 From: Gary Date: Wed, 7 Feb 2024 02:11:59 +0800 Subject: [PATCH] 1.implement recomend journal interface --- .../music/controller/JournalController.java | 36 ++++++++- .../music/controller/SearchController.java | 7 +- .../java/com/luoo/music/dao/JournalDao.java | 3 + .../com/luoo/music/dao/JournalTagDao.java | 4 + .../main/java/com/luoo/music/dao/TagDao.java | 4 + .../luoo/music/dto/mapper/CommentMapper.java | 60 +++++++++----- .../luoo/music/dto/mapper/JournalMapper.java | 79 ++++--------------- .../luoo/music/service/JournalService.java | 13 +++ .../com/luoo/music/service/TagService.java | 9 +++ 9 files changed, 126 insertions(+), 89 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 1c0c2fc..eaad5a1 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 @@ -1,8 +1,10 @@ package com.luoo.music.controller; +import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.stream.Collectors; @@ -84,7 +86,8 @@ public class JournalController { List ids=pageList.stream().map(Journal::getId).collect(Collectors.toList()); Set journalCollectSet = null == user ? Collections.emptySet() : userCollectInfoService.getCollectSet(user.getUserId(),ids, CollectTypeEnum.JOURNAL); - List list = pageList.stream().map(a -> JournalMapper.getJournalRespDTO(a, journalCollectSet,mongoTemplate,redisTemplate,commentDao)) + Map> tagMap=tagService.getTagMap(ids); + List list = pageList.stream().map(a -> JournalMapper.getJournalRespDTO(a, journalCollectSet,mongoTemplate,redisTemplate,commentDao,tagMap)) .collect(Collectors.toList()); return Result.success(new PageResult(Long.valueOf(list.size()), list)); } @@ -107,7 +110,8 @@ public class JournalController { } List pageList = journalService.orderByField(objectIds); Set journalCollectSet = new HashSet<>(objectIds); - List list = pageList.stream().map(a -> JournalMapper.getJournalRespDTO(a, journalCollectSet,mongoTemplate,redisTemplate,commentDao)) + Map> tagMap=tagService.getTagMap(objectIds); + List list = pageList.stream().map(a -> JournalMapper.getJournalRespDTO(a, journalCollectSet,mongoTemplate,redisTemplate,commentDao,tagMap)) .collect(Collectors.toList()); return Result.success(new PageResult(Long.valueOf(list.size()), list)); } @@ -123,7 +127,8 @@ public class JournalController { return Result.unauthorized(null); } Journal journal = journalService.findById(id); - JournalRespDTO journalRespDTO=JournalMapper.getJournalRespDTO(journal, Collections.emptySet(),mongoTemplate,redisTemplate,commentDao); + Map> tagMap=tagService.getTagMap(Arrays.asList(id)); + JournalRespDTO journalRespDTO=JournalMapper.getJournalRespDTO(journal, Collections.emptySet(),mongoTemplate,redisTemplate,commentDao,tagMap); boolean isCollect=null == user ? false:userCollectInfoService.isCollect(user.getUserId(),id, CollectTypeEnum.JOURNAL); journalRespDTO.setHaveCollect(isCollect); return Result.success(journalRespDTO); @@ -139,7 +144,7 @@ public class JournalController { if (null == user && !journalService.isLatest10ByJournalId(id)) { return Result.unauthorized(null); } - return Result.success(CommentMapper.getCommentDTO(id,mongoTemplate,commentDao)); + return Result.success(CommentMapper.getCommentDTO(id,mongoTemplate,commentDao,redisTemplate)); } @ApiOperation(value = "4.获取期刊筛选条件") @@ -154,6 +159,29 @@ public class JournalController { journalFilterDTO.setStyleList(tagService.getStyleList().stream().map(this::getTagDTO).collect(Collectors.toList())); return Result.success(journalFilterDTO); } + + @ApiOperation(value = "5.根据当前期刊的标签推荐6期类似期刊") + @GetMapping("/recommend/{id}") + @GlobalInterceptor + public Result> recommendById( + @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); + } + + List journals = journalService.recommendById(id); + List ids=journals.stream().map(Journal::getId).collect(Collectors.toList()); + Set journalCollectSet = null == user ? Collections.emptySet() + : userCollectInfoService.getCollectSet(user.getUserId(),ids, CollectTypeEnum.JOURNAL); + Map> tagMap=tagService.getTagMap(ids); + List list = journals.stream().map(a -> JournalMapper.getJournalRespDTO(a, journalCollectSet,mongoTemplate,redisTemplate,commentDao,tagMap)) + .collect(Collectors.toList()); + + return Result.success(list); + } + private TagDTO getTagDTO(Tag tag) { TagDTO tagDTO=new TagDTO(); tagDTO.setName(tag.getNameCh()); diff --git a/luoo_music/src/main/java/com/luoo/music/controller/SearchController.java b/luoo_music/src/main/java/com/luoo/music/controller/SearchController.java index 4a8128c..a9a5dd6 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/SearchController.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/SearchController.java @@ -94,14 +94,17 @@ public class SearchController { @ApiOperation(value = "2.搜索期刊/歌曲", notes = "根据关键词模糊搜索,如 期刊号,歌曲名 等") @ApiImplicitParams({ @ApiImplicitParam(name = "keyword", value = "搜索关键词", required = true) }) @GetMapping("/fuzzy/{keyword}") - @GlobalInterceptor + @GlobalInterceptor(checkAppUserLogin = true) public Result fuzzySearch( @RequestHeader(value = "Authorization", required = false) String authorization, @PathVariable @VerifyParam(required = true) String keyword) { SearchResultDTO searchResultDTO = new SearchResultDTO(); List journals = journalService.fuzzySearch(keyword); List songs = journalSongService.fuzzySearch(keyword); - searchResultDTO.setJournals(journals.stream().map(JournalMapper::getJournalRespDTO).collect(Collectors.toList())); + List ids=journals.stream().map(Journal::getId).collect(Collectors.toList()); + Map> tagMap=tagService.getTagMap(ids); + + searchResultDTO.setJournals(journals.stream().map(j->JournalMapper.getJournalRespDTO(j,tagMap)).collect(Collectors.toList())); searchResultDTO.setSongs(songs.stream().map(SongMapper::getSongRespDTO).collect(Collectors.toList())); return Result.success(searchResultDTO); } 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 5e377b1..f879995 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 @@ -66,5 +66,8 @@ public interface JournalDao extends JpaRepository,JpaSpecificati @Query(value = "select max(ABS(journal_no)) FROM tb_journal", nativeQuery = true) int getMaxJournalNo(); + + @Query(value = "select id from tb_journal where id not in ?2 order by rand() limit ?1", nativeQuery = true) + List random(int limit, List recommendIds); } diff --git a/luoo_music/src/main/java/com/luoo/music/dao/JournalTagDao.java b/luoo_music/src/main/java/com/luoo/music/dao/JournalTagDao.java index 471a2b1..10e3c76 100644 --- a/luoo_music/src/main/java/com/luoo/music/dao/JournalTagDao.java +++ b/luoo_music/src/main/java/com/luoo/music/dao/JournalTagDao.java @@ -31,4 +31,8 @@ public interface JournalTagDao extends JpaRepository, JpaSpec @Query(value = "select journal_id FROM tb_journal_tag where tag_id=?1", nativeQuery = true) List getJournalIdByTagId(String tagId); + + + @Query(value = "select journal_id from tb_journal_tag where tag_id in (select tag_id from tb_journal_tag where journal_id=?1) and not journal_id=?1 order by rand() limit 6", nativeQuery = true) + List recommendByJournalId(String journalId); } diff --git a/luoo_music/src/main/java/com/luoo/music/dao/TagDao.java b/luoo_music/src/main/java/com/luoo/music/dao/TagDao.java index fc47281..c5ff1ea 100644 --- a/luoo_music/src/main/java/com/luoo/music/dao/TagDao.java +++ b/luoo_music/src/main/java/com/luoo/music/dao/TagDao.java @@ -1,6 +1,7 @@ package com.luoo.music.dao; import com.luoo.music.pojo.Tag; + import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.Query; @@ -26,4 +27,7 @@ public interface TagDao extends JpaRepository, JpaSpecificationExec @Query(value = "select * from tb_tag_info where level=1", nativeQuery = true) List getLevel1Tags(); + + @Query(value = "select journal_id,name_ch from tb_journal_tag,tb_tag_info where journal_id in ?1 and tb_journal_tag.tag_id =tb_tag_info.id ;", nativeQuery = true) + List getJounralId2TagPairs(List journalIds); } 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 559ad45..1525108 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,11 +1,14 @@ package com.luoo.music.dto.mapper; import java.util.List; +import java.util.concurrent.TimeUnit; +import org.apache.commons.lang.StringUtils; 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 org.springframework.data.redis.core.RedisTemplate; import com.luoo.music.dao.CommentDao; import com.luoo.music.dto.response.CommentDTO; @@ -13,32 +16,51 @@ 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) { + public static CommentDTO getCommentDTO(String journalId, MongoTemplate mongoTemplate, CommentDao commentDao,RedisTemplate redisTemplate) { + /** + * 显示期刊点赞最多5条评论,该五条评论随机显示,每次进入刷新新,超过部分显示为“...”点击评论进入期刊详情页并自动定位到该评论,点击用户头像同上,若为空数据则显示“暂无评论,快去抢沙发吧! + */ CommentDTO commentDTO=new CommentDTO(); - List commentList = commentDao.findTop5ByJournalIdOrderByThumbupCountDesc(journalId); - commentDTO.setCommentList(commentList); + 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); + } /** * 获取期刊总评论数 一级评论数加上父评论的总回复数 */ - 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+"); + + String totalString = (String) redisTemplate.opsForValue().get("JOURNAL_TATAL_COMMENT_COUNT___"+journalId); + 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); + redisTemplate.opsForValue().set("JOURNAL_TATAL_COMMENT_COUNT___"+journalId,totalString,6,TimeUnit.HOURS); } + 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 5435d99..19ab785 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 @@ -11,22 +11,15 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; -import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; -import org.apache.commons.lang.StringUtils; 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; @@ -58,11 +51,12 @@ public class JournalMapper { return Collections.emptyList(); } - public static JournalRespDTO getJournalRespDTO(Journal journal) { - return getJournalRespDTO(journal,Collections.emptySet()); + public static JournalRespDTO getJournalRespDTO(Journal journal, Map> tagMap) { + return getJournalRespDTO(journal, Collections.emptySet(), tagMap); } - - public static JournalRespDTO getJournalRespDTO(Journal journal, Set journalCollectSet) { + + public static JournalRespDTO getJournalRespDTO(Journal journal, Set journalCollectSet, + Map> tagMap) { JournalRespDTO journalRespDTO = new JournalRespDTO(); journalRespDTO.setId(journal.getId()); journalRespDTO.setJournalNo(journal.getJournalNo()); @@ -72,7 +66,7 @@ public class JournalMapper { journalRespDTO.setHaveCollect(journalCollectSet.contains(journalRespDTO.getId())); journalRespDTO.setIpLocation("广东"); - journalRespDTO.setTags(getTags(journalRespDTO.getJournalNo())); + journalRespDTO.setTags(tagMap.getOrDefault(journalRespDTO.getId(), getTags(journalRespDTO.getJournalNo()))); String content = journal.getContent(); if (StringTools.isEmpty(content)) { Poem poem = RandomSource.languageSource().randomTangPoem(); @@ -85,58 +79,15 @@ public class JournalMapper { } return journalRespDTO; } - public static JournalRespDTO getJournalRespDTO(Journal journal, Set journalCollectSet, - MongoTemplate mongoTemplate, RedisTemplate redisTemplate,CommentDao commentDao) { - JournalRespDTO journalRespDTO = getJournalRespDTO(journal,journalCollectSet); - - /** - * 显示期刊点赞最多5条评论,该五条评论随机显示,每次进入刷新新,超过部分显示为“...”点击评论进入期刊详情页并自动定位到该评论,点击用户头像同上,若为空数据则显示“暂无评论,快去抢沙发吧! - */ - - List commentList = (List) redisTemplate.opsForValue().get("JOURNAL_TOP5_COMMENT___"+journal.getId()); - if (null!= commentList) { - journalRespDTO.setCommentList(commentList); - } else { - commentList = commentDao.findTop5ByJournalIdOrderByThumbupCountDesc(journal.getId()); - journalRespDTO.setCommentList(commentList); - redisTemplate.opsForValue().set("JOURNAL_TOP5_COMMENT___"+journal.getId(),commentList,6, TimeUnit.HOURS); - } - -// List commentList = commentDao.findTop5ByJournalIdOrderByThumbupCountDesc(journal.getId()); -// -// journalRespDTO.setCommentList(commentList); - - /** - * 获取期刊总评论数 一级评论数加上父评论的总回复数 - */ - - String totalString = (String) redisTemplate.opsForValue().get("JOURNAL_TATAL_COMMENT_COUNT___"+journal.getId()); - if (StringUtils.isNotBlank(totalString)) { - journalRespDTO.setTotalCommentReply(totalString); - }else { - totalString = "0"; - 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(); - totalString = total +""; - if (total > 99) { - totalString = "99+"; - } - } - journalRespDTO.setTotalCommentReply(totalString); - redisTemplate.opsForValue().set("JOURNAL_TATAL_COMMENT_COUNT___"+journal.getId(),totalString,6,TimeUnit.HOURS); - } + public static JournalRespDTO getJournalRespDTO(Journal journal, Set journalCollectSet, + MongoTemplate mongoTemplate, RedisTemplate redisTemplate, CommentDao commentDao, + Map> tagMap) { + JournalRespDTO journalRespDTO = getJournalRespDTO(journal, journalCollectSet, tagMap); + CommentDTO commentDTO = CommentMapper.getCommentDTO(journalRespDTO.getId(), mongoTemplate, commentDao, + redisTemplate); + journalRespDTO.setCommentList(commentDTO.getCommentList()); + journalRespDTO.setTotalCommentReply(commentDTO.getTotalCommentReply()); return journalRespDTO; } 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 0294624..3a09066 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 @@ -300,4 +300,17 @@ public class JournalService { public boolean isLatest10ByJournalId(String journalId) { return null!=journalDao.isLatest10ByJournalId(journalId); } + + + public List recommendById(String id) { + List recommendIds=journalTagDao.recommendByJournalId(id); + List extraIds=Collections.emptyList(); + if(recommendIds.size()<6) { + extraIds=journalDao.random(6-recommendIds.size(),recommendIds); + } + if(!extraIds.isEmpty()) { + recommendIds.addAll(extraIds); + } + return journalDao.orderByField(recommendIds); + } } diff --git a/luoo_music/src/main/java/com/luoo/music/service/TagService.java b/luoo_music/src/main/java/com/luoo/music/service/TagService.java index d0587c8..9a828e2 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/TagService.java +++ b/luoo_music/src/main/java/com/luoo/music/service/TagService.java @@ -1,6 +1,8 @@ package com.luoo.music.service; import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -12,6 +14,7 @@ import com.luoo.music.pojo.Tag; public class TagService { @Autowired private TagDao tagDao; + public List getLanguageList() { return tagDao.getLanguageList(); } @@ -24,4 +27,10 @@ public class TagService { return tagDao.getLevel1Tags(); } + public Map> getTagMap(List journalIds) { + List pairs = tagDao.getJounralId2TagPairs(journalIds); + return pairs.stream().collect(Collectors.groupingBy(s -> s[0].toString(), + Collectors.mapping(s -> s[1].toString(), Collectors.toList()))); + } + }