-- 051_worker_hall_queue.sql —— 抢单大厅展示队列与容量查询索引。

ALTER TABLE work_orders ADD COLUMN IF NOT EXISTS hall_queued_at TIMESTAMPTZ NULL;

UPDATE work_orders
SET hall_queued_at = COALESCE(published_at, updated_at, created_at)
WHERE status = 'open'
  AND hall_queued_at IS NULL;

CREATE INDEX IF NOT EXISTS idx_work_orders_hall_queue
  ON work_orders(hall_queued_at ASC, id ASC)
  WHERE status = 'open';

COMMENT ON COLUMN work_orders.hall_queued_at IS '进入抢单大厅候选队列的时间；用于按先进先出限制大厅展示数量';
