feat(客户): 绑定建客户用主系统分公司真名;已有客户时订单导入不再新建

- main_supplier_bindings 新增 main_branch_name(迁移 20261021)
- 绑定推送/回读接受 main_branch_name / branch_name,建客户直接用真名(缺省仍兜底)
- 重新绑定把停用(40)客户恢复启用(10)
- 测试钉住:ensureImportCustomer 先查后插 + ON CONFLICT DO NOTHING(已有客户不重复建)
This commit is contained in:
Ubuntu
2026-10-18 21:03:49 +08:00
parent 9e58332cc6
commit 5cfd722630
5 changed files with 44 additions and 6 deletions
@@ -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_<分公司码> 客户时用作客户名';
+8 -4
View File
@@ -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<UpsertBi
const accessCode = String(input.accessCode || '').trim().toUpperCase();
const mainBranchCode = String(input.mainBranchCode || '').trim().toUpperCase() || '*';
const mainSupplierCode = String(input.mainSupplierCode || '').trim().toUpperCase();
const mainBranchName = String(input.mainBranchName || '').trim() || null;
const source = input.source || 'main_push';
if (!branchCode || !accessCode || !mainSupplierCode) {
throw new Error('upsertBinding:branch_code / access_code / main_supplier_code 均不能为空');
@@ -340,24 +343,25 @@ export async function upsertBinding(input: UpsertBindingInput): Promise<UpsertBi
await pool.query(
`INSERT INTO public.main_supplier_bindings
(branch_code, access_code, main_branch_code, main_supplier_code, status, source, bound_at, unbound_at, note)
VALUES ($1, $2, $3, $4, 'active', $5, NOW(), NULL, $6)
(branch_code, access_code, main_branch_code, main_supplier_code, main_branch_name, status, source, bound_at, unbound_at, note)
VALUES ($1, $2, $3, $4, $7, 'active', $5, NOW(), NULL, $6)
ON CONFLICT (main_branch_code, main_supplier_code) DO UPDATE
SET branch_code = EXCLUDED.branch_code,
access_code = EXCLUDED.access_code,
main_branch_name = COALESCE(EXCLUDED.main_branch_name, public.main_supplier_bindings.main_branch_name),
status = 'active',
source = EXCLUDED.source,
bound_at = NOW(),
unbound_at = NULL,
note = EXCLUDED.note`,
[branchCode, accessCode, mainBranchCode, mainSupplierCode, source, input.note ?? null]
[branchCode, accessCode, mainBranchCode, mainSupplierCode, source, input.note ?? null, mainBranchName]
);
// 绑定即建客户(2026-10-18 用户要求):不再等订单导入才创建,报价单选客户立刻能看到。
const previousCustomerSync = previousBranchCode
? await removeCustomerForBinding({ branchCode: previousBranchCode, mainBranchCode })
: undefined;
const customerSync = await ensureCustomerForBinding({ branchCode, mainBranchCode });
const customerSync = await ensureCustomerForBinding({ branchCode, mainBranchCode, mainBranchName });
return { branchCode, previousBranchCode, customerSync, previousCustomerSync };
}
+8
View File
@@ -36,6 +36,8 @@ export type ParsedBindingChange = {
mainBranchCode: string | null;
/** 主系统供应商编码(兼容字段名 `supplier_code`) */
mainSupplierCode: string;
/** 主系统分公司名称(如「自贡」);对方没给就是 null */
mainBranchName: string | null;
action: BindingChangeAction;
};
@@ -63,10 +65,13 @@ export function parseBindingChange(raw: unknown): ParsedBindingChange | null {
text(item.branch_code) ||
text(item.main_system_branch_code) ||
null;
const mainBranchName =
text(item.main_branch_name) || text(item.branch_name) || text(item.mainBranchName) || null;
return {
accessCode,
mainBranchCode,
mainSupplierCode,
mainBranchName,
action: normalizeAction(item.action ?? item.change ?? item.operation),
};
}
@@ -141,6 +146,7 @@ export async function applyBindingChange(
accessCode,
mainBranchCode,
mainSupplierCode,
mainBranchName: change.mainBranchName,
source,
note: `主系统绑定同步(${source})`,
});
@@ -297,6 +303,8 @@ export async function applyBindingSnapshot(payload: unknown): Promise<BindingSyn
accessCode,
mainBranchCode,
mainSupplierCode,
mainBranchName:
text(raw.main_branch_name) || text(raw.branch_name) || text(raw.mainBranchName) || null,
action: active ? 'bind' : 'unbind',
},
'main_pull'
+2 -2
View File
@@ -69,11 +69,11 @@ describe('事件名与载荷解析(容错但不含糊)', () => {
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('缺编码或缺供应商标记为不可解析(整条丢掉,不猜)', () => {
@@ -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');
});
});