-- ========================================
-- 数据清理：删除孤儿数据（安全版本）
-- ========================================
-- 先检查表是否存在，再执行删除
-- ========================================

-- 第1步：查询当前用户和学生班级的数据状态
SELECT 
    '用户表总数' AS item,
    COUNT(*) AS count
FROM sys_user
UNION ALL
SELECT 
    '学生类型用户' AS item,
    COUNT(*) AS count
FROM sys_user
WHERE user_type = '02' OR user_type = 'student'
UNION ALL
SELECT 
    '班级关联数' AS item,
    COUNT(*) AS count
FROM study_student_class;

-- ========================================
-- 方案1：如果没有study_student表
-- （学生信息直接在sys_user表中）
-- ========================================

-- 清理学习记录详情（通过user_id关联）
DELETE FROM study_learning_detail
WHERE student_id NOT IN (
    SELECT user_id FROM sys_user WHERE user_type IN ('02', 'student')
);

-- 清理学习记录
DELETE FROM study_learning_record
WHERE student_id NOT IN (
    SELECT user_id FROM sys_user WHERE user_type IN ('02', 'student')
);

-- 清理课程分配
DELETE FROM study_course_assignment
WHERE student_id NOT IN (
    SELECT user_id FROM sys_user WHERE user_type IN ('02', 'student')
);

-- 清理学生班级关联（重要：这是导致数量不一致的主要原因）
DELETE FROM study_student_class
WHERE student_id NOT IN (
    SELECT user_id FROM sys_user WHERE user_type IN ('02', 'student')
);

-- 清理考试记录
DELETE FROM study_exam_record
WHERE student_id NOT IN (
    SELECT user_id FROM sys_user WHERE user_type IN ('02', 'student')
);

-- 清理答题记录
DELETE FROM study_student_answer
WHERE student_id NOT IN (
    SELECT user_id FROM sys_user WHERE user_type IN ('02', 'student')
);

-- 清理变更记录
DELETE FROM study_student_change_log
WHERE student_id NOT IN (
    SELECT user_id FROM sys_user WHERE user_type IN ('02', 'student')
);

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

SELECT 
    '用户表学生数' AS type,
    COUNT(*) AS count
FROM sys_user
WHERE user_type IN ('02', 'student')
UNION ALL
SELECT 
    '班级学生数' AS type,
    COUNT(DISTINCT student_id) AS count
FROM study_student_class
UNION ALL
SELECT 
    '课程分配学生数' AS type,
    COUNT(DISTINCT student_id) AS count
FROM study_course_assignment;

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

-- ========================================
-- 如果需要更激进的清理（删除所有没有用户记录的数据）
-- ========================================

-- 查询孤儿班级关联（有班级关联但用户不存在）
SELECT 
    sc.student_id,
    COUNT(*) AS class_count,
    '用户不存在' AS status
FROM study_student_class sc
LEFT JOIN sys_user u ON sc.student_id = u.user_id
WHERE u.user_id IS NULL
GROUP BY sc.student_id;

-- 如果上面查询有结果，执行删除
DELETE FROM study_student_class
WHERE student_id IN (
    SELECT sc.student_id
    FROM study_student_class sc
    LEFT JOIN sys_user u ON sc.student_id = u.user_id
    WHERE u.user_id IS NULL
);

-- ========================================
-- 最终验证
-- ========================================

-- 确认没有孤儿数据
SELECT COUNT(*) AS orphan_class_records
FROM study_student_class sc
LEFT JOIN sys_user u ON sc.student_id = u.user_id
WHERE u.user_id IS NULL;
-- 应该返回 0

-- 确认数量一致
SELECT 
    (SELECT COUNT(*) FROM sys_user WHERE user_type IN ('02', 'student')) AS user_count,
    (SELECT COUNT(DISTINCT student_id) FROM study_student_class) AS class_student_count,
    (SELECT COUNT(DISTINCT student_id) FROM study_course_assignment) AS assignment_student_count;
-- 三个数字应该一致或接近
