-- AI智能回答系统数据库初始化脚本
-- 创建日期: 2025-08-13
-- 描述: AI智能回答系统相关表结构定义

-- ============================================================================
-- 1. AI配置表 (ai_config)
-- 用于存储LLM提供商配置信息
-- ============================================================================
CREATE TABLE ai_config (
    id BIGSERIAL PRIMARY KEY,
    provider_name VARCHAR(50) NOT NULL,
    config_key VARCHAR(100) NOT NULL,
    config_value TEXT,
    enabled BOOLEAN DEFAULT TRUE,
    created_by VARCHAR(100),
    created_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_by VARCHAR(100),
    updated_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    is_deleted BOOLEAN DEFAULT FALSE
);

-- 添加列注释
COMMENT ON COLUMN ai_config.id IS '主键ID';
COMMENT ON COLUMN ai_config.provider_name IS 'LLM提供商名称(openai, claude, qwen等)';
COMMENT ON COLUMN ai_config.config_key IS '配置键';
COMMENT ON COLUMN ai_config.config_value IS '配置值(加密存储)';
COMMENT ON COLUMN ai_config.enabled IS '是否启用';
COMMENT ON COLUMN ai_config.created_by IS '创建人';
COMMENT ON COLUMN ai_config.created_time IS '创建时间';
COMMENT ON COLUMN ai_config.updated_by IS '更新人';
COMMENT ON COLUMN ai_config.updated_time IS '更新时间';
COMMENT ON COLUMN ai_config.is_deleted IS '是否删除';

-- 创建索引
CREATE INDEX idx_ai_config_provider ON ai_config(provider_name);
CREATE INDEX idx_ai_config_key ON ai_config(config_key);
CREATE INDEX idx_ai_config_enabled ON ai_config(enabled);

-- ============================================================================
-- 2. AI回答历史表 (ai_answer_history)
-- 用于存储AI生成的回答历史记录和相关信息
-- ============================================================================
CREATE TABLE ai_answer_history (
    id BIGSERIAL PRIMARY KEY,
    answer_id VARCHAR(100) UNIQUE NOT NULL,
    repair_id VARCHAR(100),
    question TEXT NOT NULL,
    answer TEXT NOT NULL,
    provider_used VARCHAR(50),
    model_used VARCHAR(100),
    confidence_score DECIMAL(5,4),
    processing_time_ms BIGINT,
    used_mcp_tools TEXT,
    status VARCHAR(20) DEFAULT 'generated',
    created_by VARCHAR(100),
    created_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_by VARCHAR(100),
    updated_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    is_deleted BOOLEAN DEFAULT FALSE
);

-- 添加列注释
COMMENT ON COLUMN ai_answer_history.id IS '主键ID';
COMMENT ON COLUMN ai_answer_history.answer_id IS '回答业务ID';
COMMENT ON COLUMN ai_answer_history.repair_id IS '关联的运维单ID';
COMMENT ON COLUMN ai_answer_history.question IS '用户提出的问题';
COMMENT ON COLUMN ai_answer_history.answer IS 'AI生成的回答';
COMMENT ON COLUMN ai_answer_history.provider_used IS '使用的LLM提供商';
COMMENT ON COLUMN ai_answer_history.model_used IS '使用的模型名称';
COMMENT ON COLUMN ai_answer_history.confidence_score IS 'AI回答的置信度分数(0-1)';
COMMENT ON COLUMN ai_answer_history.processing_time_ms IS '处理时间(毫秒)';
COMMENT ON COLUMN ai_answer_history.used_mcp_tools IS '使用的MCP工具(JSON格式)';
COMMENT ON COLUMN ai_answer_history.status IS '状态: generated, accepted, rejected, escalated';
COMMENT ON COLUMN ai_answer_history.created_by IS '创建人';
COMMENT ON COLUMN ai_answer_history.created_time IS '创建时间';
COMMENT ON COLUMN ai_answer_history.updated_by IS '更新人';
COMMENT ON COLUMN ai_answer_history.updated_time IS '更新时间';
COMMENT ON COLUMN ai_answer_history.is_deleted IS '是否删除';

-- 创建索引
CREATE INDEX idx_ai_answer_repair_id ON ai_answer_history(repair_id);
CREATE INDEX idx_ai_answer_provider ON ai_answer_history(provider_used);
CREATE INDEX idx_ai_answer_status ON ai_answer_history(status);
CREATE INDEX idx_ai_answer_created_time ON ai_answer_history(created_time);

