来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
187 lines
6.9 KiB
PL/PgSQL
187 lines
6.9 KiB
PL/PgSQL
-- 分类按分公司隔离,商品库(SKU/商品)全平台共享
|
|
|
|
-- 0. 迁移期间关闭同步触发器,避免旧函数与新建复合主键冲突
|
|
ALTER TABLE product_catalog DISABLE TRIGGER trg_sync_product_catalog_to_legacy;
|
|
|
|
-- 1. categories:增加 branch_code,复合主键
|
|
ALTER TABLE categories ADD COLUMN IF NOT EXISTS branch_code VARCHAR(50);
|
|
|
|
UPDATE categories
|
|
SET branch_code = COALESCE(
|
|
(SELECT branch_code FROM branches WHERE status = 1 ORDER BY branch_code LIMIT 1),
|
|
'BR001'
|
|
)
|
|
WHERE branch_code IS NULL;
|
|
|
|
ALTER TABLE skus DROP CONSTRAINT IF EXISTS skus_category_code_fkey;
|
|
ALTER TABLE order_items DROP CONSTRAINT IF EXISTS order_items_category_code_fkey;
|
|
ALTER TABLE customer_skus DROP CONSTRAINT IF EXISTS customer_skus_category_code_fkey;
|
|
ALTER TABLE smart_classification_rules DROP CONSTRAINT IF EXISTS smart_classification_rules_category_code_fkey;
|
|
|
|
DO $$
|
|
BEGIN
|
|
IF EXISTS (
|
|
SELECT 1 FROM information_schema.table_constraints
|
|
WHERE table_name = 'customer_image_aliases' AND constraint_name LIKE '%category_code%'
|
|
) THEN
|
|
EXECUTE (
|
|
SELECT 'ALTER TABLE customer_image_aliases DROP CONSTRAINT ' || quote_ident(constraint_name)
|
|
FROM information_schema.table_constraints
|
|
WHERE table_name = 'customer_image_aliases' AND constraint_name LIKE '%category_code%'
|
|
LIMIT 1
|
|
);
|
|
END IF;
|
|
END $$;
|
|
|
|
ALTER TABLE categories DROP CONSTRAINT IF EXISTS categories_pkey;
|
|
ALTER TABLE categories ADD PRIMARY KEY (branch_code, category_code);
|
|
|
|
DO $$
|
|
BEGIN
|
|
IF NOT EXISTS (
|
|
SELECT 1 FROM information_schema.table_constraints
|
|
WHERE constraint_name = 'categories_branch_code_fkey'
|
|
) THEN
|
|
ALTER TABLE categories
|
|
ADD CONSTRAINT categories_branch_code_fkey
|
|
FOREIGN KEY (branch_code) REFERENCES branches(branch_code);
|
|
END IF;
|
|
END $$;
|
|
|
|
-- 2. product_catalog:分类带 branch_code,商品全局唯一 code
|
|
ALTER TABLE product_catalog ADD COLUMN IF NOT EXISTS branch_code VARCHAR(50);
|
|
|
|
UPDATE product_catalog
|
|
SET branch_code = COALESCE(
|
|
(SELECT branch_code FROM branches WHERE status = 1 ORDER BY branch_code LIMIT 1),
|
|
'BR001'
|
|
)
|
|
WHERE item_type = 'category' AND branch_code IS NULL;
|
|
|
|
ALTER TABLE product_catalog DROP CONSTRAINT IF EXISTS product_catalog_code_key;
|
|
|
|
CREATE UNIQUE INDEX IF NOT EXISTS uq_product_catalog_product_code
|
|
ON product_catalog (code) WHERE item_type = 'product';
|
|
|
|
CREATE UNIQUE INDEX IF NOT EXISTS uq_product_catalog_category_branch_code
|
|
ON product_catalog (branch_code, code) WHERE item_type = 'category';
|
|
|
|
-- 3. 智能分类规则按分公司
|
|
ALTER TABLE smart_classification_rules ADD COLUMN IF NOT EXISTS branch_code VARCHAR(50);
|
|
|
|
UPDATE smart_classification_rules
|
|
SET branch_code = COALESCE(
|
|
(SELECT branch_code FROM branches WHERE status = 1 ORDER BY branch_code LIMIT 1),
|
|
'BR001'
|
|
)
|
|
WHERE branch_code IS NULL;
|
|
|
|
ALTER TABLE smart_classification_rules ALTER COLUMN branch_code SET NOT NULL;
|
|
|
|
-- 3.5 复合主键就绪后更新同步函数
|
|
CREATE OR REPLACE FUNCTION sync_product_catalog_to_legacy()
|
|
RETURNS TRIGGER AS $$
|
|
BEGIN
|
|
IF TG_OP = 'DELETE' THEN
|
|
IF OLD.item_type = 'category' THEN
|
|
DELETE FROM categories
|
|
WHERE category_code = OLD.code
|
|
AND branch_code = COALESCE(OLD.branch_code, (
|
|
SELECT branch_code FROM branches WHERE status = 1 ORDER BY branch_code LIMIT 1
|
|
));
|
|
ELSE
|
|
DELETE FROM skus WHERE sku_code = OLD.code;
|
|
END IF;
|
|
RETURN OLD;
|
|
END IF;
|
|
|
|
IF NEW.item_type = 'category' THEN
|
|
INSERT INTO categories (
|
|
branch_code, category_code, parent_code, category_name, level, sort_order,
|
|
loss_threshold, is_fresh, status, created_at
|
|
) VALUES (
|
|
COALESCE(NEW.branch_code, (SELECT branch_code FROM branches WHERE status = 1 ORDER BY branch_code LIMIT 1)),
|
|
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.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,
|
|
status = EXCLUDED.status;
|
|
ELSE
|
|
INSERT INTO skus (
|
|
sku_code, sku_name, category_code, unit, spec, barcode, image_url,
|
|
image_data, image_mime_type,
|
|
default_loss_threshold, sale_price, cost_price, status, created_at
|
|
) VALUES (
|
|
NEW.code, NEW.name, NEW.parent_code, COALESCE(NEW.unit, '斤'), NEW.spec, NEW.barcode, NEW.image_url,
|
|
NEW.image_data, NEW.image_mime_type,
|
|
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
|
|
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,
|
|
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;
|
|
|
|
-- 4. 为其他分公司复制 BR001 分类树(幂等)
|
|
INSERT INTO categories (
|
|
branch_code, category_code, parent_code, category_name, level, sort_order,
|
|
loss_threshold, is_fresh, status, created_at
|
|
)
|
|
SELECT
|
|
b.branch_code, c.category_code, c.parent_code, c.category_name, c.level, c.sort_order,
|
|
c.loss_threshold, c.is_fresh, c.status, c.created_at
|
|
FROM categories c
|
|
CROSS JOIN branches b
|
|
WHERE c.branch_code = (
|
|
SELECT branch_code FROM branches WHERE status = 1 ORDER BY branch_code LIMIT 1
|
|
)
|
|
AND b.branch_code <> c.branch_code
|
|
AND b.status = 1
|
|
ON CONFLICT (branch_code, category_code) DO NOTHING;
|
|
|
|
INSERT INTO product_catalog (
|
|
item_type, code, name, parent_code, level, sort_order, loss_threshold, is_fresh,
|
|
status, branch_code, created_at, updated_at
|
|
)
|
|
SELECT
|
|
'category', pc.code, pc.name, pc.parent_code, pc.level, pc.sort_order, pc.loss_threshold, pc.is_fresh,
|
|
pc.status, b.branch_code, pc.created_at, pc.updated_at
|
|
FROM product_catalog pc
|
|
CROSS JOIN branches b
|
|
WHERE pc.item_type = 'category'
|
|
AND pc.branch_code = (
|
|
SELECT branch_code FROM branches WHERE status = 1 ORDER BY branch_code LIMIT 1
|
|
)
|
|
AND b.branch_code <> pc.branch_code
|
|
AND b.status = 1
|
|
AND NOT EXISTS (
|
|
SELECT 1 FROM product_catalog x
|
|
WHERE x.item_type = 'category' AND x.branch_code = b.branch_code AND x.code = pc.code
|
|
);
|
|
|
|
ALTER TABLE product_catalog ENABLE TRIGGER trg_sync_product_catalog_to_legacy;
|
|
|
|
COMMENT ON COLUMN categories.branch_code IS '所属分公司;分类按分公司隔离';
|
|
COMMENT ON COLUMN product_catalog.branch_code IS '分类行必填;商品行为空表示全平台共享';
|