来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
284 lines
11 KiB
PL/PgSQL
284 lines
11 KiB
PL/PgSQL
-- 库存预警改为「按 SKU 设置数量阈值」(0 = 不预警)
|
||
--
|
||
-- 背景与决策见 docs/design/20260914-sku-low-stock-alert-qty.md
|
||
-- 1) 安全线从全局常量 10 改为每个 SKU 自己的阈值 skus.low_stock_alert_qty
|
||
-- 2) 旧布尔 skus.low_stock_alert 退役:true → 10(行为不变),随后删列
|
||
-- 3) 分类级 low_stock_alert_default 一并退役,默认一律 0
|
||
-- 4) 允许 0 ~ 300.00,两位小数;0 = 不预警
|
||
--
|
||
-- 顺序(不可调换):
|
||
-- 1) skus / product_catalog 各自加新列(含 CHECK 0~300)
|
||
-- 2) 手工回填两边(**临时禁用** product_catalog 的同步触发器:
|
||
-- 此刻触发器还是旧函数体,写入会引用即将删除的 low_stock_alert / low_stock_alert_default)
|
||
-- 3) 用新列重建 public.sync_product_catalog_to_legacy()
|
||
-- 4) 最后才删旧列(触发器已不再引用)
|
||
--
|
||
-- 注:legacy_public 为归档 schema(skus 缺 pricing_unit),沿用
|
||
-- 20260915 迁移的做法跳过,不参与本次结构变更。
|
||
|
||
-- ============================================================
|
||
-- 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.skus', sch)) IS NOT NULL THEN
|
||
EXECUTE format(
|
||
'ALTER TABLE %I.skus ADD COLUMN IF NOT EXISTS low_stock_alert_qty NUMERIC(10,2) NOT NULL DEFAULT 0',
|
||
sch
|
||
);
|
||
EXECUTE format(
|
||
'COMMENT ON COLUMN %I.skus.low_stock_alert_qty IS %L',
|
||
sch,
|
||
'库存预警安全线:可用量合计低于此值即预警;0 = 不参与预警(范围 0~300)'
|
||
);
|
||
BEGIN
|
||
EXECUTE format(
|
||
'ALTER TABLE %I.skus ADD CONSTRAINT skus_low_stock_alert_qty_range CHECK (low_stock_alert_qty >= 0 AND low_stock_alert_qty <= 300)',
|
||
sch
|
||
);
|
||
EXCEPTION WHEN duplicate_object THEN
|
||
NULL;
|
||
END;
|
||
END IF;
|
||
|
||
IF to_regclass(format('%I.product_catalog', sch)) IS NOT NULL THEN
|
||
EXECUTE format(
|
||
'ALTER TABLE %I.product_catalog ADD COLUMN IF NOT EXISTS low_stock_alert_qty NUMERIC(10,2) NOT NULL DEFAULT 0',
|
||
sch
|
||
);
|
||
EXECUTE format(
|
||
'COMMENT ON COLUMN %I.product_catalog.low_stock_alert_qty IS %L',
|
||
sch,
|
||
'该商品的库存预警安全线(0 = 不预警;同步到 skus.low_stock_alert_qty)'
|
||
);
|
||
END IF;
|
||
END LOOP;
|
||
END $$;
|
||
|
||
-- ============================================================
|
||
-- 2) 手工回填(触发器临时禁用)
|
||
-- 旧语义 = 布尔勾选 ⇔ 新语义 = 阈值 10(与原常量一致,行为不变)
|
||
-- ============================================================
|
||
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
|
||
BEGIN
|
||
EXECUTE format('ALTER TABLE %I.product_catalog DISABLE TRIGGER trg_sync_product_catalog_to_legacy', sch);
|
||
EXCEPTION WHEN undefined_object THEN
|
||
NULL;
|
||
END;
|
||
|
||
IF EXISTS (
|
||
SELECT 1 FROM information_schema.columns
|
||
WHERE table_schema = sch AND table_name = 'product_catalog' AND column_name = 'low_stock_alert'
|
||
) THEN
|
||
EXECUTE format(
|
||
'UPDATE %I.product_catalog SET low_stock_alert_qty = 10 WHERE COALESCE(low_stock_alert, false) = true AND low_stock_alert_qty = 0',
|
||
sch
|
||
);
|
||
END IF;
|
||
|
||
BEGIN
|
||
EXECUTE format('ALTER TABLE %I.product_catalog ENABLE TRIGGER trg_sync_product_catalog_to_legacy', sch);
|
||
EXCEPTION WHEN undefined_object THEN
|
||
NULL;
|
||
END;
|
||
END IF;
|
||
|
||
-- skus 侧:直接按旧布尔回填(不依赖 product_catalog 是否存在对应行)
|
||
IF to_regclass(format('%I.skus', sch)) IS NOT NULL
|
||
AND EXISTS (
|
||
SELECT 1 FROM information_schema.columns
|
||
WHERE table_schema = sch AND table_name = 'skus' AND column_name = 'low_stock_alert'
|
||
) THEN
|
||
EXECUTE format(
|
||
'UPDATE %I.skus SET low_stock_alert_qty = 10 WHERE COALESCE(low_stock_alert, false) = true AND low_stock_alert_qty = 0',
|
||
sch
|
||
);
|
||
END IF;
|
||
END LOOP;
|
||
END $$;
|
||
|
||
-- ============================================================
|
||
-- 3) 重建目录→旧表同步触发器
|
||
-- 分类分支去掉 low_stock_alert_default;商品分支 low_stock_alert → low_stock_alert_qty。
|
||
-- 结构与 migrations/20260915_low_stock_alert_default_dry_goods.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, 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),
|
||
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,
|
||
status = EXCLUDED.status;
|
||
END IF;
|
||
|
||
RETURN NEW;
|
||
END;
|
||
$$ LANGUAGE plpgsql;
|
||
|
||
-- ============================================================
|
||
-- 4) 删除旧列(触发器已重建,不再引用)
|
||
-- ============================================================
|
||
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 EXISTS (
|
||
SELECT 1 FROM information_schema.columns
|
||
WHERE table_schema = sch AND table_name = 'skus' AND column_name = 'low_stock_alert'
|
||
) THEN
|
||
EXECUTE format('ALTER TABLE %I.skus DROP COLUMN low_stock_alert', sch);
|
||
END IF;
|
||
|
||
IF EXISTS (
|
||
SELECT 1 FROM information_schema.columns
|
||
WHERE table_schema = sch AND table_name = 'product_catalog' AND column_name = 'low_stock_alert'
|
||
) THEN
|
||
EXECUTE format('ALTER TABLE %I.product_catalog DROP COLUMN low_stock_alert', sch);
|
||
END IF;
|
||
|
||
IF EXISTS (
|
||
SELECT 1 FROM information_schema.columns
|
||
WHERE table_schema = sch AND table_name = 'categories' AND column_name = 'low_stock_alert_default'
|
||
) THEN
|
||
EXECUTE format('ALTER TABLE %I.categories DROP COLUMN low_stock_alert_default', sch);
|
||
END IF;
|
||
|
||
IF EXISTS (
|
||
SELECT 1 FROM information_schema.columns
|
||
WHERE table_schema = sch AND table_name = 'product_catalog' AND column_name = 'low_stock_alert_default'
|
||
) THEN
|
||
EXECUTE format('ALTER TABLE %I.product_catalog DROP COLUMN low_stock_alert_default', sch);
|
||
END IF;
|
||
END LOOP;
|
||
END $$;
|