-- ============================================
-- 系统增强功能测试数据
-- 场景：轻医美连锁品牌员工培训系统
-- ============================================

USE kaopeilian;

-- ============================================
-- 1. 任务中心数据
-- ============================================

-- 创建任务1：产品知识培训
INSERT INTO tasks (title, description, priority, status, creator_id, deadline, requirements, progress, created_at, updated_at, is_deleted)
VALUES (
    '新品"水光焕肤"项目培训',
    '学习新上线的水光焕肤项目知识，包括产品成分、功效、适用人群、操作流程等，为春季营销活动做准备',
    'high',
    'ongoing',
    1,
    DATE_ADD(NOW(), INTERVAL 7 DAY),
    '{"must_complete": true, "allow_retake": true}',
    45,
    NOW(),
    NOW(),
    FALSE
);

-- 创建任务2：服务礼仪提升
INSERT INTO tasks (title, description, priority, status, creator_id, deadline, requirements, progress, created_at, updated_at, is_deleted)
VALUES (
    '客户服务礼仪强化训练',
    '学习高端服务礼仪规范，提升客户接待质量，打造品牌专业形象',
    'medium',
    'ongoing',
    1,
    DATE_ADD(NOW(), INTERVAL 14 DAY),
    '{"must_complete": true, "allow_retake": false}',
    70,
    DATE_SUB(NOW(), INTERVAL 3 DAY),
    NOW(),
    FALSE
);

-- 创建任务3：销售技巧月度考核
INSERT INTO tasks (title, description, priority, status, creator_id, deadline, requirements, progress, created_at, updated_at, is_deleted)
VALUES (
    '3月销售技巧月度考核',
    '针对咨询接待、需求挖掘、方案推荐、异议处理等核心销售技能进行综合考核',
    'high',
    'completed',
    1,
    DATE_SUB(NOW(), INTERVAL 2 DAY),
    '{"must_complete": true, "allow_retake": false}',
    100,
    DATE_SUB(NOW(), INTERVAL 10 DAY),
    NOW(),
    FALSE
);

-- 创建任务4：安全操作规范学习
INSERT INTO tasks (title, description, priority, status, creator_id, deadline, requirements, progress, created_at, updated_at, is_deleted)
VALUES (
    '医疗美容安全操作规范培训',
    '学习国家医疗美容行业安全操作规范，确保服务过程零事故',
    'high',
    'ongoing',
    1,
    DATE_ADD(NOW(), INTERVAL 5 DAY),
    '{"must_complete": true, "allow_retake": false}',
    30,
    DATE_SUB(NOW(), INTERVAL 1 DAY),
    NOW(),
    FALSE
);

-- 任务关联课程（假设课程ID 1-4存在）
INSERT IGNORE INTO task_courses (task_id, course_id, created_at)
VALUES 
    (1, 1, NOW()),
    (1, 2, NOW()),
    (2, 3, NOW()),
    (3, 1, NOW()),
    (3, 2, NOW()),
    (4, 4, NOW());

-- 任务分配（假设用户ID 2-6存在）
INSERT INTO task_assignments (task_id, user_id, status, progress, completed_at, created_at, updated_at)
VALUES 
    -- 任务1分配给5个员工
    (1, 2, 'completed', 100, DATE_SUB(NOW(), INTERVAL 1 DAY), DATE_SUB(NOW(), INTERVAL 3 DAY), NOW()),
    (1, 3, 'in_progress', 60, NULL, DATE_SUB(NOW(), INTERVAL 3 DAY), NOW()),
    (1, 4, 'in_progress', 40, NULL, DATE_SUB(NOW(), INTERVAL 3 DAY), NOW()),
    (1, 5, 'not_started', 0, NULL, DATE_SUB(NOW(), INTERVAL 3 DAY), NOW()),
    (1, 6, 'not_started', 0, NULL, DATE_SUB(NOW(), INTERVAL 3 DAY), NOW()),
    
    -- 任务2分配给4个员工
    (2, 2, 'completed', 100, DATE_SUB(NOW(), INTERVAL 2 DAY), DATE_SUB(NOW(), INTERVAL 5 DAY), NOW()),
    (2, 3, 'completed', 100, DATE_SUB(NOW(), INTERVAL 2 DAY), DATE_SUB(NOW(), INTERVAL 5 DAY), NOW()),
    (2, 4, 'in_progress', 80, NULL, DATE_SUB(NOW(), INTERVAL 5 DAY), NOW()),
    (2, 5, 'not_started', 20, NULL, DATE_SUB(NOW(), INTERVAL 5 DAY), NOW()),
    
    -- 任务3已完成
    (3, 2, 'completed', 100, DATE_SUB(NOW(), INTERVAL 3 DAY), DATE_SUB(NOW(), INTERVAL 12 DAY), NOW()),
    (3, 3, 'completed', 100, DATE_SUB(NOW(), INTERVAL 3 DAY), DATE_SUB(NOW(), INTERVAL 12 DAY), NOW()),
    (3, 4, 'completed', 100, DATE_SUB(NOW(), INTERVAL 3 DAY), DATE_SUB(NOW(), INTERVAL 12 DAY), NOW()),
    
    -- 任务4分配给6个员工
    (4, 2, 'in_progress', 50, NULL, DATE_SUB(NOW(), INTERVAL 1 DAY), NOW()),
    (4, 3, 'in_progress', 50, NULL, DATE_SUB(NOW(), INTERVAL 1 DAY), NOW()),
    (4, 4, 'in_progress', 40, NULL, DATE_SUB(NOW(), INTERVAL 1 DAY), NOW()),
    (4, 5, 'not_started', 0, NULL, DATE_SUB(NOW(), INTERVAL 1 DAY), NOW()),
    (4, 6, 'not_started', 0, NULL, DATE_SUB(NOW(), INTERVAL 1 DAY), NOW()),
    (4, 7, 'not_started', 0, NULL, DATE_SUB(NOW(), INTERVAL 1 DAY), NOW());

