From 6f791f4f9791548d984cd1e4bf4b96c8b24d65ff Mon Sep 17 00:00:00 2001 From: Gary Date: Fri, 26 Jan 2024 01:59:38 +0800 Subject: [PATCH] 1.add page query collect journal/song --- .../music/controller/JournalController.java | 37 ++++++++---- .../luoo/music/controller/SongController.java | 43 ++++++++------ .../java/com/luoo/music/dao/ArticleDao.java | 6 ++ .../main/java/com/luoo/music/dao/SongDao.java | 3 + .../luoo/music/service/ArticleService.java | 5 ++ .../com/luoo/music/service/SongService.java | 4 ++ .../music/service/UserCollectService.java | 57 +++++++++++++++++++ 7 files changed, 127 insertions(+), 28 deletions(-) create mode 100644 luoo_music/src/main/java/com/luoo/music/service/UserCollectService.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 cb040e6..b50c166 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 @@ -4,10 +4,8 @@ import java.util.Collections; import java.util.Date; import java.util.HashSet; import java.util.List; -import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; -import com.luoo.music.dao.UserCollectDao; import com.luoo.music.dto.request.JournalQueryReq; import com.luoo.music.dto.response.JournalRespDTO; @@ -15,8 +13,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.web.bind.annotation.*; import com.luoo.music.pojo.Article; -import com.luoo.music.pojo.UserCollect; import com.luoo.music.service.ArticleService; +import com.luoo.music.service.UserCollectService; import annotation.GlobalInterceptor; import annotation.VerifyParam; @@ -24,7 +22,10 @@ import api.PageResult; import api.Result; import constants.Constants; import dto.UserLoginDto; +import enums.CollectTypeEnum; import enums.DateTimePatternEnum; +import io.swagger.annotations.ApiImplicitParam; +import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import util.DateUtil; import util.JwtUtil; @@ -40,7 +41,7 @@ import util.JwtUtil; @RequestMapping("/journal") public class JournalController { @Autowired - private UserCollectDao userCollectDao; + private UserCollectService userCollectService; @Autowired private ArticleService articleService; @Autowired @@ -59,20 +60,32 @@ public class JournalController { queryReq.setPageSize(10); } Page
pageList = articleService.queryPage(queryReq); - Set journalCollectSet = null==user?Collections.emptySet():getCollectSet(user.getUserId()); + Set journalCollectSet = null==user?Collections.emptySet():userCollectService.getCollectSet(user.getUserId(),CollectTypeEnum.JOURNAL); List list = pageList.stream().map(a -> getArticleRespDTO(a, journalCollectSet)) .collect(Collectors.toList()); return Result.success(new PageResult(Long.valueOf(list.size()), list)); } - private Set getCollectSet(String userId) { - Optional optional=userCollectDao.findById(userId); - if(optional.isPresent()) { - return new HashSet<>(optional.get().getJournals()); - } - return Collections.emptySet(); + @ApiOperation(value = "2.查询收藏期刊信息") + @ApiImplicitParams({ + @ApiImplicitParam(name = "userId", value = "用户id", required = true), + @ApiImplicitParam(name = "pageNum", value = "分页: 页码,以1开始", required = true), + @ApiImplicitParam(name = "pageSize", value = "分页: 每页数量", required = true) + }) + @GetMapping("/collect/{userId}/{pageNum}/{pageSize}") + @GlobalInterceptor + public Result> collectPage( + @PathVariable @VerifyParam(required = true)String userId, + @PathVariable @VerifyParam(required = true)Integer pageNum, + @PathVariable @VerifyParam(required = true)Integer pageSize) { + List objectIds=userCollectService.getCollectList(userId,pageNum,pageSize,CollectTypeEnum.JOURNAL); + List
pageList = articleService.orderByField(objectIds); + Set journalCollectSet = objectIds.isEmpty()?Collections.emptySet(): new HashSet<>(objectIds); + List list = pageList.stream().map(a -> getArticleRespDTO(a, journalCollectSet)) + .collect(Collectors.toList()); + return Result.success(new PageResult(Long.valueOf(list.size()), list)); } - + private JournalRespDTO getArticleRespDTO(Article article, Set journalCollectSet) { JournalRespDTO journalRespDTO = new JournalRespDTO(); journalRespDTO.setId(article.getId()); 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 dbc1543..d4fb322 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 @@ -1,12 +1,11 @@ package com.luoo.music.controller; -import com.luoo.music.dao.UserCollectDao; import com.luoo.music.dto.response.SongRespDTO; import com.luoo.music.pojo.Article; import com.luoo.music.pojo.Song; -import com.luoo.music.pojo.UserCollect; import com.luoo.music.service.ArticleService; import com.luoo.music.service.SongService; +import com.luoo.music.service.UserCollectService; import annotation.GlobalInterceptor; import annotation.VerifyParam; @@ -14,6 +13,7 @@ import api.PageResult; import api.Result; import constants.Constants; import dto.UserLoginDto; +import enums.CollectTypeEnum; import enums.VerifyRegexEnum; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; @@ -28,7 +28,6 @@ import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Map; -import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; @@ -37,7 +36,7 @@ import java.util.stream.Collectors; @RequestMapping("/song") public class SongController { @Autowired - private UserCollectDao userCollectDao; + private UserCollectService userCollectService; @Autowired private SongService songService; @Autowired @@ -65,24 +64,36 @@ public class SongController { return Result.unauthorized(null); } List songs = songService.findByVolid(journalNo); - Set songCollectSet = null==user?Collections.emptySet():getCollectSet(user.getUserId()); + Set songCollectSet = null==user?Collections.emptySet():userCollectService.getCollectSet(user.getUserId(),CollectTypeEnum.SONG); List results = songs.stream().map(s -> getSongRespDTO(s, songCollectSet)).collect(Collectors.toList()); return Result.success(results); } - - private Set getCollectSet(String userId) { - Optional optional=userCollectDao.findById(userId); - if(optional.isPresent()) { - return new HashSet<>(optional.get().getSongs()); - } - return Collections.emptySet(); - } private boolean isLatest10(String journalNo) { return articleService.isLatest10(journalNo); } - @ApiOperation(value = "2.随机播放歌曲", notes = "雀乐FM") + @ApiOperation(value = "2.查询收藏歌曲信息") + @ApiImplicitParams({ + @ApiImplicitParam(name = "userId", value = "用户id", required = true), + @ApiImplicitParam(name = "pageNum", value = "分页: 页码,以1开始", required = true), + @ApiImplicitParam(name = "pageSize", value = "分页: 每页数量", required = true) + }) + @GetMapping("/collect/{userId}/{pageNum}/{pageSize}") + @GlobalInterceptor + public Result> collectPage( + @PathVariable @VerifyParam(required = true)String userId, + @PathVariable @VerifyParam(required = true)Integer pageNum, + @PathVariable @VerifyParam(required = true)Integer pageSize) { + List objectIds=userCollectService.getCollectList(userId,pageNum,pageSize,CollectTypeEnum.SONG); + List songs = songService.orderByField(objectIds); + + Set songCollectSet = objectIds.isEmpty()?Collections.emptySet(): new HashSet<>(objectIds); + List results = songs.stream().map(s -> getSongRespDTO(s, songCollectSet)).collect(Collectors.toList()); + return Result.success(new PageResult(Long.valueOf(results.size()), results)); + } + + @ApiOperation(value = "3.随机播放歌曲", notes = "雀乐FM") @ApiImplicitParams({ @ApiImplicitParam(name = "limit", value = "随机歌曲数,最少1首,最多30首", required = false) }) @GetMapping("/random/{limit}") @GlobalInterceptor @@ -91,11 +102,11 @@ public class SongController { @PathVariable @VerifyParam(required = true, regex = VerifyRegexEnum.RANDOM_SONG_LIMIT) Integer limit) { List songs = songService.random(limit); UserLoginDto user = jwtUtil.getUserLoginDto(token); - Set songCollectSet = null==user?Collections.emptySet():getCollectSet(user.getUserId()); + Set songCollectSet = null==user?Collections.emptySet():userCollectService.getCollectSet(user.getUserId(),CollectTypeEnum.SONG); List results = songs.stream().map(s -> getSongRespDTO(s, songCollectSet)).collect(Collectors.toList()); return Result.success(results); } - + private SongRespDTO getSongRespDTO(Song song, Set songCollectSet) { SongRespDTO songRespDTO = new SongRespDTO(); songRespDTO.setJournalNo(song.getVolid()); diff --git a/luoo_music/src/main/java/com/luoo/music/dao/ArticleDao.java b/luoo_music/src/main/java/com/luoo/music/dao/ArticleDao.java index 0ac414a..3ef1705 100644 --- a/luoo_music/src/main/java/com/luoo/music/dao/ArticleDao.java +++ b/luoo_music/src/main/java/com/luoo/music/dao/ArticleDao.java @@ -1,9 +1,12 @@ package com.luoo.music.dao; +import java.util.List; + import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import com.luoo.music.pojo.Article; + import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; @@ -24,5 +27,8 @@ public interface ArticleDao extends JpaRepository,JpaSpecificati public Article findArticleByVolid(String volid); + + @Query(value = "select * from tb_article where id in ?1 order by field(id,?1)", nativeQuery = true) + public List
orderByField(List idList); } diff --git a/luoo_music/src/main/java/com/luoo/music/dao/SongDao.java b/luoo_music/src/main/java/com/luoo/music/dao/SongDao.java index 7025889..1a7848d 100644 --- a/luoo_music/src/main/java/com/luoo/music/dao/SongDao.java +++ b/luoo_music/src/main/java/com/luoo/music/dao/SongDao.java @@ -13,4 +13,7 @@ public interface SongDao extends JpaRepository, JpaSpecificationExe @Query(value = "select * from tb_song order by rand() limit ?1 ", nativeQuery = true) List random(int limit); + + @Query(value = "select * from tb_song where id in ?1 order by field(id,?1)", nativeQuery = true) + public List orderByField(List idList); } diff --git a/luoo_music/src/main/java/com/luoo/music/service/ArticleService.java b/luoo_music/src/main/java/com/luoo/music/service/ArticleService.java index dc1cb12..67a77c4 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/ArticleService.java +++ b/luoo_music/src/main/java/com/luoo/music/service/ArticleService.java @@ -241,4 +241,9 @@ public class ArticleService { // TODO Auto-generated method stub return true; } + + + public List
orderByField(List objectIds) { + return articleDao.orderByField(objectIds); + } } diff --git a/luoo_music/src/main/java/com/luoo/music/service/SongService.java b/luoo_music/src/main/java/com/luoo/music/service/SongService.java index 3cec232..0e6a27e 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/SongService.java +++ b/luoo_music/src/main/java/com/luoo/music/service/SongService.java @@ -149,4 +149,8 @@ public class SongService { public List random(Integer limit) { return songDao.random(limit); } + + public List orderByField(List objectIds) { + return songDao.orderByField(objectIds); + } } diff --git a/luoo_music/src/main/java/com/luoo/music/service/UserCollectService.java b/luoo_music/src/main/java/com/luoo/music/service/UserCollectService.java new file mode 100644 index 0000000..74fa3a9 --- /dev/null +++ b/luoo_music/src/main/java/com/luoo/music/service/UserCollectService.java @@ -0,0 +1,57 @@ +package com.luoo.music.service; + +import java.util.Collections; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Optional; +import java.util.Set; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.luoo.music.dao.UserCollectDao; +import com.luoo.music.pojo.UserCollect; + +import enums.CollectTypeEnum; + +@Service +public class UserCollectService { + @Autowired + private UserCollectDao userCollectDao; + + public Set getCollectSet(String userId, CollectTypeEnum collectTypeEnum) { + List collectList = getCollectList(userId, null, null, collectTypeEnum); + if (collectList.isEmpty()) { + return Collections.emptySet(); + } + return new HashSet<>(collectList); + } + + public List getCollectList(String userId, Integer pageNum, Integer pageSize, + CollectTypeEnum collectTypeEnum) { + if (null == userId) { + return Collections.emptyList(); + } + Optional optional = userCollectDao.findById(userId); + if (!optional.isPresent()) { + return Collections.emptyList(); + } + UserCollect userCollect = optional.get(); + switch (collectTypeEnum) { + case SONG: + return getPageResult(pageNum, pageSize, userCollect.getSongs()); + case JOURNAL: + return getPageResult(pageNum, pageSize, userCollect.getJournals()); + default: + return Collections.emptyList(); + } + } + + private List getPageResult(Integer pageNum, Integer pageSize, LinkedList objectIds) { + if (null != pageNum && null != pageSize && pageNum > 0 && pageSize > 0) { + return objectIds.subList((pageNum - 1) * pageSize, pageNum * pageSize); + } + return objectIds; + } +}