-- ============================================
-- 提现功能数据库迁移脚本
-- 创建时间: 2026-06-06
-- ============================================

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- -------------------------------------------
-- 用户收款账号表
-- -------------------------------------------

CREATE TABLE IF NOT EXISTS user_payment_accounts (
  id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
  user_id BIGINT UNSIGNED NOT NULL COMMENT '用户ID',
  account_type VARCHAR(32) NOT NULL COMMENT '账号类型: alipay支付宝, wechat微信, bank银行卡',
  account_name VARCHAR(128) NOT NULL COMMENT '账户名（真实姓名）',
  account_no VARCHAR(255) NOT NULL COMMENT '账号（加密存储）',

  -- 银行卡专用字段
  bank_name VARCHAR(128) NOT NULL DEFAULT '' COMMENT '银行名称',
  bank_branch VARCHAR(255) NOT NULL DEFAULT '' COMMENT '开户行支行',

  -- 凭证
  certificate_urls JSON NULL COMMENT '认证凭证（收款码截图等）',

  is_default TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否默认账号',
  status VARCHAR(32) NOT NULL DEFAULT 'active' COMMENT '状态: active正常, disabled禁用',

  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

  KEY idx_user_payment_accounts_user_id (user_id),
  KEY idx_user_payment_accounts_user_default (user_id, is_default)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户收款账号表';

-- -------------------------------------------
-- 提现申请表
-- -------------------------------------------

CREATE TABLE IF NOT EXISTS withdrawal_requests (
  id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
  withdraw_no VARCHAR(64) NOT NULL COMMENT '提现单号',
  user_id BIGINT UNSIGNED NOT NULL COMMENT '用户ID',
  amount DECIMAL(12,2) NOT NULL COMMENT '提现金额',
  fee DECIMAL(12,2) NOT NULL DEFAULT 0.00 COMMENT '手续费',
  actual_amount DECIMAL(12,2) NOT NULL COMMENT '实际到账金额',

  -- 收款账号信息（快照）
  payment_account_id BIGINT UNSIGNED NULL COMMENT '收款账号ID',
  account_type VARCHAR(32) NOT NULL COMMENT '账号类型',
  account_name VARCHAR(128) NOT NULL COMMENT '账户名',
  account_no VARCHAR(128) NOT NULL COMMENT '账号（脱敏显示）',
  bank_name VARCHAR(128) NOT NULL DEFAULT '' COMMENT '银行名称',
  bank_branch VARCHAR(255) NOT NULL DEFAULT '' COMMENT '开户行支行',

  status VARCHAR(32) NOT NULL DEFAULT 'pending' COMMENT '状态: pending待审核, processing处理中, completed已完成, rejected已拒绝, cancelled已取消',

  -- 审核信息
  reviewed_by BIGINT UNSIGNED NULL COMMENT '审核人ID（财务管理员）',
  reviewed_at DATETIME NULL COMMENT '审核时间',
  review_remark VARCHAR(255) NOT NULL DEFAULT '' COMMENT '审核备注',

  -- 打款信息
  paid_by BIGINT UNSIGNED NULL COMMENT '打款人ID（财务管理员）',
  paid_at DATETIME NULL COMMENT '打款时间',
  payment_proof_url VARCHAR(512) NOT NULL DEFAULT '' COMMENT '打款凭证URL',
  payment_remark VARCHAR(255) NOT NULL DEFAULT '' COMMENT '打款备注',

  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

  UNIQUE KEY uk_withdrawal_requests_withdraw_no (withdraw_no),
  KEY idx_withdrawal_requests_user_id (user_id),
  KEY idx_withdrawal_requests_status (status),
  KEY idx_withdrawal_requests_created (created_at DESC)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='提现申请表';

-- -------------------------------------------
-- 权限数据
-- -------------------------------------------

-- 添加提现相关权限（如果不存在）
INSERT IGNORE INTO permissions (code, name, resource, action) VALUES
('withdrawal:list', '查看提现申请', 'withdrawal', 'list'),
('withdrawal:review', '审核提现申请', 'withdrawal', 'review'),
('withdrawal:pay', '确认打款', 'withdrawal', 'pay'),
('withdrawal:detail', '查看提现详情', 'withdrawal', 'detail');

-- 钱包管理员流水权限（如果不存在）
INSERT IGNORE INTO permissions (code, name, resource, action) VALUES
('wallet:admin_ledger', '查看资金流水', 'wallet', 'admin_ledger');

-- 创建财务角色（如果不存在）
INSERT IGNORE INTO roles (code, name, description) VALUES
('finance', '财务管理员', '负责提现审核、打款确认、资金流水查询');

-- 关联权限到财务角色（如果不存在）
INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT
  (SELECT id FROM roles WHERE code = 'finance'),
  id
FROM permissions
WHERE code IN ('withdrawal:list', 'withdrawal:review', 'withdrawal:pay', 'withdrawal:detail', 'wallet:admin_ledger');

SET FOREIGN_KEY_CHECKS = 1;
