From 670cf426f302fa2a9bd6e9e4a69fcc5aa596b1bc Mon Sep 17 00:00:00 2001 From: Gary Date: Mon, 8 Apr 2024 12:43:45 +0800 Subject: [PATCH] 1.recomend by limit --- .../music/controller/JournalController.java | 37 +++++-------------- .../com/luoo/music/dao/JournalTagDao.java | 4 +- .../luoo/music/service/JournalService.java | 8 ++-- 3 files changed, 15 insertions(+), 34 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 97dae1c..3bb8e9f 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 @@ -84,27 +84,6 @@ public class JournalController { return Result.success(new PageResult(Long.valueOf(pageList.size()), pageList)); } - private JournalRespDTO randomCDN1(JournalRespDTO journalRespDTO) { -// Random random = new Random(); -// int randomNum = random.nextInt(2)+1; - - Random random = new Random(); - int rate = random.nextInt(100); -// int randomNum = random.nextInt(2)+1; - int randomNum =1; - if (rate>20) { - randomNum =2; - } - if (StringUtils.isNotEmpty(journalRespDTO.getImage())){ - journalRespDTO.setImage(journalRespDTO.getImage().replaceFirst("cdn1","cdn"+randomNum)); - journalRespDTO.setImage(journalRespDTO.getImage().replaceFirst("cdn2","cdn"+randomNum)); - } - - return journalRespDTO; - } - - - private void updateCollectStatus(List pageList, Set journalCollectSet) { pageList.forEach(j->{ j.setHaveCollect(journalCollectSet.contains(j.getId())); @@ -272,18 +251,20 @@ public class JournalController { return Result.success(journalService.getJournalFilterDTO()); } - @ApiOperation(value = "5.根据当前期刊的标签推荐6期类似期刊") - @GetMapping("/recommend/{id}") + @ApiOperation(value = "5.根据当前期刊的标签推荐类似期刊,默认6期") + @GetMapping(value={"/recommend/{id}","/recommend/{id}/{limit}"}) @GlobalInterceptor public Result> recommendById( @RequestHeader(value = "Authorization", required = false) String authorization, - @PathVariable @VerifyParam(required = true) String id) { + @PathVariable @VerifyParam(required = true) String id,@PathVariable(required=false) Integer limit) { UserLoginDto user = jwtUtil.getUserLoginDto(authorization); if (null == user && !journalService.isLatest10ByJournalId(id)) { return Result.unauthorized(null); } - - List journals = journalService.recommendById(id); + if(null == limit) { + limit = 6; + } + List journals = journalService.recommendById(id,limit); List ids=journals.stream().map(Journal::getId).collect(Collectors.toList()); Set journalCollectSet = null == user ? Collections.emptySet() : userCollectInfoService.getCollectSet(user.getUserId(),ids, CollectTypeEnum.JOURNAL); @@ -295,14 +276,14 @@ public class JournalController { @ApiOperation(value = "统计期刊的浏览数", notes = "APP端打开期刊调用一次统计接口") - @RequestMapping(value="/stat/visits/{id}", method= RequestMethod.PUT) + @PutMapping("/stat/visits/{id}") public Result statVisitsCount(@ApiParam(value = "期刊ID", required = true) @PathVariable String id) { return journalService.statVisitsCount(id); } @GlobalInterceptor(checkAppUserLogin = true) @ApiOperation(value = "统计期刊的评论数", notes = "APP端增加期刊时或删除评论时调用一次该接口,status根据操作类型对应传参") - @RequestMapping(value="/stat/comment/{id}", method= RequestMethod.PUT) + @PutMapping("/stat/comment/{id}") public Result statCommentCount(@ApiParam(value = "期刊ID", required = true) @PathVariable String id, @ApiParam(value = "状态,1:增加评论,0:删除评论", required = true) @RequestBody String status) { return journalService.statCommentCount(id, status); 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 ec59206..52f352d 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 @@ -33,6 +33,6 @@ public interface JournalTagDao extends JpaRepository, JpaSpec 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); + @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 ?2", nativeQuery = true) + List recommendByJournalId(String journalId, Integer limit); } 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 1862ed6..3849373 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 @@ -331,11 +331,11 @@ public class JournalService { } - public List recommendById(String id) { - List recommendIds=journalTagDao.recommendByJournalId(id); + public List recommendById(String id, Integer limit) { + List recommendIds=journalTagDao.recommendByJournalId(id,limit); List extraIds=Collections.emptyList(); - if(recommendIds.size()<6) { - extraIds=recommendIds.isEmpty()?journalDao.random(6):journalDao.random(6-recommendIds.size(),recommendIds); + if(recommendIds.size()