-- 药物AI识别任务表创建脚本
-- 用于存储用户上传的药品图片和AI识别过程的状态追踪

CREATE TABLE IF NOT EXISTS `t_medication_recognition_tasks` (
  `id` VARCHAR(100) NOT NULL COMMENT '任务唯一标识，格式: task_{userId}_{timestamp}',
  `user_id` VARCHAR(50) NOT NULL COMMENT '用户ID',
  `front_image_url` VARCHAR(500) NOT NULL COMMENT '正面图片URL（必需）',
  `side_image_url` VARCHAR(500) NOT NULL COMMENT '侧面图片URL（必需）',
  `auxiliary_image_url` VARCHAR(500) DEFAULT NULL COMMENT '辅助面图片URL（可选，如说明书）',
  `status` VARCHAR(50) NOT NULL DEFAULT 'pending' COMMENT '识别状态: pending/analyzing_product/analyzing_suitability/analyzing_ingredients/analyzing_effects/completed/failed',
  `current_step` VARCHAR(200) DEFAULT NULL COMMENT '当前步骤描述，用于向用户展示',
  `progress` INT NOT NULL DEFAULT 0 COMMENT '进度百分比(0-100)',
  `recognition_result` TEXT DEFAULT NULL COMMENT '识别结果(JSON格式)，包含药品名称、剂型、剂量、适宜人群等完整信息',
  `error_message` TEXT DEFAULT NULL COMMENT '错误信息（仅在status为failed时有值）',
  `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `completed_at` TIMESTAMP NULL DEFAULT NULL COMMENT '完成时间（成功或失败）',
  PRIMARY KEY (`id`),
  INDEX `idx_user_id` (`user_id`),
  INDEX `idx_status` (`status`),
  INDEX `idx_created_at` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='药物AI识别任务表';

-- 添加外键约束（可选，如果需要严格的数据完整性）
-- ALTER TABLE `t_medication_recognition_tasks`
-- ADD CONSTRAINT `fk_recognition_user_id`
-- FOREIGN KEY (`user_id`) REFERENCES `t_users`(`id`) ON DELETE CASCADE;

-- 示例数据结构说明
-- recognition_result JSON 格式示例：
/*
{
  "name": "阿莫西林胶囊",
  "photoUrl": "https://cdn.example.com/medications/front_001.jpg",
  "form": "capsule",
  "dosageValue": 0.25,
  "dosageUnit": "g",
  "timesPerDay": 3,
  "medicationTimes": ["08:00", "14:00", "20:00"],
  "suitableFor": ["成年人", "细菌感染患者"],
  "unsuitableFor": ["青霉素过敏者", "孕妇", "哺乳期妇女"],
  "mainIngredients": ["阿莫西林"],
  "mainUsage": "用于敏感菌引起的各种感染",
  "sideEffects": ["恶心", "呕吐", "腹泻", "皮疹"],
  "storageAdvice": ["密封保存", "室温避光", "儿童接触不到的地方"],
  "healthAdvice": ["按时服药", "多喝水", "避免饮酒"],
  "confidence": 0.95
}
*/