-- 考培练系统时区数据迁移脚本 V2
-- 将所有UTC时间戳转换为北京时间（+8小时）
-- 只更新实际存在的表
-- 执行时间：2025-10-17

USE kaopeilian;

-- 设置安全模式为0，允许批量更新
SET SQL_SAFE_UPDATES = 0;

-- 1. 更新 users 表
UPDATE users 
SET 
    created_at = DATE_ADD(created_at, INTERVAL 8 HOUR),
    updated_at = DATE_ADD(updated_at, INTERVAL 8 HOUR),
    last_login_at = CASE WHEN last_login_at IS NOT NULL THEN DATE_ADD(last_login_at, INTERVAL 8 HOUR) ELSE NULL END,
    password_changed_at = CASE WHEN password_changed_at IS NOT NULL THEN DATE_ADD(password_changed_at, INTERVAL 8 HOUR) ELSE NULL END,
    deleted_at = CASE WHEN deleted_at IS NOT NULL THEN DATE_ADD(deleted_at, INTERVAL 8 HOUR) ELSE NULL END
WHERE id > 0;

-- 2. 更新 teams 表
UPDATE teams 
SET 
    created_at = DATE_ADD(created_at, INTERVAL 8 HOUR),
    updated_at = DATE_ADD(updated_at, INTERVAL 8 HOUR),
    deleted_at = CASE WHEN deleted_at IS NOT NULL THEN DATE_ADD(deleted_at, INTERVAL 8 HOUR) ELSE NULL END
WHERE id > 0;

-- 3. 更新 user_teams 表
UPDATE user_teams 
SET joined_at = DATE_ADD(joined_at, INTERVAL 8 HOUR)
WHERE user_id > 0;

-- 4. 更新 courses 表
UPDATE courses 
SET 
    created_at = DATE_ADD(created_at, INTERVAL 8 HOUR),
    updated_at = DATE_ADD(updated_at, INTERVAL 8 HOUR),
    published_at = CASE WHEN published_at IS NOT NULL THEN DATE_ADD(published_at, INTERVAL 8 HOUR) ELSE NULL END,
    broadcast_generated_at = CASE WHEN broadcast_generated_at IS NOT NULL THEN DATE_ADD(broadcast_generated_at, INTERVAL 8 HOUR) ELSE NULL END,
    deleted_at = CASE WHEN deleted_at IS NOT NULL THEN DATE_ADD(deleted_at, INTERVAL 8 HOUR) ELSE NULL END
WHERE id > 0;

-- 5. 更新 course_materials 表
UPDATE course_materials 
SET 
    created_at = DATE_ADD(created_at, INTERVAL 8 HOUR),
    updated_at = DATE_ADD(updated_at, INTERVAL 8 HOUR),
    deleted_at = CASE WHEN deleted_at IS NOT NULL THEN DATE_ADD(deleted_at, INTERVAL 8 HOUR) ELSE NULL END
WHERE id > 0;

-- 6. 更新 knowledge_points 表
UPDATE knowledge_points 
SET 
    created_at = DATE_ADD(created_at, INTERVAL 8 HOUR),
    updated_at = DATE_ADD(updated_at, INTERVAL 8 HOUR)
WHERE id > 0;

-- 7. 更新 exams 表
UPDATE exams 
SET 
    created_at = DATE_ADD(created_at, INTERVAL 8 HOUR),
    updated_at = DATE_ADD(updated_at, INTERVAL 8 HOUR),
    start_time = DATE_ADD(start_time, INTERVAL 8 HOUR),
    end_time = CASE WHEN end_time IS NOT NULL THEN DATE_ADD(end_time, INTERVAL 8 HOUR) ELSE NULL END
WHERE id > 0;

-- 8. 更新 exam_results 表
UPDATE exam_results 
SET 
    created_at = DATE_ADD(created_at, INTERVAL 8 HOUR),
    updated_at = DATE_ADD(updated_at, INTERVAL 8 HOUR)
