-- 创建 RagflowChunk记录表
CREATE TABLE IF NOT EXISTS ragflow_chunk_record (
    id BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '主键ID',
	database_name VARCHAR(64) NOT NULL COMMENT '数据库ID',
	table_name VARCHAR(255) NOT NULL COMMENT '数据表ID',
	chunk_id VARCHAR(64) NOT NULL COMMENT '分块ID',
    cond VARCHAR(100) COMMENT '条件参数',
    update_data JSON COMMENT '数据参数',
    scheduled_time DATETIME NOT NULL COMMENT '计划执行时间（当前时间+20秒）',
    status VARCHAR(20) NOT NULL DEFAULT '0' COMMENT '执行状态：0:未执行/1:执行成功/2:执行失败',
	error_message VARCHAR(255) COMMENT '失败信息',
	executed_time DATETIME COMMENT '执行时间',
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    INDEX idx_dataset_name (database_name),
    INDEX idx_table_name (table_name),
    INDEX idx_chunk_id (chunk_id),
    INDEX idx_scheduled_time (scheduled_time),
    INDEX idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='RagflowChunk记录表';

-- 创建 维度知识库关联表
CREATE TABLE IF NOT EXISTS dimensional_knowledge (
    id INT AUTO_INCREMENT PRIMARY KEY,
    dimensional_id VARCHAR(64) NOT NULL UNIQUE COMMENT "维度id",
    dimensional_name  VARCHAR(255) NOT NULL COMMENT "维度名称",
    knowledge_id VARCHAR(64) NOT NULL UNIQUE COMMENT "知识库id",
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    is_active BOOLEAN DEFAULT TRUE COMMENT "是否有效",
    INDEX idx_dimensional_id (dimensional_id),
    INDEX idx_knowledge_id (knowledge_id),
    INDEX idx_is_active (is_active)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 创建 API 密钥表
CREATE TABLE IF NOT EXISTS api_keys (
    id INT AUTO_INCREMENT PRIMARY KEY COMMENT "主键ID",
    api_key VARCHAR(255) NOT NULL UNIQUE COMMENT "API密钥",
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT "创建时间",
    expired_at DATETIME NULL COMMENT "过期时间",
    is_active BOOLEAN DEFAULT TRUE COMMENT "是否有效",
    INDEX idx_api_key (api_key),
    INDEX idx_is_active (is_active)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 插入一个示例 API 密钥（仅供测试使用）
INSERT IGNORE INTO api_keys (api_key, is_active) VALUES ('sk-test1234567890', TRUE);