- express/express_platform: 新增 OMS 回源取号(need_oms_waybill=1)与统一失败收尾, 失败时标记订单异常、写失败日志并清理未完成占位行;队列消费与手工分配共用同一口径 - order_print: 抖音打印按运单落库的 logistics_interface_id 精确命中取号接口配置, 临时 key 缓存键带店铺与 token 指纹,避免多店铺串用 token;打印前按 OMS 记录换新 params - order_express: 新增 print_history_id、cai_niao_auth_info_id 字段 - consume_waybill_allocate_queue / pendingOrder / delivery_order / order_async / printer_data_builder / order_delivery_table_schema: 配套调整 - migrations: 运单 wave_no 索引、OMS 打印字段 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
467 lines
16 KiB
JavaScript
467 lines
16 KiB
JavaScript
'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 WAITING_OMS_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<Object>} rows 队列行(含 id)。
|
||
* @param {string} status 目标状态。
|
||
* @param {string|null} errorMessage 失败原因。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
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<Object>} rows 队列行。
|
||
* @param {string} errorMessage 失败原因。
|
||
* @param {boolean} failImmediately 是否立即判失败(如 warehouse_id 缺失,重试无意义)。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
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 },
|
||
});
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 消费需要回 OMS 取号的队列行。
|
||
*
|
||
* 这些订单被本地取号流水线整体跳过(不分配本地快递),改由 OMS 侧外部打单账号取号:
|
||
* - 成功:order_express 落运单号(status=1),队列行置 done;
|
||
* - 不可重试失败(订单取消/作废、编码未绑定、凭据缺失等):队列行置 failed,原因写入 error_message;
|
||
* - 可重试失败(网络、OMS 5xx、平台未返回运单号):按现有重试策略退回 pending。
|
||
*
|
||
* @param {Object} ctx Egg 上下文。
|
||
* @param {Object} group 分组信息(warehouseId / storeId / rows)。
|
||
* @param {Array<Object>} waitingRows 需要回 OMS 取号的队列行。
|
||
* @return {Promise<{done: number, failed: number, retry: number}>} 处理统计。
|
||
*/
|
||
async function consumeOmsWaybillRows(ctx, group, waitingRows) {
|
||
const summary = { done: 0, failed: 0, retry: 0 };
|
||
const expressService = ctx.service && ctx.service.systems && ctx.service.systems.express;
|
||
const expressPlatformService = ctx.service && ctx.service.systems
|
||
&& (ctx.service.systems.expressPlatform || ctx.service.systems.express_platform);
|
||
|
||
if (!expressService || !expressPlatformService || typeof expressPlatformService.oms !== 'function') {
|
||
await markGroupRetryOrFailed(ctx, waitingRows, 'OMS 取号平台未实现(express_platform.oms)', true);
|
||
summary.failed = waitingRows.length;
|
||
return summary;
|
||
}
|
||
|
||
const { Op } = ctx.app.Sequelize;
|
||
const orderIds = Array.from(new Set(
|
||
waitingRows
|
||
.map(row => Number(row.order_id))
|
||
.filter(orderId => Number.isInteger(orderId) && orderId > 0)
|
||
));
|
||
|
||
if (orderIds.length === 0) {
|
||
await markGroupRetryOrFailed(ctx, waitingRows, '队列数据缺少有效 order_id', true);
|
||
summary.failed = waitingRows.length;
|
||
return summary;
|
||
}
|
||
|
||
const orderModel = await expressService.getWaybillAsyncOrderDeliveryModel(group.warehouseId);
|
||
const orderRows = orderModel && typeof orderModel.findAll === 'function'
|
||
? await orderModel.findAll({
|
||
where: {
|
||
warehouse_id: group.warehouseId,
|
||
id: { [Op.in]: orderIds },
|
||
},
|
||
attributes: [
|
||
'id', 'warehouse_id', 'store_id', 'delivery_order_code',
|
||
'logistics_code', 'sys_logistics_code', 'source_order_codes', 'need_oms_waybill', 'from_key',
|
||
],
|
||
raw: true,
|
||
})
|
||
: [];
|
||
const orderMap = new Map(
|
||
(Array.isArray(orderRows) ? orderRows : []).map(row => [ Number(row.id), row ])
|
||
);
|
||
|
||
const bizOrders = [];
|
||
const missingRows = [];
|
||
for (const row of waitingRows) {
|
||
const orderId = Number(row.order_id);
|
||
const order = orderMap.get(orderId);
|
||
|
||
if (!order) {
|
||
missingRows.push(row);
|
||
continue;
|
||
}
|
||
|
||
bizOrders.push({
|
||
order_id: orderId,
|
||
business_key: Number(row.business_key) || 1,
|
||
order_delivery: order,
|
||
});
|
||
}
|
||
|
||
if (missingRows.length > 0) {
|
||
await updateQueueRowsStatus(ctx, missingRows, 'failed', '订单不存在或已流转到其它分表');
|
||
summary.failed += missingRows.length;
|
||
}
|
||
|
||
if (bizOrders.length === 0) {
|
||
return summary;
|
||
}
|
||
|
||
let omsError = null;
|
||
try {
|
||
const omsPayload = {
|
||
warehouse_id: group.warehouseId,
|
||
store_id: group.storeId,
|
||
orders: bizOrders,
|
||
};
|
||
|
||
if (typeof expressService.dispatchWaybillAsyncPlatform === 'function') {
|
||
await expressService.dispatchWaybillAsyncPlatform('oms', omsPayload);
|
||
} else {
|
||
await expressPlatformService.oms(omsPayload);
|
||
}
|
||
} catch (error) {
|
||
// 部分成功时成功部分已在平台方法内落库,这里统一按 order_express 实际结果判定
|
||
omsError = error;
|
||
}
|
||
|
||
const orderExpressModel = ctx.model && ctx.model.OrderExpress;
|
||
const doneKeySet = new Set();
|
||
if (orderExpressModel && typeof orderExpressModel.findAll === 'function') {
|
||
const doneRows = await orderExpressModel.findAll({
|
||
where: {
|
||
warehouse_id: group.warehouseId,
|
||
[Op.or]: bizOrders.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) || 1}`);
|
||
}
|
||
}
|
||
|
||
const omsDoneRows = [];
|
||
const omsRestRows = [];
|
||
for (const row of waitingRows) {
|
||
if (missingRows.includes(row)) {
|
||
continue;
|
||
}
|
||
|
||
const rowKey = `${Number(row.order_id)}:${Number(row.business_key) || 1}`;
|
||
if (doneKeySet.has(rowKey)) {
|
||
omsDoneRows.push(row);
|
||
} else {
|
||
omsRestRows.push(row);
|
||
}
|
||
}
|
||
|
||
if (omsDoneRows.length > 0) {
|
||
await updateQueueRowsStatus(ctx, omsDoneRows, 'done', null);
|
||
summary.done += omsDoneRows.length;
|
||
}
|
||
|
||
if (omsRestRows.length > 0) {
|
||
const retryable = !omsError || omsError.retryable !== false;
|
||
const errorMessage = omsError && omsError.message ? omsError.message : 'OMS 取号未完成';
|
||
|
||
if (omsError && typeof expressService.handleWaybillAllocateFailure === 'function') {
|
||
// 与本地平台失败口径一致:标记订单异常、写失败日志、清理未完成占位行
|
||
await expressService.handleWaybillAllocateFailure(
|
||
omsRestRows.map(row => ({
|
||
order_id: Number(row.order_id),
|
||
business_key: Number(row.business_key) || 1,
|
||
})),
|
||
{ warehouse_id: group.warehouseId, sys_platform_code: 'oms' },
|
||
errorMessage
|
||
).catch(failureError => {
|
||
ctx.logger.error(
|
||
`[consume-waybill-allocate-queue] OMS 失败收尾处理异常: ${failureError && failureError.message}`
|
||
);
|
||
});
|
||
}
|
||
|
||
if (retryable) {
|
||
await markGroupRetryOrFailed(ctx, omsRestRows, errorMessage, false);
|
||
summary.retry += omsRestRows.length;
|
||
} else {
|
||
await markGroupRetryOrFailed(ctx, omsRestRows, errorMessage, true);
|
||
summary.failed += omsRestRows.length;
|
||
}
|
||
}
|
||
|
||
ctx.logger.info(
|
||
`[consume-waybill-allocate-queue] OMS 取号完成 warehouse_id=${group.warehouseId} store_id=${group.storeId} `
|
||
+ `total=${bizOrders.length} done=${summary.done} failed=${summary.failed} retry=${summary.retry}`
|
||
+ `${omsError ? ` error=${omsError.message}` : ''}`
|
||
);
|
||
|
||
return summary;
|
||
}
|
||
|
||
/**
|
||
* 拉取并消费待分配队列。
|
||
* @param {Object} ctx Egg 上下文。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
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),
|
||
},
|
||
},
|
||
});
|
||
|
||
// 1.1) 兜底恢复:OMS 取号中途进程中断遗留的 waiting_oms 行退回 pending 重新取号
|
||
await queueModel.update({
|
||
status: 'pending',
|
||
updated_at: new Date(),
|
||
}, {
|
||
where: {
|
||
status: 'waiting_oms',
|
||
updated_at: {
|
||
[Op.lt]: new Date(Date.now() - WAITING_OMS_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 {
|
||
const pipelineResult = await expressService.createWaybillCodePipeline(orders, {
|
||
awaitTasks: true,
|
||
warehouseId: group.warehouseId,
|
||
});
|
||
|
||
// 需要回 OMS 取号的订单已由流水线分流:对应队列行不参与本地分配快递,
|
||
// 转由 express_platform.oms 调用 OMS deliveryorder.getwaybill 取号并回写运单。
|
||
const needOmsWaybillOrderIdSet = new Set(
|
||
(pipelineResult && Array.isArray(pipelineResult.skipped_need_oms_waybill_order_ids)
|
||
? pipelineResult.skipped_need_oms_waybill_order_ids
|
||
: [])
|
||
.map(orderId => Number(orderId))
|
||
.filter(orderId => Number.isInteger(orderId) && orderId > 0)
|
||
);
|
||
const waitingRows = group.rows.filter(row => needOmsWaybillOrderIdSet.has(Number(row.order_id)));
|
||
const allocatableRows = group.rows.filter(row => !needOmsWaybillOrderIdSet.has(Number(row.order_id)));
|
||
let omsSummary = null;
|
||
if (waitingRows.length > 0) {
|
||
await updateQueueRowsStatus(ctx, waitingRows, 'waiting_oms', '等待外部OMS取号');
|
||
ctx.logger.info(`[consume-waybill-allocate-queue] 组内订单需要回 OMS 取号,转 OMS 取号 warehouse_id=${group.warehouseId} store_id=${group.storeId} waiting=${waitingRows.length}`);
|
||
omsSummary = await consumeOmsWaybillRows(ctx, group, waitingRows);
|
||
}
|
||
|
||
// 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 = allocatableRows.filter(row => doneKeySet.has(`${Number(row.order_id)}:${Number(row.business_key) || 1}`));
|
||
const restRows = allocatableRows.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} waiting_oms=${waitingRows.length}${omsSummary ? ` oms_done=${omsSummary.done} oms_failed=${omsSummary.failed} oms_retry=${omsSummary.retry}` : ''}`);
|
||
} 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);
|
||
}
|
||
}
|
||
}
|