1.add page query collect journal/song

main
Gary 10 months ago
parent 2be18b3fd4
commit 6f791f4f97

@ -4,10 +4,8 @@ import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import com.luoo.music.dao.UserCollectDao;
import com.luoo.music.dto.request.JournalQueryReq; import com.luoo.music.dto.request.JournalQueryReq;
import com.luoo.music.dto.response.JournalRespDTO; 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.data.domain.Page;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import com.luoo.music.pojo.Article; import com.luoo.music.pojo.Article;
import com.luoo.music.pojo.UserCollect;
import com.luoo.music.service.ArticleService; import com.luoo.music.service.ArticleService;
import com.luoo.music.service.UserCollectService;
import annotation.GlobalInterceptor; import annotation.GlobalInterceptor;
import annotation.VerifyParam; import annotation.VerifyParam;
@ -24,7 +22,10 @@ import api.PageResult;
import api.Result; import api.Result;
import constants.Constants; import constants.Constants;
import dto.UserLoginDto; import dto.UserLoginDto;
import enums.CollectTypeEnum;
import enums.DateTimePatternEnum; import enums.DateTimePatternEnum;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import util.DateUtil; import util.DateUtil;
import util.JwtUtil; import util.JwtUtil;
@ -40,7 +41,7 @@ import util.JwtUtil;
@RequestMapping("/journal") @RequestMapping("/journal")
public class JournalController { public class JournalController {
@Autowired @Autowired
private UserCollectDao userCollectDao; private UserCollectService userCollectService;
@Autowired @Autowired
private ArticleService articleService; private ArticleService articleService;
@Autowired @Autowired
@ -59,20 +60,32 @@ public class JournalController {
queryReq.setPageSize(10); queryReq.setPageSize(10);
} }
Page<Article> pageList = articleService.queryPage(queryReq); Page<Article> pageList = articleService.queryPage(queryReq);
Set<String> journalCollectSet = null==user?Collections.emptySet():getCollectSet(user.getUserId()); Set<String> journalCollectSet = null==user?Collections.emptySet():userCollectService.getCollectSet(user.getUserId(),CollectTypeEnum.JOURNAL);
List<JournalRespDTO> list = pageList.stream().map(a -> getArticleRespDTO(a, journalCollectSet)) List<JournalRespDTO> list = pageList.stream().map(a -> getArticleRespDTO(a, journalCollectSet))
.collect(Collectors.toList()); .collect(Collectors.toList());
return Result.success(new PageResult<JournalRespDTO>(Long.valueOf(list.size()), list)); return Result.success(new PageResult<JournalRespDTO>(Long.valueOf(list.size()), list));
} }
private Set<String> getCollectSet(String userId) { @ApiOperation(value = "2.查询收藏期刊信息")
Optional<UserCollect> optional=userCollectDao.findById(userId); @ApiImplicitParams({
if(optional.isPresent()) { @ApiImplicitParam(name = "userId", value = "用户id", required = true),
return new HashSet<>(optional.get().getJournals()); @ApiImplicitParam(name = "pageNum", value = "分页: 页码以1开始", required = true),
} @ApiImplicitParam(name = "pageSize", value = "分页: 每页数量", required = true)
return Collections.emptySet(); })
@GetMapping("/collect/{userId}/{pageNum}/{pageSize}")
@GlobalInterceptor
public Result<PageResult<JournalRespDTO>> collectPage(
@PathVariable @VerifyParam(required = true)String userId,
@PathVariable @VerifyParam(required = true)Integer pageNum,
@PathVariable @VerifyParam(required = true)Integer pageSize) {
List<String> objectIds=userCollectService.getCollectList(userId,pageNum,pageSize,CollectTypeEnum.JOURNAL);
List<Article> pageList = articleService.orderByField(objectIds);
Set<String> journalCollectSet = objectIds.isEmpty()?Collections.emptySet(): new HashSet<>(objectIds);
List<JournalRespDTO> list = pageList.stream().map(a -> getArticleRespDTO(a, journalCollectSet))
.collect(Collectors.toList());
return Result.success(new PageResult<JournalRespDTO>(Long.valueOf(list.size()), list));
} }
private JournalRespDTO getArticleRespDTO(Article article, Set<String> journalCollectSet) { private JournalRespDTO getArticleRespDTO(Article article, Set<String> journalCollectSet) {
JournalRespDTO journalRespDTO = new JournalRespDTO(); JournalRespDTO journalRespDTO = new JournalRespDTO();
journalRespDTO.setId(article.getId()); journalRespDTO.setId(article.getId());

@ -1,12 +1,11 @@
package com.luoo.music.controller; package com.luoo.music.controller;
import com.luoo.music.dao.UserCollectDao;
import com.luoo.music.dto.response.SongRespDTO; import com.luoo.music.dto.response.SongRespDTO;
import com.luoo.music.pojo.Article; import com.luoo.music.pojo.Article;
import com.luoo.music.pojo.Song; import com.luoo.music.pojo.Song;
import com.luoo.music.pojo.UserCollect;
import com.luoo.music.service.ArticleService; import com.luoo.music.service.ArticleService;
import com.luoo.music.service.SongService; import com.luoo.music.service.SongService;
import com.luoo.music.service.UserCollectService;
import annotation.GlobalInterceptor; import annotation.GlobalInterceptor;
import annotation.VerifyParam; import annotation.VerifyParam;
@ -14,6 +13,7 @@ import api.PageResult;
import api.Result; import api.Result;
import constants.Constants; import constants.Constants;
import dto.UserLoginDto; import dto.UserLoginDto;
import enums.CollectTypeEnum;
import enums.VerifyRegexEnum; import enums.VerifyRegexEnum;
import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiImplicitParams;
@ -28,7 +28,6 @@ import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -37,7 +36,7 @@ import java.util.stream.Collectors;
@RequestMapping("/song") @RequestMapping("/song")
public class SongController { public class SongController {
@Autowired @Autowired
private UserCollectDao userCollectDao; private UserCollectService userCollectService;
@Autowired @Autowired
private SongService songService; private SongService songService;
@Autowired @Autowired
@ -65,24 +64,36 @@ public class SongController {
return Result.unauthorized(null); return Result.unauthorized(null);
} }
List<Song> songs = songService.findByVolid(journalNo); List<Song> songs = songService.findByVolid(journalNo);
Set<String> songCollectSet = null==user?Collections.emptySet():getCollectSet(user.getUserId()); Set<String> songCollectSet = null==user?Collections.emptySet():userCollectService.getCollectSet(user.getUserId(),CollectTypeEnum.SONG);
List<SongRespDTO> results = songs.stream().map(s -> getSongRespDTO(s, songCollectSet)).collect(Collectors.toList()); List<SongRespDTO> results = songs.stream().map(s -> getSongRespDTO(s, songCollectSet)).collect(Collectors.toList());
return Result.success(results); return Result.success(results);
} }
private Set<String> getCollectSet(String userId) {
Optional<UserCollect> optional=userCollectDao.findById(userId);
if(optional.isPresent()) {
return new HashSet<>(optional.get().getSongs());
}
return Collections.emptySet();
}
private boolean isLatest10(String journalNo) { private boolean isLatest10(String journalNo) {
return articleService.isLatest10(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<PageResult<SongRespDTO>> collectPage(
@PathVariable @VerifyParam(required = true)String userId,
@PathVariable @VerifyParam(required = true)Integer pageNum,
@PathVariable @VerifyParam(required = true)Integer pageSize) {
List<String> objectIds=userCollectService.getCollectList(userId,pageNum,pageSize,CollectTypeEnum.SONG);
List<Song> songs = songService.orderByField(objectIds);
Set<String> songCollectSet = objectIds.isEmpty()?Collections.emptySet(): new HashSet<>(objectIds);
List<SongRespDTO> results = songs.stream().map(s -> getSongRespDTO(s, songCollectSet)).collect(Collectors.toList());
return Result.success(new PageResult<SongRespDTO>(Long.valueOf(results.size()), results));
}
@ApiOperation(value = "3.随机播放歌曲", notes = "雀乐FM")
@ApiImplicitParams({ @ApiImplicitParam(name = "limit", value = "随机歌曲数最少1首最多30首", required = false) }) @ApiImplicitParams({ @ApiImplicitParam(name = "limit", value = "随机歌曲数最少1首最多30首", required = false) })
@GetMapping("/random/{limit}") @GetMapping("/random/{limit}")
@GlobalInterceptor @GlobalInterceptor
@ -91,11 +102,11 @@ public class SongController {
@PathVariable @VerifyParam(required = true, regex = VerifyRegexEnum.RANDOM_SONG_LIMIT) Integer limit) { @PathVariable @VerifyParam(required = true, regex = VerifyRegexEnum.RANDOM_SONG_LIMIT) Integer limit) {
List<Song> songs = songService.random(limit); List<Song> songs = songService.random(limit);
UserLoginDto user = jwtUtil.getUserLoginDto(token); UserLoginDto user = jwtUtil.getUserLoginDto(token);
Set<String> songCollectSet = null==user?Collections.emptySet():getCollectSet(user.getUserId()); Set<String> songCollectSet = null==user?Collections.emptySet():userCollectService.getCollectSet(user.getUserId(),CollectTypeEnum.SONG);
List<SongRespDTO> results = songs.stream().map(s -> getSongRespDTO(s, songCollectSet)).collect(Collectors.toList()); List<SongRespDTO> results = songs.stream().map(s -> getSongRespDTO(s, songCollectSet)).collect(Collectors.toList());
return Result.success(results); return Result.success(results);
} }
private SongRespDTO getSongRespDTO(Song song, Set<String> songCollectSet) { private SongRespDTO getSongRespDTO(Song song, Set<String> songCollectSet) {
SongRespDTO songRespDTO = new SongRespDTO(); SongRespDTO songRespDTO = new SongRespDTO();
songRespDTO.setJournalNo(song.getVolid()); songRespDTO.setJournalNo(song.getVolid());

@ -1,9 +1,12 @@
package com.luoo.music.dao; package com.luoo.music.dao;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import com.luoo.music.pojo.Article; import com.luoo.music.pojo.Article;
import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
@ -24,5 +27,8 @@ public interface ArticleDao extends JpaRepository<Article,String>,JpaSpecificati
public Article findArticleByVolid(String volid); public Article findArticleByVolid(String volid);
@Query(value = "select * from tb_article where id in ?1 order by field(id,?1)", nativeQuery = true)
public List<Article> orderByField(List<String> idList);
} }

@ -13,4 +13,7 @@ public interface SongDao extends JpaRepository<Song,String>, JpaSpecificationExe
@Query(value = "select * from tb_song order by rand() limit ?1 ", nativeQuery = true) @Query(value = "select * from tb_song order by rand() limit ?1 ", nativeQuery = true)
List<Song> random(int limit); List<Song> random(int limit);
@Query(value = "select * from tb_song where id in ?1 order by field(id,?1)", nativeQuery = true)
public List<Song> orderByField(List<String> idList);
} }

@ -241,4 +241,9 @@ public class ArticleService {
// TODO Auto-generated method stub // TODO Auto-generated method stub
return true; return true;
} }
public List<Article> orderByField(List<String> objectIds) {
return articleDao.orderByField(objectIds);
}
} }

@ -149,4 +149,8 @@ public class SongService {
public List<Song> random(Integer limit) { public List<Song> random(Integer limit) {
return songDao.random(limit); return songDao.random(limit);
} }
public List<Song> orderByField(List<String> objectIds) {
return songDao.orderByField(objectIds);
}
} }

@ -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<String> getCollectSet(String userId, CollectTypeEnum collectTypeEnum) {
List<String> collectList = getCollectList(userId, null, null, collectTypeEnum);
if (collectList.isEmpty()) {
return Collections.emptySet();
}
return new HashSet<>(collectList);
}
public List<String> getCollectList(String userId, Integer pageNum, Integer pageSize,
CollectTypeEnum collectTypeEnum) {
if (null == userId) {
return Collections.emptyList();
}
Optional<UserCollect> 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<String> getPageResult(Integer pageNum, Integer pageSize, LinkedList<String> objectIds) {
if (null != pageNum && null != pageSize && pageNum > 0 && pageSize > 0) {
return objectIds.subList((pageNum - 1) * pageSize, pageNum * pageSize);
}
return objectIds;
}
}
Loading…
Cancel
Save