WHERE id > 0;

-- 9. 更新 exam_mistakes 表
UPDATE exam_mistakes 
SET 
    created_at = DATE_ADD(created_at, INTERVAL 8 HOUR),
    updated_at = DATE_ADD(updated_at, INTERVAL 8 HOUR),
    mastered_at = CASE WHEN mastered_at IS NOT NULL THEN DATE_ADD(mastered_at, INTERVAL 8 HOUR) ELSE NULL END
WHERE id > 0;

-- 10. 更新 questions 表
UPDATE questions 
SET 
    created_at = DATE_ADD(created_at, INTERVAL 8 HOUR),
    updated_at = DATE_ADD(updated_at, INTERVAL 8 HOUR)
WHERE id > 0;

-- 11. 更新 training_scenes 表
UPDATE training_scenes 
SET 
    created_at = DATE_ADD(created_at, INTERVAL 8 HOUR),
    updated_at = DATE_ADD(updated_at, INTERVAL 8 HOUR),
    deleted_at = CASE WHEN deleted_at IS NOT NULL THEN DATE_ADD(deleted_at, INTERVAL 8 HOUR) ELSE NULL END
WHERE id > 0;

-- 12. 更新 training_sessions 表
UPDATE training_sessions 
SET 
    created_at = DATE_ADD(created_at, INTERVAL 8 HOUR),
    updated_at = DATE_ADD(updated_at, INTERVAL 8 HOUR),
    start_time = DATE_ADD(start_time, INTERVAL 8 HOUR),
    end_time = CASE WHEN end_time IS NOT NULL THEN DATE_ADD(end_time, INTERVAL 8 HOUR) ELSE NULL END
WHERE id > 0;

-- 13. 更新 training_messages 表
UPDATE training_messages 
SET 
    created_at = DATE_ADD(created_at, INTERVAL 8 HOUR),
    updated_at = DATE_ADD(updated_at, INTERVAL 8 HOUR)
WHERE id > 0;

-- 14. 更新 training_reports 表
UPDATE training_reports 
SET 
    created_at = DATE_ADD(created_at, INTERVAL 8 HOUR),
    updated_at = DATE_ADD(updated_at, INTERVAL 8 HOUR)
WHERE id > 0;

-- 15. 更新 positions 表
UPDATE positions 
SET 
    created_at = DATE_ADD(created_at, INTERVAL 8 HOUR),
    updated_at = DATE_ADD(updated_at, INTERVAL 8 HOUR),
    deleted_at = CASE WHEN deleted_at IS NOT NULL THEN DATE_ADD(deleted_at, INTERVAL 8 HOUR) ELSE NULL END
WHERE id > 0;

-- 16. 更新 position_members 表
UPDATE position_members 
SET 
    created_at = DATE_ADD(created_at, INTERVAL 8 HOUR),
    updated_at = DATE_ADD(updated_at, INTERVAL 8 HOUR),
    joined_at = DATE_ADD(joined_at, INTERVAL 8 HOUR),
    deleted_at = CASE WHEN deleted_at IS NOT NULL THEN DATE_ADD(deleted_at, INTERVAL 8 HOUR) ELSE NULL END
WHERE id > 0;

-- 17. 更新 position_courses 表
UPDATE position_courses 
SET 
    created_at = DATE_ADD(created_at, INTERVAL 8 HOUR),
    updated_at = DATE_ADD(updated_at, INTERVAL 8 HOUR)
WHERE id > 0;

-- 18. 更新 system_logs 表
UPDATE system_logs 
SET created_at = DATE_ADD(created_at, INTERVAL 8 HOUR)
WHERE id > 0;

-- 19. 更新 tasks 表
UPDATE tasks 
SET 
    created_at = DATE_ADD(created_at, INTERVAL 8 HOUR),
    updated_at = DATE_ADD(updated_at, INTERVAL 8 HOUR),
    deleted_at = CASE WHEN deleted_at IS NOT NULL THEN DATE_ADD(deleted_at, INTERVAL 8 HOUR) ELSE NULL END
