SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;

CREATE DATABASE IF NOT EXISTS hyapp_statistics DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

USE hyapp_statistics;

CREATE TABLE IF NOT EXISTS statistics_event_consumption (
  app_code VARCHAR(32) NOT NULL COMMENT '应用编码，用于多租户隔离',
  source VARCHAR(32) NOT NULL COMMENT '事件来源 owner service',
  event_id VARCHAR(160) NOT NULL COMMENT '事件幂等 ID',
  event_type VARCHAR(64) NOT NULL COMMENT '事件类型',
  status VARCHAR(32) NOT NULL COMMENT '消费状态',
  consumed_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, source, event_id),
  KEY idx_event_consumption_type (app_code, source, event_type, consumed_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='统计事件消费幂等表';

CREATE TABLE IF NOT EXISTS stat_app_day_country (
  app_code VARCHAR(32) NOT NULL COMMENT '应用编码',
  stat_tz VARCHAR(64) NOT NULL DEFAULT 'UTC' COMMENT '统计时区，UTC 或 Asia/Shanghai',
  stat_day DATE NOT NULL COMMENT '统计日，随 stat_tz 切换',
  country_id BIGINT NOT NULL DEFAULT 0 COMMENT '国家维度 ID，0 表示未知或全局',
  region_id BIGINT NOT NULL DEFAULT 0 COMMENT '区域维度 ID，0 表示未知或全局',
  new_users BIGINT NOT NULL DEFAULT 0 COMMENT '新增用户数',
  active_users BIGINT NOT NULL DEFAULT 0 COMMENT '当日活跃去重用户数',
  paid_users BIGINT NOT NULL DEFAULT 0 COMMENT '当日充值去重用户数',
  recharge_usd_minor BIGINT NOT NULL DEFAULT 0 COMMENT '总充值美元最小单位',
  new_user_recharge_usd_minor BIGINT NOT NULL DEFAULT 0 COMMENT '新增充值美元最小单位',
  coin_seller_recharge_usd_minor BIGINT NOT NULL DEFAULT 0 COMMENT '币商充值美元最小单位',
  mifapay_recharge_usd_minor BIGINT NOT NULL DEFAULT 0 COMMENT 'MifaPay 充值美元最小单位',
  google_recharge_usd_minor BIGINT NOT NULL DEFAULT 0 COMMENT 'Google 充值美元最小单位',
  coin_seller_transfer_coin BIGINT NOT NULL DEFAULT 0 COMMENT '币商向用户转普通金币数量',
  gift_coin_spent BIGINT NOT NULL DEFAULT 0 COMMENT '礼物消耗金币',
  lucky_gift_turnover BIGINT NOT NULL DEFAULT 0 COMMENT '幸运礼物流水',
  lucky_gift_payout BIGINT NOT NULL DEFAULT 0 COMMENT '幸运礼物返奖',
  lucky_gift_payers BIGINT NOT NULL DEFAULT 0 COMMENT '幸运礼物付费去重人数',
  game_turnover BIGINT NOT NULL DEFAULT 0 COMMENT '游戏流水',
  game_players BIGINT NOT NULL DEFAULT 0 COMMENT '游戏参与去重人数',
  game_payout BIGINT NOT NULL DEFAULT 0 COMMENT '游戏返奖',
  game_refund BIGINT NOT NULL DEFAULT 0 COMMENT '游戏退款',
  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
  PRIMARY KEY (app_code, stat_tz, stat_day, country_id, region_id),
  KEY idx_stat_app_day_region (app_code, stat_tz, stat_day, region_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='App 国家日统计聚合表';

CREATE TABLE IF NOT EXISTS stat_user_day_activity (
  app_code VARCHAR(32) NOT NULL,
  stat_tz VARCHAR(64) NOT NULL DEFAULT 'UTC' COMMENT '统计时区，UTC 或 Asia/Shanghai',
  stat_day DATE NOT NULL,
  country_id BIGINT NOT NULL DEFAULT 0,
  region_id BIGINT NOT NULL DEFAULT 0,
  user_id BIGINT NOT NULL,
  first_active_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, stat_tz, stat_day, user_id),
  KEY idx_stat_user_day_country (app_code, stat_tz, stat_day, country_id),
  KEY idx_stat_user_day_region (app_code, stat_tz, stat_day, region_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户日活跃去重表';

CREATE TABLE IF NOT EXISTS stat_user_registration (
  app_code VARCHAR(32) NOT NULL,
  stat_tz VARCHAR(64) NOT NULL DEFAULT 'UTC' COMMENT '统计时区，UTC 或 Asia/Shanghai',
  user_id BIGINT NOT NULL,
  registered_day DATE NOT NULL,
  country_id BIGINT NOT NULL DEFAULT 0,
  region_id BIGINT NOT NULL DEFAULT 0,
  registered_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, stat_tz, user_id),
  KEY idx_stat_user_registration_day (app_code, stat_tz, registered_day, country_id),
  KEY idx_stat_user_registration_region (app_code, stat_tz, registered_day, region_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户注册日留存 cohort 表';

CREATE TABLE IF NOT EXISTS stat_recharge_day_payers (
  app_code VARCHAR(32) NOT NULL,
  stat_tz VARCHAR(64) NOT NULL DEFAULT 'UTC' COMMENT '统计时区，UTC 或 Asia/Shanghai',
  stat_day DATE NOT NULL,
  country_id BIGINT NOT NULL DEFAULT 0,
  region_id BIGINT NOT NULL DEFAULT 0,
  user_id BIGINT NOT NULL,
  first_paid_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, stat_tz, stat_day, user_id),
  KEY idx_stat_recharge_payer_country (app_code, stat_tz, stat_day, country_id),
  KEY idx_stat_recharge_payer_region (app_code, stat_tz, stat_day, region_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='充值日付费用户去重表';

CREATE TABLE IF NOT EXISTS stat_lucky_gift_day_payers (
  app_code VARCHAR(32) NOT NULL,
  stat_tz VARCHAR(64) NOT NULL DEFAULT 'UTC' COMMENT '统计时区，UTC 或 Asia/Shanghai',
  stat_day DATE NOT NULL,
  country_id BIGINT NOT NULL DEFAULT 0,
  region_id BIGINT NOT NULL DEFAULT 0,
  user_id BIGINT NOT NULL,
  first_paid_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, stat_tz, stat_day, user_id),
  KEY idx_stat_lucky_payer_country (app_code, stat_tz, stat_day, country_id),
  KEY idx_stat_lucky_payer_region (app_code, stat_tz, stat_day, region_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='幸运礼物日付费去重表';

CREATE TABLE IF NOT EXISTS stat_lucky_gift_pool_day_country (
  app_code VARCHAR(32) NOT NULL COMMENT '应用编码',
  stat_tz VARCHAR(64) NOT NULL DEFAULT 'UTC' COMMENT '统计时区，UTC 或 Asia/Shanghai',
  stat_day DATE NOT NULL COMMENT '统计日，随 stat_tz 切换',
  country_id BIGINT NOT NULL DEFAULT 0 COMMENT '国家维度 ID，0 表示未知或全局',
  region_id BIGINT NOT NULL DEFAULT 0 COMMENT '区域维度 ID，0 表示未知或全局',
  pool_id VARCHAR(96) NOT NULL COMMENT '幸运礼物奖池 ID',
  turnover_coin BIGINT NOT NULL DEFAULT 0 COMMENT '奖池幸运礼物流水金币',
  payout_coin BIGINT NOT NULL DEFAULT 0 COMMENT '奖池幸运礼物返奖金币',
  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
  PRIMARY KEY (app_code, stat_tz, stat_day, country_id, region_id, pool_id),
  KEY idx_stat_lucky_pool_overview (app_code, stat_tz, stat_day, pool_id),
  KEY idx_stat_lucky_pool_region (app_code, stat_tz, stat_day, region_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='幸运礼物奖池国家日统计聚合表';

CREATE TABLE IF NOT EXISTS stat_real_room_robot_gift_day_rooms (
  app_code VARCHAR(32) NOT NULL COMMENT '应用编码',
  stat_tz VARCHAR(64) NOT NULL DEFAULT 'UTC' COMMENT '统计时区，UTC 或 Asia/Shanghai',
  stat_day DATE NOT NULL COMMENT '统计日，随 stat_tz 切换',
  country_id BIGINT NOT NULL DEFAULT 0 COMMENT '国家维度 ID，0 表示未知或全局',
  region_id BIGINT NOT NULL DEFAULT 0 COMMENT '区域维度 ID，0 表示未知或全局',
  room_id VARCHAR(96) NOT NULL COMMENT '真人房间 ID',
  normal_gift_coin BIGINT NOT NULL DEFAULT 0 COMMENT '机器人在真人房送出的普通礼物金币价值',
  lucky_gift_coin BIGINT NOT NULL DEFAULT 0 COMMENT '机器人在真人房送出的幸运礼物金币价值',
  super_lucky_gift_coin BIGINT NOT NULL DEFAULT 0 COMMENT '机器人在真人房送出的超级幸运礼物金币价值',
  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
  PRIMARY KEY (app_code, stat_tz, stat_day, country_id, region_id, room_id),
  KEY idx_stat_real_room_robot_gift_region (app_code, stat_tz, stat_day, region_id),
  KEY idx_stat_real_room_robot_gift_country (app_code, stat_tz, stat_day, country_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='真人房机器人礼物房间日统计表';

CREATE TABLE IF NOT EXISTS stat_game_day_country (
  app_code VARCHAR(32) NOT NULL,
  stat_tz VARCHAR(64) NOT NULL DEFAULT 'UTC' COMMENT '统计时区，UTC 或 Asia/Shanghai',
  stat_day DATE NOT NULL,
  country_id BIGINT NOT NULL DEFAULT 0,
  region_id BIGINT NOT NULL DEFAULT 0,
  platform_code VARCHAR(64) NOT NULL DEFAULT '',
  game_id VARCHAR(96) NOT NULL,
  turnover_coin BIGINT NOT NULL DEFAULT 0,
  payout_coin BIGINT NOT NULL DEFAULT 0,
  refund_coin BIGINT NOT NULL DEFAULT 0,
  player_count BIGINT NOT NULL DEFAULT 0,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, stat_tz, stat_day, country_id, region_id, platform_code, game_id),
  KEY idx_stat_game_day_region (app_code, stat_tz, stat_day, region_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='游戏国家日统计聚合表';

CREATE TABLE IF NOT EXISTS stat_game_day_players (
  app_code VARCHAR(32) NOT NULL,
  stat_tz VARCHAR(64) NOT NULL DEFAULT 'UTC' COMMENT '统计时区，UTC 或 Asia/Shanghai',
  stat_day DATE NOT NULL,
  country_id BIGINT NOT NULL DEFAULT 0,
  region_id BIGINT NOT NULL DEFAULT 0,
  platform_code VARCHAR(64) NOT NULL DEFAULT '',
  game_id VARCHAR(96) NOT NULL,
  user_id BIGINT NOT NULL,
  first_played_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, stat_tz, stat_day, country_id, platform_code, game_id, user_id),
  KEY idx_stat_game_player_region (app_code, stat_tz, stat_day, region_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='游戏日参与用户去重表';

CREATE TABLE IF NOT EXISTS stat_self_game_h5_event_day (
  app_code VARCHAR(32) NOT NULL,
  stat_tz VARCHAR(64) NOT NULL DEFAULT 'UTC' COMMENT '统计时区，UTC 或 Asia/Shanghai',
  stat_day DATE NOT NULL,
  country_id BIGINT NOT NULL DEFAULT 0,
  region_id BIGINT NOT NULL DEFAULT 0,
  game_id VARCHAR(96) NOT NULL,
  event_name VARCHAR(64) NOT NULL,
  language VARCHAR(16) NOT NULL DEFAULT '',
  client_version VARCHAR(64) NOT NULL DEFAULT '',
  h5_version VARCHAR(64) NOT NULL DEFAULT '',
  entry_source VARCHAR(64) NOT NULL DEFAULT '',
  event_count BIGINT NOT NULL DEFAULT 0,
  user_count BIGINT NOT NULL DEFAULT 0,
  success_count BIGINT NOT NULL DEFAULT 0,
  error_count BIGINT NOT NULL DEFAULT 0,
  duration_ms_sum BIGINT NOT NULL DEFAULT 0,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, stat_tz, stat_day, country_id, region_id, game_id, event_name, language, client_version, h5_version, entry_source),
  KEY idx_stat_self_game_h5_event_name (app_code, stat_tz, stat_day, game_id, event_name),
  KEY idx_stat_self_game_h5_users_day (app_code, stat_tz, stat_day, region_id, game_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='自研游戏 H5 事件日统计表';

CREATE TABLE IF NOT EXISTS stat_self_game_h5_event_users (
  app_code VARCHAR(32) NOT NULL,
  stat_tz VARCHAR(64) NOT NULL DEFAULT 'UTC' COMMENT '统计时区，UTC 或 Asia/Shanghai',
  stat_day DATE NOT NULL,
  game_id VARCHAR(96) NOT NULL,
  event_name VARCHAR(64) NOT NULL,
  user_id BIGINT NOT NULL,
  first_seen_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, stat_tz, stat_day, game_id, event_name, user_id),
  KEY idx_stat_self_game_h5_users_day (app_code, stat_tz, stat_day, game_id, event_name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='自研游戏 H5 事件用户去重表';

CREATE TABLE IF NOT EXISTS stat_self_game_match_day (
  app_code VARCHAR(32) NOT NULL,
  stat_tz VARCHAR(64) NOT NULL DEFAULT 'UTC' COMMENT '统计时区，UTC 或 Asia/Shanghai',
  stat_day DATE NOT NULL,
  country_id BIGINT NOT NULL DEFAULT 0,
  region_id BIGINT NOT NULL DEFAULT 0,
  game_id VARCHAR(96) NOT NULL,
  stake_coin BIGINT NOT NULL DEFAULT 0,
  created_matches BIGINT NOT NULL DEFAULT 0,
  matched_matches BIGINT NOT NULL DEFAULT 0,
  settled_matches BIGINT NOT NULL DEFAULT 0,
  canceled_matches BIGINT NOT NULL DEFAULT 0,
  failed_matches BIGINT NOT NULL DEFAULT 0,
  human_matches BIGINT NOT NULL DEFAULT 0,
  robot_matches BIGINT NOT NULL DEFAULT 0,
  human_participants BIGINT NOT NULL DEFAULT 0,
  robot_participants BIGINT NOT NULL DEFAULT 0,
  winner_count BIGINT NOT NULL DEFAULT 0,
  loser_count BIGINT NOT NULL DEFAULT 0,
  draw_count BIGINT NOT NULL DEFAULT 0,
  user_stake_coin BIGINT NOT NULL DEFAULT 0,
  robot_stake_coin BIGINT NOT NULL DEFAULT 0,
  payout_coin BIGINT NOT NULL DEFAULT 0,
  refund_coin BIGINT NOT NULL DEFAULT 0,
  platform_profit_coin BIGINT NOT NULL DEFAULT 0,
  pool_delta_coin BIGINT NOT NULL DEFAULT 0,
  forced_player_lose_matches BIGINT NOT NULL DEFAULT 0,
  wait_ms_sum BIGINT NOT NULL DEFAULT 0,
  duration_ms_sum BIGINT NOT NULL DEFAULT 0,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, stat_tz, stat_day, country_id, region_id, game_id, stake_coin),
  KEY idx_stat_self_game_match_game (app_code, stat_tz, stat_day, game_id),
  KEY idx_stat_self_game_match_region (app_code, stat_tz, stat_day, region_id, game_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='自研游戏对局日统计表';

CREATE TABLE IF NOT EXISTS stat_self_game_pool_day (
  app_code VARCHAR(32) NOT NULL,
  stat_tz VARCHAR(64) NOT NULL DEFAULT 'UTC' COMMENT '统计时区，UTC 或 Asia/Shanghai',
  stat_day DATE NOT NULL,
  game_id VARCHAR(96) NOT NULL,
  direction VARCHAR(16) NOT NULL DEFAULT '',
  reason VARCHAR(64) NOT NULL DEFAULT '',
  transaction_count BIGINT NOT NULL DEFAULT 0,
  in_coin BIGINT NOT NULL DEFAULT 0,
  out_coin BIGINT NOT NULL DEFAULT 0,
  balance_after_coin BIGINT NOT NULL DEFAULT 0,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, stat_tz, stat_day, game_id, direction, reason),
  KEY idx_stat_self_game_pool_game (app_code, stat_tz, stat_day, game_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='自研游戏奖池日统计表';

CREATE TABLE IF NOT EXISTS stat_self_game_participant_day (
  app_code VARCHAR(32) NOT NULL,
  stat_tz VARCHAR(64) NOT NULL DEFAULT 'UTC' COMMENT '统计时区，UTC 或 Asia/Shanghai',
  stat_day DATE NOT NULL,
  country_id BIGINT NOT NULL DEFAULT 0,
  region_id BIGINT NOT NULL DEFAULT 0,
  game_id VARCHAR(96) NOT NULL,
  stake_coin BIGINT NOT NULL DEFAULT 0,
  participant_type VARCHAR(32) NOT NULL DEFAULT 'user',
  result VARCHAR(32) NOT NULL DEFAULT '',
  rps_gesture VARCHAR(32) NOT NULL DEFAULT '',
  dice_point INT NOT NULL DEFAULT 0,
  participant_count BIGINT NOT NULL DEFAULT 0,
  payout_coin BIGINT NOT NULL DEFAULT 0,
  net_win_coin BIGINT NOT NULL DEFAULT 0,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, stat_tz, stat_day, country_id, region_id, game_id, stake_coin, participant_type, result, rps_gesture, dice_point),
  KEY idx_stat_self_game_participant_game (app_code, stat_tz, stat_day, game_id),
  KEY idx_stat_self_game_participant_region (app_code, stat_tz, stat_day, region_id, game_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='自研游戏参与者结果分布日统计表';

CREATE TABLE IF NOT EXISTS stat_self_game_user_day (
  app_code VARCHAR(32) NOT NULL,
  stat_tz VARCHAR(64) NOT NULL DEFAULT 'UTC' COMMENT '统计时区，UTC 或 Asia/Shanghai',
  stat_day DATE NOT NULL,
  country_id BIGINT NOT NULL DEFAULT 0,
  region_id BIGINT NOT NULL DEFAULT 0,
  game_id VARCHAR(96) NOT NULL,
  user_id BIGINT NOT NULL,
  match_count BIGINT NOT NULL DEFAULT 0,
  win_count BIGINT NOT NULL DEFAULT 0,
  lose_count BIGINT NOT NULL DEFAULT 0,
  draw_count BIGINT NOT NULL DEFAULT 0,
  cancel_count BIGINT NOT NULL DEFAULT 0,
  stake_coin BIGINT NOT NULL DEFAULT 0,
  payout_coin BIGINT NOT NULL DEFAULT 0,
  net_win_coin BIGINT NOT NULL DEFAULT 0,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, stat_tz, stat_day, game_id, user_id),
  KEY idx_stat_self_game_user_win (app_code, stat_tz, stat_day, game_id, net_win_coin),
  KEY idx_stat_self_game_user_region (app_code, stat_tz, stat_day, region_id, game_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='自研游戏用户风险日统计表';
