-- ====================================================
-- 统一题库和考试题目格式，确保数据一致性
-- ====================================================

-- 问题：从题库导入题目后显示没有题目
-- 原因：数据格式不一致或字段为空

-- ====================================================
-- 第1步：检查考试题目
-- ====================================================

-- 查看考试和题目数量
SELECT 
    e.id AS 考试ID,
    e.exam_name AS 考试名称,
    e.question_count AS 记录题数,
    COUNT(q.id) AS 实际题数,
    CASE 
        WHEN e.question_count = COUNT(q.id) THEN '✓ 一致'
        WHEN e.question_count > COUNT(q.id) THEN '❌ 记录比实际多'
        WHEN e.question_count < COUNT(q.id) THEN '❌ 实际比记录多'
    END AS 状态
FROM study_exam e
LEFT JOIN study_question q ON e.id = q.exam_id
GROUP BY e.id, e.exam_name, e.question_count
ORDER BY e.create_time DESC
LIMIT 10;

-- 查看题目详情
SELECT 
    q.id AS 题目ID,
    q.exam_id AS 考试ID,
    e.exam_name AS 考试名称,
    q.question_type AS 题型,
    LEFT(q.question_content, 30) AS 题目内容,
    q.options AS 选项JSON,
    q.correct_answer AS 正确答案,
    q.score AS 分值,
    q.source_type AS 来源,
    q.sort_order AS 排序
FROM study_question q
LEFT JOIN study_exam e ON q.exam_id = e.id
WHERE e.exam_name LIKE '%Test%'  -- 根据考试名称过滤
ORDER BY q.exam_id, q.sort_order;


-- ====================================================
-- 第2步：检查数据完整性问题
-- ====================================================

-- 检查缺失必填字段的题目
SELECT 
    '考试题目' AS 来源,
    q.id,
    q.exam_id,
    CASE 
        WHEN q.question_type IS NULL OR q.question_type = '' THEN '❌ 题型为空'
        WHEN q.question_content IS NULL OR q.question_content = '' THEN '❌ 题干为空'
        WHEN q.correct_answer IS NULL OR q.correct_answer = '' THEN '❌ 答案为空'
        WHEN q.score IS NULL OR q.score = 0 THEN '❌ 分值为空或0'
        WHEN q.options IS NULL OR q.options = '' THEN '⚠️ 选项为空'
        ELSE '✓ 完整'
    END AS 问题
FROM study_question q
WHERE q.question_type IS NULL OR q.question_type = ''
   OR q.question_content IS NULL OR q.question_content = ''
   OR q.correct_answer IS NULL OR q.correct_answer = ''
   OR q.score IS NULL OR q.score = 0
ORDER BY q.exam_id, q.id;

-- 检查题库题目
SELECT 
    '题库题目' AS 来源,
    i.id,
    i.bank_id,
    CASE 
        WHEN i.question_type IS NULL OR i.question_type = '' THEN '❌ 题型为空'
        WHEN i.question_content IS NULL OR i.question_content = '' THEN '❌ 题干为空'
        WHEN i.correct_answer IS NULL OR i.correct_answer = '' THEN '❌ 答案为空'
        WHEN i.score IS NULL OR i.score = 0 THEN '❌ 分值为空或0'
        WHEN i.options IS NULL OR i.options = '' THEN '⚠️ 选项为空'
        ELSE '✓ 完整'
    END AS 问题
FROM study_question_bank_item i
WHERE i.question_type IS NULL OR i.question_type = ''
   OR i.question_content IS NULL OR i.question_content = ''
   OR i.correct_answer IS NULL OR i.correct_answer = ''
   OR i.score IS NULL OR i.score = 0
ORDER BY i.bank_id, i.id;


-- ====================================================
-- 第3步：检查options字段格式
-- ====================================================

-- 查看选项格式（应该是JSON数组格式）
SELECT 
    '考试题目' AS 来源,
    id,
    question_type,
    LEFT(question_content, 30) AS 题目,
    options,
    CASE 
        WHEN options IS NULL THEN '❌ NULL'
        WHEN options = '' THEN '❌ 空字符串'
        WHEN options LIKE '[%]' THEN '✓ JSON数组'
        WHEN options LIKE '{%}' THEN '⚠️ JSON对象'
        ELSE '❌ 其他格式'
    END AS 格式检查
FROM study_question
WHERE question_type IN ('single', 'multiple')
  AND (options IS NULL OR options = '' OR NOT (options LIKE '[%]'))
LIMIT 20;


-- ====================================================
-- 第4步：修复空数据
-- ====================================================

