Merge branch 'dev54'
# Conflicts: # src/main/java/com/labelsys/backend/service/SysConfigService.java
This commit is contained in:
@@ -4,6 +4,8 @@ import io.swagger.v3.oas.annotations.media.Schema;
|
|||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
@Schema(description = "Agent配置信息响应")
|
@Schema(description = "Agent配置信息响应")
|
||||||
@Data
|
@Data
|
||||||
@Builder
|
@Builder
|
||||||
@@ -22,7 +24,7 @@ public class AgentConfigInfoResponse {
|
|||||||
private PromptConfigInfo promptConfig;
|
private PromptConfigInfo promptConfig;
|
||||||
|
|
||||||
@Schema(description = "创建时间")
|
@Schema(description = "创建时间")
|
||||||
private java.time.LocalDateTime createdAt;
|
private LocalDateTime createdAt;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@Builder
|
@Builder
|
||||||
|
|||||||
@@ -13,8 +13,10 @@ import com.labelsys.backend.dto.request.SaveSysConfigRequest;
|
|||||||
import com.labelsys.backend.dto.request.SysConfigPageQuery;
|
import com.labelsys.backend.dto.request.SysConfigPageQuery;
|
||||||
import com.labelsys.backend.dto.request.UpdateSysConfigRequest;
|
import com.labelsys.backend.dto.request.UpdateSysConfigRequest;
|
||||||
import com.labelsys.backend.dto.response.SysConfigResponse;
|
import com.labelsys.backend.dto.response.SysConfigResponse;
|
||||||
|
import com.labelsys.backend.entity.AnnotationAgentConfig;
|
||||||
import com.labelsys.backend.entity.SysConfig;
|
import com.labelsys.backend.entity.SysConfig;
|
||||||
import com.labelsys.backend.enums.ConfigType;
|
import com.labelsys.backend.enums.ConfigType;
|
||||||
|
import com.labelsys.backend.mapper.AnnotationAgentConfigMapper;
|
||||||
import com.labelsys.backend.mapper.SysConfigMapper;
|
import com.labelsys.backend.mapper.SysConfigMapper;
|
||||||
import com.labelsys.backend.util.IdGenerator;
|
import com.labelsys.backend.util.IdGenerator;
|
||||||
import com.labelsys.backend.util.SM4Util;
|
import com.labelsys.backend.util.SM4Util;
|
||||||
@@ -24,6 +26,7 @@ import org.springframework.stereotype.Service;
|
|||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@@ -31,7 +34,8 @@ import java.util.List;
|
|||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class SysConfigService {
|
public class SysConfigService {
|
||||||
|
|
||||||
private final SysConfigMapper sysConfigMapper;
|
private final SysConfigMapper sysConfigMapper;
|
||||||
|
private final AnnotationAgentConfigMapper agentConfigMapper;
|
||||||
//private final DataPermissionService dataPermissionService;
|
//private final DataPermissionService dataPermissionService;
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
@@ -80,6 +84,9 @@ public class SysConfigService {
|
|||||||
// 1. 检查配置是否存在
|
// 1. 检查配置是否存在
|
||||||
SysConfig existing = getConfigEntity(currentUser, configId);
|
SysConfig existing = getConfigEntity(currentUser, configId);
|
||||||
|
|
||||||
|
// 记录原始配置类型,用于后续判断是否需要同步更新agentconfigs
|
||||||
|
String originalConfigType = existing.getConfigType();
|
||||||
|
|
||||||
// 2. 如果要更新configName,需要校验新名称是否与同一公司下其他配置冲突
|
// 2. 如果要更新configName,需要校验新名称是否与同一公司下其他配置冲突
|
||||||
if (StringUtils.hasText(request.configName()) && !request.configName().equals(existing.getConfigName())) {
|
if (StringUtils.hasText(request.configName()) && !request.configName().equals(existing.getConfigName())) {
|
||||||
// 检查新名称是否已被同一公司的其他配置使用
|
// 检查新名称是否已被同一公司的其他配置使用
|
||||||
@@ -109,6 +116,10 @@ public class SysConfigService {
|
|||||||
sysConfigMapper.updateById(existing);
|
sysConfigMapper.updateById(existing);
|
||||||
log.info("updated sys config, companyId={}, userId={}, configId={}",
|
log.info("updated sys config, companyId={}, userId={}, configId={}",
|
||||||
currentUser.companyId(), currentUser.userId(), configId);
|
currentUser.companyId(), currentUser.userId(), configId);
|
||||||
|
|
||||||
|
// 3. 同步更新annotation_agent_configs表中对应的配置
|
||||||
|
syncUpdateAgentConfigs(currentUser.companyId(), configId, existing);
|
||||||
|
|
||||||
return existing;
|
return existing;
|
||||||
} catch (BusinessException e) {
|
} catch (BusinessException e) {
|
||||||
throw e;
|
throw e;
|
||||||
@@ -289,4 +300,76 @@ public class SysConfigService {
|
|||||||
|
|
||||||
return sysConfigMapper.selectList(wrapper);
|
return sysConfigMapper.selectList(wrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 同步更新annotation_agent_configs表中对应的配置
|
||||||
|
*
|
||||||
|
* @param companyId 公司ID
|
||||||
|
* @param configId 系统配置ID
|
||||||
|
* @param config 更新后的系统配置
|
||||||
|
*/
|
||||||
|
private void syncUpdateAgentConfigs(Long companyId, Long configId, SysConfig config) {
|
||||||
|
try {
|
||||||
|
if (ConfigType.MODEL.name().equalsIgnoreCase(config.getConfigType())) {
|
||||||
|
// 更新所有使用该模型配置的agent配置
|
||||||
|
LambdaQueryWrapper<AnnotationAgentConfig> modelWrapper =
|
||||||
|
new LambdaQueryWrapper<>();
|
||||||
|
modelWrapper.eq(AnnotationAgentConfig::getCompanyId, companyId)
|
||||||
|
.eq(AnnotationAgentConfig::getModelConfigId, configId);
|
||||||
|
|
||||||
|
List<AnnotationAgentConfig> modelAgentConfigs =
|
||||||
|
agentConfigMapper.selectList(modelWrapper);
|
||||||
|
|
||||||
|
// 先解密获取原始API密钥,再通过setModelApiKey加密存储
|
||||||
|
String decryptedApiKey = null;
|
||||||
|
String modelUrl = null;
|
||||||
|
String llmType = null;
|
||||||
|
try {
|
||||||
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
LlmConfigModel llmConfig = objectMapper.readValue(config.getConfigValue(), LlmConfigModel.class);
|
||||||
|
if (llmConfig != null) {
|
||||||
|
modelUrl = llmConfig.getModelUrl();
|
||||||
|
llmType = llmConfig.getLlmType();
|
||||||
|
// 解密API密钥,setModelApiKey会再次加密
|
||||||
|
if (llmConfig.getApiKey() != null) {
|
||||||
|
decryptedApiKey = SM4Util.decryptSafe(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);
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info("sync updated {} agent configs with modelConfigId={}", modelAgentConfigs.size(), configId);
|
||||||
|
} else if (ConfigType.PROMPT.name().equalsIgnoreCase(config.getConfigType())) {
|
||||||
|
// 更新所有使用该提示词配置的agent配置
|
||||||
|
LambdaQueryWrapper<AnnotationAgentConfig> promptWrapper =
|
||||||
|
new LambdaQueryWrapper<>();
|
||||||
|
promptWrapper.eq(AnnotationAgentConfig::getCompanyId, companyId)
|
||||||
|
.eq(AnnotationAgentConfig::getPromptConfigId, configId);
|
||||||
|
|
||||||
|
List<AnnotationAgentConfig> 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);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("syncUpdateAgentConfigs failed, configId={}, error={}", configId, e.getMessage(), e);
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user