来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
164 lines
5.1 KiB
SQL
164 lines
5.1 KiB
SQL
-- 分类编码强制分公司前缀:{branch_code}_{old_code}
|
|
-- 同步回写同分公司引用;跳过已带正确前缀的行。
|
|
|
|
ALTER TABLE product_catalog DISABLE TRIGGER trg_sync_product_catalog_to_legacy;
|
|
|
|
-- 前缀后长度增大:统一抬高到 100
|
|
ALTER TABLE product_catalog ALTER COLUMN code TYPE VARCHAR(100);
|
|
ALTER TABLE product_catalog ALTER COLUMN parent_code TYPE VARCHAR(100);
|
|
ALTER TABLE categories ALTER COLUMN category_code TYPE VARCHAR(100);
|
|
ALTER TABLE categories ALTER COLUMN parent_code TYPE VARCHAR(100);
|
|
ALTER TABLE skus ALTER COLUMN category_code TYPE VARCHAR(100);
|
|
ALTER TABLE customer_skus ALTER COLUMN category_code TYPE VARCHAR(100);
|
|
ALTER TABLE order_items ALTER COLUMN category_code TYPE VARCHAR(100);
|
|
ALTER TABLE smart_classification_rules ALTER COLUMN category_code TYPE VARCHAR(100);
|
|
ALTER TABLE supplier_categories ALTER COLUMN category_code TYPE VARCHAR(100);
|
|
ALTER TABLE supplier_preparations ALTER COLUMN category_code TYPE VARCHAR(100);
|
|
ALTER TABLE market_prices ALTER COLUMN category_code TYPE VARCHAR(100);
|
|
ALTER TABLE market_price_indices ALTER COLUMN category_code TYPE VARCHAR(100);
|
|
|
|
CREATE TEMP TABLE tmp_category_code_rename (
|
|
branch_code VARCHAR(50) NOT NULL,
|
|
old_code VARCHAR(100) NOT NULL,
|
|
new_code VARCHAR(100) NOT NULL,
|
|
PRIMARY KEY (branch_code, old_code)
|
|
);
|
|
|
|
INSERT INTO tmp_category_code_rename (branch_code, old_code, new_code)
|
|
SELECT
|
|
branch_code,
|
|
code AS old_code,
|
|
branch_code || '_' || code AS new_code
|
|
FROM product_catalog
|
|
WHERE item_type = 'category'
|
|
AND branch_code IS NOT NULL
|
|
AND TRIM(branch_code) <> ''
|
|
AND code IS NOT NULL
|
|
AND TRIM(code) <> ''
|
|
AND code NOT ILIKE branch_code || '\_%' ESCAPE '\'
|
|
ON CONFLICT DO NOTHING;
|
|
|
|
-- 若目标码已存在则放弃该条,避免唯一冲突
|
|
DELETE FROM tmp_category_code_rename r
|
|
WHERE EXISTS (
|
|
SELECT 1
|
|
FROM product_catalog pc
|
|
WHERE pc.item_type = 'category'
|
|
AND pc.branch_code = r.branch_code
|
|
AND pc.code = r.new_code
|
|
);
|
|
|
|
-- 1) product_catalog.parent_code(商品与二级分类)
|
|
UPDATE product_catalog pc
|
|
SET parent_code = r.new_code,
|
|
updated_at = NOW()
|
|
FROM tmp_category_code_rename r
|
|
WHERE pc.branch_code = r.branch_code
|
|
AND pc.parent_code = r.old_code;
|
|
|
|
-- 2) categories.parent_code
|
|
UPDATE categories c
|
|
SET parent_code = r.new_code
|
|
FROM tmp_category_code_rename r
|
|
WHERE c.branch_code = r.branch_code
|
|
AND c.parent_code = r.old_code;
|
|
|
|
-- 3) skus / customer_skus
|
|
UPDATE skus s
|
|
SET category_code = r.new_code
|
|
FROM tmp_category_code_rename r
|
|
WHERE s.branch_code = r.branch_code
|
|
AND s.category_code = r.old_code;
|
|
|
|
UPDATE customer_skus cs
|
|
SET category_code = r.new_code
|
|
FROM tmp_category_code_rename r
|
|
WHERE cs.branch_code = r.branch_code
|
|
AND cs.category_code = r.old_code;
|
|
|
|
-- 4) order_items(经订单分公司)
|
|
UPDATE order_items oi
|
|
SET category_code = r.new_code
|
|
FROM orders o
|
|
JOIN tmp_category_code_rename r ON r.branch_code = o.branch_code
|
|
WHERE oi.order_code = o.order_code
|
|
AND oi.category_code = r.old_code;
|
|
|
|
-- 5) 智能分类规则
|
|
UPDATE smart_classification_rules scr
|
|
SET category_code = r.new_code
|
|
FROM tmp_category_code_rename r
|
|
WHERE scr.branch_code = r.branch_code
|
|
AND scr.category_code = r.old_code;
|
|
|
|
-- 6) 供应商分类绑定(经供应商分公司);先删会冲突的目标行
|
|
DELETE FROM supplier_categories sc
|
|
USING suppliers s
|
|
JOIN tmp_category_code_rename r ON r.branch_code = s.branch_code
|
|
WHERE sc.supplier_code = s.supplier_code
|
|
AND sc.category_code = r.new_code
|
|
AND EXISTS (
|
|
SELECT 1
|
|
FROM supplier_categories sc2
|
|
WHERE sc2.supplier_code = sc.supplier_code
|
|
AND sc2.category_code = r.old_code
|
|
);
|
|
|
|
UPDATE supplier_categories sc
|
|
SET category_code = r.new_code
|
|
FROM suppliers s
|
|
JOIN tmp_category_code_rename r ON r.branch_code = s.branch_code
|
|
WHERE sc.supplier_code = s.supplier_code
|
|
AND sc.category_code = r.old_code;
|
|
|
|
-- 7) 备货 / 行情
|
|
UPDATE supplier_preparations sp
|
|
SET category_code = r.new_code
|
|
FROM tmp_category_code_rename r
|
|
WHERE sp.branch_code = r.branch_code
|
|
AND sp.category_code = r.old_code;
|
|
|
|
UPDATE market_prices mp
|
|
SET category_code = r.new_code
|
|
FROM tmp_category_code_rename r
|
|
WHERE mp.branch_code = r.branch_code
|
|
AND mp.category_code = r.old_code;
|
|
|
|
DELETE FROM market_price_indices mpi
|
|
USING tmp_category_code_rename r
|
|
WHERE mpi.branch_code = r.branch_code
|
|
AND mpi.category_code = r.new_code
|
|
AND EXISTS (
|
|
SELECT 1
|
|
FROM market_price_indices x
|
|
WHERE x.branch_code = r.branch_code
|
|
AND x.category_code = r.old_code
|
|
AND x.index_date = mpi.index_date
|
|
);
|
|
|
|
UPDATE market_price_indices mpi
|
|
SET category_code = r.new_code
|
|
FROM tmp_category_code_rename r
|
|
WHERE mpi.branch_code = r.branch_code
|
|
AND mpi.category_code = r.old_code;
|
|
|
|
-- 8) 主表改码:categories
|
|
UPDATE categories c
|
|
SET category_code = r.new_code
|
|
FROM tmp_category_code_rename r
|
|
WHERE c.branch_code = r.branch_code
|
|
AND c.category_code = r.old_code;
|
|
|
|
-- 9) 主表改码:product_catalog
|
|
UPDATE product_catalog pc
|
|
SET code = r.new_code,
|
|
updated_at = NOW()
|
|
FROM tmp_category_code_rename r
|
|
WHERE pc.item_type = 'category'
|
|
AND pc.branch_code = r.branch_code
|
|
AND pc.code = r.old_code;
|
|
|
|
DROP TABLE IF EXISTS tmp_category_code_rename;
|
|
|
|
ALTER TABLE product_catalog ENABLE TRIGGER trg_sync_product_catalog_to_legacy;
|