Files
SupplierSystem/migrations/20261028_warehouse_storage_zone.sql
雨天 9ecb47f38a
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
Merge origin/main:登录口径改为「超管先开通+绑定校验」
- 登录:采用远端 13f8e72 的「不自动开分公司 + 绑定校验」,Web/小程序共用;删除本地 mainSystemSupplierLogin 与自动开通分支

- 分公司 PUT:两列并存(main_supplier_code + supplier_categories)

- 迁移改号:本地 20261026/20261027 → 20261028/20261029(远端占用 20261026)

- BranchManagement / localApiClient:两边字段与 UI 都保留
2026-10-10 15:42:50 +08:00

217 lines
8.1 KiB
PL/PgSQL
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- 仓库区域编号(可空)+ 库存批次唯一约束
--
-- 背景与决策见 docs/design/20261010-仓库区域编号与批量导入库存.md
-- 1) 商品默认存放区:product_catalog.storage_zone(同步到 skus.storage_zone)
-- 2) 批次实际存放区:inventory.storage_zone
-- 3) inventory 补 (sku_code, batch_no) 唯一约束(批量导入"累加同批次"依赖它)
--
-- 顺序(不可调换):
-- 1) 加三列
-- 2) 重建 public.sync_product_catalog_to_legacy()(带上 storage_zone,写 skus)
-- 3) inventory 唯一约束(先预检重复,有重复即报错中止;不静默去重)
--
-- 注:legacy_public 为归档 schema(skus 缺 pricing_unit),沿用既有做法跳过。
-- ============================================================
-- 1) 加列
-- ============================================================
DO $$
DECLARE
sch text;
BEGIN
FOR sch IN
SELECT nspname
FROM pg_namespace
WHERE nspname IN ('public', 'tenant_template')
OR nspname LIKE 'tenant_%'
ORDER BY CASE nspname
WHEN 'public' THEN 0
WHEN 'tenant_template' THEN 1
ELSE 2
END, nspname
LOOP
IF to_regclass(format('%I.product_catalog', sch)) IS NOT NULL THEN
EXECUTE format(
'ALTER TABLE %I.product_catalog ADD COLUMN IF NOT EXISTS storage_zone VARCHAR(50)',
sch
);
EXECUTE format(
'COMMENT ON COLUMN %I.product_catalog.storage_zone IS %L',
sch,
'商品默认存放区域编号(可空;同步到 skus.storage_zone)'
);
END IF;
IF to_regclass(format('%I.skus', sch)) IS NOT NULL THEN
EXECUTE format(
'ALTER TABLE %I.skus ADD COLUMN IF NOT EXISTS storage_zone VARCHAR(50)',
sch
);
EXECUTE format(
'COMMENT ON COLUMN %I.skus.storage_zone IS %L',
sch,
'商品默认存放区域编号(可空;由 product_catalog 同步)'
);
END IF;
IF to_regclass(format('%I.inventory', sch)) IS NOT NULL THEN
EXECUTE format(
'ALTER TABLE %I.inventory ADD COLUMN IF NOT EXISTS storage_zone VARCHAR(50)',
sch
);
EXECUTE format(
'COMMENT ON COLUMN %I.inventory.storage_zone IS %L',
sch,
'该批次实际存放区域编号(可空)'
);
END IF;
END LOOP;
END $$;
-- ============================================================
-- 2) 重建目录→旧表同步触发器(商品分支带上 storage_zone)
-- 基线:migrations/20261012_restore_product_catalog_sync_trigger.sql
-- 函数体用不带 schema 的表名,依赖调用会话的 search_path(与应用现状一致)。
-- ============================================================
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, stock_mode, direct_delivery, low_stock_alert_qty, storage_zone, 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,
NULLIF(TRIM(NEW.stock_mode), ''),
COALESCE(NEW.direct_delivery, false),
COALESCE(NEW.low_stock_alert_qty, 0),
NEW.storage_zone,
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,
stock_mode = EXCLUDED.stock_mode,
direct_delivery = EXCLUDED.direct_delivery,
low_stock_alert_qty = EXCLUDED.low_stock_alert_qty,
storage_zone = EXCLUDED.storage_zone,
status = EXCLUDED.status;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-- ============================================================
-- 3) inventory 批次唯一约束(预检 + 加约束)
-- (sku_code, batch_no):批量导入"累加同批次"依赖它做 ON CONFLICT
-- ============================================================
DO $$
DECLARE
sch text;
dup bigint;
BEGIN
FOR sch IN
SELECT nspname
FROM pg_namespace
WHERE nspname = 'tenant_template' OR nspname LIKE 'tenant_%'
ORDER BY nspname
LOOP
IF to_regclass(format('%I.inventory', sch)) IS NOT NULL THEN
EXECUTE format(
'SELECT count(*) FROM (SELECT sku_code, batch_no FROM %I.inventory GROUP BY sku_code, batch_no HAVING count(*) > 1) t',
sch
) INTO dup;
IF dup > 0 THEN
RAISE EXCEPTION 'schema % 的 inventory 存在 % 组重复 (sku_code,batch_no),请先人工合并后再迁移', sch, dup;
END IF;
IF NOT EXISTS (
SELECT 1 FROM pg_constraint
WHERE conname = 'inventory_sku_batch_uniq'
AND conrelid = format('%I.inventory', sch)::regclass
) THEN
EXECUTE format(
'ALTER TABLE %I.inventory ADD CONSTRAINT inventory_sku_batch_uniq UNIQUE (sku_code, batch_no)',
sch
);
END IF;
END IF;
END LOOP;
END $$;