-- ============================================
-- 题库功能 - 数据库表创建SQL
-- ============================================

-- 1. 题库表
DROP TABLE IF EXISTS `question_bank`;
CREATE TABLE `question_bank` (
  `id` bigint NOT NULL AUTO_INCREMENT COMMENT '题库ID',
  `bank_name` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '题库名称',
  `subject_id` bigint NOT NULL COMMENT '科目ID',
  `subject_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '科目名称',
  `description` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '题库描述',
  `question_count` int DEFAULT 0 COMMENT '题目数量',
  `create_user_id` bigint DEFAULT NULL COMMENT '创建人ID（教师ID）',
  `create_by` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '创建者',
  `create_time` datetime DEFAULT NULL COMMENT '创建时间',
  `update_by` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '更新者',
  `update_time` datetime DEFAULT NULL COMMENT '更新时间',
  `remark` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '备注',
  PRIMARY KEY (`id`),
  KEY `idx_subject_id` (`subject_id`),
  KEY `idx_create_user_id` (`create_user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='题库表';

-- 2. 题库题目表
DROP TABLE IF EXISTS `question_bank_item`;
CREATE TABLE `question_bank_item` (
  `id` bigint NOT NULL AUTO_INCREMENT COMMENT '题目ID',
  `bank_id` bigint NOT NULL COMMENT '题库ID',
  `question_type` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '题型（single-单选, multiple-多选, judge-判断, fill-填空, essay-简答）',
  `question_content` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '题干',
  `options` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '选项（JSON格式，用于单选、多选、判断题）',
  `correct_answer` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '正确答案',
  `score` decimal(10,2) DEFAULT 0.00 COMMENT '默认分值',
  `create_time` datetime DEFAULT NULL COMMENT '创建时间',
  `update_time` datetime DEFAULT NULL COMMENT '更新时间',
  PRIMARY KEY (`id`),
  KEY `idx_bank_id` (`bank_id`),
  KEY `idx_question_type` (`question_type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='题库题目表';

-- 3. 修改question表，添加来源字段
ALTER TABLE `question` 
ADD COLUMN `source_type` varchar(20) DEFAULT 'manual' COMMENT '题目来源（manual-手动创建, bank-题库抽取, ai-AI生成）' AFTER `exam_id`,
ADD COLUMN `bank_id` bigint DEFAULT NULL COMMENT '来源题库ID（如果来自题库）' AFTER `source_type`,
ADD COLUMN `bank_item_id` bigint DEFAULT NULL COMMENT '来源题库题目ID（如果来自题库）' AFTER `bank_id`;

