-- ============================================
-- 退款主表
-- ============================================
CREATE TABLE IF NOT EXISTS `t_refund` (
  `id` BIGINT NOT NULL COMMENT '退款记录ID',
  `order_id` BIGINT NOT NULL COMMENT '订单ID',
  `order_no` VARCHAR(64) NOT NULL COMMENT '订单号',
  `refund_no` VARCHAR(64) NOT NULL COMMENT '退款单号（系统生成）',
  `refund_amount` DECIMAL(10,2) NOT NULL DEFAULT 0.00 COMMENT '退款金额',
  `total_amount` DECIMAL(10,2) NOT NULL DEFAULT 0.00 COMMENT '订单原始总金额',
  `status` VARCHAR(20) NOT NULL DEFAULT 'PENDING' COMMENT '退款状态：PENDING-待审核, REFUNDED-已退款, REJECTED-已拒绝',
  `reason` VARCHAR(500) DEFAULT NULL COMMENT '退款原因',
  `remark` VARCHAR(500) DEFAULT NULL COMMENT '审核备注',
  `transaction_id` VARCHAR(100) DEFAULT NULL COMMENT '第三方退款交易号',
  `refund_time` DATETIME DEFAULT NULL COMMENT '退款完成时间',
  `reviewer_id` BIGINT DEFAULT NULL COMMENT '审核人ID',
  `review_time` DATETIME DEFAULT NULL COMMENT '审核时间',
  `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_refund_no` (`refund_no`),
  KEY `idx_order_id` (`order_id`),
  KEY `idx_order_no` (`order_no`),
  KEY `idx_status` (`status`),
  KEY `idx_create_time` (`create_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='退款记录表';

-- ============================================
-- 退款商品明细表
-- ============================================
CREATE TABLE IF NOT EXISTS `t_refund_item` (
  `id` BIGINT NOT NULL COMMENT '退款商品明细ID',
  `refund_id` BIGINT NOT NULL COMMENT '退款记录ID',
  `order_goods_id` BIGINT NOT NULL COMMENT '订单商品明细ID（关联 t_order_goods.id）',
  `product_id` BIGINT DEFAULT NULL COMMENT '商品ID',
  `product_name` VARCHAR(255) DEFAULT NULL COMMENT '商品名称',
  `pic` VARCHAR(500) DEFAULT NULL COMMENT '商品图片',
  `quantity` INT NOT NULL DEFAULT 1 COMMENT '退款数量',
  `price` DECIMAL(10,2) NOT NULL DEFAULT 0.00 COMMENT '商品单价',
  `refund_amount` DECIMAL(10,2) NOT NULL DEFAULT 0.00 COMMENT '退款金额（quantity * price）',
  `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`id`),
  KEY `idx_refund_id` (`refund_id`),
  KEY `idx_order_goods_id` (`order_goods_id`),
  KEY `idx_product_id` (`product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='退款商品明细表';

-- ============================================
-- t_order 新增字段：关联当前退款记录
-- ============================================
ALTER TABLE `t_order`
ADD COLUMN `current_refund_id` BIGINT DEFAULT NULL COMMENT '当前退款记录ID（关联 t_refund.id）' AFTER `transaction_id`;

-- ============================================
-- t_fund_log 新增字段：关联退款记录
-- ============================================
ALTER TABLE `t_fund_log`
ADD COLUMN `refund_id` BIGINT DEFAULT NULL COMMENT '退款记录ID（关联 t_refund.id）' AFTER `order_no`;

-- ============================================
-- 字典数据：支付状态新增"部分退款"，"支付失败"改为"全额退款"
-- 注意：字典使用数字编码（与代码字符串编码不一致，需后续统一）
-- ============================================
UPDATE `t_dict_data` SET `item_label` = '部分退款', `list_class` = 'warning'
WHERE `dict_code` = 'pay_status' AND `item_value` = '2';

INSERT IGNORE INTO `t_dict_data` (`dict_code`, `item_value`, `item_label`, `list_class`, `is_default`, `status`, `sort_order`, `create_by`)
VALUES
('pay_status', '3', '全额退款', 'danger', 0, 1, 4, 'system');