-- 为题型为空的题目设置默认值
-- UPDATE study_question 
-- SET question_type = 'single'
-- WHERE question_type IS NULL OR question_type = '';

-- 为分值为0或NULL的题目设置默认分值
UPDATE study_question 
SET score = 5
WHERE score IS NULL OR score = 0;

UPDATE study_question_bank_item 
SET score = 5
WHERE score IS NULL OR score = 0;

-- 为排序为空的题目设置排序
SET @row_number = 0;
UPDATE study_question q
JOIN (
    SELECT id, exam_id, (@row_number:=IF(@exam_id=exam_id, @row_number+1, 1)) AS new_order, (@exam_id:=exam_id)
    FROM study_question
    ORDER BY exam_id, IFNULL(sort_order, 999), id
) AS ranked ON q.id = ranked.id
SET q.sort_order = ranked.new_order
WHERE q.sort_order IS NULL;


-- ====================================================
-- 第5步：同步考试题量和总分
-- ====================================================

-- 重新计算每个考试的题量和总分
UPDATE study_exam e
LEFT JOIN (
    SELECT 
        exam_id,
        COUNT(*) AS actual_count,
        SUM(IFNULL(score, 0)) AS actual_score
    FROM study_question
    GROUP BY exam_id
) AS stats ON e.id = stats.exam_id
SET 
    e.question_count = IFNULL(stats.actual_count, 0),
    e.total_score = IFNULL(stats.actual_score, 0);


-- ====================================================
-- 第6步：验证修复结果
-- ====================================================

-- 查看修复后的考试信息
SELECT 
    e.id,
    e.exam_name,
    e.question_count AS 题量,
    e.total_score AS 总分,
    COUNT(q.id) AS 实际题量,
    SUM(IFNULL(q.score, 0)) AS 实际总分,
    CASE 
        WHEN e.question_count = COUNT(q.id) AND e.total_score = SUM(IFNULL(q.score, 0)) THEN '✓ 正确'
        ELSE '❌ 不一致'
    END AS 状态
FROM study_exam e
LEFT JOIN study_question q ON e.id = q.exam_id
GROUP BY e.id
ORDER BY e.create_time DESC
LIMIT 10;

-- 查看题目完整性
SELECT 
    COUNT(*) AS 总题目数,
    SUM(CASE WHEN question_type IS NOT NULL AND question_type != '' THEN 1 ELSE 0 END) AS 有题型,
    SUM(CASE WHEN question_content IS NOT NULL AND question_content != '' THEN 1 ELSE 0 END) AS 有题干,
    SUM(CASE WHEN correct_answer IS NOT NULL AND correct_answer != '' THEN 1 ELSE 0 END) AS 有答案,
    SUM(CASE WHEN score IS NOT NULL AND score > 0 THEN 1 ELSE 0 END) AS 有分值,
    SUM(CASE WHEN sort_order IS NOT NULL THEN 1 ELSE 0 END) AS 有排序
FROM study_question;


-- ====================================================
-- 第7步：标准化数据格式
-- ====================================================

-- 确保多选题答案格式为 "A,B,C" （逗号分隔，无空格）
UPDATE study_question 
SET correct_answer = REPLACE(REPLACE(correct_answer, ', ', ','), ' ,', ',')
WHERE question_type = 'multiple';

UPDATE study_question_bank_item 
SET correct_answer = REPLACE(REPLACE(correct_answer, ', ', ','), ' ,', ',')
WHERE question_type = 'multiple';

-- 确保判断题答案格式为 "正确" 或 "错误"
UPDATE study_question 
SET correct_answer = CASE 
    WHEN LOWER(correct_answer) IN ('true', 't', '1', 'yes', '对') THEN '正确'
    WHEN LOWER(correct_answer) IN ('false', 'f', '0', 'no', '错') THEN '错误'
    ELSE correct_answer
END
WHERE question_type = 'judge';

UPDATE study_question_bank_item 
SET correct_answer = CASE 
    WHEN LOWER(correct_answer) IN ('true', 't', '1', 'yes', '对') THEN '正确'
    WHEN LOWER(correct_answer) IN ('false', 'f', '0', 'no', '错') THEN '错误'
    ELSE correct_answer
END
WHERE question_type = 'judge';


-- ====================================================
-- 完成！执行说明
-- ====================================================

-- 1. 依次执行上面的检查SQL，找出问题
-- 2. 执行修复SQL（UPDATE语句）
-- 3. 执行验证SQL，确认修复成功
-- 4. 重新刷新后台页面
-- 5. 重新创建考试并导入题目测试

-- 注意：执行UPDATE前建议备份数据库！
