package com.zy.ai.controller;
|
|
import com.baomidou.mybatisplus.mapper.EntityWrapper;
|
import com.core.annotations.ManagerAuth;
|
import com.core.common.R;
|
import com.zy.ai.entity.LlmRouteConfig;
|
import com.zy.ai.service.LlmRouteConfigService;
|
import com.zy.ai.service.LlmRoutingService;
|
import com.zy.common.web.BaseController;
|
import lombok.RequiredArgsConstructor;
|
import org.springframework.web.bind.annotation.*;
|
|
import java.util.Date;
|
import java.util.List;
|
import java.util.Map;
|
|
@RestController
|
@RequestMapping("/ai/llm/config")
|
@RequiredArgsConstructor
|
public class LlmRouteConfigController extends BaseController {
|
|
private final LlmRouteConfigService llmRouteConfigService;
|
private final LlmRoutingService llmRoutingService;
|
|
@GetMapping("/list/auth")
|
@ManagerAuth
|
public R list() {
|
EntityWrapper<LlmRouteConfig> wrapper = new EntityWrapper<>();
|
wrapper.orderBy("priority", true).orderBy("id", true);
|
List<LlmRouteConfig> list = llmRouteConfigService.selectList(wrapper);
|
return R.ok(list);
|
}
|
|
@PostMapping("/save/auth")
|
@ManagerAuth
|
public R save(@RequestBody LlmRouteConfig config) {
|
if (config == null) {
|
return R.error("参数不能为空");
|
}
|
|
if (isBlank(config.getBaseUrl()) || isBlank(config.getApiKey()) || isBlank(config.getModel())) {
|
return R.error("必须填写 baseUrl/apiKey/model");
|
}
|
|
if (config.getId() == null) {
|
llmRoutingService.fillAndNormalize(config, true);
|
llmRouteConfigService.insert(config);
|
} else {
|
LlmRouteConfig db = llmRouteConfigService.selectById(config.getId());
|
if (db == null) {
|
return R.error("配置不存在");
|
}
|
// 保留统计字段,避免前端误覆盖
|
Integer failCount = db.getFailCount();
|
Integer successCount = db.getSuccessCount();
|
Integer consecutiveFailCount = db.getConsecutiveFailCount();
|
Date lastFailTime = db.getLastFailTime();
|
Date lastUsedTime = db.getLastUsedTime();
|
String lastError = db.getLastError();
|
|
llmRoutingService.fillAndNormalize(config, false);
|
config.setFailCount(failCount);
|
config.setSuccessCount(successCount);
|
config.setConsecutiveFailCount(consecutiveFailCount);
|
config.setLastFailTime(lastFailTime);
|
config.setLastUsedTime(lastUsedTime);
|
config.setLastError(lastError);
|
config.setCreateTime(db.getCreateTime());
|
llmRouteConfigService.updateById(config);
|
}
|
|
llmRoutingService.evictCache();
|
return R.ok(config);
|
}
|
|
@PostMapping("/delete/auth")
|
@ManagerAuth
|
public R delete(@RequestParam("id") Long id) {
|
if (id == null) {
|
return R.error("id不能为空");
|
}
|
llmRouteConfigService.deleteById(id);
|
llmRoutingService.evictCache();
|
return R.ok();
|
}
|
|
@PostMapping("/clearCooldown/auth")
|
@ManagerAuth
|
public R clearCooldown(@RequestParam("id") Long id) {
|
if (id == null) {
|
return R.error("id不能为空");
|
}
|
LlmRouteConfig cfg = llmRouteConfigService.selectById(id);
|
if (cfg == null) {
|
return R.error("配置不存在");
|
}
|
cfg.setCooldownUntil(null);
|
cfg.setConsecutiveFailCount(0);
|
cfg.setUpdateTime(new Date());
|
llmRouteConfigService.updateById(cfg);
|
llmRoutingService.evictCache();
|
return R.ok();
|
}
|
|
@PostMapping("/test/auth")
|
@ManagerAuth
|
public R test(@RequestBody LlmRouteConfig config) {
|
if (config == null) {
|
return R.error("参数不能为空");
|
}
|
if (isBlank(config.getBaseUrl()) || isBlank(config.getApiKey()) || isBlank(config.getModel())) {
|
return R.error("测试失败:必须填写 baseUrl/apiKey/model");
|
}
|
Map<String, Object> data = llmRoutingService.testRoute(config);
|
if (Boolean.TRUE.equals(data.get("ok")) && config.getId() != null) {
|
LlmRouteConfig db = llmRouteConfigService.selectById(config.getId());
|
if (db != null) {
|
db.setCooldownUntil(null);
|
db.setConsecutiveFailCount(0);
|
db.setUpdateTime(new Date());
|
llmRouteConfigService.updateById(db);
|
llmRoutingService.evictCache();
|
}
|
}
|
return R.ok(data);
|
}
|
|
private boolean isBlank(String s) {
|
return s == null || s.trim().isEmpty();
|
}
|
}
|