diff --git a/par-core/src/main/java/com/par/core/service/impl/SiteSnapshotServiceImpl.java b/par-core/src/main/java/com/par/core/service/impl/SiteSnapshotServiceImpl.java index 0596067..26076ee 100644 --- a/par-core/src/main/java/com/par/core/service/impl/SiteSnapshotServiceImpl.java +++ b/par-core/src/main/java/com/par/core/service/impl/SiteSnapshotServiceImpl.java @@ -134,11 +134,12 @@ public class SiteSnapshotServiceImpl implements SiteSnapshotService { return null; } - int newVersion = (latestDraft != null) ? latestDraft.getVersion() + 1 : 1; + // 删除旧 draft,保证最新只有一条 + snapshotMapper.deleteBySiteIdAndStatus(siteId, "draft"); SiteSnapshot snapshot = new SiteSnapshot(); snapshot.setSiteId(siteId); - snapshot.setVersion(newVersion); + snapshot.setVersion((latestDraft != null) ? latestDraft.getVersion() + 1 : 1); populateSnapshotFields(snapshot, data); snapshot.setChecksum(checksum); snapshot.setStatus("draft"); @@ -149,7 +150,7 @@ public class SiteSnapshotServiceImpl implements SiteSnapshotService { String bundleJson = toBundleJson(snapshot); jsonCache.put(draftKey(siteId), bundleJson); - log.info("快照已更新: siteId={}, version={}, checksum={}", siteId, newVersion, checksum.substring(0, 8)); + log.info("快照已更新: siteId={}, version={}, checksum={}", siteId, snapshot.getVersion(), checksum.substring(0, 8)); return snapshot; } catch (Exception e) { log.error("更新快照失败 siteId={}", siteId, e); @@ -177,11 +178,12 @@ public class SiteSnapshotServiceImpl implements SiteSnapshotService { return latestDraft; } - int newVersion = (latestDraft != null) ? latestDraft.getVersion() + 1 : 1; + // 删除旧 draft,保证最新只有一条 + snapshotMapper.deleteBySiteIdAndStatus(siteId, "draft"); SiteSnapshot snapshot = new SiteSnapshot(); snapshot.setSiteId(siteId); - snapshot.setVersion(newVersion); + snapshot.setVersion((latestDraft != null) ? latestDraft.getVersion() + 1 : 1); populateSnapshotFields(snapshot, data); snapshot.setChecksum(checksum); snapshot.setStatus("draft"); @@ -192,7 +194,7 @@ public class SiteSnapshotServiceImpl implements SiteSnapshotService { String bundleJson = toBundleJson(snapshot); jsonCache.put(draftKey(siteId), bundleJson); - log.info("预览版已保存: siteId={}, version={}, checksum={}", siteId, newVersion, checksum.substring(0, 8)); + log.info("预览版已保存: siteId={}, version={}, checksum={}", siteId, snapshot.getVersion(), checksum.substring(0, 8)); return snapshot; } catch (Exception e) { log.error("保存预览版失败 siteId={}", siteId, e); @@ -234,7 +236,8 @@ public class SiteSnapshotServiceImpl implements SiteSnapshotService { return current; } - // 更新状态为 published + // 先删旧 published,再改当前 draft 为 published(事务保证原子性) + snapshotMapper.deleteBySiteIdAndStatus(siteId, "published"); draft.setStatus("published"); draft.setPublishedAt(LocalDateTime.now()); snapshotMapper.updateById(draft);