-- ============================================================================
-- 3. AI反馈表 (ai_feedback)
-- 用于存储用户对AI回答的反馈信息
-- ============================================================================
CREATE TABLE ai_feedback (
    id BIGSERIAL PRIMARY KEY,
    feedback_id VARCHAR(100) UNIQUE NOT NULL,
    answer_id VARCHAR(100) NOT NULL,
    repair_id VARCHAR(100),
    feedback_type VARCHAR(20) NOT NULL,
    user_rating INTEGER CHECK (user_rating >= 1 AND user_rating <= 5),
    user_comment TEXT,
    improvement TEXT,
    feedback_username VARCHAR(100),
    feedback_nickname VARCHAR(100),
    resolution_status VARCHAR(50),
    created_by VARCHAR(100),
    created_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_by VARCHAR(100),
    updated_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    is_deleted BOOLEAN DEFAULT FALSE
);

-- 添加列注释
COMMENT ON COLUMN ai_feedback.id IS '主键ID';
COMMENT ON COLUMN ai_feedback.feedback_id IS '反馈业务ID';
COMMENT ON COLUMN ai_feedback.answer_id IS '关联的AI回答ID';
COMMENT ON COLUMN ai_feedback.repair_id IS '关联的运维单ID';
COMMENT ON COLUMN ai_feedback.feedback_type IS '反馈类型: accept, reject, escalate';
COMMENT ON COLUMN ai_feedback.user_rating IS '用户评分(1-5)';
COMMENT ON COLUMN ai_feedback.user_comment IS '用户评论';
COMMENT ON COLUMN ai_feedback.improvement IS '改进建议';
COMMENT ON COLUMN ai_feedback.feedback_username IS '反馈用户账号';
COMMENT ON COLUMN ai_feedback.feedback_nickname IS '反馈用户昵称';
COMMENT ON COLUMN ai_feedback.resolution_status IS '解决状态';
COMMENT ON COLUMN ai_feedback.created_by IS '创建人';
COMMENT ON COLUMN ai_feedback.created_time IS '创建时间';
COMMENT ON COLUMN ai_feedback.updated_by IS '更新人';
COMMENT ON COLUMN ai_feedback.updated_time IS '更新时间';
COMMENT ON COLUMN ai_feedback.is_deleted IS '是否删除';

-- 创建索引
CREATE INDEX idx_ai_feedback_answer_id ON ai_feedback(answer_id);
CREATE INDEX idx_ai_feedback_repair_id ON ai_feedback(repair_id);
CREATE INDEX idx_ai_feedback_type ON ai_feedback(feedback_type);
CREATE INDEX idx_ai_feedback_rating ON ai_feedback(user_rating);

