-- 修正历史时间：将此前按 UTC 写入的 DATETIME 字段统一调整为北京时间。
-- 重要：本脚本只能在确认历史数据整体少 8 小时后执行一次，不能重复执行。
-- 执行示例：
--   mysql -h localhost -P 3306 -u root -p12345678 financial_system < financial_system/database/sql/upgrade_20260623_timezone_shanghai.sql

SET time_zone = '+08:00';

CREATE TABLE IF NOT EXISTS `schema_migrations` (
  `version` VARCHAR(64) NOT NULL COMMENT '迁移版本号',
  `description` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '迁移说明',
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '执行时间',
  PRIMARY KEY (`version`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
  COMMENT='数据库迁移执行记录表';

SET @migration_version = _utf8mb4'20260623_timezone_shanghai' COLLATE utf8mb4_unicode_ci;
SET @should_run = (
  SELECT IF(COUNT(*) = 0, 1, 0)
  FROM `schema_migrations`
  WHERE `version` = @migration_version
);

START TRANSACTION;

UPDATE `users`
SET `created_at` = DATE_ADD(`created_at`, INTERVAL 8 HOUR),
    `updated_at` = DATE_ADD(`updated_at`, INTERVAL 8 HOUR)
WHERE @should_run = 1;

UPDATE `departments`
SET `created_at` = DATE_ADD(`created_at`, INTERVAL 8 HOUR),
    `updated_at` = DATE_ADD(`updated_at`, INTERVAL 8 HOUR)
WHERE @should_run = 1;

UPDATE `positions`
SET `created_at` = DATE_ADD(`created_at`, INTERVAL 8 HOUR),
    `updated_at` = DATE_ADD(`updated_at`, INTERVAL 8 HOUR)
WHERE @should_run = 1;

UPDATE `employees`
SET `created_at` = DATE_ADD(`created_at`, INTERVAL 8 HOUR),
    `updated_at` = DATE_ADD(`updated_at`, INTERVAL 8 HOUR)
WHERE @should_run = 1;

UPDATE `salary_profiles`
SET `created_at` = DATE_ADD(`created_at`, INTERVAL 8 HOUR),
    `updated_at` = DATE_ADD(`updated_at`, INTERVAL 8 HOUR)
WHERE @should_run = 1;

UPDATE `operation_logs`
SET `created_at` = DATE_ADD(`created_at`, INTERVAL 8 HOUR)
WHERE @should_run = 1;

UPDATE `payroll_jobs`
SET `created_at` = DATE_ADD(`created_at`, INTERVAL 8 HOUR),
    `updated_at` = DATE_ADD(`updated_at`, INTERVAL 8 HOUR)
WHERE @should_run = 1;

UPDATE `attendance_record`
SET `created_at` = DATE_ADD(`created_at`, INTERVAL 8 HOUR)
WHERE @should_run = 1;

UPDATE `leave_record`
SET `created_at` = DATE_ADD(`created_at`, INTERVAL 8 HOUR)
WHERE @should_run = 1;

UPDATE `overtime_record`
SET `created_at` = DATE_ADD(`created_at`, INTERVAL 8 HOUR)
WHERE @should_run = 1;

UPDATE `attendance_sync_jobs`
SET `created_at` = DATE_ADD(`created_at`, INTERVAL 8 HOUR),
    `updated_at` = DATE_ADD(`updated_at`, INTERVAL 8 HOUR)
WHERE @should_run = 1;

UPDATE `salary_preview`
SET `created_at` = DATE_ADD(`created_at`, INTERVAL 8 HOUR),
    `updated_at` = DATE_ADD(`updated_at`, INTERVAL 8 HOUR)
WHERE @should_run = 1;

UPDATE `monthly_payroll_runs`
SET `locked_at` = CASE WHEN `locked_at` IS NULL THEN NULL ELSE DATE_ADD(`locked_at`, INTERVAL 8 HOUR) END,
    `created_at` = DATE_ADD(`created_at`, INTERVAL 8 HOUR),
    `updated_at` = DATE_ADD(`updated_at`, INTERVAL 8 HOUR)
WHERE @should_run = 1;

UPDATE `payroll_exceptions`
SET `created_at` = DATE_ADD(`created_at`, INTERVAL 8 HOUR),
    `resolved_at` = CASE WHEN `resolved_at` IS NULL THEN NULL ELSE DATE_ADD(`resolved_at`, INTERVAL 8 HOUR) END
WHERE @should_run = 1;

UPDATE `commission_record`
SET `created_at` = DATE_ADD(`created_at`, INTERVAL 8 HOUR),
    `updated_at` = DATE_ADD(`updated_at`, INTERVAL 8 HOUR)
WHERE @should_run = 1;

UPDATE `salary_record`
SET `created_at` = DATE_ADD(`created_at`, INTERVAL 8 HOUR),
    `updated_at` = DATE_ADD(`updated_at`, INTERVAL 8 HOUR)
WHERE @should_run = 1;

UPDATE `salary_detail`
SET `created_at` = DATE_ADD(`created_at`, INTERVAL 8 HOUR)
WHERE @should_run = 1;

UPDATE `salary_config`
SET `created_at` = DATE_ADD(`created_at`, INTERVAL 8 HOUR),
    `updated_at` = DATE_ADD(`updated_at`, INTERVAL 8 HOUR)
WHERE @should_run = 1;

INSERT INTO `schema_migrations` (`version`, `description`)
SELECT @migration_version, '修正历史 UTC 时间为北京时间'
WHERE @should_run = 1;

COMMIT;
