-- 方案商改为「手工指定 ID + 厂商分组」,并按 factory.factoryflag 回填品牌商归属。 -- factoryflag 就是方案商 ID(十六进制),此前误当作无意义常量丢弃,这里补回来。 \set ON_ERROR_STOP on BEGIN; -- 1. 新增分组列;id 不再自增,去掉序列默认值(避免意外生成无意义 ID) ALTER TABLE solution_provider ADD COLUMN IF NOT EXISTS vendor integer DEFAULT 0; ALTER TABLE solution_provider ALTER COLUMN id DROP DEFAULT; -- 2. 删掉脚本 1 用自增建的占位记录,仅在无品牌商引用时删。 -- id 取决于各库 solution_provider_id_seq 的起点(广州测试环境是 43780~43782, -- 全新库则是 1~3),所以按 name 判定,不能写死 id。 DELETE FROM solution_provider sp WHERE (sp.id IN (43780, 43781, 43782) OR sp.name IN ('jieli', 'lanxun', 'henxuan')) AND NOT EXISTS (SELECT 1 FROM brand b WHERE b.providerid = sp.id); -- 3. 按真实 ID 建方案商(vendor: 1=杰里 2=蓝汛 3=恒玄 0=未指定) INSERT INTO solution_provider (id, name, vendor, description, contactmails, createtime) VALUES (x'ABEF'::int, '杰里', 1, '历史方案 ABEF(已烧录进设备,ID 不可变更)', '', extract(epoch from now())::bigint), (x'ABF0'::int, '蓝汛', 2, '历史方案 ABF0(原厂家标识:兴启点)', '', extract(epoch from now())::bigint), (x'ABF1'::int, '蓝汛', 2, '历史方案 ABF1(原厂家标识:ZENSBUDS LI)', '', extract(epoch from now())::bigint), (x'ABF2'::int, '蓝汛', 2, '历史方案 ABF2(原厂家标识:卓讯达科技)', '', extract(epoch from now())::bigint), (x'ABF3'::int, '蓝汛', 2, '历史方案 ABF3(原厂家标识:蓝宝)', '', extract(epoch from now())::bigint), (x'ABF4'::int, '恒玄', 3, '历史方案 ABF4(原厂家标识:恒玄)', '', extract(epoch from now())::bigint), (x'AB21'::int, '慕然科技', 0, '历史方案 AB21,所属厂商待确认', '', extract(epoch from now())::bigint) ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name, vendor = EXCLUDED.vendor, description = EXCLUDED.description; -- 4. 品牌商归属:直接由 factoryflag(十六进制)解析出方案商 ID UPDATE brand b SET providerid = ('x' || lpad(f.factoryflag, 8, '0'))::bit(32)::int FROM factory f WHERE f.id = b.id AND COALESCE(f.factoryflag, '') <> '' AND EXISTS (SELECT 1 FROM solution_provider s WHERE s.id = ('x' || lpad(f.factoryflag, 8, '0'))::bit(32)::int); -- 5. factoryflag 为空的两家(a01f、a023)归杰里 ABEF UPDATE brand b SET providerid = x'ABEF'::int FROM factory f WHERE f.id = b.id AND COALESCE(f.factoryflag, '') = ''; COMMIT;