refactor: 版本号由PAR审核通过时自动分配
- submitConfig: 不再比较版本号,相同hash跳过,不同hash都插入pending - approveConfig: 自动取最大版本号+1 分配(如 1, 2, 3...) - 存储路径含hash前缀避免同名冲突
This commit is contained in:
@@ -71,6 +71,19 @@ public class AdminController {
|
|||||||
|
|
||||||
Long reviewerId = (Long) request.getAttribute("accountId");
|
Long reviewerId = (Long) request.getAttribute("accountId");
|
||||||
|
|
||||||
|
// 自动分配版本号:取该站点已审批的最大版本号 + 1
|
||||||
|
var latestApproved = siteConfigMapper.selectList(
|
||||||
|
new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<SiteConfig>()
|
||||||
|
.eq(SiteConfig::getSiteId, config.getSiteId())
|
||||||
|
.eq(SiteConfig::getStatus, ConfigStatus.APPROVED)
|
||||||
|
.orderByDesc(SiteConfig::getVersion));
|
||||||
|
int nextVersion = 1;
|
||||||
|
if (!latestApproved.isEmpty()) {
|
||||||
|
try { nextVersion = Integer.parseInt(latestApproved.get(0).getVersion()) + 1; }
|
||||||
|
catch (NumberFormatException ignored) {}
|
||||||
|
}
|
||||||
|
config.setVersion(String.valueOf(nextVersion));
|
||||||
|
|
||||||
config.setStatus(ConfigStatus.APPROVED);
|
config.setStatus(ConfigStatus.APPROVED);
|
||||||
config.setReviewerId(reviewerId);
|
config.setReviewerId(reviewerId);
|
||||||
config.setReviewedAt(LocalDateTime.now());
|
config.setReviewedAt(LocalDateTime.now());
|
||||||
|
|||||||
@@ -59,28 +59,21 @@ public class ConfigServiceImpl implements ConfigService {
|
|||||||
@Override
|
@Override
|
||||||
public SiteConfig submitConfig(String siteId, String version, String configJson,
|
public SiteConfig submitConfig(String siteId, String version, String configJson,
|
||||||
String schemaVersion, Long submitterId) {
|
String schemaVersion, Long submitterId) {
|
||||||
SiteConfig existing = getConfig(siteId, version);
|
|
||||||
String configHash = HmacUtil.sha256(configJson);
|
String configHash = HmacUtil.sha256(configJson);
|
||||||
String storagePath = String.format("configs/%s/%s.json", siteId, version);
|
String storagePath = String.format("configs/%s/%s/%s.json", siteId, version, configHash.substring(0, 8));
|
||||||
|
|
||||||
// 版本已存在 → 根据 hash 判断是重复还是更新
|
// 相同 hash 的提交 → 真正重复,跳过
|
||||||
if (existing != null) {
|
SiteConfig sameHash = siteConfigMapper.selectOne(
|
||||||
if (configHash.equals(existing.getConfigHash())) {
|
new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<SiteConfig>()
|
||||||
log.info("配置未变化,跳过: siteId={}, version={}, hash={}", siteId, version, configHash);
|
.eq(SiteConfig::getSiteId, siteId)
|
||||||
return existing;
|
.eq(SiteConfig::getConfigHash, configHash)
|
||||||
}
|
.eq(SiteConfig::getDeleted, 0));
|
||||||
// hash 不同 → 覆盖更新
|
if (sameHash != null) {
|
||||||
log.info("配置内容变化,更新: siteId={}, version={}, oldHash={}, newHash={}",
|
log.info("配置未变化,跳过: siteId={}, hash={}", siteId, configHash);
|
||||||
siteId, version, existing.getConfigHash(), configHash);
|
return sameHash;
|
||||||
storageService.store(storagePath, configJson);
|
|
||||||
existing.setConfigHash(configHash);
|
|
||||||
existing.setFileSize((long) configJson.getBytes(StandardCharsets.UTF_8).length);
|
|
||||||
existing.setStoragePath(storagePath);
|
|
||||||
siteConfigMapper.updateById(existing);
|
|
||||||
return existing;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 存储到抽象存储层(七牛或本地)
|
// 存储到抽象存储层
|
||||||
try {
|
try {
|
||||||
storageService.store(storagePath, configJson);
|
storageService.store(storagePath, configJson);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -90,7 +83,7 @@ public class ConfigServiceImpl implements ConfigService {
|
|||||||
|
|
||||||
SiteConfig config = new SiteConfig();
|
SiteConfig config = new SiteConfig();
|
||||||
config.setSiteId(siteId);
|
config.setSiteId(siteId);
|
||||||
config.setVersion(version);
|
config.setVersion(version); // 客户端提交的版本号,仅供参考
|
||||||
config.setConfigHash(configHash);
|
config.setConfigHash(configHash);
|
||||||
config.setStoragePath(storagePath);
|
config.setStoragePath(storagePath);
|
||||||
config.setFileSize((long) configJson.getBytes(StandardCharsets.UTF_8).length);
|
config.setFileSize((long) configJson.getBytes(StandardCharsets.UTF_8).length);
|
||||||
@@ -100,7 +93,7 @@ public class ConfigServiceImpl implements ConfigService {
|
|||||||
config.setIsLatest(false);
|
config.setIsLatest(false);
|
||||||
|
|
||||||
siteConfigMapper.insert(config);
|
siteConfigMapper.insert(config);
|
||||||
log.info("Config submitted: siteId={}, version={}, id={}", siteId, version, config.getId());
|
log.info("Config submitted: siteId={}, version={}, id={}, hash={}", siteId, version, config.getId(), configHash);
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user