1.update url for song/image/lyric

main
Gary 10 months ago
parent e8ff3810af
commit c9d913fc61

@ -46,4 +46,6 @@ public interface JournalSongDao extends JpaRepository<JournalSong,String>, JpaSp
@Query(value = "select * from tb_journal_song where id in ?1 order by field(id,?1)", nativeQuery = true) @Query(value = "select * from tb_journal_song where id in ?1 order by field(id,?1)", nativeQuery = true)
public List<JournalSong> orderByField(List<String> idList); public List<JournalSong> orderByField(List<String> idList);
List<JournalSong> findByJournalNoOrderBySongNo(String journalNo);
} }

@ -1,5 +1,6 @@
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;
@ -25,5 +26,13 @@ public interface SongInfoDao extends JpaRepository<SongInfo,String>, JpaSpecific
@Query("UPDATE SongInfo s SET s.lyric = :lyric WHERE s.id = :id") @Query("UPDATE SongInfo s SET s.lyric = :lyric WHERE s.id = :id")
int updateSongLyric(@Param("id") String id, @Param("lyric") String lyric); 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);
List<SongInfo> findByIdIn(List<String> ids); List<SongInfo> findByIdIn(List<String> ids);
@Query(value = "select * from tb_song_info where id in ?1 order by field(id,?1)", nativeQuery = true)
List<SongInfo> orderByField(List<String> objectIds);
} }

@ -16,9 +16,9 @@ public class SongMapper {
songRespDTO.setArtist(song.getArtist()); songRespDTO.setArtist(song.getArtist());
songRespDTO.setId(song.getId()); songRespDTO.setId(song.getId());
songRespDTO.setTitle(song.getName()); songRespDTO.setTitle(song.getName());
songRespDTO.setSrc(Constants.MUSIC_RESOURCE_PREFIX + song.getUrl()); songRespDTO.setSrc(Constants.RESOURCE_PREFIX + song.getUrl());
songRespDTO.setPic(Constants.MUSIC_RESOURCE_PREFIX + song.getUrl().replace(".mp3", ".jpg")); songRespDTO.setPic(Constants.RESOURCE_PREFIX + song.getImage());
songRespDTO.setLrc(Constants.MUSIC_RESOURCE_PREFIX + song.getUrl().replace(".mp3", ".lyric")); songRespDTO.setLrc(Constants.RESOURCE_PREFIX + song.getLyricUrl());
songRespDTO.setSongNo(song.getSongNo()); songRespDTO.setSongNo(song.getSongNo());
songRespDTO.setHaveCollect(songCollectSet.contains(songRespDTO.getId())); songRespDTO.setHaveCollect(songCollectSet.contains(songRespDTO.getId()));
return songRespDTO; return songRespDTO;

@ -53,4 +53,8 @@ public class JournalSong implements Serializable{
* ID * ID
*/ */
private String songId; private String songId;
/**
*
*/
private String lyricUrl;
} }

@ -88,4 +88,9 @@ public class SongInfo implements Serializable {
* *
*/ */
private String lyric; private String lyric;
/**
*
*/
private String lyricUrl;
} }

