-- 模型服务商集合刷新脚本
-- 用途：
-- 1. 清理已下线的旧服务商及其关联模型（星火、火山、千帆、Gitee）
-- 2. 对保留服务商做默认路径对齐
-- 3. 补齐新的服务商预设（智谱、MiniMax、Kimi、自部署）

START TRANSACTION;

DELETE m
FROM tb_model AS m
INNER JOIN tb_model_provider AS p ON p.id = m.provider_id
WHERE p.provider_type IN ('gitee', 'baidu', 'volcengine', 'spark');

DELETE FROM tb_model_provider
WHERE provider_type IN ('gitee', 'baidu', 'volcengine', 'spark');

UPDATE tb_model_provider
SET provider_name = 'DeepSeek',
    endpoint = 'https://api.deepseek.com',
    chat_path = '/chat/completions',
    embed_path = '',
    rerank_path = ''
WHERE provider_type = 'deepseek';

UPDATE tb_model_provider
SET provider_name = 'OpenAI',
    endpoint = 'https://api.openai.com',
    chat_path = '/v1/chat/completions',
    embed_path = '/v1/embeddings',
    rerank_path = ''
WHERE provider_type = 'openai';

UPDATE tb_model_provider
SET provider_name = '阿里百炼',
    endpoint = 'https://dashscope.aliyuncs.com',
    chat_path = '/compatible-mode/v1/chat/completions',
    embed_path = '/compatible-mode/v1/embeddings',
    rerank_path = '/api/v1/services/rerank/text-rerank/text-rerank'
WHERE provider_type = 'aliyun';

UPDATE tb_model_provider
SET provider_name = '硅基流动',
    endpoint = 'https://api.siliconflow.cn',
    chat_path = '/v1/chat/completions',
    embed_path = '/v1/embeddings',
    rerank_path = '/v1/rerank'
WHERE provider_type = 'siliconlow';

UPDATE tb_model_provider
SET provider_name = 'Ollama',
    endpoint = 'http://127.0.0.1:11434',
    chat_path = '/v1/chat/completions',
    embed_path = '/api/embed',
    rerank_path = ''
WHERE provider_type = 'ollama';

INSERT INTO tb_model_provider (`id`, `provider_name`, `provider_type`, `icon`, `api_key`, `endpoint`, `chat_path`, `embed_path`, `rerank_path`, `created`, `created_by`, `modified`, `modified_by`)
SELECT 366100000000000001, '智谱', 'zhipu', '', '', 'https://open.bigmodel.cn', '/api/paas/v4/chat/completions', '/api/paas/v4/embeddings', '', NOW(), 1, NOW(), 1
WHERE NOT EXISTS (
    SELECT 1 FROM tb_model_provider WHERE provider_type = 'zhipu'
);

INSERT INTO tb_model_provider (`id`, `provider_name`, `provider_type`, `icon`, `api_key`, `endpoint`, `chat_path`, `embed_path`, `rerank_path`, `created`, `created_by`, `modified`, `modified_by`)
SELECT 366100000000000002, 'MiniMax', 'minimax', '', '', 'https://api.minimax.io', '/v1/chat/completions', '', '', NOW(), 1, NOW(), 1
WHERE NOT EXISTS (
    SELECT 1 FROM tb_model_provider WHERE provider_type = 'minimax'
);

INSERT INTO tb_model_provider (`id`, `provider_name`, `provider_type`, `icon`, `api_key`, `endpoint`, `chat_path`, `embed_path`, `rerank_path`, `created`, `created_by`, `modified`, `modified_by`)
SELECT 366100000000000003, 'Kimi', 'kimi', '', '', 'https://api.moonshot.cn', '/v1/chat/completions', '', '', NOW(), 1, NOW(), 1
WHERE NOT EXISTS (
    SELECT 1 FROM tb_model_provider WHERE provider_type = 'kimi'
);

INSERT INTO tb_model_provider (`id`, `provider_name`, `provider_type`, `icon`, `api_key`, `endpoint`, `chat_path`, `embed_path`, `rerank_path`, `created`, `created_by`, `modified`, `modified_by`)
SELECT 366100000000000004, '自部署', 'self-hosted', '', '', 'http://127.0.0.1:8000', '/v1/chat/completions', '/v1/embeddings', '/v1/score', NOW(), 1, NOW(), 1
WHERE NOT EXISTS (
    SELECT 1 FROM tb_model_provider WHERE provider_type = 'self-hosted'
);

COMMIT;
