diff --git a/luoo_common/src/main/java/constants/Constants.java b/luoo_common/src/main/java/constants/Constants.java index c30d211..ac7b86f 100644 --- a/luoo_common/src/main/java/constants/Constants.java +++ b/luoo_common/src/main/java/constants/Constants.java @@ -21,6 +21,7 @@ public class Constants { public static final String RESOURCE_PREFIX = "http://cdn.indie.cn/"; public static final String MUSIC_RESOURCE_PREFIX = RESOURCE_PREFIX + "music/"; + public static final String SONG_RESOURCE_PREFIX = RESOURCE_PREFIX + "song/"; public static final String TAG_RESOURCE_PREFIX = RESOURCE_PREFIX + "tag/"; 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 1627ea6..18467e8 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 @@ -4,13 +4,16 @@ import com.luoo.music.dto.mapper.SongMapper; import com.luoo.music.dto.request.CollectQueryReq; import com.luoo.music.dto.response.SongRespDTO; import com.luoo.music.pojo.JournalSong; +import com.luoo.music.pojo.SongInfo; import com.luoo.music.service.JournalService; import com.luoo.music.service.JournalSongService; +import com.luoo.music.service.SongInfoService; import com.luoo.music.service.UserCollectInfoService; import annotation.GlobalInterceptor; import annotation.VerifyParam; import api.PageResult; import api.Result; +import api.StatusCode; import dto.UserLoginDto; import enums.CollectTypeEnum; import enums.VerifyRegexEnum; @@ -42,6 +45,8 @@ public class SongController { @Autowired private JournalService journalService; @Autowired + private SongInfoService songInfoService; + @Autowired private JwtUtil jwtUtil; /** @@ -65,7 +70,7 @@ public class SongController { return Result.unauthorized(null); } List songs = journalSongService.findByVolid(journalNo); - List ids=songs.stream().map(JournalSong::getId).collect(Collectors.toList()); + List ids=songs.stream().map(JournalSong::getSongId).collect(Collectors.toList()); Set songCollectSet = null == user ? Collections.emptySet() : userCollectInfoService.getCollectSet(user.getUserId(),ids, CollectTypeEnum.SONG); List results = songs.stream().map(s -> SongMapper.getSongRespDTO(s, songCollectSet)) @@ -87,7 +92,7 @@ public class SongController { if (objectIds.isEmpty()) { return Result.success(new PageResult(0L, Collections.emptyList())); } - List songs = journalSongService.orderByField(objectIds); + List songs = songInfoService.orderByField(objectIds); Set songCollectSet = new HashSet<>(objectIds); List results = songs.stream().map(s -> SongMapper.getSongRespDTO(s, songCollectSet)) @@ -110,10 +115,10 @@ public class SongController { public Result> random( @RequestHeader(value = "Authorization", required = false) String authorization, @PathVariable @VerifyParam(required = true, regex = VerifyRegexEnum.RANDOM_SONG_LIMIT) Integer limit) { - List songs = journalSongService.random(limit); + List songs = songInfoService.random(limit); UserLoginDto user = jwtUtil.getUserLoginDto(authorization); - List ids=songs.stream().map(JournalSong::getId).collect(Collectors.toList()); + List ids=songs.stream().map(SongInfo::getId).collect(Collectors.toList()); Set songCollectSet = null == user ? Collections.emptySet() : userCollectInfoService.getCollectSet(user.getUserId(),ids, CollectTypeEnum.SONG); @@ -127,14 +132,14 @@ public class SongController { @GlobalInterceptor public Result findById(@RequestHeader(value = "Authorization", required = false) String authorization, @PathVariable @VerifyParam(required = true) String id) { + SongInfo song = songInfoService.findById(id); + if(null==song) { + return Result.failed(StatusCode.MUSIC_COMMON_FAILED,"找不到歌曲, id: "+id); + } UserLoginDto user = jwtUtil.getUserLoginDto(authorization); - JournalSong song = journalSongService.findById(id); - SongRespDTO songRespDTO=SongMapper.getSongRespDTO(song); boolean isCollect=null == user ? false:userCollectInfoService.isCollect(user.getUserId(),id, CollectTypeEnum.SONG); songRespDTO.setHaveCollect(isCollect); return Result.success(songRespDTO); } - - } diff --git a/luoo_music/src/main/java/com/luoo/music/dao/SongInfoDao.java b/luoo_music/src/main/java/com/luoo/music/dao/SongInfoDao.java index f1848bd..66bbe1d 100644 --- a/luoo_music/src/main/java/com/luoo/music/dao/SongInfoDao.java +++ b/luoo_music/src/main/java/com/luoo/music/dao/SongInfoDao.java @@ -27,6 +27,12 @@ public interface SongInfoDao extends JpaRepository, JpaSpecific List findByIdIn(List ids); - @Query(value = "select * from tb_song_info where id in ?1 order by field(id,?1)", nativeQuery = true) + @Query(value = "select * from tb_song_info where id in ?1 and state='1' order by field(id,?1)", nativeQuery = true) List orderByField(List objectIds); + + @Query(value = "select * from tb_song_info where state='1' order by rand() limit ?1 ", nativeQuery = true) + List random(Integer limit); + + @Query(value = "select * from tb_song_info where id=?1", nativeQuery = true) + SongInfo getSongById(String id); } diff --git a/luoo_music/src/main/java/com/luoo/music/dto/mapper/SongMapper.java b/luoo_music/src/main/java/com/luoo/music/dto/mapper/SongMapper.java index d93eb1d..d2e37c7 100644 --- a/luoo_music/src/main/java/com/luoo/music/dto/mapper/SongMapper.java +++ b/luoo_music/src/main/java/com/luoo/music/dto/mapper/SongMapper.java @@ -5,8 +5,10 @@ import java.util.Set; import com.luoo.music.dto.response.SongRespDTO; import com.luoo.music.pojo.JournalSong; +import com.luoo.music.pojo.SongInfo; import constants.Constants; +import util.StringTools; public class SongMapper { public static SongRespDTO getSongRespDTO(JournalSong song, Set songCollectSet) { @@ -14,11 +16,15 @@ public class SongMapper { songRespDTO.setJournalNo(song.getJournalNo()); songRespDTO.setAlbum(song.getAlbum()); songRespDTO.setArtist(song.getArtist()); - songRespDTO.setId(song.getId()); + songRespDTO.setId(song.getSongId()); songRespDTO.setTitle(song.getName()); songRespDTO.setSrc(Constants.RESOURCE_PREFIX + song.getUrl()); songRespDTO.setPic(Constants.RESOURCE_PREFIX + song.getImage()); - songRespDTO.setLrc(Constants.RESOURCE_PREFIX + song.getLyricUrl()); + String lyric=song.getLyricUrl(); + if(!StringTools.isEmpty(lyric)) { + songRespDTO.setLrc(Constants.RESOURCE_PREFIX + lyric); + } + songRespDTO.setSongNo(song.getSongNo()); songRespDTO.setHaveCollect(songCollectSet.contains(songRespDTO.getId())); return songRespDTO; @@ -27,4 +33,24 @@ public class SongMapper { public static SongRespDTO getSongRespDTO(JournalSong song) { return getSongRespDTO(song, Collections.emptySet()); } + + public static SongRespDTO getSongRespDTO(SongInfo song, Set songCollectSet) { + SongRespDTO songRespDTO = new SongRespDTO(); + songRespDTO.setAlbum(song.getAlbum()); + songRespDTO.setArtist(song.getArtist()); + songRespDTO.setId(song.getId()); + songRespDTO.setTitle(song.getName()); + songRespDTO.setSrc(Constants.SONG_RESOURCE_PREFIX + song.getUrl()); + songRespDTO.setPic(Constants.SONG_RESOURCE_PREFIX + song.getImage()); + String lyric=song.getLyricUrl(); + if(!StringTools.isEmpty(lyric)) { + songRespDTO.setLrc(Constants.SONG_RESOURCE_PREFIX + lyric); + } + songRespDTO.setHaveCollect(songCollectSet.contains(songRespDTO.getId())); + return songRespDTO; + } + + public static SongRespDTO getSongRespDTO(SongInfo song) { + return getSongRespDTO(song, Collections.emptySet()); + } } diff --git a/luoo_music/src/main/java/com/luoo/music/service/SongInfoService.java b/luoo_music/src/main/java/com/luoo/music/service/SongInfoService.java new file mode 100644 index 0000000..c8511a4 --- /dev/null +++ b/luoo_music/src/main/java/com/luoo/music/service/SongInfoService.java @@ -0,0 +1,27 @@ +package com.luoo.music.service; + +import com.luoo.music.dao.SongInfoDao; +import com.luoo.music.pojo.SongInfo; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import java.util.List; + +@Service +public class SongInfoService { + + @Autowired + private SongInfoDao songInfoDao; + + public List random(Integer limit) { + return songInfoDao.random(limit); + } + + public List orderByField(List objectIds) { + return songInfoDao.orderByField(objectIds); + } + + public SongInfo findById(String id) { + return songInfoDao.getSongById(id); + } +}