-- 修复题目格式问题
-- 问题：选项和答案包含 "A. " 前缀，导致答案判断失败

-- ==========================================
-- 1. 查看有问题的题目
-- ==========================================
SELECT 
    id,
    question_type,
    question_content,
    options,
    correct_answer,
    exam_id
FROM study_question
WHERE 
    -- 选项包含 "A. " 前缀的
    options LIKE '%"A. %' 
    OR options LIKE '%"B. %'
    OR options LIKE '%"C. %'
    -- 或正确答案包含前缀的
    OR (question_type = 'single' AND correct_answer LIKE '%. %')
ORDER BY exam_id, id;

-- ==========================================
-- 2. 手动修复示例（替换你的题目ID和内容）
-- ==========================================

-- 修复单选题（去除选项和答案的前缀）
-- 示例：
-- UPDATE study_question
-- SET 
--     options = '["选项内容1","选项内容2","选项内容3"]',  -- 去除 "A. "、"B. " 等前缀
--     correct_answer = 'A'  -- 只保留字母
-- WHERE id = {题目ID};

-- ==========================================
-- 3. 批量修复脚本（谨慎使用！先备份！）
-- ==========================================

-- 备份表
-- CREATE TABLE study_question_backup AS SELECT * FROM study_question;

-- 注意：以下脚本需要根据实际数据调整，建议一条一条手动执行

-- 修复考试14的题目（根据你的实际情况修改）
-- 题目149
UPDATE study_question
SET 
    options = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
        options,
        '"A. ', '"'),
        '"B. ', '"'),
        '"C. ', '"'),
        '"D. ', '"'),
        '"E. ', '"'),
        '"F. ', '"'),
    correct_answer = CASE 
        WHEN correct_answer LIKE 'A. %' THEN 'A'
        WHEN correct_answer LIKE 'B. %' THEN 'B'
        WHEN correct_answer LIKE 'C. %' THEN 'C'
        WHEN correct_answer LIKE 'D. %' THEN 'D'
        WHEN correct_answer LIKE 'E. %' THEN 'E'
        WHEN correct_answer LIKE 'F. %' THEN 'F'
        ELSE correct_answer
    END
WHERE exam_id = 14 AND question_type IN ('single', 'multiple');

-- ==========================================
-- 4. 验证修复结果
-- ==========================================
SELECT 
    id,
    question_type,
    question_content,
    options,
    correct_answer
FROM study_question
WHERE exam_id = 14
ORDER BY id;

-- ==========================================
-- 5. 检查学生答案是否也需要修复
-- ==========================================
SELECT 
    sa.id,
    sa.question_id,
    sa.student_answer,
    sa.is_correct,
    q.correct_answer,
    q.question_type
FROM study_student_answer sa
JOIN study_question q ON sa.question_id = q.id
WHERE sa.exam_id = 14
ORDER BY sa.student_id, sa.question_id;

-- 如果学生答案也包含前缀，也需要修复
-- UPDATE study_student_answer
-- SET student_answer = CASE 
--     WHEN student_answer LIKE 'A. %' THEN 'A'
--     WHEN student_answer LIKE 'B. %' THEN 'B'
--     WHEN student_answer LIKE 'C. %' THEN 'C'
--     WHEN student_answer LIKE 'D. %' THEN 'D'
--     ELSE student_answer
-- END
-- WHERE exam_id = 14;
