fix(wave): 修复并发取消订单时事务死锁导致 rollback 错误
replaceOrderWaveIndexByWaveIds 内部 DELETE WHERE wave_id IN(...) 在并发取消同一波次时可能死锁,MySQL 自动回滚事务后,内层 try-catch 吞掉错误继续使用已死事务导致报错。去掉内层 try-catch,让错误传播到外层统一处理 rollback。
This commit is contained in:
parent
02b3a5e813
commit
02596feb3d
@ -85,42 +85,38 @@ class OpenapiOrderService extends MainService {
|
||||
|
||||
// 波次清理:删除波次索引、重建波次索引、更新波次统计
|
||||
if (existingWaveId > 0) {
|
||||
try {
|
||||
await ctx.service.systemMethods.order.wave.deleteOrderWaveIndexByOrderIds({
|
||||
warehouseId,
|
||||
orderIds: [ existingOrder.id ],
|
||||
await ctx.service.systemMethods.order.wave.deleteOrderWaveIndexByOrderIds({
|
||||
warehouseId,
|
||||
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,
|
||||
});
|
||||
|
||||
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,
|
||||
});
|
||||
|
||||
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 }
|
||||
);
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -238,56 +238,52 @@ class orderServer extends MainService {
|
||||
|
||||
await existingOrder.update(updateValues, { transaction });
|
||||
|
||||
// 波次清理:删除波次索引、更新波次统计
|
||||
// 波次清理:删除波次索引、重建波次索引、更新波次统计
|
||||
if (existingWaveId > 0) {
|
||||
try {
|
||||
await ctx.service.systemMethods.order.wave.deleteOrderWaveIndexByOrderIds({
|
||||
warehouseId: info.warehouseId,
|
||||
orderIds: [ existingOrder.id ],
|
||||
await ctx.service.systemMethods.order.wave.deleteOrderWaveIndexByOrderIds({
|
||||
warehouseId: info.warehouseId,
|
||||
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,
|
||||
});
|
||||
|
||||
// 重建受影响波次的订单索引(wave_index 重新排序)
|
||||
await ctx.service.systemMethods.order.wave.replaceOrderWaveIndexByWaveIds({
|
||||
warehouseId: info.warehouseId,
|
||||
waveIds: [ existingWaveId ],
|
||||
orderModel: DeliveryModel,
|
||||
const wave = await WaveBatch.findOne({
|
||||
where: { wave_id: existingWaveId, warehouse_id: info.warehouseId },
|
||||
attributes: [ 'wave_id', 'total_orders', 'wave_status' ],
|
||||
raw: true,
|
||||
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,
|
||||
});
|
||||
|
||||
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 }
|
||||
);
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user