WHERE id > 0;

-- 20. 更新 task_assignments 表
UPDATE task_assignments 
SET 
    created_at = DATE_ADD(created_at, INTERVAL 8 HOUR),
    updated_at = DATE_ADD(updated_at, INTERVAL 8 HOUR),
    assigned_at = CASE WHEN assigned_at IS NOT NULL THEN DATE_ADD(assigned_at, INTERVAL 8 HOUR) ELSE NULL END,
    completed_at = CASE WHEN completed_at IS NOT NULL THEN DATE_ADD(completed_at, INTERVAL 8 HOUR) ELSE NULL END
WHERE id > 0;

-- 21. 更新 practice_scenes 表
UPDATE practice_scenes 
SET 
    created_at = DATE_ADD(created_at, INTERVAL 8 HOUR),
    updated_at = DATE_ADD(updated_at, INTERVAL 8 HOUR)
WHERE id > 0;

-- 22. 更新 practice_sessions 表
UPDATE practice_sessions 
SET 
    created_at = DATE_ADD(created_at, INTERVAL 8 HOUR),
    updated_at = DATE_ADD(updated_at, INTERVAL 8 HOUR),
    start_time = CASE WHEN start_time IS NOT NULL THEN DATE_ADD(start_time, INTERVAL 8 HOUR) ELSE NULL END,
    end_time = CASE WHEN end_time IS NOT NULL THEN DATE_ADD(end_time, INTERVAL 8 HOUR) ELSE NULL END
WHERE id > 0;

-- 23. 更新 practice_dialogues 表
UPDATE practice_dialogues 
SET 
    created_at = DATE_ADD(created_at, INTERVAL 8 HOUR),
    updated_at = DATE_ADD(updated_at, INTERVAL 8 HOUR)
WHERE id > 0;

-- 24. 更新 practice_reports 表
UPDATE practice_reports 
SET 
    created_at = DATE_ADD(created_at, INTERVAL 8 HOUR),
    updated_at = DATE_ADD(updated_at, INTERVAL 8 HOUR)
WHERE id > 0;

-- 25. 更新 ability_assessments 表
UPDATE ability_assessments 
SET 
    created_at = DATE_ADD(created_at, INTERVAL 8 HOUR),
    updated_at = DATE_ADD(updated_at, INTERVAL 8 HOUR),
    assessed_at = CASE WHEN assessed_at IS NOT NULL THEN DATE_ADD(assessed_at, INTERVAL 8 HOUR) ELSE NULL END
WHERE id > 0;

-- 26. 更新 growth_paths 表
UPDATE growth_paths 
SET 
    created_at = DATE_ADD(created_at, INTERVAL 8 HOUR),
    updated_at = DATE_ADD(updated_at, INTERVAL 8 HOUR)
WHERE id > 0;

-- 27. 更新 course_exam_settings 表
UPDATE course_exam_settings 
SET 
    created_at = DATE_ADD(created_at, INTERVAL 8 HOUR),
    updated_at = DATE_ADD(updated_at, INTERVAL 8 HOUR)
WHERE id > 0;

-- 恢复安全模式
SET SQL_SAFE_UPDATES = 1;

-- 验证：查看更新后的时间
SELECT '============================================' as '';
SELECT '时区迁移完成！以下是部分数据验证：' as message;
SELECT '============================================' as '';
SELECT '用户表示例：' as '';
SELECT id, username, created_at, updated_at FROM users LIMIT 3;
SELECT '课程表示例：' as '';
SELECT id, title, created_at, updated_at FROM courses LIMIT 3;
SELECT '当前数据库时间：' as '';
SELECT NOW() as current_beijing_time, UTC_TIMESTAMP() as utc_time;

