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 app_tracking_events (
  app_code VARCHAR(32) NOT NULL COMMENT '应用编码',
  event_id VARCHAR(160) NOT NULL COMMENT '客户端生成的埋点幂等 ID',
  event_name VARCHAR(96) NOT NULL COMMENT '事件名',
  event_type VARCHAR(64) NOT NULL DEFAULT '' COMMENT '事件类型，如 click、view、submit',
  screen VARCHAR(128) NOT NULL DEFAULT '' COMMENT '页面或模块',
  target_type VARCHAR(64) NOT NULL DEFAULT '' COMMENT '目标类型，如 banner、button、room',
  target_id VARCHAR(128) NOT NULL DEFAULT '' COMMENT '目标 ID',
  user_id BIGINT NOT NULL DEFAULT 0 COMMENT '服务端识别的用户 ID，匿名为 0',
  device_id VARCHAR(128) NOT NULL DEFAULT '' COMMENT '设备 ID',
  identity_key VARCHAR(160) GENERATED ALWAYS AS (CASE WHEN user_id > 0 THEN CONCAT('u:', user_id) ELSE CONCAT('d:', device_id) END) VIRTUAL COMMENT '漏斗/留存去重身份，登录用户优先 user_id，匿名用户回退 device_id',
  session_id VARCHAR(160) NOT NULL DEFAULT '' COMMENT '会话 ID',
  platform VARCHAR(32) NOT NULL DEFAULT '' COMMENT '平台',
  app_version VARCHAR(64) NOT NULL DEFAULT '' COMMENT 'App 版本',
  language VARCHAR(32) NOT NULL DEFAULT '' COMMENT '语言',
  timezone VARCHAR(64) NOT NULL DEFAULT '' COMMENT '客户端时区，仅作属性记录',
  country_id BIGINT NOT NULL DEFAULT 0 COMMENT '服务端用户国家维度',
  region_id BIGINT NOT NULL DEFAULT 0 COMMENT '服务端用户区域维度',
  duration_ms BIGINT NOT NULL DEFAULT 0 COMMENT '客户端记录的耗时毫秒',
  success TINYINT(1) NOT NULL DEFAULT 0 COMMENT '客户端动作是否成功',
  error_code VARCHAR(64) NOT NULL DEFAULT '' COMMENT '客户端错误码',
  properties_json JSON NOT NULL COMMENT '事件扩展属性',
  occurred_at_ms BIGINT NOT NULL COMMENT '客户端事件发生时间，UTC epoch ms',
  received_at_ms BIGINT NOT NULL COMMENT '服务端接收时间，UTC epoch ms',
  stat_day DATE NOT NULL COMMENT 'UTC 统计日',
  PRIMARY KEY (app_code, event_id),
  KEY idx_app_tracking_event_name (app_code, stat_day, event_name),
  KEY idx_app_tracking_screen (app_code, stat_day, screen),
  KEY idx_app_tracking_user (app_code, user_id, occurred_at_ms),
  KEY idx_app_tracking_device (app_code, device_id, occurred_at_ms),
  KEY idx_app_tracking_event_window (app_code, event_name, occurred_at_ms, identity_key, device_id),
  KEY idx_app_tracking_retention_identity (app_code, stat_day, identity_key)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='App 埋点原始明细表';

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 '币商充值美元最小单位',
  coin_seller_stock_coin 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 '币商向用户转普通金币数量',
  coin_total BIGINT NOT NULL DEFAULT 0 COMMENT '普通用户 COIN 钱包余额快照总和',
  consumed_coin BIGINT NOT NULL DEFAULT 0 COMMENT '游戏、礼物、转盘和红包发放消耗金币',
  output_coin BIGINT NOT NULL DEFAULT 0 COMMENT '游戏、收礼、lucky、转盘和红包领取退回产出金币',
  platform_grant_coin BIGINT NOT NULL DEFAULT 0 COMMENT '排行榜、活动、签到和火箭奖励金币发放',
  manual_grant_coin BIGINT NOT NULL DEFAULT 0 COMMENT '后台人工发放金币',
  salary_usd_minor BIGINT NOT NULL DEFAULT 0 COMMENT '主播和代理工资钱包余额快照 USD 最小单位',
  salary_transfer_coin BIGINT NOT NULL DEFAULT 0 COMMENT '工资转币商入账金币数量',
  mic_online_ms BIGINT NOT NULL DEFAULT 0 COMMENT '麦上总时长毫秒',
  mic_online_users 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),
  KEY idx_stat_user_registration_country_day (app_code, stat_tz, country_id, registered_day, user_id),
  KEY idx_stat_user_registration_region_day (app_code, stat_tz, region_id, registered_day, country_id, user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户注册日留存 cohort 表';

CREATE TABLE IF NOT EXISTS stat_user_dimension (
  app_code VARCHAR(32) NOT NULL,
  user_id BIGINT NOT NULL,
  country_id BIGINT NOT NULL DEFAULT 0,
  region_id BIGINT NOT NULL DEFAULT 0,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, user_id),
  KEY idx_stat_user_dimension_country (app_code, country_id),
  KEY idx_stat_user_dimension_region (app_code, region_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='统计服务用户国家区域维度投影';

CREATE TABLE IF NOT EXISTS stat_user_day_mic (
  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,
  mic_online_ms BIGINT NOT NULL DEFAULT 0,
  updated_at_ms BIGINT NOT NULL,
  PRIMARY KEY (app_code, stat_tz, stat_day, user_id),
  KEY idx_stat_user_day_mic_country (app_code, stat_tz, stat_day, country_id),
  KEY idx_stat_user_day_mic_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_social_user_day (
  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 切换',
  subject_key VARCHAR(160) NOT NULL COMMENT '用户或设备维度去重键，u:<user_id> 或 d:<device_id>',
  user_id BIGINT NOT NULL DEFAULT 0 COMMENT '登录用户 ID，匿名为 0',
  device_id VARCHAR(128) NOT NULL DEFAULT '' COMMENT '匿名设备 ID',
  country_id BIGINT NOT NULL DEFAULT 0 COMMENT '事件发生时国家维度',
  region_id BIGINT NOT NULL DEFAULT 0 COMMENT '事件发生时区域维度',
  user_role VARCHAR(16) NOT NULL DEFAULT 'user' COMMENT '当日用户身份快照，user 或 host',
  app_first_open TINYINT(1) NOT NULL DEFAULT 0 COMMENT '首次打开 App 去重标记',
  registered_success TINYINT(1) NOT NULL DEFAULT 0 COMMENT '注册成功去重标记',
  active_user TINYINT(1) NOT NULL DEFAULT 0 COMMENT '当日活跃去重标记',
  room_join_success TINYINT(1) NOT NULL DEFAULT 0 COMMENT '进房成功去重标记',
  room_join_fail TINYINT(1) NOT NULL DEFAULT 0 COMMENT '进房失败去重标记',
  mic_up_success TINYINT(1) NOT NULL DEFAULT 0 COMMENT '上麦成功去重标记',
  gift_sent TINYINT(1) NOT NULL DEFAULT 0 COMMENT '送礼成功去重标记',
  normal_gift_sent TINYINT(1) NOT NULL DEFAULT 0 COMMENT '普通礼物送礼去重标记',
  lucky_gift_sent TINYINT(1) NOT NULL DEFAULT 0 COMMENT '幸运礼物送礼去重标记',
  super_lucky_gift_sent TINYINT(1) NOT NULL DEFAULT 0 COMMENT '超级幸运礼物送礼去重标记',
  recharge_user TINYINT(1) NOT NULL DEFAULT 0 COMMENT '当日充值用户去重标记',
  first_recharge_user TINYINT(1) NOT NULL DEFAULT 0 COMMENT '首充用户去重标记',
  repeat_recharge_user TINYINT(1) NOT NULL DEFAULT 0 COMMENT '复购用户去重标记',
  game_bet_user TINYINT(1) NOT NULL DEFAULT 0 COMMENT '游戏押注用户去重标记',
  self_game_bet_user TINYINT(1) NOT NULL DEFAULT 0 COMMENT '自研游戏押注用户去重标记',
  probability_game_bet_user TINYINT(1) NOT NULL DEFAULT 0 COMMENT '概率游戏押注用户去重标记',
  chat_sent TINYINT(1) NOT NULL DEFAULT 0 COMMENT '公屏聊天去重标记',
  follow_user TINYINT(1) NOT NULL DEFAULT 0 COMMENT '关注用户去重标记',
  follow_room TINYINT(1) NOT NULL DEFAULT 0 COMMENT '关注房间去重标记',
  gift_panel_open TINYINT(1) NOT NULL DEFAULT 0 COMMENT '礼物面板打开去重标记',
  probability_game_panel_open TINYINT(1) NOT NULL DEFAULT 0 COMMENT '概率游戏面板打开去重标记',
  app_stay_ms BIGINT NOT NULL DEFAULT 0 COMMENT 'App 停留时长毫秒',
  room_stay_ms BIGINT NOT NULL DEFAULT 0 COMMENT '房间停留时长毫秒，客户端 room_stay 埋点口径',
  room_online_ms BIGINT NOT NULL DEFAULT 0 COMMENT '房间在线时长毫秒，服务端进退房事实口径',
  mic_stay_ms BIGINT NOT NULL DEFAULT 0 COMMENT '麦上停留时长毫秒',
  recharge_count BIGINT NOT NULL DEFAULT 0 COMMENT '充值次数',
  recharge_usd_minor BIGINT NOT NULL DEFAULT 0 COMMENT '总充值 USD 最小单位',
  google_recharge_usd_minor BIGINT NOT NULL DEFAULT 0 COMMENT 'Google 充值 USD 最小单位',
  third_party_recharge_usd_minor BIGINT NOT NULL DEFAULT 0 COMMENT '三方充值 USD 最小单位',
  coin_seller_recharge_usd_minor BIGINT NOT NULL DEFAULT 0 COMMENT '币商充值 USD 最小单位',
  first_recharge_usd_minor BIGINT NOT NULL DEFAULT 0 COMMENT '首充 USD 最小单位',
  repeat_recharge_usd_minor BIGINT NOT NULL DEFAULT 0 COMMENT '复购 USD 最小单位',
  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 '超级幸运礼物金币流水',
  lucky_gift_payout_coin BIGINT NOT NULL DEFAULT 0 COMMENT '幸运礼物返奖金币',
  super_lucky_gift_payout_coin BIGINT NOT NULL DEFAULT 0 COMMENT '超级幸运礼物返奖金币',
  game_bet_coin BIGINT NOT NULL DEFAULT 0 COMMENT '游戏押注金币',
  game_bet_count BIGINT NOT NULL DEFAULT 0 COMMENT '游戏押注次数',
  self_game_bet_coin BIGINT NOT NULL DEFAULT 0 COMMENT '自研游戏押注金币',
  self_game_bet_count BIGINT NOT NULL DEFAULT 0 COMMENT '自研游戏押注次数',
  probability_game_bet_coin BIGINT NOT NULL DEFAULT 0 COMMENT '概率游戏押注金币',
  probability_game_bet_count BIGINT NOT NULL DEFAULT 0 COMMENT '概率游戏押注次数',
  other_consume_coin BIGINT NOT NULL DEFAULT 0 COMMENT '其他消费金币',
  output_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, subject_key),
  KEY idx_stat_social_user_day_user (app_code, stat_tz, stat_day, user_id),
  KEY idx_stat_social_user_day_region (app_code, stat_tz, stat_day, region_id),
  KEY idx_stat_social_user_day_filter (app_code, stat_tz, stat_day, user_role, recharge_user)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Social BI P0-P5 用户日级读模型';

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_platform_grant_coin_events (
  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 切换',
  event_id VARCHAR(160) NOT NULL COMMENT '钱包 outbox 事件 ID',
  event_type VARCHAR(64) NOT NULL COMMENT '钱包奖励事件类型',
  user_id BIGINT NOT NULL COMMENT '收币用户 ID',
  country_id BIGINT NOT NULL DEFAULT 0 COMMENT '事件发生时用户国家维度',
  region_id BIGINT NOT NULL DEFAULT 0 COMMENT '事件发生时用户区域维度',
  amount BIGINT NOT NULL DEFAULT 0 COMMENT '本次平台发放金币',
  occurred_at_ms BIGINT NOT NULL COMMENT '事件发生时间，UTC epoch ms',
  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
  PRIMARY KEY (app_code, stat_tz, event_id),
  KEY idx_stat_platform_grant_country (app_code, stat_tz, stat_day, country_id, region_id, amount),
  KEY idx_stat_platform_grant_user (app_code, stat_tz, stat_day, country_id, user_id, occurred_at_ms),
  KEY idx_stat_platform_grant_source (app_code, stat_tz, stat_day, event_type, country_id, user_id, occurred_at_ms)
) 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='自研游戏用户风险日统计表';
