SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;


-- 使用 utf8mb4 连接字符集，保证中文注释、中文名称和 emoji 在导入时不被降级。
CREATE DATABASE IF NOT EXISTS hyapp_game DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

USE hyapp_game;

CREATE TABLE IF NOT EXISTS game_platforms (
  app_code VARCHAR(32) NOT NULL COMMENT '应用编码，用于多租户隔离',
  platform_code VARCHAR(64) NOT NULL COMMENT '平台编码',
  platform_name VARCHAR(128) NOT NULL COMMENT '平台名称',
  status VARCHAR(32) NOT NULL COMMENT '业务状态',
  api_base_url VARCHAR(512) NOT NULL DEFAULT '' COMMENT 'API 基准 URL',
  adapter_type VARCHAR(64) NOT NULL DEFAULT 'demo' COMMENT '适配器类型',
  callback_secret_ciphertext VARCHAR(1024) NOT NULL DEFAULT '' COMMENT '回调密钥密文',
  callback_ip_whitelist JSON NULL COMMENT '回调IP白名单',
  adapter_config JSON NULL COMMENT '适配器配置',
  sort_order INT NOT NULL DEFAULT 0 COMMENT '排序权重，数值越小越靠前',
  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
  PRIMARY KEY(app_code, platform_code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='游戏平台表';

CREATE TABLE IF NOT EXISTS game_catalog (
  app_code VARCHAR(32) NOT NULL COMMENT '应用编码，用于多租户隔离',
  game_id VARCHAR(96) NOT NULL COMMENT '游戏 ID',
  platform_code VARCHAR(64) NOT NULL COMMENT '平台编码',
  provider_game_id VARCHAR(128) NOT NULL COMMENT '提供方游戏 ID',
  game_name VARCHAR(128) NOT NULL COMMENT '游戏名称',
  category VARCHAR(64) NOT NULL DEFAULT '' COMMENT '分类',
  icon_url VARCHAR(512) NOT NULL DEFAULT '' COMMENT '图标 URL',
  cover_url VARCHAR(512) NOT NULL DEFAULT '' COMMENT '封面 URL',
  launch_mode VARCHAR(32) NOT NULL DEFAULT 'h5_popup' COMMENT '启动模式',
  orientation VARCHAR(32) NOT NULL DEFAULT 'portrait' COMMENT '屏幕方向',
  min_coin BIGINT NOT NULL DEFAULT 0 COMMENT '最小金币',
  status VARCHAR(32) NOT NULL COMMENT '业务状态',
  sort_order INT NOT NULL DEFAULT 0 COMMENT '排序权重，数值越小越靠前',
  tags JSON NULL COMMENT '标签',
  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
  PRIMARY KEY(app_code, game_id),
  UNIQUE KEY uk_game_provider(app_code, platform_code, provider_game_id),
  KEY idx_game_platform_status(app_code, platform_code, status, sort_order)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='游戏目录表';

CREATE TABLE IF NOT EXISTS game_display_rules (
  app_code VARCHAR(32) NOT NULL COMMENT '应用编码，用于多租户隔离',
  rule_id VARCHAR(96) NOT NULL COMMENT '规则 ID',
  game_id VARCHAR(96) NOT NULL COMMENT '游戏 ID',
  scene VARCHAR(64) NOT NULL COMMENT '场景',
  region_id BIGINT NOT NULL DEFAULT 0 COMMENT '区域 ID',
  language VARCHAR(32) NOT NULL DEFAULT '' COMMENT '语言',
  platform VARCHAR(16) NOT NULL DEFAULT '' COMMENT '平台',
  visible TINYINT(1) NOT NULL DEFAULT 1 COMMENT '是否可见',
  enabled TINYINT(1) NOT NULL DEFAULT 1 COMMENT '启用',
  sort_order INT NOT NULL DEFAULT 0 COMMENT '排序权重，数值越小越靠前',
  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
  PRIMARY KEY(app_code, rule_id),
  KEY idx_game_display(app_code, scene, region_id, visible, sort_order)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='游戏展示规则表';

CREATE TABLE IF NOT EXISTS game_launch_sessions (
  app_code VARCHAR(32) NOT NULL COMMENT '应用编码，用于多租户隔离',
  session_id VARCHAR(96) NOT NULL COMMENT '会话 ID',
  user_id BIGINT NOT NULL COMMENT '用户 ID',
  display_user_id VARCHAR(32) NOT NULL COMMENT '展示用户 ID',
  room_id VARCHAR(96) NOT NULL DEFAULT '' COMMENT '房间 ID',
  region_id BIGINT NOT NULL DEFAULT 0 COMMENT '用户启动游戏时的区域 ID',
  scene VARCHAR(64) NOT NULL COMMENT '场景',
  platform_code VARCHAR(64) NOT NULL COMMENT '平台编码',
  game_id VARCHAR(96) NOT NULL COMMENT '游戏 ID',
  provider_game_id VARCHAR(128) NOT NULL COMMENT '提供方游戏 ID',
  launch_token_hash VARCHAR(128) NOT NULL COMMENT '启动令牌哈希值',
  status VARCHAR(32) NOT NULL COMMENT '业务状态',
  expires_at_ms BIGINT NOT 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',
  PRIMARY KEY(app_code, session_id),
  KEY idx_game_launch_user(app_code, user_id, created_at_ms),
  KEY idx_game_launch_token(app_code, launch_token_hash)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='游戏启动会话表';

CREATE TABLE IF NOT EXISTS game_orders (
  app_code VARCHAR(32) NOT NULL COMMENT '应用编码，用于多租户隔离',
  order_id VARCHAR(96) NOT NULL COMMENT '订单 ID',
  platform_code VARCHAR(64) NOT NULL COMMENT '平台编码',
  provider_order_id VARCHAR(160) NOT NULL COMMENT '提供方订单 ID',
  provider_round_id VARCHAR(160) NOT NULL DEFAULT '' COMMENT '提供方轮次 ID',
  game_id VARCHAR(96) NOT NULL COMMENT '游戏 ID',
  provider_game_id VARCHAR(128) NOT NULL COMMENT '提供方游戏 ID',
  user_id BIGINT NOT NULL COMMENT '用户 ID',
  room_id VARCHAR(96) NOT NULL DEFAULT '' COMMENT '房间 ID',
  region_id BIGINT NOT NULL DEFAULT 0 COMMENT '用户启动游戏时的区域 ID',
  op_type VARCHAR(32) NOT NULL COMMENT 'op类型',
  coin_amount BIGINT NOT NULL COMMENT '金币数量',
  status VARCHAR(32) NOT NULL COMMENT '业务状态',
  wallet_transaction_id VARCHAR(96) NOT NULL DEFAULT '' COMMENT '钱包交易 ID',
  wallet_balance_after BIGINT NOT NULL DEFAULT 0 COMMENT '钱包余额后',
  request_hash VARCHAR(128) NOT NULL COMMENT '请求哈希值',
  raw_payload_ref VARCHAR(256) NOT NULL DEFAULT '' COMMENT '原始负载外部引用',
  failure_code VARCHAR(64) NOT NULL DEFAULT '' COMMENT '失败编码',
  failure_message VARCHAR(256) NOT NULL DEFAULT '' COMMENT '失败消息',
  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
  PRIMARY KEY(app_code, order_id),
  UNIQUE KEY uk_game_provider_order(app_code, platform_code, provider_order_id),
  KEY idx_game_order_user_time(app_code, user_id, created_at_ms),
  KEY idx_game_order_round(app_code, platform_code, provider_round_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='游戏钱包订单表';

CREATE TABLE IF NOT EXISTS game_callback_logs (
  app_code VARCHAR(32) NOT NULL COMMENT '应用编码，用于多租户隔离',
  callback_id VARCHAR(96) NOT NULL COMMENT '回调 ID',
  platform_code VARCHAR(64) NOT NULL COMMENT '平台编码',
  operation VARCHAR(64) NOT NULL COMMENT '操作',
  request_id VARCHAR(96) NOT NULL DEFAULT '' COMMENT '链路请求 ID，用于排查问题',
  provider_request_id VARCHAR(160) NOT NULL DEFAULT '' COMMENT '提供方请求 ID',
  signature_valid TINYINT(1) NOT NULL DEFAULT 0 COMMENT '签名有效',
  request_hash VARCHAR(128) NOT NULL COMMENT '请求哈希值',
  response_code VARCHAR(64) NOT NULL DEFAULT '' COMMENT '响应编码',
  response_body_hash VARCHAR(128) NOT NULL DEFAULT '' COMMENT '响应正文哈希值',
  status VARCHAR(32) NOT NULL COMMENT '业务状态',
  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
  PRIMARY KEY(app_code, callback_id),
  KEY idx_game_callback_platform_time(app_code, platform_code, created_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='游戏回调日志表';

CREATE TABLE IF NOT EXISTS game_repair_orders (
  app_code VARCHAR(32) NOT NULL COMMENT '应用编码，用于多租户隔离',
  repair_id VARCHAR(96) NOT NULL COMMENT '修复 ID',
  platform_code VARCHAR(64) NOT NULL COMMENT '平台编码',
  provider_order_id VARCHAR(160) NOT NULL COMMENT '提供方订单 ID',
  reason VARCHAR(128) NOT NULL COMMENT '原因',
  status VARCHAR(32) NOT NULL COMMENT '业务状态',
  operator_admin_id BIGINT NOT NULL DEFAULT 0 COMMENT '操作管理员 ID',
  attempts INT NOT NULL DEFAULT 0 COMMENT '尝试次数',
  next_run_at_ms BIGINT NOT NULL DEFAULT 0 COMMENT '下一个运行时间，UTC epoch ms',
  last_error_code VARCHAR(64) NOT NULL DEFAULT '' COMMENT '最后错误编码',
  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
  PRIMARY KEY(app_code, repair_id),
  UNIQUE KEY uk_game_repair_provider_order(app_code, platform_code, provider_order_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='游戏修复订单表';

CREATE TABLE IF NOT EXISTS game_level_event_outbox (
  app_code VARCHAR(32) NOT NULL COMMENT '应用编码，用于多租户隔离',
  event_id VARCHAR(160) NOT NULL COMMENT '事件幂等 ID',
  order_id VARCHAR(96) NOT NULL COMMENT '订单 ID',
  user_id BIGINT NOT NULL COMMENT '用户 ID',
  game_id VARCHAR(96) NOT NULL COMMENT '游戏 ID',
  provider_order_id VARCHAR(160) NOT NULL COMMENT '提供方订单 ID',
  provider_round_id VARCHAR(160) NOT NULL DEFAULT '' COMMENT '提供方轮次 ID',
  coin_amount BIGINT NOT NULL COMMENT '金币数量',
  wallet_transaction_id VARCHAR(96) NOT NULL COMMENT '钱包交易 ID',
  payload_json JSON NULL COMMENT '业务负载 JSON 快照',
  status VARCHAR(32) NOT NULL COMMENT '业务状态',
  attempt_count INT NOT NULL DEFAULT 0 COMMENT '尝试次数',
  next_retry_at_ms BIGINT NOT NULL DEFAULT 0 COMMENT '下一次重试时间，UTC epoch ms',
  locked_by VARCHAR(128) NOT NULL DEFAULT '' COMMENT '当前锁定 worker',
  locked_until_ms BIGINT NOT NULL DEFAULT 0 COMMENT '锁定截止时间，UTC epoch ms',
  failure_reason VARCHAR(255) NOT NULL DEFAULT '' COMMENT '失败原因',
  occurred_at_ms BIGINT NOT 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',
  PRIMARY KEY(app_code, event_id),
  UNIQUE KEY uk_game_level_event_order(app_code, order_id),
  KEY idx_game_level_event_pending(app_code, status, next_retry_at_ms, created_at_ms),
  KEY idx_game_level_event_claim(app_code, status, locked_until_ms, next_retry_at_ms, created_at_ms, event_id),
  KEY idx_game_level_event_retention(app_code, status, updated_at_ms, event_id),
  KEY idx_game_level_event_user(app_code, user_id, created_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='游戏等级事件 outbox 表';

CREATE TABLE IF NOT EXISTS game_outbox (
  app_code VARCHAR(32) NOT NULL COMMENT '应用编码，用于多租户隔离',
  event_id VARCHAR(160) NOT NULL COMMENT '事件幂等 ID',
  event_type VARCHAR(64) NOT NULL COMMENT '事件类型',
  order_id VARCHAR(96) NOT NULL COMMENT '订单 ID',
  user_id BIGINT NOT NULL COMMENT '用户 ID',
  platform_code VARCHAR(64) NOT NULL COMMENT '平台编码',
  game_id VARCHAR(96) NOT NULL COMMENT '游戏 ID',
  op_type VARCHAR(32) NOT NULL COMMENT '游戏改账方向',
  coin_amount BIGINT NOT NULL COMMENT '金币数量',
  payload_json JSON NOT NULL COMMENT '业务负载 JSON 快照',
  status VARCHAR(32) NOT NULL COMMENT '业务状态',
  worker_id VARCHAR(128) NOT NULL DEFAULT '' COMMENT 'MQ 投递 worker ID',
  lock_until_ms BIGINT NOT NULL DEFAULT 0 COMMENT 'MQ 投递锁过期时间，UTC epoch ms',
  retry_count INT NOT NULL DEFAULT 0 COMMENT '重试次数',
  next_retry_at_ms BIGINT NOT NULL DEFAULT 0 COMMENT '下一次 MQ 投递重试时间，UTC epoch ms',
  last_error VARCHAR(512) NOT NULL DEFAULT '' COMMENT '最后一次失败原因',
  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
  PRIMARY KEY(app_code, event_id),
  KEY idx_game_outbox_status_created(app_code, status, next_retry_at_ms, created_at_ms, event_id),
  KEY idx_game_outbox_lock(app_code, status, lock_until_ms, created_at_ms, event_id),
  KEY idx_game_outbox_retention(app_code, status, updated_at_ms, event_id),
  KEY idx_game_outbox_order(app_code, order_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='游戏统计事件 outbox 表';

INSERT INTO game_platforms (
  app_code, platform_code, platform_name, status, api_base_url, sort_order, created_at_ms, updated_at_ms
) VALUES (
  'lalu', 'demo', 'Demo Games', 'active', 'https://game.example.local/h5', 10, 0, 0
) ON DUPLICATE KEY UPDATE platform_name = VALUES(platform_name);

INSERT INTO game_catalog (
  app_code, game_id, platform_code, provider_game_id, game_name, category, icon_url, cover_url,
  launch_mode, orientation, min_coin, status, sort_order, tags, created_at_ms, updated_at_ms
) VALUES (
  'lalu', 'demo_rocket_001', 'demo', 'rocket_001', 'Rocket', 'casual',
  'https://cdn.example.com/game/rocket.png', 'https://cdn.example.com/game/rocket-cover.png',
  'h5_popup', 'portrait', 100, 'active', 10, JSON_ARRAY('demo','voice_room'), 0, 0
) ON DUPLICATE KEY UPDATE game_name = VALUES(game_name);

INSERT INTO game_display_rules (
  app_code, rule_id, game_id, scene, region_id, language, platform, visible, enabled, sort_order, created_at_ms, updated_at_ms
) VALUES (
  'lalu', 'demo_rocket_voice_default', 'demo_rocket_001', 'voice_room', 0, '', '', 1, 1, 10, 0, 0
) ON DUPLICATE KEY UPDATE visible = VALUES(visible), enabled = VALUES(enabled);
