CREATE TABLE IF NOT EXISTS room_user_contribution_period_stats (
  app_code VARCHAR(32) NOT NULL COMMENT '应用编码，用于多租户隔离',
  room_id VARCHAR(64) NOT NULL COMMENT '房间 ID',
  period VARCHAR(16) NOT NULL COMMENT '周期类型：day/week/month',
  period_start_ms BIGINT NOT NULL COMMENT '周期开始时间，UTC epoch ms，包含',
  period_end_ms BIGINT NOT NULL COMMENT '周期结束时间，UTC epoch ms，不包含',
  user_id BIGINT NOT NULL COMMENT '送礼用户 ID',
  gift_value BIGINT NOT NULL DEFAULT 0 COMMENT '当前周期内累计送礼贡献值',
  last_gift_at_ms BIGINT NOT NULL DEFAULT 0 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, room_id, period, period_start_ms, user_id),
  KEY idx_room_contribution_period_rank (app_code, room_id, period, period_start_ms, gift_value DESC, last_gift_at_ms DESC, user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='房间用户周期贡献榜统计表';
