SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;


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

USE hyapp_wallet;

CREATE TABLE IF NOT EXISTS wallet_accounts (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码，用于多租户隔离',
  user_id BIGINT NOT NULL COMMENT '用户 ID',
  asset_type VARCHAR(32) NOT NULL COMMENT '钱包资产类型',
  available_amount BIGINT NOT NULL COMMENT '可用数量',
  frozen_amount BIGINT NOT NULL COMMENT '冻结数量',
  version BIGINT 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, user_id, asset_type)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='钱包账户余额表';

CREATE TABLE IF NOT EXISTS wallet_transactions (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码，用于多租户隔离',
  transaction_id VARCHAR(96) NOT NULL COMMENT '钱包交易 ID',
  command_id VARCHAR(128) NOT NULL COMMENT '业务命令幂等 ID',
  biz_type VARCHAR(64) NOT NULL COMMENT '业务类型',
  status VARCHAR(32) NOT NULL COMMENT '业务状态',
  request_hash VARCHAR(128) NOT NULL COMMENT '请求哈希值',
  external_ref VARCHAR(128) NOT NULL DEFAULT '' COMMENT '外部引用',
  metadata_json JSON NULL COMMENT '扩展元数据 JSON',
  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
  PRIMARY KEY (app_code, transaction_id),
  UNIQUE KEY uk_wallet_tx_command (app_code, command_id),
  KEY idx_wallet_tx_biz_time (app_code, biz_type, created_at_ms),
  KEY idx_wallet_tx_external_ref (app_code, external_ref)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='钱包交易主表';

CREATE TABLE IF NOT EXISTS wallet_entries (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码，用于多租户隔离',
  entry_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT '流水 ID',
  transaction_id VARCHAR(96) NOT NULL COMMENT '钱包交易 ID',
  user_id BIGINT NOT NULL COMMENT '用户 ID',
  asset_type VARCHAR(32) NOT NULL COMMENT '钱包资产类型',
  available_delta BIGINT NOT NULL COMMENT '可用变动',
  frozen_delta BIGINT NOT NULL COMMENT '冻结变动',
  available_after BIGINT NOT NULL COMMENT '可用后',
  frozen_after BIGINT NOT NULL COMMENT '冻结后',
  counterparty_user_id BIGINT NOT NULL DEFAULT 0 COMMENT '交易对手用户 ID',
  room_id VARCHAR(96) NOT NULL DEFAULT '' COMMENT '房间 ID',
  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
  KEY idx_wallet_entries_user_time (app_code, user_id, created_at_ms),
  KEY idx_wallet_entries_tx (app_code, transaction_id),
  KEY idx_wallet_entries_asset_time (app_code, asset_type, created_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='钱包复式流水表';

CREATE TABLE IF NOT EXISTS wallet_outbox (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码，用于多租户隔离',
  event_id VARCHAR(128) NOT NULL COMMENT '事件幂等 ID',
  event_type VARCHAR(64) NOT NULL COMMENT '事件类型',
  transaction_id VARCHAR(96) NOT NULL COMMENT '钱包交易 ID',
  command_id VARCHAR(128) NOT NULL COMMENT '业务命令幂等 ID',
  user_id BIGINT NOT NULL COMMENT '用户 ID',
  asset_type VARCHAR(32) NOT NULL COMMENT '钱包资产类型',
  available_delta BIGINT NOT NULL COMMENT '可用变动',
  frozen_delta BIGINT NOT NULL COMMENT '冻结变动',
  payload JSON NOT NULL COMMENT '业务负载快照',
  status VARCHAR(32) NOT NULL COMMENT '业务状态',
  retry_count INT NOT NULL DEFAULT 0 COMMENT '重试次数',
  last_error TEXT 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, event_id),
  KEY idx_wallet_outbox_pending (app_code, status, created_at_ms),
  KEY idx_wallet_outbox_tx (app_code, transaction_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='钱包事件 outbox 表';

CREATE TABLE IF NOT EXISTS wallet_gift_prices (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码，用于多租户隔离',
  gift_id VARCHAR(96) NOT NULL COMMENT '礼物 ID',
  price_version VARCHAR(64) NOT NULL COMMENT '价格版本',
  status VARCHAR(32) NOT NULL COMMENT '业务状态',
  charge_asset_type VARCHAR(32) NOT NULL DEFAULT 'COIN' COMMENT '扣费资产类型',
  coin_price BIGINT NOT NULL COMMENT '金币价格',
  gift_point_amount BIGINT NOT NULL COMMENT '礼物积分数量',
  heat_value BIGINT NOT NULL COMMENT '热度值',
  effective_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, gift_id, price_version),
  KEY idx_wallet_gift_prices_effective (app_code, gift_id, status, effective_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='钱包礼物价格表';

CREATE TABLE IF NOT EXISTS wallet_recharge_policies (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码，用于多租户隔离',
  policy_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT '策略 ID',
  region_id BIGINT NOT NULL COMMENT '区域 ID',
  policy_version VARCHAR(64) NOT NULL COMMENT '策略版本',
  status VARCHAR(32) NOT NULL COMMENT '业务状态',
  currency_code VARCHAR(3) NOT NULL DEFAULT 'USD' COMMENT '币种编码',
  coin_amount BIGINT NOT NULL COMMENT '金币数量',
  usd_minor_amount BIGINT NOT NULL COMMENT '美元最小单位数量',
  effective_from_ms BIGINT NOT NULL COMMENT '生效开始时间，UTC epoch ms',
  effective_to_ms BIGINT NULL COMMENT '生效结束时间，UTC epoch ms',
  created_by_user_id BIGINT NULL COMMENT '创建人用户 ID',
  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
  KEY idx_wallet_recharge_policies_active (app_code, region_id, status, effective_from_ms),
  UNIQUE KEY uk_wallet_recharge_policy_version (app_code, region_id, policy_version)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='钱包充值策略表';

CREATE TABLE IF NOT EXISTS wallet_recharge_records (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码，用于多租户隔离',
  transaction_id VARCHAR(96) NOT NULL COMMENT '钱包交易 ID',
  user_id BIGINT NOT NULL COMMENT '用户 ID',
  seller_user_id BIGINT NOT NULL COMMENT '商户用户 ID',
  seller_region_id BIGINT NOT NULL COMMENT '商户区域 ID',
  target_region_id BIGINT NOT NULL COMMENT '目标区域 ID',
  policy_id BIGINT NOT NULL COMMENT '策略 ID',
  policy_version VARCHAR(64) NOT NULL COMMENT '策略版本',
  currency_code VARCHAR(3) NOT NULL COMMENT '币种编码',
  coin_amount BIGINT NOT NULL COMMENT '金币数量',
  usd_minor_amount BIGINT NOT NULL COMMENT '美元最小单位数量',
  exchange_coin_amount BIGINT NOT NULL COMMENT '兑换金币数量',
  exchange_usd_minor_amount BIGINT NOT NULL COMMENT '兑换美元最小单位数量',
  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
  PRIMARY KEY (app_code, transaction_id),
  KEY idx_wallet_recharge_records_user_time (app_code, user_id, created_at_ms),
  KEY idx_wallet_recharge_records_seller_time (app_code, seller_user_id, created_at_ms),
  KEY idx_wallet_recharge_records_region_time (app_code, target_region_id, created_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='钱包充值记录表';

CREATE TABLE IF NOT EXISTS wallet_recharge_products (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码，用于多租户隔离',
  product_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT '商品 ID',
  product_code VARCHAR(128) NULL COMMENT '商品编码',
  product_name VARCHAR(128) NOT NULL COMMENT '商品名称',
  description VARCHAR(512) NOT NULL DEFAULT '' COMMENT '描述信息',
  platform VARCHAR(16) NOT NULL COMMENT '平台',
  channel VARCHAR(32) NOT NULL COMMENT '渠道',
  currency_code VARCHAR(8) NOT NULL DEFAULT 'USDT' COMMENT '币种编码',
  amount_micro BIGINT NOT NULL COMMENT '微单位金额',
  coin_amount BIGINT NOT NULL COMMENT '金币数量',
  resource_asset_type VARCHAR(32) NOT NULL DEFAULT 'COIN' COMMENT '资源资产类型',
  status VARCHAR(32) NOT NULL COMMENT '业务状态',
  sort_order INT NOT NULL DEFAULT 0 COMMENT '排序权重，数值越小越靠前',
  created_by_user_id BIGINT NULL COMMENT '创建人用户 ID',
  updated_by_user_id BIGINT NULL COMMENT '更新人用户 ID',
  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
  UNIQUE KEY uk_wallet_recharge_products_code (app_code, product_code),
  KEY idx_wallet_recharge_products_scope (app_code, platform, status, amount_micro, product_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='钱包充值商品表';

CREATE TABLE IF NOT EXISTS wallet_recharge_product_regions (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码，用于多租户隔离',
  product_id BIGINT NOT NULL COMMENT '商品 ID',
  region_id BIGINT NOT NULL COMMENT '区域 ID',
  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
  PRIMARY KEY (app_code, product_id, region_id),
  KEY idx_wallet_recharge_product_regions_region (app_code, region_id, product_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='钱包充值商品区域表';

CREATE TABLE IF NOT EXISTS coin_seller_stock_records (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码，用于多租户隔离',
  stock_id VARCHAR(96) NOT NULL COMMENT '库存 ID',
  transaction_id VARCHAR(96) NOT NULL COMMENT '钱包交易 ID',
  command_id VARCHAR(128) NOT NULL COMMENT '业务命令幂等 ID',
  seller_user_id BIGINT NOT NULL COMMENT '商户用户 ID',
  stock_type VARCHAR(32) NOT NULL COMMENT '库存类型',
  counts_as_seller_recharge BOOLEAN NOT NULL COMMENT '数量as商户充值',
  coin_amount BIGINT NOT NULL COMMENT '金币数量',
  paid_currency_code VARCHAR(8) NOT NULL DEFAULT '' COMMENT '付款币种编码',
  paid_amount_micro BIGINT NOT NULL DEFAULT 0 COMMENT '付款数量微单位',
  payment_ref VARCHAR(128) NULL COMMENT '支付外部引用',
  evidence_ref VARCHAR(256) NOT NULL DEFAULT '' COMMENT '凭证外部引用',
  operator_user_id BIGINT NOT NULL COMMENT '操作人用户 ID',
  reason VARCHAR(512) NOT NULL DEFAULT '' COMMENT '原因',
  balance_after BIGINT NOT NULL COMMENT '余额后',
  metadata_json JSON NULL COMMENT '扩展元数据 JSON',
  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
  PRIMARY KEY (app_code, stock_id),
  UNIQUE KEY uk_coin_seller_stock_command (app_code, command_id),
  UNIQUE KEY uk_coin_seller_stock_payment_ref (app_code, stock_type, payment_ref),
  KEY idx_coin_seller_stock_seller_time (app_code, seller_user_id, created_at_ms),
  KEY idx_coin_seller_stock_type_time (app_code, stock_type, created_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='金币商户库存记录表';

CREATE TABLE IF NOT EXISTS resources (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码，用于多租户隔离',
  resource_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT '资源 ID',
  resource_code VARCHAR(96) NOT NULL COMMENT '资源编码',
  resource_type VARCHAR(32) NOT NULL COMMENT '资源类型',
  name VARCHAR(128) NOT NULL COMMENT '名称',
  status VARCHAR(32) NOT NULL COMMENT '业务状态',
  grantable BOOLEAN NOT NULL DEFAULT TRUE COMMENT '是否允许发放',
  manager_grant_enabled BOOLEAN NOT NULL DEFAULT TRUE COMMENT '管理端是否允许发放',
  grant_strategy VARCHAR(32) NOT NULL COMMENT '发放策略',
  wallet_asset_type VARCHAR(32) NOT NULL DEFAULT '' COMMENT '钱包资产类型',
  wallet_asset_amount BIGINT NOT NULL DEFAULT 0 COMMENT '钱包资产数量',
  price_type VARCHAR(32) NOT NULL DEFAULT '' COMMENT '资源价格类型：coin/free',
  coin_price BIGINT NOT NULL DEFAULT 0 COMMENT '资源金币价格',
  gift_point_amount BIGINT NOT NULL DEFAULT 0 COMMENT '资源礼物积分',
  usage_scope_json JSON NOT NULL COMMENT '使用范围 JSON 配置或快照',
  asset_url VARCHAR(512) NOT NULL DEFAULT '' COMMENT '资产 URL',
  preview_url VARCHAR(512) NOT NULL DEFAULT '' COMMENT '预览 URL',
  animation_url VARCHAR(512) NOT NULL DEFAULT '' COMMENT '动画 URL',
  metadata_json JSON NULL COMMENT '扩展元数据 JSON',
  sort_order INT NOT NULL DEFAULT 0 COMMENT '排序权重，数值越小越靠前',
  created_by_user_id BIGINT NOT NULL DEFAULT 0 COMMENT '创建人用户 ID',
  updated_by_user_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',
  UNIQUE KEY uk_resources_code (app_code, resource_code),
  KEY idx_resources_type_status (app_code, resource_type, status, sort_order),
  KEY idx_resources_manager_grant (app_code, manager_grant_enabled, status, grantable, sort_order)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='资源表';

SET @ddl := IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'resources' AND COLUMN_NAME = 'manager_grant_enabled') = 0,
  'ALTER TABLE resources ADD COLUMN manager_grant_enabled BOOLEAN NOT NULL DEFAULT TRUE COMMENT ''管理端是否允许发放'' AFTER grantable',
  'SELECT 1'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl := IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'resources' AND COLUMN_NAME = 'price_type') = 0,
  'ALTER TABLE resources ADD COLUMN price_type VARCHAR(32) NOT NULL DEFAULT '''' COMMENT ''资源价格类型：coin/free'' AFTER wallet_asset_amount',
  'SELECT 1'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl := IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'resources' AND COLUMN_NAME = 'coin_price') = 0,
  'ALTER TABLE resources ADD COLUMN coin_price BIGINT NOT NULL DEFAULT 0 COMMENT ''资源金币价格'' AFTER price_type',
  'SELECT 1'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl := IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'resources' AND COLUMN_NAME = 'gift_point_amount') = 0,
  'ALTER TABLE resources ADD COLUMN gift_point_amount BIGINT NOT NULL DEFAULT 0 COMMENT ''资源礼物积分'' AFTER coin_price',
  '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 = 'resources' AND INDEX_NAME = 'idx_resources_manager_grant') = 0,
  'ALTER TABLE resources ADD KEY idx_resources_manager_grant (app_code, manager_grant_enabled, status, grantable, sort_order)',
  'SELECT 1'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

CREATE TABLE IF NOT EXISTS resource_groups (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码，用于多租户隔离',
  group_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT '分组 ID',
  group_code VARCHAR(96) NOT NULL COMMENT '分组编码',
  name VARCHAR(128) NOT NULL COMMENT '名称',
  status VARCHAR(32) NOT NULL COMMENT '业务状态',
  description VARCHAR(512) NOT NULL DEFAULT '' COMMENT '描述信息',
  sort_order INT NOT NULL DEFAULT 0 COMMENT '排序权重，数值越小越靠前',
  created_by_user_id BIGINT NOT NULL DEFAULT 0 COMMENT '创建人用户 ID',
  updated_by_user_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',
  UNIQUE KEY uk_resource_groups_code (app_code, group_code),
  KEY idx_resource_groups_status (app_code, status, sort_order)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='资源分组表';

CREATE TABLE IF NOT EXISTS resource_group_items (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码，用于多租户隔离',
  group_item_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT '分组明细 ID',
  group_id BIGINT NOT NULL COMMENT '分组 ID',
  item_type VARCHAR(32) NOT NULL DEFAULT 'resource' COMMENT '明细类型',
  resource_id BIGINT NOT NULL DEFAULT 0 COMMENT '资源 ID',
  wallet_asset_type VARCHAR(32) NOT NULL DEFAULT '' COMMENT '钱包资产类型',
  wallet_asset_amount BIGINT NOT NULL DEFAULT 0 COMMENT '钱包资产数量',
  quantity BIGINT NOT NULL DEFAULT 1 COMMENT '数量',
  duration_ms BIGINT NOT NULL DEFAULT 0 COMMENT '持续时长，毫秒',
  sort_order INT 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',
  UNIQUE KEY uk_resource_group_items_member (app_code, group_id, item_type, resource_id, wallet_asset_type),
  KEY idx_resource_group_items_group (app_code, group_id, sort_order)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='资源分组明细表';

CREATE TABLE IF NOT EXISTS gift_type_configs (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码，用于多租户隔离',
  type_code VARCHAR(32) NOT NULL COMMENT '礼物类型编码，供 gift_configs 引用',
  name VARCHAR(64) NOT NULL COMMENT '后台展示名称',
  tab_key VARCHAR(32) NOT NULL COMMENT 'App 礼物面板 tab 展示 key',
  status VARCHAR(32) NOT NULL DEFAULT 'active' COMMENT 'tab 状态，active 才在 App 面板展示',
  sort_order INT NOT NULL DEFAULT 0 COMMENT 'tab 排序权重，数值越小越靠前',
  created_by_user_id BIGINT NOT NULL DEFAULT 0 COMMENT '创建人用户 ID',
  updated_by_user_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, type_code),
  UNIQUE KEY uk_gift_type_configs_tab_key (app_code, tab_key),
  KEY idx_gift_type_configs_status_sort (app_code, status, sort_order)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='礼物类型和 App 面板 tab 配置表';

CREATE TABLE IF NOT EXISTS gift_configs (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码，用于多租户隔离',
  gift_id VARCHAR(96) NOT NULL COMMENT '礼物 ID',
  resource_id BIGINT NOT NULL COMMENT '资源 ID',
  status VARCHAR(32) NOT NULL COMMENT '业务状态',
  name VARCHAR(128) NOT NULL COMMENT '名称',
  sort_order INT NOT NULL DEFAULT 0 COMMENT '排序权重，数值越小越靠前',
  presentation_json JSON NULL COMMENT '展示配置 JSON 配置或快照',
  gift_type_code VARCHAR(32) NOT NULL DEFAULT 'normal' COMMENT '礼物类型编码',
  effective_from_ms BIGINT NOT NULL DEFAULT 0 COMMENT '生效开始时间，UTC epoch ms',
  effective_to_ms BIGINT NOT NULL DEFAULT 0 COMMENT '生效结束时间，UTC epoch ms',
  effect_types_json JSON NULL COMMENT '礼物特效类型 JSON 配置',
  created_by_user_id BIGINT NOT NULL DEFAULT 0 COMMENT '创建人用户 ID',
  updated_by_user_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, gift_id),
  KEY idx_gift_configs_resource (app_code, resource_id),
  KEY idx_gift_configs_status_sort (app_code, status, sort_order),
  KEY idx_gift_configs_effective (app_code, status, effective_from_ms, effective_to_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='礼物配置表';

SET @ddl := IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'wallet_gift_prices' AND COLUMN_NAME = 'charge_asset_type') = 0,
  'ALTER TABLE wallet_gift_prices ADD COLUMN charge_asset_type VARCHAR(32) NOT NULL DEFAULT ''COIN'' COMMENT ''扣费资产类型'' AFTER status',
  'SELECT 1'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl := IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'gift_configs' AND COLUMN_NAME = 'gift_type_code') = 0,
  'ALTER TABLE gift_configs ADD COLUMN gift_type_code VARCHAR(32) NOT NULL DEFAULT ''normal'' COMMENT ''礼物类型编码'' AFTER presentation_json',
  'SELECT 1'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl := IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'gift_configs' AND COLUMN_NAME = 'effective_from_ms') = 0,
  'ALTER TABLE gift_configs ADD COLUMN effective_from_ms BIGINT NOT NULL DEFAULT 0 COMMENT ''生效开始时间，UTC epoch ms'' AFTER gift_type_code',
  'SELECT 1'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl := IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'gift_configs' AND COLUMN_NAME = 'effective_to_ms') = 0,
  'ALTER TABLE gift_configs ADD COLUMN effective_to_ms BIGINT NOT NULL DEFAULT 0 COMMENT ''生效结束时间，UTC epoch ms'' AFTER effective_from_ms',
  'SELECT 1'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @ddl := IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'gift_configs' AND COLUMN_NAME = 'effect_types_json') = 0,
  'ALTER TABLE gift_configs ADD COLUMN effect_types_json JSON NULL COMMENT ''礼物特效类型 JSON 配置'' AFTER effective_to_ms',
  'SELECT 1'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

CREATE TABLE IF NOT EXISTS gift_config_regions (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码，用于多租户隔离',
  gift_id VARCHAR(96) NOT NULL COMMENT '礼物 ID',
  region_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 (app_code, gift_id, region_id),
  KEY idx_gift_config_regions_region (app_code, region_id, gift_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='礼物配置区域表';

CREATE TABLE IF NOT EXISTS user_gift_wall (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码，用于多租户隔离',
  user_id BIGINT NOT NULL COMMENT '用户 ID',
  gift_id VARCHAR(96) NOT NULL COMMENT '礼物 ID',
  gift_name VARCHAR(128) NOT NULL COMMENT '礼物名称',
  resource_id BIGINT NOT NULL DEFAULT 0 COMMENT '资源 ID',
  resource_snapshot_json JSON NOT NULL COMMENT '资源快照 JSON',
  gift_type_code VARCHAR(32) NOT NULL DEFAULT 'normal' COMMENT '礼物类型编码',
  presentation_json JSON NULL COMMENT '展示配置 JSON 配置或快照',
  gift_count BIGINT NOT NULL DEFAULT 0 COMMENT '礼物数量',
  total_value BIGINT NOT NULL DEFAULT 0 COMMENT '总值',
  total_coin_value BIGINT NOT NULL DEFAULT 0 COMMENT '总金币值',
  total_diamond_value BIGINT NOT NULL DEFAULT 0 COMMENT '总钻石值',
  total_gift_point BIGINT NOT NULL DEFAULT 0 COMMENT '总礼物积分',
  total_heat_value BIGINT NOT NULL DEFAULT 0 COMMENT '总热度值',
  charge_asset_type VARCHAR(32) NOT NULL DEFAULT 'COIN' COMMENT '扣费资产类型',
  last_price_version VARCHAR(64) NOT NULL DEFAULT '' COMMENT '最后价格版本',
  first_received_at_ms BIGINT NOT NULL DEFAULT 0 COMMENT '首次收到时间，UTC epoch ms',
  last_received_at_ms BIGINT NOT NULL DEFAULT 0 COMMENT '最后收到时间，UTC epoch ms',
  sort_order INT 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, user_id, gift_id),
  KEY idx_user_gift_wall_user_sort (app_code, user_id, sort_order, total_value, last_received_at_ms),
  KEY idx_user_gift_wall_gift (app_code, gift_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户礼物墙表';

CREATE TABLE IF NOT EXISTS wallet_projection_events (
  projection_name VARCHAR(64) NOT NULL COMMENT '投影名称',
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码，用于多租户隔离',
  event_id VARCHAR(128) NOT NULL COMMENT '事件幂等 ID',
  status VARCHAR(32) NOT NULL COMMENT '业务状态',
  retry_count INT NOT NULL DEFAULT 0 COMMENT '重试次数',
  locked_by VARCHAR(96) NOT NULL DEFAULT '' COMMENT '当前锁定 worker',
  locked_until_ms BIGINT NOT NULL DEFAULT 0 COMMENT '锁定截止时间，UTC epoch ms',
  last_error TEXT NULL COMMENT '最后一次失败原因',
  processed_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 (projection_name, app_code, event_id),
  KEY idx_wallet_projection_claim (projection_name, app_code, status, locked_until_ms, updated_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='钱包投影事件表';

CREATE TABLE IF NOT EXISTS resource_grants (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码，用于多租户隔离',
  grant_id VARCHAR(96) NOT NULL COMMENT '发放 ID',
  command_id VARCHAR(128) NOT NULL COMMENT '业务命令幂等 ID',
  target_user_id BIGINT NOT NULL COMMENT '目标用户 ID',
  grant_source VARCHAR(32) NOT NULL COMMENT '发放来源',
  grant_subject_type VARCHAR(32) NOT NULL COMMENT '发放主体类型',
  grant_subject_id VARCHAR(96) NOT NULL COMMENT '发放主体 ID',
  status VARCHAR(32) NOT NULL COMMENT '业务状态',
  request_hash VARCHAR(128) NOT NULL COMMENT '请求哈希值',
  group_snapshot_json JSON NULL COMMENT '资源分组快照 JSON',
  reason VARCHAR(256) NOT NULL COMMENT '原因',
  operator_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 (app_code, grant_id),
  UNIQUE KEY uk_resource_grants_command (app_code, command_id),
  KEY idx_resource_grants_target_time (app_code, target_user_id, created_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='资源发放表';

CREATE TABLE IF NOT EXISTS resource_grant_items (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码，用于多租户隔离',
  grant_item_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT '发放明细 ID',
  grant_id VARCHAR(96) NOT NULL COMMENT '发放 ID',
  resource_id BIGINT NOT NULL COMMENT '资源 ID',
  resource_snapshot_json JSON NOT NULL COMMENT '资源快照 JSON',
  quantity BIGINT NOT NULL DEFAULT 1 COMMENT '数量',
  duration_ms BIGINT NOT NULL DEFAULT 0 COMMENT '持续时长，毫秒',
  result_type VARCHAR(32) NOT NULL COMMENT '结果类型',
  wallet_transaction_id VARCHAR(96) NOT NULL DEFAULT '' COMMENT '钱包交易 ID',
  entitlement_id VARCHAR(96) NOT NULL DEFAULT '' COMMENT '权益 ID',
  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
  KEY idx_resource_grant_items_grant (app_code, grant_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='资源发放明细表';

CREATE TABLE IF NOT EXISTS user_resource_entitlements (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码，用于多租户隔离',
  entitlement_id VARCHAR(96) NOT NULL COMMENT '权益 ID',
  user_id BIGINT NOT NULL COMMENT '用户 ID',
  resource_id BIGINT NOT NULL COMMENT '资源 ID',
  status VARCHAR(32) NOT NULL COMMENT '业务状态',
  quantity BIGINT NOT NULL DEFAULT 1 COMMENT '数量',
  remaining_quantity BIGINT NOT NULL DEFAULT 1 COMMENT '剩余数量',
  effective_at_ms BIGINT NOT NULL COMMENT '生效时间，UTC epoch ms',
  expires_at_ms BIGINT NOT NULL DEFAULT 0 COMMENT '过期时间，UTC epoch ms',
  source_grant_id VARCHAR(96) 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, entitlement_id),
  KEY idx_user_resource_active (app_code, user_id, resource_id, status, expires_at_ms),
  KEY idx_user_resource_user_type (app_code, user_id, status, updated_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户资源权益表';

CREATE TABLE IF NOT EXISTS user_resource_equipment (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码，用于多租户隔离',
  user_id BIGINT NOT NULL COMMENT '用户 ID',
  resource_type VARCHAR(32) NOT NULL COMMENT '资源类型',
  entitlement_id VARCHAR(96) NOT NULL COMMENT '权益 ID',
  resource_id BIGINT NOT NULL COMMENT '资源 ID',
  updated_at_ms BIGINT NOT NULL COMMENT '更新时间，UTC epoch ms',
  PRIMARY KEY (app_code, user_id, resource_type),
  KEY idx_user_resource_equipment_user (app_code, user_id, updated_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户资源装备表';

CREATE TABLE IF NOT EXISTS wallet_diamond_exchange_rules (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码，用于多租户隔离',
  rule_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT '规则 ID',
  exchange_type VARCHAR(32) NOT NULL COMMENT '兑换类型',
  status VARCHAR(32) NOT NULL COMMENT '业务状态',
  from_asset_type VARCHAR(32) NOT NULL DEFAULT 'DIAMOND' COMMENT '源资产类型',
  to_asset_type VARCHAR(32) NOT NULL COMMENT '目标资产类型',
  from_amount BIGINT NOT NULL COMMENT '源数量',
  to_amount BIGINT NOT NULL COMMENT '目标数量',
  sort_order INT 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',
  UNIQUE KEY uk_diamond_exchange_rule (app_code, exchange_type, from_amount, to_asset_type),
  KEY idx_diamond_exchange_active (app_code, status, sort_order)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='钱包钻石兑换规则表';

CREATE TABLE IF NOT EXISTS wallet_withdrawal_requests (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码，用于多租户隔离',
  withdrawal_id VARCHAR(96) NOT NULL COMMENT '提现 ID',
  command_id VARCHAR(128) NOT NULL COMMENT '业务命令幂等 ID',
  user_id BIGINT NOT NULL COMMENT '用户 ID',
  asset_type VARCHAR(32) NOT NULL COMMENT '钱包资产类型',
  amount BIGINT NOT NULL COMMENT '金额或数量',
  status VARCHAR(32) NOT NULL COMMENT '业务状态',
  payout_account VARCHAR(256) NOT NULL COMMENT '出款账户',
  reason VARCHAR(512) NOT NULL DEFAULT '' COMMENT '原因',
  wallet_transaction_id VARCHAR(96) NOT NULL COMMENT '钱包交易 ID',
  reviewed_by_user_id BIGINT NULL COMMENT '审核用户 ID',
  review_reason VARCHAR(512) NOT NULL DEFAULT '' COMMENT '审核原因',
  reviewed_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, withdrawal_id),
  UNIQUE KEY uk_withdrawal_command (app_code, command_id),
  KEY idx_withdrawal_user_time (app_code, user_id, created_at_ms),
  KEY idx_withdrawal_status_time (app_code, status, created_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='钱包提现申请表';

CREATE TABLE IF NOT EXISTS vip_levels (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码，用于多租户隔离',
  level INT NOT NULL COMMENT '等级',
  name VARCHAR(64) NOT NULL COMMENT '名称',
  status VARCHAR(32) NOT NULL COMMENT '业务状态',
  price_coin BIGINT NOT NULL COMMENT 'VIP 价格金币数',
  duration_ms BIGINT NOT NULL COMMENT '持续时长，毫秒',
  reward_resource_group_id BIGINT NOT NULL DEFAULT 0 COMMENT '奖励资源分组 ID',
  sort_order INT 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, level),
  KEY idx_vip_levels_status_sort (app_code, status, sort_order)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='VIP 等级表';

CREATE TABLE IF NOT EXISTS user_vip_memberships (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码，用于多租户隔离',
  user_id BIGINT NOT NULL COMMENT '用户 ID',
  level INT NOT NULL COMMENT '等级',
  name VARCHAR(64) NOT NULL DEFAULT '' COMMENT '名称',
  status VARCHAR(32) NOT NULL COMMENT '业务状态',
  started_at_ms BIGINT NOT NULL DEFAULT 0 COMMENT '开始时间，UTC epoch ms',
  expires_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, user_id),
  KEY idx_user_vip_expiry (app_code, status, expires_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户 VIP 成员关系表';

CREATE TABLE IF NOT EXISTS vip_purchase_orders (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码，用于多租户隔离',
  order_id VARCHAR(96) NOT NULL COMMENT '订单 ID',
  command_id VARCHAR(128) NOT NULL COMMENT '业务命令幂等 ID',
  user_id BIGINT NOT NULL COMMENT '用户 ID',
  target_level INT NOT NULL COMMENT '目标等级',
  previous_level INT NOT NULL COMMENT '原等级',
  price_coin BIGINT NOT NULL COMMENT 'VIP 价格金币数',
  status VARCHAR(32) NOT NULL COMMENT '业务状态',
  wallet_transaction_id VARCHAR(96) NOT NULL COMMENT '钱包交易 ID',
  resource_grant_id VARCHAR(96) NOT NULL DEFAULT '' COMMENT '资源发放 ID',
  request_hash VARCHAR(128) 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, order_id),
  UNIQUE KEY uk_vip_purchase_command (app_code, command_id),
  KEY idx_vip_purchase_user_time (app_code, user_id, created_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='VIP 购买订单表';

CREATE TABLE IF NOT EXISTS user_vip_history (
  app_code VARCHAR(32) NOT NULL DEFAULT 'lalu' COMMENT '应用编码，用于多租户隔离',
  history_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT '历史 ID',
  user_id BIGINT NOT NULL COMMENT '用户 ID',
  action VARCHAR(32) NOT NULL COMMENT '操作',
  previous_level INT NOT NULL COMMENT '原等级',
  new_level INT NOT NULL COMMENT '新等级',
  order_id VARCHAR(96) NOT NULL DEFAULT '' COMMENT '订单 ID',
  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
  KEY idx_user_vip_history_user_time (app_code, user_id, created_at_ms)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户 VIP 历史表';

INSERT IGNORE INTO wallet_gift_prices (
  app_code, gift_id, price_version, status, charge_asset_type, coin_price, gift_point_amount, heat_value,
  effective_at_ms, created_at_ms, updated_at_ms
) VALUES
  ('lalu', 'rose', 'v1', 'active', 'COIN', 10, 10, 10, 0, 0, 0),
  ('lalu', 'rocket', 'v1', 'active', 'COIN', 100, 100, 100, 0, 0, 0);

INSERT IGNORE INTO resources (
  app_code, resource_code, resource_type, name, status, grantable, grant_strategy,
  wallet_asset_type, wallet_asset_amount, price_type, coin_price, gift_point_amount, usage_scope_json, asset_url, preview_url,
  animation_url, metadata_json, sort_order, created_by_user_id, updated_by_user_id,
  created_at_ms, updated_at_ms
) VALUES
  ('lalu', 'gift_rose', 'gift', 'Rose', 'active', TRUE, 'increase_quantity', '', 0, 'coin', 10, 10, JSON_ARRAY('gift'), '', '', '', JSON_OBJECT(), 10, 0, 0, 0, 0),
  ('lalu', 'gift_rocket', 'gift', 'Rocket', 'active', TRUE, 'increase_quantity', '', 0, 'coin', 100, 100, JSON_ARRAY('gift'), '', '', '', JSON_OBJECT(), 20, 0, 0, 0, 0);

INSERT IGNORE INTO resources (
  app_code, resource_code, resource_type, name, status, grantable, grant_strategy,
  wallet_asset_type, wallet_asset_amount, price_type, coin_price, gift_point_amount, usage_scope_json, asset_url, preview_url,
  animation_url, metadata_json, sort_order, created_by_user_id, updated_by_user_id,
  created_at_ms, updated_at_ms
) VALUES
  ('lalu', 'vip1_avatar_frame', 'avatar_frame', 'VIP1 Avatar Frame', 'active', TRUE, 'set_active_flag', '', 0, 'free', 0, 0, JSON_ARRAY('profile', 'room'), '', '', '', JSON_OBJECT(), 101, 0, 0, 0, 0),
  ('lalu', 'vip2_avatar_frame', 'avatar_frame', 'VIP2 Avatar Frame', 'active', TRUE, 'set_active_flag', '', 0, 'free', 0, 0, JSON_ARRAY('profile', 'room'), '', '', '', JSON_OBJECT(), 102, 0, 0, 0, 0),
  ('lalu', 'vip3_vehicle', 'vehicle', 'VIP3 Vehicle', 'active', TRUE, 'set_active_flag', '', 0, 'free', 0, 0, JSON_ARRAY('room_entry'), '', '', '', JSON_OBJECT(), 103, 0, 0, 0, 0);

INSERT IGNORE INTO resource_groups (
  app_code, group_code, name, status, description, sort_order,
  created_by_user_id, updated_by_user_id, created_at_ms, updated_at_ms
) VALUES
  ('lalu', 'vip1_rewards', 'VIP1 Rewards', 'active', 'VIP1 purchase rewards', 101, 0, 0, 0, 0),
  ('lalu', 'vip2_rewards', 'VIP2 Rewards', 'active', 'VIP2 purchase rewards', 102, 0, 0, 0, 0),
  ('lalu', 'vip3_rewards', 'VIP3 Rewards', 'active', 'VIP3 purchase rewards', 103, 0, 0, 0, 0);

INSERT IGNORE INTO resource_group_items (
  app_code, group_id, item_type, resource_id, wallet_asset_type, wallet_asset_amount,
  quantity, duration_ms, sort_order, created_at_ms, updated_at_ms
)
SELECT 'lalu', g.group_id, 'resource', r.resource_id, '', 0, 1, 604800000, 10, 0, 0
FROM resource_groups g
JOIN resources r ON r.app_code = g.app_code AND r.resource_code = 'vip1_avatar_frame'
WHERE g.app_code = 'lalu' AND g.group_code = 'vip1_rewards';

INSERT IGNORE INTO resource_group_items (
  app_code, group_id, item_type, resource_id, wallet_asset_type, wallet_asset_amount,
  quantity, duration_ms, sort_order, created_at_ms, updated_at_ms
)
SELECT 'lalu', g.group_id, 'resource', r.resource_id, '', 0, 1, 604800000, 10, 0, 0
FROM resource_groups g
JOIN resources r ON r.app_code = g.app_code AND r.resource_code = 'vip2_avatar_frame'
WHERE g.app_code = 'lalu' AND g.group_code = 'vip2_rewards';

INSERT IGNORE INTO resource_group_items (
  app_code, group_id, item_type, resource_id, wallet_asset_type, wallet_asset_amount,
  quantity, duration_ms, sort_order, created_at_ms, updated_at_ms
)
SELECT 'lalu', g.group_id, 'resource', r.resource_id, '', 0, 1, 604800000, 10, 0, 0
FROM resource_groups g
JOIN resources r ON r.app_code = g.app_code AND r.resource_code = 'vip3_vehicle'
WHERE g.app_code = 'lalu' AND g.group_code = 'vip3_rewards';

INSERT IGNORE INTO vip_levels (
  app_code, level, name, status, price_coin, duration_ms, reward_resource_group_id,
  sort_order, created_at_ms, updated_at_ms
)
SELECT 'lalu', 1, 'VIP1', 'active', 1000, 604800000, group_id, 10, 0, 0
FROM resource_groups
WHERE app_code = 'lalu' AND group_code = 'vip1_rewards';

INSERT IGNORE INTO vip_levels (
  app_code, level, name, status, price_coin, duration_ms, reward_resource_group_id,
  sort_order, created_at_ms, updated_at_ms
)
SELECT 'lalu', 2, 'VIP2', 'active', 3000, 604800000, group_id, 20, 0, 0
FROM resource_groups
WHERE app_code = 'lalu' AND group_code = 'vip2_rewards';

INSERT IGNORE INTO vip_levels (
  app_code, level, name, status, price_coin, duration_ms, reward_resource_group_id,
  sort_order, created_at_ms, updated_at_ms
)
SELECT 'lalu', 3, 'VIP3', 'active', 8000, 604800000, group_id, 30, 0, 0
FROM resource_groups
WHERE app_code = 'lalu' AND group_code = 'vip3_rewards';

INSERT IGNORE INTO wallet_diamond_exchange_rules (
  app_code, exchange_type, status, from_asset_type, to_asset_type,
  from_amount, to_amount, sort_order, created_at_ms, updated_at_ms
) VALUES
  ('lalu', 'diamond_to_coin', 'active', 'DIAMOND', 'COIN', 100, 100, 10, 0, 0),
  ('lalu', 'diamond_to_usd_balance', 'active', 'DIAMOND', 'USD_BALANCE', 1000, 100, 20, 0, 0);

INSERT IGNORE INTO gift_type_configs (
  app_code, type_code, name, tab_key, status, sort_order,
  created_by_user_id, updated_by_user_id, created_at_ms, updated_at_ms
) VALUES
  ('lalu', 'normal', '普通礼物', 'Gift', 'active', 10, 0, 0, 0, 0),
  ('lalu', 'cp', 'CP礼物', 'CP', 'active', 20, 0, 0, 0, 0),
  ('lalu', 'lucky', '幸运礼物', 'Lucky', 'active', 30, 0, 0, 0, 0),
  ('lalu', 'super_lucky', '超级幸运礼物', 'Super Lucky', 'active', 40, 0, 0, 0, 0),
  ('lalu', 'exclusive', '专属礼物', 'Exclusive', 'active', 50, 0, 0, 0, 0),
  ('lalu', 'noble', '贵族礼物', 'Noble', 'active', 60, 0, 0, 0, 0),
  ('lalu', 'flag', '国旗礼物', 'Flag', 'active', 70, 0, 0, 0, 0),
  ('lalu', 'activity', '活动礼物', 'Activity', 'active', 80, 0, 0, 0, 0),
  ('lalu', 'magic', '魔法礼物', 'Magic', 'active', 90, 0, 0, 0, 0),
  ('lalu', 'custom', '定制礼物', 'Custom', 'active', 100, 0, 0, 0, 0);

INSERT IGNORE INTO gift_configs (
  app_code, gift_id, resource_id, status, name, sort_order, presentation_json,
  gift_type_code, effective_from_ms, effective_to_ms, effect_types_json,
  created_by_user_id, updated_by_user_id, created_at_ms, updated_at_ms
)
SELECT 'lalu', 'rose', resource_id, 'active', 'Rose', 10, JSON_OBJECT(),
       'normal', 0, 0, JSON_ARRAY(),
       0, 0, 0, 0
FROM resources
WHERE app_code = 'lalu' AND resource_code = 'gift_rose'
LIMIT 1;

INSERT IGNORE INTO gift_configs (
  app_code, gift_id, resource_id, status, name, sort_order, presentation_json,
  gift_type_code, effective_from_ms, effective_to_ms, effect_types_json,
  created_by_user_id, updated_by_user_id, created_at_ms, updated_at_ms
)
SELECT 'lalu', 'rocket', resource_id, 'active', 'Rocket', 20, JSON_OBJECT(),
       'normal', 0, 0, JSON_ARRAY('animation', 'global_broadcast'),
       0, 0, 0, 0
FROM resources
WHERE app_code = 'lalu' AND resource_code = 'gift_rocket'
LIMIT 1;

INSERT IGNORE INTO gift_config_regions (
  app_code, gift_id, region_id, created_at_ms, updated_at_ms
)
SELECT gc.app_code, gc.gift_id, 0, 0, 0
FROM gift_configs gc
LEFT JOIN gift_config_regions existing
  ON existing.app_code = gc.app_code AND existing.gift_id = gc.gift_id
WHERE existing.gift_id IS NULL
  AND gc.app_code = 'lalu'
  AND gc.gift_id IN ('rose', 'rocket');
