From 84c9a11eafc11a6e0192674523928519bc4c4b62 Mon Sep 17 00:00:00 2001 From: Gary Date: Thu, 8 Feb 2024 10:06:15 +0800 Subject: [PATCH] 1.check same content before update --- .../luoo/music/service/CMSJournalService.java | 56 ++++++++++++++++++- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/luoo_music/src/main/java/com/luoo/music/service/CMSJournalService.java b/luoo_music/src/main/java/com/luoo/music/service/CMSJournalService.java index ed64ee9..e26f970 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/CMSJournalService.java +++ b/luoo_music/src/main/java/com/luoo/music/service/CMSJournalService.java @@ -413,29 +413,79 @@ public class CMSJournalService { // 歌曲信息 List updateSongs = param.getSongs(); if (!CollectionUtils.isEmpty(updateSongs)) { - // 清理当前journalSong - journalSongDao.deleteByJournalNo(journalNo); List songList = songDao.orderByField(updateSongs); + List journalSongs=journalSongDao.findByJournalNoOrderBySongNo(journalNo); + //如果JournalSong 保存信息与SongInfo 一致,则不更新 + if(isSameContent(songList,journalSongs)) { + return; + } + + Map songId2JournalSongIdMap=journalSongs.stream().collect(Collectors.toMap(JournalSong::getSongId, JournalSong::getId)); + List updatedJournalSong = new ArrayList<>(); for (int i = 0; i < songList.size(); i++) { SongInfo song = songList.get(i); JournalSong journalSong = new JournalSong(); - journalSong.setId(song.getId()); + + 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&¤t.equals(previous))) { + return true; + } + return false; + } + + private boolean isSameContent(List current, List 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();