-- ============================================
-- 清空用户数据和问卷，保留量表配置和管理员账号
-- 执行前请备份数据库！
-- ============================================

-- 1. 关闭外键检查（避免删除顺序问题）
SET FOREIGN_KEY_CHECKS = 0;

-- ============================================
-- 2. 清空测评相关数据
-- ============================================

-- 清空测评答案
DELETE FROM psy_assessment_answer;

-- 清空因子得分
DELETE FROM psy_factor_score;

-- 清空测评报告
DELETE FROM psy_assessment_report;

-- 清空测评记录
DELETE FROM psy_assessment;

-- ============================================
-- 3. 清空问卷相关数据
-- ============================================

-- 清空问卷答案详情
DELETE FROM psy_questionnaire_answer_detail;

-- 清空问卷答案
DELETE FROM psy_questionnaire_answer;

-- 清空问卷报告
DELETE FROM psy_questionnaire_report;

-- 清空问卷题目选项
DELETE FROM psy_questionnaire_option;

-- 清空问卷题目
DELETE FROM psy_questionnaire_item;

-- 清空问卷
DELETE FROM psy_questionnaire;

-- ============================================
-- 4. 清空用户档案和预警数据
-- ============================================

-- 清空用户心理档案
DELETE FROM psy_user_profile;

-- 清空预警记录
DELETE FROM psy_warning;

-- ============================================
-- 5. 清空测试用户（保留admin和ry）
-- ============================================

-- 删除user_id > 2的所有用户（保留admin=1, ry=2）
DELETE FROM sys_user WHERE user_id > 2;

-- ============================================
-- 6. 重新开启外键检查
-- ============================================
SET FOREIGN_KEY_CHECKS = 1;

-- ============================================
-- 验证清空结果
-- ============================================
SELECT '测评记录' as 表名, COUNT(*) as 记录数 FROM psy_assessment
UNION ALL SELECT '测评答案', COUNT(*) FROM psy_assessment_answer
UNION ALL SELECT '测评报告', COUNT(*) FROM psy_assessment_report
UNION ALL SELECT '因子得分', COUNT(*) FROM psy_factor_score
UNION ALL SELECT '问卷', COUNT(*) FROM psy_questionnaire
UNION ALL SELECT '问卷答案', COUNT(*) FROM psy_questionnaire_answer
UNION ALL SELECT '用户档案', COUNT(*) FROM psy_user_profile
UNION ALL SELECT '预警记录', COUNT(*) FROM psy_warning
UNION ALL SELECT '系统用户', COUNT(*) FROM sys_user;

-- 查看保留的数据
SELECT '保留的量表' as 说明, COUNT(*) as 数量 FROM psy_scale
UNION ALL SELECT '保留的用户', COUNT(*) FROM sys_user;
