更新期刊曲库

main
JH 10 months ago
parent 599af030be
commit 135ff29a05

@ -2,10 +2,11 @@ package com.luoo.music.controller;
import api.PageResult; import api.PageResult;
import api.Result; import api.Result;
import com.luoo.music.request.cms.JournalAddModel; import com.luoo.music.dto.request.cms.JournalAddModel;
import com.luoo.music.request.cms.JournalPublishReq; import com.luoo.music.dto.request.cms.JournalPublishReq;
import com.luoo.music.request.cms.JournalQueryModel; import com.luoo.music.dto.request.cms.JournalQueryModel;
import com.luoo.music.response.cms.JournalVO; 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.CMSJournalService;
import com.luoo.music.service.S3Service; import com.luoo.music.service.S3Service;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
@ -33,31 +34,29 @@ public class CMSJournalController {
@ApiOperation(value = "查询期刊列表", notes = "查询期刊列表") @ApiOperation(value = "查询期刊列表", notes = "查询期刊列表")
@RequestMapping(value="/search/{page}/{size}", method= RequestMethod.POST) @RequestMapping(value="/search/{page}/{size}", method= RequestMethod.POST)
public Result<PageResult<JournalVO>> search(@ApiParam(value = "查询条件对象", required = true) @RequestBody JournalQueryModel queryModel, public Result<PageResult<JournalListRespDto>> search(@ApiParam(value = "查询条件对象", required = true) @RequestBody JournalQueryModel queryModel,
@ApiParam(value = "页码", required = true) @PathVariable int page, @ApiParam(value = "页码", required = true) @PathVariable int page,
@ApiParam(value = "每页条数", required = true) @PathVariable int size){ @ApiParam(value = "每页条数", required = true) @PathVariable int size){
return journalService.search(queryModel, page, size); return journalService.search(queryModel, page, size);
} }
@ApiOperation(value = "新增期刊", notes = "新增期刊") @ApiOperation(value = "新增期刊", notes = "新增期刊")
@RequestMapping(value = "/add", method = RequestMethod.POST) @RequestMapping(value = "/add", method = RequestMethod.POST)
public Result add(@ApiParam(value = "Header中的token信息", required = true) @RequestHeader("Authorization") String token, public Result add(@ApiParam(value = "期刊信息", required = true) @Valid @RequestBody JournalAddModel addModel){
@ApiParam(value = "期刊信息", required = true) @Valid @RequestBody JournalAddModel addModel){ return journalService.add(addModel);
return journalService.add(token, addModel);
} }
@ApiOperation(value = "查询期刊详情", notes = "查询期刊详情") @ApiOperation(value = "查询期刊详情", notes = "查询期刊详情")
@RequestMapping(value="/{id}",method= RequestMethod.GET) @RequestMapping(value="/{id}",method= RequestMethod.GET)
public Result<JournalVO> findOne(@ApiParam(value = "期刊ID", required = true) @PathVariable String id){ public Result<JournalRespDto> findOne(@ApiParam(value = "期刊ID", required = true) @PathVariable String id){
return journalService.findOne(id); return journalService.findOne(id);
} }
@ApiOperation(value = "更新期刊", notes = "更新期刊") @ApiOperation(value = "更新期刊", notes = "更新期刊")
@RequestMapping(value = "/{id}", method = RequestMethod.PUT) @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
public Result update(@ApiParam(value = "期刊ID", required = true) @PathVariable String id, 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){ @ApiParam(value = "期刊信息", required = true) @RequestBody JournalAddModel updateModel){
return journalService.update(id, token, updateModel); return journalService.update(id, updateModel);
} }
@ApiOperation(value = "发布期刊", notes = "发布期刊") @ApiOperation(value = "发布期刊", notes = "发布期刊")
@ -74,7 +73,7 @@ public class CMSJournalController {
return journalService.updateJournalState(id, state); return journalService.updateJournalState(id, state);
} }
@ApiOperation(value = "删除单条期刊", notes = "删除单条期刊") @ApiOperation(value = "删除单条期刊", notes = "物理删除")
@RequestMapping(value="/{id}", method= RequestMethod.DELETE) @RequestMapping(value="/{id}", method= RequestMethod.DELETE)
public Result delete(@ApiParam(value = "期刊ID", required = true) @PathVariable String id){ public Result delete(@ApiParam(value = "期刊ID", required = true) @PathVariable String id){
return journalService.deleteById(id); return journalService.deleteById(id);

@ -2,9 +2,12 @@ package com.luoo.music.controller;
import api.PageResult; import api.PageResult;
import api.Result; import api.Result;
import com.luoo.music.request.cms.SongAddModel; import com.luoo.music.dto.request.cms.SongAddModel;
import com.luoo.music.request.cms.SongQueryModel; import com.luoo.music.dto.request.cms.SongQueryModel;
import com.luoo.music.response.cms.SongVO; 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.CMSSongService;
import com.luoo.music.service.S3Service; import com.luoo.music.service.S3Service;
import io.swagger.annotations.*; import io.swagger.annotations.*;
@ -30,9 +33,9 @@ public class CMSSongController {
@ApiOperation(value = "查询歌曲列表", notes = "查询歌曲列表") @ApiOperation(value = "查询歌曲列表", notes = "查询歌曲列表")
@RequestMapping(value="/search/{page}/{size}",method= RequestMethod.POST) @RequestMapping(value="/search/{page}/{size}",method= RequestMethod.POST)
public Result<PageResult<SongVO>> search(@ApiParam(value = "查询条件") @RequestBody SongQueryModel queryModel, public Result<PageResult<SongListRespDto>> search(@ApiParam(value = "查询条件") @RequestBody SongQueryModel queryModel,
@ApiParam(value = "页码", required = true) @PathVariable int page, @ApiParam(value = "页码", required = true) @PathVariable int page,
@ApiParam(value = "每页条数", required = true)@PathVariable int size){ @ApiParam(value = "每页条数", required = true)@PathVariable int size){
return songService.search(queryModel, page, size); return songService.search(queryModel, page, size);
} }
@ -45,11 +48,11 @@ public class CMSSongController {
@ApiOperation(value = "查询歌曲详情", notes = "查询歌曲详情") @ApiOperation(value = "查询歌曲详情", notes = "查询歌曲详情")
@RequestMapping(value="/{id}",method= RequestMethod.GET) @RequestMapping(value="/{id}",method= RequestMethod.GET)
public Result<SongVO> findOne(@ApiParam(value = "歌曲ID", required = true) @PathVariable String id){ public Result<SongRespDto> findOne(@ApiParam(value = "歌曲ID", required = true) @PathVariable String id){
return songService.findOne(id); return songService.findOne(id);
} }
@ApiOperation(value = "更新歌曲信息", notes = "更新歌曲信息") @ApiOperation(value = "更新歌曲信息", notes = "同步更新期刊音乐表中的歌曲信息更新歌曲文件时需同时传上传歌曲时返回的size、duration")
@RequestMapping(value = "/{id}", method = RequestMethod.PUT) @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
public Result update(@ApiParam(value = "Header中的token信息", required = true) @RequestHeader("Authorization") String token, public Result update(@ApiParam(value = "Header中的token信息", required = true) @RequestHeader("Authorization") String token,
@ApiParam(value = "歌曲ID", required = true) @PathVariable String id, @ApiParam(value = "歌曲ID", required = true) @PathVariable String id,
@ -57,20 +60,20 @@ public class CMSSongController {
return songService.update(id, token, updateModel); return songService.update(id, token, updateModel);
} }
@ApiOperation(value = "更新歌曲启停状态", notes = "更新歌曲启停状态") @ApiOperation(value = "更新歌曲启停状态", notes = "更新歌曲启停状态,并删除期刊中歌曲的引用")
@RequestMapping(value="/update/state/{id}",method= RequestMethod.PUT) @RequestMapping(value="/update/state/{id}",method= RequestMethod.PUT)
public Result updateSongState(@ApiParam(value = "歌曲ID", required = true) @PathVariable String id, public Result updateSongState(@ApiParam(value = "歌曲ID", required = true) @PathVariable String id,
@ApiParam(value = "歌曲状态0:停用1:启用", required = true) @RequestBody String state){ @ApiParam(value = "歌曲状态0:停用1:启用", required = true) @RequestBody String state){
return songService.updateSongState(id, state); return songService.updateSongState(id, state);
} }
@ApiOperation(value = "获取歌曲歌词", notes = "获取歌曲歌词") @ApiOperation(value = "获取歌曲歌词", notes = "读取表中的歌曲内容内容为空时会检查歌词url是否存在并读取")
@RequestMapping(value="/lyric/{id}",method= RequestMethod.GET) @RequestMapping(value="/lyric/{id}",method= RequestMethod.GET)
public Result<SongVO> querySongLyric(@ApiParam(value = "歌曲ID", required = true) @PathVariable String id){ public Result<SongLyricRespDto> querySongLyric(@ApiParam(value = "歌曲ID", required = true) @PathVariable String id){
return songService.querySongLyric(id); return songService.querySongLyric(id);
} }
@ApiOperation(value = "更新歌曲歌词", notes = "更新歌曲歌词") @ApiOperation(value = "更新歌曲歌词", notes = "更新表中歌词内容并将歌词写入文件上传至s3")
@RequestMapping(value="/update/lyric/{id}",method= RequestMethod.PUT) @RequestMapping(value="/update/lyric/{id}",method= RequestMethod.PUT)
public Result updateSongLyric(@ApiParam(value = "歌曲ID", required = true) @PathVariable String id, public Result updateSongLyric(@ApiParam(value = "歌曲ID", required = true) @PathVariable String id,
@ApiParam(value = "歌曲歌词信息", required = true) @RequestBody String lyric){ @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) @RequestMapping(value="/{id}", method= RequestMethod.DELETE)
public Result delete(@ApiParam(value = "歌曲ID", required = true) @PathVariable String id){ public Result delete(@ApiParam(value = "歌曲ID", required = true) @PathVariable String id){
return songService.deleteById(id); return songService.deleteById(id);
} }
@ApiOperation(value = "上传歌曲", notes = "上传歌曲") @ApiOperation(value = "上传歌曲", notes = "上传歌曲不会新增到曲库会上传到s3返回临时url保存时才会新增能到曲库")
@RequestMapping(value = "/upload/song", method = RequestMethod.POST) @RequestMapping(value = "/upload/song", method = RequestMethod.POST)
public Result batchUpload(@ApiParam(value = "歌曲文件", required = true) @RequestParam("file") MultipartFile file) { public Result batchUpload(@ApiParam(value = "歌曲文件", required = true) @RequestParam("file") MultipartFile file) {
return s3Service.upload(file, Boolean.TRUE); return s3Service.upload(file, Boolean.TRUE);
} }
@ApiOperation(value = "批量上传歌曲", notes = "批量上传歌曲") @ApiOperation(value = "批量上传歌曲", notes = "上传歌曲会新增到曲库")
@RequestMapping(value = "/batch", method = RequestMethod.POST) @RequestMapping(value = "/batch", method = RequestMethod.POST)
public Result batchUpload(@ApiParam(value = "多歌曲文件", required = true) @RequestParam("files") List<MultipartFile> files) { public Result<List<SongBaseRespDto>> batchUpload(@ApiParam(value = "Header中的token信息", required = true) @RequestHeader("Authorization") String token,
return songService.batchUpload(files); @ApiParam(value = "多歌曲文件", required = true) @RequestParam("files") List<MultipartFile> files) {
return songService.batchUpload(token, files);
} }
} }

@ -17,7 +17,7 @@ import java.util.Set;
*/ */
public interface JournalDao extends JpaRepository<Journal,String>,JpaSpecificationExecutor<Journal>{ public interface JournalDao extends JpaRepository<Journal,String>,JpaSpecificationExecutor<Journal>{
List<Journal> findByJournalNo(String journalNo); List<Journal> findByJournalNo(String journalNo);
List<Journal> findByJournalNoIn(Set<String> journalNos); List<Journal> findByJournalNoIn(Set<String> journalNos);
@ -46,15 +46,6 @@ public interface JournalDao extends JpaRepository<Journal,String>,JpaSpecificati
@Transactional @Transactional
@Query("UPDATE Journal j SET j.state = :state WHERE j.id = :id") @Query("UPDATE Journal j SET j.state = :state WHERE j.id = :id")
int updateJournalState(@Param("id") String id, @Param("state") String state); 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<Journal> orderByField(List<String> 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 @Modifying
@Transactional @Transactional
@ -71,6 +62,15 @@ public interface JournalDao extends JpaRepository<Journal,String>,JpaSpecificati
@Query("UPDATE Journal j SET j.comment = j.comment - 1 WHERE j.id = :journalId") @Query("UPDATE Journal j SET j.comment = j.comment - 1 WHERE j.id = :journalId")
int decreaseCommentCount(@Param("journalId") String 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<Journal> orderByField(List<String> 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) @Query(value = "select max(ABS(journal_no)) FROM tb_journal", nativeQuery = true)
int getMaxJournalNo(); int getMaxJournalNo();

@ -1,6 +1,5 @@
package com.luoo.music.dao; package com.luoo.music.dao;
import com.luoo.music.pojo.Journal;
import com.luoo.music.pojo.SongInfo; import com.luoo.music.pojo.SongInfo;
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;
@ -23,13 +22,8 @@ public interface SongInfoDao extends JpaRepository<SongInfo,String>, JpaSpecific
@Modifying @Modifying
@Transactional @Transactional
@Query("UPDATE SongInfo s SET s.lyric = :lyric WHERE s.id = :id") @Query("UPDATE SongInfo s SET s.lyric = :lyric, s.lyricUrl = :lyricUrl WHERE s.id = :id")
int updateSongLyric(@Param("id") String id, @Param("lyric") String lyric); int updateSongLyricAndUrl(@Param("id") String id, @Param("lyric") String lyric, @Param("lyricUrl") String lyricUrl);
@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);
List<SongInfo> findByIdIn(List<String> ids); List<SongInfo> findByIdIn(List<String> ids);

@ -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.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
@ -14,11 +14,11 @@ import java.util.List;
@ApiModel(value = "增加或修改期刊对象") @ApiModel(value = "增加或修改期刊对象")
public class JournalAddModel { public class JournalAddModel {
@NotBlank(message = "期刊的期刊必填") @NotBlank(message = "期刊编号必填")
@ApiModelProperty(value = "期刊编号") @ApiModelProperty(value = "期刊编号")
private String journalNo; private String journalNo;
@NotBlank(message = "期刊标题必填") @NotBlank(message = "期刊标题必填")
@ApiModelProperty(value = "期刊标题") @ApiModelProperty(value = "期刊标题")
private String title; private String title;
@ -28,14 +28,15 @@ public class JournalAddModel {
@ApiModelProperty(value = "发布人ID") @ApiModelProperty(value = "发布人ID")
private String userId; private String userId;
@NotBlank(message = "期刊的封面必填")
@ApiModelProperty(value = "期刊封面路径") @ApiModelProperty(value = "期刊封面路径")
private String image; private String image;
@ApiModelProperty(value = "关联歌曲音乐ID") @ApiModelProperty(value = "关联歌曲音乐ID")
private List<String> songs; private List<String> songs;
@NotBlank(message = "期刊的文案必填") @ApiModelProperty(value = "期刊文案")
@ApiModelProperty(value = "期刊内容")
private String content; private String content;
@ApiModelProperty(value = "期刊简介")
private String summary;
} }

@ -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.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;

@ -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.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;

@ -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.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;

@ -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.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;

@ -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<String> tag;
public static JournalListRespDto convertPojo(Journal journal,
Map<String, List<String>> journalTagMap,
Map<String, Long> 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;
}
}

@ -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<String> tag;
@ApiModelProperty(value = "期刊歌曲信息")
private List<SongBaseRespDto> songs;
public static JournalRespDto convertPojo(Journal journal, List<String> tagInfo, List<SongBaseRespDto> 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;
}
}

@ -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;
}
}

@ -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<String> journal;
@ApiModelProperty(value = "歌曲风格")
private List<String> tags;
public static SongListRespDto convertPojo(SongInfo song,
Map<String, List<String>> songJournalMap,
Map<String, List<String>> 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;
}
}

@ -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;
}

@ -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<String> tags;
public static SongRespDto convertPojo(SongInfo song, List<String> 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;
}
}

@ -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.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
@ -10,12 +10,9 @@ import java.util.List;
* @author locust * @author locust
*/ */
@Data @Data
@ApiModel(description = "文件上传后的路径") @ApiModel(description = "文件临时上传后响应信息")
public class UploadRespDto { public class UploadRespDto {
@ApiModelProperty(value = "部分路径")
private String partUrl;
@ApiModelProperty(value = "全路径") @ApiModelProperty(value = "全路径")
private String fullUrl; private String fullUrl;

@ -22,7 +22,8 @@ import java.time.LocalDateTime;
@Entity @Entity
@DynamicInsert @DynamicInsert
@DynamicUpdate @DynamicUpdate
@Table(name="tb_journal") // @Table(name="tb_journal")
@Table(name="tb_journal_copy1")
@EntityListeners(AuditingEntityListener.class) @EntityListeners(AuditingEntityListener.class)
public class Journal implements Serializable{ public class Journal implements Serializable{
@ -41,6 +42,10 @@ public class Journal implements Serializable{
* *
*/ */
private String image; private String image;
/**
*
*/
private String summary;
/** /**
* *
*/ */

@ -7,12 +7,11 @@ import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate; import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener; import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.Entity; import javax.persistence.*;
import javax.persistence.EntityListeners;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable; import java.io.Serializable;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects; import java.util.Objects;
@Getter @Getter
@ -85,6 +84,12 @@ public class Tag implements Serializable {
@LastModifiedDate @LastModifiedDate
private LocalDateTime updateTime; private LocalDateTime updateTime;
@ManyToMany(mappedBy = "tags")
private List<Journal> journals = new ArrayList<>();
@ManyToMany(mappedBy = "tags")
private List<Song> songs = new ArrayList<>();
@Override @Override
public final boolean equals(Object o) { public final boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;

@ -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<String> 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<SongVO> songs;
@ApiModelProperty(value = "期刊简介信息")
private String content;
}

@ -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<String> tags;
@ApiModelProperty(value = "封面路径")
private String image;
@ApiModelProperty(value = "文件大小")
private String size;
@ApiModelProperty(value = "歌曲时长")
private String duration;
@ApiModelProperty(value = "关联期刊")
private List<String> 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;
}

@ -5,13 +5,13 @@ import api.Result;
import com.luoo.music.client.UserClient; import com.luoo.music.client.UserClient;
import com.luoo.music.dao.*; import com.luoo.music.dao.*;
import com.luoo.music.pojo.*; import com.luoo.music.pojo.*;
import com.luoo.music.request.cms.JournalAddModel; import com.luoo.music.dto.request.cms.JournalAddModel;
import com.luoo.music.request.cms.JournalPublishReq; import com.luoo.music.dto.request.cms.JournalPublishReq;
import com.luoo.music.request.cms.JournalQueryModel; import com.luoo.music.dto.request.cms.JournalQueryModel;
import com.luoo.music.response.cms.JournalVO; import com.luoo.music.dto.response.cms.JournalListRespDto;
import com.luoo.music.response.cms.SongVO; import com.luoo.music.dto.response.cms.JournalRespDto;
import com.luoo.music.dto.response.cms.SongBaseRespDto;
import com.luoo.music.util.Constants; import com.luoo.music.util.Constants;
import dto.UserLoginDto;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page; 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.stereotype.Service;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import util.IdWorker; import util.IdWorker;
import util.JwtUtil;
import util.StringTools;
import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.CriteriaQuery;
@ -30,7 +28,6 @@ import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root; import javax.persistence.criteria.Root;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.*; import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@ -60,20 +57,19 @@ public class CMSJournalService {
/** /**
* + * +
*
* @param param * @param param
* @param page * @param page
* @param size * @param size
* @return * @return
*/ */
public Result<PageResult<JournalVO>> search(JournalQueryModel param, int page, int size) { public Result<PageResult<JournalListRespDto>> search(JournalQueryModel param, int page, int size) {
List<JournalVO> result = new ArrayList<>(); List<JournalListRespDto> result = new ArrayList<>();
// Sort sort = new Sort(Sort.Direction.DESC, "journalNo");
PageRequest pageRequest = PageRequest.of(page - 1, size); PageRequest pageRequest = PageRequest.of(page - 1, size);
Page<Journal> journalPage; Page<Journal> journalPage;
List<String> journalIdsByTags = null;
if (!Objects.isNull(param)) { if (!Objects.isNull(param)) {
List<String> journalIdsByTags = null;
if (!CollectionUtils.isEmpty(param.getTags())) { if (!CollectionUtils.isEmpty(param.getTags())) {
// 查询标签对应的全部期刊
journalIdsByTags = journalTagDao.findJournalIdsByTags(param.getTags()); journalIdsByTags = journalTagDao.findJournalIdsByTags(param.getTags());
if (journalIdsByTags.size() == 0) { if (journalIdsByTags.size() == 0) {
return Result.success(new PageResult<>(0L, result)); return Result.success(new PageResult<>(0L, result));
@ -82,7 +78,6 @@ public class CMSJournalService {
Specification<Journal> journalSpecification = buildSearchSpecification(param, journalIdsByTags); Specification<Journal> journalSpecification = buildSearchSpecification(param, journalIdsByTags);
journalPage = journalDao.findAll(journalSpecification, pageRequest); journalPage = journalDao.findAll(journalSpecification, pageRequest);
} else { } else {
List<String> journalIdsByTags = null;
Specification<Journal> journalSpecification = buildSearchSpecification(param, journalIdsByTags); Specification<Journal> journalSpecification = buildSearchSpecification(param, journalIdsByTags);
journalPage = journalDao.findAll(journalSpecification, pageRequest); journalPage = journalDao.findAll(journalSpecification, pageRequest);
} }
@ -90,29 +85,11 @@ public class CMSJournalService {
List<Journal> content = journalPage.getContent(); List<Journal> content = journalPage.getContent();
if (!CollectionUtils.isEmpty(content)) { if (!CollectionUtils.isEmpty(content)) {
List<String> ids = content.stream().map(Journal::getId).collect(Collectors.toList()); List<String> ids = content.stream().map(Journal::getId).collect(Collectors.toList());
Map<String, List<String>> journalTagMap = tagInfo(ids); Map<String, List<String>> journalTagMap = journalTagInfo(ids);
List<String> journalNos = content.stream().map(Journal::getJournalNo).collect(Collectors.toList()); List<String> journalNos = content.stream().map(Journal::getJournalNo).collect(Collectors.toList());
Map<String, Long> songCountMap = songCountInfo(journalNos); Map<String, Long> journalSongCountMap = journalSongCountInfo(journalNos);
for (Journal item : content) { for (Journal item : content) {
JournalVO response = new JournalVO(); JournalListRespDto response = JournalListRespDto.convertPojo(item, journalTagMap, journalSongCountMap);
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());
result.add(response); result.add(response);
} }
} }
@ -121,7 +98,6 @@ public class CMSJournalService {
/** /**
* *
*
* @param param * @param param
* @return * @return
*/ */
@ -159,35 +135,17 @@ public class CMSJournalService {
}; };
} }
/**
*
*
* @param journalNos
* @return
*/
private Map<String, Long> songCountInfo(List<String> journalNos) {
Map<String, Long> songCountMap = new HashMap<>();
if (!CollectionUtils.isEmpty(journalNos)) {
List<Map<String, Long>> 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 * @param ids
* @return * @return
*/ */
private Map<String, List<String>> tagInfo(List<String> ids) { private Map<String, List<String>> journalTagInfo(List<String> ids) {
Map<String, List<String>> journalTagMap = new HashMap<>(); Map<String, List<String>> journalTagMap = new HashMap<>();
List<JournalTag> journalTagList = journalTagDao.findByJournalIds(ids); List<JournalTag> journalTagList = journalTagDao.findByJournalIds(ids);
Set<String> tagIdSet = journalTagList.stream().map(JournalTag::getTagId).collect(Collectors.toSet()); // Set<String> tagIdSet = journalTagList.stream().map(JournalTag::getTagId).collect(Collectors.toSet());
List<Tag> tagList = tagDao.findByIdIn(tagIdSet); // List<Tag> tagList = tagDao.findByIdIn(tagIdSet);
Map<String, Tag> idTagMap = tagList.stream().collect(Collectors.toMap(Tag::getId, obj -> obj)); // Map<String, Tag> idTagMap = tagList.stream().collect(Collectors.toMap(Tag::getId, obj -> obj));
List<String> list; List<String> list;
for (JournalTag item : journalTagList) { for (JournalTag item : journalTagList) {
String journalId = item.getJournalId(); String journalId = item.getJournalId();
@ -196,74 +154,90 @@ public class CMSJournalService {
} else { } else {
list = new ArrayList<>(); list = new ArrayList<>();
} }
if (idTagMap.containsKey(item.getTagId())) { list.add(item.getTagId());
Tag tag = idTagMap.get(item.getTagId());
list.add(tag.getId());
}
journalTagMap.put(journalId, list); journalTagMap.put(journalId, list);
} }
return journalTagMap; return journalTagMap;
} }
/** /**
* *
* * @param journalNos
* @param param
* @return * @return
*/ */
public Result add(String token, JournalAddModel param) { private Map<String, Long> journalSongCountInfo(List<String> journalNos) {
List<Journal> byJournalNo = journalDao.findByJournalNo(param.getJournalNo()); Map<String, Long> songCountMap = new HashMap<>();
if (byJournalNo.size() > 0) { if (!CollectionUtils.isEmpty(journalNos)) {
return Result.failed("保存失败,期刊编号已存在"); List<Map<String, Long>> journalSongCountList = journalSongDao.findSongCountByJournalNos(journalNos);
} songCountMap = journalSongCountList.stream()
String srcKey = param.getImage().substring(param.getImage().indexOf(Constants.TEMP_KEY_PREFIX)); .collect(Collectors.toMap(map -> String.valueOf(map.get("journalNo")),
String image = moveJournalImage(srcKey, param); map -> Long.valueOf(String.valueOf(map.get("songCount")))));
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("保存失败");
} }
return songCountMap;
} }
/** /**
* *
*
* @param param * @param param
* @return * @return
*/ */
private String moveJournalImage(String srcKey, JournalAddModel param) { public Result add(JournalAddModel param) {
String image = param.getImage(); List<Journal> byJournalNo = journalDao.findByJournalNo(param.getJournalNo());
String suffix = image.substring(image.lastIndexOf(Constants.DOT)); if (byJournalNo.size() > 0) {
String destKeySuffix = String.format("%05d/00%s", Integer.parseInt(param.getJournalNo()), suffix); return Result.failed("保存失败,期刊编号已存在");
String destKey = Constants.MUSIC_KEY_PREFIX + destKeySuffix;
int copy = s3Service.copy(Constants.BUCKET, srcKey, destKey);
if (copy > 0) {
return destKeySuffix;
} }
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 id
* @param param * @param param
* @param token
* @return * @return
*/ */
private Journal buildJournal(String id, JournalAddModel param, String token) { private Journal buildJournal(String id, JournalAddModel param) {
Journal journal = new Journal(); Journal journal = new Journal();
if (!StringUtils.isBlank(id)) { journal.setId(id);
journal.setId(id);
}
journal.setJournalNo(param.getJournalNo()); journal.setJournalNo(param.getJournalNo());
journal.setTitle(param.getTitle()); journal.setTitle(param.getTitle());
journal.setSummary(param.getSummary());
journal.setContent(param.getContent()); journal.setContent(param.getContent());
UserInfo userInfo = userClient.queryUserInfoById(param.getUserId()); UserInfo userInfo = userClient.queryUserInfoById(param.getUserId());
if (!Objects.isNull(userInfo)) { if (!Objects.isNull(userInfo)) {
@ -276,7 +250,6 @@ public class CMSJournalService {
/** /**
* *
*
* @param journalId * @param journalId
* @param param * @param param
*/ */
@ -293,43 +266,53 @@ public class CMSJournalService {
} }
journalTagDao.saveAll(journalTagList); journalTagDao.saveAll(journalTagList);
//save tag 中文名到 journal 中 //save tag 中文名到 journal 中
updateJouranlNameChTags(journalId); updateJournalNameChTags(journalId);
} }
// 歌曲信息 // 歌曲信息
if (!CollectionUtils.isEmpty(param.getSongs())) { if (!CollectionUtils.isEmpty(param.getSongs())) {
List<SongInfo> songList = songDao.findByIdIn(param.getSongs()); List<SongInfo> songList = songDao.findByIdIn(param.getSongs());
Map<String, SongInfo> idSongMap = songList.stream().collect(Collectors.toMap(SongInfo::getId, obj -> obj)); Map<String, SongInfo> idSongMap = songList.stream().collect(Collectors.toMap(SongInfo::getId, obj -> obj));
List<JournalSong> journalSongList = new ArrayList<>(); List<JournalSong> journalSongList = new ArrayList<>();
int num = 0; for (int i = 0; i < param.getSongs().size(); i++) {
for (String item : param.getSongs()) { String item = param.getSongs().get(i);
if (idSongMap.containsKey(item)) { if (idSongMap.containsKey(item)) {
num += 1; SongInfo song = idSongMap.get(item);
SongInfo song = idSongMap.get(item); JournalSong journalSong = new JournalSong();
JournalSong journalSong = new JournalSong(); journalSong.setId(String.valueOf(idWorker.nextId()));
journalSong.setId(String.valueOf(idWorker.nextId())); journalSong.setName(song.getName());
journalSong.setName(song.getName()); journalSong.setAlbum(song.getAlbum());
journalSong.setAlbum(song.getAlbum()); journalSong.setArtist(song.getArtist());
journalSong.setArtist(song.getArtist()); if (StringUtils.isNotBlank(song.getUrl())) {
journalSong.setUrl(song.getUrl()); journalSong.setUrl(Constants.SONG_KEY_PREFIX + song.getUrl());
journalSong.setImage(song.getImage()); } else {
journalSong.setSongId(item); journalSong.setUrl(song.getUrl());
journalSong.setJournalNo(param.getJournalNo()); }
journalSong.setSongNo(num); if (StringUtils.isNotBlank(song.getImage())) {
journalSong.setSongId(song.getId()); journalSong.setImage(Constants.SONG_KEY_PREFIX + song.getImage());
journalSongList.add(journalSong); } else {
} journalSong.setImage(song.getImage());
} }
s3Service.fromSongToMusic(journalSongList); journalSong.setSongId(item);
journalSongDao.saveAll(journalSongList); 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<Journal> optional=journalDao.findById(journalId); Optional<Journal> optional=journalDao.findById(journalId);
if(optional.isPresent()) { if(optional.isPresent()) {
List<String> tags=tagDao.getTagNameChByJournalId(journalId); List<String> tags=tagDao.getTagNameChByJournalId(journalId);
if(!tags.isEmpty()) { 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 journal=optional.get();
journal.setNameChTags(tagStr); journal.setNameChTags(tagStr);
journalDao.save(journal); journalDao.save(journal);
@ -339,246 +322,109 @@ public class CMSJournalService {
/** /**
* *
*
* @param id * @param id
* @return * @return
*/ */
public Result<JournalVO> findOne(String id) { public Result<JournalRespDto> findOne(String id) {
Optional<Journal> optional=journalDao.findById(id); Optional<Journal> optional=journalDao.findById(id);
if(!optional.isPresent()) { if(!optional.isPresent()) {
return Result.failed("无法找到期刊: "+id); return Result.failed("无法找到期刊: " + id);
} }
Journal journal = optional.get(); Journal journal = optional.get();
JournalVO response = new JournalVO(); List<String> tagInfo = journalTagDao.findTagsByJournalId(id);
response.setId(journal.getId()); List<SongBaseRespDto> songInfo = journalSongInfo(journal.getJournalNo());
response.setJournalNo(journal.getJournalNo()); JournalRespDto response = JournalRespDto.convertPojo(journal, tagInfo, songInfo);
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());
return Result.success(response); return Result.success(response);
} }
/** /**
* *
*
* @param journalNo * @param journalNo
* @return * @return
*/ */
private List<SongVO> journalSongInfo(String journalNo) { private List<SongBaseRespDto> journalSongInfo(String journalNo) {
List<SongVO> list = new ArrayList<>(); List<SongBaseRespDto> list = new ArrayList<>();
List<JournalSong> journalSongList = journalSongDao.findByJournalNo(journalNo); List<JournalSong> journalSongList = journalSongDao.findByJournalNo(journalNo);
if (!CollectionUtils.isEmpty(journalSongList)) { if (!CollectionUtils.isEmpty(journalSongList)) {
for (JournalSong item : journalSongList) { for (JournalSong item : journalSongList) {
SongVO songVO = new SongVO(); SongBaseRespDto songVO = new SongBaseRespDto();
songVO.setId(item.getSongId()); songVO.setId(item.getSongId());
songVO.setName(item.getName()); songVO.setName(item.getName());
songVO.setArtist(item.getArtist()); songVO.setArtist(item.getArtist());
songVO.setAlbum(item.getAlbum()); 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())) { if (StringUtils.isNotBlank(item.getUrl())) {
songVO.setUrl(Constants.RESOURCE_PREFIX + item.getUrl()); songVO.setUrl(Constants.RESOURCE_PREFIX + item.getUrl());
} } else {
if (StringUtils.isNotBlank(item.getImage())) { songVO.setUrl(item.getUrl());
songVO.setImage(Constants.RESOURCE_PREFIX + item.getImage()); }
}
list.add(songVO); list.add(songVO);
} }
} }
return list; return list;
} }
/** /**
* *
* * @param id
* @param id * @param param
* @param token * @return
* @param param */
* @return public Result update(String id, JournalAddModel param) {
*/ // TODO
public Result update(String id, String token, JournalAddModel param) { Optional<Journal> optional = journalDao.findById(id);
Optional<Journal> optional = journalDao.findById(id);
if (!optional.isPresent()) { if (!optional.isPresent()) {
return Result.failed("找不到期刊: " + id); return Result.failed("找不到期刊: " + id);
} }
Journal journal = optional.get(); // 当前编辑页面期刊编号可修改
updateJournal(journal, param, token); Journal oldJournal = optional.get();
journalDao.save(journal); List<Journal> journals = journalDao.findByJournalNo(param.getJournalNo());
redisTemplate.opsForValue().set("journal_" + id, journal); if (!oldJournal.getJournalNo().equals(param.getJournalNo()) && journals.size() > 0) {
return Result.failed("期刊编号已存在");
batchUpdateJournalRelatesInfo(id, journal.getJournalNo(), param); }
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(); return Result.success();
} }
private void batchUpdateJournalRelatesInfo(String journalId, String journalNo, JournalAddModel param) {
// 标签信息
List<String> updateTags = param.getTag();
List<String> currentTags = journalTagDao.findTagsByJournalId(journalId);
if (!CollectionUtils.isEmpty(updateTags) && !isCollectionEqual(currentTags, updateTags)) {
// 清理当前journalTag
journalTagDao.deleteByJournalId(journalId);
List<JournalTag> updatedJournalTags = updateTags.stream().map(t -> createJournalTag(t, journalId))
.collect(Collectors.toList());
journalTagDao.saveAll(updatedJournalTags);
//update tag 中文名到 journal 中
updateJouranlNameChTags(journalId);
}
// 歌曲信息
List<String> updateSongs = param.getSongs();
if (!CollectionUtils.isEmpty(updateSongs)) {
List<SongInfo> songList = songDao.orderByField(updateSongs);
List<JournalSong> journalSongs=journalSongDao.findByJournalNoOrderBySongNo(journalNo);
Map<String,String> songId2JournalSongIdMap=journalSongs.stream().collect(Collectors.toMap(JournalSong::getSongId, JournalSong::getId));
List<JournalSong> 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&&current.equals(previous))) {
return true;
}
return false;
}
private boolean isSameContent(List<SongInfo> current, List<JournalSong> 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<String> current, List<String> 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 journalId
* @param journalNo * @param journalNo
*/ */
@ -589,7 +435,6 @@ public class CMSJournalService {
/** /**
* *
*
* @param id * @param id
* @param queryModel * @param queryModel
* @return * @return
@ -611,7 +456,6 @@ public class CMSJournalService {
/** /**
* *
*
* @param id * @param id
* @param state * @param state
* @return * @return
@ -623,7 +467,6 @@ public class CMSJournalService {
/** /**
* *
*
* @param id * @param id
* @return * @return
*/ */

@ -2,12 +2,14 @@ package com.luoo.music.service;
import api.PageResult; import api.PageResult;
import api.Result; import api.Result;
import com.luoo.music.client.UserClient;
import com.luoo.music.dao.*; import com.luoo.music.dao.*;
import com.luoo.music.pojo.*; import com.luoo.music.pojo.*;
import com.luoo.music.request.cms.SongAddModel; import com.luoo.music.dto.request.cms.SongAddModel;
import com.luoo.music.request.cms.SongQueryModel; import com.luoo.music.dto.request.cms.SongQueryModel;
import com.luoo.music.response.cms.SongVO; 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.CommonUtil;
import com.luoo.music.util.Constants; import com.luoo.music.util.Constants;
import dto.UserLoginDto; import dto.UserLoginDto;
@ -57,7 +59,7 @@ public class CMSSongService {
@Autowired @Autowired
private TagDao tagDao; private TagDao tagDao;
@Autowired @Autowired
private UserClient userClient; private JwtUtil jwtUtil;
/** /**
* + * +
@ -66,11 +68,10 @@ public class CMSSongService {
* @param size * @param size
* @return * @return
*/ */
public Result<PageResult<SongVO>> search(SongQueryModel param, int page, int size) { public Result<PageResult<SongListRespDto>> search(SongQueryModel param, int page, int size) {
List<SongVO> result = new ArrayList<>(); List<SongListRespDto> result = new ArrayList<>();
Sort sort = new Sort(Sort.Direction.DESC, "id"); Sort sort = new Sort(Sort.Direction.DESC, "id");
PageRequest pageRequest = PageRequest.of(page - 1, size, sort); PageRequest pageRequest = PageRequest.of(page - 1, size, sort);
// PageRequest pageRequest = PageRequest.of(page - 1, size);
Page<SongInfo> songPage; Page<SongInfo> songPage;
if (!Objects.isNull(param)) { if (!Objects.isNull(param)) {
Specification<SongInfo> songSpecification = buildSearchSpecification(param); Specification<SongInfo> songSpecification = buildSearchSpecification(param);
@ -82,29 +83,12 @@ public class CMSSongService {
List<SongInfo> content = songPage.getContent(); List<SongInfo> content = songPage.getContent();
if (!CollectionUtils.isEmpty(content)) { if (!CollectionUtils.isEmpty(content)) {
List<String> ids = content.stream().map(SongInfo::getId).collect(Collectors.toList()); List<String> ids = content.stream().map(SongInfo::getId).collect(Collectors.toList());
// 查询歌曲对应的期刊信息
Map<String, List<String>> songJournalMap = buildSongJournalInfo(ids); Map<String, List<String>> songJournalMap = buildSongJournalInfo(ids);
// 查询歌曲对应的标签信息
Map<String, List<String>> songTagMap = buildSongTagInfo(ids); Map<String, List<String>> songTagMap = buildSongTagInfo(ids);
for (SongInfo item : content) { for (SongInfo item : content) {
SongVO response = new SongVO(); SongListRespDto response = SongListRespDto.convertPojo(item, songJournalMap, songTagMap);
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());
}
result.add(response); result.add(response);
} }
} }
@ -143,14 +127,19 @@ public class CMSSongService {
}; };
} }
/**
*
* @param ids
* @return
*/
private Map<String, List<String>> buildSongJournalInfo(List<String> ids) { private Map<String, List<String>> buildSongJournalInfo(List<String> ids) {
Map<String, List<String>> result = new HashMap<>(); Map<String, List<String>> result = new HashMap<>();
List<JournalSong> bySongId = journalSongDao.findBySongIds(ids); List<JournalSong> journalSongList = journalSongDao.findBySongIds(ids);
if (!CollectionUtils.isEmpty(bySongId)) { if (!CollectionUtils.isEmpty(journalSongList)) {
Set<String> idSet = bySongId.stream().map(JournalSong::getJournalNo).collect(Collectors.toSet()); Set<String> idSet = journalSongList.stream().map(JournalSong::getJournalNo).collect(Collectors.toSet());
List<Journal> journalList = journalDao.findByJournalNoIn(idSet); List<Journal> journalList = journalDao.findByJournalNoIn(idSet);
Map<String, Journal> idJournalMap = journalList.stream().collect(Collectors.toMap(Journal::getJournalNo, obj -> obj)); Map<String, Journal> idJournalMap = journalList.stream().collect(Collectors.toMap(Journal::getJournalNo, obj -> obj));
for (JournalSong item : bySongId) { for (JournalSong item : journalSongList) {
String songId = item.getSongId(); String songId = item.getSongId();
String journalNo = item.getJournalNo(); String journalNo = item.getJournalNo();
List<String> list; List<String> list;
@ -169,14 +158,19 @@ public class CMSSongService {
return result; return result;
} }
/**
*
* @param ids
* @return
*/
private Map<String, List<String>> buildSongTagInfo(List<String> ids) { private Map<String, List<String>> buildSongTagInfo(List<String> ids) {
Map<String, List<String>> result = new HashMap<>(); Map<String, List<String>> result = new HashMap<>();
List<SongTag> bySongId = songTagDao.findBySongIds(ids); List<SongTag> songTagList = songTagDao.findBySongIds(ids);
if (!CollectionUtils.isEmpty(bySongId)) { if (!CollectionUtils.isEmpty(songTagList)) {
Set<String> idSet = bySongId.stream().map(SongTag::getTagId).collect(Collectors.toSet()); Set<String> idSet = songTagList.stream().map(SongTag::getTagId).collect(Collectors.toSet());
List<Tag> tagList = tagDao.findByIdIn(idSet); List<Tag> tagList = tagDao.findByIdIn(idSet);
Map<String, Tag> idTagMap = tagList.stream().collect(Collectors.toMap(Tag::getId, obj -> obj)); Map<String, Tag> idTagMap = tagList.stream().collect(Collectors.toMap(Tag::getId, obj -> obj));
for (SongTag item : bySongId) { for (SongTag item : songTagList) {
String songId = item.getSongId(); String songId = item.getSongId();
String tagId = item.getTagId(); String tagId = item.getTagId();
List<String> list; List<String> list;
@ -187,7 +181,6 @@ public class CMSSongService {
} }
if (idTagMap.keySet().contains(tagId)) { if (idTagMap.keySet().contains(tagId)) {
Tag tag = idTagMap.get(tagId); Tag tag = idTagMap.get(tagId);
// list.add(tag.getNameCh());
list.add(tag.getId()); list.add(tag.getId());
} }
result.put(songId, list); result.put(songId, list);
@ -197,7 +190,7 @@ public class CMSSongService {
} }
/** /**
* *
* @param token * @param token
* @param param * @param param
* @return * @return
@ -206,67 +199,59 @@ public class CMSSongService {
String id = String.valueOf(idWorker.nextId()); String id = String.valueOf(idWorker.nextId());
String url = null; String url = null;
if (StringUtils.isNotBlank(param.getUrl())) { if (StringUtils.isNotBlank(param.getUrl())) {
url = moveSongFile(id, param); url = moveSongFileOrImage(id, param.getUrl());
if (StringUtils.isBlank(url)) { if (StringUtils.isBlank(url)) {
return Result.failed("保存失败"); return Result.failed("保存失败");
} }
} }
String image = null; String image = null;
if (StringUtils.isNotBlank(param.getImage())) { if (StringUtils.isNotBlank(param.getImage())) {
image = moveSongImage(id, param); image = moveSongFileOrImage(id, param.getImage());
if (StringUtils.isBlank(image)) { if (StringUtils.isBlank(image)) {
return Result.failed("保存失败"); return Result.failed("保存失败");
} }
} }
SongInfo song = buildSong(id, param, token); SongInfo song = buildSong(id, param, token);
song.setId(id);
song.setUrl(url); song.setUrl(url);
song.setImage(image); song.setImage(image);
songDao.save(song); songDao.save(song);
// 保存歌曲的标签信息
batchSaveSongTagInfo(id, param); batchSaveSongTagInfo(id, param);
return Result.success(); return Result.success();
} }
private SongInfo buildSong(String id, SongAddModel param, String token) { private SongInfo buildSong(String id, SongAddModel param, String token) {
SongInfo song = new SongInfo(); SongInfo song = new SongInfo();
if (!StringUtils.isBlank(id)) { song.setId(id);
song.setId(id);
}
song.setName(param.getName()); song.setName(param.getName());
song.setArtist(param.getArtist()); song.setArtist(param.getArtist());
song.setAlbum(param.getAlbum()); song.setAlbum(param.getAlbum());
song.setLyric(param.getLyric()); // 通过 token 获取用户信息
UserInfo userInfo = userClient.queryUserInfoById(param.getUserId()); UserLoginDto userLoginDto = getUserInfoByToken(token);
if (!Objects.isNull(userInfo)) { if (!Objects.isNull(userLoginDto)) {
song.setUserId(userInfo.getId()); song.setUserId(userLoginDto.getUserId());
song.setUserName(userInfo.getName()); song.setUserName(userLoginDto.getNickName());
song.setUserType(userInfo.getType()); song.setUserType(userLoginDto.getRoles());
} }
return song; return song;
} }
private String moveSongFile(String id, SongAddModel param) { /**
String url = param.getUrl(); * temp/ copy to song/
String srcKey = url.substring(url.indexOf(Constants.TEMP_KEY_PREFIX)); * @param id
String suffix = url.substring(url.lastIndexOf(Constants.DOT)); * @param path
String destKeySuffix = id + "/00" + suffix; * @return
String destKey = Constants.SONG_KEY_PREFIX + destKeySuffix; */
int copy = s3Service.copy(Constants.BUCKET, srcKey, destKey); private String moveSongFileOrImage(String id, String path) {
if (copy > 0) { if (StringUtils.isNotBlank(path)) {
return destKeySuffix; String srcKey = path.substring(path.indexOf(Constants.TEMP_KEY_PREFIX));
} String suffix = path.substring(path.lastIndexOf(Constants.DOT));
return null; 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);
private String moveSongImage(String id, SongAddModel param) { if (copy > 0) {
String image = param.getImage(); return destKeySuffix;
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;
} }
return null; return null;
} }
@ -291,20 +276,14 @@ public class CMSSongService {
} }
/** /**
* *
* @param id * @param id
* @return * @return
*/ */
public Result<SongVO> findOne(String id) { public Result<SongRespDto> findOne(String id) {
SongInfo song = songDao.findById(id).get(); SongInfo song = songDao.findById(id).get();
SongVO response = new SongVO(); List<String> tagIdsBySongId = songTagDao.findTagIdsBySongId(id);
response.setId(song.getId()); SongRespDto response = SongRespDto.convertPojo(song, tagIdsBySongId);
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());
return Result.success(response); return Result.success(response);
} }
@ -318,9 +297,9 @@ public class CMSSongService {
public Result update(String id, String token, SongAddModel param) { public Result update(String id, String token, SongAddModel param) {
SongInfo song = buildSong(id, param, token); SongInfo song = buildSong(id, param, token);
SongInfo oldSong = songDao.findById(id).get(); SongInfo oldSong = songDao.findById(id).get();
String url = null; // 路径包含temp/说明歌曲文件或封面修改了
if (param.getUrl().contains(Constants.TEMP_KEY_PREFIX)) { if (param.getUrl().contains(Constants.TEMP_KEY_PREFIX)) {
url = moveSongFile(id, param); String url = moveSongFileOrImage(id, param.getUrl());
if (StringUtils.isBlank(url)) { if (StringUtils.isBlank(url)) {
return Result.failed("更新失败"); return Result.failed("更新失败");
} }
@ -332,9 +311,8 @@ public class CMSSongService {
song.setSize(oldSong.getSize()); song.setSize(oldSong.getSize());
song.setDuration(oldSong.getDuration()); song.setDuration(oldSong.getDuration());
} }
String image = null;
if (param.getImage().contains(Constants.TEMP_KEY_PREFIX)) { if (param.getImage().contains(Constants.TEMP_KEY_PREFIX)) {
image = moveSongImage(id, param); String image = moveSongFileOrImage(id, param.getImage());
if (StringUtils.isBlank(image)) { if (StringUtils.isBlank(image)) {
return Result.failed("更新失败"); return Result.failed("更新失败");
} }
@ -344,9 +322,13 @@ public class CMSSongService {
} }
song.setState(oldSong.getState()); song.setState(oldSong.getState());
song.setCreateTime(oldSong.getCreateTime()); song.setCreateTime(oldSong.getCreateTime());
song.setLyric(oldSong.getLyric());
song.setLyricUrl(oldSong.getLyricUrl());
songDao.save(song); songDao.save(song);
// 更新标签信息
songTagDao.deleteBySongId(id); songTagDao.deleteBySongId(id);
batchSaveSongTagInfo(id, param); batchSaveSongTagInfo(id, param);
// 更新期刊歌曲表的信息
batchUpdateJournalSongInfo(id, song); batchUpdateJournalSongInfo(id, song);
return Result.success(); return Result.success();
} }
@ -365,8 +347,17 @@ public class CMSSongService {
item.setAlbum(song.getAlbum()); item.setAlbum(song.getAlbum());
item.setUrl(song.getUrl()); item.setUrl(song.getUrl());
item.setImage(song.getImage()); 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); journalSongDao.saveAll(journalSongList);
} }
} }
@ -379,6 +370,7 @@ public class CMSSongService {
*/ */
public Result updateSongState(String id, String state) { public Result updateSongState(String id, String state) {
songDao.updateSongState(id, state); songDao.updateSongState(id, state);
// 歌曲停用删除期刊歌曲表的歌曲信息
if ("0".equals(state)) { if ("0".equals(state)) {
journalSongDao.deleteBySongId(id); journalSongDao.deleteBySongId(id);
} }
@ -390,18 +382,21 @@ public class CMSSongService {
* @param id * @param id
* @return * @return
*/ */
public Result<SongVO> querySongLyric(String id) { public Result<SongLyricRespDto> querySongLyric(String id) {
SongVO result = new SongVO();
SongInfo song = songDao.findById(id).get(); SongInfo song = songDao.findById(id).get();
if (StringUtils.isNotBlank(song.getLyric())) { SongLyricRespDto result = new SongLyricRespDto();
result.setLyric(song.getLyric()); if (!Objects.isNull(song)) {
} else { result.setId(song.getId());
if (StringUtils.isNotBlank(song.getUrl())) { if (StringUtils.isNotBlank(song.getLyric())) {
//String url = song.getUrl(); result.setLyric(song.getLyric());
//String lyricSuffix = url.substring(0, url.lastIndexOf(Constants.DOT)) + ".lyric"; } else {
String lyricUrl = Constants.RESOURCE_PREFIX + Constants.SONG_KEY_PREFIX + song.getLyricUrl(); if (StringUtils.isNotBlank(song.getUrl())) {
String lyric = readLyric(lyricUrl); String lyricUrl = Constants.SONG_KEY_PREFIX + song.getLyricUrl();
result.setLyric(lyric); if (s3Service.checkFileExist(Constants.BUCKET, lyricUrl)) {
String lyric = readLyric(lyricUrl);
result.setLyric(lyric);
}
}
} }
} }
return Result.success(result); return Result.success(result);
@ -415,16 +410,28 @@ public class CMSSongService {
*/ */
public Result updateSongLyric(String id, String lyric) { public Result updateSongLyric(String id, String lyric) {
if (StringUtils.isNotBlank(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; String key = Constants.SONG_KEY_PREFIX + lyricUrl;
songDao.updateSongLyricAndUrl(id, lyric, lyricUrl); int upload = s3Service.uploadText(Constants.BUCKET, key, lyric);
s3Service.uploadText(Constants.BUCKET, key, lyric); if (upload > 0) {
songDao.updateSongLyricAndUrl(id, lyric, lyricUrl);
// 更新期刊歌曲表的歌词路径
List<JournalSong> 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(); return Result.success();
} }
/** /**
* *
* @param id * @param id
* @return * @return
*/ */
@ -441,16 +448,19 @@ public class CMSSongService {
/** /**
* *
* @param token
* @param files * @param files
* @return * @return
*/ */
public Result batchUpload(List<MultipartFile> files) { public Result<List<SongBaseRespDto>> batchUpload(String token, List<MultipartFile> files) {
List<SongBaseRespDto> result = new ArrayList<>();
UserLoginDto userInfo = getUserInfoByToken(token);
for (MultipartFile file : files) for (MultipartFile file : files)
if (!file.isEmpty()) { if (!file.isEmpty()) {
String id = String.valueOf(idWorker.nextId()); String id = String.valueOf(idWorker.nextId());
String originalFilename = file.getOriginalFilename(); String originalFilename = file.getOriginalFilename();
String suffix = originalFilename.substring(originalFilename.lastIndexOf(Constants.DOT)); 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; String key = Constants.SONG_KEY_PREFIX + keySuffix;
int upload = s3Service.upload(Constants.BUCKET, key, file); int upload = s3Service.upload(Constants.BUCKET, key, file);
if (upload > 0) { if (upload > 0) {
@ -460,10 +470,17 @@ public class CMSSongService {
song.setUrl(keySuffix); song.setUrl(keySuffix);
song.setSize(file.isEmpty() ? 0L : file.getSize()); song.setSize(file.isEmpty() ? 0L : file.getSize());
song.setDuration(CommonUtil.getSongDuration(file)); 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 * @param token
* @return * @return
*/ */
/*private UserLoginDto getUserInfoByToken(String token) { private UserLoginDto getUserInfoByToken(String token) {
if (StringUtils.isNotBlank(token)) { if (StringUtils.isNotBlank(token)) {
try { try {
UserLoginDto user = jwtUtil.getUserLoginDto(token); UserLoginDto user = jwtUtil.getUserLoginDto(token);
@ -482,7 +499,7 @@ public class CMSSongService {
} }
} }
return null; return null;
}*/ }
/** /**
* *
@ -492,7 +509,8 @@ public class CMSSongService {
private String readLyric(String lyricUrl) { private String readLyric(String lyricUrl) {
try { try {
// 创建 URL 对象 // 创建 URL 对象
URL url = new URL(lyricUrl); String fullPath = Constants.RESOURCE_PREFIX + lyricUrl;
URL url = new URL(fullPath);
// 打开 HTTP 连接 // 打开 HTTP 连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 设置请求方法 // 设置请求方法

@ -1,11 +1,9 @@
package com.luoo.music.service; package com.luoo.music.service;
import api.Result; import api.Result;
import com.luoo.music.pojo.JournalSong; import com.luoo.music.dto.response.cms.UploadRespDto;
import com.luoo.music.response.cms.UploadRespDto;
import com.luoo.music.util.CommonUtil; import com.luoo.music.util.CommonUtil;
import com.luoo.music.util.Constants; import com.luoo.music.util.Constants;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
@ -17,14 +15,11 @@ import util.IdWorker;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.util.List;
@Service @Service
public class S3Service { public class S3Service {
@Autowired @Autowired
private S3Client s3Client; private S3Client s3Client;
@Autowired @Autowired
@ -35,7 +30,7 @@ public class S3Service {
* *
* @param journalSongList * @param journalSongList
*/ */
public void fromSongToMusic(List<JournalSong> journalSongList) { /*public void fromSongToMusic(List<JournalSong> journalSongList) {
journalSongList.parallelStream().forEach(this::fromSongToMusic); journalSongList.parallelStream().forEach(this::fromSongToMusic);
} }
private void fromSongToMusic(JournalSong item) { private void fromSongToMusic(JournalSong item) {
@ -70,7 +65,8 @@ public class S3Service {
copy(Constants.BUCKET, srcLyric, destLyric); copy(Constants.BUCKET, srcLyric, destLyric);
} }
} }
} }*/
/** /**
* @param file * @param file
* @return * @return

@ -12,22 +12,17 @@ public class Constants {
public static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); 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 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 SONG_RESOURCE_PREFIX="http://cdn.indie.cn/song/";
public static final String BUCKET = "indie"; public static final String BUCKET = "indie";
public static final String RESOURCE_PREFIX="http://cdn.indie.cn/"; public static final String RESOURCE_PREFIX="http://cdn.indie.cn/";
public static final String MUSIC_KEY_PREFIX = "music/"; 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 SONG_KEY_PREFIX = "song/";
public static final String TEMP_KEY_PREFIX = "temp/"; public static final String TEMP_KEY_PREFIX = "temp/";
public static final String DOT = "."; public static final String DOT = ".";
public static final String SLASH = "/"; public static final String COMMA = ",";
} }

Loading…
Cancel
Save