-- ========================================
-- 修复题目答案格式问题
-- 将包含完整格式的答案（如"B. 选项内容"）转换为纯字母（如"B"）
-- ========================================

-- 备份数据
CREATE TABLE IF NOT EXISTS question_backup_20251205 AS SELECT * FROM question;

-- 查看需要修复的数据
SELECT 
    id,
    SUBSTRING(question_content, 1, 30) AS 题目,
    correct_answer AS 当前答案,
    CASE 
        WHEN correct_answer LIKE '%,%' THEN
            -- 多选题：提取字母部分（如 "A. 选项1, B. 选项2" → "A,B"）
            TRIM(CONCAT_WS(',',
                IF(correct_answer LIKE '%A.%', 'A', NULL),
                IF(correct_answer LIKE '%B.%', 'B', NULL),
                IF(correct_answer LIKE '%C.%', 'C', NULL),
                IF(correct_answer LIKE '%D.%', 'D', NULL),
                IF(correct_answer LIKE '%E.%', 'E', NULL),
                IF(correct_answer LIKE '%F.%', 'F', NULL)
            ))
        WHEN correct_answer LIKE '%.%' THEN
            -- 单选题：提取字母部分（如 "B. 选项内容" → "B"）
            SUBSTRING(correct_answer, 1, 1)
        ELSE
            correct_answer
    END AS 修复后答案
FROM question
WHERE correct_answer LIKE '%. %';

-- 修复数据：将完整格式的答案转换为纯字母
-- 注意：此SQL需要手动执行，执行前请检查上面的查询结果
-- UPDATE question
-- SET correct_answer = CASE 
--     WHEN correct_answer LIKE '%,%' THEN
--         -- 多选题：提取字母部分
--         REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
--             correct_answer,
--             'A. ', 'A,'),
--             'B. ', 'B,'),
--             'C. ', 'C,'),
--             'D. ', 'D,'),
--             'E. ', 'E,'),
--             'F. ', 'F,')
--     WHEN correct_answer LIKE '%. %' THEN
--         -- 单选题：提取字母部分
--         SUBSTRING(correct_answer, 1, 1)
--     ELSE
--         correct_answer
-- END
-- WHERE correct_answer LIKE '%. %';

-- 更简单的修复方法：
UPDATE question
SET correct_answer = SUBSTRING(correct_answer, 1, LOCATE('.', correct_answer) - 1)
WHERE correct_answer LIKE '%. %'
  AND correct_answer NOT LIKE '%,%';  -- 只处理单选题

-- 多选题的修复（复杂，需要逐个处理）
-- 示例：ID 184 的题目
-- 原答案："A. 顺丰的高度官方的,B. 多少分多少多少,C. 撒地方收到"
-- 应改为："A,B,C"
UPDATE question
SET correct_answer = 'A,B,C'
WHERE id = 184;

-- 验证修复结果
SELECT 
    id,
    SUBSTRING(question_content, 1, 30) AS 题目,
    correct_answer AS 答案,
    question_type AS 题型
FROM question
WHERE id IN (37, 164, 166, 184);

-- 注意事项：
-- 1. 执行前请先备份数据库
-- 2. 执行UPDATE前先查看SELECT结果确认无误
-- 3. 如有疑问请手动检查并修复
