feat: 修改 saveParams 方法支持首次保存自动复制默认参数

This commit is contained in:
wkc
2026-03-06 15:25:41 +08:00
parent 7dba7845cc
commit c09cd77723

View File

@@ -105,51 +105,65 @@ public class CcdiModelParamServiceImpl implements ICcdiModelParamService {
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void saveParams(ModelParamSaveDTO saveDTO) { public void saveParams(ModelParamSaveDTO saveDTO) {
Long projectId = saveDTO.getProjectId(); try {
if (projectId == null) { // 1. 参数验证
projectId = 0L; if (saveDTO.getProjectId() == null) {
} throw new ServiceException("项目ID不能为空");
}
// 空列表校验 if (StringUtils.isBlank(saveDTO.getModelCode())) {
if (saveDTO.getParams() == null || saveDTO.getParams().isEmpty()) { throw new ServiceException("模型编码不能为空");
throw new ServiceException("参数列表不能为空"); }
} if (saveDTO.getParams() == null || saveDTO.getParams().isEmpty()) {
throw new ServiceException("参数列表不能为空");
String username = SecurityUtils.getUsername();
Date now = new Date();
// 查询现有参数
List<CcdiModelParam> existingParams = modelParamMapper.selectByProjectAndModel(
projectId,
saveDTO.getModelCode()
);
if (existingParams.isEmpty()) {
throw new ServiceException("未找到模型参数配置");
}
// 构建Map提升性能
Map<String, CcdiModelParam> existingMap = existingParams.stream()
.collect(Collectors.toMap(CcdiModelParam::getParamCode, p -> p));
// 准备更新列表 - 只更新 param_value 字段
List<CcdiModelParam> updateList = new ArrayList<>();
for (ModelParamSaveDTO.ParamValueItem item : saveDTO.getParams()) {
CcdiModelParam existing = existingMap.get(item.getParamCode());
if (existing != null) {
// ⚠️ 关键:只修改 param_value 字段
CcdiModelParam updateParam = new CcdiModelParam();
updateParam.setId(existing.getId());
updateParam.setParamValue(item.getParamValue()); // 只更新阈值
updateParam.setUpdateBy(username);
updateParam.setUpdateTime(now);
updateList.add(updateParam);
} }
}
if (!updateList.isEmpty()) { Long projectId = saveDTO.getProjectId();
modelParamMapper.batchUpdateParamValues(updateList);
// 2. 如果是项目保存projectId > 0需要检查是否首次保存
if (projectId > 0) {
// 查询项目信息
CcdiProject project = projectMapper.selectById(projectId);
if (project == null) {
throw new ServiceException("项目不存在");
}
// 3. 如果是首次保存configType=default需要复制系统默认参数
if ("default".equals(project.getConfigType())) {
int copiedCount = copyDefaultParamsToProject(projectId, saveDTO.getModelCode());
if (copiedCount == 0) {
log.warn("系统默认参数为空projectId={}, modelCode={}",
projectId, saveDTO.getModelCode());
}
// 更新项目配置类型为 custom
project.setConfigType("custom");
projectMapper.updateById(project);
log.info("项目配置类型已更新为 customprojectId={}", projectId);
}
}
// 4. 更新参数值
String username = SecurityUtils.getUsername();
for (ModelParamSaveDTO.ParamValueItem item : saveDTO.getParams()) {
int updated = modelParamMapper.updateParamValue(
projectId,
saveDTO.getModelCode(),
item.getParamCode(),
item.getParamValue()
);
if (updated == 0) {
log.warn("参数不存在或未更新paramCode={}", item.getParamCode());
}
}
} catch (ServiceException e) {
// 业务异常,直接抛出
throw e;
} catch (Exception e) {
// 系统异常,记录日志并抛出
log.error("保存模型参数失败", e);
throw new ServiceException("保存模型参数失败:" + e.getMessage());
} }
} }