-- ==============================================
-- 为 user_id=6 (nurse_001) 生成统计分析数据
-- ==============================================

USE kaopeilian;

SET @user_id = 6;
SET @user_name = (SELECT username FROM users WHERE id = @user_id);

SELECT CONCAT('开始为用户 ', @user_name, ' (ID: ', @user_id, ') 生成统计数据') AS '';

-- ============================================
-- 1. 生成考试记录 (50条)
-- ============================================
-- 保留原有的3条记录，再生成47条
INSERT INTO exams (
    user_id, course_id, exam_name, question_count, pass_score,
    questions, start_time, end_time, status, duration_minutes,
    round1_score, is_passed, created_at, updated_at
)
SELECT 
    @user_id,
    c.id,
    CONCAT(c.name, '练习测试_', DATE_FORMAT(exam_dt, '%m月%d日')),
    10,
    60,
    '[]',
    exam_dt,
    DATE_ADD(exam_dt, INTERVAL FLOOR(20 + RAND() * 40) MINUTE),
    'completed',
    FLOOR(20 + RAND() * 40),  -- 20-60分钟
    FLOOR(55 + RAND() * 45),  -- 55-100分
    1,
    exam_dt,
    exam_dt
FROM (
    SELECT DATE_SUB(NOW(), INTERVAL days DAY) + INTERVAL hours HOUR + INTERVAL minutes MINUTE as exam_dt
    FROM (
        SELECT 0 as days UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL
        SELECT 5 UNION ALL SELECT 7 UNION ALL SELECT 9 UNION ALL SELECT 12 UNION ALL SELECT 15
    ) d,
    (SELECT 9 as hours UNION ALL SELECT 10 UNION ALL SELECT 14 UNION ALL SELECT 15 UNION ALL SELECT 16) h,
    (SELECT 0 as minutes UNION ALL SELECT 15 UNION ALL SELECT 30 UNION ALL SELECT 45) m
) exam_dates
CROSS JOIN (
    SELECT id, name FROM courses WHERE is_deleted = FALSE LIMIT 3
) c
LIMIT 47;

-- 更新 is_passed 标志
UPDATE exams SET is_passed = (round1_score >= pass_score) WHERE user_id = @user_id AND is_passed = 0;

SELECT CONCAT('✓ 当前考试记录总数: ', COUNT(*), ' 条') AS '' 
FROM exams WHERE user_id = @user_id;

-- ============================================
-- 2. 生成错题记录 (250条)
-- ============================================
-- 删除旧的错题记录，重新生成
DELETE FROM exam_mistakes WHERE user_id = @user_id;

-- 为每个考试生成3-6个错题
INSERT INTO exam_mistakes (
    user_id, exam_id, question_id, knowledge_point_id,
    question_content, correct_answer, user_answer, question_type,
    created_at, updated_at
)
SELECT
    e.user_id,
    e.id,
    NULL,
    kp.id,
    CONCAT('关于"', kp.name, '"的',
           ELT(1 + FLOOR(RAND() * 3), '概念理解', '实际应用', '综合分析'), '问题'),
    CONCAT('正确答案：', kp.name, '的标准解释'),
    CONCAT('错误理解：', '学员对', kp.name, '的误解'),
    ELT(1 + FLOOR(RAND() * 5), 'single_choice', 'multiple_choice', 'true_false', 'fill_blank', 'essay'),
    e.start_time,
    e.start_time
FROM exams e
CROSS JOIN (
    SELECT 1 AS mistake_num UNION ALL SELECT 2 UNION ALL SELECT 3 
    UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6
) AS mistake_counts
INNER JOIN knowledge_points kp ON kp.course_id = e.course_id
WHERE e.user_id = @user_id
  AND kp.is_deleted = FALSE
  AND RAND() < 0.8
ORDER BY RAND()
LIMIT 250;

SELECT CONCAT('✓ 错题记录: ', COUNT(*), ' 条') AS ''
FROM exam_mistakes WHERE user_id = @user_id;

