topfans/docker/sql/migrations/V8__exhibition_for_mock_assets.sql
2026-05-17 23:33:45 +08:00

46 lines
1.8 KiB
PL/PgSQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- V8: 将mock资产插入展示表exhibitions (2026-05-17)
-- 为所有mock资产(asset_id: 10001-40037)创建展示记录
-- 结束时间 = 开始时间 + 1个月
BEGIN;
-- 获取当前时间戳(毫秒)作为开始时间
DO $$
DECLARE
start_ts bigint := (EXTRACT(EPOCH FROM NOW()) * 1000)::bigint;
expire_ts bigint := (EXTRACT(EPOCH FROM NOW()) * 1000)::bigint + (30::bigint * 24 * 60 * 60 * 1000); -- 加1个月
slot_ids bigint[];
slot_rec record;
asset_rec record;
i int := 0;
BEGIN
-- 收集star_id=87的所有可用slot_id排除已被用户1占用的
SELECT array_agg(slot_id ORDER BY slot_id) INTO slot_ids
FROM booth_slots
WHERE star_id = 87
AND slot_id NOT IN (SELECT slot_id FROM exhibitions WHERE occupier_uid = 1 AND deleted_at IS NULL);
-- 为每个mock资产创建展示记录
FOR asset_rec IN SELECT id, owner_uid FROM assets WHERE id >= 10000 ORDER BY id LOOP
-- 循环分配slot_id
IF array_length(slot_ids, 1) > 0 THEN
i := (i % array_length(slot_ids, 1)) + 1;
-- 获取对应slot的host_profile_id
SELECT host_profile_id INTO slot_rec FROM booth_slots WHERE slot_id = slot_ids[i];
INSERT INTO exhibitions (asset_id, slot_id, host_profile_id, occupier_uid, occupier_star_id, start_time, expire_at, created_at, updated_at)
VALUES (asset_rec.id, slot_ids[i], slot_rec.host_profile_id, asset_rec.owner_uid, 87, start_ts, expire_ts, start_ts, start_ts)
ON CONFLICT (asset_id) WHERE deleted_at IS NULL DO NOTHING;
END IF;
END LOOP;
END $$;
-- 将asset_registry的display_status设为1
UPDATE asset_registry SET display_status = 1 WHERE id >= 10000;
UPDATE asset_registry SET status = 1 WHERE id >= 10000 AND display_status = 1;
UPDATE assets SET status = 1 WHERE star_id = 87 AND status = 0;
COMMIT;