SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;
SET time_zone = '+00:00';

USE hyapp_wallet;

-- 运行政策是小型不可变快照；在线补列不扫描 wallet_transactions 或提现申请流水。
SET @ddl := IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS
   WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'host_agency_salary_policies'
     AND COLUMN_NAME = 'coin_seller_withdrawal_limit_period') = 0,
  'ALTER TABLE host_agency_salary_policies ADD COLUMN coin_seller_withdrawal_limit_period VARCHAR(16) NOT NULL DEFAULT ''month'' COMMENT ''用户找币商提现限制周期：day/week/month'' AFTER residual_diamond_to_usd_rate, ALGORITHM=INPLACE, LOCK=NONE',
  '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 = 'host_agency_salary_policies'
     AND COLUMN_NAME = 'coin_seller_withdrawal_limit_count') = 0,
  'ALTER TABLE host_agency_salary_policies ADD COLUMN coin_seller_withdrawal_limit_count BIGINT NOT NULL DEFAULT 0 COMMENT ''用户找币商提现次数，0 表示不限'' AFTER coin_seller_withdrawal_limit_period, ALGORITHM=INPLACE, LOCK=NONE',
  '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 = 'host_agency_salary_policies'
     AND COLUMN_NAME = 'platform_withdrawal_limit_period') = 0,
  'ALTER TABLE host_agency_salary_policies ADD COLUMN platform_withdrawal_limit_period VARCHAR(16) NOT NULL DEFAULT ''month'' COMMENT ''用户找平台提现限制周期：day/week/month'' AFTER coin_seller_withdrawal_limit_count, ALGORITHM=INPLACE, LOCK=NONE',
  '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 = 'host_agency_salary_policies'
     AND COLUMN_NAME = 'platform_withdrawal_limit_count') = 0,
  'ALTER TABLE host_agency_salary_policies ADD COLUMN platform_withdrawal_limit_count BIGINT NOT NULL DEFAULT 0 COMMENT ''用户找平台提现次数，0 表示不限'' AFTER platform_withdrawal_limit_period, ALGORITHM=INPLACE, LOCK=NONE',
  'SELECT 1'
);
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- 运行时只按完整主键点查并锁定一个用户当前周期的计数行，不扫描钱包或提现流水。
CREATE TABLE IF NOT EXISTS point_withdrawal_policy_counters (
  app_code VARCHAR(32) NOT NULL COMMENT '应用编码',
  user_id BIGINT NOT NULL COMMENT '提现用户 ID',
  channel VARCHAR(24) NOT NULL COMMENT '提现渠道：coin_seller/platform',
  period VARCHAR(16) NOT NULL COMMENT '限制周期类型：day/week/month',
  period_key VARCHAR(10) NOT NULL COMMENT 'UTC 周期键：yyyy-MM-dd/yyyy-MM',
  withdrawal_count BIGINT UNSIGNED 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, channel, period, period_key)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户自然周期提现次数计数表';

-- 平台提现先冻结后创建申请；reservation 让“创建失败释放”可幂等撤销次数，人工驳回不修改本表状态。
CREATE TABLE IF NOT EXISTS point_withdrawal_limit_reservations (
  app_code VARCHAR(32) NOT NULL COMMENT '应用编码',
  withdrawal_ref VARCHAR(128) NOT NULL COMMENT 'gateway 生成的申请前置引用',
  freeze_command_id VARCHAR(128) NOT NULL COMMENT '首次冻结命令幂等 ID',
  user_id BIGINT NOT NULL COMMENT '提现用户 ID',
  channel VARCHAR(24) NOT NULL COMMENT '提现渠道，当前为 platform',
  policy_id BIGINT UNSIGNED NOT NULL COMMENT '命中的工资政策 ID',
  policy_version BIGINT UNSIGNED NOT NULL COMMENT '命中的不可变政策版本',
  period VARCHAR(16) NOT NULL COMMENT '命中的限制周期类型：day/week/month',
  period_key VARCHAR(10) NOT NULL COMMENT '命中的 UTC 限制周期键',
  counted BOOLEAN NOT NULL DEFAULT FALSE COMMENT '是否实际占用了非零次数限制',
  status VARCHAR(24) NOT NULL DEFAULT 'active' COMMENT '状态：active/released',
  created_at_ms BIGINT NOT NULL COMMENT '创建时间，UTC epoch ms',
  released_at_ms BIGINT NOT NULL DEFAULT 0 COMMENT '创建失败回滚时间，UTC epoch ms',
  PRIMARY KEY (app_code, withdrawal_ref),
  UNIQUE KEY uk_point_withdrawal_limit_reservation_command (app_code, freeze_command_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='平台积分提现次数预留表';
