diff --git a/luoo_music/src/main/java/com/luoo/music/controller/CMSJournalController.java b/luoo_music/src/main/java/com/luoo/music/controller/CMSJournalController.java index 447863d..20bb019 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/CMSJournalController.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/CMSJournalController.java @@ -2,10 +2,11 @@ package com.luoo.music.controller; import api.PageResult; import api.Result; -import com.luoo.music.request.cms.JournalAddModel; -import com.luoo.music.request.cms.JournalPublishReq; -import com.luoo.music.request.cms.JournalQueryModel; -import com.luoo.music.response.cms.JournalVO; +import com.luoo.music.dto.request.cms.JournalAddModel; +import com.luoo.music.dto.request.cms.JournalPublishReq; +import com.luoo.music.dto.request.cms.JournalQueryModel; +import com.luoo.music.dto.response.cms.JournalListRespDto; +import com.luoo.music.dto.response.cms.JournalRespDto; import com.luoo.music.service.CMSJournalService; import com.luoo.music.service.S3Service; import io.swagger.annotations.Api; @@ -33,31 +34,29 @@ public class CMSJournalController { @ApiOperation(value = "查询期刊列表", notes = "查询期刊列表") @RequestMapping(value="/search/{page}/{size}", method= RequestMethod.POST) - public Result> search(@ApiParam(value = "查询条件对象", required = true) @RequestBody JournalQueryModel queryModel, - @ApiParam(value = "页码", required = true) @PathVariable int page, - @ApiParam(value = "每页条数", required = true) @PathVariable int size){ + public Result> search(@ApiParam(value = "查询条件对象", required = true) @RequestBody JournalQueryModel queryModel, + @ApiParam(value = "页码", required = true) @PathVariable int page, + @ApiParam(value = "每页条数", required = true) @PathVariable int size){ return journalService.search(queryModel, page, size); } @ApiOperation(value = "新增期刊", notes = "新增期刊") @RequestMapping(value = "/add", method = RequestMethod.POST) - public Result add(@ApiParam(value = "Header中的token信息", required = true) @RequestHeader("Authorization") String token, - @ApiParam(value = "期刊信息", required = true) @Valid @RequestBody JournalAddModel addModel){ - return journalService.add(token, addModel); + public Result add(@ApiParam(value = "期刊信息", required = true) @Valid @RequestBody JournalAddModel addModel){ + return journalService.add(addModel); } @ApiOperation(value = "查询期刊详情", notes = "查询期刊详情") @RequestMapping(value="/{id}",method= RequestMethod.GET) - public Result findOne(@ApiParam(value = "期刊ID", required = true) @PathVariable String id){ + public Result findOne(@ApiParam(value = "期刊ID", required = true) @PathVariable String id){ return journalService.findOne(id); } @ApiOperation(value = "更新期刊", notes = "更新期刊") @RequestMapping(value = "/{id}", method = RequestMethod.PUT) public Result update(@ApiParam(value = "期刊ID", required = true) @PathVariable String id, - @ApiParam(value = "Header中的token信息", required = true) @RequestHeader("Authorization") String token, @ApiParam(value = "期刊信息", required = true) @RequestBody JournalAddModel updateModel){ - return journalService.update(id, token, updateModel); + return journalService.update(id, updateModel); } @ApiOperation(value = "发布期刊", notes = "发布期刊") @@ -74,7 +73,7 @@ public class CMSJournalController { return journalService.updateJournalState(id, state); } - @ApiOperation(value = "删除单条期刊", notes = "删除单条期刊") + @ApiOperation(value = "删除单条期刊", notes = "物理删除") @RequestMapping(value="/{id}", method= RequestMethod.DELETE) public Result delete(@ApiParam(value = "期刊ID", required = true) @PathVariable String id){ return journalService.deleteById(id); diff --git a/luoo_music/src/main/java/com/luoo/music/controller/CMSSongController.java b/luoo_music/src/main/java/com/luoo/music/controller/CMSSongController.java index 40aea76..0982fc8 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/CMSSongController.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/CMSSongController.java @@ -2,9 +2,12 @@ package com.luoo.music.controller; import api.PageResult; import api.Result; -import com.luoo.music.request.cms.SongAddModel; -import com.luoo.music.request.cms.SongQueryModel; -import com.luoo.music.response.cms.SongVO; +import com.luoo.music.dto.request.cms.SongAddModel; +import com.luoo.music.dto.request.cms.SongQueryModel; +import com.luoo.music.dto.response.cms.SongBaseRespDto; +import com.luoo.music.dto.response.cms.SongLyricRespDto; +import com.luoo.music.dto.response.cms.SongListRespDto; +import com.luoo.music.dto.response.cms.SongRespDto; import com.luoo.music.service.CMSSongService; import com.luoo.music.service.S3Service; import io.swagger.annotations.*; @@ -30,9 +33,9 @@ public class CMSSongController { @ApiOperation(value = "查询歌曲列表", notes = "查询歌曲列表") @RequestMapping(value="/search/{page}/{size}",method= RequestMethod.POST) - public Result> search(@ApiParam(value = "查询条件") @RequestBody SongQueryModel queryModel, - @ApiParam(value = "页码", required = true) @PathVariable int page, - @ApiParam(value = "每页条数", required = true)@PathVariable int size){ + public Result> search(@ApiParam(value = "查询条件") @RequestBody SongQueryModel queryModel, + @ApiParam(value = "页码", required = true) @PathVariable int page, + @ApiParam(value = "每页条数", required = true)@PathVariable int size){ return songService.search(queryModel, page, size); } @@ -45,11 +48,11 @@ public class CMSSongController { @ApiOperation(value = "查询歌曲详情", notes = "查询歌曲详情") @RequestMapping(value="/{id}",method= RequestMethod.GET) - public Result findOne(@ApiParam(value = "歌曲ID", required = true) @PathVariable String id){ + public Result findOne(@ApiParam(value = "歌曲ID", required = true) @PathVariable String id){ return songService.findOne(id); } - @ApiOperation(value = "更新歌曲信息", notes = "更新歌曲信息") + @ApiOperation(value = "更新歌曲信息", notes = "同步更新期刊音乐表中的歌曲信息,更新歌曲文件时需同时传上传歌曲时返回的size、duration") @RequestMapping(value = "/{id}", method = RequestMethod.PUT) public Result update(@ApiParam(value = "Header中的token信息", required = true) @RequestHeader("Authorization") String token, @ApiParam(value = "歌曲ID", required = true) @PathVariable String id, @@ -57,20 +60,20 @@ public class CMSSongController { return songService.update(id, token, updateModel); } - @ApiOperation(value = "更新歌曲启停状态", notes = "更新歌曲启停状态") + @ApiOperation(value = "更新歌曲启停状态", notes = "更新歌曲启停状态,并删除期刊中歌曲的引用") @RequestMapping(value="/update/state/{id}",method= RequestMethod.PUT) public Result updateSongState(@ApiParam(value = "歌曲ID", required = true) @PathVariable String id, @ApiParam(value = "歌曲状态,0:停用,1:启用", required = true) @RequestBody String state){ return songService.updateSongState(id, state); } - @ApiOperation(value = "获取歌曲歌词", notes = "获取歌曲歌词") + @ApiOperation(value = "获取歌曲歌词", notes = "读取表中的歌曲内容,内容为空时会检查歌词url是否存在并读取") @RequestMapping(value="/lyric/{id}",method= RequestMethod.GET) - public Result querySongLyric(@ApiParam(value = "歌曲ID", required = true) @PathVariable String id){ + public Result querySongLyric(@ApiParam(value = "歌曲ID", required = true) @PathVariable String id){ return songService.querySongLyric(id); } - @ApiOperation(value = "更新歌曲歌词", notes = "更新歌曲歌词") + @ApiOperation(value = "更新歌曲歌词", notes = "更新表中歌词内容,并将歌词写入文件上传至s3") @RequestMapping(value="/update/lyric/{id}",method= RequestMethod.PUT) public Result updateSongLyric(@ApiParam(value = "歌曲ID", required = true) @PathVariable String id, @ApiParam(value = "歌曲歌词信息", required = true) @RequestBody String lyric){ @@ -78,21 +81,22 @@ public class CMSSongController { } - @ApiOperation(value = "删除单条歌曲", notes = "删除单条歌曲") + @ApiOperation(value = "删除单条歌曲", notes = "物理删除") @RequestMapping(value="/{id}", method= RequestMethod.DELETE) public Result delete(@ApiParam(value = "歌曲ID", required = true) @PathVariable String id){ return songService.deleteById(id); } - @ApiOperation(value = "上传歌曲", notes = "上传歌曲") + @ApiOperation(value = "上传歌曲", notes = "上传的歌曲不会新增到曲库,会上传到s3,返回临时url,保存时才会新增能到曲库") @RequestMapping(value = "/upload/song", method = RequestMethod.POST) public Result batchUpload(@ApiParam(value = "歌曲文件", required = true) @RequestParam("file") MultipartFile file) { return s3Service.upload(file, Boolean.TRUE); } - @ApiOperation(value = "批量上传歌曲", notes = "批量上传歌曲") + @ApiOperation(value = "批量上传歌曲", notes = "上传的歌曲会新增到曲库") @RequestMapping(value = "/batch", method = RequestMethod.POST) - public Result batchUpload(@ApiParam(value = "多歌曲文件", required = true) @RequestParam("files") List files) { - return songService.batchUpload(files); + public Result> batchUpload(@ApiParam(value = "Header中的token信息", required = true) @RequestHeader("Authorization") String token, + @ApiParam(value = "多歌曲文件", required = true) @RequestParam("files") List files) { + return songService.batchUpload(token, files); } } diff --git a/luoo_music/src/main/java/com/luoo/music/dao/JournalDao.java b/luoo_music/src/main/java/com/luoo/music/dao/JournalDao.java index 2b3a2a1..480d99d 100644 --- a/luoo_music/src/main/java/com/luoo/music/dao/JournalDao.java +++ b/luoo_music/src/main/java/com/luoo/music/dao/JournalDao.java @@ -17,7 +17,7 @@ import java.util.Set; */ public interface JournalDao extends JpaRepository,JpaSpecificationExecutor{ - List findByJournalNo(String journalNo); + List findByJournalNo(String journalNo); List findByJournalNoIn(Set journalNos); @@ -46,15 +46,6 @@ public interface JournalDao extends JpaRepository,JpaSpecificati @Transactional @Query("UPDATE Journal j SET j.state = :state WHERE j.id = :id") int updateJournalState(@Param("id") String id, @Param("state") String state); - - @Query(value = "select * from tb_journal where id in ?1 order by field(id,?1)", nativeQuery = true) - List orderByField(List objectIds); - - @Query(value = "select 1 from tb_journal where ?1 in (select journal_no_tmp.journal_no from (select * from tb_journal order by ABS(journal_no) desc limit 10) as journal_no_tmp) limit 1", nativeQuery = true) - Integer isLatest10ByJournalNo(String journalNo); - - @Query(value = "select 1 from tb_journal where ?1 in (select journal_no_tmp.id from (select * from tb_journal order by ABS(journal_no) desc limit 10) as journal_no_tmp) limit 1", nativeQuery = true) - Integer isLatest10ByJournalId(String journalId); @Modifying @Transactional @@ -71,6 +62,15 @@ public interface JournalDao extends JpaRepository,JpaSpecificati @Query("UPDATE Journal j SET j.comment = j.comment - 1 WHERE j.id = :journalId") int decreaseCommentCount(@Param("journalId") String journalId); + @Query(value = "select * from tb_journal where id in ?1 order by field(id,?1)", nativeQuery = true) + List orderByField(List objectIds); + + @Query(value = "select 1 from tb_journal where ?1 in (select journal_no_tmp.journal_no from (select * from tb_journal order by ABS(journal_no) desc limit 10) as journal_no_tmp) limit 1", nativeQuery = true) + Integer isLatest10ByJournalNo(String journalNo); + + @Query(value = "select 1 from tb_journal where ?1 in (select journal_no_tmp.id from (select * from tb_journal order by ABS(journal_no) desc limit 10) as journal_no_tmp) limit 1", nativeQuery = true) + Integer isLatest10ByJournalId(String journalId); + @Query(value = "select max(ABS(journal_no)) FROM tb_journal", nativeQuery = true) int getMaxJournalNo(); 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 7e79875..f1848bd 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 @@ -1,6 +1,5 @@ package com.luoo.music.dao; -import com.luoo.music.pojo.Journal; import com.luoo.music.pojo.SongInfo; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; @@ -23,13 +22,8 @@ public interface SongInfoDao extends JpaRepository, JpaSpecific @Modifying @Transactional - @Query("UPDATE SongInfo s SET s.lyric = :lyric WHERE s.id = :id") - int updateSongLyric(@Param("id") String id, @Param("lyric") String lyric); - - @Modifying - @Transactional - @Query("UPDATE SongInfo s SET s.lyric = :lyric,s.lyricUrl = :lyricUrl WHERE s.id = :id") - int updateSongLyricAndUrl(@Param("id") String id, @Param("lyric") String lyric,@Param("lyricUrl") String lyricUrl); + @Query("UPDATE SongInfo s SET s.lyric = :lyric, s.lyricUrl = :lyricUrl WHERE s.id = :id") + int updateSongLyricAndUrl(@Param("id") String id, @Param("lyric") String lyric, @Param("lyricUrl") String lyricUrl); List findByIdIn(List ids); diff --git a/luoo_music/src/main/java/com/luoo/music/request/cms/JournalAddModel.java b/luoo_music/src/main/java/com/luoo/music/dto/request/cms/JournalAddModel.java similarity index 74% rename from luoo_music/src/main/java/com/luoo/music/request/cms/JournalAddModel.java rename to luoo_music/src/main/java/com/luoo/music/dto/request/cms/JournalAddModel.java index 1de764c..016a5a2 100644 --- a/luoo_music/src/main/java/com/luoo/music/request/cms/JournalAddModel.java +++ b/luoo_music/src/main/java/com/luoo/music/dto/request/cms/JournalAddModel.java @@ -1,4 +1,4 @@ -package com.luoo.music.request.cms; +package com.luoo.music.dto.request.cms; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; @@ -14,11 +14,11 @@ import java.util.List; @ApiModel(value = "增加或修改期刊对象") public class JournalAddModel { - @NotBlank(message = "期刊的期刊必填") + @NotBlank(message = "期刊编号必填") @ApiModelProperty(value = "期刊编号") private String journalNo; - @NotBlank(message = "期刊的标题必填") + @NotBlank(message = "期刊标题必填") @ApiModelProperty(value = "期刊标题") private String title; @@ -28,14 +28,15 @@ public class JournalAddModel { @ApiModelProperty(value = "发布人ID") private String userId; - @NotBlank(message = "期刊的封面必填") @ApiModelProperty(value = "期刊封面路径") private String image; @ApiModelProperty(value = "关联歌曲,音乐ID") private List songs; - @NotBlank(message = "期刊的文案必填") - @ApiModelProperty(value = "期刊内容") + @ApiModelProperty(value = "期刊文案") private String content; + + @ApiModelProperty(value = "期刊简介") + private String summary; } diff --git a/luoo_music/src/main/java/com/luoo/music/request/cms/JournalPublishReq.java b/luoo_music/src/main/java/com/luoo/music/dto/request/cms/JournalPublishReq.java similarity index 91% rename from luoo_music/src/main/java/com/luoo/music/request/cms/JournalPublishReq.java rename to luoo_music/src/main/java/com/luoo/music/dto/request/cms/JournalPublishReq.java index 1f7fbcb..38c8a06 100644 --- a/luoo_music/src/main/java/com/luoo/music/request/cms/JournalPublishReq.java +++ b/luoo_music/src/main/java/com/luoo/music/dto/request/cms/JournalPublishReq.java @@ -1,4 +1,4 @@ -package com.luoo.music.request.cms; +package com.luoo.music.dto.request.cms; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; diff --git a/luoo_music/src/main/java/com/luoo/music/request/cms/JournalQueryModel.java b/luoo_music/src/main/java/com/luoo/music/dto/request/cms/JournalQueryModel.java similarity index 95% rename from luoo_music/src/main/java/com/luoo/music/request/cms/JournalQueryModel.java rename to luoo_music/src/main/java/com/luoo/music/dto/request/cms/JournalQueryModel.java index 72d050b..cb2f1ca 100644 --- a/luoo_music/src/main/java/com/luoo/music/request/cms/JournalQueryModel.java +++ b/luoo_music/src/main/java/com/luoo/music/dto/request/cms/JournalQueryModel.java @@ -1,4 +1,4 @@ -package com.luoo.music.request.cms; +package com.luoo.music.dto.request.cms; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; diff --git a/luoo_music/src/main/java/com/luoo/music/request/cms/SongAddModel.java b/luoo_music/src/main/java/com/luoo/music/dto/request/cms/SongAddModel.java similarity index 96% rename from luoo_music/src/main/java/com/luoo/music/request/cms/SongAddModel.java rename to luoo_music/src/main/java/com/luoo/music/dto/request/cms/SongAddModel.java index 1caa4de..13e3b0c 100644 --- a/luoo_music/src/main/java/com/luoo/music/request/cms/SongAddModel.java +++ b/luoo_music/src/main/java/com/luoo/music/dto/request/cms/SongAddModel.java @@ -1,4 +1,4 @@ -package com.luoo.music.request.cms; +package com.luoo.music.dto.request.cms; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; diff --git a/luoo_music/src/main/java/com/luoo/music/request/cms/SongQueryModel.java b/luoo_music/src/main/java/com/luoo/music/dto/request/cms/SongQueryModel.java similarity index 94% rename from luoo_music/src/main/java/com/luoo/music/request/cms/SongQueryModel.java rename to luoo_music/src/main/java/com/luoo/music/dto/request/cms/SongQueryModel.java index ef275f5..59d28ab 100644 --- a/luoo_music/src/main/java/com/luoo/music/request/cms/SongQueryModel.java +++ b/luoo_music/src/main/java/com/luoo/music/dto/request/cms/SongQueryModel.java @@ -1,4 +1,4 @@ -package com.luoo.music.request.cms; +package com.luoo.music.dto.request.cms; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; diff --git a/luoo_music/src/main/java/com/luoo/music/dto/response/cms/JournalListRespDto.java b/luoo_music/src/main/java/com/luoo/music/dto/response/cms/JournalListRespDto.java new file mode 100644 index 0000000..29eefd4 --- /dev/null +++ b/luoo_music/src/main/java/com/luoo/music/dto/response/cms/JournalListRespDto.java @@ -0,0 +1,91 @@ +package com.luoo.music.dto.response.cms; + +import com.luoo.music.pojo.Journal; +import com.luoo.music.util.Constants; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import org.apache.commons.lang.StringUtils; + +import java.util.List; +import java.util.Map; +import java.util.Objects; + +/** + * @author locust + */ +@Data +@ApiModel(description = "期刊列表信息") +public class JournalListRespDto { + + @ApiModelProperty(value = "主键ID") + private String id; + + @ApiModelProperty(value = "期刊编号") + private String journalNo; + + @ApiModelProperty(value = "期刊标题") + private String title; + + @ApiModelProperty(value = "期刊封面") + private String image; + + @ApiModelProperty(value = "歌曲数") + private Long songCount; + + @ApiModelProperty(value = "评论数") + private Long comment; + + @ApiModelProperty(value = "播放数") + private Long visits; + + @ApiModelProperty(value = "发布作者id") + private String userId; + + @ApiModelProperty(value = "发布作者昵称") + private String userName; + + @ApiModelProperty(value = "创建时间") + private String createTime; + + @ApiModelProperty(value = "发布时间") + private String pubTime; + + @ApiModelProperty(value = "期刊启停状态,0:停用,1:启用") + private String state; + + @ApiModelProperty(value = "期刊发布状态,0:待发布,1:已发布") + private String isPublish; + + @ApiModelProperty(value = "期刊标签, 标签ID的一个集合") + private List tag; + + public static JournalListRespDto convertPojo(Journal journal, + Map> journalTagMap, + Map journalSongCountMap) { + JournalListRespDto response = new JournalListRespDto(); + response.setId(journal.getId()); + response.setJournalNo(journal.getJournalNo()); + response.setTitle(journal.getTitle()); + if (StringUtils.isNotBlank(journal.getImage())) { + response.setImage(Constants.MUSIC_RESOURCE_PREFIX + journal.getImage()); + } else { + response.setImage(""); + } + if (!Objects.isNull(journal.getCreateTime())) { + response.setCreateTime(journal.getCreateTime().format(Constants.formatter)); + } + response.setState(journal.getState()); + response.setIsPublish(journal.getIsPublish()); + if (!Objects.isNull(journal.getPubTime())) { + response.setPubTime(journal.getPubTime().format(Constants.formatter)); + } + response.setUserId(journal.getUserId()); + response.setUserName(journal.getUserName()); + response.setSongCount(journalSongCountMap.get(journal.getJournalNo())); + response.setTag(journalTagMap.get(journal.getId())); + response.setVisits(journal.getVisits()); + response.setComment(journal.getComment()); + return response; + } +} diff --git a/luoo_music/src/main/java/com/luoo/music/dto/response/cms/JournalRespDto.java b/luoo_music/src/main/java/com/luoo/music/dto/response/cms/JournalRespDto.java new file mode 100644 index 0000000..4d48fdc --- /dev/null +++ b/luoo_music/src/main/java/com/luoo/music/dto/response/cms/JournalRespDto.java @@ -0,0 +1,75 @@ +package com.luoo.music.dto.response.cms; + +import com.luoo.music.pojo.Journal; +import com.luoo.music.util.Constants; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import org.apache.commons.lang.StringUtils; + +import java.util.List; + +/** + * @author locust + */ +@Data +@ApiModel(description = "期刊编辑信息") +public class JournalRespDto { + + @ApiModelProperty(value = "主键ID") + private String id; + + @ApiModelProperty(value = "期刊编号") + private String journalNo; + + @ApiModelProperty(value = "期刊标题") + private String title; + + @ApiModelProperty(value = "期刊封面") + private String image; + + @ApiModelProperty(value = "期刊简介信息") + private String summary; + + @ApiModelProperty(value = "期刊文案信息") + private String content; + + @ApiModelProperty(value = "发布作者id") + private String userId; + + @ApiModelProperty(value = "发布作者昵称") + private String userName; + + @ApiModelProperty(value = "期刊启停状态,0:停用,1:启用") + private String state; + + @ApiModelProperty(value = "期刊发布状态,0:待发布,1:已发布") + private String isPublish; + + @ApiModelProperty(value = "期刊标签, 标签ID的一个集合") + private List tag; + + @ApiModelProperty(value = "期刊歌曲信息") + private List songs; + + public static JournalRespDto convertPojo(Journal journal, List tagInfo, List songInfo) { + JournalRespDto response = new JournalRespDto(); + response.setId(journal.getId()); + response.setJournalNo(journal.getJournalNo()); + response.setTitle(journal.getTitle()); + if (StringUtils.isNotBlank(journal.getImage())) { + response.setImage(Constants.MUSIC_RESOURCE_PREFIX + journal.getImage()); + } else { + response.setImage(""); + } + response.setSummary(journal.getSummary()); + response.setContent(journal.getContent()); + response.setState(journal.getState()); + response.setIsPublish(journal.getIsPublish()); + response.setUserId(journal.getUserId()); + response.setUserName(journal.getUserName()); + response.setTag(tagInfo); + response.setSongs(songInfo); + return response; + } +} diff --git a/luoo_music/src/main/java/com/luoo/music/dto/response/cms/SongBaseRespDto.java b/luoo_music/src/main/java/com/luoo/music/dto/response/cms/SongBaseRespDto.java new file mode 100644 index 0000000..8391624 --- /dev/null +++ b/luoo_music/src/main/java/com/luoo/music/dto/response/cms/SongBaseRespDto.java @@ -0,0 +1,64 @@ +package com.luoo.music.dto.response.cms; + +import com.luoo.music.pojo.SongInfo; +import com.luoo.music.util.CommonUtil; +import com.luoo.music.util.Constants; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import org.apache.commons.lang.StringUtils; + +/** + * @author locust + */ +@Data +@ApiModel(description = "歌曲基本信息") +public class SongBaseRespDto { + + @ApiModelProperty(value = "主键ID") + private String id; + + @ApiModelProperty(value = "歌曲名称") + private String name; + + @ApiModelProperty(value = "歌手或乐队") + private String artist; + + @ApiModelProperty(value = "专辑") + private String album; + + @ApiModelProperty(value = "封面路径") + private String image; + + @ApiModelProperty(value = "文件大小") + private String size; + + @ApiModelProperty(value = "歌曲时长") + private String duration; + + @ApiModelProperty(value = "文件路径") + private String url; + + + public static SongBaseRespDto convertPojo(SongInfo song) { + SongBaseRespDto response = new SongBaseRespDto(); + String id = song.getId(); + response.setId(id); + response.setName(song.getName()); + response.setArtist(song.getArtist() == null ? "" : song.getArtist()); + response.setAlbum(song.getAlbum() == null ? "" : song.getAlbum()); + if (StringUtils.isNotBlank(song.getImage())) { + response.setImage(Constants.SONG_RESOURCE_PREFIX + song.getImage()); + } else { + response.setImage(""); + } + response.setSize(CommonUtil.formatSongSize(song.getSize())); + response.setDuration(CommonUtil.formatSongDuration(song.getDuration())); + if (StringUtils.isNotBlank(song.getUrl())) { + response.setUrl(Constants.SONG_RESOURCE_PREFIX + song.getUrl()); + } else { + response.setUrl(""); + } + return response; + } +} diff --git a/luoo_music/src/main/java/com/luoo/music/dto/response/cms/SongListRespDto.java b/luoo_music/src/main/java/com/luoo/music/dto/response/cms/SongListRespDto.java new file mode 100644 index 0000000..6c2b915 --- /dev/null +++ b/luoo_music/src/main/java/com/luoo/music/dto/response/cms/SongListRespDto.java @@ -0,0 +1,97 @@ +package com.luoo.music.dto.response.cms; + +import com.luoo.music.pojo.SongInfo; +import com.luoo.music.util.CommonUtil; +import com.luoo.music.util.Constants; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import org.apache.commons.lang.StringUtils; + +import java.util.List; +import java.util.Map; +import java.util.Objects; + +/** + * @author locust + */ +@Data +@ApiModel(description = "歌曲列表信息") +public class SongListRespDto { + + @ApiModelProperty(value = "主键ID") + private String id; + + @ApiModelProperty(value = "歌曲名称") + private String name; + + @ApiModelProperty(value = "歌手或乐队") + private String artist; + + @ApiModelProperty(value = "专辑") + private String album; + + @ApiModelProperty(value = "封面路径") + private String image; + + @ApiModelProperty(value = "文件大小") + private String size; + + @ApiModelProperty(value = "歌曲时长") + private String duration; + + @ApiModelProperty(value = "修改时间") + private String updateTime; + + @ApiModelProperty(value = "歌曲启停状态,0:停用,1:启用") + private String state; + + @ApiModelProperty(value = "上传人员ID") + private String userId; + + @ApiModelProperty(value = "上传人员昵称") + private String userName; + + @ApiModelProperty(value = "文件路径") + private String url; + + @ApiModelProperty(value = "关联期刊") + private List journal; + + @ApiModelProperty(value = "歌曲风格") + private List tags; + + public static SongListRespDto convertPojo(SongInfo song, + Map> songJournalMap, + Map> songTagMap) { + SongListRespDto response = new SongListRespDto(); + String id = song.getId(); + response.setId(id); + response.setName(song.getName()); + response.setArtist(song.getArtist()); + response.setAlbum(song.getAlbum()); + response.setTags(songTagMap.get(id)); + if (StringUtils.isNotBlank(song.getImage())) { + response.setImage(Constants.SONG_RESOURCE_PREFIX + song.getImage()); + } else { + response.setImage(""); + } + response.setSize(CommonUtil.formatSongSize(song.getSize())); + response.setDuration(CommonUtil.formatSongDuration(song.getDuration())); + response.setJournal(songJournalMap.get(id)); + if (Objects.isNull(song.getUpdateTime())) { + response.setUpdateTime(""); + } else { + response.setUpdateTime(song.getUpdateTime().format(Constants.formatter)); + } + response.setState(song.getState()); + response.setUserId(song.getUserId()); + response.setUserName(song.getUserName()); + if (StringUtils.isNotBlank(song.getUrl())) { + response.setUrl(Constants.SONG_RESOURCE_PREFIX + song.getUrl()); + } else { + response.setUrl(""); + } + return response; + } +} diff --git a/luoo_music/src/main/java/com/luoo/music/dto/response/cms/SongLyricRespDto.java b/luoo_music/src/main/java/com/luoo/music/dto/response/cms/SongLyricRespDto.java new file mode 100644 index 0000000..d9714a7 --- /dev/null +++ b/luoo_music/src/main/java/com/luoo/music/dto/response/cms/SongLyricRespDto.java @@ -0,0 +1,19 @@ +package com.luoo.music.dto.response.cms; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +/** + * @author locust + */ +@Data +@ApiModel(description = "歌曲歌词信息") +public class SongLyricRespDto { + + @ApiModelProperty(value = "主键ID") + private String id; + + @ApiModelProperty(value = "歌词") + private String lyric; +} diff --git a/luoo_music/src/main/java/com/luoo/music/dto/response/cms/SongRespDto.java b/luoo_music/src/main/java/com/luoo/music/dto/response/cms/SongRespDto.java new file mode 100644 index 0000000..1b56dfa --- /dev/null +++ b/luoo_music/src/main/java/com/luoo/music/dto/response/cms/SongRespDto.java @@ -0,0 +1,53 @@ +package com.luoo.music.dto.response.cms; + +import com.luoo.music.pojo.SongInfo; +import com.luoo.music.util.Constants; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import org.springframework.util.CollectionUtils; + +import java.util.List; + +/** + * @author locust + */ +@Data +@ApiModel(description = "歌曲编辑信息") +public class SongRespDto { + + @ApiModelProperty(value = "主键ID") + private String id; + + @ApiModelProperty(value = "歌曲名称") + private String name; + + @ApiModelProperty(value = "歌手或乐队") + private String artist; + + @ApiModelProperty(value = "专辑") + private String album; + + @ApiModelProperty(value = "封面路径") + private String image; + + @ApiModelProperty(value = "文件路径") + private String url; + + @ApiModelProperty(value = "歌曲风格") + private List tags; + + public static SongRespDto convertPojo(SongInfo song, List tagIdsBySongId) { + SongRespDto response = new SongRespDto(); + response.setId(song.getId()); + response.setName(song.getName()); + response.setArtist(song.getArtist()); + response.setAlbum(song.getAlbum()); + response.setUrl(Constants.SONG_RESOURCE_PREFIX + song.getUrl()); + response.setImage(Constants.SONG_RESOURCE_PREFIX + song.getImage()); + if (!CollectionUtils.isEmpty(tagIdsBySongId)) { + response.setTags(tagIdsBySongId); + } + return response; + } +} diff --git a/luoo_music/src/main/java/com/luoo/music/response/cms/UploadRespDto.java b/luoo_music/src/main/java/com/luoo/music/dto/response/cms/UploadRespDto.java similarity index 71% rename from luoo_music/src/main/java/com/luoo/music/response/cms/UploadRespDto.java rename to luoo_music/src/main/java/com/luoo/music/dto/response/cms/UploadRespDto.java index 5c4792b..c39de5b 100644 --- a/luoo_music/src/main/java/com/luoo/music/response/cms/UploadRespDto.java +++ b/luoo_music/src/main/java/com/luoo/music/dto/response/cms/UploadRespDto.java @@ -1,4 +1,4 @@ -package com.luoo.music.response.cms; +package com.luoo.music.dto.response.cms; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; @@ -10,12 +10,9 @@ import java.util.List; * @author locust */ @Data -@ApiModel(description = "文件上传后的路径") +@ApiModel(description = "文件临时上传后响应信息") public class UploadRespDto { - @ApiModelProperty(value = "部分路径") - private String partUrl; - @ApiModelProperty(value = "全路径") private String fullUrl; diff --git a/luoo_music/src/main/java/com/luoo/music/pojo/Journal.java b/luoo_music/src/main/java/com/luoo/music/pojo/Journal.java index 224b798..7f97b70 100644 --- a/luoo_music/src/main/java/com/luoo/music/pojo/Journal.java +++ b/luoo_music/src/main/java/com/luoo/music/pojo/Journal.java @@ -22,7 +22,8 @@ import java.time.LocalDateTime; @Entity @DynamicInsert @DynamicUpdate -@Table(name="tb_journal") +// @Table(name="tb_journal") +@Table(name="tb_journal_copy1") @EntityListeners(AuditingEntityListener.class) public class Journal implements Serializable{ @@ -41,6 +42,10 @@ public class Journal implements Serializable{ * 封面路径 */ private String image; + /** + * 概要 + */ + private String summary; /** * 期刊文案 */ diff --git a/luoo_music/src/main/java/com/luoo/music/pojo/Tag.java b/luoo_music/src/main/java/com/luoo/music/pojo/Tag.java index c9ac810..d64cf77 100644 --- a/luoo_music/src/main/java/com/luoo/music/pojo/Tag.java +++ b/luoo_music/src/main/java/com/luoo/music/pojo/Tag.java @@ -7,12 +7,11 @@ import org.springframework.data.annotation.CreatedDate; import org.springframework.data.annotation.LastModifiedDate; import org.springframework.data.jpa.domain.support.AuditingEntityListener; -import javax.persistence.Entity; -import javax.persistence.EntityListeners; -import javax.persistence.Id; -import javax.persistence.Table; +import javax.persistence.*; import java.io.Serializable; import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; import java.util.Objects; @Getter @@ -85,6 +84,12 @@ public class Tag implements Serializable { @LastModifiedDate private LocalDateTime updateTime; + @ManyToMany(mappedBy = "tags") + private List journals = new ArrayList<>(); + + @ManyToMany(mappedBy = "tags") + private List songs = new ArrayList<>(); + @Override public final boolean equals(Object o) { if (this == o) return true; diff --git a/luoo_music/src/main/java/com/luoo/music/response/cms/JournalVO.java b/luoo_music/src/main/java/com/luoo/music/response/cms/JournalVO.java deleted file mode 100644 index 5bbd874..0000000 --- a/luoo_music/src/main/java/com/luoo/music/response/cms/JournalVO.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.luoo.music.response.cms; - -import com.luoo.music.response.cms.SongVO; -import io.swagger.annotations.ApiModel; -import io.swagger.annotations.ApiModelProperty; -import lombok.Data; - -import java.util.List; - -/** - * @author locust - */ -@Data -@ApiModel(description = "期刊信息") -public class JournalVO { - - @ApiModelProperty(value = "主键ID") - private String id; - - @ApiModelProperty(value = "期刊编号") - private String journalNo; - - @ApiModelProperty(value = "期刊标题") - private String title; - - @ApiModelProperty(value = "期刊封面") - private String image; - - @ApiModelProperty(value = "期刊标签, 标签ID的一个集合") - private List tag; - - @ApiModelProperty(value = "歌曲数") - private Long songCount; - - @ApiModelProperty(value = "评论数") - private Long comment; - - @ApiModelProperty(value = "播放数") - private Long visits; - - @ApiModelProperty(value = "发布作者id") - private String userId; - - @ApiModelProperty(value = "发布作者昵称") - private String userName; - - @ApiModelProperty(value = "创建时间") - private String createTime; - - @ApiModelProperty(value = "发布时间") - private String pubTime; - - @ApiModelProperty(value = "期刊启停状态,0:停用,1:启用") - private String state; - - @ApiModelProperty(value = "期刊发布状态,0:待发布,1:已发布") - private String isPublish; - - @ApiModelProperty(value = "期刊歌曲信息") - private List songs; - - @ApiModelProperty(value = "期刊简介信息") - private String content; -} diff --git a/luoo_music/src/main/java/com/luoo/music/response/cms/SongVO.java b/luoo_music/src/main/java/com/luoo/music/response/cms/SongVO.java deleted file mode 100644 index ee95e63..0000000 --- a/luoo_music/src/main/java/com/luoo/music/response/cms/SongVO.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.luoo.music.response.cms; - -import io.swagger.annotations.ApiModel; -import io.swagger.annotations.ApiModelProperty; -import lombok.Data; - -import java.util.List; - -/** - * @author locust - */ -@Data -@ApiModel(description = "歌曲信息") -public class SongVO { - - @ApiModelProperty(value = "主键ID") - private String id; - - @ApiModelProperty(value = "歌曲名称") - private String name; - - @ApiModelProperty(value = "歌手或乐队") - private String artist; - - @ApiModelProperty(value = "专辑") - private String album; - - @ApiModelProperty(value = "歌曲风格") - private List tags; - - @ApiModelProperty(value = "封面路径") - private String image; - - @ApiModelProperty(value = "文件大小") - private String size; - - @ApiModelProperty(value = "歌曲时长") - private String duration; - - @ApiModelProperty(value = "关联期刊") - private List journal; - - @ApiModelProperty(value = "修改时间") - private String updateTime; - - @ApiModelProperty(value = "歌曲启停状态,0:停用,1:启用") - private String state; - - @ApiModelProperty(value = "上传人员ID") - private String userId; - - @ApiModelProperty(value = "上传人员昵称") - private String userName; - - @ApiModelProperty(value = "文件路径") - private String url; - - @ApiModelProperty(value = "歌词") - private String lyric; -} diff --git a/luoo_music/src/main/java/com/luoo/music/service/CMSJournalService.java b/luoo_music/src/main/java/com/luoo/music/service/CMSJournalService.java index 4d359de..30719c3 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/CMSJournalService.java +++ b/luoo_music/src/main/java/com/luoo/music/service/CMSJournalService.java @@ -5,13 +5,13 @@ import api.Result; import com.luoo.music.client.UserClient; import com.luoo.music.dao.*; import com.luoo.music.pojo.*; -import com.luoo.music.request.cms.JournalAddModel; -import com.luoo.music.request.cms.JournalPublishReq; -import com.luoo.music.request.cms.JournalQueryModel; -import com.luoo.music.response.cms.JournalVO; -import com.luoo.music.response.cms.SongVO; +import com.luoo.music.dto.request.cms.JournalAddModel; +import com.luoo.music.dto.request.cms.JournalPublishReq; +import com.luoo.music.dto.request.cms.JournalQueryModel; +import com.luoo.music.dto.response.cms.JournalListRespDto; +import com.luoo.music.dto.response.cms.JournalRespDto; +import com.luoo.music.dto.response.cms.SongBaseRespDto; import com.luoo.music.util.Constants; -import dto.UserLoginDto; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; @@ -21,8 +21,6 @@ import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import util.IdWorker; -import util.JwtUtil; -import util.StringTools; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; @@ -30,7 +28,6 @@ import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; import java.time.LocalDateTime; import java.util.*; -import java.util.function.Function; import java.util.stream.Collectors; /** @@ -60,20 +57,19 @@ public class CMSJournalService { /** * 分页 + 条件检索 - * * @param param * @param page * @param size * @return */ - public Result> search(JournalQueryModel param, int page, int size) { - List result = new ArrayList<>(); -// Sort sort = new Sort(Sort.Direction.DESC, "journalNo"); + public Result> search(JournalQueryModel param, int page, int size) { + List result = new ArrayList<>(); PageRequest pageRequest = PageRequest.of(page - 1, size); Page journalPage; + List journalIdsByTags = null; if (!Objects.isNull(param)) { - List journalIdsByTags = null; if (!CollectionUtils.isEmpty(param.getTags())) { + // 查询标签对应的全部期刊 journalIdsByTags = journalTagDao.findJournalIdsByTags(param.getTags()); if (journalIdsByTags.size() == 0) { return Result.success(new PageResult<>(0L, result)); @@ -82,7 +78,6 @@ public class CMSJournalService { Specification journalSpecification = buildSearchSpecification(param, journalIdsByTags); journalPage = journalDao.findAll(journalSpecification, pageRequest); } else { - List journalIdsByTags = null; Specification journalSpecification = buildSearchSpecification(param, journalIdsByTags); journalPage = journalDao.findAll(journalSpecification, pageRequest); } @@ -90,29 +85,11 @@ public class CMSJournalService { List content = journalPage.getContent(); if (!CollectionUtils.isEmpty(content)) { List ids = content.stream().map(Journal::getId).collect(Collectors.toList()); - Map> journalTagMap = tagInfo(ids); + Map> journalTagMap = journalTagInfo(ids); List journalNos = content.stream().map(Journal::getJournalNo).collect(Collectors.toList()); - Map songCountMap = songCountInfo(journalNos); + Map journalSongCountMap = journalSongCountInfo(journalNos); for (Journal item : content) { - JournalVO response = new JournalVO(); - response.setId(item.getId()); - response.setJournalNo(item.getJournalNo()); - response.setTitle(item.getTitle()); - response.setImage(Constants.MUSIC_RESOURCE_PREFIX + item.getImage()); - if (!Objects.isNull(item.getCreateTime())) { - response.setCreateTime(item.getCreateTime().format(Constants.formatter)); - } - response.setState(item.getState()); - response.setIsPublish(item.getIsPublish()); - if (!Objects.isNull(item.getPubTime())) { - response.setPubTime(item.getPubTime().format(Constants.formatter)); - } - response.setUserId(item.getUserId()); - response.setUserName(item.getUserName()); - response.setSongCount(songCountMap.get(item.getJournalNo())); - response.setTag(journalTagMap.get(item.getId())); - response.setVisits(item.getVisits()); - response.setComment(item.getComment()); + JournalListRespDto response = JournalListRespDto.convertPojo(item, journalTagMap, journalSongCountMap); result.add(response); } } @@ -121,7 +98,6 @@ public class CMSJournalService { /** * 检索条件 - * * @param param * @return */ @@ -159,35 +135,17 @@ public class CMSJournalService { }; } - /** - * 期刊的音乐数量 - * - * @param journalNos - * @return - */ - private Map songCountInfo(List journalNos) { - Map songCountMap = new HashMap<>(); - if (!CollectionUtils.isEmpty(journalNos)) { - List> journalSongCountList = journalSongDao.findSongCountByJournalNos(journalNos); - songCountMap = journalSongCountList.stream() - .collect(Collectors.toMap(map -> String.valueOf(map.get("journalNo")), - map -> Long.valueOf(String.valueOf(map.get("songCount"))))); - } - return songCountMap; - } - /** * 期刊的标签信息 - * * @param ids * @return */ - private Map> tagInfo(List ids) { + private Map> journalTagInfo(List ids) { Map> journalTagMap = new HashMap<>(); List journalTagList = journalTagDao.findByJournalIds(ids); - Set tagIdSet = journalTagList.stream().map(JournalTag::getTagId).collect(Collectors.toSet()); - List tagList = tagDao.findByIdIn(tagIdSet); - Map idTagMap = tagList.stream().collect(Collectors.toMap(Tag::getId, obj -> obj)); + // Set tagIdSet = journalTagList.stream().map(JournalTag::getTagId).collect(Collectors.toSet()); + // List tagList = tagDao.findByIdIn(tagIdSet); + // Map idTagMap = tagList.stream().collect(Collectors.toMap(Tag::getId, obj -> obj)); List list; for (JournalTag item : journalTagList) { String journalId = item.getJournalId(); @@ -196,74 +154,90 @@ public class CMSJournalService { } else { list = new ArrayList<>(); } - if (idTagMap.containsKey(item.getTagId())) { - Tag tag = idTagMap.get(item.getTagId()); - list.add(tag.getId()); - } + list.add(item.getTagId()); journalTagMap.put(journalId, list); } return journalTagMap; } /** - * 新增期刊 - * - * @param param + * 期刊的音乐数量 + * @param journalNos * @return */ - public Result add(String token, JournalAddModel param) { - List byJournalNo = journalDao.findByJournalNo(param.getJournalNo()); - if (byJournalNo.size() > 0) { - return Result.failed("保存失败,期刊编号已存在"); - } - String srcKey = param.getImage().substring(param.getImage().indexOf(Constants.TEMP_KEY_PREFIX)); - String image = moveJournalImage(srcKey, param); - if (StringUtils.isNotBlank(image)) { - Journal journal = buildJournal(null, param, token); - String id = String.valueOf(idWorker.nextId()); - journal.setId(id); - journal.setImage(image); - journalDao.save(journal); - batchSaveJournalRelatesInfo(id, param); - return Result.success(); - } else { - return Result.failed("保存失败"); + private Map journalSongCountInfo(List journalNos) { + Map songCountMap = new HashMap<>(); + if (!CollectionUtils.isEmpty(journalNos)) { + List> journalSongCountList = journalSongDao.findSongCountByJournalNos(journalNos); + songCountMap = journalSongCountList.stream() + .collect(Collectors.toMap(map -> String.valueOf(map.get("journalNo")), + map -> Long.valueOf(String.valueOf(map.get("songCount"))))); } + return songCountMap; } + + /** - * 移动对象 - * + * 新增期刊 * @param param * @return */ - private String moveJournalImage(String srcKey, JournalAddModel param) { - String image = param.getImage(); - String suffix = image.substring(image.lastIndexOf(Constants.DOT)); - String destKeySuffix = String.format("%05d/00%s", Integer.parseInt(param.getJournalNo()), suffix); - String destKey = Constants.MUSIC_KEY_PREFIX + destKeySuffix; - int copy = s3Service.copy(Constants.BUCKET, srcKey, destKey); - if (copy > 0) { - return destKeySuffix; + public Result add(JournalAddModel param) { + List byJournalNo = journalDao.findByJournalNo(param.getJournalNo()); + if (byJournalNo.size() > 0) { + return Result.failed("保存失败,期刊编号已存在"); } - return null; + String image = null; + if (StringUtils.isNotBlank(param.getImage())) { + // 新增时有图片一定是 temp/ + String srcKey = param.getImage().substring(param.getImage().indexOf(Constants.TEMP_KEY_PREFIX)); + image = moveJournalImage(srcKey, param); + if (StringUtils.isBlank(image)) { + return Result.failed("保存失败"); + } + } + String id = String.valueOf(idWorker.nextId()); + Journal journal = buildJournal(id, param); + journal.setImage(image); + journalDao.save(journal); + // 保存期刊的标签、歌曲信息 + batchSaveJournalRelatesInfo(id, param); + return Result.success(); } + /** + * 移动期刊封面 + * @param srcKey + * @param param + * @return + */ + private String moveJournalImage(String srcKey, JournalAddModel param) { + String image = param.getImage(); + if (StringUtils.isNotBlank(image) && StringUtils.isNotBlank(srcKey)) { + String suffix = image.substring(image.lastIndexOf(Constants.DOT)); + String destKeySuffix = String.format("%05d/%d%s", Integer.parseInt(param.getJournalNo()), idWorker.nextId(), suffix); + String destKey = Constants.MUSIC_KEY_PREFIX + destKeySuffix; + int copy = s3Service.copy(Constants.BUCKET, srcKey, destKey); + if (copy > 0) { + return destKeySuffix; + } + } + return null; + } + /** * 构建对象 - * * @param id * @param param - * @param token * @return */ - private Journal buildJournal(String id, JournalAddModel param, String token) { + private Journal buildJournal(String id, JournalAddModel param) { Journal journal = new Journal(); - if (!StringUtils.isBlank(id)) { - journal.setId(id); - } + journal.setId(id); journal.setJournalNo(param.getJournalNo()); journal.setTitle(param.getTitle()); + journal.setSummary(param.getSummary()); journal.setContent(param.getContent()); UserInfo userInfo = userClient.queryUserInfoById(param.getUserId()); if (!Objects.isNull(userInfo)) { @@ -276,7 +250,6 @@ public class CMSJournalService { /** * 保存期刊的标签、音乐信息 - * * @param journalId * @param param */ @@ -293,43 +266,53 @@ public class CMSJournalService { } journalTagDao.saveAll(journalTagList); //save tag 中文名到 journal 中 - updateJouranlNameChTags(journalId); + updateJournalNameChTags(journalId); } // 歌曲信息 if (!CollectionUtils.isEmpty(param.getSongs())) { List songList = songDao.findByIdIn(param.getSongs()); Map idSongMap = songList.stream().collect(Collectors.toMap(SongInfo::getId, obj -> obj)); List journalSongList = new ArrayList<>(); - int num = 0; - for (String item : param.getSongs()) { - if (idSongMap.containsKey(item)) { - num += 1; - SongInfo song = idSongMap.get(item); - JournalSong journalSong = new JournalSong(); - journalSong.setId(String.valueOf(idWorker.nextId())); - journalSong.setName(song.getName()); - journalSong.setAlbum(song.getAlbum()); - journalSong.setArtist(song.getArtist()); - journalSong.setUrl(song.getUrl()); - journalSong.setImage(song.getImage()); - journalSong.setSongId(item); - journalSong.setJournalNo(param.getJournalNo()); - journalSong.setSongNo(num); - journalSong.setSongId(song.getId()); - journalSongList.add(journalSong); - } - } - s3Service.fromSongToMusic(journalSongList); - journalSongDao.saveAll(journalSongList); + for (int i = 0; i < param.getSongs().size(); i++) { + String item = param.getSongs().get(i); + if (idSongMap.containsKey(item)) { + SongInfo song = idSongMap.get(item); + JournalSong journalSong = new JournalSong(); + journalSong.setId(String.valueOf(idWorker.nextId())); + journalSong.setName(song.getName()); + journalSong.setAlbum(song.getAlbum()); + journalSong.setArtist(song.getArtist()); + if (StringUtils.isNotBlank(song.getUrl())) { + journalSong.setUrl(Constants.SONG_KEY_PREFIX + song.getUrl()); + } else { + journalSong.setUrl(song.getUrl()); + } + if (StringUtils.isNotBlank(song.getImage())) { + journalSong.setImage(Constants.SONG_KEY_PREFIX + song.getImage()); + } else { + journalSong.setImage(song.getImage()); + } + journalSong.setSongId(item); + journalSong.setJournalNo(param.getJournalNo()); + journalSong.setSongNo(i+1); + if (StringUtils.isNotBlank(song.getLyricUrl())) { + journalSong.setLyricUrl(Constants.SONG_KEY_PREFIX + song.getLyricUrl()); + } else { + journalSong.setLyricUrl(song.getLyricUrl()); + } + journalSongList.add(journalSong); + } + } + journalSongDao.saveAll(journalSongList); } } - private void updateJouranlNameChTags(String journalId) { + private void updateJournalNameChTags(String journalId) { Optional optional=journalDao.findById(journalId); if(optional.isPresent()) { List tags=tagDao.getTagNameChByJournalId(journalId); if(!tags.isEmpty()) { - String tagStr=tags.stream().distinct().sorted().collect(Collectors.joining(",")); + String tagStr=tags.stream().distinct().sorted().collect(Collectors.joining(Constants.COMMA)); Journal journal=optional.get(); journal.setNameChTags(tagStr); journalDao.save(journal); @@ -339,246 +322,109 @@ public class CMSJournalService { /** * 查询期刊详情 - * * @param id * @return */ - public Result findOne(String id) { + public Result findOne(String id) { Optional optional=journalDao.findById(id); if(!optional.isPresent()) { - return Result.failed("无法找到期刊: "+id); + return Result.failed("无法找到期刊: " + id); } Journal journal = optional.get(); - JournalVO response = new JournalVO(); - response.setId(journal.getId()); - response.setJournalNo(journal.getJournalNo()); - response.setTitle(journal.getTitle()); - response.setTag(journalTagDao.findTagsByJournalId(id)); - response.setUserId(journal.getUserId()); - if (StringUtils.isNotBlank(journal.getImage())) { - response.setImage(Constants.MUSIC_RESOURCE_PREFIX + journal.getImage()); - } - response.setSongs(journalSongInfo(journal.getJournalNo())); - response.setContent(journal.getContent()); + List tagInfo = journalTagDao.findTagsByJournalId(id); + List songInfo = journalSongInfo(journal.getJournalNo()); + JournalRespDto response = JournalRespDto.convertPojo(journal, tagInfo, songInfo); return Result.success(response); } /** - * 期刊的音乐信息 - * + * 期刊的歌曲信息 * @param journalNo * @return */ - private List journalSongInfo(String journalNo) { - List list = new ArrayList<>(); + private List journalSongInfo(String journalNo) { + List list = new ArrayList<>(); List journalSongList = journalSongDao.findByJournalNo(journalNo); if (!CollectionUtils.isEmpty(journalSongList)) { for (JournalSong item : journalSongList) { - SongVO songVO = new SongVO(); + SongBaseRespDto songVO = new SongBaseRespDto(); songVO.setId(item.getSongId()); songVO.setName(item.getName()); songVO.setArtist(item.getArtist()); songVO.setAlbum(item.getAlbum()); + if (StringUtils.isNotBlank(item.getImage())) { + songVO.setImage(Constants.RESOURCE_PREFIX + item.getImage()); + } else { + songVO.setImage(item.getImage()); + } if (StringUtils.isNotBlank(item.getUrl())) { songVO.setUrl(Constants.RESOURCE_PREFIX + item.getUrl()); - } - if (StringUtils.isNotBlank(item.getImage())) { - songVO.setImage(Constants.RESOURCE_PREFIX + item.getImage()); - } + } else { + songVO.setUrl(item.getUrl()); + } list.add(songVO); } } return list; } - /** - * 更新期刊 - * - * @param id - * @param token - * @param param - * @return - */ - public Result update(String id, String token, JournalAddModel param) { - Optional optional = journalDao.findById(id); + /** + * 更新期刊 + * @param id + * @param param + * @return + */ + public Result update(String id, JournalAddModel param) { + // TODO + Optional optional = journalDao.findById(id); if (!optional.isPresent()) { return Result.failed("找不到期刊: " + id); } - Journal journal = optional.get(); - updateJournal(journal, param, token); - journalDao.save(journal); - redisTemplate.opsForValue().set("journal_" + id, journal); - - batchUpdateJournalRelatesInfo(id, journal.getJournalNo(), param); + // 当前编辑页面期刊编号可修改 + Journal oldJournal = optional.get(); + List journals = journalDao.findByJournalNo(param.getJournalNo()); + if (!oldJournal.getJournalNo().equals(param.getJournalNo()) && journals.size() > 0) { + return Result.failed("期刊编号已存在"); + } + Journal newJournal = buildJournal(id, param); + String srcKey = null; + if (StringUtils.isNotBlank(param.getImage())) { + if (param.getImage().contains(Constants.TEMP_KEY_PREFIX)) { + srcKey = param.getImage().substring(param.getImage().indexOf(Constants.TEMP_KEY_PREFIX)); + } else { + if (!oldJournal.getJournalNo().equals(param.getJournalNo())) { + srcKey = param.getImage().substring(param.getImage().indexOf(Constants.MUSIC_KEY_PREFIX)); + } + } + if (StringUtils.isNotBlank(srcKey)) { + String image = moveJournalImage(srcKey, param); + if (StringUtils.isBlank(image)) { + return Result.failed("更新失败"); + } + newJournal.setImage(image); + } else { + newJournal.setImage(oldJournal.getImage()); + } + } else { + newJournal.setImage(param.getImage()); + } + newJournal.setState(oldJournal.getState()); + newJournal.setIsPublish(oldJournal.getIsPublish()); + newJournal.setIsScheduled(oldJournal.getIsScheduled()); + newJournal.setVisits(oldJournal.getVisits()); + newJournal.setComment(oldJournal.getComment()); + newJournal.setCreateTime(oldJournal.getCreateTime()); + newJournal.setPubTime(oldJournal.getPubTime()); + Journal journal = journalDao.save(newJournal); + redisTemplate.opsForValue().set("journal_" + id, journal); + // 删除旧的关联信息,更新的关联信息 + batchDeleteJournalRelatesInfo(id, oldJournal.getJournalNo()); + batchSaveJournalRelatesInfo(id, param); return Result.success(); } - private void batchUpdateJournalRelatesInfo(String journalId, String journalNo, JournalAddModel param) { - // 标签信息 - List updateTags = param.getTag(); - List currentTags = journalTagDao.findTagsByJournalId(journalId); - if (!CollectionUtils.isEmpty(updateTags) && !isCollectionEqual(currentTags, updateTags)) { - // 清理当前journalTag - journalTagDao.deleteByJournalId(journalId); - List updatedJournalTags = updateTags.stream().map(t -> createJournalTag(t, journalId)) - .collect(Collectors.toList()); - journalTagDao.saveAll(updatedJournalTags); - - //update tag 中文名到 journal 中 - updateJouranlNameChTags(journalId); - } - - // 歌曲信息 - List updateSongs = param.getSongs(); - if (!CollectionUtils.isEmpty(updateSongs)) { - List songList = songDao.orderByField(updateSongs); - List journalSongs=journalSongDao.findByJournalNoOrderBySongNo(journalNo); - - Map songId2JournalSongIdMap=journalSongs.stream().collect(Collectors.toMap(JournalSong::getSongId, JournalSong::getId)); - - List updatedJournalSong = new ArrayList<>(); -// for (int i = 0; i < songList.size(); i++) { update: zjb from:gary 歌曲保存顺序问题 -// SongInfo song = songList.get(i); - for (int i = 0; i < param.getSongs().size(); i++) { - int finalI = i; - SongInfo song = songList.stream() - .filter(e -> e.getId().equals(param.getSongs().get(finalI))) - .findFirst().orElse(null); - if(null == song){ - continue; - } - JournalSong journalSong = new JournalSong(); - - journalSong.setId(songId2JournalSongIdMap.getOrDefault(song.getId(), String.valueOf(idWorker.nextId()))); - - journalSong.setName(song.getName()); - journalSong.setAlbum(song.getAlbum()); - journalSong.setArtist(song.getArtist()); - journalSong.setUrl(Constants.SONG_KEY_PREFIX+song.getUrl()); - journalSong.setImage(Constants.SONG_KEY_PREFIX+song.getImage()); - journalSong.setLyricUrl(Constants.SONG_KEY_PREFIX+song.getLyricUrl()); - - journalSong.setSongId(song.getId()); - journalSong.setJournalNo(journalNo); - journalSong.setSongNo(i+1); - - updatedJournalSong.add(journalSong); - } - // 清理当前journalSong - journalSongDao.deleteByJournalNo(journalNo); - journalSongDao.saveAll(updatedJournalSong); - } - } - private boolean isSameContent(SongInfo song, JournalSong previousJournalSong) { - boolean isSameName=isSameString(song.getName(),previousJournalSong.getName()); - boolean isSameAlbum=isSameString(song.getAlbum(),previousJournalSong.getAlbum()); - boolean isSameArtist=isSameString(song.getArtist(),previousJournalSong.getArtist()); - boolean isSameUrl=isSameUrl(song.getUrl(),previousJournalSong.getUrl()); - boolean isSameImage=isSameUrl(song.getImage(),previousJournalSong.getImage()); - boolean isSameLyricUrl=isSameUrl(song.getLyricUrl(),previousJournalSong.getLyricUrl()); - return isSameName&&isSameAlbum&&isSameArtist&&isSameUrl&&isSameImage&&isSameLyricUrl; - } - - private boolean isSameUrl(String songUrl, String journalSongUrl) { - return (Constants.SONG_KEY_PREFIX+songUrl).equals(journalSongUrl); - } - - private boolean isSameString(String current, String previous) { - boolean isCurrentEmpty=StringTools.isEmpty(current); - boolean isPreviousEmpty=StringTools.isEmpty(previous); - if((isCurrentEmpty&&isPreviousEmpty)||(!isCurrentEmpty&&!isPreviousEmpty&¤t.equals(previous))) { - return true; - } - return false; - } - - private boolean isSameContent(List current, List update) { - if ((null == current && null == update) || (current.isEmpty() && update.isEmpty())) { - return true; - } - if (current.size() != update.size()) { - return false; - } - Collections.sort(current,Comparator.comparing(SongInfo::getId)); - Collections.sort(update,Comparator.comparing(JournalSong::getSongId)); - for (int i = 0; i < current.size(); i++) { - if (!isSameContent(current.get(i),update.get(i))) { - return false; - } - } - return true; - } - - private JournalTag createJournalTag(String tagId, String journalId) { - JournalTag journalTag = new JournalTag(); - journalTag.setId(String.valueOf(idWorker.nextId())); - journalTag.setJournalId(journalId); - journalTag.setTagId(tagId); - return journalTag; - } - - private boolean isCollectionEqual(List current, List update) { - if ((null == current && null == update) || (current.isEmpty() && update.isEmpty())) { - return true; - } - if (current.size() != update.size()) { - return false; - } - Collections.sort(current); - Collections.sort(update); - for (int i = 0; i < current.size(); i++) { - if (!current.get(i).equals(update.get(i))) { - return false; - } - } - return true; - } - - private void updateJournal(Journal journal, JournalAddModel param, String token) { - String title = param.getTitle(); - if (!StringTools.isEmpty(title)) { - journal.setTitle(title); - } - - String content = param.getContent(); - if (!StringTools.isEmpty(content)) { - journal.setContent(content); - } - - String userId = param.getUserId(); - if (!StringTools.isEmpty(userId)) { - UserInfo userInfo = userClient.queryUserInfoById(userId); - if (!Objects.isNull(userInfo)) { - journal.setUserId(userInfo.getId()); - journal.setUserName(userInfo.getName()); - journal.setUserType(userInfo.getType()); - } - } - - String tempImage = param.getImage(); - if (!StringTools.isEmpty(tempImage) && tempImage.contains(Constants.TEMP_KEY_PREFIX)) { - String srcKey = param.getImage().substring(param.getImage().indexOf(Constants.TEMP_KEY_PREFIX)); - String image = copyJournalImage(srcKey, journal.getJournalNo()); - if (null != image) { - journal.setImage(image); - } - } - } - - private String copyJournalImage(String srcKey, String journalNo) { - String destKeySuffix = String.format("%05d/%s", Integer.parseInt(journalNo), - srcKey.replace(Constants.TEMP_KEY_PREFIX, "")); - String destKey = Constants.MUSIC_KEY_PREFIX + destKeySuffix; - int copy = s3Service.copy(Constants.BUCKET, srcKey, destKey); - if (copy > 0) { - return destKeySuffix; - } - return null; - } - /** * 删除期刊的标签、音乐信息 - * * @param journalId * @param journalNo */ @@ -589,7 +435,6 @@ public class CMSJournalService { /** * 发布期刊 - * * @param id * @param queryModel * @return @@ -611,7 +456,6 @@ public class CMSJournalService { /** * 更新期刊启停状态 - * * @param id * @param state * @return @@ -623,7 +467,6 @@ public class CMSJournalService { /** * 删除期刊 - * * @param id * @return */ diff --git a/luoo_music/src/main/java/com/luoo/music/service/CMSSongService.java b/luoo_music/src/main/java/com/luoo/music/service/CMSSongService.java index cc71f88..97d5e84 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/CMSSongService.java +++ b/luoo_music/src/main/java/com/luoo/music/service/CMSSongService.java @@ -2,12 +2,14 @@ package com.luoo.music.service; import api.PageResult; import api.Result; -import com.luoo.music.client.UserClient; import com.luoo.music.dao.*; import com.luoo.music.pojo.*; -import com.luoo.music.request.cms.SongAddModel; -import com.luoo.music.request.cms.SongQueryModel; -import com.luoo.music.response.cms.SongVO; +import com.luoo.music.dto.request.cms.SongAddModel; +import com.luoo.music.dto.request.cms.SongQueryModel; +import com.luoo.music.dto.response.cms.SongBaseRespDto; +import com.luoo.music.dto.response.cms.SongListRespDto; +import com.luoo.music.dto.response.cms.SongLyricRespDto; +import com.luoo.music.dto.response.cms.SongRespDto; import com.luoo.music.util.CommonUtil; import com.luoo.music.util.Constants; import dto.UserLoginDto; @@ -57,7 +59,7 @@ public class CMSSongService { @Autowired private TagDao tagDao; @Autowired - private UserClient userClient; + private JwtUtil jwtUtil; /** * 分页 + 条件检索 @@ -66,11 +68,10 @@ public class CMSSongService { * @param size * @return */ - public Result> search(SongQueryModel param, int page, int size) { - List result = new ArrayList<>(); + public Result> search(SongQueryModel param, int page, int size) { + List result = new ArrayList<>(); Sort sort = new Sort(Sort.Direction.DESC, "id"); PageRequest pageRequest = PageRequest.of(page - 1, size, sort); - // PageRequest pageRequest = PageRequest.of(page - 1, size); Page songPage; if (!Objects.isNull(param)) { Specification songSpecification = buildSearchSpecification(param); @@ -82,29 +83,12 @@ public class CMSSongService { List content = songPage.getContent(); if (!CollectionUtils.isEmpty(content)) { List ids = content.stream().map(SongInfo::getId).collect(Collectors.toList()); + // 查询歌曲对应的期刊信息 Map> songJournalMap = buildSongJournalInfo(ids); + // 查询歌曲对应的标签信息 Map> songTagMap = buildSongTagInfo(ids); for (SongInfo item : content) { - SongVO response = new SongVO(); - String id = item.getId(); - response.setId(id); - response.setName(item.getName()); - response.setArtist(item.getArtist()); - response.setAlbum(item.getAlbum()); - response.setTags(songTagMap.get(id)); - if (StringUtils.isNotBlank(item.getImage())) { - response.setImage(Constants.SONG_RESOURCE_PREFIX + item.getImage()); - } - response.setSize(CommonUtil.formatSongSize(item.getSize())); - response.setDuration(CommonUtil.formatSongDuration(item.getDuration())); - response.setJournal(songJournalMap.get(id)); - response.setUpdateTime(item.getUpdateTime().format(Constants.formatter)); - response.setState(item.getState()); - response.setUserId(item.getUserId()); - response.setUserName(item.getUserName()); - if (StringUtils.isNotBlank(item.getUrl())) { - response.setUrl(Constants.SONG_RESOURCE_PREFIX + item.getUrl()); - } + SongListRespDto response = SongListRespDto.convertPojo(item, songJournalMap, songTagMap); result.add(response); } } @@ -143,14 +127,19 @@ public class CMSSongService { }; } + /** + * 歌曲的期刊信息 + * @param ids + * @return + */ private Map> buildSongJournalInfo(List ids) { Map> result = new HashMap<>(); - List bySongId = journalSongDao.findBySongIds(ids); - if (!CollectionUtils.isEmpty(bySongId)) { - Set idSet = bySongId.stream().map(JournalSong::getJournalNo).collect(Collectors.toSet()); + List journalSongList = journalSongDao.findBySongIds(ids); + if (!CollectionUtils.isEmpty(journalSongList)) { + Set idSet = journalSongList.stream().map(JournalSong::getJournalNo).collect(Collectors.toSet()); List journalList = journalDao.findByJournalNoIn(idSet); Map idJournalMap = journalList.stream().collect(Collectors.toMap(Journal::getJournalNo, obj -> obj)); - for (JournalSong item : bySongId) { + for (JournalSong item : journalSongList) { String songId = item.getSongId(); String journalNo = item.getJournalNo(); List list; @@ -169,14 +158,19 @@ public class CMSSongService { return result; } + /** + * 歌曲的标签信息 + * @param ids + * @return + */ private Map> buildSongTagInfo(List ids) { Map> result = new HashMap<>(); - List bySongId = songTagDao.findBySongIds(ids); - if (!CollectionUtils.isEmpty(bySongId)) { - Set idSet = bySongId.stream().map(SongTag::getTagId).collect(Collectors.toSet()); + List songTagList = songTagDao.findBySongIds(ids); + if (!CollectionUtils.isEmpty(songTagList)) { + Set idSet = songTagList.stream().map(SongTag::getTagId).collect(Collectors.toSet()); List tagList = tagDao.findByIdIn(idSet); Map idTagMap = tagList.stream().collect(Collectors.toMap(Tag::getId, obj -> obj)); - for (SongTag item : bySongId) { + for (SongTag item : songTagList) { String songId = item.getSongId(); String tagId = item.getTagId(); List list; @@ -187,7 +181,6 @@ public class CMSSongService { } if (idTagMap.keySet().contains(tagId)) { Tag tag = idTagMap.get(tagId); - // list.add(tag.getNameCh()); list.add(tag.getId()); } result.put(songId, list); @@ -197,7 +190,7 @@ public class CMSSongService { } /** - * 新增曲目 + * 新增歌曲 * @param token * @param param * @return @@ -206,67 +199,59 @@ public class CMSSongService { String id = String.valueOf(idWorker.nextId()); String url = null; if (StringUtils.isNotBlank(param.getUrl())) { - url = moveSongFile(id, param); + url = moveSongFileOrImage(id, param.getUrl()); if (StringUtils.isBlank(url)) { return Result.failed("保存失败"); } } String image = null; if (StringUtils.isNotBlank(param.getImage())) { - image = moveSongImage(id, param); + image = moveSongFileOrImage(id, param.getImage()); if (StringUtils.isBlank(image)) { return Result.failed("保存失败"); } } SongInfo song = buildSong(id, param, token); - song.setId(id); song.setUrl(url); song.setImage(image); songDao.save(song); + // 保存歌曲的标签信息 batchSaveSongTagInfo(id, param); return Result.success(); } private SongInfo buildSong(String id, SongAddModel param, String token) { SongInfo song = new SongInfo(); - if (!StringUtils.isBlank(id)) { - song.setId(id); - } + song.setId(id); song.setName(param.getName()); song.setArtist(param.getArtist()); song.setAlbum(param.getAlbum()); - song.setLyric(param.getLyric()); - UserInfo userInfo = userClient.queryUserInfoById(param.getUserId()); - if (!Objects.isNull(userInfo)) { - song.setUserId(userInfo.getId()); - song.setUserName(userInfo.getName()); - song.setUserType(userInfo.getType()); + // 通过 token 获取用户信息 + UserLoginDto userLoginDto = getUserInfoByToken(token); + if (!Objects.isNull(userLoginDto)) { + song.setUserId(userLoginDto.getUserId()); + song.setUserName(userLoginDto.getNickName()); + song.setUserType(userLoginDto.getRoles()); } return song; } - private String moveSongFile(String id, SongAddModel param) { - String url = param.getUrl(); - String srcKey = url.substring(url.indexOf(Constants.TEMP_KEY_PREFIX)); - String suffix = url.substring(url.lastIndexOf(Constants.DOT)); - String destKeySuffix = id + "/00" + suffix; - String destKey = Constants.SONG_KEY_PREFIX + destKeySuffix; - int copy = s3Service.copy(Constants.BUCKET, srcKey, destKey); - if (copy > 0) { - return destKeySuffix; - } - return null; - } - - private String moveSongImage(String id, SongAddModel param) { - String image = param.getImage(); - String srcKey = image.substring(image.indexOf(Constants.TEMP_KEY_PREFIX)); - String suffix = image.substring(image.lastIndexOf(Constants.DOT)); - String destKeySuffix = id + "/00" + suffix; - String destKey = Constants.SONG_KEY_PREFIX + destKeySuffix; - int copy = s3Service.copy(Constants.BUCKET, srcKey, destKey); - if (copy > 0) { - return destKeySuffix; + /** + * 歌曲文件或封面 temp/ copy to song/ + * @param id + * @param path + * @return + */ + private String moveSongFileOrImage(String id, String path) { + if (StringUtils.isNotBlank(path)) { + String srcKey = path.substring(path.indexOf(Constants.TEMP_KEY_PREFIX)); + String suffix = path.substring(path.lastIndexOf(Constants.DOT)); + String destKeySuffix = String.format("%s/%d%s", id, idWorker.nextId(), suffix); + String destKey = Constants.SONG_KEY_PREFIX + destKeySuffix; + int copy = s3Service.copy(Constants.BUCKET, srcKey, destKey); + if (copy > 0) { + return destKeySuffix; + } } return null; } @@ -291,20 +276,14 @@ public class CMSSongService { } /** - * 查询曲目详情 + * 查询歌曲详情 * @param id * @return */ - public Result findOne(String id) { + public Result findOne(String id) { SongInfo song = songDao.findById(id).get(); - SongVO response = new SongVO(); - response.setId(song.getId()); - response.setName(song.getName()); - response.setArtist(song.getArtist()); - response.setAlbum(song.getAlbum()); - response.setTags(songTagDao.findTagIdsBySongId(id)); - response.setUrl(Constants.SONG_RESOURCE_PREFIX + song.getUrl()); - response.setImage(Constants.SONG_RESOURCE_PREFIX + song.getImage()); + List tagIdsBySongId = songTagDao.findTagIdsBySongId(id); + SongRespDto response = SongRespDto.convertPojo(song, tagIdsBySongId); return Result.success(response); } @@ -318,9 +297,9 @@ public class CMSSongService { public Result update(String id, String token, SongAddModel param) { SongInfo song = buildSong(id, param, token); SongInfo oldSong = songDao.findById(id).get(); - String url = null; + // 路径包含temp/说明歌曲文件或封面修改了 if (param.getUrl().contains(Constants.TEMP_KEY_PREFIX)) { - url = moveSongFile(id, param); + String url = moveSongFileOrImage(id, param.getUrl()); if (StringUtils.isBlank(url)) { return Result.failed("更新失败"); } @@ -332,9 +311,8 @@ public class CMSSongService { song.setSize(oldSong.getSize()); song.setDuration(oldSong.getDuration()); } - String image = null; if (param.getImage().contains(Constants.TEMP_KEY_PREFIX)) { - image = moveSongImage(id, param); + String image = moveSongFileOrImage(id, param.getImage()); if (StringUtils.isBlank(image)) { return Result.failed("更新失败"); } @@ -344,9 +322,13 @@ public class CMSSongService { } song.setState(oldSong.getState()); song.setCreateTime(oldSong.getCreateTime()); + song.setLyric(oldSong.getLyric()); + song.setLyricUrl(oldSong.getLyricUrl()); songDao.save(song); + // 更新标签信息 songTagDao.deleteBySongId(id); batchSaveSongTagInfo(id, param); + // 更新期刊歌曲表的信息 batchUpdateJournalSongInfo(id, song); return Result.success(); } @@ -365,8 +347,17 @@ public class CMSSongService { item.setAlbum(song.getAlbum()); item.setUrl(song.getUrl()); item.setImage(song.getImage()); + if (StringUtils.isNotBlank(song.getUrl())) { + item.setUrl(Constants.SONG_KEY_PREFIX + song.getUrl()); + } else { + item.setUrl(song.getUrl()); + } + if (StringUtils.isNotBlank(song.getImage())) { + item.setImage(Constants.SONG_KEY_PREFIX + song.getImage()); + } else { + item.setImage(song.getImage()); + } } - s3Service.fromSongToMusic(journalSongList); journalSongDao.saveAll(journalSongList); } } @@ -379,6 +370,7 @@ public class CMSSongService { */ public Result updateSongState(String id, String state) { songDao.updateSongState(id, state); + // 歌曲停用删除期刊歌曲表的歌曲信息 if ("0".equals(state)) { journalSongDao.deleteBySongId(id); } @@ -390,18 +382,21 @@ public class CMSSongService { * @param id * @return */ - public Result querySongLyric(String id) { - SongVO result = new SongVO(); + public Result querySongLyric(String id) { SongInfo song = songDao.findById(id).get(); - if (StringUtils.isNotBlank(song.getLyric())) { - result.setLyric(song.getLyric()); - } else { - if (StringUtils.isNotBlank(song.getUrl())) { - //String url = song.getUrl(); - //String lyricSuffix = url.substring(0, url.lastIndexOf(Constants.DOT)) + ".lyric"; - String lyricUrl = Constants.RESOURCE_PREFIX + Constants.SONG_KEY_PREFIX + song.getLyricUrl(); - String lyric = readLyric(lyricUrl); - result.setLyric(lyric); + SongLyricRespDto result = new SongLyricRespDto(); + if (!Objects.isNull(song)) { + result.setId(song.getId()); + if (StringUtils.isNotBlank(song.getLyric())) { + result.setLyric(song.getLyric()); + } else { + if (StringUtils.isNotBlank(song.getUrl())) { + String lyricUrl = Constants.SONG_KEY_PREFIX + song.getLyricUrl(); + if (s3Service.checkFileExist(Constants.BUCKET, lyricUrl)) { + String lyric = readLyric(lyricUrl); + result.setLyric(lyric); + } + } } } return Result.success(result); @@ -415,16 +410,28 @@ public class CMSSongService { */ public Result updateSongLyric(String id, String lyric) { if (StringUtils.isNotBlank(lyric)) { - String lyricUrl=id + "/"+idWorker.nextId()+".lyric"; + String lyricUrl = String.format("%s/%d%s", id, idWorker.nextId(), ".lyric"); String key = Constants.SONG_KEY_PREFIX + lyricUrl; - songDao.updateSongLyricAndUrl(id, lyric, lyricUrl); - s3Service.uploadText(Constants.BUCKET, key, lyric); + int upload = s3Service.uploadText(Constants.BUCKET, key, lyric); + if (upload > 0) { + songDao.updateSongLyricAndUrl(id, lyric, lyricUrl); + // 更新期刊歌曲表的歌词路径 + List journalSongList = journalSongDao.findBySongId(id); + if (journalSongList.size() > 0) { + for (JournalSong item : journalSongList) { + item.setLyricUrl(key); + } + journalSongDao.saveAll(journalSongList); + } + } else { + return Result.failed("歌词更新失败"); + } } return Result.success(); } /** - * 删除曲目 + * 删除歌曲 * @param id * @return */ @@ -441,16 +448,19 @@ public class CMSSongService { /** * 批量上传歌曲,获取不到歌曲的歌手乐队、专辑 + * @param token * @param files * @return */ - public Result batchUpload(List files) { + public Result> batchUpload(String token, List files) { + List result = new ArrayList<>(); + UserLoginDto userInfo = getUserInfoByToken(token); for (MultipartFile file : files) if (!file.isEmpty()) { String id = String.valueOf(idWorker.nextId()); String originalFilename = file.getOriginalFilename(); String suffix = originalFilename.substring(originalFilename.lastIndexOf(Constants.DOT)); - String keySuffix = id + "/00" + suffix; + String keySuffix = String.format("%s/%d%s", id, idWorker.nextId(), suffix); String key = Constants.SONG_KEY_PREFIX + keySuffix; int upload = s3Service.upload(Constants.BUCKET, key, file); if (upload > 0) { @@ -460,10 +470,17 @@ public class CMSSongService { song.setUrl(keySuffix); song.setSize(file.isEmpty() ? 0L : file.getSize()); song.setDuration(CommonUtil.getSongDuration(file)); - songDao.save(song); + if (!Objects.isNull(userInfo)) { + song.setUserId(userInfo.getUserId()); + song.setUserName(userInfo.getNickName()); + song.setUserType(userInfo.getRoles()); + } + SongInfo save = songDao.save(song); + SongBaseRespDto songBaseRespDto = SongBaseRespDto.convertPojo(save); + result.add(songBaseRespDto); } } - return Result.success(); + return Result.success(result); } /** @@ -471,7 +488,7 @@ public class CMSSongService { * @param token * @return */ - /*private UserLoginDto getUserInfoByToken(String token) { + private UserLoginDto getUserInfoByToken(String token) { if (StringUtils.isNotBlank(token)) { try { UserLoginDto user = jwtUtil.getUserLoginDto(token); @@ -482,7 +499,7 @@ public class CMSSongService { } } return null; - }*/ + } /** * 读取歌词文件 @@ -492,7 +509,8 @@ public class CMSSongService { private String readLyric(String lyricUrl) { try { // 创建 URL 对象 - URL url = new URL(lyricUrl); + String fullPath = Constants.RESOURCE_PREFIX + lyricUrl; + URL url = new URL(fullPath); // 打开 HTTP 连接 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // 设置请求方法 diff --git a/luoo_music/src/main/java/com/luoo/music/service/S3Service.java b/luoo_music/src/main/java/com/luoo/music/service/S3Service.java index 3011882..9cc1086 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/S3Service.java +++ b/luoo_music/src/main/java/com/luoo/music/service/S3Service.java @@ -1,11 +1,9 @@ package com.luoo.music.service; import api.Result; -import com.luoo.music.pojo.JournalSong; -import com.luoo.music.response.cms.UploadRespDto; +import com.luoo.music.dto.response.cms.UploadRespDto; import com.luoo.music.util.CommonUtil; import com.luoo.music.util.Constants; -import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; @@ -17,14 +15,11 @@ import util.IdWorker; import java.io.ByteArrayInputStream; import java.io.IOException; -import java.util.List; @Service public class S3Service { - - @Autowired private S3Client s3Client; @Autowired @@ -35,7 +30,7 @@ public class S3Service { * 期刊添加音乐时复制音乐的文件 * @param journalSongList */ - public void fromSongToMusic(List journalSongList) { + /*public void fromSongToMusic(List journalSongList) { journalSongList.parallelStream().forEach(this::fromSongToMusic); } private void fromSongToMusic(JournalSong item) { @@ -70,7 +65,8 @@ public class S3Service { copy(Constants.BUCKET, srcLyric, destLyric); } } - } + }*/ + /** * @param file * @return diff --git a/luoo_music/src/main/java/com/luoo/music/util/Constants.java b/luoo_music/src/main/java/com/luoo/music/util/Constants.java index 5719bb2..c2064eb 100644 --- a/luoo_music/src/main/java/com/luoo/music/util/Constants.java +++ b/luoo_music/src/main/java/com/luoo/music/util/Constants.java @@ -12,22 +12,17 @@ public class Constants { public static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); - - private String SONG_PREF = "song"; - - + // 期刊封面前缀 public static final String MUSIC_RESOURCE_PREFIX="http://cdn.indie.cn/music/"; - public static final String IMAGE_RESOURCE_PREFIX="http://cdn.indie.cn/image/"; + // 歌曲文件、封面、歌词前缀 public static final String SONG_RESOURCE_PREFIX="http://cdn.indie.cn/song/"; - public static final String BUCKET = "indie"; public static final String RESOURCE_PREFIX="http://cdn.indie.cn/"; public static final String MUSIC_KEY_PREFIX = "music/"; - public static final String IMAGE_KEY_PREFIX = "image/"; public static final String SONG_KEY_PREFIX = "song/"; public static final String TEMP_KEY_PREFIX = "temp/"; public static final String DOT = "."; - public static final String SLASH = "/"; + public static final String COMMA = ","; }