From 02596feb3d768b4dfe85a1840076e616bb07ef36 Mon Sep 17 00:00:00 2001 From: liufumingren Date: Sun, 2 Aug 2026 00:52:11 +0800 Subject: [PATCH] =?UTF-8?q?fix(wave):=20=E4=BF=AE=E5=A4=8D=E5=B9=B6?= =?UTF-8?q?=E5=8F=91=E5=8F=96=E6=B6=88=E8=AE=A2=E5=8D=95=E6=97=B6=E4=BA=8B?= =?UTF-8?q?=E5=8A=A1=E6=AD=BB=E9=94=81=E5=AF=BC=E8=87=B4=20rollback=20?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit replaceOrderWaveIndexByWaveIds 内部 DELETE WHERE wave_id IN(...) 在并发取消同一波次时可能死锁,MySQL 自动回滚事务后,内层 try-catch 吞掉错误继续使用已死事务导致报错。去掉内层 try-catch,让错误传播到外层统一处理 rollback。 --- app/service/openapiMethods/order.js | 58 ++++++++++----------- app/service/qimenMethods/order.js | 78 ++++++++++++++--------------- 2 files changed, 64 insertions(+), 72 deletions(-) diff --git a/app/service/openapiMethods/order.js b/app/service/openapiMethods/order.js index 85cc3f6..6c6764b 100644 --- a/app/service/openapiMethods/order.js +++ b/app/service/openapiMethods/order.js @@ -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); } } diff --git a/app/service/qimenMethods/order.js b/app/service/qimenMethods/order.js index 4110b00..a949a49 100644 --- a/app/service/qimenMethods/order.js +++ b/app/service/qimenMethods/order.js @@ -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); } }