@ -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 < > ( 0 L , result ) ) ;
return Result . success ( new PageResult < > ( 0 L , 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 = t agInfo( ids ) ;
Map < String , List < String > > journalTagMap = journalT agInfo( 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 = s ongCountInfo( journalNos ) ;
Map < String , Long > journalSongCountMap = journalS ongCountInfo( 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 > > t agInfo( 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 中
updateJour a nlNameChTags( journalId ) ;
updateJour na lNameChTags( 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 updateJour na lNameChTags( 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 < Journal VO > findOne ( String id ) {
public Result < Journal RespDto > 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 < Song VO > journalSongInfo ( String journalNo ) {
private List < Song BaseRespDto > journalSongInfo ( String journalNo ) {
List < Song VO > list = new ArrayList < > ( ) ;
List < Song BaseRespDto > 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
* /
* /