@ -14,6 +14,7 @@ import annotation.VerifyParam;
import api.PageResult ;
import api.Result ;
import api.StatusCode ;
import constants.Constants ;
import dto.UserLoginDto ;
import enums.CollectTypeEnum ;
import enums.VerifyRegexEnum ;
@ -26,10 +27,12 @@ import util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.data.domain.PageRequest ;
import org.springframework.data.redis.core.RedisTemplate ;
import org.springframework.util.CollectionUtils ;
import org.springframework.web.bind.annotation.* ;
import java.util.* ;
import java.util.concurrent.TimeUnit ;
import java.util.stream.Collectors ;
@RestController
@ -43,6 +46,8 @@ public class SongController {
private JournalSongService journalSongService ;
@Autowired
private JournalService journalService ;
@Autowired
private RedisTemplate redisTemplate ;
//@Autowired
//private SongInfoService songInfoService;
@Autowired
@ -140,12 +145,13 @@ public class SongController {
if ( objectIds . isEmpty ( ) ) {
return Result . success ( new PageResult < SongRespDTO > ( 0 L , Collections . emptyList ( ) ) ) ;
}
Long count = ( long ) getSongCollectSet ( queryUserId ) . size ( ) ;
List < JournalSong > songs = journalSongService . orderByField ( objectIds ) ;
Set < String > songCollectSet = getMyCollectSet ( loginUserId , queryUserId , objectIds , isJouranlSource ) ;
List < SongRespDTO > results = songs . stream ( ) . map ( s - > SongMapper . getSongRespDTO ( s , songCollectSet ) )
. collect ( Collectors . toList ( ) ) ;
// results = results.stream().map(songRespDTO -> randomCDN(songRespDTO)).collect(Collectors.toList());
return Result . success ( new PageResult < SongRespDTO > ( Long. valueOf ( results . size ( ) ) , results ) ) ;
return Result . success ( new PageResult < SongRespDTO > ( count , results ) ) ;
}
private Set < String > getMyCollectSet ( String loginUser , String checkUser , List < String > objectIds , boolean isJouranlSource ) {
@ -160,12 +166,20 @@ public class SongController {
}
private List < String > getSongIds ( CollectQueryReq queryReq ) {
if ( null = = queryReq . getPageNum ( ) | | queryReq . getPageNum ( ) < 1 | | null = = queryReq . getPageSize ( ) | | queryReq . getPageSize ( ) < 1 ) {
return userCollectInfoService . findByUserIdAndCollectType ( queryReq . getUserId ( ) , CollectTypeEnum . SONG ) ;
return new ArrayList < > ( getSongCollectSet ( queryReq . getUserId ( ) ) ) ;
}
PageRequest pageRequest = PageRequest . of ( queryReq . getPageNum ( ) - 1 , queryReq . getPageSize ( ) ) ;
return userCollectInfoService . findByUserIdAndCollectType ( queryReq . getUserId ( ) , CollectTypeEnum . SONG , pageRequest ) ;
}
private Set < String > getSongCollectSet ( String userId ) {
Set < String > set = ( Set < String > ) redisTemplate . opsForValue ( ) . get ( Constants . REDIS_KEY_USER_COLLECT_SONG + userId ) ;
if ( null = = set ) {
set = userCollectInfoService . getCollectSet ( userId , CollectTypeEnum . SONG ) ;
redisTemplate . opsForValue ( ) . set ( Constants . REDIS_KEY_USER_COLLECT_SONG + userId , set , 6 , TimeUnit . HOURS ) ;
}
return set ;
}
@ApiOperation ( value = "3.随机播放歌曲" , notes = "雀乐FM" )
@ApiImplicitParams ( { @ApiImplicitParam ( name = "limit" , value = "随机歌曲数, 最少1首, 最多30首" , required = false ) } )
@GetMapping ( "/random/{limit}" )