From 80c73bb34b3dda7ad1a0773cd05cf8992b3649be Mon Sep 17 00:00:00 2001 From: liufumingren Date: Thu, 27 Aug 2026 20:11:52 +0800 Subject: [PATCH] =?UTF-8?q?feat(order):=20=E8=AE=A2=E5=8D=95=E5=88=9B?= =?UTF-8?q?=E5=BB=BA=E8=87=AA=E5=8A=A8=E5=88=86=E9=85=8D=E5=BF=AB=E9=80=92?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=E9=98=9F=E5=88=97+=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E6=89=B9=E9=87=8F=E6=B6=88=E8=B4=B9=EF=BC=8C=E6=B6=88=E9=99=A4?= =?UTF-8?q?=E5=8F=96=E5=8F=B7=E6=AD=BB=E9=94=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 wms_waybill_allocate_queue 队列表(migration + model),订单创建异步流程仅入队(截单/未审核不入队) - 新增 consume_waybill_allocate_queue 定时任务:15s/worker/重入保护,按仓库+货主串行批量消费,同关系下同一时刻仅一个取号事务 - express_platform 三处结算事务统一先 FOR UPDATE wms_store_warehouse 再更新 wms_order_express(lockAndDeductWaybillSettlement),消除 AB-BA 死锁 --- app/model/waybill_allocate_queue.js | 102 ++++++ .../consume_waybill_allocate_queue.js | 255 ++++++++++++++ app/service/systems/delivery_order.js | 67 +++- app/service/systems/express_platform.js | 323 ++++++------------ ...60000-create-wms-waybill-allocate-queue.js | 107 ++++++ 5 files changed, 627 insertions(+), 227 deletions(-) create mode 100644 app/model/waybill_allocate_queue.js create mode 100644 app/schedule/consume_waybill_allocate_queue.js create mode 100644 database/migrations/20260827160000-create-wms-waybill-allocate-queue.js diff --git a/app/model/waybill_allocate_queue.js b/app/model/waybill_allocate_queue.js new file mode 100644 index 0000000..592e1f7 --- /dev/null +++ b/app/model/waybill_allocate_queue.js @@ -0,0 +1,102 @@ +'use strict'; + +/** + * 订单创建后自动分配快递的消费队列模型。 + * + * 订单创建后的异步流程只入队(截单/未审核不入队),由定时任务 + * app/schedule/consume_waybill_allocate_queue.js 按仓库+货主串行批量消费, + * 避免高并发逐单取号时的 InnoDB AB-BA 死锁。 + * + * @param {import('egg').Application} app Egg 应用实例。 + * @return {import('sequelize').ModelCtor} Sequelize 模型。 + */ +module.exports = app => { + const { BIGINT, INTEGER, STRING, DATE } = app.Sequelize; + + const WaybillAllocateQueue = app.model.define('waybill_allocate_queue', { + id: { + type: BIGINT(20).UNSIGNED, + primaryKey: true, + autoIncrement: true, + allowNull: false, + comment: '主键', + }, + order_id: { + type: BIGINT(20).UNSIGNED, + allowNull: false, + comment: '订单ID(关联订单分表)', + }, + business_key: { + type: INTEGER(3), + allowNull: false, + defaultValue: 1, + comment: '业务键(与 wms_order_express.business_key 一致)', + }, + warehouse_id: { + type: BIGINT(20).UNSIGNED, + allowNull: false, + comment: '仓库ID(消费分组与数据隔离维度)', + }, + store_id: { + type: BIGINT(20).UNSIGNED, + allowNull: false, + defaultValue: 0, + comment: '货主ID(消费分组维度)', + }, + status: { + type: STRING(20), + allowNull: false, + defaultValue: 'pending', + comment: '状态:pending/processing/done/failed', + }, + retry_count: { + type: INTEGER(3), + allowNull: false, + defaultValue: 0, + comment: '重试次数', + }, + next_retry_at: { + type: DATE, + allowNull: true, + comment: '下次可消费时间(失败退避)', + }, + error_message: { + type: STRING(500), + allowNull: true, + comment: '最近一次失败原因', + }, + created_at: { + type: DATE, + allowNull: false, + comment: '创建时间', + }, + updated_at: { + type: DATE, + allowNull: false, + comment: '更新时间', + }, + }, { + tableName: 'wms_waybill_allocate_queue', + freezeTableName: true, + underscored: true, + timestamps: false, + comment: '订单创建后自动分配快递消费队列', + indexes: [ + { + name: 'uk_order_id_business_key', + unique: true, + fields: [ 'order_id', 'business_key' ], + }, + { + name: 'idx_status_id', + fields: [ 'status', 'id' ], + }, + { + name: 'idx_status_updated_at', + fields: [ 'status', 'updated_at' ], + }, + ], + }); + + return WaybillAllocateQueue; +}; diff --git a/app/schedule/consume_waybill_allocate_queue.js b/app/schedule/consume_waybill_allocate_queue.js new file mode 100644 index 0000000..8e2034e --- /dev/null +++ b/app/schedule/consume_waybill_allocate_queue.js @@ -0,0 +1,255 @@ +'use strict'; + +/** + * 定时消费订单创建时入队的自动分配快递队列(wms_waybill_allocate_queue)。 + * + * 背景:订单创建后的异步流程逐单直接取号,大批量订单创建时 N 条并发取号事务 + * 与结算事务产生 wms_store_warehouse / wms_order_express 的 AB-BA 死锁。 + * 本任务按仓库+货主串行消费,保证同一签约关系下同一时刻只有一个取号事务在跑。 + * + * 截单语义:订单创建时已判定(截单/未审核不入队),已入队的正常消费,消费侧不再校验截单。 + * 间隔 15 秒、type: worker(仅一个 worker 执行),带重入保护。 + */ + +const MAX_CONSUME_ROWS_PER_WINDOW = 200; +const MAX_RETRY_COUNT = 3; +const RETRY_BASE_DELAY_MS = 60 * 1000; +const PROCESSING_STALE_MS = 10 * 60 * 1000; +const MAX_ERROR_MESSAGE_LENGTH = 500; + +module.exports = { + schedule: { + interval: '15s', + type: 'worker', + immediate: false, + disable: false, + }, + + async task(ctx) { + // 防止重入 + if (ctx.app._consumeWaybillAllocateQueueRunning) return; + ctx.app._consumeWaybillAllocateQueueRunning = true; + + try { + await consumePendingRows(ctx); + } catch (err) { + ctx.logger.error(`[consume-waybill-allocate-queue] 消费异常: ${err.message}`, { stack: err.stack }); + } finally { + ctx.app._consumeWaybillAllocateQueueRunning = false; + } + }, +}; + +/** + * 批量更新队列行状态。 + * @param {Object} ctx Egg 上下文。 + * @param {Array} rows 队列行(含 id)。 + * @param {string} status 目标状态。 + * @param {string|null} errorMessage 失败原因。 + * @return {Promise} 无返回值。 + */ +async function updateQueueRowsStatus(ctx, rows, status, errorMessage) { + const queueModel = ctx.model && ctx.model.WaybillAllocateQueue; + if (!queueModel || !Array.isArray(rows) || rows.length === 0) { + return; + } + + const { Op } = ctx.app.Sequelize; + const ids = rows.map(row => Number(row.id)).filter(id => Number.isInteger(id) && id > 0); + if (ids.length === 0) { + return; + } + + await queueModel.update({ + status, + error_message: errorMessage ? String(errorMessage).slice(0, MAX_ERROR_MESSAGE_LENGTH) : null, + updated_at: new Date(), + }, { + where: { + id: { + [Op.in]: ids, + }, + }, + }); +} + +/** + * 组消费失败或未完成时:重试次数+1,未超限退回 pending(带退避),超限转 failed。 + * @param {Object} ctx Egg 上下文。 + * @param {Array} rows 队列行。 + * @param {string} errorMessage 失败原因。 + * @param {boolean} failImmediately 是否立即判失败(如 warehouse_id 缺失,重试无意义)。 + * @return {Promise} 无返回值。 + */ +async function markGroupRetryOrFailed(ctx, rows, errorMessage, failImmediately) { + const queueModel = ctx.model && ctx.model.WaybillAllocateQueue; + if (!queueModel || !Array.isArray(rows) || rows.length === 0) { + return; + } + + const normalizedErrorMessage = String(errorMessage || '运单分配失败'); + + for (const row of rows) { + const rowId = Number(row.id); + if (!Number.isInteger(rowId) || rowId <= 0) { + continue; + } + + const retryCount = Number(row.retry_count) || 0; + if (failImmediately || retryCount + 1 >= MAX_RETRY_COUNT) { + await queueModel.update({ + status: 'failed', + retry_count: retryCount + 1, + error_message: normalizedErrorMessage.slice(0, MAX_ERROR_MESSAGE_LENGTH), + updated_at: new Date(), + }, { + where: { id: rowId }, + }); + } else { + const delay = (retryCount + 1) * RETRY_BASE_DELAY_MS; + await queueModel.update({ + status: 'pending', + retry_count: retryCount + 1, + next_retry_at: new Date(Date.now() + delay), + error_message: null, + updated_at: new Date(), + }, { + where: { id: rowId }, + }); + } + } +} + +/** + * 拉取并消费待分配队列。 + * @param {Object} ctx Egg 上下文。 + * @return {Promise} 无返回值。 + */ +async function consumePendingRows(ctx) { + const queueModel = ctx.model && ctx.model.WaybillAllocateQueue; + const expressService = ctx.service && ctx.service.systems && ctx.service.systems.express; + if (!queueModel || typeof queueModel.findAll !== 'function' + || !expressService || typeof expressService.createWaybillCodePipeline !== 'function') { + return; + } + + const { Op } = ctx.app.Sequelize; + + // 1) 兜底恢复:上次运行中断遗留的 processing 行退回 pending + await queueModel.update({ + status: 'pending', + updated_at: new Date(), + }, { + where: { + status: 'processing', + updated_at: { + [Op.lt]: new Date(Date.now() - PROCESSING_STALE_MS), + }, + }, + }); + + // 2) 拉取待消费行 + const now = new Date(); + const pendingRows = await queueModel.findAll({ + where: { + status: 'pending', + [Op.or]: [ + { next_retry_at: null }, + { next_retry_at: { [Op.lte]: now } }, + ], + }, + order: [[ 'id', 'ASC' ]], + limit: MAX_CONSUME_ROWS_PER_WINDOW, + raw: true, + }); + + if (!Array.isArray(pendingRows) || pendingRows.length === 0) { + return; + } + + // 3) 标记消费中(单 worker + 重入保护下仅作状态记录) + const pendingIds = pendingRows.map(row => Number(row.id)).filter(id => Number.isInteger(id) && id > 0); + if (pendingIds.length > 0) { + await queueModel.update({ + status: 'processing', + updated_at: new Date(), + }, { + where: { + id: { + [Op.in]: pendingIds, + }, + }, + }); + } + + // 4) 按 warehouse_id + store_id 分组,逐组串行消费 + const groupMap = new Map(); + for (const row of pendingRows) { + const warehouseId = Number(row.warehouse_id) || 0; + const storeId = Number(row.store_id) || 0; + const groupKey = `${warehouseId}:${storeId}`; + if (!groupMap.has(groupKey)) { + groupMap.set(groupKey, { + warehouseId, + storeId, + rows: [], + }); + } + groupMap.get(groupKey).rows.push(row); + } + + const orderExpressModel = ctx.model && ctx.model.OrderExpress; + + for (const group of groupMap.values()) { + const orders = group.rows + .map(row => { + const orderId = Number(row.order_id); + const businessKey = Number(row.business_key) || 1; + return { order_id: orderId, business_key: businessKey }; + }) + .filter(order => Number.isInteger(order.order_id) && order.order_id > 0); + + if (group.warehouseId <= 0 || orders.length === 0) { + await markGroupRetryOrFailed(ctx, group.rows, '队列数据缺少有效 warehouse_id/order_id', true); + continue; + } + + try { + await expressService.createWaybillCodePipeline(orders, { + awaitTasks: true, + warehouseId: group.warehouseId, + }); + + // 5) 判定结果:取到单号(status=1)的订单视为完成 + const doneKeySet = new Set(); + if (orderExpressModel && typeof orderExpressModel.findAll === 'function') { + const doneRows = await orderExpressModel.findAll({ + where: { + warehouse_id: group.warehouseId, + [Op.or]: orders.map(order => ({ + order_id: order.order_id, + business_key: order.business_key, + })), + status: 1, + }, + attributes: [ 'order_id', 'business_key' ], + raw: true, + }); + for (const doneRow of Array.isArray(doneRows) ? doneRows : []) { + doneKeySet.add(`${Number(doneRow.order_id)}:${Number(doneRow.business_key)}`); + } + } + + const doneRows = group.rows.filter(row => doneKeySet.has(`${Number(row.order_id)}:${Number(row.business_key) || 1}`)); + const restRows = group.rows.filter(row => !doneKeySet.has(`${Number(row.order_id)}:${Number(row.business_key) || 1}`)); + + await updateQueueRowsStatus(ctx, doneRows, 'done', null); + await markGroupRetryOrFailed(ctx, restRows, '运单分配未完成', false); + + ctx.logger.info(`[consume-waybill-allocate-queue] 组消费完成 warehouse_id=${group.warehouseId} store_id=${group.storeId} total=${group.rows.length} done=${doneRows.length} rest=${restRows.length}`); + } catch (error) { + ctx.logger.error(`[consume-waybill-allocate-queue] 组消费失败 warehouse_id=${group.warehouseId} store_id=${group.storeId}: ${error.message}`); + await markGroupRetryOrFailed(ctx, group.rows, error && error.message ? error.message : String(error), false); + } + } +} diff --git a/app/service/systems/delivery_order.js b/app/service/systems/delivery_order.js index 8cb969c..dedf7a1 100644 --- a/app/service/systems/delivery_order.js +++ b/app/service/systems/delivery_order.js @@ -1862,6 +1862,63 @@ class DeliveryOrderService extends Service { }; } + /** + * 将订单入队到自动分配快递消费队列(wms_waybill_allocate_queue)。 + * + * 仅写入队列不取号,由定时任务 app/schedule/consume_waybill_allocate_queue.js + * 按仓库+货主串行批量消费。入队幂等(uk_order_id_business_key),重复入队忽略。 + * 调用方需保证截单/未审核订单不会走到此方法。 + * + * @param {Object} context 异步处理上下文。 + * @return {Promise} 无返回值。 + */ + async enqueueAutoWaybillAllocation(context) { + const queueModel = this.ctx.model && this.ctx.model.WaybillAllocateQueue; + if (!queueModel || typeof queueModel.findOrCreate !== 'function') { + this.ctx.logger.warn('[DeliveryOrderService.enqueueAutoWaybillAllocation] 运单分配队列模型未初始化,跳过入队'); + return; + } + + const orderId = Number(context && context.orderId || 0); + const warehouseId = Number(context && context.warehouseId || 0); + const storeId = Number(context && context.storeId || 0); + if (!Number.isInteger(orderId) || orderId <= 0 || !Number.isInteger(warehouseId) || warehouseId <= 0) { + return; + } + + try { + await queueModel.findOrCreate({ + where: { + order_id: orderId, + business_key: 1, + }, + defaults: { + order_id: orderId, + business_key: 1, + warehouse_id: warehouseId, + store_id: storeId > 0 ? storeId : 0, + status: 'pending', + retry_count: 0, + next_retry_at: null, + error_message: null, + created_at: new Date(), + updated_at: new Date(), + }, + }); + this.ctx.logger.info('[DeliveryOrderService.enqueueAutoWaybillAllocation] 订单已入队等待批量分配快递', { + orderId, + warehouseId, + storeId, + }); + } catch (error) { + // 唯一键冲突(重复入队)或 DB 异常仅记录,不阻断订单创建后续异步流程 + this.ctx.logger.warn('[DeliveryOrderService.enqueueAutoWaybillAllocation] 入队失败: %s', error.message, { + orderId, + error: error.message, + }); + } + } + /** * 构建储位分配显式请求体。 * @@ -2540,13 +2597,9 @@ class DeliveryOrderService extends Service { if (isAutoAllocateExpressEnabled) { await this.runAsyncProcessStep('allocateExpress', async () => { - const request = this.buildExpressAllocateRequest(context); - await this.withPatchedCtxUser({ - store_id: context.warehouseId, - warehouse_id: context.warehouseId, - }, async () => { - await this.ctx.service.systemMethods.order.pendingOrder.allocateExpressCode(request); - }); + // 不再逐单直接取号:改为写入消费队列,由定时任务按仓库+货主串行批量消费, + // 避免大批量订单创建时 N 条并发取号事务的 AB-BA 死锁。截单/未审核订单在此处不会入队。 + await this.enqueueAutoWaybillAllocation(context); }); } diff --git a/app/service/systems/express_platform.js b/app/service/systems/express_platform.js index fdb30ef..6136e96 100644 --- a/app/service/systems/express_platform.js +++ b/app/service/systems/express_platform.js @@ -1560,6 +1560,10 @@ class ExpressPlatformService extends MainService { }); try { await this.ctx.model.transaction(async transaction => { + // 统一加锁顺序:先锁 store_warehouse 再更新 order_express, + // 与 createWaybillCode 回写事务一致,避免 AB-BA 死锁 + await this.lockAndDeductWaybillSettlement(settlementRelationMap, storeWarehouseModel, transaction); + for (const item of successUpdates) { const warehouseId = Number(item.warehouse_id) || 0; const orderId = Number(item.order_id) || 0; @@ -1587,78 +1591,6 @@ class ExpressPlatformService extends MainService { }); } } - - if (storeWarehouseModel - && typeof storeWarehouseModel.findAll === 'function' - && settlementRelationMap.size > 0) { - const { Op } = this.app.Sequelize; - const relationList = Array.from(settlementRelationMap.values()); - const relationRows = await storeWarehouseModel.findAll({ - attributes: [ 'id', 'store_id', 'warehouse_id', 'print_quantity', 'print_quantity_locked' ], - where: { - [Op.or]: relationList.map(item => ({ - store_id: item.store_id, - warehouse_id: item.warehouse_id, - })), - }, - raw: true, - transaction, - lock: transaction.LOCK.UPDATE, - }); - - const relationRowMap = new Map(); - for (const row of Array.isArray(relationRows) ? relationRows : []) { - const relationKey = `${Number(row && row.store_id) || 0}:${Number(row && row.warehouse_id) || 0}`; - if (relationKey !== '0:0') { - relationRowMap.set(relationKey, row); - } - } - - for (const item of relationList) { - const relationKey = `${item.store_id}:${item.warehouse_id}`; - const row = relationRowMap.get(relationKey); - if (!row) { - continue; - } - - const currentPrintQuantity = Number(row.print_quantity || 0); - const currentLockedQuantity = Math.max(0, Number(row.print_quantity_locked || 0)); - const successCount = Number(item.success_count || 0); - if (successCount <= 0) { - continue; - } - const printDecrement = successCount; - const lockedDecrement = Math.min(currentLockedQuantity, printDecrement); - const nextPrintQuantity = currentPrintQuantity - printDecrement; - const nextLockedQuantity = Math.max(0, currentLockedQuantity - lockedDecrement); - await storeWarehouseModel.update({ - print_quantity: nextPrintQuantity, - print_quantity_locked: nextLockedQuantity, - }, { - where: { - id: row.id, - }, - transaction, - }); - - await this.ctx.model.StoreWarehousePrintQuantityLog.create({ - contract_id: Number(row.id || 0), - store_id: Number(row.store_id || 0), - warehouse_id: Number(row.warehouse_id || 0), - change_type: 'order_deduct', - business_type: 'order_express', - business_id: null, - change_quantity: -printDecrement, - balance_before: Math.max(0, currentPrintQuantity), - balance_after: Math.max(0, nextPrintQuantity), - reason: '获取快递号成功扣减', - operator_id: null, - created_by: null, - }, { - transaction, - }); - } - } }); } catch (error) { this.ctx.logger.error('[expressPlatform.polyapi] DB 回写事务异常', { @@ -2239,6 +2171,97 @@ class ExpressPlatformService extends MainService { return settlementRelationMap; } + /** + * 事务内按 store:warehouse 扣减打印余量并写扣减流水。 + * + * 必须在结算事务内最先执行:先锁 wms_store_warehouse,再更新 wms_order_express, + * 与 createWaybillCode 回写事务(先锁 store_warehouse 后锁 order_express)保持 + * 一致加锁顺序,消除两类事务间的 AB-BA 死锁。 + * + * @param {Map} settlementRelationMap 结算关系聚合。 + * @param {Object} storeWarehouseModel 签约关系模型。 + * @param {Object} transaction Sequelize 事务。 + * @return {Promise} 无返回值。 + */ + async lockAndDeductWaybillSettlement(settlementRelationMap = new Map(), storeWarehouseModel, transaction) { + if (!storeWarehouseModel + || typeof storeWarehouseModel.findAll !== 'function' + || !settlementRelationMap + || settlementRelationMap.size === 0) { + return; + } + + const { Op } = this.app.Sequelize; + const relationList = Array.from(settlementRelationMap.values()); + const relationRows = await storeWarehouseModel.findAll({ + attributes: [ 'id', 'store_id', 'warehouse_id', 'print_quantity', 'print_quantity_locked' ], + where: { + [Op.or]: relationList.map(item => ({ + store_id: item.store_id, + warehouse_id: item.warehouse_id, + })), + }, + raw: true, + transaction, + lock: transaction.LOCK.UPDATE, + }); + + const relationRowMap = new Map(); + for (const row of Array.isArray(relationRows) ? relationRows : []) { + const relationKey = `${Number(row && row.store_id) || 0}:${Number(row && row.warehouse_id) || 0}`; + if (relationKey !== '0:0') { + relationRowMap.set(relationKey, row); + } + } + + for (const item of relationList) { + const relationKey = `${item.store_id}:${item.warehouse_id}`; + const row = relationRowMap.get(relationKey); + if (!row) { + continue; + } + + const currentPrintQuantity = Number(row.print_quantity || 0); + const currentLockedQuantity = Math.max(0, Number(row.print_quantity_locked || 0)); + const successCount = Number(item.success_count || 0); + if (successCount <= 0) { + continue; + } + + const printDecrement = successCount; + const lockedDecrement = Math.min(currentLockedQuantity, printDecrement); + const nextPrintQuantity = currentPrintQuantity - printDecrement; + const nextLockedQuantity = Math.max(0, currentLockedQuantity - lockedDecrement); + + await storeWarehouseModel.update({ + print_quantity: nextPrintQuantity, + print_quantity_locked: nextLockedQuantity, + }, { + where: { + id: row.id, + }, + transaction, + }); + + await this.ctx.model.StoreWarehousePrintQuantityLog.create({ + contract_id: Number(row.id || 0), + store_id: Number(row.store_id || 0), + warehouse_id: Number(row.warehouse_id || 0), + change_type: 'order_deduct', + business_type: 'order_express', + business_id: null, + change_quantity: -printDecrement, + balance_before: Math.max(0, currentPrintQuantity), + balance_after: Math.max(0, nextPrintQuantity), + reason: '获取快递号成功扣减', + operator_id: null, + created_by: null, + }, { + transaction, + }); + } + } + /** * 回写成功获取的面单结果,并扣减打印余量与写入操作日志。 * @@ -2258,6 +2281,10 @@ class ExpressPlatformService extends MainService { if (typeof this.ctx.model.transaction === 'function') { await this.ctx.model.transaction(async transaction => { + // 统一加锁顺序:先锁 store_warehouse 再更新 order_express, + // 与 createWaybillCode 回写事务一致,避免 AB-BA 死锁 + await this.lockAndDeductWaybillSettlement(settlementRelationMap, storeWarehouseModel, transaction); + for (const item of normalizedSuccessUpdates) { if (!item.warehouse_id || !item.order_id || !item.business_key) { throw new Error('平台接口返回 object_id 无法解析'); @@ -2282,80 +2309,6 @@ class ExpressPlatformService extends MainService { }); } } - - if (storeWarehouseModel - && typeof storeWarehouseModel.findAll === 'function' - && settlementRelationMap.size > 0) { - const { Op } = this.app.Sequelize; - const relationList = Array.from(settlementRelationMap.values()); - const relationRows = await storeWarehouseModel.findAll({ - attributes: [ 'id', 'store_id', 'warehouse_id', 'print_quantity', 'print_quantity_locked' ], - where: { - [Op.or]: relationList.map(item => ({ - store_id: item.store_id, - warehouse_id: item.warehouse_id, - })), - }, - raw: true, - transaction, - lock: transaction.LOCK.UPDATE, - }); - - const relationRowMap = new Map(); - for (const row of Array.isArray(relationRows) ? relationRows : []) { - const relationKey = `${Number(row && row.store_id) || 0}:${Number(row && row.warehouse_id) || 0}`; - if (relationKey !== '0:0') { - relationRowMap.set(relationKey, row); - } - } - - for (const item of relationList) { - const relationKey = `${item.store_id}:${item.warehouse_id}`; - const row = relationRowMap.get(relationKey); - if (!row) { - continue; - } - - const currentPrintQuantity = Number(row.print_quantity || 0); - const currentLockedQuantity = Math.max(0, Number(row.print_quantity_locked || 0)); - const successCount = Number(item.success_count || 0); - if (successCount <= 0) { - continue; - } - - const printDecrement = successCount; - const lockedDecrement = Math.min(currentLockedQuantity, printDecrement); - const nextPrintQuantity = currentPrintQuantity - printDecrement; - const nextLockedQuantity = Math.max(0, currentLockedQuantity - lockedDecrement); - - await storeWarehouseModel.update({ - print_quantity: nextPrintQuantity, - print_quantity_locked: nextLockedQuantity, - }, { - where: { - id: row.id, - }, - transaction, - }); - - await this.ctx.model.StoreWarehousePrintQuantityLog.create({ - contract_id: Number(row.id || 0), - store_id: Number(row.store_id || 0), - warehouse_id: Number(row.warehouse_id || 0), - change_type: 'order_deduct', - business_type: 'order_express', - business_id: null, - change_quantity: -printDecrement, - balance_before: Math.max(0, currentPrintQuantity), - balance_after: Math.max(0, nextPrintQuantity), - reason: '获取快递号成功扣减', - operator_id: null, - created_by: null, - }, { - transaction, - }); - } - } }); } @@ -2661,6 +2614,10 @@ class ExpressPlatformService extends MainService { const storeWarehouseModel = this.ctx.model && this.ctx.model.StoreWarehouse; if (typeof this.ctx.model.transaction === 'function' && successUpdates.length > 0) { await this.ctx.model.transaction(async transaction => { + // 统一加锁顺序:先锁 store_warehouse 再更新 order_express, + // 与 createWaybillCode 回写事务一致,避免 AB-BA 死锁 + await this.lockAndDeductWaybillSettlement(settlementRelationMap, storeWarehouseModel, transaction); + for (const item of successUpdates) { if (!item.warehouse_id || !item.order_id || !item.business_key) { throw new Error('拼多多平台接口返回 object_id 无法解析'); @@ -2685,80 +2642,6 @@ class ExpressPlatformService extends MainService { }); } } - - if (storeWarehouseModel - && typeof storeWarehouseModel.findAll === 'function' - && settlementRelationMap.size > 0) { - const { Op } = this.app.Sequelize; - const relationList = Array.from(settlementRelationMap.values()); - const relationRows = await storeWarehouseModel.findAll({ - attributes: [ 'id', 'store_id', 'warehouse_id', 'print_quantity', 'print_quantity_locked' ], - where: { - [Op.or]: relationList.map(item => ({ - store_id: item.store_id, - warehouse_id: item.warehouse_id, - })), - }, - raw: true, - transaction, - lock: transaction.LOCK.UPDATE, - }); - - const relationRowMap = new Map(); - for (const row of Array.isArray(relationRows) ? relationRows : []) { - const relationKey = `${Number(row && row.store_id) || 0}:${Number(row && row.warehouse_id) || 0}`; - if (relationKey !== '0:0') { - relationRowMap.set(relationKey, row); - } - } - - for (const item of relationList) { - const relationKey = `${item.store_id}:${item.warehouse_id}`; - const row = relationRowMap.get(relationKey); - if (!row) { - continue; - } - - const currentPrintQuantity = Number(row.print_quantity || 0); - const currentLockedQuantity = Math.max(0, Number(row.print_quantity_locked || 0)); - const successCount = Number(item.success_count || 0); - if (successCount <= 0) { - continue; - } - - const printDecrement = successCount; - const lockedDecrement = Math.min(currentLockedQuantity, printDecrement); - const nextPrintQuantity = currentPrintQuantity - printDecrement; - const nextLockedQuantity = Math.max(0, currentLockedQuantity - lockedDecrement); - - await storeWarehouseModel.update({ - print_quantity: nextPrintQuantity, - print_quantity_locked: nextLockedQuantity, - }, { - where: { - id: row.id, - }, - transaction, - }); - - await this.ctx.model.StoreWarehousePrintQuantityLog.create({ - contract_id: Number(row.id || 0), - store_id: Number(row.store_id || 0), - warehouse_id: Number(row.warehouse_id || 0), - change_type: 'order_deduct', - business_type: 'order_express', - business_id: null, - change_quantity: -printDecrement, - balance_before: Math.max(0, currentPrintQuantity), - balance_after: Math.max(0, nextPrintQuantity), - reason: '获取快递号成功扣减', - operator_id: null, - created_by: null, - }, { - transaction, - }); - } - } }); } diff --git a/database/migrations/20260827160000-create-wms-waybill-allocate-queue.js b/database/migrations/20260827160000-create-wms-waybill-allocate-queue.js new file mode 100644 index 0000000..0db2247 --- /dev/null +++ b/database/migrations/20260827160000-create-wms-waybill-allocate-queue.js @@ -0,0 +1,107 @@ +'use strict'; + +/** + * 创建订单创建后自动分配快递的消费队列表 wms_waybill_allocate_queue。 + * + * 背景:订单创建后的异步流程原为逐单直接取号,大批量订单创建时会产生 N 条并发 + * createWaybillCode 回写事务与结算事务的 AB-BA 死锁(wms_store_warehouse 与 + * wms_order_express 锁序相反)。改造为创建时只入队(截单/未审核不入队),由定时 + * 任务按仓库+货主串行批量消费。 + * + * 字段: + * - order_id / business_key:待分配运单的订单与业务键(与 wms_order_express 一致) + * - warehouse_id / store_id:消费分组与数据隔离维度 + * - status:pending(待消费)/ processing(消费中)/ done(完成)/ failed(超限失败) + * - retry_count / next_retry_at / error_message:重试与失败信息 + * + * 索引: + * - uk_order_id_business_key:幂等入队唯一键 + * - idx_status_id:消费拉取(status=pending 按 id 升序) + * - idx_status_updated_at:处理中兜底恢复扫描 + */ + +const TABLE_NAME = 'wms_waybill_allocate_queue'; + +module.exports = { + async up(queryInterface, Sequelize) { + const { BIGINT, INTEGER, STRING, DATE } = Sequelize; + await queryInterface.createTable(TABLE_NAME, { + id: { + type: BIGINT(20).UNSIGNED, + primaryKey: true, + autoIncrement: true, + allowNull: false, + comment: '主键', + }, + order_id: { + type: BIGINT(20).UNSIGNED, + allowNull: false, + comment: '订单ID(关联订单分表)', + }, + business_key: { + type: INTEGER(3), + allowNull: false, + defaultValue: 1, + comment: '业务键(与 wms_order_express.business_key 一致)', + }, + warehouse_id: { + type: BIGINT(20).UNSIGNED, + allowNull: false, + comment: '仓库ID(消费分组与数据隔离维度)', + }, + store_id: { + type: BIGINT(20).UNSIGNED, + allowNull: false, + defaultValue: 0, + comment: '货主ID(消费分组维度)', + }, + status: { + type: STRING(20), + allowNull: false, + defaultValue: 'pending', + comment: '状态:pending/processing/done/failed', + }, + retry_count: { + type: INTEGER(3), + allowNull: false, + defaultValue: 0, + comment: '重试次数', + }, + next_retry_at: { + type: DATE, + allowNull: true, + comment: '下次可消费时间(失败退避)', + }, + error_message: { + type: STRING(500), + allowNull: true, + comment: '最近一次失败原因', + }, + created_at: { + type: DATE, + allowNull: false, + comment: '创建时间', + }, + updated_at: { + type: DATE, + allowNull: false, + comment: '更新时间', + }, + }); + + await queryInterface.addIndex(TABLE_NAME, [ 'order_id', 'business_key' ], { + name: 'uk_order_id_business_key', + unique: true, + }); + await queryInterface.addIndex(TABLE_NAME, [ 'status', 'id' ], { + name: 'idx_status_id', + }); + await queryInterface.addIndex(TABLE_NAME, [ 'status', 'updated_at' ], { + name: 'idx_status_updated_at', + }); + }, + + async down(queryInterface) { + await queryInterface.dropTable(TABLE_NAME); + }, +};