SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;

-- 提现前余额必须由来源钱包在申请时固化。NULL 明确表示历史申请没有可信快照，
-- 后台不能用审核时的当前余额回填，否则充值、扣款或退款会让财务看到错误事实。
SET @balance_before_exists = (
  SELECT COUNT(*)
  FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = 'admin_user_withdrawal_applications'
    AND COLUMN_NAME = 'balance_before'
);
SET @balance_before_ddl = IF(
  @balance_before_exists = 0,
  'ALTER TABLE admin_user_withdrawal_applications ADD COLUMN balance_before DECIMAL(18,2) NULL COMMENT ''来源钱包申请提现前可用余额快照'' AFTER withdraw_amount_minor, ALGORITHM=INSTANT',
  'SELECT 1'
);
PREPARE stmt FROM @balance_before_ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
