-- ========================================
-- 清理孤儿数据 - 立即执行
-- ========================================
-- 问题：4194条班级关联记录的学生已被删除
-- 影响：课程分配学生数 ≠ 用户管理学生数
-- 解决：删除所有孤儿数据
-- ========================================

-- 第1步：查看要删除的数据（确认）
SELECT 
    '孤儿班级关联' AS type,
    COUNT(*) AS count
FROM student_class sc
LEFT JOIN sys_user u ON sc.student_id = u.user_id
WHERE u.user_id IS NULL

UNION ALL

SELECT 
    '孤儿课程分配' AS type,
    COUNT(*) AS count
FROM course_assignment ca
LEFT JOIN sys_user u ON ca.student_id = u.user_id
WHERE u.user_id IS NULL

UNION ALL

SELECT 
    '孤儿学习记录' AS type,
    COUNT(*) AS count
FROM learning_record lr
LEFT JOIN sys_user u ON lr.student_id = u.user_id
WHERE u.user_id IS NULL;

-- ========================================
-- 第2步：执行清理（按依赖顺序）
-- ========================================

-- 2.1 删除孤儿学习记录详情
DELETE FROM learning_detail
WHERE student_id IN (
    SELECT student_id FROM (
        SELECT ld.student_id
        FROM learning_detail ld
        LEFT JOIN sys_user u ON ld.student_id = u.user_id
        WHERE u.user_id IS NULL
    ) AS orphan_details
);

-- 2.2 删除孤儿学习记录
DELETE FROM learning_record
WHERE student_id IN (
    SELECT student_id FROM (
        SELECT lr.student_id
        FROM learning_record lr
        LEFT JOIN sys_user u ON lr.student_id = u.user_id
        WHERE u.user_id IS NULL
    ) AS orphan_records
);

-- 2.3 删除孤儿课程分配
DELETE FROM course_assignment
WHERE student_id IN (
    SELECT student_id FROM (
        SELECT ca.student_id
        FROM course_assignment ca
        LEFT JOIN sys_user u ON ca.student_id = u.user_id
        WHERE u.user_id IS NULL
    ) AS orphan_assignments
);

-- 2.4 删除孤儿学生班级关联（重要！）
DELETE FROM student_class
WHERE student_id IN (
    SELECT student_id FROM (
        SELECT sc.student_id
        FROM student_class sc
        LEFT JOIN sys_user u ON sc.student_id = u.user_id
        WHERE u.user_id IS NULL
    ) AS orphan_classes
);

-- 2.5 删除孤儿考试记录
DELETE FROM exam_record
WHERE student_id IN (
    SELECT student_id FROM (
        SELECT er.student_id
        FROM exam_record er
        LEFT JOIN sys_user u ON er.student_id = u.user_id
        WHERE u.user_id IS NULL
    ) AS orphan_exams
);

-- 2.6 删除孤儿答题记录
DELETE FROM student_answer
WHERE student_id IN (
    SELECT student_id FROM (
        SELECT sa.student_id
        FROM student_answer sa
        LEFT JOIN sys_user u ON sa.student_id = u.user_id
        WHERE u.user_id IS NULL
    ) AS orphan_answers
);

-- 2.7 删除孤儿学生变更记录
DELETE FROM study_student_change_log
WHERE student_id IN (
    SELECT student_id FROM (
        SELECT sc.student_id
        FROM study_student_change_log sc
        LEFT JOIN sys_user u ON sc.student_id = u.user_id
        WHERE u.user_id IS NULL
    ) AS orphan_changes
);

-- ========================================
-- 第3步：验证结果
-- ========================================

-- 再次查询孤儿数据（应该全为0）
SELECT 
    '孤儿班级关联' AS type,
    COUNT(*) AS remaining
FROM student_class sc
LEFT JOIN sys_user u ON sc.student_id = u.user_id
WHERE u.user_id IS NULL

UNION ALL

SELECT 
    '孤儿课程分配' AS type,
    COUNT(*) AS remaining
FROM course_assignment ca
LEFT JOIN sys_user u ON ca.student_id = u.user_id
WHERE u.user_id IS NULL

UNION ALL

SELECT 
    '孤儿学习记录' AS type,
    COUNT(*) AS remaining
FROM learning_record lr
LEFT JOIN sys_user u ON lr.student_id = u.user_id
WHERE u.user_id IS NULL;

-- 应该全部显示 0

-- ========================================
-- 第4步：统计清理结果
-- ========================================

SELECT 
    '用户表学生数' AS type,
    COUNT(*) AS count
FROM sys_user
WHERE user_type IN ('00', '01', '02', 'student')

UNION ALL

SELECT 
    '班级学生数' AS type,
    COUNT(DISTINCT student_id) AS count
FROM student_class

UNION ALL

SELECT 
    '课程分配学生数' AS type,
    COUNT(DISTINCT student_id) AS count
FROM course_assignment;

-- 三个数字应该一致或接近
