-- 创建支付记录表 tb_payment
-- 统一承接所有支付凭证（微信/支付宝/钱包）
CREATE TABLE tb_payment (
    id                   BIGSERIAL PRIMARY KEY,
    payment_no           VARCHAR(40)  NOT NULL UNIQUE,    -- 保持 CRCH/PAY 前缀兼容
    order_id             BIGINT       NOT NULL,
    order_type           VARCHAR(30)  NOT NULL,           -- 'package_order' | 'recharge_order'
    payment_method       VARCHAR(20)  NOT NULL,           -- wallet | wechat | alipay | offline
    amount               BIGINT       NOT NULL,           -- 分
    status               SMALLINT     NOT NULL DEFAULT 1, -- 1待支付 2已支付 3已失败 4已退款
    third_party_trade_no VARCHAR(100),
    payment_config_id    BIGINT,
    payment_voucher_key  VARCHAR(500),
    paid_at              TIMESTAMPTZ,
    created_at           TIMESTAMPTZ  NOT NULL DEFAULT NOW(),
    updated_at           TIMESTAMPTZ  NOT NULL DEFAULT NOW(),
    deleted_at           TIMESTAMPTZ
);

-- 索引
CREATE INDEX idx_payment_order    ON tb_payment(order_id, order_type);
CREATE INDEX idx_payment_no       ON tb_payment(payment_no) WHERE deleted_at IS NULL;
CREATE INDEX idx_payment_status   ON tb_payment(status) WHERE deleted_at IS NULL;
