CREATE TABLE `order` (
    `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `order_no` varchar(255) NOT NULL COMMENT '商户订单号',
    `user_id` bigint NOT NULL COMMENT '用户ID',
    `product_id` bigint NOT NULL COMMENT '产品ID（软关联到产品表）',
    `payment_platform` enum(
        'alipay',
        'wechat',
        'appleiap'
    ) NOT NULL COMMENT '支付平台（支付宝、微信、苹果内购）',
    `payment_scene` enum(
        'app',
        'h5',
        'mini_program',
        'public_account'
    ) NOT NULL COMMENT '支付场景（App、H5、微信小程序、公众号）',
    `platform_order_id` varchar(255) DEFAULT NULL COMMENT '支付平台订单号',
    `amount` decimal(10, 2) NOT NULL COMMENT '支付金额',
    `status` enum(
        'pending',
        'paid',
        'failed',
        'refunding',
        'refunded',
        'closed'
    ) NOT NULL DEFAULT 'pending' COMMENT '支付状态',
    `del_state` tinyint NOT NULL DEFAULT '0' COMMENT '删除状态',
    `version` bigint NOT NULL DEFAULT '0' COMMENT '版本号',
    `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    `pay_time` datetime DEFAULT NULL COMMENT '支付时间',
    `refund_time` datetime DEFAULT NULL COMMENT '退款时间',
    `close_time` datetime DEFAULT NULL COMMENT '订单关闭时间',
    `delete_time` datetime DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `unique_order_no` (`order_no`),
    UNIQUE KEY `unique_platform_order_id` (`platform_order_id`),
    KEY `idx_user_id` (`user_id`),
    KEY `idx_product_id` (`product_id`),
    KEY `idx_payment_platform` (`payment_platform`),
    KEY `idx_payment_scene` (`payment_scene`),
    KEY `idx_status` (`status`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '订单表';

CREATE TABLE `order_refund` (
    `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `refund_no` varchar(255) NOT NULL COMMENT '退款单号',
    `order_id` bigint NOT NULL COMMENT '关联的订单ID',
    `user_id` bigint NOT NULL COMMENT '用户ID',
    `product_id` bigint NOT NULL COMMENT '产品ID',
    `platform_refund_id` varchar(255) DEFAULT NULL COMMENT '支付平台退款单号',
    `refund_amount` decimal(10, 2) NOT NULL COMMENT '退款金额',
    `refund_reason` varchar(255) DEFAULT NULL COMMENT '退款原因',
    `status` enum(
        'pending',
        'success',
        'failed',
        'closed'
    ) NOT NULL DEFAULT 'pending' COMMENT '退款状态',
    `del_state` tinyint NOT NULL DEFAULT '0' COMMENT '删除状态',
    `version` bigint NOT NULL DEFAULT '0' COMMENT '版本号',
    `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    `refund_time` datetime DEFAULT NULL COMMENT '退款成功时间',
    `close_time` datetime DEFAULT NULL COMMENT '退款关闭时间',
    `delete_time` datetime DEFAULT NULL COMMENT '删除时间',
    PRIMARY KEY (`id`),
    UNIQUE KEY `unique_refund_no` (`refund_no`),
    UNIQUE KEY `unique_platform_refund_id` (`platform_refund_id`),
    UNIQUE KEY `unique_order_id` (`order_id`),
    KEY `idx_user_id` (`user_id`),
    KEY `idx_product_id` (`product_id`),
    KEY `idx_status` (`status`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '退款记录表';