SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;


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

USE hyapp_lucky_gift;

CREATE TABLE IF NOT EXISTS activities (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码，用于多租户隔离',
  activity_id VARCHAR(96) NOT NULL COMMENT '活动 ID',
  status VARCHAR(32) NOT NULL COMMENT '业务状态',
  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
  PRIMARY KEY (app_code, activity_id),
  KEY idx_activities_status (app_code, status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='幸运礼物兼容活动实例表';

CREATE TABLE IF NOT EXISTS activity_event_consumption (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码，用于多租户隔离',
  event_id VARCHAR(96) NOT NULL COMMENT '事件幂等 ID',
  event_type VARCHAR(96) NOT NULL COMMENT '事件类型',
  source VARCHAR(64) NOT NULL COMMENT '来源',
  status VARCHAR(32) NOT NULL COMMENT '业务状态',
  failure_reason VARCHAR(255) NULL COMMENT '失败原因',
  consumed_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',
  PRIMARY KEY (app_code, event_id),
  KEY idx_activity_event_consumption_status (app_code, status),
  KEY idx_activity_event_consumption_updated_at (app_code, updated_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='活动事件消费位点表';

CREATE TABLE IF NOT EXISTS lucky_gift_outbox (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码，用于多租户隔离',
  outbox_id VARCHAR(96) NOT NULL COMMENT '发件箱 ID',
  event_type VARCHAR(96) NOT NULL COMMENT '事件类型',
  payload BLOB NOT NULL COMMENT '业务负载快照',
  status VARCHAR(32) NOT NULL COMMENT '业务状态',
  retry_count INT NOT NULL DEFAULT 0 COMMENT '重试次数',
  next_retry_at_ms BIGINT NOT NULL COMMENT '下一次重试时间，UTC epoch ms',
  locked_by VARCHAR(128) NOT NULL DEFAULT '' COMMENT '当前锁定 worker',
  lock_until_ms BIGINT NOT NULL DEFAULT 0 COMMENT '锁过期时间，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, outbox_id),
  KEY idx_lucky_gift_outbox_status_retry (app_code, status, next_retry_at_ms),
  KEY idx_lucky_gift_outbox_lucky_retry (app_code, event_type, status, next_retry_at_ms, created_at_ms, outbox_id),
  KEY idx_lucky_gift_outbox_lucky_lock (app_code, event_type, status, lock_until_ms, created_at_ms, outbox_id),
  KEY idx_lucky_gift_outbox_retention (app_code, status, updated_at_ms, outbox_id),
  KEY idx_lucky_gift_outbox_claim_retry (event_type, status, next_retry_at_ms, created_at_ms, outbox_id),
  KEY idx_lucky_gift_outbox_claim_lock (event_type, status, lock_until_ms, created_at_ms, outbox_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='幸运礼物 owner outbox 表';

-- Worker 的 outbox 抢占 SQL 不带 app_code（跨 App 统一补偿），老索引都以 app_code 开头无法命中，
-- 每次轮询（默认 1 秒 × 空轮询最多 6 条查询）都会全索引扫描并 filesort 整表。
-- 这两个无 app_code 前缀的索引让空轮询变成零行索引定位，是线上 MySQL 基线 CPU 的主要治理点。
SET @ddl := IF(
  (SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'lucky_gift_outbox' AND INDEX_NAME = 'idx_lucky_gift_outbox_claim_retry') = 0,
  'ALTER TABLE lucky_gift_outbox ADD INDEX idx_lucky_gift_outbox_claim_retry (event_type, status, next_retry_at_ms, created_at_ms, outbox_id), ALGORITHM=INPLACE, LOCK=NONE',
  'SELECT 1'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl := IF(
  (SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'lucky_gift_outbox' AND INDEX_NAME = 'idx_lucky_gift_outbox_claim_lock') = 0,
  'ALTER TABLE lucky_gift_outbox ADD INDEX idx_lucky_gift_outbox_claim_lock (event_type, status, lock_until_ms, created_at_ms, outbox_id), ALGORITHM=INPLACE, LOCK=NONE',
  'SELECT 1'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

CREATE TABLE IF NOT EXISTS lucky_gift_rule_versions (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码',
  pool_id VARCHAR(96) NOT NULL COMMENT '幸运礼物奖池 ID',
  rule_version BIGINT NOT NULL COMMENT '不可变规则版本',
  enabled TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否启用',
  strategy_version VARCHAR(32) NOT NULL DEFAULT 'fixed_v2' COMMENT 'fixed_v2/dynamic_v3；旧版本固定按 fixed_v2 解释',
  target_rtp_ppm BIGINT NOT NULL COMMENT '基础返奖目标，ppm',
  pool_rate_ppm BIGINT NOT NULL COMMENT '每笔扣费进入基础奖池比例，ppm',
  profit_rate_ppm BIGINT NOT NULL DEFAULT 0 COMMENT '每笔扣费计入平台盈利比例，ppm',
  anchor_rate_ppm BIGINT NOT NULL DEFAULT 0 COMMENT '每笔扣费计入主播收益比例，ppm',
  settlement_window_wager BIGINT NOT NULL COMMENT 'RTP 结算窗口流水，单位金币',
  control_band_ppm BIGINT NOT NULL COMMENT '正常波动带，ppm',
  gift_price_reference BIGINT NOT NULL COMMENT '配置参考礼物价格',
  novice_max_equivalent_draws BIGINT NOT NULL DEFAULT 2000 COMMENT '新手阶段截止等价抽数，按累计金币/参考价格折算',
  normal_max_equivalent_draws BIGINT NOT NULL DEFAULT 20000 COMMENT '正常阶段截止等价抽数，超过后进入高阶阶段',
  effective_from_ms BIGINT NOT NULL COMMENT '生效时间，UTC epoch ms',
  created_by_admin_id BIGINT NOT NULL DEFAULT 0 COMMENT '发布人',
  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
  initial_pool_coins BIGINT NOT NULL DEFAULT 0 COMMENT 'dynamic_v3 奖池首次物化的初始注资金币',
  loss_streak_guarantee BIGINT NOT NULL DEFAULT 0 COMMENT '连续未中奖后的保底触发抽数',
  low_watermark_coins BIGINT NOT NULL DEFAULT 0 COMMENT '低水位阈值金币',
  low_water_nonzero_factor_ppm BIGINT NOT NULL DEFAULT 0 COMMENT '低水位非零奖档概率因子，ppm',
  high_watermark_coins BIGINT NOT NULL DEFAULT 0 COMMENT '高水位阈值金币',
  high_water_nonzero_factor_ppm BIGINT NOT NULL DEFAULT 0 COMMENT '高水位非零奖档概率因子，ppm',
  recharge_boost_window_ms BIGINT NOT NULL DEFAULT 0 COMMENT '最近充值概率加权窗口，毫秒',
  recharge_boost_factor_ppm BIGINT NOT NULL DEFAULT 0 COMMENT '最近充值非零奖档概率因子，ppm',
  jackpot_multiplier_ppms JSON NOT NULL DEFAULT (JSON_ARRAY()) COMMENT '动态大奖倍率列表，1x=1000000',
  jackpot_global_rtp_max_ppm BIGINT NOT NULL DEFAULT 0 COMMENT '动态大奖全局 RTP 上限，ppm',
  jackpot_user_day_rtp_max_ppm BIGINT NOT NULL DEFAULT 0 COMMENT '动态大奖用户当日 RTP 上限，ppm',
  jackpot_user_72h_rtp_max_ppm BIGINT NOT NULL DEFAULT 0 COMMENT '动态大奖用户滚动 72 小时 RTP 上限，ppm',
  jackpot_spend_threshold_coins BIGINT NOT NULL DEFAULT 0 COMMENT '动态大奖用户消费门槛金币，由 App 显式换算 50 美元等值',
  max_jackpot_hits_per_user_day BIGINT NOT NULL DEFAULT 0 COMMENT '单用户 UTC 日动态大奖命中上限',
  max_single_payout BIGINT NOT NULL DEFAULT 0 COMMENT '单次返奖金币上限',
  user_hourly_payout_cap BIGINT NOT NULL DEFAULT 0 COMMENT '单用户 UTC 小时返奖上限',
  user_daily_payout_cap BIGINT NOT NULL DEFAULT 0 COMMENT '单用户 UTC 日返奖上限',
  device_daily_payout_cap BIGINT NOT NULL DEFAULT 0 COMMENT '单设备 UTC 日返奖上限',
  room_hourly_payout_cap BIGINT NOT NULL DEFAULT 0 COMMENT '单房间 UTC 小时返奖上限',
  anchor_daily_payout_cap BIGINT NOT NULL DEFAULT 0 COMMENT '单主播 UTC 日返奖上限',
  PRIMARY KEY (app_code, pool_id, rule_version),
  KEY idx_lucky_gift_rule_versions_latest (app_code, pool_id, rule_version),
  KEY idx_lucky_gift_rule_versions_enabled (app_code, enabled, created_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='幸运礼物 v2 规则版本表';

CREATE TABLE IF NOT EXISTS lucky_gift_stage_tiers (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码',
  pool_id VARCHAR(96) NOT NULL COMMENT '幸运礼物奖池 ID',
  rule_version BIGINT NOT NULL COMMENT '规则版本',
  stage VARCHAR(32) NOT NULL COMMENT 'novice/normal/advanced',
  min_recharge_7d_coins BIGINT NOT NULL DEFAULT 0 COMMENT '进入该阶段的最近 7 日充值金币门槛',
  min_recharge_30d_coins BIGINT NOT NULL DEFAULT 0 COMMENT '进入该阶段的最近 30 日充值金币门槛',
  tier_id VARCHAR(96) NOT NULL COMMENT '奖档 ID',
  multiplier_ppm BIGINT NOT NULL COMMENT '倍率，1x = 1000000',
  base_weight_ppm BIGINT NOT NULL COMMENT '正常模式基础概率，ppm',
  high_water_only TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否高水位才开放',
  enabled TINYINT(1) NOT NULL DEFAULT 1 COMMENT '是否启用',
  PRIMARY KEY (app_code, pool_id, rule_version, stage, tier_id),
  KEY idx_lucky_gift_stage_tiers_stage (app_code, pool_id, rule_version, stage)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='幸运礼物 v2 阶段奖档表';

CREATE TABLE IF NOT EXISTS lucky_rtp_windows (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码',
  scope_type VARCHAR(32) NOT NULL COMMENT 'global 或 gift',
  scope_id VARCHAR(128) NOT NULL COMMENT '奖池+不可变规则版本作用域',
  window_index BIGINT NOT NULL COMMENT 'RTP 窗口序号',
  target_rtp_ppm INT NOT NULL COMMENT '窗口目标 RTP',
  control_window_draws BIGINT NOT NULL COMMENT 'fixed_v2窗口抽数；dynamic_v3仅兼容展示，滚窗读取规则真实流水阈值',
  paid_draws BIGINT NOT NULL DEFAULT 0 COMMENT '窗口已抽次数',
  wager_coins BIGINT NOT NULL DEFAULT 0 COMMENT '窗口消耗金币',
  target_payout_coins BIGINT NOT NULL COMMENT '窗口目标基础返奖',
  actual_base_payout_coins BIGINT NOT NULL DEFAULT 0 COMMENT '窗口实际基础返奖',
  carry_ppm BIGINT NOT NULL DEFAULT 0 COMMENT '整数除法余数',
  status VARCHAR(32) NOT NULL DEFAULT 'open' COMMENT 'open/closed/underpaid',
  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
  PRIMARY KEY (app_code, scope_type, scope_id, window_index),
  KEY idx_lucky_rtp_windows_open (app_code, scope_type, scope_id, status, window_index)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='幸运礼物 RTP 控制窗口';

CREATE TABLE IF NOT EXISTS lucky_pools (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码',
  scope_type VARCHAR(32) NOT NULL COMMENT 'pool',
  scope_id VARCHAR(96) NOT NULL COMMENT '幸运礼物奖池 ID',
  balance BIGINT NOT NULL DEFAULT 0 COMMENT '当前可用水位',
  reserve_floor BIGINT NOT NULL DEFAULT 0 COMMENT '安全水位',
  total_in BIGINT NOT NULL DEFAULT 0 COMMENT '累计入池',
  total_out BIGINT NOT NULL DEFAULT 0 COMMENT '累计基础返奖支出',
  manual_credit_total BIGINT NOT NULL DEFAULT 0 COMMENT '累计人工注资金币，不计入业务 total_in',
  manual_debit_total BIGINT NOT NULL DEFAULT 0 COMMENT '累计人工扣资金币，不计入业务 total_out',
  profit_total BIGINT NOT NULL DEFAULT 0 COMMENT 'dynamic_v3 累计平台盈利拆账金币',
  anchor_income_total BIGINT NOT NULL DEFAULT 0 COMMENT 'dynamic_v3 累计主播收益拆账金币',
  initial_seed_total BIGINT NOT NULL DEFAULT 0 COMMENT 'dynamic_v3 累计初始注资金币，只在首次物化时增加',
  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
  PRIMARY KEY (app_code, scope_type, scope_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='幸运礼物单一基础奖池';

CREATE TABLE IF NOT EXISTS lucky_pool_adjustments (
  app_code VARCHAR(32) NOT NULL COMMENT '应用编码',
  adjustment_id VARCHAR(128) NOT NULL COMMENT '调用方资金幂等 ID',
  pool_id VARCHAR(96) NOT NULL COMMENT '幸运礼物奖池 ID',
  strategy_version VARCHAR(32) NOT NULL COMMENT 'fixed_v2/dynamic_v3',
  scope_type VARCHAR(32) NOT NULL COMMENT '实际资金账本 scope_type',
  direction VARCHAR(16) NOT NULL COMMENT 'in/out',
  amount_coins BIGINT NOT NULL COMMENT '本次人工调整金币',
  reason VARCHAR(512) NOT NULL COMMENT '管理员填写的调整原因',
  operator_admin_id BIGINT NOT NULL COMMENT '操作管理员 ID',
  request_id VARCHAR(128) NOT NULL DEFAULT '' COMMENT '链路 request_id，不作为幂等键',
  insert_token VARCHAR(64) NOT NULL COMMENT '首次插入事务随机令牌，用于并发幂等判定',
  balance_before BIGINT NOT NULL DEFAULT 0 COMMENT '首次应用前余额',
  balance_after BIGINT NOT NULL DEFAULT 0 COMMENT '首次应用后余额',
  pool_snapshot_json JSON NULL COMMENT '首次应用后的完整奖池快照，供幂等重放',
  status VARCHAR(16) NOT NULL DEFAULT 'pending' COMMENT 'pending/applied',
  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
  PRIMARY KEY (app_code, adjustment_id),
  KEY idx_lucky_pool_adjustments_pool (app_code, pool_id, strategy_version, created_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='幸运礼物人工奖池调整事实';

CREATE TABLE IF NOT EXISTS lucky_risk_counters (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码',
  scope_type VARCHAR(32) NOT NULL COMMENT 'user/device/room/anchor',
  scope_id VARCHAR(255) NOT NULL COMMENT 'pool_id+风控主体 ID；容纳96字符奖池与128字符设备',
  window_type VARCHAR(32) NOT NULL COMMENT 'hour/day',
  bucket_key VARCHAR(32) NOT NULL COMMENT 'UTC 窗口键',
  payout_coins BIGINT 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, scope_type, scope_id, window_type, bucket_key)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='幸运礼物多维风控窗口计数';

CREATE TABLE IF NOT EXISTS lucky_user_rtp_hour_buckets (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码',
  pool_id VARCHAR(96) NOT NULL COMMENT '幸运礼物奖池 ID',
  user_id BIGINT NOT NULL COMMENT '用户 ID',
  bucket_hour_ms BIGINT NOT NULL COMMENT 'UTC 小时开始时间，epoch ms',
  wager_coins BIGINT NOT NULL DEFAULT 0 COMMENT '小时内抽奖流水金币',
  payout_coins BIGINT 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, pool_id, user_id, bucket_hour_ms),
  KEY idx_lucky_user_rtp_hour_retention (app_code, bucket_hour_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户滚动72小时RTP完整小时聚合桶';

-- 小时桶负责完整小时，边界事件只用于精确补齐 [now-72h,now) 两端不足一小时的片段。
-- 这样热路径最多扫描 71 个小时桶和两个边界小时内的用户事件，不回扫全量抽奖事实。
CREATE TABLE IF NOT EXISTS lucky_user_rtp_boundary_events (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码',
  pool_id VARCHAR(96) NOT NULL COMMENT '幸运礼物奖池 ID',
  user_id BIGINT NOT NULL COMMENT '用户 ID',
  draw_id VARCHAR(128) NOT NULL COMMENT '单抽事实 ID，用于幂等',
  occurred_at_ms BIGINT NOT NULL COMMENT '扣费事实时间，UTC epoch ms',
  wager_coins BIGINT NOT NULL COMMENT '本抽流水金币',
  payout_coins BIGINT NOT NULL COMMENT '本抽已承诺返奖负债',
  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
  PRIMARY KEY (app_code, pool_id, user_id, draw_id),
  KEY idx_lucky_user_rtp_boundary_range (app_code, pool_id, user_id, occurred_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户严格滚动72小时RTP边界事件';

CREATE TABLE IF NOT EXISTS lucky_user_strategy_days (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码',
  pool_id VARCHAR(96) NOT NULL COMMENT '幸运礼物奖池 ID',
  user_id BIGINT NOT NULL COMMENT '用户 ID',
  stat_day DATE NOT NULL COMMENT 'UTC 自然日',
  wager_coins BIGINT NOT NULL DEFAULT 0 COMMENT '当日抽奖流水金币',
  payout_coins BIGINT NOT NULL DEFAULT 0 COMMENT '当日返奖金币',
  spend_coins BIGINT NOT NULL DEFAULT 0 COMMENT '当日用于动态大奖资格的累计消费金币',
  jackpot_hits BIGINT 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, pool_id, user_id, stat_day),
  KEY idx_lucky_user_strategy_days_retention (app_code, stat_day)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户 dynamic_v3 UTC 日级消费、RTP 与大奖资格状态';

CREATE TABLE IF NOT EXISTS lucky_user_states (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码',
  user_id BIGINT NOT NULL COMMENT '用户 ID',
  gift_id VARCHAR(96) NOT NULL COMMENT '奖池 ID；当前列名沿用旧内部作用域名',
  paid_draws BIGINT NOT NULL DEFAULT 0 COMMENT '该用户幸运礼物累计付费抽数',
  cumulative_wager_coins BIGINT NOT NULL DEFAULT 0 COMMENT '该用户在当前奖池累计消耗金币',
  equivalent_draws BIGINT NOT NULL DEFAULT 0 COMMENT '累计金币按规则参考价格折算后的等价抽数',
  loss_streak BIGINT NOT NULL DEFAULT 0 COMMENT '连续未获得可见奖励次数，中奖后清零',
  pending_jackpot_tokens BIGINT NOT NULL DEFAULT 0 COMMENT '未消费大奖机会；按UTC日消费产生，资金不足时允许跨日保留',
  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
  PRIMARY KEY (app_code, user_id, gift_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='幸运礼物用户体验池状态';

-- draw 事实不存在时 SELECT ... FOR UPDATE 没有可锁记录；该锁行保证同一钱包 command_id 的
-- 首次并发请求串行执行，等待者在首事务提交后只回读原始结果，不重新随机或重复推进奖池。
CREATE TABLE IF NOT EXISTS lucky_gift_command_locks (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码',
  command_id VARCHAR(128) NOT NULL COMMENT '钱包已扣费命令幂等 ID',
  created_at_ms BIGINT NOT NULL COMMENT '首次请求时间，UTC epoch ms',
  PRIMARY KEY (app_code, command_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='内部幸运礼物并发幂等锁行';

CREATE TABLE IF NOT EXISTS lucky_draw_records (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码',
  draw_id VARCHAR(128) NOT NULL COMMENT '抽奖 ID',
  command_id VARCHAR(128) NOT NULL COMMENT '幂等命令 ID',
  user_id BIGINT NOT NULL COMMENT '用户 ID',
  device_id VARCHAR(128) NOT NULL COMMENT '设备 ID',
  room_id VARCHAR(96) NOT NULL COMMENT '房间 ID',
  anchor_id VARCHAR(96) NOT NULL COMMENT '主播关联 ID',
  visible_region_id BIGINT NOT NULL DEFAULT 0 COMMENT '房间可见区域 ID',
  pool_id VARCHAR(96) NOT NULL COMMENT '幸运礼物奖池 ID',
  gift_id VARCHAR(96) NOT NULL COMMENT '礼物 ID',
  coin_spent BIGINT NOT NULL COMMENT '消耗金币',
  rule_version BIGINT NOT NULL COMMENT '规则版本',
  experience_pool VARCHAR(32) NOT NULL COMMENT '体验池',
  rtp_window_index BIGINT NOT NULL COMMENT '全站 RTP 窗口',
  gift_rtp_window_index BIGINT NOT NULL COMMENT '礼物 RTP 窗口',
  selected_tier_id VARCHAR(96) NOT NULL COMMENT '命中奖档',
  base_reward_coins BIGINT NOT NULL DEFAULT 0 COMMENT '基础 RTP 返奖',
  effective_reward_coins BIGINT NOT NULL DEFAULT 0 COMMENT '用户可见总奖励',
  stage_feedback TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否阶段反馈',
  high_multiplier TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否高倍命中',
  candidate_tiers_json JSON NOT NULL COMMENT '候选过滤快照',
  pool_snapshot_json JSON NOT NULL COMMENT '奖池快照',
  rtp_snapshot_json JSON NOT NULL COMMENT 'RTP 快照',
  reward_status VARCHAR(32) NOT NULL COMMENT 'pending/granted/failed',
  reward_transaction_id VARCHAR(128) NOT NULL DEFAULT '' COMMENT '返奖钱包交易 ID',
  reward_failure_reason VARCHAR(255) NOT NULL DEFAULT '' COMMENT '返奖失败原因',
  paid_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, draw_id),
  UNIQUE KEY uk_lucky_draw_command (app_code, command_id),
  KEY idx_lucky_draw_pool (app_code, pool_id, created_at_ms),
  KEY idx_lucky_draw_user (app_code, user_id, created_at_ms),
  KEY idx_lucky_draw_gift (app_code, gift_id, created_at_ms),
  KEY idx_lucky_draw_room (app_code, room_id, created_at_ms),
  KEY idx_lucky_draw_region_created (app_code, visible_region_id, created_at_ms, draw_id),
  KEY idx_lucky_draw_status (app_code, reward_status, updated_at_ms),
  KEY idx_lucky_draw_created (app_code, created_at_ms, draw_id),
  KEY idx_lucky_draw_pool_status_summary (app_code, pool_id, reward_status, created_at_ms, draw_id),
  KEY idx_lucky_draw_gift_status_summary (app_code, pool_id, gift_id, reward_status, created_at_ms, draw_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='幸运礼物抽奖事实';

CREATE TABLE IF NOT EXISTS lucky_draw_pool_stats (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码',
  pool_id VARCHAR(96) NOT NULL COMMENT '幸运礼物奖池 ID',
  gift_id VARCHAR(96) NOT NULL DEFAULT '' COMMENT '礼物 ID，空值表示奖池汇总',
  total_draws BIGINT NOT NULL DEFAULT 0 COMMENT '抽奖次数',
  unique_users BIGINT NOT NULL DEFAULT 0 COMMENT '参与用户数',
  unique_rooms BIGINT NOT NULL DEFAULT 0 COMMENT '参与房间数',
  total_spent_coins BIGINT NOT NULL DEFAULT 0 COMMENT '消耗金币',
  total_reward_coins BIGINT NOT NULL DEFAULT 0 COMMENT '返还金币',
  base_reward_coins BIGINT NOT NULL DEFAULT 0 COMMENT '基础 RTP 返奖',
  pending_draws BIGINT NOT NULL DEFAULT 0 COMMENT '待发放数量',
  granted_draws BIGINT NOT NULL DEFAULT 0 COMMENT '已发放数量',
  failed_draws BIGINT 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, pool_id, gift_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='幸运礼物池级汇总统计';

CREATE TABLE IF NOT EXISTS lucky_draw_pool_day_stats (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码',
  stat_day DATE NOT NULL COMMENT 'UTC 统计日',
  visible_region_id BIGINT NOT NULL DEFAULT 0 COMMENT '房间可见区域 ID',
  pool_id VARCHAR(96) NOT NULL COMMENT '幸运礼物奖池 ID',
  gift_id VARCHAR(96) NOT NULL DEFAULT '' COMMENT '礼物 ID，空值表示奖池汇总',
  draw_count BIGINT NOT NULL DEFAULT 0 COMMENT '抽奖次数',
  turnover_coin BIGINT NOT NULL DEFAULT 0 COMMENT '消耗金币',
  payout_coin BIGINT NOT NULL DEFAULT 0 COMMENT '用户可见返奖金币',
  base_reward_coin BIGINT NOT NULL DEFAULT 0 COMMENT '基础 RTP 返奖',
  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
  PRIMARY KEY (app_code, stat_day, visible_region_id, pool_id, gift_id),
  KEY idx_lucky_draw_pool_day_overview (app_code, stat_day, pool_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='幸运礼物 Databi 日维度汇总';

CREATE TABLE IF NOT EXISTS lucky_draw_pool_stat_users (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码',
  pool_id VARCHAR(96) NOT NULL COMMENT '幸运礼物奖池 ID',
  gift_id VARCHAR(96) NOT NULL DEFAULT '' COMMENT '礼物 ID，空值表示奖池汇总',
  user_id BIGINT NOT NULL COMMENT '用户 ID',
  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
  PRIMARY KEY (app_code, pool_id, gift_id, user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='幸运礼物汇总用户去重';

CREATE TABLE IF NOT EXISTS lucky_draw_pool_stat_rooms (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码',
  pool_id VARCHAR(96) NOT NULL COMMENT '幸运礼物奖池 ID',
  gift_id VARCHAR(96) NOT NULL DEFAULT '' COMMENT '礼物 ID，空值表示奖池汇总',
  room_id VARCHAR(96) NOT NULL COMMENT '房间 ID',
  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
  PRIMARY KEY (app_code, pool_id, gift_id, room_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='幸运礼物汇总房间去重';

CREATE TABLE IF NOT EXISTS lucky_draw_pool_stat_cursors (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码',
  cursor_name VARCHAR(64) NOT NULL COMMENT '游标名称',
  last_draw_created_at_ms BIGINT NOT NULL DEFAULT 0 COMMENT '最后已聚合 draw 创建时间',
  last_draw_id VARCHAR(128) NOT NULL DEFAULT '' COMMENT '同一毫秒内最后已聚合 draw ID',
  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
  PRIMARY KEY (app_code, cursor_name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='幸运礼物后台统计快照游标';

CREATE TABLE IF NOT EXISTS external_lucky_gift_request_locks (
  app_code VARCHAR(32) NOT NULL COMMENT '外部 App 应用编码',
  request_id VARCHAR(128) NOT NULL COMMENT '外部业务幂等 ID',
  created_at_ms BIGINT NOT NULL COMMENT '首次请求时间，UTC epoch ms',
  PRIMARY KEY (app_code, request_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='外部幸运礼物并发幂等锁行';

CREATE TABLE IF NOT EXISTS external_lucky_gift_draws (
  app_code VARCHAR(32) NOT NULL COMMENT '外部 App 应用编码',
  request_id VARCHAR(128) NOT NULL COMMENT '外部 App 业务幂等 ID',
  draw_id VARCHAR(128) NOT NULL COMMENT 'HyApp 抽奖 ID',
  external_user_id VARCHAR(128) NOT NULL COMMENT '外部 App 用户 ID',
  pool_id VARCHAR(96) NOT NULL COMMENT '幸运礼物奖池 ID',
  gift_count BIGINT NOT NULL COMMENT '礼物数量',
  unit_amount BIGINT NOT NULL COMMENT '单个礼物金额，当前按 COIN 等价单位',
  total_amount BIGINT NOT NULL COMMENT '总金额，必须等于 gift_count * unit_amount',
  currency VARCHAR(16) NOT NULL DEFAULT 'COIN' COMMENT '金额币种',
  metadata_json JSON NOT NULL COMMENT '外部 App 扩展元数据',
  rule_version BIGINT NOT NULL DEFAULT 0 COMMENT '命中规则版本；0 表示未启用规则',
  selected_tier_id VARCHAR(96) NOT NULL DEFAULT '' COMMENT '命中奖档',
  multiplier_ppm BIGINT NOT NULL DEFAULT 0 COMMENT '倍率，1x = 1000000',
  reward_amount BIGINT NOT NULL DEFAULT 0 COMMENT '返回给外部 App 处理的奖励金额',
  reward_status VARCHAR(32) NOT NULL DEFAULT 'granted' COMMENT 'granted/failed；不代表 HyApp wallet 入账',
  paid_at_ms BIGINT NOT NULL COMMENT '外部 App 扣费完成时间，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, request_id),
  UNIQUE KEY uk_external_lucky_draw_id (app_code, draw_id),
  KEY idx_external_lucky_user (app_code, external_user_id, created_at_ms),
  KEY idx_external_lucky_pool (app_code, pool_id, created_at_ms),
  KEY idx_external_lucky_status (app_code, reward_status, created_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='外部 App 幸运礼物抽奖事实表';

CREATE TABLE IF NOT EXISTS external_lucky_gift_draw_items (
  app_code VARCHAR(32) NOT NULL COMMENT '外部 App 应用编码',
  request_id VARCHAR(128) NOT NULL COMMENT '聚合请求幂等 ID',
  item_index INT NOT NULL COMMENT '从1开始的顺序子抽序号',
  draw_id VARCHAR(128) NOT NULL COMMENT '子抽事实 ID',
  command_id VARCHAR(128) NOT NULL COMMENT '稳定子命令 ID',
  external_user_id VARCHAR(128) NOT NULL COMMENT '外部 App 用户 ID',
  pool_id VARCHAR(96) NOT NULL COMMENT '幸运礼物奖池 ID',
  rule_version BIGINT NOT NULL COMMENT '不可变规则版本',
  unit_amount BIGINT NOT NULL COMMENT '本抽实际金额',
  reward_amount BIGINT NOT NULL COMMENT '本抽奖励金额',
  selected_tier_id VARCHAR(96) NOT NULL COMMENT '本抽最终奖档',
  multiplier_ppm BIGINT NOT NULL COMMENT '本抽倍率',
  stage VARCHAR(32) NOT NULL COMMENT '充值分层；缺可信外部充值事实时为novice',
  candidate_snapshot_json JSON NOT NULL COMMENT '动态权重、删除重抽及大奖条件快照',
  pool_snapshot_json JSON NOT NULL COMMENT '本抽资金拆分和池快照',
  rtp_snapshot_json JSON NOT NULL COMMENT '本抽RTP快照',
  reward_status VARCHAR(32) NOT NULL DEFAULT 'granted' COMMENT '外部调用方自行结算',
  paid_at_ms BIGINT NOT NULL COMMENT '外部扣费时间，UTC epoch ms',
  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
  PRIMARY KEY (app_code, request_id, item_index),
  UNIQUE KEY uk_external_lucky_item_draw (app_code, draw_id),
  KEY idx_external_lucky_item_user (app_code, external_user_id, created_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='外部dynamic_v3顺序子抽审计';