-- ============================================================================
-- 4. AI统计表 (ai_statistics)
-- 用于存储AI使用统计数据和指标
-- ============================================================================
CREATE TABLE ai_statistics (
    id BIGSERIAL PRIMARY KEY,
    stat_date DATE NOT NULL,
    provider_name VARCHAR(50) NOT NULL,
    total_requests BIGINT DEFAULT 0,
    successful_requests BIGINT DEFAULT 0,
    failed_requests BIGINT DEFAULT 0,
    average_response_time BIGINT DEFAULT 0,
    total_tokens_used BIGINT DEFAULT 0,
    created_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 添加列注释
COMMENT ON COLUMN ai_statistics.id IS '主键ID';
COMMENT ON COLUMN ai_statistics.stat_date IS '统计日期';
COMMENT ON COLUMN ai_statistics.provider_name IS 'LLM提供商名称';
COMMENT ON COLUMN ai_statistics.total_requests IS '总请求数';
COMMENT ON COLUMN ai_statistics.successful_requests IS '成功请求数';
COMMENT ON COLUMN ai_statistics.failed_requests IS '失败请求数';
COMMENT ON COLUMN ai_statistics.average_response_time IS '平均响应时间(毫秒)';
COMMENT ON COLUMN ai_statistics.total_tokens_used IS '总消耗Token数';
COMMENT ON COLUMN ai_statistics.created_time IS '创建时间';
COMMENT ON COLUMN ai_statistics.updated_time IS '更新时间';

-- 创建索引
CREATE UNIQUE INDEX idx_ai_statistics_date_provider ON ai_statistics(stat_date, provider_name);
CREATE INDEX idx_ai_statistics_date ON ai_statistics(stat_date);
CREATE INDEX idx_ai_statistics_provider ON ai_statistics(provider_name);

-- ============================================================================
-- 5. AI知识库表 (ai_knowledge_base)
-- 用于存储AI训练和回答所需的知识库条目，支持向量存储和检索
-- ============================================================================
CREATE TABLE ai_knowledge_base (
    id BIGSERIAL PRIMARY KEY,
    kb_id VARCHAR(100) UNIQUE NOT NULL,
    title VARCHAR(200) NOT NULL,
    content TEXT NOT NULL,
    category VARCHAR(100),
    keywords TEXT,
    problem_type VARCHAR(100),
    solution_steps TEXT,
    relevance_score DECIMAL(5,4) DEFAULT 0.0,
    usage_count INTEGER DEFAULT 0,
    -- 向量化相关字段
    embedding_json TEXT,
    embedding_model VARCHAR(100),
    embedding_dimension INTEGER,
    source_type VARCHAR(50) DEFAULT 'manual',
    source_repair_id VARCHAR(100),
    last_sync_time TIMESTAMP,
    vectorization_status VARCHAR(20) DEFAULT 'pending',
    -- 原有字段
    created_by VARCHAR(100),
    created_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_by VARCHAR(100),
    updated_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    is_deleted BOOLEAN DEFAULT FALSE
);

-- 添加列注释
COMMENT ON COLUMN ai_knowledge_base.id IS '主键ID';
COMMENT ON COLUMN ai_knowledge_base.kb_id IS '知识库条目ID';
COMMENT ON COLUMN ai_knowledge_base.title IS '知识标题';
COMMENT ON COLUMN ai_knowledge_base.content IS '知识内容';
COMMENT ON COLUMN ai_knowledge_base.category IS '知识分类';
COMMENT ON COLUMN ai_knowledge_base.keywords IS '关键词(JSON数组格式)';
COMMENT ON COLUMN ai_knowledge_base.problem_type IS '问题类型';
COMMENT ON COLUMN ai_knowledge_base.solution_steps IS '解决步骤(JSON数组格式)';
COMMENT ON COLUMN ai_knowledge_base.relevance_score IS '相关性评分';
COMMENT ON COLUMN ai_knowledge_base.usage_count IS '使用次数';
COMMENT ON COLUMN ai_knowledge_base.embedding_json IS '向量数据(JSON格式存储)';
COMMENT ON COLUMN ai_knowledge_base.embedding_model IS '生成向量的模型名称';
COMMENT ON COLUMN ai_knowledge_base.embedding_dimension IS '向量维度';
COMMENT ON COLUMN ai_knowledge_base.source_type IS '数据来源类型: manual, repair, auto';
COMMENT ON COLUMN ai_knowledge_base.source_repair_id IS '来源工单ID(如果来自工单)';
COMMENT ON COLUMN ai_knowledge_base.last_sync_time IS '最后同步时间';
COMMENT ON COLUMN ai_knowledge_base.vectorization_status IS '向量化状态: pending, processing, completed, failed';
COMMENT ON COLUMN ai_knowledge_base.created_by IS '创建人';
COMMENT ON COLUMN ai_knowledge_base.created_time IS '创建时间';
COMMENT ON COLUMN ai_knowledge_base.updated_by IS '更新人';
COMMENT ON COLUMN ai_knowledge_base.updated_time IS '更新时间';
COMMENT ON COLUMN ai_knowledge_base.is_deleted IS '是否删除';

-- 创建索引
CREATE INDEX idx_ai_kb_category ON ai_knowledge_base(category);
CREATE INDEX idx_ai_kb_problem_type ON ai_knowledge_base(problem_type);
CREATE INDEX idx_ai_kb_relevance ON ai_knowledge_base(relevance_score);
CREATE INDEX idx_ai_kb_usage ON ai_knowledge_base(usage_count);
CREATE INDEX idx_ai_kb_source_type ON ai_knowledge_base(source_type);
CREATE INDEX idx_ai_kb_source_repair_id ON ai_knowledge_base(source_repair_id);
CREATE INDEX idx_ai_kb_vectorization_status ON ai_knowledge_base(vectorization_status);
CREATE INDEX idx_ai_kb_last_sync_time ON ai_knowledge_base(last_sync_time);
CREATE INDEX idx_ai_kb_embedding_model ON ai_knowledge_base(embedding_model);

-- ============================================================================
-- 6. AI MCP工具表 (ai_mcp_tools)
-- 用于存储MCP工具配置和权限管理
-- ============================================================================
CREATE TABLE ai_mcp_tools (
    id BIGSERIAL PRIMARY KEY,
    tool_name VARCHAR(100) UNIQUE NOT NULL,
    tool_description TEXT,
    tool_config TEXT,
    permission_level INTEGER DEFAULT 1,
    enabled BOOLEAN DEFAULT TRUE,
    usage_count BIGINT DEFAULT 0,
    created_by VARCHAR(100),
    created_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_by VARCHAR(100),
    updated_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    is_deleted BOOLEAN DEFAULT FALSE
);

-- 添加列注释
COMMENT ON COLUMN ai_mcp_tools.id IS '主键ID';
COMMENT ON COLUMN ai_mcp_tools.tool_name IS 'MCP工具名称';
COMMENT ON COLUMN ai_mcp_tools.tool_description IS '工具描述';
COMMENT ON COLUMN ai_mcp_tools.tool_config IS '工具配置(JSON格式)';
COMMENT ON COLUMN ai_mcp_tools.permission_level IS '权限级别: 1基础, 2中级, 3高级';
COMMENT ON COLUMN ai_mcp_tools.enabled IS '是否启用';
COMMENT ON COLUMN ai_mcp_tools.usage_count IS '使用次数';
COMMENT ON COLUMN ai_mcp_tools.created_by IS '创建人';
COMMENT ON COLUMN ai_mcp_tools.created_time IS '创建时间';
COMMENT ON COLUMN ai_mcp_tools.updated_by IS '更新人';
COMMENT ON COLUMN ai_mcp_tools.updated_time IS '更新时间';
COMMENT ON COLUMN ai_mcp_tools.is_deleted IS '是否删除';

-- 创建索引
CREATE INDEX idx_ai_mcp_permission ON ai_mcp_tools(permission_level);
CREATE INDEX idx_ai_mcp_enabled ON ai_mcp_tools(enabled);
CREATE INDEX idx_ai_mcp_usage ON ai_mcp_tools(usage_count);

-- ============================================================================
-- 7. AI向量化统计表 (ai_vectorization_stats)
-- 用于存储向量化迁移的统计信息和进度追踪
-- ============================================================================
CREATE TABLE ai_vectorization_stats (
    id BIGSERIAL PRIMARY KEY,
    stat_date DATE NOT NULL,
    migration_type VARCHAR(20) NOT NULL,
    total_records BIGINT DEFAULT 0,
    processed_records BIGINT DEFAULT 0,
    failed_records BIGINT DEFAULT 0,
    processing_time_ms BIGINT DEFAULT 0,
    error_details TEXT,
    batch_size INTEGER DEFAULT 100,
    start_time TIMESTAMP,
    end_time TIMESTAMP,
    status VARCHAR(20) DEFAULT 'pending',
    created_by VARCHAR(100),
    created_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 添加列注释
COMMENT ON COLUMN ai_vectorization_stats.id IS '主键ID';
COMMENT ON COLUMN ai_vectorization_stats.stat_date IS '统计日期';
COMMENT ON COLUMN ai_vectorization_stats.migration_type IS '迁移类型: full, incremental, manual';
COMMENT ON COLUMN ai_vectorization_stats.total_records IS '总记录数';
COMMENT ON COLUMN ai_vectorization_stats.processed_records IS '已处理记录数';
COMMENT ON COLUMN ai_vectorization_stats.failed_records IS '失败记录数';
COMMENT ON COLUMN ai_vectorization_stats.processing_time_ms IS '处理耗时(毫秒)';
COMMENT ON COLUMN ai_vectorization_stats.error_details IS '错误详情(JSON格式)';
COMMENT ON COLUMN ai_vectorization_stats.batch_size IS '批处理大小';
COMMENT ON COLUMN ai_vectorization_stats.start_time IS '开始时间';
COMMENT ON COLUMN ai_vectorization_stats.end_time IS '结束时间';
COMMENT ON COLUMN ai_vectorization_stats.status IS '状态: pending, running, completed, failed, cancelled';
COMMENT ON COLUMN ai_vectorization_stats.created_by IS '创建人';
COMMENT ON COLUMN ai_vectorization_stats.created_time IS '创建时间';
COMMENT ON COLUMN ai_vectorization_stats.updated_time IS '更新时间';

-- 创建索引
CREATE INDEX idx_ai_vectorization_date ON ai_vectorization_stats(stat_date);
CREATE INDEX idx_ai_vectorization_type ON ai_vectorization_stats(migration_type);
CREATE INDEX idx_ai_vectorization_status ON ai_vectorization_stats(status);
CREATE INDEX idx_ai_vectorization_start_time ON ai_vectorization_stats(start_time);

-- ============================================================================
-- 外键约束
-- ============================================================================
-- ai_feedback表的answer_id关联ai_answer_history表
-- 注意: 由于使用VARCHAR类型的业务ID，这里使用逻辑外键而非物理外键

-- ============================================================================
-- 初始化数据
-- ============================================================================

-- 初始化AI配置数据
INSERT INTO ai_config (provider_name, config_key, config_value, enabled, created_by) VALUES
('openai', 'api_key', '', false, 'system'),
('openai', 'base_url', 'https://api.openai.com/v1', true, 'system'),
('openai', 'max_tokens', '4000', true, 'system'),
('claude', 'api_key', '', false, 'system'),
('claude', 'base_url', 'https://api.anthropic.com', true, 'system'),
('claude', 'max_tokens', '4000', true, 'system'),
('qwen', 'api_key', '', false, 'system'),
('qwen', 'base_url', 'https://dashscope.aliyuncs.com/api/v1', true, 'system');

-- 初始化MCP工具配置
INSERT INTO ai_mcp_tools (tool_name, tool_description, permission_level, enabled, created_by) VALUES
('database_query', '数据库查询工具，用于查询运维单和相关信息', 2, true, 'system'),
('repair_analysis', '运维单分析工具，用于分析问题类型和优先级', 2, true, 'system'),
('knowledge_search', '知识库搜索工具，用于查找相关解决方案', 1, true, 'system'),
('notification_send', '通知发送工具，用于发送各类运维通知', 3, true, 'system'),
('file_processor', '文件处理工具，用于处理附件和文档', 2, true, 'system');

-- 初始化知识库示例数据
INSERT INTO ai_knowledge_base (kb_id, title, content, category, keywords, problem_type, solution_steps, created_by) VALUES
('kb_001', '系统登录问题处理', '用户无法登录系统时的标准处理流程', '系统问题', '["登录", "账号", "密码", "权限"]', '系统访问', '["检查账号状态", "重置密码", "验证权限配置", "测试登录功能"]', 'system'),
('kb_002', '数据库连接异常处理', '数据库连接超时或异常的排查和处理方法', '数据库问题', '["数据库", "连接", "超时", "异常"]', '数据库故障', '["检查数据库服务状态", "验证连接配置", "检查网络连通性", "重启数据库服务"]', 'system'),
('kb_003', '网络故障排查流程', '网络连接问题的标准排查和处理流程', '网络问题', '["网络", "连接", "超时", "丢包"]', '网络故障', '["ping测试", "检查网络设备", "验证路由配置", "联系网络管理员"]', 'system');

-- ============================================================================
-- 表注释
-- ============================================================================
COMMENT ON TABLE ai_config IS 'AI配置表-存储LLM提供商配置信息';
COMMENT ON TABLE ai_answer_history IS 'AI回答历史表-存储AI生成的回答记录';
COMMENT ON TABLE ai_feedback IS 'AI反馈表-存储用户对AI回答的反馈';
COMMENT ON TABLE ai_statistics IS 'AI统计表-存储AI使用统计数据';
COMMENT ON TABLE ai_knowledge_base IS 'AI知识库表-存储AI训练所需知识条目,支持向量化存储';
COMMENT ON TABLE ai_mcp_tools IS 'AI MCP工具表-存储MCP工具配置和权限';
COMMENT ON TABLE ai_vectorization_stats IS 'AI向量化统计表-存储向量化迁移统计信息';

-- ============================================================================
-- 脚本执行完成
-- ============================================================================