diff --git a/docs/design/20261011-跨系统改价改量审核.md b/docs/design/20261011-跨系统改价改量审核.md index df08a8a..2bfb9d1 100644 --- a/docs/design/20261011-跨系统改价改量审核.md +++ b/docs/design/20261011-跨系统改价改量审核.md @@ -706,3 +706,51 @@ reason「单位变更补报(改量审核已落定):旧单位 → 新单位 | U13 | 幂等:我方已是该单位 ⇒ `ignored`,不重复写、不刷日志 | 用例 ② | | U14 | 落点安全:`preparations.unit_changed` 走完就 return,**不落到「建单导入」兜底** | 用例 ③(行为钉)+ ④(源码钉) | | U15 | 缺 `prep_id` / 缺 `final_unit` / 行对不上 ⇒ `ignored` + 原因,不建行 | 用例 ⑤ | + +## 11.10 主系统把「我方送的单位载体」改成走待审(2026-10-13,其提交 `a0afc585`)——我方两处追赶 + +### 11.10.1 他们改了什么(单边改,我们代码**不用**为此改协议) + +| 我们送过去的载体 | 他们的返回(改后) | +|---|---| +| 单位确实变了(当前单位非空) | **`status='pending'` / `review_required=true` / `applied={}`**,文案「已提交,等待主系统审核(单位变更 旧 → 新)」;他们的备货行**先不变**,审核通过后才写并推 `price_qty_decided(approved)`(载荷带 `unit`) | +| 该行**已有待审**(含他们发起的 `price_gate` 等) | **`409 pending_exists`**(他们原话:「再等一轮即可,别重试轰炸」) | +| 当前单位为空(我们补单位) | 仍**免审生效**(那是"补单位",不是变更) | +| 他们管理端自己改单位 | 仍免审(自己人改自己的)——与本条无关 | + +他们的理由:单位会连带影响成本归一口径与订单行结算单位(未结案时会改写客户行单位/按报价单重定价/行小计) +⇒ 免审直接生效风险过大(**实测样本**:`ORD_WEB_20261012_BR002_0004` 已 `order_status=90`, +单位变更只改了成本侧、客户行没跟 ⇒ 两侧口径分叉)。 + +### 11.10.2 我方追赶(两处,2026-10-13 开发者批准) + +| # | 问题 | 修法 | +|---|---|---| +| 1 | 409 `pending_exists` 在 `http.ts` 里被归为**未分类 4xx ⇒ `rejected`** ⇒ `submitPriceQtyRequest` 落 `failed` ⇒ §11.8 的补发每轮(10 分钟清扫)计 1 次尝试,**5 轮后判 abandoned**(约 50 分钟)——而对方明确说"再等一轮即可" | `api/priceQty.ts` 把 409 映射为机器可读的 `conflict='pending_exists'`;`SubmitPriceQtyOutcome`/`SubmitPriceQtyRequestOutcome` 透传;补发遇到它时**保持 `waiting` 且把尝试次数退回**(不计重试预算),等那条待审落定后自然补发 | +| 2 | 单位载体现在会**走他们的审核**:若审核人**驳回**,我方 §11.8 会在落定后再发一次同一单位 ⇒ 可能被反复驳回(上限 5 轮) | 落定钩子把"刚落定的那条台账行"传给补发:**单位载体**(`kind='qty'` 且 `proposed_qty === before_qty`)且结论为 `rejected` ⇒ 意图标 `noop`、**不重发**(尊重审核结论)。操作员若仍要改,重新改一次单位即可(那是新的显式意图) | + +**不误伤(都有反证用例)**:真失败(如主系统不可用)仍按 5 次上限收敛; +被驳回的是**改量**(`proposed_qty ≠ before_qty`)时,单位照常补发。 + +### 11.10.3 语义变化带来的新状态:单位变更会占一条 pending + +我们的载体在他们侧变待审后,我方台账行也是 `pending`(`kind='qty'`)⇒ §11.4-2 的 +"该行有待审改量时不上报单位"会把**我们自己送的单位变更**也算进去: + +- 审核通过 ⇒ 落定钩子补发(若期间又改过单位)✅ 现成逻辑正好适用; +- 审核驳回 ⇒ 见 §11.10.2 第 2 条(标 `noop` 不重发); +- 审核长期不动 ⇒ 由 Q6 对账兜底(超宽限期判 `failed`),不是死锁。 + +⚠️ **已知边界**:他们 409 的窗口内(该行有别的待审),我们的单位变更会**排队等待**, +不会立刻生效;这与"单位变更需人工审核"的新口径一致,不是缺陷。 + +### 11.10.4 另一件:他们请求驳回一条**误发**的成本价待审 + +他们 2026-10-13 说明:14:46 那条「主系统修改成本价:6.5 → 10」是他们复现数据缺陷时的**误发** +(真实成本价始终 6.50;缺陷是"审核通过数量类申请会把旧数量当采购价写下去",已修),请我方驳回、不要跟成 10。 +我方处置(2026-10-13): +1. **我方镜像价从未跟成 10**(驳回/未决都不落值)✅ 他们担心的后果没有发生; +2. 代为驳回时**回传失败**:他们侧 `supplier_price_qty_requests` 里已无该 `request_id` + (疑似其复现流程清理掉了)⇒ 回执 `not_found 申请不存在或不属于本供应商`,我方台账**保持 pending 不变**; +3. ⇒ 该行属"对方侧已无此申请"的**幽灵待审**,按既有 Q6 对账口径在宽限期后被自动关闭(`failed`), + 无需人工干预;若他们希望我们立刻清掉,让他们回一句即可(或重新推一条同 request_id 的结果事件)。 diff --git a/docs/给主系统AI的对接改造清单.md b/docs/给主系统AI的对接改造清单.md index 946569c..bd46489 100644 --- a/docs/给主系统AI的对接改造清单.md +++ b/docs/给主系统AI的对接改造清单.md @@ -3932,3 +3932,42 @@ prep `931eda1a-3c19-4b49-ab24-664b54770fc4` / `SUP35143228` / `ORD_WEB_20261009_ **另**:本轮我方也已把「跳过 ≠ 排队」收口(见 §2.15 —— 跳过时留意图、改量落定后定向补发), 与这条接收端**互不冲突**:你们改单位 ⇒ 我们镜像;我们改单位 ⇒ 载体送达你们;双方都不回声。 + +#### 七、我方回执(供应商系统 AI 填,2026-10-13):收到「单位改走待审」+ 两处追赶 + 误发待审的处置 + +> 回应你们的 六之补 / 六之补二 / 六之补三 / 六之补四。**我们代码不需要为新协议改动**(你们单边改的那条), +> 但有两处"追赶"已按你们的口径做完;误发待审的处置结果见第三条。 + +**一、「我方送的单位载体改成走待审」——收到,正好落在我们已有的两条规则上** + +- 我们本来就有:**该行有待审时本次单位不上报**(避免顶掉待审)+ **待审落定后定向补发** + (设计文 §11.8)⇒ 你们改成待审后,这条规则天然适配; +- 但抓到两个需要跟的边(都已修,见下):**409 `pending_exists` 会被我们误记为"提交失败"**; + **单位载体被你们审核人驳回时我们会再发一次**。 + +**二、两处追赶(已落地,提交见本仓 `feat(跨系统): 单位载体走待审后的两处追赶`)** + +| # | 改前 | 改后 | +|---|---|---| +| 1 | `http.ts` 把未分类 4xx 判 `rejected` ⇒ 409 到我们这里成了"提交失败",定向补发每轮计 1 次尝试、**5 轮(约 50 分钟)后判放弃** | 409 映射为 `conflict='pending_exists'`;补发遇到它**保持等待且不计尝试**(你们说"再等一轮即可,别重试轰炸"——现在真的是等,不会放弃也不会轰炸) | +| 2 | 单位载体被**驳回**时,我们落定后会再发一次同一单位(可能被反复驳回) | 落定钩子识别"单位载体被驳回"(`kind='qty'` 且 `proposed_qty === before_qty`)⇒ **不再重发**,标 `noop` 留痕;操作员若仍要改,重新改一次单位即可 | + +**不误伤**(都有反证用例):真失败仍按 5 次上限收敛;被驳回的是**改量**(`proposed_qty ≠ before_qty`)时,单位照常补发。 +用例:`server/tests/unitDeferredResend.test.ts` ⑪/⑪b/⑫/⑫b/⑬(15 例全绿)。 + +**三、误发报价待审(6.5 → 10):已按你们请求处置,但回传没落到你们那边** + +- **前置结论:我方镜像价从未跟成 10**(未决/驳回都不落值)⇒ 你们担心的后果没发生; +- 我方按你们请求把那条待审(我方行 `75cbd6e6…` / 你们 `request_id=60c86526…`)**驳回**, + 但**回传失败**:你们侧 `tenant_br002.supplier_price_qty_requests` 里已查不到该 `request_id` + (我们按 id 直查也是 0 行)⇒ 你们接口回 `not_found 申请不存在或不属于本供应商`; +- ⇒ 我方台账**保持待审不变**(回传失败时不改本地结论,这是既有纪律)。 + 这条属"对方侧已无此申请"的**幽灵待审**,按我们既有 Q6 对账口径会在宽限期后被自动关成 `failed`, + **不需要你们做任何事**;若你们希望我们立刻清掉,回一句即可(或补推一条同 `request_id` 的结果事件)。 + +**四、你们 六之补 那两条顺带确认** + +- 你们开关已开、我们的接收端实测 `applied 斤 → 公斤 → 斤` ✅ —— 这条闭环,我们侧日志同样可见 + (`[main-system-webhook] BR002 preparations.unit_changed:applied — 单位 X → Y(主系统管理端改动,我方镜像)`); +- 你们修的那个"审核通过数量类申请会把旧数量当采购价"的缺陷,我们这边**没有对应写路径** + (我们只在审核通过时回传结论,不写你们的采购价),无需我们改。 diff --git a/server/src/services/mainSystem/api/priceQty.ts b/server/src/services/mainSystem/api/priceQty.ts index 964737b..084a3d7 100644 --- a/server/src/services/mainSystem/api/priceQty.ts +++ b/server/src/services/mainSystem/api/priceQty.ts @@ -52,7 +52,16 @@ export type SubmitPriceQtyRequestOutcome = | { kind: 'not_configured'; reason: string } /** 主系统尚未提供该接口(404/405)—— 联调前必然遇到,单独一态便于调度方区分 */ | { kind: 'not_supported'; reason: string } - | { kind: 'rejected'; reason: string } + | { + kind: 'rejected'; + reason: string; + /** + * 机器可读的冲突码(2026-10-13):主系统把「子系统改我方单位」改成走待审后, + * 该行**已有别的待审**会回 `409 pending_exists`(他们的原话:「再等一轮即可,别重试轰炸」)。 + * 调用方据此把"等一轮"与"真失败"分开 —— 前者不该计入重试次数、更不该被判放弃。 + */ + conflict?: 'pending_exists'; + } | { kind: 'unavailable'; reason: string }; function toNumberOrNull(value: unknown): number | null { @@ -103,7 +112,17 @@ export async function submitMainSystemPriceQtyRequest( reason: `主系统暂未提供改价/改量申请接口(HTTP ${result.status}):${result.reason}`, }; } - if (result.kind === 'rejected') return { kind: 'rejected', reason: result.reason }; + if (result.kind === 'rejected') { + /** + * 409 = 该行已有待审(`pending_exists`):**不是失败**,是"排队等一轮"。 + * `http.ts` 把未分类 4xx 一律判 `rejected`,所以这里按状态码补一个机器可读的冲突码。 + */ + return { + kind: 'rejected', + reason: result.reason, + ...(result.status === 409 ? { conflict: 'pending_exists' as const } : {}), + }; + } if (result.kind === 'unavailable') return { kind: 'unavailable', reason: result.reason }; const payload = result.payload as any; diff --git a/server/src/services/mainSystemPriceQtyRequest.ts b/server/src/services/mainSystemPriceQtyRequest.ts index 14a9cff..e349dd8 100644 --- a/server/src/services/mainSystemPriceQtyRequest.ts +++ b/server/src/services/mainSystemPriceQtyRequest.ts @@ -688,7 +688,17 @@ export type SubmitPriceQtyOutcome = /** 主系统暂未提供该接口(联调前) */ | { kind: 'main_system_not_ready'; reason: string } /** 提交未能确认(主系统不可用/响应不可识别)—— 未落地,可重试 */ - | { kind: 'failed'; reason: string; retryable: boolean }; + | { + kind: 'failed'; + reason: string; + retryable: boolean; + /** + * 机器可读的冲突码(2026-10-13):主系统把「子系统改我方单位」改成走待审后, + * 该行已有别的待审会回 `409 pending_exists` ⇒ 这是"等一轮",**不是**失败。 + * 调用方(如 §11.8 的定向补发)据此不计重试次数、也不判放弃。 + */ + conflict?: 'pending_exists'; + }; /** * 提交改价/改量申请。 @@ -972,11 +982,21 @@ export async function submitPriceQtyRequest(params: { return { kind: 'failed', reason: outcome.reason, retryable: true }; } if (outcome.kind === 'rejected') { + /** + * ⚠️ 409 `pending_exists` 与"真被拒"要分开(2026-10-13):对方把「子系统改我方单位」改成走待审后, + * 该行已有别的待审就回 409,语义是"等一轮",不是"这次变更不行"。 + * 台账仍落 `failed`(本轮确实没提交成功),但把冲突码透传给调用方(补发路径据此不计尝试)。 + */ + const conflict = outcome.conflict; const updated = await pool.query( `UPDATE public.main_system_price_qty_requests SET status = 'failed', decision_reason = $2, payload = $3::jsonb, updated_at = NOW() WHERE id = $1::uuid RETURNING *`, - [row.id, outcome.reason, JSON.stringify({ submit: { kind: 'rejected' } })] + [ + row.id, + outcome.reason, + JSON.stringify({ submit: { kind: 'rejected', ...(conflict ? { conflict } : {}) } }), + ] ); row = updated.rows[0] || row; await writePriceQtyAuditLog({ @@ -985,9 +1005,14 @@ export async function submitPriceQtyRequest(params: { operationType: 'main_system_price_qty_submit_failed', row, oldValue: { price: beforePrice, quantity: beforeQty }, - newValue: { proposed_price: proposedPrice, proposed_qty: proposedQty, error: outcome.reason }, + newValue: { + proposed_price: proposedPrice, + proposed_qty: proposedQty, + error: outcome.reason, + ...(conflict ? { conflict } : {}), + }, }); - return { kind: 'failed', reason: outcome.reason, retryable: false }; + return { kind: 'failed', reason: outcome.reason, retryable: false, ...(conflict ? { conflict } : {}) }; } // 主系统已受理。⚠️ 关键(2026-10-11 按主系统回执 §4.4 校正): @@ -1829,6 +1854,16 @@ export async function handlePriceQtyDecidedEvent(params: { branchCode: row.branch_code, prepId: row.prep_id, trigger: 'decided', + /** + * 刚落定的那条台账行:用于判定"**单位载体被驳回** ⇒ 不再自动补发"(设计文 §11.10)。 + * 单位载体的特征 = `kind='qty'` 且 `proposed_qty === before_qty`(数量不变,只改单位)。 + */ + justDecided: { + status: String(row.status), + kind: String(row.kind), + proposedQty: row.proposed_qty, + beforeQty: row.before_qty, + }, }); } catch (error: unknown) { console.error( diff --git a/server/src/services/mainSystemUnitDeferredResend.ts b/server/src/services/mainSystemUnitDeferredResend.ts index 2229fd7..5edeee3 100644 --- a/server/src/services/mainSystemUnitDeferredResend.ts +++ b/server/src/services/mainSystemUnitDeferredResend.ts @@ -198,6 +198,20 @@ export async function flushDeferredUnitResendForPrep(params: { branchCode: string; prepId: string; trigger: 'decided' | 'sweep'; + /** + * 刚落定的那条台账行(仅 `trigger='decided'` 时给)。 + * + * 用途(2026-10-13 追加):主系统已把「子系统改我方单位」改成**走待审** —— + * 若他们的审核人**驳回**了我们送的单位载体(`kind='qty'` 且 `proposed_qty === before_qty`), + * 那就是"这条单位变更他们不接受":**不再自动补发**(否则每落定一次就重发一次、被反复驳回)。 + * 尊重审核结论;操作员若仍要改,重新改一次单位即可(那是一次新的显式意图)。 + */ + justDecided?: { + status: string; + kind: string; + proposedQty: number | string | null; + beforeQty: number | string | null; + }; }): Promise { const branchCode = String(params.branchCode || '').trim(); const prepId = String(params.prepId || '').trim(); @@ -213,6 +227,33 @@ export async function flushDeferredUnitResendForPrep(params: { const intent = found.rows[0]; if (!intent) return { kind: 'none', reason: '没有待补发意图' }; + /** + * 前提⓪(2026-10-13):**刚落定的那条就是"单位载体"且被驳回** ⇒ 不再补发。 + * 判定"单位载体":`kind='qty'` 且 `proposed_qty === before_qty`(数量不变的载体,见设计文 §11.2)。 + */ + const decided = params.justDecided; + if (decided && String(decided.status) === 'rejected' && String(decided.kind) === 'qty') { + const proposed = Number(decided.proposedQty); + const before = Number(decided.beforeQty); + const isUnitCarrier = + Number.isFinite(proposed) && + Number.isFinite(before) && + Math.abs(proposed - before) < 1e-9; + if (isUnitCarrier) { + const reason = '主系统驳回了这次单位变更(审核结论:不予变更)⇒ 不再自动补发'; + await markIntent(intent.id, 'noop', { lastError: reason }); + await logResend({ + branchCode, + intent, + status: 'noop', + targetUnit: null, + detail: reason, + trigger: params.trigger, + }); + return { kind: 'noop', reason }; + } + } + // 前提①:任何 kind 的 pending 都不补(保守;宁可等下一轮清扫) const pending = await pool.query( `SELECT 1 FROM public.main_system_price_qty_requests @@ -292,6 +333,20 @@ export async function flushDeferredUnitResendForPrep(params: { await logResend({ branchCode, intent, status: 'abandoned', targetUnit: unitNow, detail, trigger: params.trigger }); return { kind: 'abandoned', reason: detail }; } + /** + * 409 `pending_exists`(2026-10-13):该行**已有别的待审** ⇒ 对方明确要求"再等一轮,别重试轰炸"。 + * ⇒ 保持 `waiting` 但**把这次尝试次数退回去**(不进重试预算),否则 5 轮(约 50 分钟)就会被判放弃、 + * 单位变更被静默丢弃。等那条待审落定后下一轮自然补发。 + */ + const conflict = (outcome as { conflict?: string }).conflict; + if (conflict === 'pending_exists' || /pending_exists/i.test(detail)) { + await markIntent(intent.id, 'waiting', { + attempts: Number(intent.attempts || 0), // 退回:不计入重试预算 + lastError: detail, + targetUnit: unitNow, + }); + return { kind: 'waiting', reason: `该行仍有别的待审(pending_exists),等一轮再补发:${detail}` }; + } if (outcome.kind === 'no_change' && !/待审核的改量/.test(detail)) { // 既不是"又被待审挡住",也不是真的没变(单位刚被改回去) ⇒ 视为不必补发 await markIntent(intent.id, 'noop', { lastError: detail }); diff --git a/server/tests/unitDeferredResend.test.ts b/server/tests/unitDeferredResend.test.ts index 50635aa..6212d8b 100644 --- a/server/tests/unitDeferredResend.test.ts +++ b/server/tests/unitDeferredResend.test.ts @@ -243,4 +243,69 @@ describe('单位变更定向补发(§11.8)', () => { expect(result.abandoned).toBe(0); expect(submitPriceQty).toHaveBeenCalledTimes(1); }); + + /** + * 2026-10-13(主系统把「子系统改我方单位」改成走待审之后的追赶): + * ⑪ 409 `pending_exists` = "该行已有别的待审,再等一轮" ⇒ **不计重试次数**、绝不因此放弃; + * ⑫ 他们审核人**驳回**我们送的单位载体 ⇒ 不再自动重发(尊重审核结论)。 + * 两条都带"不误伤"反证:真失败照样计数、被驳回的是**改量**(不是单位载体)时照旧补发。 + */ + it('⑪ 409 pending_exists ⇒ 保持 waiting 且**不计**重试次数(连试多轮也不放弃)', async () => { + submitPriceQty.mockResolvedValue({ + kind: 'failed', + reason: 'pending_exists:该备货行已有待审申请', + retryable: false, + conflict: 'pending_exists', + }); + for (let i = 0; i < 6; i += 1) { + const outcome = await flushDeferredUnitResendForPrep({ branchCode: BRANCH, prepId: PREP, trigger: 'sweep' }); + expect(outcome.kind).toBe('waiting'); + expect(String(outcome.reason)).toContain('pending_exists'); + } + expect(submitPriceQty, '每轮仍会试一次(10 分钟一跳,不是轰炸)').toHaveBeenCalledTimes(6); + expect(intent?.status, '始终留在 waiting,没有被判放弃').toBe('waiting'); + expect(intent?.attempts, '不计入重试预算').toBe(0); + }); + + it('⑪b 反证:真正的失败仍按上限收敛(不会被这条豁免拖成永久重试)', async () => { + intent = makeIntent({ attempts: 4 }); + submitPriceQty.mockResolvedValue({ kind: 'failed', reason: '主系统不可用', retryable: true }); + const outcome = await flushDeferredUnitResendForPrep({ branchCode: BRANCH, prepId: PREP, trigger: 'sweep' }); + expect(outcome.kind).toBe('abandoned'); + }); + + it('⑫ 单位载体被驳回 ⇒ 不再自动重发(intent 标 noop、不发 HTTP)', async () => { + const outcome = await flushDeferredUnitResendForPrep({ + branchCode: BRANCH, + prepId: PREP, + trigger: 'decided', + justDecided: { status: 'rejected', kind: 'qty', proposedQty: 10, beforeQty: 10 }, + }); + expect(outcome.kind).toBe('noop'); + expect(String(outcome.reason)).toContain('驳回了这次单位变更'); + expect(submitPriceQty, '尊重审核结论:不重发').not.toHaveBeenCalled(); + expect(intent?.status).toBe('noop'); + }); + + it('⑫b 反证:被驳回的是**改量**(proposed ≠ before)⇒ 单位仍照常补发', async () => { + const outcome = await flushDeferredUnitResendForPrep({ + branchCode: BRANCH, + prepId: PREP, + trigger: 'decided', + justDecided: { status: 'rejected', kind: 'qty', proposedQty: 12, beforeQty: 10 }, + }); + expect(outcome.kind).toBe('sent'); + expect(submitPriceQty).toHaveBeenCalledTimes(1); + }); + + it('⑬ 契约钉:对方 409 会被映射成 conflict=pending_exists(不是笼统的"被拒")', () => { + const apiSrc = readFileSync( + path.join(__dirname, '../src/services/mainSystem/api/priceQty.ts'), + 'utf8' + ); + expect(apiSrc).toContain("conflict: 'pending_exists' as const"); + expect(apiSrc).toMatch(/result\.status === 409/); + expect(src).toContain("conflict === 'pending_exists'"); + expect(src).toContain('attempts: Number(intent.attempts || 0)'); + }); });