-- 创建退款记录表
-- 执行时间: 2024-01-01
-- 描述: 为微信退款功能创建退款记录表，记录所有退款申请和处理状态

CREATE TABLE IF NOT EXISTS `ai_refunds` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '退款记录ID',
  `refund_no` varchar(64) NOT NULL COMMENT '退款单号，系统生成的唯一退款编号',
  `order_id` bigint unsigned NOT NULL COMMENT '关联订单ID',
  `order_no` varchar(32) NOT NULL COMMENT '订单号',
  `user_id` bigint unsigned NOT NULL COMMENT '用户ID',
  `refund_type` tinyint NOT NULL DEFAULT 1 COMMENT '退款类型：1=仅退款，2=退货退款',
  `refund_reason` varchar(255) NOT NULL COMMENT '退款原因',
  `refund_description` text COMMENT '退款详细说明',
  `refund_amount` decimal(10,2) NOT NULL COMMENT '退款金额（分）',
  `refund_fee` decimal(10,2) DEFAULT 0.00 COMMENT '退款手续费（分）',
  `actual_refund_amount` decimal(10,2) NOT NULL COMMENT '实际退款金额（分）',
  `status` tinyint NOT NULL DEFAULT 1 COMMENT '退款状态：1=待审核，2=审核通过，3=审核拒绝，4=退款中，5=退款成功，6=退款失败',
  `apply_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '申请时间',
  `audit_time` timestamp NULL COMMENT '审核时间',
  `refund_time` timestamp NULL COMMENT '退款完成时间',
  `admin_id` bigint unsigned NULL COMMENT '审核管理员ID',
  `admin_remark` text COMMENT '管理员备注',
  `reject_reason` varchar(255) NULL COMMENT '拒绝原因',
  
  -- 微信退款相关字段
  `wechat_refund_id` varchar(64) NULL COMMENT '微信退款单号',
  `wechat_out_refund_no` varchar(64) NULL COMMENT '商户退款单号',
  `wechat_transaction_id` varchar(64) NULL COMMENT '微信支付交易号',
  `wechat_refund_status` varchar(32) NULL COMMENT '微信退款状态：SUCCESS=成功，CLOSED=关闭，PROCESSING=处理中',
  `wechat_refund_recv_accout` varchar(64) NULL COMMENT '退款入账账户',
  `wechat_success_time` timestamp NULL COMMENT '微信退款成功时间',
  `wechat_user_received_account` varchar(64) NULL COMMENT '退款到账账户',
  `wechat_refund_account` varchar(32) NULL COMMENT '退款资金来源：AVAILABLE=可用余额，UNAVAILABLE=不可用余额',
  
  -- 退货相关字段（当refund_type=2时使用）
  `return_logistics_company` varchar(50) NULL COMMENT '退货物流公司',
  `return_logistics_no` varchar(100) NULL COMMENT '退货物流单号',
  `return_address` varchar(255) NULL COMMENT '退货地址',
  `goods_received_time` timestamp NULL COMMENT '商家收货时间',
  
  -- 图片证据
  `evidence_images` json NULL COMMENT '退款凭证图片JSON数组',
  
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_refund_no` (`refund_no`),
  UNIQUE KEY `uk_wechat_out_refund_no` (`wechat_out_refund_no`),
  KEY `idx_order_id` (`order_id`),
  KEY `idx_order_no` (`order_no`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_status` (`status`),
  KEY `idx_apply_time` (`apply_time`),
  KEY `idx_wechat_refund_id` (`wechat_refund_id`),
  KEY `idx_wechat_transaction_id` (`wechat_transaction_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='退款记录表';

-- 创建退款项目表（支持部分退款）
CREATE TABLE IF NOT EXISTS `ai_refund_items` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '退款项目ID',
  `refund_id` bigint unsigned NOT NULL COMMENT '退款记录ID',
  `order_item_id` bigint unsigned NOT NULL COMMENT '订单项ID',
  `product_id` bigint unsigned NOT NULL COMMENT '商品ID',
  `sku_id` bigint unsigned NULL COMMENT 'SKU ID',
  `product_name` varchar(100) NOT NULL COMMENT '商品名称',
  `product_image` varchar(255) NULL COMMENT '商品图片',
  `spec_info` json NULL COMMENT '规格信息',
  `quantity` int NOT NULL COMMENT '退款数量',
  `unit_price` decimal(10,2) NOT NULL COMMENT '单价（分）',
  `total_price` decimal(10,2) NOT NULL COMMENT '退款总价（分）',
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  
  PRIMARY KEY (`id`),
  KEY `idx_refund_id` (`refund_id`),
  KEY `idx_order_item_id` (`order_item_id`),
  KEY `idx_product_id` (`product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='退款项目表';

-- 创建退款日志表
CREATE TABLE IF NOT EXISTS `ai_refund_logs` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '日志ID',
  `refund_id` bigint unsigned NOT NULL COMMENT '退款记录ID',
  `action` varchar(50) NOT NULL COMMENT '操作类型：apply=申请，audit=审核，refund=退款，callback=回调',
  `status_from` tinyint NULL COMMENT '状态变更前',
  `status_to` tinyint NULL COMMENT '状态变更后',
  `operator_type` varchar(20) NOT NULL COMMENT '操作者类型：user=用户，admin=管理员，system=系统',
  `operator_id` bigint unsigned NULL COMMENT '操作者ID',
  `remark` text NULL COMMENT '操作备注',
  `extra_data` json NULL COMMENT '额外数据',
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  
  PRIMARY KEY (`id`),
  KEY `idx_refund_id` (`refund_id`),
  KEY `idx_action` (`action`),
  KEY `idx_created_at` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='退款操作日志表';