diff --git a/src/main/java/com/labelsys/backend/service/SysConfigService.java b/src/main/java/com/labelsys/backend/service/SysConfigService.java index 80baddc..7717cbb 100644 --- a/src/main/java/com/labelsys/backend/service/SysConfigService.java +++ b/src/main/java/com/labelsys/backend/service/SysConfigService.java @@ -77,18 +77,27 @@ public class SysConfigService { @Transactional public SysConfig updateConfig(LoginUser currentUser, Long configId, UpdateSysConfigRequest request) { try { - validateConfigType(request.configType()); + // 1. 检查配置是否存在 SysConfig existing = getConfigEntity(currentUser, configId); - if (StringUtils.hasText(request.configName())) { + // 2. 如果要更新configName,需要校验新名称是否与同一公司下其他配置冲突 + if (StringUtils.hasText(request.configName()) && !request.configName().equals(existing.getConfigName())) { + // 检查新名称是否已被同一公司的其他配置使用 + SysConfig duplicate = sysConfigMapper.findByCompanyIdAndConfigName(currentUser.companyId(), + request.configName()); + if (duplicate != null && !duplicate.getId().equals(configId)) { + throw new BusinessException(ResultCode.CONFLICT, "配置名称已存在"); + } existing.setConfigName(request.configName()); } + if (StringUtils.hasText(request.configType())) { + validateConfigType(request.configType()); existing.setConfigType(request.configType()); } if (StringUtils.hasText(request.configValue())) { // 如果是model类型,对apiKey进行加密处理 - if (ConfigType.MODEL.name().equalsIgnoreCase(request.configType())) { + if (ConfigType.MODEL.name().equalsIgnoreCase(existing.getConfigType())) { existing.setConfigValue(processModelConfigValue(request.configValue(), true)); } else { existing.setConfigValue(request.configValue()); @@ -146,6 +155,35 @@ public class SysConfigService { } public SysConfigResponse toResponse(SysConfig config) { + // 如果是模型配置,需要解密API密钥 + if (ConfigType.MODEL.name().equalsIgnoreCase(config.getConfigType()) && config.getConfigValue() != null) { + try { + ObjectMapper objectMapper = new ObjectMapper(); + LlmConfigModel model = objectMapper.readValue(config.getConfigValue(), LlmConfigModel.class); + if (model != null && model.getApiKey() != null) { + // 解密API密钥 + String decryptedApiKey = SM4Util.decryptSafe(model.getApiKey()); + model.setApiKey(decryptedApiKey); + // 更新配置值 + String updatedConfigValue = objectMapper.writeValueAsString(model); + config = SysConfig.builder() + .id(config.getId()) + .companyId(config.getCompanyId()) + .configType(config.getConfigType()) + .configName(config.getConfigName()) + .configValue(updatedConfigValue) + .status(config.getStatus()) + .creatorId(config.getCreatorId()) + .creatorRole(config.getCreatorRole()) + .createdAt(config.getCreatedAt()) + .updatedAt(config.getUpdatedAt()) + .build(); + } + } catch (JsonProcessingException e) { + log.error("解密模型配置API密钥失败: {}", e.getMessage(), e); + } + } + return new SysConfigResponse( config.getId(), config.getConfigType(),