fix(receiving): 绑定关系软删除恢复与唯一索引冲突幂等修复(findOrCreate 撞 uk 索引)
This commit is contained in:
parent
8c50bcd6b7
commit
08f194c7d2
@ -172,8 +172,9 @@ class ReceivingEntryMatchService extends Service {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (existing) {
|
if (existing) {
|
||||||
if (existing.deleted_at) {
|
if (existing.deletedAt || existing.deleted_at) {
|
||||||
// 已软删除的记录,恢复它
|
// 已软删除的记录,恢复它(台账重置为 {},从 0 重新分配)
|
||||||
|
existing.allocated_quantity = {};
|
||||||
await existing.restore();
|
await existing.restore();
|
||||||
ctx.logger.info(
|
ctx.logger.info(
|
||||||
`[ReceivingEntryMatch] 恢复已删除的绑定关系 entryOrderId=${entryOrderId} receivingId=${receivingId}`
|
`[ReceivingEntryMatch] 恢复已删除的绑定关系 entryOrderId=${entryOrderId} receivingId=${receivingId}`
|
||||||
@ -352,33 +353,65 @@ class ReceivingEntryMatchService extends Service {
|
|||||||
throw new Error(`入库单绑定收货单失败,已回滚: ${error.message}`);
|
throw new Error(`入库单绑定收货单失败,已回滚: ${error.message}`);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// CREATED:保持原有逻辑,仅逐条写入主表绑定关系
|
// CREATED:逐条写入主表绑定关系,幂等三态:
|
||||||
|
// 已存在未删除 → 跳过;已软删除(解绑产生)→ 恢复;不存在 → 新建。
|
||||||
|
// 注意不能用 findOrCreate:其查询默认排除软删除行,解绑后再绑定同一对
|
||||||
|
// (entry_order_id, receiving_id) 会撞唯一索引 uk_entry_order_receiving,
|
||||||
|
// 抛 SequelizeUniqueConstraintError(message 为 "Validation error")。
|
||||||
for (const receiving of validatedReceivings) {
|
for (const receiving of validatedReceivings) {
|
||||||
try {
|
try {
|
||||||
const [ , created ] = await ctx.model.EntryOrderReceiving.findOrCreate({
|
const existing = await ctx.model.EntryOrderReceiving.findOne({
|
||||||
where: { entry_order_id: entryOrderId, receiving_id: receiving.id },
|
where: { entry_order_id: entryOrderId, receiving_id: receiving.id },
|
||||||
defaults: {
|
paranoid: false,
|
||||||
entry_order_id: entryOrderId,
|
|
||||||
receiving_id: receiving.id,
|
|
||||||
// 新链路绑定行台账初始化为空对象(区别于旧数据 NULL)
|
|
||||||
allocated_quantity: {},
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (created) {
|
let created = false;
|
||||||
ctx.logger.info(
|
let restored = false;
|
||||||
`[ReceivingEntryMatch] 主表绑定成功 entryOrderId=${entryOrderId} receivingId=${receiving.id}`
|
|
||||||
);
|
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 {
|
} else {
|
||||||
ctx.logger.info(
|
try {
|
||||||
`[ReceivingEntryMatch] 主表绑定已存在,跳过 entryOrderId=${entryOrderId} receivingId=${receiving.id}`
|
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({
|
boundResults.push({
|
||||||
receiving_id: receiving.id,
|
receiving_id: receiving.id,
|
||||||
receiving_number: receiving.receiving_number,
|
receiving_number: receiving.receiving_number,
|
||||||
created,
|
created,
|
||||||
|
restored,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
ctx.logger.error(
|
ctx.logger.error(
|
||||||
@ -491,6 +524,8 @@ class ReceivingEntryMatchService extends Service {
|
|||||||
if (existing) {
|
if (existing) {
|
||||||
const deletedAt = existing.deletedAt || existing.deleted_at || null;
|
const deletedAt = existing.deletedAt || existing.deleted_at || null;
|
||||||
if (deletedAt) {
|
if (deletedAt) {
|
||||||
|
// 恢复软删除行(台账在解绑时已重置为 {},此处显式兜底,从 0 重新分配)
|
||||||
|
existing.allocated_quantity = {};
|
||||||
await existing.restore({ transaction });
|
await existing.restore({ transaction });
|
||||||
restored = true;
|
restored = true;
|
||||||
ctx.logger.info(
|
ctx.logger.info(
|
||||||
@ -2125,17 +2160,42 @@ class ReceivingEntryMatchService extends Service {
|
|||||||
operator_name: ctx.currentUser?.employees_name || '',
|
operator_name: ctx.currentUser?.employees_name || '',
|
||||||
}, { transaction });
|
}, { transaction });
|
||||||
|
|
||||||
// 6. 确保主级绑定存在(新链路台账初始化为空对象)
|
// 6. 确保主级绑定存在(新链路台账初始化为空对象)。
|
||||||
await ctx.model.EntryOrderReceiving.findOrCreate({
|
// 三态幂等:findOrCreate 默认排除软删除行,解绑后再分配会撞唯一索引,
|
||||||
where: { entry_order_id: entryOrderId, receiving_id: lockedDetail[0].receiving_id },
|
// 这里改为 paranoid:false 查询 + 恢复软删除行 + 新建(唯一索引冲突视为并发已建立)。
|
||||||
defaults: {
|
const receivingId = Number(lockedDetail[0].receiving_id);
|
||||||
entry_order_id: entryOrderId,
|
const existingBinding = await ctx.model.EntryOrderReceiving.findOne({
|
||||||
receiving_id: lockedDetail[0].receiving_id,
|
where: { entry_order_id: entryOrderId, receiving_id: receivingId },
|
||||||
allocated_quantity: {},
|
paranoid: false,
|
||||||
},
|
|
||||||
transaction,
|
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. 回写数量台账:本批次对该收货明细的分配量累加到绑定行
|
// 7. 回写数量台账:本批次对该收货明细的分配量累加到绑定行
|
||||||
const ledgerDeltaMap = new Map();
|
const ledgerDeltaMap = new Map();
|
||||||
ledgerDeltaMap.set(Number(receivingDetailId), {
|
ledgerDeltaMap.set(Number(receivingDetailId), {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user