-- ========================================
-- 数据清理：删除孤儿学生数据
-- ========================================
-- 问题：删除用户时没有级联删除学生相关数据
-- 导致：课程分配统计的学生数 ≠ 用户管理的学生数
-- 解决：清理所有没有对应用户的学生数据
-- ========================================

-- 1. 查询孤儿学生数据（有学生记录但用户已删除）
SELECT 
    s.student_id,
    s.student_name,
    s.user_id,
    '用户已删除' AS status
FROM study_student s
LEFT JOIN sys_user u ON s.user_id = u.user_id
WHERE u.user_id IS NULL;

-- 统计孤儿数据数量
SELECT COUNT(*) AS orphan_count
FROM study_student s
LEFT JOIN sys_user u ON s.user_id = u.user_id
WHERE u.user_id IS NULL;

-- ========================================
-- 清理步骤（按依赖顺序删除）
-- ========================================

-- 第1步：删除学习记录详情（study_learning_detail）
DELETE FROM study_learning_detail
WHERE student_id IN (
    SELECT s.student_id
    FROM study_student s
    LEFT JOIN sys_user u ON s.user_id = u.user_id
    WHERE u.user_id IS NULL
);

-- 第2步：删除学习记录（study_learning_record）
DELETE FROM study_learning_record
WHERE student_id IN (
    SELECT s.student_id
    FROM study_student s
    LEFT JOIN sys_user u ON s.user_id = u.user_id
    WHERE u.user_id IS NULL
);

-- 第3步：删除课程分配（study_course_assignment）
DELETE FROM study_course_assignment
WHERE student_id IN (
    SELECT s.student_id
    FROM study_student s
    LEFT JOIN sys_user u ON s.user_id = u.user_id
    WHERE u.user_id IS NULL
);

-- 第4步：删除学生班级关联（study_student_class）
DELETE FROM study_student_class
WHERE student_id IN (
    SELECT s.student_id
    FROM study_student s
    LEFT JOIN sys_user u ON s.user_id = u.user_id
    WHERE u.user_id IS NULL
);

-- 第5步：删除考试记录（study_exam_record）
DELETE FROM study_exam_record
WHERE student_id IN (
    SELECT s.student_id
    FROM study_student s
    LEFT JOIN sys_user u ON s.user_id = u.user_id
    WHERE u.user_id IS NULL
);

-- 第6步：删除答题记录（study_student_answer）
DELETE FROM study_student_answer
WHERE student_id IN (
    SELECT s.student_id
    FROM study_student s
    LEFT JOIN sys_user u ON s.user_id = u.user_id
    WHERE u.user_id IS NULL
);

-- 第7步：删除学生变更记录（study_student_change_log）
DELETE FROM study_student_change_log
WHERE student_id IN (
    SELECT s.student_id
    FROM study_student s
    LEFT JOIN sys_user u ON s.user_id = u.user_id
    WHERE u.user_id IS NULL
);

-- 第8步：删除语音评测记录（study_voice_evaluation）如果有
DELETE FROM study_voice_evaluation
WHERE student_id IN (
    SELECT s.student_id
    FROM study_student s
    LEFT JOIN sys_user u ON s.user_id = u.user_id
    WHERE u.user_id IS NULL
);

-- 第9步：最后删除学生信息（study_student）
DELETE FROM study_student
WHERE user_id IN (
    SELECT s.user_id
    FROM study_student s
    LEFT JOIN sys_user u ON s.user_id = u.user_id
    WHERE u.user_id IS NULL
);

-- ========================================
-- 验证清理结果
-- ========================================

-- 再次查询孤儿学生（应该为空）
SELECT COUNT(*) AS remaining_orphans
FROM study_student s
LEFT JOIN sys_user u ON s.user_id = u.user_id
WHERE u.user_id IS NULL;

-- 查询当前学生数量
SELECT 
    '用户表学生数' AS type,
    COUNT(*) AS count
FROM sys_user
WHERE user_type = 'student' OR user_type = '02'
UNION ALL
SELECT 
    '学生表数量' AS type,
    COUNT(*) AS count
FROM study_student;

-- ========================================
-- 使用说明
-- ========================================
-- 1. 先执行前面的SELECT查询，确认要删除的数据
-- 2. 备份数据库（重要！）
-- 3. 按顺序执行DELETE语句
-- 4. 执行验证查询，确认清理成功
-- 5. 重新编译并部署后端（包含级联删除修复）
-- ========================================
