1.check same content before update

main
Gary 10 months ago
parent cfc3e8296a
commit 84c9a11eaf

@ -413,29 +413,79 @@ public class CMSJournalService {
// 歌曲信息 // 歌曲信息
List<String> updateSongs = param.getSongs(); List<String> updateSongs = param.getSongs();
if (!CollectionUtils.isEmpty(updateSongs)) { if (!CollectionUtils.isEmpty(updateSongs)) {
// 清理当前journalSong
journalSongDao.deleteByJournalNo(journalNo);
List<SongInfo> songList = songDao.orderByField(updateSongs); List<SongInfo> songList = songDao.orderByField(updateSongs);
List<JournalSong> journalSongs=journalSongDao.findByJournalNoOrderBySongNo(journalNo);
//如果JournalSong 保存信息与SongInfo 一致,则不更新
if(isSameContent(songList,journalSongs)) {
return;
}
Map<String,String> songId2JournalSongIdMap=journalSongs.stream().collect(Collectors.toMap(JournalSong::getSongId, JournalSong::getId));
List<JournalSong> updatedJournalSong = new ArrayList<>(); List<JournalSong> updatedJournalSong = new ArrayList<>();
for (int i = 0; i < songList.size(); i++) { for (int i = 0; i < songList.size(); i++) {
SongInfo song = songList.get(i); SongInfo song = songList.get(i);
JournalSong journalSong = new JournalSong(); JournalSong journalSong = new JournalSong();
journalSong.setId(song.getId());
journalSong.setId(songId2JournalSongIdMap.getOrDefault(song.getId(), 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());
journalSong.setUrl(Constants.SONG_KEY_PREFIX+song.getUrl()); journalSong.setUrl(Constants.SONG_KEY_PREFIX+song.getUrl());
journalSong.setImage(Constants.SONG_KEY_PREFIX+song.getImage()); journalSong.setImage(Constants.SONG_KEY_PREFIX+song.getImage());
journalSong.setLyricUrl(Constants.SONG_KEY_PREFIX+song.getLyricUrl()); journalSong.setLyricUrl(Constants.SONG_KEY_PREFIX+song.getLyricUrl());
journalSong.setSongId(song.getId()); journalSong.setSongId(song.getId());
journalSong.setJournalNo(journalNo); journalSong.setJournalNo(journalNo);
journalSong.setSongNo(i+1); journalSong.setSongNo(i+1);
updatedJournalSong.add(journalSong); updatedJournalSong.add(journalSong);
} }
// 清理当前journalSong
journalSongDao.deleteByJournalNo(journalNo);
journalSongDao.saveAll(updatedJournalSong); 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) { private JournalTag createJournalTag(String tagId, String journalId) {
JournalTag journalTag = new JournalTag(); JournalTag journalTag = new JournalTag();

Loading…
Cancel
Save