-- 订单买家快照字段：buyer_phone、buyer_nickname（下单时快照买家手机号和昵称）
-- 套餐类型快照字段：package_type（下单时快照套餐类型，避免套餐删除后信息丢失）

ALTER TABLE tb_order
    ADD COLUMN IF NOT EXISTS buyer_phone    VARCHAR(20)  NULL,
    ADD COLUMN IF NOT EXISTS buyer_nickname VARCHAR(100) NULL;

COMMENT ON COLUMN tb_order.buyer_phone    IS '下单时买家主手机号快照（个人客户有值，代理商留空）';
COMMENT ON COLUMN tb_order.buyer_nickname IS '下单时买家昵称快照（个人客户有值，代理商留空）';

CREATE INDEX IF NOT EXISTS idx_order_buyer_phone ON tb_order (buyer_phone) WHERE buyer_phone IS NOT NULL;

ALTER TABLE tb_order_item
    ADD COLUMN IF NOT EXISTS package_type VARCHAR(20) NULL;

COMMENT ON COLUMN tb_order_item.package_type IS '套餐类型快照 (formal:正式套餐, addon:加油包)';
