-- 1️⃣ 先查看要删除的用户（确认范围）
SELECT user_id, user_name, nick_name, register_type, create_time 
FROM sys_user 
WHERE user_id > 200 AND del_flag = '0'
ORDER BY user_id;

-- 2️⃣ 查看影响范围（统计）
SELECT 
    '用户数' AS 项目,
    COUNT(*) AS 数量
FROM sys_user 
WHERE user_id > 200 AND del_flag = '0'
UNION ALL
SELECT 
    '学习记录数',
    COUNT(*)
FROM learning_record 
WHERE student_id > 200
UNION ALL
SELECT 
    '学习详情数',
    COUNT(*)
FROM learning_detail 
WHERE student_id > 200
UNION ALL
SELECT 
    '考试成绩数',
    COUNT(*)
FROM score 
WHERE student_id > 200
UNION ALL
SELECT 
    '班级关联数',
    COUNT(*)
FROM student_class 
WHERE student_id > 200;

-- ============================================================
-- ⚠️ 确认无误后，执行以下删除操作
-- ============================================================

-- 3️⃣ 删除关联数据（按顺序）

-- 删除学习详情
DELETE FROM learning_detail 
WHERE student_id > 200;

-- 删除学习记录
DELETE FROM learning_record 
WHERE student_id > 200;

-- 删除考试成绩
DELETE FROM score 
WHERE student_id > 200;

-- 删除班级关联
DELETE FROM student_class 
WHERE student_id > 200;

-- 删除用户角色关联
DELETE FROM sys_user_role 
WHERE user_id > 200;

-- 删除课程分配（如果用户是学生）
DELETE FROM course_assignment 
WHERE student_id > 200;

-- 4️⃣ 最后删除用户表记录
DELETE FROM sys_user 
WHERE user_id > 200 AND del_flag = '0';

-- 5️⃣ 验证删除结果
SELECT 
    '剩余用户数' AS 验证项,
    COUNT(*) AS 数量
FROM sys_user 
WHERE del_flag = '0';

-- ============================================================
-- 💡 如果想使用逻辑删除（推荐，可恢复），使用以下语句代替
-- ============================================================

/*
-- 逻辑删除用户（设置 del_flag = '2'）
UPDATE sys_user 
SET del_flag = '2', 
    update_time = NOW()
WHERE user_id > 200 AND del_flag = '0';

-- 验证逻辑删除
SELECT user_id, user_name, nick_name, del_flag 
FROM sys_user 
WHERE user_id > 200;
*/
