-- 检疫报告共用粒度可配置:categories/product_catalog 加 quality_report_share_scope -- 设计:docs/design/20260723-quality-report-share-scope-config.md -- 取值:'category'(整类共用,默认)/ 'product_name'(按商品名归组共用,如水产) DO $$ DECLARE sch text; BEGIN FOR sch IN SELECT nspname FROM pg_namespace WHERE nspname IN ('public', 'legacy_public', 'tenant_template') OR nspname LIKE 'tenant_%' LOOP IF EXISTS ( SELECT 1 FROM information_schema.tables WHERE table_schema = sch AND table_name = 'product_catalog' ) THEN EXECUTE format( 'ALTER TABLE %I.product_catalog ADD COLUMN IF NOT EXISTS quality_report_share_scope VARCHAR(20) NOT NULL DEFAULT ''category''', sch ); END IF; IF EXISTS ( SELECT 1 FROM information_schema.tables WHERE table_schema = sch AND table_name = 'categories' ) THEN EXECUTE format( 'ALTER TABLE %I.categories ADD COLUMN IF NOT EXISTS quality_report_share_scope VARCHAR(20) NOT NULL DEFAULT ''category''', sch ); END IF; END LOOP; END $$; -- 同步触发器:把新列从 product_catalog 带到 categories CREATE OR REPLACE FUNCTION public.sync_product_catalog_to_legacy() RETURNS TRIGGER AS $$ DECLARE v_branch VARCHAR(50); BEGIN IF TG_OP = 'DELETE' THEN v_branch := COALESCE(OLD.branch_code, ( SELECT branch_code FROM branches WHERE status = 1 ORDER BY branch_code LIMIT 1 )); IF OLD.item_type = 'category' THEN DELETE FROM categories WHERE category_code = OLD.code AND branch_code = v_branch; ELSE DELETE FROM skus WHERE sku_code = OLD.code; END IF; RETURN OLD; END IF; v_branch := COALESCE(NEW.branch_code, ( SELECT branch_code FROM branches WHERE status = 1 ORDER BY branch_code LIMIT 1 )); IF NEW.item_type = 'category' THEN INSERT INTO categories ( branch_code, category_code, parent_code, category_name, level, sort_order, loss_threshold, is_fresh, requires_photo_inspection, requires_quality_report, quality_report_type, quality_report_label, quality_report_share_scope, status, created_at ) VALUES ( v_branch, NEW.code, NEW.parent_code, NEW.name, COALESCE(NEW.level, 1), COALESCE(NEW.sort_order, 0), COALESCE(NEW.loss_threshold, 5.00), COALESCE(NEW.is_fresh, true), COALESCE(NEW.requires_photo_inspection, true), COALESCE(NEW.requires_quality_report, false), NEW.quality_report_type, NEW.quality_report_label, COALESCE(NEW.quality_report_share_scope, 'category'), COALESCE(NEW.status, 1), COALESCE(NEW.created_at, NOW()) ) ON CONFLICT (branch_code, category_code) DO UPDATE SET parent_code = EXCLUDED.parent_code, category_name = EXCLUDED.category_name, level = EXCLUDED.level, sort_order = EXCLUDED.sort_order, loss_threshold = EXCLUDED.loss_threshold, is_fresh = EXCLUDED.is_fresh, requires_photo_inspection = EXCLUDED.requires_photo_inspection, requires_quality_report = EXCLUDED.requires_quality_report, quality_report_type = EXCLUDED.quality_report_type, quality_report_label = EXCLUDED.quality_report_label, quality_report_share_scope = EXCLUDED.quality_report_share_scope, status = EXCLUDED.status; ELSE INSERT INTO skus ( branch_code, sku_code, sku_name, category_code, unit, spec, barcode, image_url, image_data, image_mime_type, is_prepackaged, brand, tax_code, tax_rate, default_loss_threshold, sale_price, cost_price, status, created_at ) VALUES ( v_branch, NEW.code, NEW.name, NEW.parent_code, COALESCE(NEW.unit, '斤'), NEW.spec, NEW.barcode, NEW.image_url, NEW.image_data, NEW.image_mime_type, COALESCE(NEW.is_prepackaged, false), NEW.brand, NULLIF(TRIM(NEW.tax_code), ''), NEW.tax_rate, NEW.default_loss_threshold, COALESCE(NEW.sale_price, 0), COALESCE(NEW.cost_price, 0), COALESCE(NEW.status, 1), COALESCE(NEW.created_at, NOW()) ) ON CONFLICT (sku_code) DO UPDATE SET branch_code = EXCLUDED.branch_code, sku_name = EXCLUDED.sku_name, category_code = EXCLUDED.category_code, unit = EXCLUDED.unit, spec = EXCLUDED.spec, barcode = EXCLUDED.barcode, image_url = EXCLUDED.image_url, image_data = EXCLUDED.image_data, image_mime_type = EXCLUDED.image_mime_type, is_prepackaged = EXCLUDED.is_prepackaged, brand = EXCLUDED.brand, tax_code = EXCLUDED.tax_code, tax_rate = EXCLUDED.tax_rate, default_loss_threshold = EXCLUDED.default_loss_threshold, sale_price = EXCLUDED.sale_price, cost_price = EXCLUDED.cost_price, status = EXCLUDED.status; END IF; RETURN NEW; END; $$ LANGUAGE plpgsql; -- 数据订正:水产 → 按商品名归组共用(牛蛙/草鱼/鲫鱼各自一张) -- 注意:product_catalog 上有 trg_sync_product_catalog_to_legacy 触发器(public 函数,裸表名 categories/skus), -- 默认 search_path 下 UPDATE 会报「关系 categories 不存在」。订正时临时禁用该触发器,完成后恢复。 DO $$ DECLARE sch text; BEGIN FOR sch IN SELECT nspname FROM pg_namespace WHERE nspname = 'tenant_template' OR nspname LIKE 'tenant_%' LOOP IF EXISTS ( SELECT 1 FROM information_schema.tables WHERE table_schema = sch AND table_name = 'product_catalog' ) THEN BEGIN EXECUTE format('ALTER TABLE %I.product_catalog DISABLE TRIGGER USER', sch); EXECUTE format( 'UPDATE %I.product_catalog SET quality_report_share_scope = ''product_name'' WHERE item_type = ''category'' AND name = ''水产''', sch ); EXECUTE format('ALTER TABLE %I.product_catalog ENABLE TRIGGER USER', sch); EXCEPTION WHEN OTHERS THEN BEGIN EXECUTE format('ALTER TABLE %I.product_catalog ENABLE TRIGGER USER', sch); EXCEPTION WHEN OTHERS THEN NULL; END; RAISE NOTICE 'skip product_catalog backfill on %: %', sch, SQLERRM; END; END IF; IF EXISTS ( SELECT 1 FROM information_schema.tables WHERE table_schema = sch AND table_name = 'categories' ) THEN EXECUTE format( 'UPDATE %I.categories SET quality_report_share_scope = ''product_name'' WHERE category_name = ''水产''', sch ); END IF; END LOOP; END $$;