fix(receiving): 绑定关系软删除恢复与唯一索引冲突幂等修复(findOrCreate 撞 uk 索引)

This commit is contained in:
liufumingren 2026-09-30 03:08:55 +08:00
parent 8c50bcd6b7
commit 08f194c7d2

View File

@ -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), {