-- ====================================
-- 清理孤儿用户数据脚本
-- 功能：删除那些引用了不存在的用户ID的数据
-- 使用场景：当删除sys_user后，清理关联表中的残留数据
-- ====================================

-- 1. 查看孤儿数据统计（执行清理前先查看）
-- ====================================

-- 1.1 查看班级学生关联表中的孤儿数据
SELECT '班级学生关联表(study_student_class)孤儿数据' AS 检查项, COUNT(*) AS 孤儿数据条数
FROM study_student_class ssc
WHERE NOT EXISTS (
    SELECT 1 FROM sys_user u WHERE u.user_id = ssc.user_id
);

-- 1.2 查看学习记录表中的孤儿数据
SELECT '学习记录表(study_learning_record)孤儿数据' AS 检查项, COUNT(*) AS 孤儿数据条数
FROM study_learning_record slr
WHERE NOT EXISTS (
    SELECT 1 FROM sys_user u WHERE u.user_id = slr.student_id
);

-- 1.3 查看学习明细表中的孤儿数据
SELECT '学习明细表(study_learning_detail)孤儿数据' AS 检查项, COUNT(*) AS 孤儿数据条数
FROM study_learning_detail sld
WHERE NOT EXISTS (
    SELECT 1 FROM sys_user u WHERE u.user_id = sld.student_id
);

-- 1.4 查看语音评测表中的孤儿数据
SELECT '语音评测表(study_voice_score)孤儿数据' AS 检查项, COUNT(*) AS 孤儿数据条数
FROM study_voice_score svs
WHERE svs.student_id IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM sys_user u WHERE u.user_id = svs.student_id
);

-- ====================================
-- 2. 执行清理（请先执行上面的查询确认后再执行清理）
-- ====================================

-- 2.1 清理班级学生关联表
DELETE FROM study_student_class
WHERE NOT EXISTS (
    SELECT 1 FROM sys_user u WHERE u.user_id = study_student_class.user_id
);

-- 2.2 清理学习记录表
DELETE FROM study_learning_record
WHERE NOT EXISTS (
    SELECT 1 FROM sys_user u WHERE u.user_id = study_learning_record.student_id
);

-- 2.3 清理学习明细表
DELETE FROM study_learning_detail
WHERE NOT EXISTS (
    SELECT 1 FROM sys_user u WHERE u.user_id = study_learning_detail.student_id
);

-- 2.4 清理语音评测表
DELETE FROM study_voice_score
WHERE student_id IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM sys_user u WHERE u.user_id = study_voice_score.student_id
);

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

-- 3.1 再次统计孤儿数据（应该为0）
SELECT '班级学生关联表' AS 表名, COUNT(*) AS 剩余孤儿数据
FROM study_student_class ssc
WHERE NOT EXISTS (SELECT 1 FROM sys_user u WHERE u.user_id = ssc.user_id)
UNION ALL
SELECT '学习记录表', COUNT(*)
FROM study_learning_record slr
WHERE NOT EXISTS (SELECT 1 FROM sys_user u WHERE u.user_id = slr.student_id)
UNION ALL
SELECT '学习明细表', COUNT(*)
FROM study_learning_detail sld
WHERE NOT EXISTS (SELECT 1 FROM sys_user u WHERE u.user_id = sld.student_id)
UNION ALL
SELECT '语音评测表', COUNT(*)
FROM study_voice_score svs
WHERE svs.student_id IS NOT NULL
  AND NOT EXISTS (SELECT 1 FROM sys_user u WHERE u.user_id = svs.student_id);

-- 3.2 查看当前有效用户统计
SELECT 
    '总用户数' AS 统计项,
    COUNT(*) AS 数量
FROM sys_user
WHERE user_id != 1  -- 排除管理员
UNION ALL
SELECT 
    '学生数（按userName数字开头）',
    COUNT(*)
FROM sys_user
WHERE user_name REGEXP '^[0-9]' AND user_id != 1
UNION ALL
SELECT 
    '班级关联学生数',
    COUNT(DISTINCT user_id)
FROM study_student_class;

-- ====================================
-- 使用说明：
-- 1. 先执行第1部分，查看有多少孤儿数据
-- 2. 确认无误后，执行第2部分进行清理
-- 3. 最后执行第3部分，验证清理结果
-- 
-- 注意事项：
-- - 本脚本会永久删除数据，执行前请备份数据库！
-- - 建议在测试环境先验证
-- ====================================
