-- ----------------------------
-- AI模块数据库表结构
-- ----------------------------

-- ----------------------------
-- AI Provider配置表
-- ----------------------------
DROP TABLE IF EXISTS `ai_provider_config`;
CREATE TABLE `ai_provider_config` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `provider_name` varchar(100) NOT NULL COMMENT 'Provider名称',
  `provider_type` varchar(50) NOT NULL COMMENT 'Provider类型',
  `enabled` tinyint(1) NOT NULL DEFAULT '1' COMMENT '是否启用(0=禁用 1=启用)',
  `priority` int(11) NOT NULL DEFAULT '0' COMMENT '优先级(数字越小优先级越高)',
  `weight` int(11) NOT NULL DEFAULT '1' COMMENT '权重(用于负载均衡)',
  `max_concurrency` int(11) NOT NULL DEFAULT '10' COMMENT '最大并发数',
  `timeout_seconds` int(11) NOT NULL DEFAULT '30' COMMENT '超时时间(秒)',
  `max_retries` int(11) NOT NULL DEFAULT '3' COMMENT '最大重试次数',
  `circuit_breaker_config` json DEFAULT NULL COMMENT '熔断器配置',
  `rate_limit_config` json DEFAULT NULL COMMENT '限流配置',
  `api_config` json DEFAULT NULL COMMENT 'API配置',
  `model_config` json DEFAULT NULL COMMENT '模型配置',
  `extension_config` json DEFAULT NULL COMMENT '扩展配置',
  `health_check_url` varchar(500) DEFAULT NULL COMMENT '健康检查URL',
  `health_check_interval` int(11) NOT NULL DEFAULT '60' COMMENT '健康检查间隔(秒)',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `create_by` varchar(64) DEFAULT NULL COMMENT '创建者',
  `update_by` varchar(64) DEFAULT NULL COMMENT '更新者',
  `remark` varchar(500) DEFAULT NULL COMMENT '备注',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_provider_name` (`provider_name`),
  KEY `idx_provider_type` (`provider_type`),
  KEY `idx_enabled` (`enabled`),
  KEY `idx_priority` (`priority`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='AI Provider配置表';

-- ----------------------------
-- AI Provider配置历史表
-- ----------------------------
DROP TABLE IF EXISTS `ai_provider_config_history`;
CREATE TABLE `ai_provider_config_history` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `config_id` bigint(20) NOT NULL COMMENT '配置ID',
  `provider_name` varchar(100) NOT NULL COMMENT 'Provider名称',
  `config_data` json NOT NULL COMMENT '配置数据',
  `operation_type` varchar(20) NOT NULL COMMENT '操作类型(CREATE/UPDATE/DELETE)',
  `operation_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '操作时间',
  `operation_by` varchar(64) DEFAULT NULL COMMENT '操作者',
  `remark` varchar(500) DEFAULT NULL COMMENT '备注',
  PRIMARY KEY (`id`),
  KEY `idx_config_id` (`config_id`),
  KEY `idx_provider_name` (`provider_name`),
  KEY `idx_operation_time` (`operation_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='AI Provider配置历史表';

-- ----------------------------
-- AI聊天记录表
-- ----------------------------
DROP TABLE IF EXISTS `ai_chat_record`;
CREATE TABLE `ai_chat_record` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `session_id` varchar(100) NOT NULL COMMENT '会话ID',
  `user_id` bigint(20) NOT NULL COMMENT '用户ID',
  `provider_name` varchar(100) NOT NULL COMMENT 'Provider名称',
  `model_name` varchar(100) DEFAULT NULL COMMENT '模型名称',
  `message_type` varchar(20) NOT NULL COMMENT '消息类型(USER/ASSISTANT)',
  `content` text NOT NULL COMMENT '消息内容',
  `tokens_used` int(11) DEFAULT NULL COMMENT '使用的Token数',
  `cost` decimal(10,6) DEFAULT NULL COMMENT '成本',
  `response_time` int(11) DEFAULT NULL COMMENT '响应时间(毫秒)',
  `is_stream` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否流式响应',
  `error_message` text DEFAULT NULL COMMENT '错误信息',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`id`),
  KEY `idx_session_id` (`session_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_provider_name` (`provider_name`),
  KEY `idx_create_time` (`create_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='AI聊天记录表';

-- ----------------------------
-- AI使用统计表
-- ----------------------------
DROP TABLE IF EXISTS `ai_usage_stats`;
CREATE TABLE `ai_usage_stats` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `user_id` bigint(20) NOT NULL COMMENT '用户ID',
  `provider_name` varchar(100) NOT NULL COMMENT 'Provider名称',
  `model_name` varchar(100) DEFAULT NULL COMMENT '模型名称',
  `stat_date` date NOT NULL COMMENT '统计日期',
  `request_count` int(11) NOT NULL DEFAULT '0' COMMENT '请求次数',
  `success_count` int(11) NOT NULL DEFAULT '0' COMMENT '成功次数',
  `error_count` int(11) NOT NULL DEFAULT '0' COMMENT '错误次数',
  `total_tokens` bigint(20) NOT NULL DEFAULT '0' COMMENT '总Token数',
  `total_cost` decimal(10,6) NOT NULL DEFAULT '0.000000' COMMENT '总成本',
  `avg_response_time` int(11) NOT NULL DEFAULT '0' COMMENT '平均响应时间(毫秒)',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_user_provider_date` (`user_id`,`provider_name`,`stat_date`),
  KEY `idx_provider_name` (`provider_name`),
  KEY `idx_stat_date` (`stat_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='AI使用统计表';

-- ----------------------------
-- AI评价记录表
-- ----------------------------
DROP TABLE IF EXISTS `ai_rating_record`;
CREATE TABLE `ai_rating_record` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `message_id` varchar(100) NOT NULL COMMENT '消息ID',
  `user_id` bigint(20) NOT NULL COMMENT '用户ID',
  `provider_name` varchar(100) NOT NULL COMMENT 'Provider名称',
  `rating` int(11) NOT NULL COMMENT '评分(1-5)',
  `feedback` text DEFAULT NULL COMMENT '反馈内容',
  `dimension` varchar(50) DEFAULT NULL COMMENT '评价维度',
  `anonymous` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否匿名',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_message_user` (`message_id`,`user_id`),
  KEY `idx_provider_name` (`provider_name`),
  KEY `idx_rating` (`rating`),
  KEY `idx_create_time` (`create_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='AI评价记录表';

-- ----------------------------
-- AI敏感词库表
-- ----------------------------
DROP TABLE IF EXISTS `ai_sensitive_word`;
CREATE TABLE `ai_sensitive_word` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `word` varchar(100) NOT NULL COMMENT '敏感词',
  `word_type` varchar(50) NOT NULL COMMENT '词类型(PROFANITY/HATE_SPEECH/VIOLENCE/SEXUAL_CONTENT/ILLEGAL_ACTIVITY/PERSONAL_INFO/SPAM/MALICIOUS_CODE/POLITICAL_SENSITIVE/COMMERCIAL_AD)',
  `enabled` tinyint(1) NOT NULL DEFAULT '1' COMMENT '是否启用(0=禁用 1=启用)',
  `priority` int(11) NOT NULL DEFAULT '0' COMMENT '优先级(数字越大优先级越高)',
  `remark` varchar(500) 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 '更新时间',
  `create_by` varchar(64) DEFAULT NULL COMMENT '创建者',
  `update_by` varchar(64) DEFAULT NULL COMMENT '更新者',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_word` (`word`),
  KEY `idx_word_type` (`word_type`),
  KEY `idx_enabled` (`enabled`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='AI敏感词库表';

-- ----------------------------
-- AI绘画历史记录表
-- ----------------------------
DROP TABLE IF EXISTS `ai_painting_record`;
CREATE TABLE `ai_painting_record` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `task_id` varchar(100) NOT NULL COMMENT '任务ID',
  `user_id` bigint(20) DEFAULT NULL COMMENT '用户ID',
  `engine_type` varchar(50) NOT NULL COMMENT '绘画引擎类型(DALL_E_3/MIDJOURNEY/STABLE_DIFFUSION)',
  `prompt` text NOT NULL COMMENT '提示词',
  `params` json DEFAULT NULL COMMENT '绘画参数(JSON)',
  `status` varchar(50) NOT NULL COMMENT '状态(PENDING/RUNNING/SUCCESS/FAILED)',
  `progress` int(11) NOT NULL DEFAULT '0' COMMENT '进度(0-100)',
  `images` json DEFAULT NULL COMMENT '生成的图片URL列表(JSON数组)',
  `message` text 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 '更新时间',
  `share_code` varchar(64) DEFAULT NULL COMMENT '分享码（用于分享作品）',
  `is_public` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否公开分享(0=私有 1=公开)',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_task_id` (`task_id`),
  UNIQUE KEY `uk_share_code` (`share_code`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_engine_type` (`engine_type`),
  KEY `idx_status` (`status`),
  KEY `idx_create_time` (`create_time`),
  KEY `idx_is_public` (`is_public`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='AI绘画历史记录表';

-- ----------------------------
-- AI绘画参数模板表
-- ----------------------------
DROP TABLE IF EXISTS `ai_painting_template`;
CREATE TABLE `ai_painting_template` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `template_name` varchar(100) NOT NULL COMMENT '模板名称',
  `user_id` bigint(20) DEFAULT NULL COMMENT '用户ID(为空表示系统模板)',
  `engine_type` varchar(50) NOT NULL COMMENT '绘画引擎类型(DALL_E_3/MIDJOURNEY/STABLE_DIFFUSION)',
  `params` json NOT NULL COMMENT '绘画参数(JSON)',
  `description` varchar(500) DEFAULT NULL COMMENT '模板描述',
  `is_public` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否公开(0=私有 1=公开)',
  `sort` int(11) NOT NULL DEFAULT '0' COMMENT '排序',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `create_by` varchar(64) DEFAULT NULL COMMENT '创建者',
  `update_by` varchar(64) DEFAULT NULL COMMENT '更新者',
  PRIMARY KEY (`id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_engine_type` (`engine_type`),
  KEY `idx_is_public` (`is_public`),
  KEY `idx_create_time` (`create_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='AI绘画参数模板表';

-- ----------------------------
-- AI用户配额管理表
-- ----------------------------
DROP TABLE IF EXISTS `ai_user_quota`;
CREATE TABLE `ai_user_quota` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `user_id` bigint(20) NOT NULL COMMENT '用户ID',
  `permission_level` varchar(50) NOT NULL DEFAULT 'BASIC' COMMENT '权限级别(GUEST/BASIC/PREMIUM/VIP/ADMIN)',
  `daily_quota` bigint(20) NOT NULL DEFAULT '0' COMMENT '每日配额限制(0表示无限制)',
  `monthly_quota` bigint(20) NOT NULL DEFAULT '0' COMMENT '每月配额限制(0表示无限制)',
  `daily_used` bigint(20) NOT NULL DEFAULT '0' COMMENT '每日已使用配额',
  `monthly_used` bigint(20) NOT NULL DEFAULT '0' COMMENT '每月已使用配额',
  `last_reset_daily` datetime DEFAULT NULL COMMENT '上次重置日配额时间',
  `last_reset_monthly` datetime DEFAULT NULL COMMENT '上次重置月配额时间',
  `custom_limits` json DEFAULT NULL COMMENT '自定义限制(JSON)',
  `remark` varchar(500) 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 '更新时间',
  `create_by` varchar(64) DEFAULT NULL COMMENT '创建者',
  `update_by` varchar(64) DEFAULT NULL COMMENT '更新者',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_user_id` (`user_id`),
  KEY `idx_permission_level` (`permission_level`),
  KEY `idx_create_time` (`create_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='AI用户配额管理表';

-- ----------------------------
-- AI绘画作品收藏表
-- ----------------------------
DROP TABLE IF EXISTS `ai_painting_favorite`;
CREATE TABLE `ai_painting_favorite` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `user_id` bigint(20) NOT NULL COMMENT '用户ID',
  `painting_record_id` bigint(20) NOT NULL COMMENT '绘画记录ID',
  `task_id` varchar(100) DEFAULT NULL COMMENT '任务ID',
  `remark` varchar(500) DEFAULT NULL COMMENT '备注',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '收藏时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_user_painting` (`user_id`, `painting_record_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_painting_record_id` (`painting_record_id`),
  KEY `idx_create_time` (`create_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='AI绘画作品收藏表';

-- ----------------------------
-- 初始化AI Provider配置数据
-- ----------------------------
INSERT INTO `ai_provider_config` (`provider_name`, `provider_type`, `enabled`, `priority`, `weight`, `max_concurrency`, `timeout_seconds`, `max_retries`, `api_config`, `model_config`, `remark`) VALUES
('openai', 'OPENAI', 0, 1, 10, 10, 30, 3, '{"apiKey":"","baseUrl":"https://api.openai.com/v1","organization":""}', '{"defaultModel":"gpt-3.5-turbo","supportedModels":["gpt-3.5-turbo","gpt-4","gpt-4-turbo"]}', 'OpenAI官方API'),
('azure-openai', 'AZURE_OPENAI', 0, 2, 8, 8, 30, 3, '{"apiKey":"","endpoint":"","apiVersion":"2023-12-01-preview"}', '{"defaultModel":"gpt-35-turbo","supportedModels":["gpt-35-turbo","gpt-4"]}', 'Azure OpenAI服务'),
('chatglm', 'CHATGLM', 0, 3, 6, 6, 30, 3, '{"apiKey":"","baseUrl":"https://open.bigmodel.cn/api/paas/v4"}', '{"defaultModel":"glm-4","supportedModels":["glm-4","glm-3-turbo"]}', '智谱AI ChatGLM'),
('qwen', 'QWEN', 0, 4, 6, 6, 30, 3, '{"apiKey":"","baseUrl":"https://dashscope.aliyuncs.com/api/v1"}', '{"defaultModel":"qwen-turbo","supportedModels":["qwen-turbo","qwen-plus","qwen-max"]}', '阿里云通义千问'),
('zhipu', 'ZHIPU', 0, 5, 5, 5, 30, 3, '{"apiKey":"","baseUrl":"https://open.bigmodel.cn/api/paas/v4"}', '{"defaultModel":"glm-4","supportedModels":["glm-4","glm-3-turbo"]}', '智谱AI'),
('fastgpt', 'FASTGPT', 0, 6, 4, 4, 30, 3, '{"apiKey":"","baseUrl":"","chatId":""}', '{"defaultModel":"fastgpt","supportedModels":["fastgpt"]}', 'FastGPT平台'),
('coze', 'COZE', 0, 7, 3, 3, 30, 3, '{"apiKey":"","baseUrl":"https://www.coze.cn/api","botId":""}', '{"defaultModel":"coze","supportedModels":["coze"]}', 'Coze平台'),
('dify', 'DIFY', 0, 8, 3, 3, 30, 3, '{"apiKey":"","baseUrl":"","appId":""}', '{"defaultModel":"dify","supportedModels":["dify"]}', 'Dify平台'),
('knowledge', 'KNOWLEDGE', 1, 9, 2, 2, 30, 3, '{"enabled":true}', '{"defaultModel":"knowledge","supportedModels":["knowledge"]}', '内部知识库');