-- 优化历史功能数据库升级方案
-- 腾讯云MySQL数据库表结构设计

-- 1. 优化历史表
CREATE TABLE `optimization_history` (
    `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `user_id` BIGINT NOT NULL COMMENT '用户ID',
    `original_text` TEXT NOT NULL COMMENT '原始输入文本',
    `optimized_text` TEXT NOT NULL COMMENT '优化后文本',
    `optimization_type` VARCHAR(50) DEFAULT '提示词优化' COMMENT '优化类型',
    `industry` VARCHAR(100) DEFAULT NULL COMMENT '行业分类',
    `profession` VARCHAR(100) DEFAULT NULL COMMENT '职业分类',
    `sub_category` VARCHAR(100) DEFAULT NULL COMMENT '子分类',
    `template_id` BIGINT DEFAULT NULL COMMENT '使用的模板ID',
    `satisfaction_rating` TINYINT DEFAULT NULL COMMENT '满意度评分(1-5)',
    `generation_time` INT DEFAULT NULL COMMENT '生成耗时(毫秒)',
    `ip_address` VARCHAR(45) DEFAULT NULL COMMENT '用户IP地址',
    `user_agent` TEXT DEFAULT NULL COMMENT '用户代理信息',
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    PRIMARY KEY (`id`),
    KEY `idx_user_id` (`user_id`),
    KEY `idx_created_at` (`created_at`),
    KEY `idx_optimization_type` (`optimization_type`),
    KEY `idx_industry` (`industry`),
    KEY `idx_template_id` (`template_id`),
    KEY `idx_user_created` (`user_id`, `created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='优化历史记录表';

-- 2. 优化历史标签表（用于存储优化建议标签）
CREATE TABLE `optimization_tags` (
    `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `history_id` BIGINT NOT NULL COMMENT '历史记录ID',
    `tag_name` VARCHAR(50) NOT NULL COMMENT '标签名称',
    `tag_type` VARCHAR(20) DEFAULT 'optimization' COMMENT '标签类型',
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    PRIMARY KEY (`id`),
    KEY `idx_history_id` (`history_id`),
    KEY `idx_tag_name` (`tag_name`),
    CONSTRAINT `fk_optimization_tags_history` FOREIGN KEY (`history_id`) REFERENCES `optimization_history` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='优化历史标签表';

-- 3. 用户使用统计表
CREATE TABLE `user_usage_stats` (
    `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `user_id` BIGINT NOT NULL COMMENT '用户ID',
    `date` DATE NOT NULL COMMENT '统计日期',
    `generation_count` INT DEFAULT 0 COMMENT '生成次数',
    `total_time_saved` INT DEFAULT 0 COMMENT '节省时间(分钟)',
    `avg_rating` DECIMAL(3,2) DEFAULT NULL COMMENT '平均评分',
    `total_ratings` INT DEFAULT 0 COMMENT '总评分次数',
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_user_date` (`user_id`, `date`),
    KEY `idx_date` (`date`),
    KEY `idx_user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户使用统计表';

-- 4. 优化历史收藏表
CREATE TABLE `optimization_favorites` (
    `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `user_id` BIGINT NOT NULL COMMENT '用户ID',
    `history_id` BIGINT NOT NULL COMMENT '历史记录ID',
    `favorite_name` VARCHAR(100) DEFAULT NULL COMMENT '收藏名称',
    `notes` TEXT DEFAULT NULL COMMENT '备注',
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_user_history` (`user_id`, `history_id`),
    KEY `idx_user_id` (`user_id`),
    KEY `idx_history_id` (`history_id`),
    CONSTRAINT `fk_favorites_history` FOREIGN KEY (`history_id`) REFERENCES `optimization_history` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='优化历史收藏表';

-- 5. 系统配置表（用于存储系统级配置）
CREATE TABLE `system_config` (
    `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `config_key` VARCHAR(100) NOT NULL COMMENT '配置键',
    `config_value` TEXT NOT NULL COMMENT '配置值',
    `config_type` VARCHAR(20) DEFAULT 'string' COMMENT '配置类型',
    `description` VARCHAR(255) DEFAULT NULL COMMENT '配置描述',
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_config_key` (`config_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='系统配置表';

-- 插入默认配置
INSERT INTO `system_config` (`config_key`, `config_value`, `config_type`, `description`) VALUES
('max_history_per_user', '1000', 'integer', '每个用户最大历史记录数'),
('history_retention_days', '365', 'integer', '历史记录保留天数'),
('enable_analytics', 'true', 'boolean', '是否启用使用分析'),
('default_optimization_type', '提示词优化', 'string', '默认优化类型');

-- 创建索引优化
-- 复合索引用于常用查询
CREATE INDEX `idx_user_type_created` ON `optimization_history` (`user_id`, `optimization_type`, `created_at`);
CREATE INDEX `idx_user_rating` ON `optimization_history` (`user_id`, `satisfaction_rating`);

-- 分区表（可选，用于大数据量场景）
-- ALTER TABLE optimization_history PARTITION BY RANGE (YEAR(created_at)) (
--     PARTITION p2024 VALUES LESS THAN (2025),
--     PARTITION p2025 VALUES LESS THAN (2026),
--     PARTITION p_future VALUES LESS THAN MAXVALUE
-- );
