SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;

-- 使用 utf8mb4 连接字符集，保证中文注释、中文名称和 emoji 在导入时不被降级。

CREATE TABLE IF NOT EXISTS admin_data_scopes (
  id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT COMMENT '主键 ID',
  role_id BIGINT UNSIGNED NOT NULL COMMENT '角色 ID',
  scope_type VARCHAR(40) NOT NULL COMMENT '范围类型',
  scope_value VARCHAR(120) NOT NULL COMMENT '范围值',
  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
  UNIQUE KEY uk_admin_data_scopes_role_scope (role_id, scope_type, scope_value),
  INDEX idx_admin_data_scopes_role_id (role_id),
  INDEX idx_admin_data_scopes_scope (scope_type, scope_value)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='后台数据范围表';

CREATE TABLE IF NOT EXISTS admin_jobs (
  id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT COMMENT '主键 ID',
  type VARCHAR(80) NOT NULL COMMENT '类型',
  status VARCHAR(32) NOT NULL COMMENT '业务状态',
  payload_json TEXT COMMENT '业务负载 JSON 快照',
  result_json TEXT COMMENT '结果 JSON 配置或快照',
  error TEXT COMMENT '错误',
  created_by BIGINT UNSIGNED NOT NULL COMMENT '创建人',
  created_by_name VARCHAR(64) NOT NULL COMMENT '创建人名称',
  started_at_ms BIGINT NULL COMMENT '开始时间，UTC epoch ms',
  finished_at_ms BIGINT NULL COMMENT '结束时间，UTC epoch ms',
  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
  INDEX idx_admin_jobs_type (type),
  INDEX idx_admin_jobs_status (status),
  INDEX idx_admin_jobs_created_by (created_by),
  INDEX idx_admin_jobs_created_by_name (created_by_name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='后台任务表';
