From 5cfd722630809bdd611debbbd04510995907bf40 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Sun, 18 Oct 2026 21:03:49 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E5=AE=A2=E6=88=B7):=20=E7=BB=91=E5=AE=9A?= =?UTF-8?q?=E5=BB=BA=E5=AE=A2=E6=88=B7=E7=94=A8=E4=B8=BB=E7=B3=BB=E7=BB=9F?= =?UTF-8?q?=E5=88=86=E5=85=AC=E5=8F=B8=E7=9C=9F=E5=90=8D=EF=BC=9B=E5=B7=B2?= =?UTF-8?q?=E6=9C=89=E5=AE=A2=E6=88=B7=E6=97=B6=E8=AE=A2=E5=8D=95=E5=AF=BC?= =?UTF-8?q?=E5=85=A5=E4=B8=8D=E5=86=8D=E6=96=B0=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - main_supplier_bindings 新增 main_branch_name(迁移 20261021) - 绑定推送/回读接受 main_branch_name / branch_name,建客户直接用真名(缺省仍兜底) - 重新绑定把停用(40)客户恢复启用(10) - 测试钉住:ensureImportCustomer 先查后插 + ON CONFLICT DO NOTHING(已有客户不重复建) --- ...61021_main_supplier_bindings_branch_name.sql | 9 +++++++++ server/src/services/branchAccessCode.ts | 12 ++++++++---- server/src/services/branchBindingSync.ts | 8 ++++++++ server/tests/branchBindingSync.test.ts | 4 ++-- server/tests/mainSystemBranchCustomer.test.ts | 17 +++++++++++++++++ 5 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 migrations/20261021_main_supplier_bindings_branch_name.sql diff --git a/migrations/20261021_main_supplier_bindings_branch_name.sql b/migrations/20261021_main_supplier_bindings_branch_name.sql new file mode 100644 index 0000000..4ba3595 --- /dev/null +++ b/migrations/20261021_main_supplier_bindings_branch_name.sql @@ -0,0 +1,9 @@ +-- 绑定表补「主系统分公司名称」(2026-10-18 用户要求:建客户用真名,而不是「主系统分公司 BR002」) +-- +-- 来源:主系统绑定推送/回读里的 `main_branch_name`(缺省 null ⇒ 建客户时退回兜底名)。 +-- 可重放:ADD COLUMN IF NOT EXISTS。 +ALTER TABLE public.main_supplier_bindings + ADD COLUMN IF NOT EXISTS main_branch_name VARCHAR(100); + +COMMENT ON COLUMN public.main_supplier_bindings.main_branch_name IS + '主系统分公司名称(如「自贡」);建 MAIN_<分公司码> 客户时用作客户名'; diff --git a/server/src/services/branchAccessCode.ts b/server/src/services/branchAccessCode.ts index 36d213e..c4ead0f 100644 --- a/server/src/services/branchAccessCode.ts +++ b/server/src/services/branchAccessCode.ts @@ -288,6 +288,8 @@ export type UpsertBindingInput = { accessCode: string; mainBranchCode: string; mainSupplierCode: string; + /** 主系统分公司名称(如「自贡」):绑定即建客户时用作客户名 */ + mainBranchName?: string | null; source?: 'main_push' | 'main_pull' | 'manual' | 'backfill'; note?: string | null; }; @@ -313,6 +315,7 @@ export async function upsertBinding(input: UpsertBindingInput): Promise { supplier_code: 'sup1', action: 'bind', }) - ).toEqual({ accessCode: 'sc7k3q9m2xtp', mainBranchCode: 'br002', mainSupplierCode: 'sup1', action: 'bind' }); + ).toEqual({ accessCode: 'sc7k3q9m2xtp', mainBranchCode: 'br002', mainSupplierCode: 'sup1', mainBranchName: null, action: 'bind' }); expect( parseBindingChange({ access_code: 'SC1', main_branch_code: 'BR2', main_supplier_code: 'SUP3' }) - ).toEqual({ accessCode: 'SC1', mainBranchCode: 'BR2', mainSupplierCode: 'SUP3', action: 'bind' }); + ).toEqual({ accessCode: 'SC1', mainBranchCode: 'BR2', mainSupplierCode: 'SUP3', mainBranchName: null, action: 'bind' }); }); it('缺编码或缺供应商标记为不可解析(整条丢掉,不猜)', () => { diff --git a/server/tests/mainSystemBranchCustomer.test.ts b/server/tests/mainSystemBranchCustomer.test.ts index a6abdcb..94fc60c 100644 --- a/server/tests/mainSystemBranchCustomer.test.ts +++ b/server/tests/mainSystemBranchCustomer.test.ts @@ -82,4 +82,21 @@ describe('存量归属改挂(决策 ③)', () => { expect(fn).toContain('SELECT count(*)::text AS count FROM orders WHERE customer_code = $1'); expect(script).toContain('reattributeMainSystemOrdersToBranchCustomers'); }); + + it('已有客户时不再新建:订单导入按 customer_code 命中即更新,INSERT 走 ON CONFLICT DO NOTHING', () => { + const start = importSrc.indexOf('async function ensureImportCustomer'); + const fn = importSrc.slice(start, start + 3200); + expect(fn).toContain('SELECT status, source, company_name FROM customers WHERE customer_code = $1'); + expect(fn).toContain('INSERT INTO customers (customer_code, company_name, branch_code, status, source)'); + expect(fn).toContain('ON CONFLICT (customer_code) DO NOTHING'); + // 先查后插:已有客户只更新、不新建 + expect(fn.indexOf('SELECT status, source, company_name')).toBeLessThan(fn.indexOf('INSERT INTO customers')); + }); + + it('绑定即建客户:重新绑定时把停用(40)恢复为启用(10)', () => { + const start = importSrc.indexOf('export async function ensureMainBranchCustomerForBinding'); + const fn = importSrc.slice(start, start + 1200); + expect(fn).toContain('UPDATE customers SET status = 10 WHERE customer_code = $1 AND status = 40'); + expect(fn).toContain('ensureImportCustomer(params.branchCode'); + }); });