-- 为统计分析页面生成轻医美场景的模拟数据
-- 使用方法: mysql -h localhost -u root -p'nj861021' kaopeilian < seed_statistics_demo_data.sql

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ============================================
-- 1. 获取用户ID（使用admin用户）
-- ============================================
SET @user_id = (SELECT id FROM users WHERE username = 'admin' LIMIT 1);
SET @user_id = IFNULL(@user_id, 1);

SELECT CONCAT('使用用户ID: ', @user_id) AS info;

-- ============================================
-- 2. 为课程添加知识点（如果不存在）
-- ============================================
-- 皮肤生理学基础
SET @course_id_1 = (SELECT id FROM courses WHERE name LIKE '%皮肤生理学%' LIMIT 1);
INSERT INTO knowledge_points (course_id, material_id, name, description, type, source, is_deleted, created_at, updated_at)
SELECT 
    @course_id_1,
    NULL,
    kp.name,
    CONCAT('核心知识点：', kp.name),
    '核心概念',
    0,
    FALSE,
    NOW(),
    NOW()
FROM (
    SELECT '皮肤结构与层次' AS name UNION ALL
    SELECT '皮肤类型与特征' UNION ALL
    SELECT '皮肤屏障功能' UNION ALL
    SELECT '皮肤老化机制' UNION ALL
    SELECT '皮肤色素形成' UNION ALL
    SELECT '皮肤水分平衡'
) AS kp
WHERE @course_id_1 IS NOT NULL
AND NOT EXISTS (
    SELECT 1 FROM knowledge_points 
    WHERE course_id = @course_id_1 AND name = kp.name AND is_deleted = FALSE
);

-- 医美产品知识与应用
SET @course_id_2 = (SELECT id FROM courses WHERE name LIKE '%医美产品%' LIMIT 1);
INSERT INTO knowledge_points (course_id, material_id, name, description, type, source, is_deleted, created_at, updated_at)
SELECT 
    @course_id_2,
    NULL,
    kp.name,
    CONCAT('核心知识点：', kp.name),
    '核心概念',
    0,
    FALSE,
    NOW(),
    NOW()
FROM (
    SELECT '透明质酸的应用' AS name UNION ALL
    SELECT '肉毒素的作用机理' UNION ALL
    SELECT '光子嫩肤原理' UNION ALL
    SELECT '果酸焕肤技术' UNION ALL
    SELECT '维生素C美白' UNION ALL
    SELECT '胶原蛋白补充'
) AS kp
WHERE @course_id_2 IS NOT NULL
AND NOT EXISTS (
    SELECT 1 FROM knowledge_points 
    WHERE course_id = @course_id_2 AND name = kp.name AND is_deleted = FALSE
);

SELECT '✓ 知识点创建完成' AS info;

-- ============================================
-- 3. 生成考试记录（过去60天，呈现进步趋势）
-- ============================================
-- 删除旧的演示数据
DELETE FROM exam_mistakes WHERE user_id = @user_id;
DELETE FROM exams WHERE user_id = @user_id;

-- 生成考试记录
INSERT INTO exams (
    user_id, course_id, exam_name, question_count, total_score, pass_score,
    start_time, end_time, duration_minutes, round1_score, score, is_passed, status,
    created_at, updated_at
)
SELECT
    @user_id,
    c.id,
    CONCAT(c.name, '测试'),
    10,
    100.0,
    60.0,
    DATE_SUB(NOW(), INTERVAL FLOOR(RAND() * 60) DAY) - INTERVAL FLOOR(RAND() * 86400) SECOND,
    DATE_SUB(NOW(), INTERVAL FLOOR(RAND() * 60) DAY) - INTERVAL FLOOR(RAND() * 86400) SECOND + INTERVAL (15 + FLOOR(RAND() * 30)) MINUTE,
    15 + FLOOR(RAND() * 30),
    -- 分数呈现进步趋势：早期60-75分，后期75-95分
    60 + (RAND() * 15) + (30 * RAND()),
    60 + (RAND() * 15) + (30 * RAND()),
    TRUE,
    'submitted',
    DATE_SUB(NOW(), INTERVAL FLOOR(RAND() * 60) DAY),
    DATE_SUB(NOW(), INTERVAL FLOOR(RAND() * 60) DAY)
FROM courses c
CROSS JOIN (
    SELECT 1 AS n UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5
    UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10
    UNION ALL SELECT 11 UNION ALL SELECT 12 UNION ALL SELECT 13 UNION ALL SELECT 14 UNION ALL SELECT 15
    UNION ALL SELECT 16 UNION ALL SELECT 17 UNION ALL SELECT 18 UNION ALL SELECT 19 UNION ALL SELECT 20
) AS numbers
WHERE c.is_deleted = FALSE AND c.status = 'published'
LIMIT 50;

-- 更新is_passed状态
UPDATE exams SET is_passed = (round1_score >= pass_score) WHERE user_id = @user_id;

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

-- ============================================
-- 4. 生成错题记录
-- ============================================
-- 为每个考试生成错题（根据分数决定错题数）
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() * 5), 'single_choice', 'multiple_choice', 'true_false', 'fill_blank', 'essay'),
    e.created_at,
    e.created_at
FROM exams e
CROSS JOIN knowledge_points kp
WHERE e.user_id = @user_id
  AND kp.course_id = e.course_id
  AND kp.is_deleted = FALSE
  AND RAND() < (100 - e.round1_score) / 100  -- 分数越低，错题概率越高
LIMIT 200;

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

-- ============================================
-- 5. 生成陪练会话记录
-- ============================================
-- 删除旧的陪练记录
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, scene_type,
    start_time, end_time, duration_seconds, turns, status, is_deleted,
    created_at, updated_at
)
SELECT
    CONCAT('session_', @user_id, '_', UNIX_TIMESTAMP(start_dt)),
    @user_id,
    1 + FLOOR(RAND() * 5),
    ELT(1 + FLOOR(RAND() * 5), '电话销售', '面对面咨询', '客户投诉处理', '售后服务', '产品介绍'),
    ELT(1 + FLOOR(RAND() * 5), 'phone', 'face', 'complaint', 'after-sales', 'product-intro'),
    start_dt,
    DATE_ADD(start_dt, INTERVAL (600 + FLOOR(RAND() * 1200)) SECOND),
    600 + FLOOR(RAND() * 1200),
    10 + FLOOR(RAND() * 20),
    'completed',
    FALSE,
    start_dt,
    start_dt
FROM (
    SELECT DATE_SUB(NOW(), INTERVAL FLOOR(RAND() * 60) DAY) - INTERVAL FLOOR(RAND() * 86400) SECOND AS start_dt
    FROM (
        SELECT 1 AS n UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5
        UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9 UNION ALL SELECT 10
        UNION ALL SELECT 11 UNION ALL SELECT 12 UNION ALL SELECT 13 UNION ALL SELECT 14 UNION ALL SELECT 15
    ) AS numbers
) AS dates;

SELECT CONCAT('✓ 创建了 ', COUNT(*), ' 条陪练会话记录') AS info 
FROM practice_sessions WHERE user_id = @user_id;

-- ============================================
-- 6. 统计汇总
-- ============================================
SELECT '========================================' AS '';
SELECT '✅ 数据生成完成！' AS '';
SELECT '========================================' AS '';

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

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

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

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

SELECT '========================================' AS '';
SELECT '现在可以访问统计分析页面查看数据：' AS '';
SELECT 'http://localhost:5173/analysis/statistics' AS '';
SELECT '========================================' AS '';

SET FOREIGN_KEY_CHECKS = 1;

