BEGIN;

ALTER TABLE public.ml_orders
    ADD COLUMN IF NOT EXISTS cancelled_at TIMESTAMPTZ NULL,
    ADD COLUMN IF NOT EXISTS consumer_deleted_at TIMESTAMPTZ NULL;

COMMENT ON COLUMN public.ml_orders.cancelled_at IS '订单取消时间，包含用户取消与支付超时取消';
COMMENT ON COLUMN public.ml_orders.consumer_deleted_at IS '消费者侧软删除时间，仅影响消费者订单列表展示';

CREATE INDEX IF NOT EXISTS idx_ml_orders_consumer_deleted_at
ON public.ml_orders(user_id, consumer_deleted_at, created_at DESC);

CREATE INDEX IF NOT EXISTS idx_ml_orders_consumer_visible
ON public.ml_orders(user_id, created_at DESC)
WHERE consumer_deleted_at IS NULL;

COMMIT;