-- ============================================
-- 2. 系统日志数据（展示关键操作记录）
-- ============================================

-- 登录日志
INSERT INTO system_logs (level, type, message, user_id, user, ip, path, method, user_agent, created_at)
VALUES 
    ('INFO', 'security', '用户 admin 登录成功', 1, 'admin', '192.168.1.100', '/api/v1/auth/login', 'POST', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)', DATE_SUB(NOW(), INTERVAL 2 HOUR)),
    ('INFO', 'security', '用户 manager01 登录成功', 2, 'manager01', '192.168.1.101', '/api/v1/auth/login', 'POST', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)', DATE_SUB(NOW(), INTERVAL 1 HOUR)),
    ('WARNING', 'security', '用户 unknown_user 登录失败：密码错误', NULL, 'unknown_user', '192.168.1.200', '/api/v1/auth/login', 'POST', 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_0)', DATE_SUB(NOW(), INTERVAL 30 MINUTE)),
    ('INFO', 'security', '用户 trainee01 登录成功', 3, 'trainee01', '192.168.1.102', '/api/v1/auth/login', 'POST', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)', DATE_SUB(NOW(), INTERVAL 20 MINUTE));

-- 用户管理日志
INSERT INTO system_logs (level, type, message, user_id, user, ip, path, method, user_agent, created_at)
VALUES 
    ('INFO', 'user', '管理员 admin 创建用户: 李美美', 1, 'admin', '192.168.1.100', '/api/v1/users/', 'POST', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)', DATE_SUB(NOW(), INTERVAL 5 DAY)),
    ('INFO', 'user', '管理员 admin 创建用户: 王欣欣', 1, 'admin', '192.168.1.100', '/api/v1/users/', 'POST', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)', DATE_SUB(NOW(), INTERVAL 4 DAY));

-- 课程管理日志
INSERT INTO system_logs (level, type, message, user_id, user, ip, path, method, user_agent, created_at)
VALUES 
    ('INFO', 'api', '创建课程: 水光焕肤项目全解析', 1, 'admin', '192.168.1.100', '/api/v1/courses', 'POST', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)', DATE_SUB(NOW(), INTERVAL 7 DAY)),
    ('INFO', 'api', '创建课程: 高端服务礼仪培训', 1, 'admin', '192.168.1.100', '/api/v1/courses', 'POST', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)', DATE_SUB(NOW(), INTERVAL 6 DAY)),
    ('INFO', 'api', '删除课程: 过期培训课程-2024Q1', 1, 'admin', '192.168.1.100', '/api/v1/courses/15', 'DELETE', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)', DATE_SUB(NOW(), INTERVAL 3 DAY));

