-- 003_kuaishou_industry_voucher_eticket_type.sql
-- 电子凭证类型落库，避免后台默认 DINING_OPEN_TICKET 覆盖真实类目类型。

ALTER TABLE kuaishou_industry_vouchers
  ADD COLUMN IF NOT EXISTS eticket_type TEXT NOT NULL DEFAULT '';

COMMENT ON COLUMN kuaishou_industry_vouchers.eticket_type IS
  '快手电子凭证类型，发码请求 eticketType（如 GAME_OPEN_TICKET_CONSUME）';

-- 从历史 raw_payload 回填
UPDATE kuaishou_industry_vouchers
SET eticket_type = COALESCE(
  NULLIF(BTRIM(raw_payload_json #>> '{body,eticketType}'), ''),
  NULLIF(BTRIM(raw_payload_json ->> 'eticketType'), ''),
  eticket_type
)
WHERE BTRIM(COALESCE(eticket_type, '')) = '';
