-- 分销佣金提现模块升级脚本
-- 说明：
-- 1. 在分销佣金账户中增加累计提现金额。
-- 2. 在分销佣金账户流水中增加冻结余额与提现单关联字段。
-- 3. 新增会员提现收款方式表与提现申请表。

ALTER TABLE `xxk_distribution_account`
    ADD COLUMN `total_withdraw_amount` DECIMAL(18,2) NOT NULL DEFAULT 0.00 COMMENT '累计提现金额' AFTER `total_transfer_amount`;

ALTER TABLE `xxk_distribution_account_flow`
    ADD COLUMN `before_frozen_balance` DECIMAL(18,2) NOT NULL DEFAULT 0.00 COMMENT '变动前冻结余额' AFTER `after_balance`,
    ADD COLUMN `after_frozen_balance` DECIMAL(18,2) NOT NULL DEFAULT 0.00 COMMENT '变动后冻结余额' AFTER `before_frozen_balance`,
    ADD COLUMN `related_withdraw_no` VARCHAR(64) DEFAULT NULL COMMENT '关联提现单号' AFTER `related_wallet_flow_no`,
    ADD KEY `idx_xxk_distribution_account_flow_related_withdraw_no` (`related_withdraw_no`);

CREATE TABLE `xxk_distribution_withdraw_method` (
  `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `member_user_id` BIGINT NOT NULL COMMENT '会员ID',
  `method_type` VARCHAR(32) NOT NULL COMMENT '收款方式(ALIPAY_ACCOUNT/ALIPAY_QR/WECHAT_QR)',
  `account_name` VARCHAR(64) NOT NULL COMMENT '收款人姓名',
  `account_no` VARCHAR(128) DEFAULT NULL COMMENT '收款账号',
  `qr_code_url` VARCHAR(255) DEFAULT NULL COMMENT '收款码地址',
  `is_default` TINYINT NOT NULL DEFAULT 0 COMMENT '是否默认(1-是 0-否)',
  `status` TINYINT NOT NULL DEFAULT 1 COMMENT '状态(1-正常 0-禁用)',
  `remark` VARCHAR(255) DEFAULT NULL COMMENT '备注',
  `create_by` BIGINT DEFAULT NULL COMMENT '创建人',
  `update_by` BIGINT DEFAULT NULL COMMENT '更新人',
  `create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  KEY `idx_xxk_distribution_withdraw_method_member_user_id` (`member_user_id`),
  KEY `idx_xxk_distribution_withdraw_method_default` (`member_user_id`, `is_default`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='分销提现收款方式表';

CREATE TABLE `xxk_distribution_withdraw_apply` (
  `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `withdraw_no` VARCHAR(64) NOT NULL COMMENT '提现单号',
  `member_user_id` BIGINT NOT NULL COMMENT '会员ID',
  `username_snapshot` VARCHAR(64) DEFAULT NULL COMMENT '会员账号快照',
  `nickname_snapshot` VARCHAR(64) DEFAULT NULL COMMENT '会员昵称快照',
  `mobile_snapshot` VARCHAR(32) DEFAULT NULL COMMENT '会员手机号快照',
  `withdraw_method_id` BIGINT DEFAULT NULL COMMENT '提现方式ID',
  `method_type` VARCHAR(32) NOT NULL COMMENT '收款方式类型',
  `account_name_snapshot` VARCHAR(64) NOT NULL COMMENT '收款人快照',
  `account_no_snapshot` VARCHAR(128) DEFAULT NULL COMMENT '收款账号快照',
  `qr_code_url_snapshot` VARCHAR(255) DEFAULT NULL COMMENT '收款码快照',
  `amount` DECIMAL(18,2) NOT NULL DEFAULT 0.00 COMMENT '申请提现金额',
  `paid_amount` DECIMAL(18,2) DEFAULT NULL COMMENT '实际打款金额',
  `status` VARCHAR(32) NOT NULL COMMENT '状态(PENDING/APPROVED/REJECTED/PAID)',
  `submit_remark` VARCHAR(255) DEFAULT NULL COMMENT '申请备注',
  `submit_time` DATETIME DEFAULT NULL COMMENT '申请时间',
  `audit_remark` VARCHAR(255) DEFAULT NULL COMMENT '审核备注',
  `audit_by` BIGINT DEFAULT NULL COMMENT '审核人',
  `audit_time` DATETIME DEFAULT NULL COMMENT '审核时间',
  `pay_remark` VARCHAR(255) DEFAULT NULL COMMENT '打款备注',
  `pay_proof_urls_json` TEXT DEFAULT NULL COMMENT '打款凭证JSON',
  `pay_by` BIGINT DEFAULT NULL COMMENT '打款确认人',
  `pay_time` DATETIME DEFAULT NULL COMMENT '打款时间',
  `create_by` BIGINT DEFAULT NULL COMMENT '创建人',
  `update_by` BIGINT DEFAULT NULL COMMENT '更新人',
  `create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_xxk_distribution_withdraw_apply_no` (`withdraw_no`),
  KEY `idx_xxk_distribution_withdraw_apply_member_user_id` (`member_user_id`),
  KEY `idx_xxk_distribution_withdraw_apply_status` (`status`),
  KEY `idx_xxk_distribution_withdraw_apply_submit_time` (`submit_time`),
  KEY `idx_xxk_distribution_withdraw_apply_pay_time` (`pay_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='分销提现申请表';
