fix: 保存/发布快照时清理同状态旧记录,每个站点每种状态最多保留一条

This commit is contained in:
mediabot-pt
2026-07-07 13:23:13 +08:00
parent e953e9a7fd
commit 2c126a6ec5

View File

@@ -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);