SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;


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

USE hyapp_room;

CREATE TABLE IF NOT EXISTS rooms (
  app_code VARCHAR(32) NOT NULL COMMENT '应用编码，用于多租户隔离',
  room_id VARCHAR(64) NOT NULL COMMENT '房间 ID',
  room_short_id VARCHAR(32) NOT NULL DEFAULT '' COMMENT '房间短号 ID',
  owner_user_id BIGINT NOT NULL COMMENT '房主用户 ID',
  seat_count INT NOT NULL COMMENT '麦位数量',
  mode VARCHAR(64) NOT NULL COMMENT '业务模式',
  status VARCHAR(32) NOT NULL COMMENT '业务状态',
  room_password_hash VARCHAR(128) NOT NULL DEFAULT '' COMMENT '房间密码哈希值',
  close_reason VARCHAR(512) NOT NULL DEFAULT '' COMMENT '关闭原因',
  closed_by_admin_id BIGINT UNSIGNED NOT NULL DEFAULT 0 COMMENT '关闭操作管理员 ID',
  closed_by_admin_name VARCHAR(128) NOT NULL DEFAULT '' COMMENT '关闭操作管理员名称',
  closed_at_ms BIGINT NOT NULL DEFAULT 0 COMMENT '关闭时间，UTC epoch ms',
  visible_region_id BIGINT NOT NULL DEFAULT 0 COMMENT '可见区域 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, room_id),
  UNIQUE KEY uk_rooms_short_id (app_code, room_short_id),
  UNIQUE KEY uk_rooms_owner_user (app_code, owner_user_id),
  KEY idx_rooms_region_status (app_code, visible_region_id, status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='房间核心状态持久表';

CREATE TABLE IF NOT EXISTS room_list_entries (
  app_code VARCHAR(32) NOT NULL COMMENT '应用编码，用于多租户隔离',
  room_id VARCHAR(64) NOT NULL COMMENT '房间 ID',
  room_short_id VARCHAR(32) NOT NULL DEFAULT '' COMMENT '房间短号 ID',
  visible_region_id BIGINT NOT NULL DEFAULT 0 COMMENT '可见区域 ID',
  owner_user_id BIGINT NOT NULL COMMENT '房主用户 ID',
  title VARCHAR(128) NOT NULL DEFAULT '' COMMENT '标题',
  cover_url VARCHAR(512) NOT NULL DEFAULT '' COMMENT '封面 URL',
  mode VARCHAR(64) NOT NULL COMMENT '业务模式',
  status VARCHAR(32) NOT NULL COMMENT '业务状态',
  locked BOOLEAN NOT NULL DEFAULT FALSE COMMENT '是否锁定',
  close_reason VARCHAR(512) NOT NULL DEFAULT '' COMMENT '关闭原因',
  closed_by_admin_id BIGINT UNSIGNED NOT NULL DEFAULT 0 COMMENT '关闭操作管理员 ID',
  closed_by_admin_name VARCHAR(128) NOT NULL DEFAULT '' COMMENT '关闭操作管理员名称',
  closed_at_ms BIGINT NOT NULL DEFAULT 0 COMMENT '关闭时间，UTC epoch ms',
  heat BIGINT NOT NULL DEFAULT 0 COMMENT '热度值',
  online_count INT NOT NULL DEFAULT 0 COMMENT '在线数量',
  seat_count INT NOT NULL DEFAULT 0 COMMENT '麦位数量',
  occupied_seat_count INT NOT NULL DEFAULT 0 COMMENT '占用麦位数量',
  sort_score 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, room_id),
  KEY idx_room_list_short_id (app_code, room_short_id),
  KEY idx_room_list_region_hot (app_code, visible_region_id, status, sort_score DESC, room_id),
  KEY idx_room_list_region_new (app_code, visible_region_id, status, created_at_ms DESC, room_id),
  KEY idx_room_list_owner (app_code, owner_user_id, updated_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='房间列表项表';

CREATE TABLE IF NOT EXISTS room_user_presence (
  app_code VARCHAR(32) NOT NULL COMMENT '应用编码，用于多租户隔离',
  user_id BIGINT NOT NULL COMMENT '用户 ID',
  room_id VARCHAR(64) NOT NULL COMMENT '房间 ID',
  role VARCHAR(32) NOT NULL COMMENT '角色',
  publish_state VARCHAR(32) NOT NULL DEFAULT '' COMMENT '推流状态',
  mic_session_id VARCHAR(128) NOT NULL DEFAULT '' COMMENT '麦位会话 ID',
  room_version BIGINT NOT NULL COMMENT 'Room Cell 状态版本',
  status VARCHAR(32) NOT NULL COMMENT '业务状态',
  joined_at_ms BIGINT NOT NULL DEFAULT 0 COMMENT '进房时间，UTC epoch ms',
  last_seen_at_ms BIGINT NOT NULL DEFAULT 0 COMMENT '最后活跃时间，UTC epoch ms',
  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
  PRIMARY KEY (app_code, user_id),
  KEY idx_room_user_presence_room (app_code, room_id, status),
  KEY idx_room_user_presence_updated (app_code, status, updated_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='房间用户在线状态表';

CREATE TABLE IF NOT EXISTS room_user_gift_stats (
  app_code VARCHAR(32) NOT NULL COMMENT '应用编码，用于多租户隔离',
  room_id VARCHAR(64) NOT NULL COMMENT '房间 ID',
  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, user_id),
  KEY idx_room_user_gift_stats_room_value (app_code, room_id, gift_value DESC, last_gift_at_ms DESC, user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='房间用户送礼价值统计表';

CREATE TABLE IF NOT EXISTS room_background_images (
  app_code VARCHAR(32) NOT NULL COMMENT '应用编码，用于多租户隔离',
  background_id BIGINT NOT NULL AUTO_INCREMENT COMMENT '背景图 ID',
  room_id VARCHAR(64) NOT NULL COMMENT '房间 ID',
  image_url VARCHAR(256) NOT NULL COMMENT '背景图 URL',
  created_by_user_id BIGINT NOT NULL COMMENT '保存背景图的用户 ID',
  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
  PRIMARY KEY (background_id),
  UNIQUE KEY uk_room_background_url (app_code, room_id, image_url),
  KEY idx_room_background_list (app_code, room_id, created_at_ms DESC, background_id DESC)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='房间背景图素材表';

CREATE TABLE IF NOT EXISTS room_snapshots (
  app_code VARCHAR(32) NOT NULL COMMENT '应用编码，用于多租户隔离',
  room_id VARCHAR(64) NOT NULL COMMENT '房间 ID',
  room_version BIGINT NOT NULL COMMENT 'Room Cell 状态版本',
  payload LONGBLOB NOT 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, room_id),
  INDEX idx_room_snapshots_version (app_code, room_id, room_version)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Room Cell 快照恢复表';

CREATE TABLE IF NOT EXISTS room_command_log (
  app_code VARCHAR(32) NOT NULL COMMENT '应用编码，用于多租户隔离',
  id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT '主键 ID',
  room_id VARCHAR(64) NOT NULL COMMENT '房间 ID',
  room_version BIGINT NOT NULL COMMENT 'Room Cell 状态版本',
  command_id VARCHAR(128) NOT NULL COMMENT '业务命令幂等 ID',
  command_type VARCHAR(64) NOT NULL COMMENT '命令类型',
  owner_node_id VARCHAR(128) NOT NULL DEFAULT '' COMMENT '房主节点 ID',
  lease_token VARCHAR(128) NOT NULL DEFAULT '' COMMENT '租约令牌',
  payload LONGBLOB NOT NULL COMMENT '业务负载快照',
  replayable BOOLEAN NOT NULL COMMENT '是否可重放',
  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
  UNIQUE KEY uk_room_command (app_code, room_id, command_id),
  KEY idx_room_command_replay (app_code, room_id, room_version)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Room Cell 命令日志表';

CREATE TABLE IF NOT EXISTS room_outbox (
  app_code VARCHAR(32) NOT NULL COMMENT '应用编码，用于多租户隔离',
  event_id VARCHAR(128) NOT NULL COMMENT '事件幂等 ID',
  event_type VARCHAR(64) NOT NULL COMMENT '事件类型',
  room_id VARCHAR(64) NOT NULL COMMENT '房间 ID',
  status VARCHAR(32) NOT NULL COMMENT '业务状态',
  worker_id VARCHAR(128) NOT NULL DEFAULT '' COMMENT 'worker ID',
  lock_until_ms BIGINT NULL COMMENT '锁定过期时间，UTC epoch ms',
  envelope LONGBLOB NOT NULL COMMENT '事件信封',
  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
  retry_count INT NOT NULL DEFAULT 0 COMMENT '重试次数',
  next_retry_at_ms BIGINT NOT NULL DEFAULT 0 COMMENT '下一次重试时间，UTC epoch ms',
  last_error TEXT NULL COMMENT '最后一次失败原因',
  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
  PRIMARY KEY (app_code, event_id),
  KEY idx_room_outbox_pending (app_code, status, next_retry_at_ms, created_at_ms, event_id),
  KEY idx_room_outbox_retry (app_code, status, next_retry_at_ms, created_at_ms, event_id),
  KEY idx_room_outbox_claim (app_code, status, lock_until_ms, created_at_ms, event_id),
  KEY idx_room_outbox_event_created (app_code, event_type, created_at_ms, event_id),
  KEY idx_room_outbox_retention (app_code, status, updated_at_ms, event_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='房间事件 outbox 表';

CREATE TABLE IF NOT EXISTS room_user_feed_entries (
  app_code VARCHAR(32) NOT NULL COMMENT '应用编码，用于多租户隔离',
  user_id BIGINT NOT NULL COMMENT '用户 ID',
  feed_type VARCHAR(32) NOT NULL COMMENT '动态类型',
  room_id VARCHAR(64) NOT NULL COMMENT '房间 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, user_id, feed_type, room_id),
  KEY idx_room_user_feed_list (app_code, user_id, feed_type, updated_at_ms DESC, room_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='房间用户动态项表';

CREATE TABLE IF NOT EXISTS room_follows (
  app_code VARCHAR(32) NOT NULL COMMENT '应用编码，用于多租户隔离',
  user_id BIGINT NOT NULL COMMENT '用户 ID',
  room_id VARCHAR(64) NOT NULL COMMENT '房间 ID',
  status VARCHAR(32) NOT NULL COMMENT '业务状态',
  followed_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, room_id),
  KEY idx_room_follows_user_list (app_code, user_id, status, followed_at_ms DESC, room_id),
  KEY idx_room_follows_room (app_code, room_id, status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='房间关注表';

CREATE TABLE IF NOT EXISTS room_region_pins (
  id BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键 ID',
  app_code VARCHAR(32) NOT NULL COMMENT '应用编码，用于多租户隔离',
  visible_region_id BIGINT NOT NULL COMMENT '可见区域 ID',
  pin_type VARCHAR(32) NOT NULL DEFAULT 'region' COMMENT '置顶类型：region 区域置顶，global 全区置顶',
  room_id VARCHAR(64) NOT NULL COMMENT '房间 ID',
  weight BIGINT NOT NULL DEFAULT 0 COMMENT '权重',
  status VARCHAR(32) NOT NULL COMMENT '业务状态',
  pinned_at_ms BIGINT NOT NULL COMMENT '置顶时间，UTC epoch ms',
  expires_at_ms BIGINT NOT NULL COMMENT '过期时间，UTC epoch ms',
  cancelled_at_ms BIGINT NOT NULL DEFAULT 0 COMMENT '取消时间，UTC epoch ms',
  created_by_admin_id BIGINT UNSIGNED NOT NULL DEFAULT 0 COMMENT '创建管理员 ID',
  cancelled_by_admin_id BIGINT UNSIGNED NOT NULL DEFAULT 0 COMMENT '取消操作管理员 ID',
  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
  PRIMARY KEY (id),
  UNIQUE KEY uk_room_region_pins_scope (app_code, pin_type, visible_region_id, room_id),
  KEY idx_room_region_pins_active_scope (app_code, pin_type, visible_region_id, status, pinned_at_ms, expires_at_ms, weight DESC, room_id),
  KEY idx_room_region_pins_room (app_code, room_id, status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='房间置顶表';

CREATE TABLE IF NOT EXISTS room_seat_configs (
  app_code VARCHAR(32) NOT NULL COMMENT '应用编码，用于多租户隔离',
  allowed_seat_counts VARCHAR(128) NOT NULL COMMENT '允许麦位数量',
  default_seat_count INT NOT 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)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='房间麦位配置表';

CREATE TABLE IF NOT EXISTS room_rocket_configs (
  app_code VARCHAR(32) NOT NULL COMMENT '应用编码，用于多租户隔离',
  enabled TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否启用房间火箭',
  config_version BIGINT NOT NULL DEFAULT 1 COMMENT '配置版本，后台每次保存递增',
  fuel_source VARCHAR(32) NOT NULL COMMENT '燃料来源，默认按送礼价值累计',
  launch_delay_ms BIGINT NOT NULL COMMENT '燃料满后发射倒计时，UTC 毫秒时长',
  broadcast_enabled TINYINT(1) NOT NULL DEFAULT 1 COMMENT '倒计时开始后是否广播',
  broadcast_scope VARCHAR(32) NOT NULL COMMENT '广播范围：none/region/global',
  broadcast_delay_ms BIGINT NOT NULL DEFAULT 0 COMMENT '广播后到发射的延迟，UTC 毫秒时长',
  reward_stack_policy VARCHAR(32) NOT NULL COMMENT '同一用户多角色命中奖励时的叠加策略',
  levels_json JSON NOT NULL COMMENT '5 级火箭物料、阈值和奖励池配置',
  gift_fuel_rules_json JSON NOT NULL COMMENT '礼物燃料倍率、固定燃料和排除规则',
  updated_by_admin_id BIGINT UNSIGNED NOT NULL DEFAULT 0 COMMENT '最后更新管理员 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),
  KEY idx_room_rocket_enabled (app_code, enabled)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='语音房火箭后台配置表';

INSERT IGNORE INTO room_seat_configs (
  app_code,
  allowed_seat_counts,
  default_seat_count,
  created_at_ms,
  updated_at_ms
) VALUES (
  'lalu',
  '[10,15,20,25,30]',
  15,
  0,
  0
);

ALTER TABLE room_rocket_configs
  MODIFY COLUMN levels_json JSON NOT NULL COMMENT '5 级火箭物料、阈值和奖励池配置';

INSERT IGNORE INTO room_rocket_configs (
  app_code,
  enabled,
  config_version,
  fuel_source,
  launch_delay_ms,
  broadcast_enabled,
  broadcast_scope,
  broadcast_delay_ms,
  reward_stack_policy,
  levels_json,
  gift_fuel_rules_json,
  updated_by_admin_id,
  created_at_ms,
  updated_at_ms
) VALUES (
  'lalu',
  0,
  1,
  'heat_value',
  30000,
  1,
  'region',
  0,
  'allow_stack',
  JSON_ARRAY(
    JSON_OBJECT('level', 1, 'fuelThreshold', 1000, 'coverUrl', '', 'animationUrl', '', 'launchAnimationUrl', '', 'launchedImageUrl', '', 'inRoomRewards', JSON_ARRAY(), 'top1Rewards', JSON_ARRAY(), 'igniterRewards', JSON_ARRAY()),
    JSON_OBJECT('level', 2, 'fuelThreshold', 3000, 'coverUrl', '', 'animationUrl', '', 'launchAnimationUrl', '', 'launchedImageUrl', '', 'inRoomRewards', JSON_ARRAY(), 'top1Rewards', JSON_ARRAY(), 'igniterRewards', JSON_ARRAY()),
    JSON_OBJECT('level', 3, 'fuelThreshold', 6000, 'coverUrl', '', 'animationUrl', '', 'launchAnimationUrl', '', 'launchedImageUrl', '', 'inRoomRewards', JSON_ARRAY(), 'top1Rewards', JSON_ARRAY(), 'igniterRewards', JSON_ARRAY()),
    JSON_OBJECT('level', 4, 'fuelThreshold', 10000, 'coverUrl', '', 'animationUrl', '', 'launchAnimationUrl', '', 'launchedImageUrl', '', 'inRoomRewards', JSON_ARRAY(), 'top1Rewards', JSON_ARRAY(), 'igniterRewards', JSON_ARRAY()),
    JSON_OBJECT('level', 5, 'fuelThreshold', 15000, 'coverUrl', '', 'animationUrl', '', 'launchAnimationUrl', '', 'launchedImageUrl', '', 'inRoomRewards', JSON_ARRAY(), 'top1Rewards', JSON_ARRAY(), 'igniterRewards', JSON_ARRAY())
  ),
  JSON_ARRAY(),
  0,
  0,
  0
);
