-- P0 财务止损:单位换算主数据表(基准单位「斤」) -- 用途:orders.total_weight 统一折算到斤(配送成本 = total_weight × 单重费率)。 -- 箱/件/个等非重量单位不入表 —— 无可靠换算系数,重量合计保持排除(见 unitConversionService.ts)。 CREATE OR REPLACE FUNCTION public.tenant_business_tables() RETURNS TEXT[] LANGUAGE sql IMMUTABLE AS $$ SELECT ARRAY[ 'after_sales', 'alias_recognition_logs', 'allocation_tasks', 'bid_prices', 'categories', 'cost_adjustments', 'cost_details', 'customer_alias_stats', 'customer_delivery_assignments', 'customer_image_aliases', 'customer_quote_versions', 'customer_quote_sheets', 'customer_quote_sheet_items', 'customer_quote_sheet_customers', 'customer_sku_aliases', 'customer_processing_aliases', 'customer_skus', 'customer_statements', 'statement_lines', 'statement_disputes', 'statement_payments', 'customers', 'delivery_person_users', 'delivery_persons', 'delivery_person_locations', 'delivery_location_history', 'delivery_tasks', 'exception_tickets', 'fixed_costs', 'inspections', 'inspection_loss_confirmations', 'inspection_returns', 'inspection_review_logs', 'inventory', 'inventory_logs', 'invoices', 'notifications', 'operation_logs', 'order_item_fulfillments', 'order_items', 'orders', 'picking_assign_rules', 'picking_items', 'picking_tasks', 'picking_containers', 'picking_scan_logs', 'processing_logs', 'processing_tasks', 'product_catalog', 'purchase_orders', 'purchaser_users', 'quality_reports', 'sign_records', 'sku_aliases', 'sku_suppliers', 'skus', 'smart_classification_rules', 'supplier_categories', 'supplier_preparations', 'supplier_prep_amend_requests', 'supplier_statements', 'supplier_rating_snapshots', 'product_substitutions', 'delivery_signatures', 'customer_recipients', 'customer_portal_users', 'market_prices', 'market_compare_exclusions', 'market_price_indices', 'market_external_catalog', 'market_sku_external_bindings', 'seasonal_recommendations', 'order_allocation_batches', 'delivery_schedules', 'supplier_users', 'suppliers', 'system_configs', 'unit_conversions', 'voice_recognition_logs', 'warehouses', 'warehouse_locations' ]::text[]; $$; DO $$ DECLARE sch text; BEGIN FOR sch IN SELECT nspname FROM pg_namespace WHERE nspname = 'public' OR nspname = 'tenant_template' OR nspname LIKE 'tenant_%' LOOP IF NOT EXISTS ( SELECT 1 FROM information_schema.tables WHERE table_schema = sch AND table_name = 'orders' ) THEN CONTINUE; END IF; -- unit_code 为规范化单位键(去空格 + 小写,与 normalizeWeightUnitKey 对齐) EXECUTE format( 'CREATE TABLE IF NOT EXISTS %I.unit_conversions ( unit_code VARCHAR(50) PRIMARY KEY, unit_name VARCHAR(50) NOT NULL, base_unit VARCHAR(20) NOT NULL DEFAULT ''斤'', factor_to_base NUMERIC(12,6) NOT NULL, is_weight BOOLEAN NOT NULL DEFAULT true, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW() )', sch ); -- 预置重量单位:1 斤 = 500 克;公斤/千克/kg = 2 斤;克/g = 0.002 斤;两 = 0.1 斤 EXECUTE format( 'INSERT INTO %I.unit_conversions (unit_code, unit_name, base_unit, factor_to_base, is_weight) VALUES (''斤'', ''斤'', ''斤'', 1, true), (''市斤'', ''市斤'', ''斤'', 1, true), (''jin'', ''斤'', ''斤'', 1, true), (''公斤'', ''公斤'', ''斤'', 2, true), (''千克'', ''千克'', ''斤'', 2, true), (''kg'', ''公斤'', ''斤'', 2, true), (''克'', ''克'', ''斤'', 0.002, true), (''g'', ''克'', ''斤'', 0.002, true), (''两'', ''两'', ''斤'', 0.1, true) ON CONFLICT (unit_code) DO NOTHING', sch ); END LOOP; END $$; SELECT public.ensure_tenant_schemas_match_template();