Files
SupplierSystem/migrations/20260915_low_stock_alert_default_dry_goods.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

227 lines
8.2 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.
-- 1) 加列(先加列,再更新触发器,最后回填数据)
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_%'
ORDER BY CASE nspname
WHEN 'public' THEN 0
WHEN 'legacy_public' THEN 1
WHEN 'tenant_template' THEN 2
ELSE 3
END, nspname
LOOP
IF to_regclass(format('%I.categories', sch)) IS NOT NULL THEN
EXECUTE format(
'ALTER TABLE %I.categories ADD COLUMN IF NOT EXISTS low_stock_alert_default BOOLEAN NOT NULL DEFAULT false',
sch
);
EXECUTE format(
'COMMENT ON COLUMN %I.categories.low_stock_alert_default IS %L',
sch,
'该分类商品新建时的「库存预警」勾选默认值(干杂 true,生鲜 false)'
);
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_default BOOLEAN NOT NULL DEFAULT false',
sch
);
EXECUTE format(
'COMMENT ON COLUMN %I.product_catalog.low_stock_alert_default IS %L',
sch,
'分类行:该分类商品新建时的库存预警勾选默认值'
);
END IF;
END LOOP;
END $$;
-- 2) 触发器(带新列):目录 → 旧表 同步
-- 目录 → 旧表 同步触发器:分类分支带上 low_stock_alert_default,商品分支带上 low_stock_alert
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,
low_stock_alert_default,
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.low_stock_alert_default, 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,
low_stock_alert_default = EXCLUDED.low_stock_alert_default,
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, 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, false),
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 = EXCLUDED.low_stock_alert,
status = EXCLUDED.status;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-- 3) 存量回填:干杂默认开启 —— 分类默认值 + 该分类下已有商品
-- 注意:product_catalog 上的同步触发器用的是**不带 schema 的**表名,
-- 因此回填前必须把 search_path 切到当前 schema,否则触发器找不到 categories/skus。
DO $$
DECLARE
sch text;
v_dry text[];
BEGIN
FOR sch IN
SELECT nspname
FROM pg_namespace
WHERE nspname IN ('public', 'legacy_public', 'tenant_template')
OR nspname LIKE 'tenant_%'
ORDER BY CASE nspname
WHEN 'public' THEN 0
WHEN 'legacy_public' THEN 1
WHEN 'tenant_template' THEN 2
ELSE 3
END, nspname
LOOP
IF to_regclass(format('%I.categories', sch)) IS NULL THEN
CONTINUE;
END IF;
-- 归档/旧结构 schema(如 legacy_public 的 skus 缺 pricing_unit)跳过:
-- 触发器按现代 skus 结构写列,这类 schema 不由应用使用。
IF to_regclass(format('%I.skus', sch)) IS NOT NULL
AND NOT EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_schema = sch AND table_name = 'skus' AND column_name = 'pricing_unit'
) THEN
CONTINUE;
END IF;
PERFORM set_config('search_path', format('%I, public', sch), true);
EXECUTE format(
'UPDATE %I.categories SET low_stock_alert_default = true WHERE category_name = %L',
sch, '干杂'
);
EXECUTE format(
'SELECT COALESCE(array_agg(category_code), ARRAY[]::text[]) FROM %I.categories
WHERE category_name = %L AND COALESCE(low_stock_alert_default, false)',
sch, '干杂'
) INTO v_dry;
IF v_dry IS NULL OR array_length(v_dry, 1) IS NULL THEN
CONTINUE;
END IF;
IF to_regclass(format('%I.product_catalog', sch)) IS NOT NULL THEN
EXECUTE format(
'UPDATE %I.product_catalog SET low_stock_alert_default = true
WHERE item_type = %L AND name = %L',
sch, 'category', '干杂'
);
EXECUTE format(
'UPDATE %I.product_catalog SET low_stock_alert = true
WHERE item_type = %L AND parent_code = ANY($1) AND COALESCE(low_stock_alert, false) = false',
sch, 'product'
) USING v_dry;
END IF;
IF to_regclass(format('%I.skus', sch)) IS NOT NULL THEN
EXECUTE format(
'UPDATE %I.skus SET low_stock_alert = true
WHERE category_code = ANY($1) AND COALESCE(low_stock_alert, false) = false',
sch
) USING v_dry;
END IF;
END LOOP;
PERFORM set_config('search_path', 'public', true);
END $$;