-- SKU 计价单位 + 包装折算;备货归一单价与脏价标记 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, merge_same_day_fulfillment, 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.merge_same_day_fulfillment, false), 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, merge_same_day_fulfillment = EXCLUDED.merge_same_day_fulfillment, status = EXCLUDED.status; ELSE INSERT INTO skus ( branch_code, sku_code, sku_name, category_code, unit, pricing_unit, pack_factors, spec, barcode, image_url, image_data, image_mime_type, is_prepackaged, brand, tax_code, tax_rate, default_loss_threshold, status, created_at ) VALUES ( v_branch, NEW.code, NEW.name, NEW.parent_code, COALESCE(NEW.unit, '斤'), COALESCE(NULLIF(TRIM(NEW.pricing_unit), ''), COALESCE(NEW.unit, '斤')), COALESCE(NEW.pack_factors, '{}'::jsonb), 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.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, pricing_unit = EXCLUDED.pricing_unit, pack_factors = EXCLUDED.pack_factors, 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, status = EXCLUDED.status; END IF; RETURN NEW; END; $$ LANGUAGE plpgsql; DO $$ DECLARE sch text; BEGIN FOR sch IN SELECT nspname FROM pg_namespace WHERE nspname IN ('public', 'tenant_template', 'legacy_public') OR nspname LIKE 'tenant_%' LOOP BEGIN 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 pricing_unit VARCHAR(20), ADD COLUMN IF NOT EXISTS pack_factors JSONB NOT NULL DEFAULT ''{}''::jsonb', sch ); EXECUTE format( 'UPDATE %I.product_catalog SET pricing_unit = COALESCE(NULLIF(TRIM(pricing_unit), ''''), NULLIF(TRIM(unit), ''''), ''斤'') WHERE item_type = ''product'' AND (pricing_unit IS NULL OR TRIM(pricing_unit) = '''')', sch ); END IF; IF EXISTS ( SELECT 1 FROM information_schema.tables WHERE table_schema = sch AND table_name = 'skus' ) THEN EXECUTE format( 'ALTER TABLE %I.skus ADD COLUMN IF NOT EXISTS pricing_unit VARCHAR(20), ADD COLUMN IF NOT EXISTS pack_factors JSONB NOT NULL DEFAULT ''{}''::jsonb', sch ); EXECUTE format( 'UPDATE %I.skus SET pricing_unit = COALESCE(NULLIF(TRIM(pricing_unit), ''''), NULLIF(TRIM(unit), ''''), ''斤'') WHERE pricing_unit IS NULL OR TRIM(pricing_unit) = ''''', sch ); END IF; IF EXISTS ( SELECT 1 FROM information_schema.tables WHERE table_schema = sch AND table_name = 'supplier_preparations' ) THEN EXECUTE format( 'ALTER TABLE %I.supplier_preparations ADD COLUMN IF NOT EXISTS purchase_price_normalized NUMERIC(12,4), ADD COLUMN IF NOT EXISTS normalized_pricing_unit VARCHAR(20), ADD COLUMN IF NOT EXISTS pack_factor_applied NUMERIC(12,6), ADD COLUMN IF NOT EXISTS price_outlier BOOLEAN NOT NULL DEFAULT false', sch ); END IF; EXCEPTION WHEN OTHERS THEN RAISE NOTICE 'skip pricing normalize on schema %: %', sch, SQLERRM; END; END LOOP; END $$; SELECT public.ensure_tenant_schemas_match_template();