-- 考试操作日志
INSERT INTO system_logs (level, type, message, user_id, user, ip, path, method, user_agent, created_at)
VALUES 
    ('INFO', 'api', '用户 trainee01 开始考试（课程ID: 1）', 3, 'trainee01', '192.168.1.102', '/api/v1/exams/start', 'POST', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)', DATE_SUB(NOW(), INTERVAL 2 HOUR)),
    ('INFO', 'api', '用户 trainee01 提交考试（考试ID: 25，得分: 92）', 3, 'trainee01', '192.168.1.102', '/api/v1/exams/submit', 'POST', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)', DATE_SUB(NOW(), INTERVAL 1 HOUR)),
    ('INFO', 'api', '用户 trainee02 开始考试（课程ID: 2）', 4, 'trainee02', '192.168.1.103', '/api/v1/exams/start', 'POST', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)', DATE_SUB(NOW(), INTERVAL 90 MINUTE)),
    ('INFO', 'api', '用户 trainee02 提交考试（考试ID: 26，得分: 88）', 4, 'trainee02', '192.168.1.103', '/api/v1/exams/submit', 'POST', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)', DATE_SUB(NOW(), INTERVAL 45 MINUTE));

-- 任务管理日志
INSERT INTO system_logs (level, type, message, user_id, user, ip, path, method, user_agent, created_at)
VALUES 
    ('INFO', 'api', '创建任务: 新品"水光焕肤"项目培训', 1, 'admin', '192.168.1.100', '/api/v1/manager/tasks', 'POST', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)', DATE_SUB(NOW(), INTERVAL 3 DAY)),
    ('INFO', 'api', '创建任务: 客户服务礼仪强化训练', 1, 'admin', '192.168.1.100', '/api/v1/manager/tasks', 'POST', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)', DATE_SUB(NOW(), INTERVAL 3 DAY)),
    ('INFO', 'api', '删除任务: 过期培训任务-2024Q1', 1, 'admin', '192.168.1.100', '/api/v1/manager/tasks/99', 'DELETE', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)', DATE_SUB(NOW(), INTERVAL 1 DAY));

-- API调用日志
INSERT INTO system_logs (level, type, message, user_id, user, ip, path, method, user_agent, created_at)
VALUES 
    ('INFO', 'api', '用户 manager01 查询学员成绩列表', 2, 'manager01', '192.168.1.101', '/api/v1/manager/student-scores', 'GET', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)', DATE_SUB(NOW(), INTERVAL 30 MINUTE)),
    ('INFO', 'api', '用户 manager01 导出考试成绩报表', 2, 'manager01', '192.168.1.101', '/api/v1/manager/export/scores', 'GET', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)', DATE_SUB(NOW(), INTERVAL 25 MINUTE));

-- 错误日志
INSERT INTO system_logs (level, type, message, user_id, user, ip, path, method, user_agent, created_at)
VALUES 
    ('ERROR', 'error', '数据库连接超时：MySQL connection timeout', NULL, 'system', NULL, NULL, NULL, NULL, DATE_SUB(NOW(), INTERVAL 12 HOUR)),
    ('WARNING', 'error', 'API调用频率超限：用户 trainee03 在1分钟内调用接口50次', 5, 'trainee03', '192.168.1.104', '/api/v1/courses', 'GET', 'Mozilla/5.0', DATE_SUB(NOW(), INTERVAL 6 HOUR));

-- ============================================
-- 3. 更新课程学员统计数据
-- ============================================

-- 更新现有课程的学员数（模拟多个学员参与）
UPDATE courses SET student_count = 8 WHERE id = 1;
UPDATE courses SET student_count = 6 WHERE id = 2;
UPDATE courses SET student_count = 5 WHERE id = 3;
UPDATE courses SET student_count = 4 WHERE id = 4;

-- ============================================
-- 4. 添加一些符合轻医美场景的错题掌握数据
-- ============================================

-- 更新现有错题的掌握状态
UPDATE exam_mistakes 
SET mastery_status = 'mastered', 
    difficulty = 'easy',
    wrong_count = 1,
    mastered_at = DATE_SUB(NOW(), INTERVAL 1 DAY)
WHERE id IN (1, 2, 3)
LIMIT 3;

UPDATE exam_mistakes 
SET mastery_status = 'unmastered', 
    difficulty = 'medium',
    wrong_count = 2
WHERE id IN (4, 5)
LIMIT 2;

UPDATE exam_mistakes 
SET mastery_status = 'unmastered', 
    difficulty = 'hard',
    wrong_count = 3
WHERE id IN (6, 7)
LIMIT 2;

-- ============================================
-- 执行统计
-- ============================================

SELECT '=== 数据插入完成 ===' AS Status;
SELECT '任务数量:', COUNT(*) FROM tasks WHERE is_deleted = FALSE;
SELECT '任务分配数量:', COUNT(*) FROM task_assignments;
SELECT '系统日志数量:', COUNT(*) FROM system_logs;
SELECT '课程学员统计:', SUM(student_count) AS total_students FROM courses;

