Files
SupplierSystem/migrations/20260788_category_quality_reports.sql
Ubuntu deb3e72750
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
chore: 以 AI 生鲜配送系统 当前版本作为 SupplierSystem 起点
来源仓库:jxc/ai-fresh-delivery
来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d
快照树:  09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件)

按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据
(生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、
deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值),
整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。
供应商系统后续开发自本提交开始。
2026-10-08 17:35:43 +08:00

316 lines
9.8 KiB
PL/PgSQL

-- 分类检验报告 + 硬门禁开关
-- 设计:docs/design/20260721-category-quality-report-trace.md
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',
'voice_recognition_logs',
'warehouses',
'warehouse_locations'
]::text[];
$$;
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_br%'
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 requires_quality_report BOOLEAN NOT NULL DEFAULT false',
sch
);
EXECUTE format(
'ALTER TABLE %I.product_catalog
ADD COLUMN IF NOT EXISTS quality_report_type VARCHAR(20)',
sch
);
EXECUTE format(
'ALTER TABLE %I.product_catalog
ADD COLUMN IF NOT EXISTS quality_report_label VARCHAR(50)',
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 requires_quality_report BOOLEAN NOT NULL DEFAULT false',
sch
);
EXECUTE format(
'ALTER TABLE %I.categories
ADD COLUMN IF NOT EXISTS quality_report_type VARCHAR(20)',
sch
);
EXECUTE format(
'ALTER TABLE %I.categories
ADD COLUMN IF NOT EXISTS quality_report_label VARCHAR(50)',
sch
);
END IF;
IF NOT EXISTS (
SELECT 1 FROM information_schema.tables
WHERE table_schema = sch AND table_name = 'supplier_preparations'
) THEN
CONTINUE;
END IF;
EXECUTE format(
'CREATE TABLE IF NOT EXISTS %I.quality_reports (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
branch_code VARCHAR(50),
report_type VARCHAR(20) NOT NULL DEFAULT ''other'',
report_label VARCHAR(50),
preparation_id UUID,
purchase_code VARCHAR(50),
order_code VARCHAR(50),
sku_code VARCHAR(50),
supplier_code VARCHAR(50),
batch_no VARCHAR(50),
file_paths TEXT[] NOT NULL DEFAULT ARRAY[]::TEXT[],
status SMALLINT NOT NULL DEFAULT 10,
source_party VARCHAR(20) NOT NULL DEFAULT ''supplier'',
uploaded_by UUID,
uploaded_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
tested_at DATE,
valid_from DATE,
valid_to DATE,
compare_at DATE,
validity_status VARCHAR(20) NOT NULL DEFAULT ''unknown'',
ai_raw JSONB,
ai_parsed_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
CONSTRAINT quality_reports_prep_or_purchase CHECK (
preparation_id IS NOT NULL OR (purchase_code IS NOT NULL AND TRIM(purchase_code) <> '''')
)
)',
sch
);
EXECUTE format(
'CREATE UNIQUE INDEX IF NOT EXISTS uq_quality_reports_prep_active
ON %I.quality_reports (preparation_id)
WHERE status = 10 AND preparation_id IS NOT NULL',
sch
);
EXECUTE format(
'CREATE UNIQUE INDEX IF NOT EXISTS uq_quality_reports_purchase_sku_active
ON %I.quality_reports (purchase_code, (COALESCE(sku_code, '''')))
WHERE status = 10 AND purchase_code IS NOT NULL',
sch
);
EXECUTE format(
'CREATE INDEX IF NOT EXISTS idx_quality_reports_batch
ON %I.quality_reports (batch_no)
WHERE batch_no IS NOT NULL',
sch
);
EXECUTE format(
'CREATE INDEX IF NOT EXISTS idx_quality_reports_branch_status
ON %I.quality_reports (branch_code, status)',
sch
);
END LOOP;
END $$;
-- 同步触发器:分类新字段写入 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,
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.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,
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;
INSERT INTO system_configs (config_key, config_value, config_group, description, branch_code)
SELECT
'quality_report_hard_gate_enabled',
'{"enabled": false}'::jsonb,
'preparation',
'开启后:需检验报告的分类在验收入库前必须有有效期内报告',
NULL
WHERE NOT EXISTS (
SELECT 1 FROM system_configs
WHERE config_key = 'quality_report_hard_gate_enabled' AND branch_code IS NULL
);
SELECT public.ensure_tenant_schemas_match_template();