-- 为 tb_refund_request 新增订单号快照字段
-- order_no：创建退款申请时从 tb_order.order_no 复制，避免列表/详情查询联表
ALTER TABLE tb_refund_request
    ADD COLUMN IF NOT EXISTS order_no VARCHAR(30) NOT NULL DEFAULT '';

COMMENT ON COLUMN tb_refund_request.order_no IS '关联订单号快照';

-- 存量数据回填：通过 order_id 关联 tb_order 填充订单号快照
UPDATE tb_refund_request r
SET order_no = o.order_no
FROM tb_order o
WHERE r.order_id = o.id
  AND (r.order_no IS NULL OR r.order_no = '');
