@ -5,13 +5,13 @@ import api.Result;
import com.luoo.music.client.UserClient ;
import com.luoo.music.dao.* ;
import com.luoo.music.pojo.* ;
import com.luoo.music.request.cms.JournalAddModel ;
import com.luoo.music.request.cms.JournalPublishReq ;
import com.luoo.music.request.cms.JournalQueryModel ;
import com.luoo.music.response.cms.JournalVO ;
import com.luoo.music.response.cms.SongVO ;
import com.luoo.music.dto.request.cms.JournalAddModel ;
import com.luoo.music.dto.request.cms.JournalPublishReq ;
import com.luoo.music.dto.request.cms.JournalQueryModel ;
import com.luoo.music.dto.response.cms.JournalListRespDto ;
import com.luoo.music.dto.response.cms.JournalRespDto ;
import com.luoo.music.dto.response.cms.SongBaseRespDto ;
import com.luoo.music.util.Constants ;
import dto.UserLoginDto ;
import org.apache.commons.lang.StringUtils ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.data.domain.Page ;
@ -21,8 +21,6 @@ import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service ;
import org.springframework.util.CollectionUtils ;
import util.IdWorker ;
import util.JwtUtil ;
import util.StringTools ;
import javax.persistence.criteria.CriteriaBuilder ;
import javax.persistence.criteria.CriteriaQuery ;
@ -30,7 +28,6 @@ import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root ;
import java.time.LocalDateTime ;
import java.util.* ;
import java.util.function.Function ;
import java.util.stream.Collectors ;
/ * *
@ -60,20 +57,19 @@ public class CMSJournalService {
/ * *
* 分 页 + 条 件 检 索
*
* @param param
* @param page
* @param size
* @return
* /
public Result < PageResult < JournalVO > > search ( JournalQueryModel param , int page , int size ) {
List < JournalVO > result = new ArrayList < > ( ) ;
// Sort sort = new Sort(Sort.Direction.DESC, "journalNo");
public Result < PageResult < JournalListRespDto > > search ( JournalQueryModel param , int page , int size ) {
List < JournalListRespDto > result = new ArrayList < > ( ) ;
PageRequest pageRequest = PageRequest . of ( page - 1 , size ) ;
Page < Journal > journalPage ;
if ( ! Objects . isNull ( param ) ) {
List < String > journalIdsByTags = null ;
if ( ! Objects . isNull ( param ) ) {
if ( ! CollectionUtils . isEmpty ( param . getTags ( ) ) ) {
// 查询标签对应的全部期刊
journalIdsByTags = journalTagDao . findJournalIdsByTags ( param . getTags ( ) ) ;
if ( journalIdsByTags . size ( ) = = 0 ) {
return Result . success ( new PageResult < > ( 0 L , result ) ) ;
@ -82,7 +78,6 @@ public class CMSJournalService {
Specification < Journal > journalSpecification = buildSearchSpecification ( param , journalIdsByTags ) ;
journalPage = journalDao . findAll ( journalSpecification , pageRequest ) ;
} else {
List < String > journalIdsByTags = null ;
Specification < Journal > journalSpecification = buildSearchSpecification ( param , journalIdsByTags ) ;
journalPage = journalDao . findAll ( journalSpecification , pageRequest ) ;
}
@ -90,29 +85,11 @@ public class CMSJournalService {
List < Journal > content = journalPage . getContent ( ) ;
if ( ! CollectionUtils . isEmpty ( content ) ) {
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 ( ) ) ;
Map < String , Long > songCountMap = s ongCountInfo( journalNos ) ;
Map < String , Long > journalSongCountMap = journalS ongCountInfo( journalNos ) ;
for ( Journal item : content ) {
JournalVO response = new JournalVO ( ) ;
response . setId ( item . getId ( ) ) ;
response . setJournalNo ( item . getJournalNo ( ) ) ;
response . setTitle ( item . getTitle ( ) ) ;
response . setImage ( Constants . MUSIC_RESOURCE_PREFIX + item . getImage ( ) ) ;
if ( ! Objects . isNull ( item . getCreateTime ( ) ) ) {
response . setCreateTime ( item . getCreateTime ( ) . format ( Constants . formatter ) ) ;
}
response . setState ( item . getState ( ) ) ;
response . setIsPublish ( item . getIsPublish ( ) ) ;
if ( ! Objects . isNull ( item . getPubTime ( ) ) ) {
response . setPubTime ( item . getPubTime ( ) . format ( Constants . formatter ) ) ;
}
response . setUserId ( item . getUserId ( ) ) ;
response . setUserName ( item . getUserName ( ) ) ;
response . setSongCount ( songCountMap . get ( item . getJournalNo ( ) ) ) ;
response . setTag ( journalTagMap . get ( item . getId ( ) ) ) ;
response . setVisits ( item . getVisits ( ) ) ;
response . setComment ( item . getComment ( ) ) ;
JournalListRespDto response = JournalListRespDto . convertPojo ( item , journalTagMap , journalSongCountMap ) ;
result . add ( response ) ;
}
}
@ -121,7 +98,6 @@ public class CMSJournalService {
/ * *
* 检 索 条 件
*
* @param param
* @return
* /
@ -159,35 +135,17 @@ public class CMSJournalService {
} ;
}
/ * *
* 期 刊 的 音 乐 数 量
*
* @param journalNos
* @return
* /
private Map < 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
* @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 < > ( ) ;
List < JournalTag > journalTagList = journalTagDao . findByJournalIds ( ids ) ;
Set < String > tagIdSet = journalTagList . stream ( ) . map ( JournalTag : : getTagId ) . collect ( Collectors . toSet ( ) ) ;
List < Tag > tagList = tagDao . findByIdIn ( tagIdSet ) ;
Map < String , Tag > idTagMap = tagList . stream ( ) . collect ( Collectors . toMap ( Tag : : getId , obj - > obj ) ) ;
// Set<String> tagIdSet = journalTagList.stream().map(JournalTag::getTagId).collect(Collectors.toSet());
// List<Tag> tagList = tagDao.findByIdIn(tagIdSet);
// Map<String, Tag> idTagMap = tagList.stream().collect(Collectors.toMap(Tag::getId, obj -> obj));
List < String > list ;
for ( JournalTag item : journalTagList ) {
String journalId = item . getJournalId ( ) ;
@ -196,74 +154,90 @@ public class CMSJournalService {
} else {
list = new ArrayList < > ( ) ;
}
if ( idTagMap . containsKey ( item . getTagId ( ) ) ) {
Tag tag = idTagMap . get ( item . getTagId ( ) ) ;
list . add ( tag . getId ( ) ) ;
}
list . add ( item . getTagId ( ) ) ;
journalTagMap . put ( journalId , list ) ;
}
return journalTagMap ;
}
/ * *
* 期 刊 的 音 乐 数 量
* @param journalNos
* @return
* /
private Map < String , Long > journalSongCountInfo ( 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 param
* @return
* /
public Result add ( String token , JournalAddModel param ) {
public Result add ( JournalAddModel param ) {
List < Journal > byJournalNo = journalDao . findByJournalNo ( param . getJournalNo ( ) ) ;
if ( byJournalNo . size ( ) > 0 ) {
return Result . failed ( "保存失败,期刊编号已存在" ) ;
}
String image = null ;
if ( StringUtils . isNotBlank ( param . getImage ( ) ) ) {
// 新增时有图片一定是 temp/
String srcKey = param . getImage ( ) . substring ( param . getImage ( ) . indexOf ( Constants . TEMP_KEY_PREFIX ) ) ;
String image = moveJournalImage ( srcKey , param ) ;
if ( StringUtils . isNotBlank ( image ) ) {
Journal journal = buildJournal ( null , param , token ) ;
image = moveJournalImage ( srcKey , param ) ;
if ( StringUtils . isBlank ( image ) ) {
return Result . failed ( "保存失败" ) ;
}
}
String id = String . valueOf ( idWorker . nextId ( ) ) ;
journal . setId ( id ) ;
Journal journal = buildJournal ( id , param ) ;
journal . setImage ( image ) ;
journalDao . save ( journal ) ;
// 保存期刊的标签、歌曲信息
batchSaveJournalRelatesInfo ( id , param ) ;
return Result . success ( ) ;
} else {
return Result . failed ( "保存失败" ) ;
}
}
/ * *
* 移 动 对 象
*
* 移 动 期 刊 封 面
* @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/ 00 %s", Integer . parseInt ( param . getJournalNo ( ) ) , suffix ) ;
String destKeySuffix = String . format ( "%05d/ %d %s", Integer . parseInt ( param . getJournalNo ( ) ) , idWorker . nextId ( ) , suffix ) ;
String destKey = Constants . MUSIC_KEY_PREFIX + destKeySuffix ;
int copy = s3Service . copy ( Constants . BUCKET , srcKey , destKey ) ;
if ( copy > 0 ) {
return destKeySuffix ;
}
}
return null ;
}
/ * *
* 构 建 对 象
*
* @param id
* @param param
* @param token
* @return
* /
private Journal buildJournal ( String id , JournalAddModel param , String token ) {
private Journal buildJournal ( String id , JournalAddModel param ) {
Journal journal = new Journal ( ) ;
if ( ! StringUtils . isBlank ( id ) ) {
journal . setId ( id ) ;
}
journal . setJournalNo ( param . getJournalNo ( ) ) ;
journal . setTitle ( param . getTitle ( ) ) ;
journal . setSummary ( param . getSummary ( ) ) ;
journal . setContent ( param . getContent ( ) ) ;
UserInfo userInfo = userClient . queryUserInfoById ( param . getUserId ( ) ) ;
if ( ! Objects . isNull ( userInfo ) ) {
@ -276,7 +250,6 @@ public class CMSJournalService {
/ * *
* 保 存 期 刊 的 标 签 、 音 乐 信 息
*
* @param journalId
* @param param
* /
@ -293,43 +266,53 @@ public class CMSJournalService {
}
journalTagDao . saveAll ( journalTagList ) ;
//save tag 中文名到 journal 中
updateJour a nlNameChTags( journalId ) ;
updateJour na lNameChTags( journalId ) ;
}
// 歌曲信息
if ( ! CollectionUtils . isEmpty ( param . getSongs ( ) ) ) {
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 ( ) ) {
for ( int i = 0 ; i < param . getSongs ( ) . size ( ) ; i + + ) {
String item = param . getSongs ( ) . get ( i ) ;
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 ( ) ) ;
if ( StringUtils . isNotBlank ( song . getUrl ( ) ) ) {
journalSong . setUrl ( Constants . SONG_KEY_PREFIX + song . getUrl ( ) ) ;
} else {
journalSong . setUrl ( song . getUrl ( ) ) ;
}
if ( StringUtils . isNotBlank ( song . getImage ( ) ) ) {
journalSong . setImage ( Constants . SONG_KEY_PREFIX + song . getImage ( ) ) ;
} else {
journalSong . setImage ( song . getImage ( ) ) ;
}
journalSong . setSongId ( item ) ;
journalSong . setJournalNo ( param . getJournalNo ( ) ) ;
journalSong . setSongNo ( num ) ;
journalSong . setSongId ( song . getId ( ) ) ;
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 ) ;
}
}
s3Service . fromSongToMusic ( journalSongList ) ;
journalSongDao . saveAll ( journalSongList ) ;
}
}
private void updateJour a nlNameChTags( String journalId ) {
private void updateJour na lNameChTags( String journalId ) {
Optional < Journal > optional = journalDao . findById ( journalId ) ;
if ( optional . isPresent ( ) ) {
List < String > tags = tagDao . getTagNameChByJournalId ( journalId ) ;
if ( ! tags . isEmpty ( ) ) {
String tagStr = tags . stream ( ) . distinct ( ) . sorted ( ) . collect ( Collectors . joining ( "," ) ) ;
String tagStr = tags . stream ( ) . distinct ( ) . sorted ( ) . collect ( Collectors . joining ( Constants . COMMA ) ) ;
Journal journal = optional . get ( ) ;
journal . setNameChTags ( tagStr ) ;
journalDao . save ( journal ) ;
@ -339,51 +322,45 @@ public class CMSJournalService {
/ * *
* 查 询 期 刊 详 情
*
* @param id
* @return
* /
public Result < Journal VO > findOne ( String id ) {
public Result < Journal RespDto > findOne ( String id ) {
Optional < Journal > optional = journalDao . findById ( id ) ;
if ( ! optional . isPresent ( ) ) {
return Result . failed ( "无法找到期刊: " + id ) ;
}
Journal journal = optional . get ( ) ;
JournalVO response = new JournalVO ( ) ;
response . setId ( journal . getId ( ) ) ;
response . setJournalNo ( journal . getJournalNo ( ) ) ;
response . setTitle ( journal . getTitle ( ) ) ;
response . setTag ( journalTagDao . findTagsByJournalId ( id ) ) ;
response . setUserId ( journal . getUserId ( ) ) ;
if ( StringUtils . isNotBlank ( journal . getImage ( ) ) ) {
response . setImage ( Constants . MUSIC_RESOURCE_PREFIX + journal . getImage ( ) ) ;
}
response . setSongs ( journalSongInfo ( journal . getJournalNo ( ) ) ) ;
response . setContent ( journal . getContent ( ) ) ;
List < String > tagInfo = journalTagDao . findTagsByJournalId ( id ) ;
List < SongBaseRespDto > songInfo = journalSongInfo ( journal . getJournalNo ( ) ) ;
JournalRespDto response = JournalRespDto . convertPojo ( journal , tagInfo , songInfo ) ;
return Result . success ( response ) ;
}
/ * *
* 期 刊 的 音 乐 信 息
*
* 期 刊 的 歌 曲 信 息
* @param journalNo
* @return
* /
private List < Song VO > journalSongInfo ( String journalNo ) {
List < Song VO > list = new ArrayList < > ( ) ;
private List < Song BaseRespDto > journalSongInfo ( String journalNo ) {
List < Song BaseRespDto > list = new ArrayList < > ( ) ;
List < JournalSong > journalSongList = journalSongDao . findByJournalNo ( journalNo ) ;
if ( ! CollectionUtils . isEmpty ( journalSongList ) ) {
for ( JournalSong item : journalSongList ) {
SongVO songVO = new SongVO ( ) ;
SongBaseRespDto songVO = new SongBaseRespDto ( ) ;
songVO . setId ( item . getSongId ( ) ) ;
songVO . setName ( item . getName ( ) ) ;
songVO . setArtist ( item . getArtist ( ) ) ;
songVO . setAlbum ( item . getAlbum ( ) ) ;
if ( StringUtils . isNotBlank ( item . getUrl ( ) ) ) {
songVO . setUrl ( Constants . RESOURCE_PREFIX + item . getUrl ( ) ) ;
}
if ( StringUtils . isNotBlank ( item . getImage ( ) ) ) {
songVO . setImage ( Constants . RESOURCE_PREFIX + item . getImage ( ) ) ;
} else {
songVO . setImage ( item . getImage ( ) ) ;
}
if ( StringUtils . isNotBlank ( item . getUrl ( ) ) ) {
songVO . setUrl ( Constants . RESOURCE_PREFIX + item . getUrl ( ) ) ;
} else {
songVO . setUrl ( item . getUrl ( ) ) ;
}
list . add ( songVO ) ;
}
@ -393,192 +370,61 @@ public class CMSJournalService {
/ * *
* 更 新 期 刊
*
* @param id
* @param token
* @param param
* @return
* /
public Result update ( String id , String token , JournalAddModel param ) {
public Result update ( String id , JournalAddModel param ) {
// TODO
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 ) ;
batchUpdateJournalRelatesInfo ( id , journal . getJournalNo ( ) , param ) ;
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 ) ;
// 当前编辑页面期刊编号可修改
Journal oldJournal = optional . get ( ) ;
List < Journal > journals = journalDao . findByJournalNo ( param . getJournalNo ( ) ) ;
if ( ! oldJournal . getJournalNo ( ) . equals ( param . getJournalNo ( ) ) & & journals . size ( ) > 0 ) {
return Result . failed ( "期刊编号已存在" ) ;
}
Journal newJournal = buildJournal ( id , param ) ;
String srcKey = null ;
if ( StringUtils . isNotBlank ( param . getImage ( ) ) ) {
if ( param . getImage ( ) . contains ( Constants . TEMP_KEY_PREFIX ) ) {
srcKey = param . getImage ( ) . substring ( param . getImage ( ) . indexOf ( Constants . TEMP_KEY_PREFIX ) ) ;
} else {
if ( ! oldJournal . getJournalNo ( ) . equals ( param . getJournalNo ( ) ) ) {
srcKey = param . getImage ( ) . substring ( param . getImage ( ) . indexOf ( Constants . MUSIC_KEY_PREFIX ) ) ;
}
}
if ( StringUtils . isNotBlank ( srcKey ) ) {
String image = moveJournalImage ( srcKey , param ) ;
if ( StringUtils . isBlank ( image ) ) {
return Result . failed ( "更新失败" ) ;
}
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 ;
newJournal . setImage ( image ) ;
} else {
newJournal . setImage ( oldJournal . getImage ( ) ) ;
}
return null ;
} 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 ( ) ;
}
/ * *
* 删 除 期 刊 的 标 签 、 音 乐 信 息
*
* @param journalId
* @param journalNo
* /
@ -589,7 +435,6 @@ public class CMSJournalService {
/ * *
* 发 布 期 刊
*
* @param id
* @param queryModel
* @return
@ -611,7 +456,6 @@ public class CMSJournalService {
/ * *
* 更 新 期 刊 启 停 状 态
*
* @param id
* @param state
* @return
@ -623,7 +467,6 @@ public class CMSJournalService {
/ * *
* 删 除 期 刊
*
* @param id
* @return
* /