ALTER TABLE supplier_media_resources
    ADD COLUMN IF NOT EXISTS resource_url TEXT,
    ADD COLUMN IF NOT EXISTS baidu_weight INT,
    ADD COLUMN IF NOT EXISTS resource_remark TEXT;

UPDATE supplier_media_resources
SET resource_url = NULLIF(BTRIM(COALESCE(raw_json->>'meitianli', raw_json->>'resource_url', raw_json->>'url', raw_json->>'site_url', raw_json->>'link')), ''),
    baidu_weight = CASE
        WHEN BTRIM(COALESCE(raw_json->>'meitiquanzhong', raw_json->>'baidu_weight', raw_json->>'weight', raw_json->>'quanzhong')) ~ '^[0-9]+$'
            THEN BTRIM(COALESCE(raw_json->>'meitiquanzhong', raw_json->>'baidu_weight', raw_json->>'weight', raw_json->>'quanzhong'))::INT
        ELSE NULL
    END,
    resource_remark = NULLIF(BTRIM(COALESCE(raw_json->>'meitibeizhu', raw_json->>'remark', raw_json->>'beizhu', raw_json->>'note', raw_json->>'description')), '')
WHERE resource_url IS NULL OR baidu_weight IS NULL OR resource_remark IS NULL;

CREATE INDEX IF NOT EXISTS idx_supplier_media_resources_remark
    ON supplier_media_resources (supplier, resource_remark);
