用户要求:供应商编码被某配送商分公司绑定后,该分公司客户应立刻出现在客户列表,不等派单; 解绑后消失。 - 根因:MAIN_<主系统分公司> 客户此前只在订单导入时创建;且 /api/data 客户缓存未失效 → 时有时无 - 新增 mainSystemCustomerBindingService:绑定建客户(status 10、缓存失效)、解绑删除; 有历史订单删不掉 ⇒ 停用 status=40(保住历史归属);重新绑定恢复启用 - 接入 upsertBinding / unbindBinding(含改绑时清理旧分公司客户);通配 * 跳过
104 lines
4.6 KiB
TypeScript
104 lines
4.6 KiB
TypeScript
import { runWithTenantDbContext } from './tenantDbContext';
|
|
import { ensureMainBranchCustomerForBinding } from './mainSystemOrderImport';
|
|
import { deleteCustomer } from './customerDeleteService';
|
|
import { invalidateQueryCacheForTable } from './queryCacheService';
|
|
|
|
/**
|
|
* 绑定即建客户 / 解绑即删客户(2026-10-18 用户要求):
|
|
*
|
|
* 用户原话:「供应商的编码被哪一个配送商分公司绑定了,那个配送商分公司应该自动出现在供应商的
|
|
* 客户列表里面,而不是等着派单过来才出现;解绑了就消失。当前出现报价单选客户找不到绑定了编码的
|
|
* 配送商分公司客户,有时出现有时消失。」
|
|
*
|
|
* 根因:`MAIN_<主系统分公司>` 客户此前**只在订单导入时**才由 `mainSystemOrderImport.ensureImportCustomer`
|
|
* 创建;没派过单的分公司永远没有客户 ⇒ 报价单选客户时看不到,且历史占位客户被停用后更不一致。
|
|
*
|
|
* 现在:绑定(`upsertBinding` / 手工登记)**当场创建**该客户(status=10 正常、source='main_system'),
|
|
* 解绑当场删除(有历史订单时删不掉 ⇒ 退化为停用 status=40,绝不删掉有订单归属的客户)。
|
|
*
|
|
* ⚠️ 通配绑定 `main_branch_code='*'` 无法对应具体分公司,跳过(与历史占位客户行为一致)。
|
|
*/
|
|
|
|
export type BindingCustomerSyncResult = {
|
|
ok: boolean;
|
|
action: 'created_or_kept' | 'deleted' | 'disabled' | 'skipped';
|
|
customer_code?: string | null;
|
|
reason?: string | null;
|
|
};
|
|
|
|
export function mainBranchCustomerCode(mainBranchCode: string | null | undefined): string | null {
|
|
const code = String(mainBranchCode || '').trim().toUpperCase();
|
|
if (!code || code === '*') return null;
|
|
return `MAIN_${code}`;
|
|
}
|
|
|
|
/** 绑定生效:确保该 (我方分公司, 主系统分公司) 的客户已存在且为启用状态 */
|
|
export async function ensureCustomerForBinding(params: {
|
|
branchCode: string;
|
|
mainBranchCode: string | null | undefined;
|
|
mainBranchName?: string | null;
|
|
}): Promise<BindingCustomerSyncResult> {
|
|
const customerCode = mainBranchCustomerCode(params.mainBranchCode);
|
|
if (!customerCode) return { ok: true, action: 'skipped', reason: 'wildcard_or_empty_main_branch' };
|
|
try {
|
|
await runWithTenantDbContext(params.branchCode, () =>
|
|
ensureMainBranchCustomerForBinding({
|
|
branchCode: params.branchCode,
|
|
mainBranchCode: String(params.mainBranchCode || '').trim().toUpperCase(),
|
|
mainBranchName: params.mainBranchName ?? null,
|
|
})
|
|
);
|
|
// 前端报价单选客户走的是 /api/data 查询缓存 ⇒ 必须失效,否则「有时看得到有时看不到」
|
|
await invalidateQueryCacheForTable('customers');
|
|
return { ok: true, action: 'created_or_kept', customer_code: customerCode };
|
|
} catch (error: any) {
|
|
return {
|
|
ok: false,
|
|
action: 'created_or_kept',
|
|
customer_code: customerCode,
|
|
reason: error?.message || '建客户失败',
|
|
};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 解绑:删除该客户;**有历史订单时删不掉**(客户删除守卫:有订单即拒绝)⇒ 退化为停用。
|
|
* 停用而不是硬删,保证历史订单的客户归属线索不丢。
|
|
*/
|
|
export async function removeCustomerForBinding(params: {
|
|
branchCode: string;
|
|
mainBranchCode: string | null | undefined;
|
|
}): Promise<BindingCustomerSyncResult> {
|
|
const customerCode = mainBranchCustomerCode(params.mainBranchCode);
|
|
if (!customerCode) return { ok: true, action: 'skipped', reason: 'wildcard_or_empty_main_branch' };
|
|
try {
|
|
await runWithTenantDbContext(params.branchCode, () => deleteCustomer(customerCode));
|
|
await invalidateQueryCacheForTable('customers');
|
|
return { ok: true, action: 'deleted', customer_code: customerCode };
|
|
} catch (error: any) {
|
|
const status = Number(error?.status) || 0;
|
|
if (status === 404) return { ok: true, action: 'deleted', customer_code: customerCode };
|
|
// 有历史订单 / 有报价单引用:只能停用
|
|
try {
|
|
await runWithTenantDbContext(params.branchCode, async () => {
|
|
const { pool } = await import('../db');
|
|
await pool.query(`UPDATE customers SET status = 40 WHERE customer_code = $1`, [customerCode]);
|
|
});
|
|
await invalidateQueryCacheForTable('customers');
|
|
return {
|
|
ok: true,
|
|
action: 'disabled',
|
|
customer_code: customerCode,
|
|
reason: error?.message || '客户仍有引用,已停用',
|
|
};
|
|
} catch (disableError: any) {
|
|
return {
|
|
ok: false,
|
|
action: 'disabled',
|
|
customer_code: customerCode,
|
|
reason: disableError?.message || error?.message || '删除/停用客户失败',
|
|
};
|
|
}
|
|
}
|
|
}
|