# AI服务数据库设计文档

## 1. 数据库环境说明

- 数据库名称: xlxumu_ai
- 字符集: utf8mb4
- 排序规则: utf8mb4_unicode_ci

## 2. 数据表设计

### 2.1 智能问答记录表 (ai_qa_records)

存储用户与AI的问答记录。

```sql
CREATE TABLE `ai_qa_records` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `user_id` BIGINT UNSIGNED NOT NULL COMMENT '用户ID',
  `question` TEXT NOT NULL COMMENT '用户问题',
  `answer` TEXT NOT NULL COMMENT 'AI回答',
  `model_version` VARCHAR(50) NOT NULL COMMENT '使用的模型版本',
  `processing_time` INT NOT NULL COMMENT '处理耗时(毫秒)',
  `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_created_at` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='智能问答记录表';
```

### 2.2 图像识别记录表 (image_recognition_records)

存储图像识别请求和结果。

```sql
CREATE TABLE `image_recognition_records` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `user_id` BIGINT UNSIGNED NOT NULL COMMENT '用户ID',
  `image_url` VARCHAR(500) NOT NULL COMMENT '图像URL',
  `recognition_type` VARCHAR(50) NOT NULL COMMENT '识别类型(病虫害识别/作物识别等)',
  `result` JSON NOT NULL COMMENT '识别结果(JSON格式)',
  `confidence` DECIMAL(5,4) NOT NULL COMMENT '置信度',
  `processing_time` INT NOT NULL COMMENT '处理耗时(毫秒)',
  `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_recognition_type` (`recognition_type`),
  KEY `idx_created_at` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='图像识别记录表';
```

### 2.3 数据分析任务表 (analysis_tasks)

存储数据分析任务信息。

```sql
CREATE TABLE `analysis_tasks` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `user_id` BIGINT UNSIGNED NOT NULL COMMENT '用户ID',
  `task_name` VARCHAR(200) NOT NULL COMMENT '任务名称',
  `task_type` VARCHAR(50) NOT NULL COMMENT '任务类型(产量预测/价格分析等)',
  `input_data` JSON NOT NULL COMMENT '输入数据(JSON格式)',
  `result` JSON COMMENT '分析结果(JSON格式)',
  `status` VARCHAR(20) NOT NULL DEFAULT 'PENDING' COMMENT '任务状态(PENDING/PROCESSING/COMPLETED/FAILED)',
  `processing_time` INT COMMENT '处理耗时(毫秒)',
  `error_message` TEXT COMMENT '错误信息',
  `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_task_type` (`task_type`),
  KEY `idx_status` (`status`),
  KEY `idx_created_at` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='数据分析任务表';
```

### 2.4 模型版本表 (model_versions)

存储AI模型版本信息。

```sql
CREATE TABLE `model_versions` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `model_name` VARCHAR(100) NOT NULL COMMENT '模型名称',
  `version` VARCHAR(50) NOT NULL COMMENT '版本号',
  `description` TEXT COMMENT '版本描述',
  `file_path` VARCHAR(500) NOT NULL COMMENT '模型文件路径',
  `accuracy` DECIMAL(5,4) COMMENT '模型准确率',
  `is_active` TINYINT(1) NOT NULL DEFAULT '0' COMMENT '是否为当前活跃版本',
  `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_model_version` (`model_name`, `version`),
  KEY `idx_model_name` (`model_name`),
  KEY `idx_is_active` (`is_active`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='模型版本表';
```

### 2.5 用户反馈表 (user_feedbacks)

存储用户对AI服务的反馈。

```sql
CREATE TABLE `user_feedbacks` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `user_id` BIGINT UNSIGNED NOT NULL COMMENT '用户ID',
  `service_type` VARCHAR(50) NOT NULL COMMENT '服务类型(QA/IMAGE_RECOGNITION/ANALYSIS)',
  `record_id` BIGINT UNSIGNED NOT NULL COMMENT '相关记录ID(问答记录ID/识别记录ID等)',
  `rating` TINYINT NOT NULL COMMENT '评分(1-5星)',
  `feedback` TEXT COMMENT '反馈内容',
  `is_helpful` TINYINT(1) NOT NULL COMMENT '是否有帮助',
  `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_service_type` (`service_type`),
  KEY `idx_record_id` (`record_id`),
  KEY `idx_created_at` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户反馈表';
```