feat: 修改 saveParams 方法支持首次保存自动复制默认参数
This commit is contained in:
@@ -105,51 +105,65 @@ public class CcdiModelParamServiceImpl implements ICcdiModelParamService {
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void saveParams(ModelParamSaveDTO saveDTO) {
|
||||
Long projectId = saveDTO.getProjectId();
|
||||
if (projectId == null) {
|
||||
projectId = 0L;
|
||||
try {
|
||||
// 1. 参数验证
|
||||
if (saveDTO.getProjectId() == null) {
|
||||
throw new ServiceException("项目ID不能为空");
|
||||
}
|
||||
if (StringUtils.isBlank(saveDTO.getModelCode())) {
|
||||
throw new ServiceException("模型编码不能为空");
|
||||
}
|
||||
|
||||
// 空列表校验
|
||||
if (saveDTO.getParams() == null || saveDTO.getParams().isEmpty()) {
|
||||
throw new ServiceException("参数列表不能为空");
|
||||
}
|
||||
|
||||
Long projectId = saveDTO.getProjectId();
|
||||
|
||||
// 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("项目配置类型已更新为 custom,projectId={}", projectId);
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 更新参数值
|
||||
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);
|
||||
int updated = modelParamMapper.updateParamValue(
|
||||
projectId,
|
||||
saveDTO.getModelCode(),
|
||||
item.getParamCode(),
|
||||
item.getParamValue()
|
||||
);
|
||||
if (updated == 0) {
|
||||
log.warn("参数不存在或未更新,paramCode={}", item.getParamCode());
|
||||
}
|
||||
}
|
||||
|
||||
if (!updateList.isEmpty()) {
|
||||
modelParamMapper.batchUpdateParamValues(updateList);
|
||||
} catch (ServiceException e) {
|
||||
// 业务异常,直接抛出
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
// 系统异常,记录日志并抛出
|
||||
log.error("保存模型参数失败", e);
|
||||
throw new ServiceException("保存模型参数失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user