@ -30,6 +30,7 @@ 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;
/** /**
@ -38,449 +39,528 @@ import java.util.stream.Collectors;
@Service @Service
public class CMSJournalService { public class CMSJournalService {
@Autowired @Autowired
private S3Service s3Service; private S3Service s3Service;
@Autowired @Autowired
private IdWorker idWorker; private IdWorker idWorker;
@Autowired @Autowired
private JournalTagDao journalTagDao; private JournalTagDao journalTagDao;
@Autowired @Autowired
private JournalDao journalDao; private JournalDao journalDao;
@Autowired @Autowired
private JournalSongDao journalSongDao; private JournalSongDao journalSongDao;
@Autowired @Autowired
private TagDao tagDao; private TagDao tagDao;
@Autowired @Autowired
private SongInfoDao songDao; private SongInfoDao songDao;
@Autowired @Autowired
private UserClient userClient; private UserClient userClient;
@Autowired @Autowired
private RedisTemplate redisTemplate; private RedisTemplate redisTemplate;
/**
/** * +
* + *
* @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<JournalVO>> search(JournalQueryModel param, int page, int size) {
List<JournalVO> result = new ArrayList<>(); List<JournalVO> result = new ArrayList<>();
// Sort sort = new Sort(Sort.Direction.DESC, "journalNo"); // 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;
if (!Objects.isNull(param)) { if (!Objects.isNull(param)) {
List<String> journalIdsByTags = null; 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));
} }
} }
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; 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);
} }
long totalElements = journalPage.getTotalElements(); long totalElements = journalPage.getTotalElements();
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 = tagInfo(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> songCountMap = songCountInfo(journalNos);
for (Journal item : content) { for (Journal item : content) {
JournalVO response = new JournalVO(); JournalVO response = new JournalVO();
response.setId(item.getId()); response.setId(item.getId());
response.setJournalNo(item.getJournalNo()); response.setJournalNo(item.getJournalNo());
response.setTitle(item.getTitle()); response.setTitle(item.getTitle());
response.setImage(Constants.MUSIC_RESOURCE_PREFIX + item.getImage()); response.setImage(Constants.MUSIC_RESOURCE_PREFIX + item.getImage());
if (!Objects.isNull(item.getCreateTime())) { if (!Objects.isNull(item.getCreateTime())) {
response.setCreateTime(item.getCreateTime().format(Constants.formatter)); response.setCreateTime(item.getCreateTime().format(Constants.formatter));
} }
response.setState(item.getState()); response.setState(item.getState());
response.setIsPublish(item.getIsPublish()); response.setIsPublish(item.getIsPublish());
if (!Objects.isNull(item.getPubTime())) { if (!Objects.isNull(item.getPubTime())) {
response.setPubTime(item.getPubTime().format(Constants.formatter)); response.setPubTime(item.getPubTime().format(Constants.formatter));
} }
response.setUserId(item.getUserId()); response.setUserId(item.getUserId());
response.setUserName(item.getUserName()); response.setUserName(item.getUserName());
response.setSongCount(songCountMap.get(item.getJournalNo())); response.setSongCount(songCountMap.get(item.getJournalNo()));
response.setTag(journalTagMap.get(item.getId())); response.setTag(journalTagMap.get(item.getId()));
response.setVisits(item.getVisits()); response.setVisits(item.getVisits());
response.setComment(item.getComment()); response.setComment(item.getComment());
result.add(response); result.add(response);
} }
} }
return Result.success(new PageResult<>(totalElements, result)); return Result.success(new PageResult<>(totalElements, result));
} }
/**
*
*
* @param param
* @return
*/
private Specification<Journal> buildSearchSpecification(JournalQueryModel param, List<String> ids) {
return (Root<Journal> root, CriteriaQuery<?> query, CriteriaBuilder builder) -> {
List<Predicate> predicateList = new ArrayList<Predicate>();
if ("1".equals(param.getTab())) {
predicateList.add(builder.equal(root.get("isPublish"), "1"));
} else if ("2".equals(param.getTab())) {
predicateList.add(builder.equal(root.get("state"), "0"));
} else {
Predicate state = builder.equal(root.get("state"), "1");
Predicate isPublish = builder.equal(root.get("isPublish"), "0");
predicateList.add(builder.and(state, isPublish));
}
if (StringUtils.isNotBlank(param.getUserId())) {
predicateList.add(builder.equal(root.get("userId"), param.getUserId()));
}
if (StringUtils.isNotBlank(param.getStart()) && StringUtils.isNotBlank(param.getEnd())) {
LocalDateTime start = LocalDateTime.parse(param.getStart() + "T00:00:00");
LocalDateTime end = LocalDateTime.parse(param.getEnd() + "T00:00:00");
predicateList.add(builder.between(root.get("createTime"), start, end));
}
if (!CollectionUtils.isEmpty(ids)) {
predicateList.add(root.get("journalNo").in(ids));
}
if (StringUtils.isNotBlank(param.getKeyword())) {
String likeExpression = "%" + param.getKeyword() + "%";
Predicate journalNo = builder.like(root.get("journalNo"), likeExpression);
Predicate title = builder.like(root.get("title"), likeExpression);
predicateList.add(builder.or(journalNo, title));
}
query.orderBy(builder.desc(root.get("journalNo").as(Integer.class)));
return builder.and(predicateList.toArray(new Predicate[predicateList.size()]));
};
}
/** /**
* *
* @param param *
* @return * @param journalNos
*/ * @return
private Specification<Journal> buildSearchSpecification(JournalQueryModel param, List<String> ids) { */
return (Root<Journal> root, CriteriaQuery<?> query, CriteriaBuilder builder) -> { private Map<String, Long> songCountInfo(List<String> journalNos) {
List<Predicate> predicateList = new ArrayList<Predicate>(); Map<String, Long> songCountMap = new HashMap<>();
if("1".equals(param.getTab())) { if (!CollectionUtils.isEmpty(journalNos)) {
predicateList.add(builder.equal(root.get("isPublish"), "1")); List<Map<String, Long>> journalSongCountList = journalSongDao.findSongCountByJournalNos(journalNos);
} else if ("2".equals(param.getTab())) { songCountMap = journalSongCountList.stream()
predicateList.add(builder.equal(root.get("state"), "0")); .collect(Collectors.toMap(map -> String.valueOf(map.get("journalNo")),
} else { map -> Long.valueOf(String.valueOf(map.get("songCount")))));
Predicate state = builder.equal(root.get("state"), "1"); }
Predicate isPublish = builder.equal(root.get("isPublish"), "0"); return songCountMap;
predicateList.add(builder.and(state, isPublish)); }
}
if(StringUtils.isNotBlank(param.getUserId())) {
predicateList.add(builder.equal(root.get("userId"), param.getUserId()));
}
if (StringUtils.isNotBlank(param.getStart()) && StringUtils.isNotBlank(param.getEnd())) {
LocalDateTime start = LocalDateTime.parse(param.getStart() + "T00:00:00");
LocalDateTime end = LocalDateTime.parse(param.getEnd() + "T00:00:00");
predicateList.add(builder.between(root.get("createTime"), start, end));
}
if (!CollectionUtils.isEmpty(ids)) {
predicateList.add(root.get("journalNo").in(ids));
}
if(StringUtils.isNotBlank(param.getKeyword())) {
String likeExpression = "%" + param.getKeyword() + "%";
Predicate journalNo = builder.like(root.get("journalNo"), likeExpression);
Predicate title = builder.like(root.get("title"), likeExpression);
predicateList.add(builder.or(journalNo, title));
}
query.orderBy(builder.desc(root.get("journalNo").as(Integer.class)));
return builder.and(predicateList.toArray(new Predicate[predicateList.size()]));
};
}
/** /**
* *
* @param journalNos *
* @return * @param ids
*/ * @return
private Map<String, Long> songCountInfo(List<String> journalNos) { */
Map<String, Long> songCountMap = new HashMap<>(); private Map<String, List<String>> tagInfo(List<String> ids) {
if (!CollectionUtils.isEmpty(journalNos)) { Map<String, List<String>> journalTagMap = new HashMap<>();
List<Map<String, Long>> journalSongCountList = journalSongDao.findSongCountByJournalNos(journalNos); List<JournalTag> journalTagList = journalTagDao.findByJournalIds(ids);
songCountMap = journalSongCountList.stream() Set<String> tagIdSet = journalTagList.stream().map(JournalTag::getTagId).collect(Collectors.toSet());
.collect(Collectors.toMap( List<Tag> tagList = tagDao.findByIdIn(tagIdSet);
map -> String.valueOf(map.get("journalNo")), Map<String, Tag> idTagMap = tagList.stream().collect(Collectors.toMap(Tag::getId, obj -> obj));
map -> Long.valueOf(String.valueOf(map.get("songCount"))))); List<String> list;
} for (JournalTag item : journalTagList) {
return songCountMap; String journalId = item.getJournalId();
} if (journalTagMap.containsKey(journalId)) {
list = journalTagMap.get(journalId);
} else {
list = new ArrayList<>();
}
if (idTagMap.containsKey(item.getTagId())) {
Tag tag = idTagMap.get(item.getTagId());
list.add(tag.getId());
}
journalTagMap.put(journalId, list);
}
return journalTagMap;
}
/** /**
* *
* @param ids *
* @return * @param param
*/ * @return
private Map<String, List<String>> tagInfo(List<String> ids) { */
Map<String, List<String>> journalTagMap = new HashMap<>(); public Result add(String token, JournalAddModel param) {
List<JournalTag> journalTagList = journalTagDao.findByJournalIds(ids); List<Journal> byJournalNo = journalDao.findByJournalNo(param.getJournalNo());
Set<String> tagIdSet = journalTagList.stream().map(JournalTag::getTagId).collect(Collectors.toSet()); if (byJournalNo.size() > 0) {
List<Tag> tagList = tagDao.findByIdIn(tagIdSet); return Result.failed("保存失败,期刊编号已存在");
Map<String, Tag> idTagMap = tagList.stream().collect(Collectors.toMap(Tag::getId, obj -> obj)); }
List<String> list; String srcKey = param.getImage().substring(param.getImage().indexOf(Constants.TEMP_KEY_PREFIX));
for (JournalTag item : journalTagList) { String image = moveJournalImage(srcKey, param);
String journalId = item.getJournalId(); if (StringUtils.isNotBlank(image)) {
if (journalTagMap.containsKey(journalId)) { Journal journal = buildJournal(null, param, token);
list = journalTagMap.get(journalId); String id = String.valueOf(idWorker.nextId());
} else { journal.setId(id);
list = new ArrayList<>(); journal.setImage(image);
} journalDao.save(journal);
if (idTagMap.containsKey(item.getTagId())) { batchSaveJournalRelatesInfo(id, param);
Tag tag = idTagMap.get(item.getTagId()); return Result.success();
list.add(tag.getId()); } else {
} return Result.failed("保存失败");
journalTagMap.put(journalId, list); }
} }
return journalTagMap;
}
/** /**
* *
* @param param *
* @return * @param param
*/ * @return
public Result add(String token, JournalAddModel param) { */
List<Journal> byJournalNo = journalDao.findByJournalNo(param.getJournalNo()); private String moveJournalImage(String srcKey, JournalAddModel param) {
if (byJournalNo.size() > 0) { String image = param.getImage();
return Result.failed("保存失败,期刊编号已存在"); String suffix = image.substring(image.lastIndexOf(Constants.DOT));
} String destKeySuffix = String.format("%05d/00%s", Integer.parseInt(param.getJournalNo()), suffix);
String srcKey = param.getImage().substring(param.getImage().indexOf(Constants.TEMP_KEY_PREFIX)); String destKey = Constants.MUSIC_KEY_PREFIX + destKeySuffix;
String image = moveJournalImage(srcKey, param); int copy = s3Service.copy(Constants.BUCKET, srcKey, destKey);
if (StringUtils.isNotBlank(image)) { if (copy > 0) {
Journal journal = buildJournal(null, param, token); return destKeySuffix;
String id = String.valueOf(idWorker.nextId()); }
journal.setId(id); return null;
journal.setImage(image); }
journalDao.save(journal);
batchSaveJournalRelatesInfo(id, param);
return Result.success();
} else {
return Result.failed("保存失败");
}
}
/** /**
* *
* @param param *
* @return * @param id
*/ * @param param
private String moveJournalImage(String srcKey, JournalAddModel param) { * @param token
String image = param.getImage(); * @return
String suffix = image.substring(image.lastIndexOf(Constants.DOT)); */
String destKeySuffix = String.format("%05d/00%s", Integer.parseInt(param.getJournalNo()), suffix); private Journal buildJournal(String id, JournalAddModel param, String token) {
String destKey = Constants.MUSIC_KEY_PREFIX + destKeySuffix; Journal journal = new Journal();
int copy = s3Service.copy(Constants.BUCKET, srcKey, destKey); if (!StringUtils.isBlank(id)) {
if (copy > 0) { journal.setId(id);
return destKeySuffix; }
} journal.setJournalNo(param.getJournalNo());
return null; journal.setTitle(param.getTitle());
} journal.setContent(param.getContent());
UserInfo userInfo = userClient.queryUserInfoById(param.getUserId());
if (!Objects.isNull(userInfo)) {
journal.setUserId(userInfo.getId());
journal.setUserName(userInfo.getName());
journal.setUserType(userInfo.getType());
}
return journal;
}
/** /**
* *
* @param id *
* @param param * @param journalId
* @param token * @param param
* @return */
*/ private void batchSaveJournalRelatesInfo(String journalId, JournalAddModel param) {
private Journal buildJournal(String id, JournalAddModel param, String token) { // 标签信息
Journal journal = new Journal(); if (!CollectionUtils.isEmpty(param.getTag())) {
if (!StringUtils.isBlank(id)) { List<JournalTag> journalTagList = new ArrayList<>();
journal.setId(id); for (String item : param.getTag()) {
} JournalTag journalTag = new JournalTag();
journal.setJournalNo(param.getJournalNo()); journalTag.setId(String.valueOf(idWorker.nextId()));
journal.setTitle(param.getTitle()); journalTag.setJournalId(journalId);
journal.setContent(param.getContent()); journalTag.setTagId(item);
UserInfo userInfo = userClient.queryUserInfoById(param.getUserId()); journalTagList.add(journalTag);
if (!Objects.isNull(userInfo)) { }
journal.setUserId(userInfo.getId()); journalTagDao.saveAll(journalTagList);
journal.setUserName(userInfo.getName()); }
journal.setUserType(userInfo.getType()); // 歌曲信息
} if (!CollectionUtils.isEmpty(param.getSongs())) {
return journal; List<SongInfo> songList = songDao.findByIdIn(param.getSongs());
} Map<String, SongInfo> idSongMap = songList.stream().collect(Collectors.toMap(SongInfo::getId, obj -> obj));
List<JournalSong> 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);
}
}
/** /**
* *
* @param journalId *
* @param param * @param id
*/ * @return
private void batchSaveJournalRelatesInfo(String journalId, JournalAddModel param) { */
// 标签信息 public Result<JournalVO> findOne(String id) {
if (!CollectionUtils.isEmpty(param.getTag())) { Optional<Journal> optional=journalDao.findById(id);
List<JournalTag> journalTagList = new ArrayList<>(); if(!optional.isPresent()) {
for (String item : param.getTag()) { return Result.failed("无法找到期刊: "+id);
JournalTag journalTag = new JournalTag(); }
journalTag.setId(String.valueOf(idWorker.nextId())); Journal journal = optional.get();
journalTag.setJournalId(journalId); JournalVO response = new JournalVO();
journalTag.setTagId(item); response.setId(journal.getId());
journalTagList.add(journalTag); response.setJournalNo(journal.getJournalNo());
} response.setTitle(journal.getTitle());
journalTagDao.saveAll(journalTagList); response.setTag(journalTagDao.findTagsByJournalId(id));
} response.setUserId(journal.getUserId());
// 歌曲信息 if (StringUtils.isNotBlank(journal.getImage())) {
if (!CollectionUtils.isEmpty(param.getSongs())) { response.setImage(Constants.MUSIC_RESOURCE_PREFIX + journal.getImage());
List<SongInfo> songList = songDao.findByIdIn(param.getSongs()); }
Map<String, SongInfo> idSongMap = songList.stream().collect(Collectors.toMap(SongInfo::getId, obj -> obj)); response.setSongs(journalSongInfo(journal.getJournalNo()));
List<JournalSong> journalSongList = new ArrayList<>(); response.setContent(journal.getContent());
int num = 0; return Result.success(response);
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);
}
}
/** /**
* *
* @param id *
* @return * @param journalNo
*/ * @return
public Result<JournalVO> findOne(String id) { */
Journal journal = journalDao.findById(id).get(); private List<SongVO> journalSongInfo(String journalNo) {
JournalVO response = new JournalVO(); List<SongVO> list = new ArrayList<>();
response.setId(journal.getId()); List<JournalSong> journalSongList = journalSongDao.findByJournalNo(journalNo);
response.setJournalNo(journal.getJournalNo()); if (!CollectionUtils.isEmpty(journalSongList)) {
response.setTitle(journal.getTitle()); for (JournalSong item : journalSongList) {
response.setTag(journalTagDao.findTagsByJournalId(id)); SongVO songVO = new SongVO();
response.setUserId(journal.getUserId()); songVO.setId(item.getSongId());
if (StringUtils.isNotBlank(journal.getImage())) { songVO.setName(item.getName());
response.setImage(Constants.MUSIC_RESOURCE_PREFIX + journal.getImage()); songVO.setArtist(item.getArtist());
} songVO.setAlbum(item.getAlbum());
response.setSongs(journalSongInfo(journal.getJournalNo())); if (StringUtils.isNotBlank(item.getUrl())) {
response.setContent(journal.getContent()); songVO.setUrl(Constants.RESOURCE_PREFIX + item.getUrl());
return Result.success(response); }
} if (StringUtils.isNotBlank(item.getImage())) {
songVO.setImage(Constants.RESOURCE_PREFIX + item.getImage());
}
list.add(songVO);
}
}
return list;
}
/** /**
* *
* @param journalNo *
* @return * @param id
*/ * @param token
private List<SongVO> journalSongInfo(String journalNo) { * @param param
List<SongVO> list = new ArrayList<>(); * @return
List<JournalSong> journalSongList = journalSongDao.findByJournalNo(journalNo); */
if (!CollectionUtils.isEmpty(journalSongList)) { public Result update(String id, String token, JournalAddModel param) {
for (JournalSong item : journalSongList) { Optional<Journal> optional = journalDao.findById(id);
SongVO songVO = new SongVO(); if (!optional.isPresent()) {
songVO.setId(item.getSongId()); return Result.failed("找不到期刊: " + id);
songVO.setName(item.getName()); }
songVO.setArtist(item.getArtist()); Journal journal = optional.get();
songVO.setAlbum(item.getAlbum()); updateJournal(journal, param, token);
if (StringUtils.isNotBlank(item.getUrl())) { journalDao.save(journal);
songVO.setUrl(Constants.MUSIC_RESOURCE_PREFIX + item.getUrl()); redisTemplate.opsForValue().set("journal_" + id, journal);
}
if (StringUtils.isNotBlank(item.getImage())) {
songVO.setImage(Constants.MUSIC_RESOURCE_PREFIX + item.getImage());
}
list.add(songVO);
}
}
return list;
}
/** batchUpdateJournalRelatesInfo(id, journal.getJournalNo(), param);
* return Result.success();
* @param id
* @param token
* @param param
* @return
*/
public Result update(String id, String token, JournalAddModel param) {
Optional<Journal> 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);
if (!CollectionUtils.isEmpty(param.getTag())) {
journalTagDao.deleteByJournalId(id);
}
if (!CollectionUtils.isEmpty(param.getSongs())) {
journalSongDao.deleteByJournalNo(id);
}
batchSaveJournalRelatesInfo(id, param);
return Result.success();
}
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) { private void batchUpdateJournalRelatesInfo(String journalId, String journalNo, JournalAddModel param) {
String destKeySuffix = String.format("%05d/%s", Integer.parseInt(journalNo), srcKey.replace(Constants.TEMP_KEY_PREFIX, "")); // 标签信息
String destKey = Constants.MUSIC_KEY_PREFIX + destKeySuffix; List<String> updateTags = param.getTag();
int copy = s3Service.copy(Constants.BUCKET, srcKey, destKey); List<String> currentTags = journalTagDao.findTagsByJournalId(journalId);
if (copy > 0) { if (!CollectionUtils.isEmpty(updateTags) && !isCollectionEqual(currentTags, updateTags)) {
return destKeySuffix; // 清理当前journalTag
} journalTagDao.deleteByJournalId(journalId);
return null; List<JournalTag> updatedJournalTags = updateTags.stream().map(t -> createJournalTag(t, journalId))
} .collect(Collectors.toList());
/** journalTagDao.saveAll(updatedJournalTags);
* }
* @param journalId
* @param journalNo
*/
private void batchDeleteJournalRelatesInfo(String journalId, String journalNo) {
journalTagDao.deleteByJournalId(journalId);
journalSongDao.deleteByJournalNo(journalNo);
}
/** // 歌曲信息
* List<String> updateSongs = param.getSongs();
* @param id if (!CollectionUtils.isEmpty(updateSongs)) {
* @param queryModel // 清理当前journalSong
* @return journalSongDao.deleteByJournalNo(journalId);
*/ List<SongInfo> songList = songDao.orderByField(updateSongs);
public Result publish(String id, JournalPublishReq queryModel) { List<JournalSong> updatedJournalSong = new ArrayList<>();
String scheduled = queryModel.getIsScheduled(); for (int i = 1; i < songList.size(); i++) {
String pubTimeStr = queryModel.getPubTime(); SongInfo song = songList.get(i);
LocalDateTime pubTime = LocalDateTime.now(); JournalSong journalSong = new JournalSong();
if ("1".equals(scheduled)) { journalSong.setId(String.valueOf(idWorker.nextId()));
if (StringUtils.isNotBlank(pubTimeStr)){ journalSong.setName(song.getName());
pubTime = LocalDateTime.parse(pubTimeStr, Constants.formatter); journalSong.setAlbum(song.getAlbum());
journalDao.updateScheduledPubById(id, pubTime); journalSong.setArtist(song.getArtist());
} journalSong.setUrl(Constants.SONG_KEY_PREFIX+song.getUrl());
} else { journalSong.setImage(Constants.SONG_KEY_PREFIX+song.getImage());
journalDao.updatePubById(id, pubTime); journalSong.setLyricUrl(Constants.SONG_KEY_PREFIX+song.getLyricUrl());
} journalSong.setSongId(song.getId());
return Result.success(); journalSong.setJournalNo(journalNo);
} journalSong.setSongNo(i);
/** updatedJournalSong.add(journalSong);
* }
* @param id journalSongDao.saveAll(updatedJournalSong);
* @param state }
* @return }
*/
public Result updateJournalState(String id, String state) { private JournalTag createJournalTag(String tagId, String journalId) {
journalDao.updateJournalState(id, state); JournalTag journalTag = new JournalTag();
return Result.success(); 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())) {
* @param id return true;
* @return }
*/ if (current.size() != update.size()) {
public Result deleteById(String id) { return false;
Journal journal = journalDao.findById(id).get(); }
if (!Objects.isNull(journal)) { Collections.sort(current);
journalDao.deleteById(id); Collections.sort(update);
batchDeleteJournalRelatesInfo(id, journal.getJournalNo()); for (int i = 0; i < current.size(); i++) {
} if (!current.get(i).equals(update.get(i))) {
return Result.success(); 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
*/
private void batchDeleteJournalRelatesInfo(String journalId, String journalNo) {
journalTagDao.deleteByJournalId(journalId);
journalSongDao.deleteByJournalNo(journalNo);
}
/**
*
*
* @param id
* @param queryModel
* @return
*/
public Result publish(String id, JournalPublishReq queryModel) {
String scheduled = queryModel.getIsScheduled();
String pubTimeStr = queryModel.getPubTime();
LocalDateTime pubTime = LocalDateTime.now();
if ("1".equals(scheduled)) {
if (StringUtils.isNotBlank(pubTimeStr)) {
pubTime = LocalDateTime.parse(pubTimeStr, Constants.formatter);
journalDao.updateScheduledPubById(id, pubTime);
}
} else {
journalDao.updatePubById(id, pubTime);
}
return Result.success();
}
/**
*
*
* @param id
* @param state
* @return
*/
public Result updateJournalState(String id, String state) {
journalDao.updateJournalState(id, state);
return Result.success();
}
/**
*
*
* @param id
* @return
*/
public Result deleteById(String id) {
Journal journal = journalDao.findById(id).get();
if (!Objects.isNull(journal)) {
journalDao.deleteById(id);
batchDeleteJournalRelatesInfo(id, journal.getJournalNo());
}
return Result.success();
}
} }

@ -397,9 +397,9 @@ public class CMSSongService {
result.setLyric(song.getLyric()); result.setLyric(song.getLyric());
} else { } else {
if (StringUtils.isNotBlank(song.getUrl())) { if (StringUtils.isNotBlank(song.getUrl())) {
String url = song.getUrl(); //String url = song.getUrl();
String lyricSuffix = url.substring(0, url.lastIndexOf(Constants.DOT)) + ".lyric"; //String lyricSuffix = url.substring(0, url.lastIndexOf(Constants.DOT)) + ".lyric";
String lyricUrl = Constants.RESOURCE_PREFIX + Constants.SONG_KEY_PREFIX + lyricSuffix; String lyricUrl = Constants.RESOURCE_PREFIX + Constants.SONG_KEY_PREFIX + song.getLyricUrl();
String lyric = readLyric(lyricUrl); String lyric = readLyric(lyricUrl);
result.setLyric(lyric); result.setLyric(lyric);
} }
@ -414,17 +414,11 @@ public class CMSSongService {
* @return * @return
*/ */
public Result updateSongLyric(String id, String lyric) { public Result updateSongLyric(String id, String lyric) {
songDao.updateSongLyric(id, lyric);
if (StringUtils.isNotBlank(lyric)) { if (StringUtils.isNotBlank(lyric)) {
String key = Constants.SONG_KEY_PREFIX + id + "/00.lyric"; String lyricUrl=id + "/"+idWorker.nextId()+".lyric";
String key = Constants.SONG_KEY_PREFIX + lyricUrl;
songDao.updateSongLyricAndUrl(id, lyric, lyricUrl);
s3Service.uploadText(Constants.BUCKET, key, lyric); s3Service.uploadText(Constants.BUCKET, key, lyric);
List<JournalSong> bySongId = journalSongDao.findBySongId(id);
for (JournalSong item : bySongId) {
String format = String.format("%05d/%02d", Integer.parseInt(item.getJournalNo()), item.getSongNo());
String destLyricSuffix = format + ".lyric";
String destLyric = Constants.MUSIC_KEY_PREFIX + destLyricSuffix;
s3Service.copy(Constants.BUCKET, key, destLyric);
}
} }
return Result.success(); return Result.success();
} }

Loading…
Cancel
Save