-- ============================================
-- 3. 生成陪练记录 (20条)
-- ============================================
-- 删除旧的陪练记录
DELETE FROM practice_dialogues WHERE session_id IN (
    SELECT session_id FROM practice_sessions WHERE user_id = @user_id
);
DELETE FROM practice_reports WHERE session_id IN (
    SELECT session_id FROM practice_sessions WHERE user_id = @user_id
);
DELETE FROM practice_sessions WHERE user_id = @user_id;

-- 生成新的陪练记录
INSERT INTO practice_sessions (
    session_id, user_id, scene_id, scene_name,
    duration_seconds, status,
    start_time, end_time, created_at, updated_at
)
SELECT
    CONCAT('session_', @user_id, '_', UNIX_TIMESTAMP(practice_dt)),
    @user_id,
    1 + FLOOR(RAND() * 5),
    ELT(1 + FLOOR(RAND() * 5), '电话销售陪练', '面对面咨询陪练', '客户投诉处理陪练', '售后服务陪练', '产品介绍陪练'),
    FLOOR(600 + RAND() * 3000),  -- 600-3600秒 (10-60分钟)
    'completed',
    practice_dt,
    DATE_ADD(practice_dt, INTERVAL FLOOR(10 + RAND() * 50) MINUTE),
    practice_dt,
    practice_dt
FROM (
    SELECT DATE_SUB(NOW(), INTERVAL days DAY) + INTERVAL hours HOUR as practice_dt
    FROM (
        SELECT 0 as days UNION ALL SELECT 1 UNION ALL SELECT 3 UNION ALL SELECT 5 UNION ALL
        SELECT 7 UNION ALL SELECT 10 UNION ALL SELECT 12 UNION ALL SELECT 15 UNION ALL SELECT 20
    ) d,
    (SELECT 10 as hours UNION ALL SELECT 14 UNION ALL SELECT 16) h
) practice_dates
LIMIT 20;

SELECT CONCAT('✓ 陪练记录: ', COUNT(*), ' 条') AS ''
FROM practice_sessions WHERE user_id = @user_id;

-- ============================================
-- 4. 统计概览
-- ============================================
SELECT '========================================' AS '';
SELECT '数据生成完成' AS '';
SELECT '========================================' AS '';

SELECT CONCAT('用户: ', username, ' (', role, ')') AS ''
FROM users WHERE id = @user_id;

SELECT CONCAT('✓ 考试记录: ', COUNT(*), ' 条') AS ''
FROM exams WHERE user_id = @user_id;

SELECT CONCAT('✓ 错题记录: ', COUNT(*), ' 条') AS ''
FROM exam_mistakes WHERE user_id = @user_id;

SELECT CONCAT('✓ 陪练记录: ', COUNT(*), ' 条') AS ''
FROM practice_sessions WHERE user_id = @user_id;

SELECT CONCAT('✓ 知识点数量: ', COUNT(*), ' 个') AS ''
FROM knowledge_points WHERE is_deleted = FALSE;

-- 成绩分布
SELECT '========================================' AS '';
SELECT '成绩分布情况' AS '';
SELECT '========================================' AS '';
SELECT 
    CONCAT('优秀(90-100): ', SUM(CASE WHEN round1_score >= 90 THEN 1 ELSE 0 END), ' 场') AS '',
    CONCAT('良好(80-89): ', SUM(CASE WHEN round1_score >= 80 AND round1_score < 90 THEN 1 ELSE 0 END), ' 场') AS '',
    CONCAT('中等(70-79): ', SUM(CASE WHEN round1_score >= 70 AND round1_score < 80 THEN 1 ELSE 0 END), ' 场') AS '',
    CONCAT('及格(60-69): ', SUM(CASE WHEN round1_score >= 60 AND round1_score < 70 THEN 1 ELSE 0 END), ' 场') AS '',
    CONCAT('不及格(<60): ', SUM(CASE WHEN round1_score < 60 THEN 1 ELSE 0 END), ' 场') AS ''
FROM exams WHERE user_id = @user_id;

SELECT '✓ 数据生成成功，可以刷新统计分析页面查看' AS '';

