CREATE TABLE IF NOT EXISTS `sys_llm_route` (
  `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键',
  `name` VARCHAR(64) NOT NULL COMMENT '路由名称',
  `base_url` VARCHAR(255) DEFAULT NULL COMMENT 'LLM API Base URL',
  `api_key` VARCHAR(512) DEFAULT NULL COMMENT 'API Key',
  `model` VARCHAR(128) DEFAULT NULL COMMENT '模型名',
  `thinking` TINYINT NOT NULL DEFAULT 0 COMMENT '是否开启深度思考:1是0否',
  `priority` INT NOT NULL DEFAULT 100 COMMENT '优先级(越小越优先)',
  `status` TINYINT NOT NULL DEFAULT 1 COMMENT '状态:1启用0禁用',
  `switch_on_quota` TINYINT NOT NULL DEFAULT 1 COMMENT '额度耗尽时是否切换',
  `switch_on_error` TINYINT NOT NULL DEFAULT 1 COMMENT '故障时是否切换',
  `cooldown_seconds` INT NOT NULL DEFAULT 300 COMMENT '故障后冷却秒数',
  `cooldown_until` DATETIME DEFAULT NULL COMMENT '冷却截止时间',
  `fail_count` INT NOT NULL DEFAULT 0 COMMENT '总失败次数',
  `success_count` INT NOT NULL DEFAULT 0 COMMENT '总成功次数',
  `consecutive_fail_count` INT NOT NULL DEFAULT 0 COMMENT '连续失败次数',
  `last_error` VARCHAR(512) DEFAULT NULL COMMENT '最近错误摘要',
  `last_used_time` DATETIME DEFAULT NULL COMMENT '最近成功调用时间',
  `last_fail_time` DATETIME DEFAULT NULL COMMENT '最近失败时间',
  `create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `memo` VARCHAR(255) DEFAULT NULL COMMENT '备注',
  PRIMARY KEY (`id`),
  KEY `idx_sys_llm_route_status_priority` (`status`, `priority`),
  KEY `idx_sys_llm_route_cooldown` (`cooldown_until`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='LLM 路由配置(多API/多模型/多Key自动切换)';

-- 示例数据（按需修改后执行）
-- INSERT INTO sys_llm_route(name, base_url, api_key, model, thinking, priority, status, switch_on_quota, switch_on_error, cooldown_seconds)
-- VALUES ('主路由-gpt5', 'https://api.xiaomimimo.com/v1', 'sk-xxxx', 'gpt-5', 1, 10, 1, 1, 1, 300);
-- INSERT INTO sys_llm_route(name, base_url, api_key, model, thinking, priority, status, switch_on_quota, switch_on_error, cooldown_seconds)
-- VALUES ('备路由-mimo', 'https://api.xiaomimimo.com/v1', 'sk-yyyy', 'mimo-v2-flash', 0, 20, 1, 1, 1, 300);
