diff --git a/src/main/java/com/labelsys/backend/service/SysConfigService.java b/src/main/java/com/labelsys/backend/service/SysConfigService.java index 4a399f2..4b01af7 100644 --- a/src/main/java/com/labelsys/backend/service/SysConfigService.java +++ b/src/main/java/com/labelsys/backend/service/SysConfigService.java @@ -1,6 +1,7 @@ package com.labelsys.backend.service; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; @@ -311,17 +312,8 @@ public class SysConfigService { private void syncUpdateAgentConfigs(Long companyId, Long configId, SysConfig config) { try { if (ConfigType.MODEL.name().equalsIgnoreCase(config.getConfigType())) { - // 更新所有使用该模型配置的agent配置 - LambdaQueryWrapper modelWrapper = - new LambdaQueryWrapper<>(); - modelWrapper.eq(AnnotationAgentConfig::getCompanyId, companyId) - .eq(AnnotationAgentConfig::getModelConfigId, configId); - - List modelAgentConfigs = - agentConfigMapper.selectList(modelWrapper); - - // 先解密获取原始API密钥,再通过setModelApiKey加密存储 - String decryptedApiKey = null; + // 从配置值中提取模型URL、加密的API密钥和LLM类型 + String encryptedApiKey = null; String modelUrl = null; String llmType = null; try { @@ -330,42 +322,37 @@ public class SysConfigService { if (llmConfig != null) { modelUrl = llmConfig.getModelUrl(); llmType = llmConfig.getLlmType(); - // 解密API密钥,setModelApiKey会再次加密 + // 直接获取加密后的API密钥,不进行解密 if (llmConfig.getApiKey() != null) { - decryptedApiKey = SM4Util.decryptSafe(llmConfig.getApiKey()); + encryptedApiKey = llmConfig.getApiKey(); } } } catch (Exception e) { log.warn("解析模型配置值失败,configId={}, error={}", configId, e.getMessage()); } - for (AnnotationAgentConfig agentConfig : modelAgentConfigs) { - agentConfig.setModelName(config.getConfigName()); - agentConfig.setModelUrl(modelUrl); - agentConfig.setModelApiKey(decryptedApiKey); // setModelApiKey会加密存储 - agentConfig.setLlmType(llmType); - agentConfig.setCreatedAt(LocalDateTime.now()); - agentConfigMapper.updateById(agentConfig); - } + // 使用UpdateWrapper批量更新,直接存储加密后的API密钥 + UpdateWrapper updateWrapper = new UpdateWrapper<>(); + updateWrapper.eq("company_id", companyId) + .eq("model_config_id", configId) + .set("model_name", config.getConfigName()) + .set("model_url", modelUrl) + .set("model_api_key", encryptedApiKey) // 直接设置加密后的API密钥 + .set("llm_type", llmType) + .set("created_at", LocalDateTime.now()); - log.info("sync updated {} agent configs with modelConfigId={}", modelAgentConfigs.size(), configId); + int updatedCount = agentConfigMapper.update(null, updateWrapper); + log.info("sync updated {} agent configs with modelConfigId={}", updatedCount, configId); } else if (ConfigType.PROMPT.name().equalsIgnoreCase(config.getConfigType())) { // 更新所有使用该提示词配置的agent配置 - LambdaQueryWrapper promptWrapper = - new LambdaQueryWrapper<>(); - promptWrapper.eq(AnnotationAgentConfig::getCompanyId, companyId) - .eq(AnnotationAgentConfig::getPromptConfigId, configId); + UpdateWrapper updateWrapper = new UpdateWrapper<>(); + updateWrapper.eq("company_id", companyId) + .eq("prompt_config_id", configId) + .set("prompt_text", config.getConfigValue()) + .set("created_at", LocalDateTime.now()); - List promptAgentConfigs = - agentConfigMapper.selectList(promptWrapper); - - for (AnnotationAgentConfig agentConfig : promptAgentConfigs) { - agentConfig.setPromptText(config.getConfigValue()); - agentConfig.setCreatedAt(LocalDateTime.now()); - agentConfigMapper.updateById(agentConfig); - } - - log.info("sync updated {} agent configs with promptConfigId={}", promptAgentConfigs.size(), configId); + int updatedCount = agentConfigMapper.update(null, updateWrapper); + log.info("sync updated {} agent configs with promptConfigId={}", updatedCount, configId); } } catch (Exception e) { log.error("syncUpdateAgentConfigs failed, configId={}, error={}", configId, e.getMessage(), e);