-- =============================================================================
-- 退款流水 biz_refund_record
-- 用途：售后完结后原路退款（微信 V3）；幂等键 aftersale_id
-- 前置：biz_order_aftersale.sql、biz_pay_record.sql
-- =============================================================================

CREATE TABLE IF NOT EXISTS `biz_refund_record` (
  `refund_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '退款记录ID',
  `aftersale_id` bigint(20) NOT NULL COMMENT '售后ID（biz_order_aftersale.aftersale_id）',
  `aftersale_no` varchar(32) NOT NULL COMMENT '售后编号',
  `order_id` bigint(20) NOT NULL COMMENT '订单ID',
  `order_no` varchar(32) NOT NULL COMMENT '订单编号',
  `pay_id` bigint(20) NOT NULL COMMENT '支付流水ID（biz_pay_record.pay_id）',
  `member_id` bigint(20) NOT NULL COMMENT '会员ID',
  `shop_id` bigint(20) NOT NULL COMMENT '店铺ID',
  `out_refund_no` varchar(64) NOT NULL COMMENT '商户退款单号（RF+售后编号）',
  `wx_refund_id` varchar(64) DEFAULT NULL COMMENT '微信退款单号',
  `refund_amount` decimal(12,2) NOT NULL COMMENT '退款金额（元）',
  `refund_status` char(1) NOT NULL DEFAULT '0' COMMENT '0待退款 1成功 2失败 3处理中',
  `fail_reason` varchar(256) DEFAULT NULL COMMENT '失败原因',
  `notify_time` datetime DEFAULT NULL COMMENT '退款完成时间',
  `create_time` datetime NOT NULL COMMENT '创建时间',
  `update_time` datetime DEFAULT NULL COMMENT '更新时间',
  PRIMARY KEY (`refund_id`),
  UNIQUE KEY `uk_aftersale_id` (`aftersale_id`),
  UNIQUE KEY `uk_out_refund_no` (`out_refund_no`),
  KEY `idx_order_id` (`order_id`),
  KEY `idx_refund_status` (`refund_status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='售后退款流水';
