fix(wave): 修复并发取消订单时事务死锁导致 rollback 错误

replaceOrderWaveIndexByWaveIds 内部 DELETE WHERE wave_id IN(...) 在并发取消同一波次时可能死锁,MySQL 自动回滚事务后,内层 try-catch 吞掉错误继续使用已死事务导致报错。去掉内层 try-catch,让错误传播到外层统一处理 rollback。
This commit is contained in:
liufumingren 2026-08-02 00:52:11 +08:00
parent 02b3a5e813
commit 02596feb3d
2 changed files with 64 additions and 72 deletions

View File

@ -85,42 +85,38 @@ class OpenapiOrderService extends MainService {
// 波次清理:删除波次索引、重建波次索引、更新波次统计 // 波次清理:删除波次索引、重建波次索引、更新波次统计
if (existingWaveId > 0) { if (existingWaveId > 0) {
try { await ctx.service.systemMethods.order.wave.deleteOrderWaveIndexByOrderIds({
await ctx.service.systemMethods.order.wave.deleteOrderWaveIndexByOrderIds({ warehouseId,
warehouseId, orderIds: [ existingOrder.id ],
orderIds: [ existingOrder.id ], transaction,
});
await ctx.service.systemMethods.order.wave.replaceOrderWaveIndexByWaveIds({
warehouseId,
waveIds: [ existingWaveId ],
orderModel: DeliveryModel,
transaction,
});
const WaveBatch = ctx.model.WaveBatch;
if (WaveBatch) {
const wave = await WaveBatch.findOne({
where: { wave_id: existingWaveId, warehouse_id: warehouseId },
attributes: [ 'wave_id', 'total_orders', 'wave_status' ],
raw: true,
transaction, transaction,
}); });
await ctx.service.systemMethods.order.wave.replaceOrderWaveIndexByWaveIds({ if (wave) {
warehouseId, const totalOrders = Number(wave.total_orders);
waveIds: [ existingWaveId ], const normalizedTotalOrders = Number.isFinite(totalOrders) ? totalOrders : 0;
orderModel: DeliveryModel, if (normalizedTotalOrders <= 0) {
transaction, await WaveBatch.update(
}); { total_orders: 0, wave_status: -1, update_time: new Date() },
{ where: { wave_id: existingWaveId, warehouse_id: warehouseId }, transaction }
const WaveBatch = ctx.model.WaveBatch; );
if (WaveBatch) {
const wave = await WaveBatch.findOne({
where: { wave_id: existingWaveId, warehouse_id: warehouseId },
attributes: [ 'wave_id', 'total_orders', 'wave_status' ],
raw: true,
transaction,
});
if (wave) {
const totalOrders = Number(wave.total_orders);
const normalizedTotalOrders = Number.isFinite(totalOrders) ? totalOrders : 0;
if (normalizedTotalOrders <= 0) {
await WaveBatch.update(
{ total_orders: 0, wave_status: -1, update_time: new Date() },
{ where: { wave_id: existingWaveId, warehouse_id: warehouseId }, transaction }
);
}
} }
} }
} catch (waveErr) {
ctx.logger.warn('[OpenapiOrderCancel] 清理波次关联失败,不影响订单取消', waveErr);
} }
} }

View File

@ -238,56 +238,52 @@ class orderServer extends MainService {
await existingOrder.update(updateValues, { transaction }); await existingOrder.update(updateValues, { transaction });
// 波次清理:删除波次索引、更新波次统计 // 波次清理:删除波次索引、重建波次索引、更新波次统计
if (existingWaveId > 0) { if (existingWaveId > 0) {
try { await ctx.service.systemMethods.order.wave.deleteOrderWaveIndexByOrderIds({
await ctx.service.systemMethods.order.wave.deleteOrderWaveIndexByOrderIds({ warehouseId: info.warehouseId,
warehouseId: info.warehouseId, orderIds: [ existingOrder.id ],
orderIds: [ existingOrder.id ], transaction,
});
// 重建受影响波次的订单索引(wave_index 重新排序)
await ctx.service.systemMethods.order.wave.replaceOrderWaveIndexByWaveIds({
warehouseId: info.warehouseId,
waveIds: [ existingWaveId ],
orderModel: DeliveryModel,
transaction,
});
const { Op } = this.app.Sequelize;
const WaveBatch = ctx.model.WaveBatch;
if (WaveBatch) {
await WaveBatch.decrement('total_orders', {
by: 1,
where: {
wave_id: existingWaveId,
warehouse_id: info.warehouseId,
total_orders: { [Op.gt]: 0 },
},
transaction, transaction,
}); });
// 重建受影响波次的订单索引(wave_index 重新排序) const wave = await WaveBatch.findOne({
await ctx.service.systemMethods.order.wave.replaceOrderWaveIndexByWaveIds({ where: { wave_id: existingWaveId, warehouse_id: info.warehouseId },
warehouseId: info.warehouseId, attributes: [ 'wave_id', 'total_orders', 'wave_status' ],
waveIds: [ existingWaveId ], raw: true,
orderModel: DeliveryModel,
transaction, transaction,
}); });
const { Op } = this.app.Sequelize; if (wave) {
const WaveBatch = ctx.model.WaveBatch; const totalOrders = Number(wave.total_orders);
if (WaveBatch) { const normalizedTotalOrders = Number.isFinite(totalOrders) ? totalOrders : 0;
await WaveBatch.decrement('total_orders', { if (normalizedTotalOrders <= 0) {
by: 1, await WaveBatch.update(
where: { { total_orders: 0, wave_status: -1, update_time: new Date() },
wave_id: existingWaveId, { where: { wave_id: existingWaveId, warehouse_id: info.warehouseId }, transaction }
warehouse_id: info.warehouseId, );
total_orders: { [Op.gt]: 0 },
},
transaction,
});
const wave = await WaveBatch.findOne({
where: { wave_id: existingWaveId, warehouse_id: info.warehouseId },
attributes: [ 'wave_id', 'total_orders', 'wave_status' ],
raw: true,
transaction,
});
if (wave) {
const totalOrders = Number(wave.total_orders);
const normalizedTotalOrders = Number.isFinite(totalOrders) ? totalOrders : 0;
if (normalizedTotalOrders <= 0) {
await WaveBatch.update(
{ total_orders: 0, wave_status: -1, update_time: new Date() },
{ where: { wave_id: existingWaveId, warehouse_id: info.warehouseId }, transaction }
);
}
} }
} }
} catch (waveErr) {
ctx.logger.warn('[QimenOrderCancel] 清理波次关联失败,不影响订单取消', waveErr);
} }
} }