From 08f194c7d23c865d88d49da33824c4e2b2d43597 Mon Sep 17 00:00:00 2001 From: liufumingren Date: Wed, 30 Sep 2026 03:08:55 +0800 Subject: [PATCH] =?UTF-8?q?fix(receiving):=20=E7=BB=91=E5=AE=9A=E5=85=B3?= =?UTF-8?q?=E7=B3=BB=E8=BD=AF=E5=88=A0=E9=99=A4=E6=81=A2=E5=A4=8D=E4=B8=8E?= =?UTF-8?q?=E5=94=AF=E4=B8=80=E7=B4=A2=E5=BC=95=E5=86=B2=E7=AA=81=E5=B9=82?= =?UTF-8?q?=E7=AD=89=E4=BF=AE=E5=A4=8D=EF=BC=88findOrCreate=20=E6=92=9E=20?= =?UTF-8?q?uk=20=E7=B4=A2=E5=BC=95=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/service/systems/receiving_entry_match.js | 110 ++++++++++++++----- 1 file changed, 85 insertions(+), 25 deletions(-) diff --git a/app/service/systems/receiving_entry_match.js b/app/service/systems/receiving_entry_match.js index abf7bbf..6e44dcf 100644 --- a/app/service/systems/receiving_entry_match.js +++ b/app/service/systems/receiving_entry_match.js @@ -172,8 +172,9 @@ class ReceivingEntryMatchService extends Service { }); if (existing) { - if (existing.deleted_at) { - // 已软删除的记录,恢复它 + if (existing.deletedAt || existing.deleted_at) { + // 已软删除的记录,恢复它(台账重置为 {},从 0 重新分配) + existing.allocated_quantity = {}; await existing.restore(); ctx.logger.info( `[ReceivingEntryMatch] 恢复已删除的绑定关系 entryOrderId=${entryOrderId} receivingId=${receivingId}` @@ -352,33 +353,65 @@ class ReceivingEntryMatchService extends Service { throw new Error(`入库单绑定收货单失败,已回滚: ${error.message}`); } } else { - // CREATED:保持原有逻辑,仅逐条写入主表绑定关系 + // CREATED:逐条写入主表绑定关系,幂等三态: + // 已存在未删除 → 跳过;已软删除(解绑产生)→ 恢复;不存在 → 新建。 + // 注意不能用 findOrCreate:其查询默认排除软删除行,解绑后再绑定同一对 + // (entry_order_id, receiving_id) 会撞唯一索引 uk_entry_order_receiving, + // 抛 SequelizeUniqueConstraintError(message 为 "Validation error")。 for (const receiving of validatedReceivings) { try { - const [ , created ] = await ctx.model.EntryOrderReceiving.findOrCreate({ + const existing = await ctx.model.EntryOrderReceiving.findOne({ where: { entry_order_id: entryOrderId, receiving_id: receiving.id }, - defaults: { - entry_order_id: entryOrderId, - receiving_id: receiving.id, - // 新链路绑定行台账初始化为空对象(区别于旧数据 NULL) - allocated_quantity: {}, - }, + paranoid: false, }); - if (created) { - ctx.logger.info( - `[ReceivingEntryMatch] 主表绑定成功 entryOrderId=${entryOrderId} receivingId=${receiving.id}` - ); + let created = false; + let restored = false; + + if (existing) { + if (existing.deletedAt || existing.deleted_at) { + // 解绑产生的软删除行:台账在解绑时已重置为 {},恢复后从 0 重新分配 + existing.allocated_quantity = {}; + await existing.restore(); + restored = true; + ctx.logger.info( + `[ReceivingEntryMatch] 恢复已解绑的绑定关系 entryOrderId=${entryOrderId} receivingId=${receiving.id}` + ); + } else { + ctx.logger.info( + `[ReceivingEntryMatch] 主表绑定已存在,跳过 entryOrderId=${entryOrderId} receivingId=${receiving.id}` + ); + } } else { - ctx.logger.info( - `[ReceivingEntryMatch] 主表绑定已存在,跳过 entryOrderId=${entryOrderId} receivingId=${receiving.id}` - ); + try { + await ctx.model.EntryOrderReceiving.create({ + entry_order_id: entryOrderId, + receiving_id: receiving.id, + // 新链路绑定行台账初始化为空对象(区别于旧数据 NULL) + allocated_quantity: {}, + }); + created = true; + ctx.logger.info( + `[ReceivingEntryMatch] 主表绑定成功 entryOrderId=${entryOrderId} receivingId=${receiving.id}` + ); + } catch (createError) { + // 并发场景:唯一索引冲突视为绑定已建立(与 findOrCreate 幂等语义一致) + if (createError.name === 'SequelizeUniqueConstraintError') { + ctx.logger.warn( + `[ReceivingEntryMatch] 主表绑定已存在(并发写入) entryOrderId=${entryOrderId} ` + + `receivingId=${receiving.id}` + ); + } else { + throw createError; + } + } } boundResults.push({ receiving_id: receiving.id, receiving_number: receiving.receiving_number, created, + restored, }); } catch (error) { ctx.logger.error( @@ -491,6 +524,8 @@ class ReceivingEntryMatchService extends Service { if (existing) { const deletedAt = existing.deletedAt || existing.deleted_at || null; if (deletedAt) { + // 恢复软删除行(台账在解绑时已重置为 {},此处显式兜底,从 0 重新分配) + existing.allocated_quantity = {}; await existing.restore({ transaction }); restored = true; ctx.logger.info( @@ -2125,17 +2160,42 @@ class ReceivingEntryMatchService extends Service { operator_name: ctx.currentUser?.employees_name || '', }, { transaction }); - // 6. 确保主级绑定存在(新链路台账初始化为空对象) - await ctx.model.EntryOrderReceiving.findOrCreate({ - where: { entry_order_id: entryOrderId, receiving_id: lockedDetail[0].receiving_id }, - defaults: { - entry_order_id: entryOrderId, - receiving_id: lockedDetail[0].receiving_id, - allocated_quantity: {}, - }, + // 6. 确保主级绑定存在(新链路台账初始化为空对象)。 + // 三态幂等:findOrCreate 默认排除软删除行,解绑后再分配会撞唯一索引, + // 这里改为 paranoid:false 查询 + 恢复软删除行 + 新建(唯一索引冲突视为并发已建立)。 + const receivingId = Number(lockedDetail[0].receiving_id); + const existingBinding = await ctx.model.EntryOrderReceiving.findOne({ + where: { entry_order_id: entryOrderId, receiving_id: receivingId }, + paranoid: false, transaction, }); + if (existingBinding) { + if (existingBinding.deletedAt || existingBinding.deleted_at) { + existingBinding.allocated_quantity = {}; + await existingBinding.restore({ transaction }); + ctx.logger.info( + `[ReceivingEntryMatch] 恢复已解绑的主表绑定(PDA 分配) entryOrderId=${entryOrderId} ` + + `receivingId=${receivingId}` + ); + } + } else { + try { + await ctx.model.EntryOrderReceiving.create({ + entry_order_id: entryOrderId, + receiving_id: receivingId, + allocated_quantity: {}, + }, { transaction }); + } catch (createError) { + if (createError.name !== 'SequelizeUniqueConstraintError') { + throw createError; + } + ctx.logger.warn( + `[ReceivingEntryMatch] 主表绑定已存在(并发写入) entryOrderId=${entryOrderId} receivingId=${receivingId}` + ); + } + } + // 7. 回写数量台账:本批次对该收货明细的分配量累加到绑定行 const ledgerDeltaMap = new Map(); ledgerDeltaMap.set(Number(receivingDetailId), {