feat(waybill): OMS 取号与抖音打印 params 按店铺隔离

- 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>
This commit is contained in:
liufumingren 2026-11-10 02:53:15 +08:00
parent 3c0c7634c5
commit 25dac21b4b
12 changed files with 2136 additions and 136 deletions

View File

@ -87,6 +87,9 @@ const LINE_BREAK_MAP = Object.freeze({
CRLF: '\r\n',
});
/** 快手加密密钥(wms_order_express.encryptprintinfo)长度上限:实测 172 字符,密文为数千字符。 */
const KUAISHOU_ENCRYPT_KEY_MAX_LENGTH = 512;
/**
* 拼多多自定义区默认模板地址。
* @type {string}
@ -971,6 +974,36 @@ function collectCainiaoMainContentCandidates(payload) {
return candidateList;
}
/**
* 用平铺密文字段回拼菜鸟加密面单主 content。
*
* 部分来源(如 OMS 取号)把菜鸟面单拆成 `encryptprintinfo` / `signature` / `ver` 平铺返回,
* `printinfo` 为空或只放了裸密文;此处回拼成与 printinfo 整包同构的
* `{ encryptedData, signature, ver }`,避免打印退化成模板兜底。
*
* @param {Object} orderExpress 快递单对象。
* @return {Object|null} 加密面单主 content;缺少密文时返回 null。
*/
function buildCainiaoEncryptContentFromOrderExpress(orderExpress = {}) {
const safeOrderExpress = isPlainObject(orderExpress) ? orderExpress : {};
const encryptedData = toTrimmedString(safeOrderExpress.encryptprintinfo);
if (!encryptedData) {
return null;
}
const content = { encryptedData };
const signature = toTrimmedString(safeOrderExpress.signature);
if (signature) {
content.signature = signature;
}
const version = toTrimmedString(safeOrderExpress.ver);
if (version) {
content.ver = version;
}
return content;
}
/**
* 解析菜鸟 printinfo 并归一化主面单 content。
*
@ -985,10 +1018,11 @@ function collectCainiaoMainContentCandidates(payload) {
*
* @param {any} printinfo 原始 printinfo。
* @param {string[]} warnings 风险提示列表。
* @param {boolean} [warnOnFailure=true] printinfo 解析失败时是否记录告警(调用方另有兜底时可关闭)。
* @return {Object|null} 解析后的主面单 content;失败返回 null。
*/
function resolveCainiaoMainContentFromPrintinfo(printinfo, warnings = []) {
const payload = parseCainiaoPrintinfoObject(printinfo, warnings, true);
function resolveCainiaoMainContentFromPrintinfo(printinfo, warnings = [], warnOnFailure = true) {
const payload = parseCainiaoPrintinfoObject(printinfo, warnings, warnOnFailure);
if (!payload) {
return null;
}
@ -1505,6 +1539,10 @@ function applyKuaishouExpressFieldMapping(platform, skeleton, orderExpress, orde
}
if (!key) {
warnings.push('快手模板字段 key 缺失映射源(order_express.encryptprintinfo)');
} else if (key.length > KUAISHOU_ENCRYPT_KEY_MAX_LENGTH || key === encryptedData) {
// 快手密钥为逐单短串(实测 172 字符),密文是数 KB 的十六进制串;
// 命中说明上游把密文写进了 encryptprintinfo、且没有返回真正的 key
warnings.push('快手模板字段 key 疑似为密文而非密钥(上游未返回 key),打印可能失败');
}
}
@ -1882,9 +1920,22 @@ function applyCainiaoExpressFieldMapping(
return;
}
const cainiaoMainContent = resolveCainiaoMainContentFromPrintinfo(safeOrderExpress.printinfo, warnings);
// printinfo 缺失或不是可识别的 JSON 时(OMS 取号只给平铺密文),用密文字段回拼加密面单 content
const cainiaoEncryptContent = buildCainiaoEncryptContentFromOrderExpress(safeOrderExpress);
// 有密文可回拼时不再报「回退模板骨架」,避免误导
const cainiaoMainContent = resolveCainiaoMainContentFromPrintinfo(
safeOrderExpress.printinfo,
warnings,
!cainiaoEncryptContent
);
const hasPrintinfoMainContent = isPlainObject(cainiaoMainContent);
if (cainiaoEncryptContent && !hasPrintinfoMainContent) {
warnings.push('菜鸟 printinfo 缺失或非 JSON,已用 encryptprintinfo + signature + ver 回拼加密面单 content');
} else if (!cainiaoEncryptContent && !hasPrintinfoMainContent) {
warnings.push('菜鸟 printinfo 为空且缺少 encryptprintinfo,已回退模板骨架');
}
const waybillCode = toTrimmedString(safeOrderExpress.waybill_code);
const cainiaoCpCode = resolveCainiaoCpCode(safeOrderExpress, safeOrderDelivery);
const cainiaoMainTemplateURL = resolveCainiaoMainTemplateURL(safeOrderExpress);
@ -1943,9 +1994,15 @@ function applyCainiaoExpressFieldMapping(
}
}
// 模板样例里的主面单模板地址:content 被整体替换(加密面单/模板兜底)后仍要保留兜底来源
const skeletonMainTemplateURL = toTrimmedString(mainContent.templateURL || mainContent.templateUrl);
if (hasPrintinfoMainContent) {
mainContent = cloneStructuredValue(cainiaoMainContent);
contents[0] = mainContent;
} else if (cainiaoEncryptContent) {
mainContent = cloneStructuredValue(cainiaoEncryptContent);
contents[0] = mainContent;
} else {
mainContent = cloneStructuredValue(mainContent);
contents[0] = mainContent;
@ -1955,7 +2012,7 @@ function applyCainiaoExpressFieldMapping(
if (isPlainObject(mainContent.data)) {
mainContent.data = ensureCainiaoMainDataSource(mainContent.data, 'printinfo');
}
} else {
} else if (!cainiaoEncryptContent) {
mainContent.data = ensureCainiaoMainDataSource(
buildCainiaoFallbackMainData(mainContent.data),
'template'
@ -1969,6 +2026,11 @@ function applyCainiaoExpressFieldMapping(
if (cainiaoMainTemplateURL) {
mainContent.templateURL = cainiaoMainTemplateURL;
} else if (!toTrimmedString(mainContent.templateURL) && skeletonMainTemplateURL) {
// 接口未配置模板地址时退回模板样例地址,避免 content 无 templateURL 导致渲染失败
mainContent.templateURL = skeletonMainTemplateURL;
} else if (!toTrimmedString(mainContent.templateURL)) {
warnings.push('菜鸟主面单缺少 templateURL(接口未配置 template_url 且模板样例为空)');
}
if (!toTrimmedString(customControlTemplateURL) || !customControlText) {

View File

@ -24,10 +24,15 @@ const AUTO_INCREMENT_SAFE_MARGIN = 50;
* 对应的索引在 dispatch 分表中需要额外携带 dispatch_lane 才能覆盖 count 统计
* (否则统计仍需逐行回表核对 dispatch_lane),因此 dispatch 分支需要识别并
* 剔除从 current 继承来的同名后缀索引,改用自身版本,避免同一张表维护两份功能重叠的索引。
*
* 带 `_list_logistics_code_` 前缀的是「物流公司编码等值过滤 + 排序」变体(见 buildListSortIndexes),
* 同样需要按分表类型区分版本,故一并登记后缀。
*/
const LIST_SORT_INDEX_SUFFIXES = Object.freeze([
'_list_updated_at',
'_list_delivery_time',
'_list_logistics_code_updated_at',
'_list_logistics_code_delivery_time',
]);
const TABLE_PATTERNS = Object.freeze([
@ -557,6 +562,12 @@ function buildTableOptions(app, warehouseId, type) {
* 生产 wms_order_delivery_1000028_dispatch(59 万行 / 1.1GB)实测单请求因此达到 2 秒,
* 故在此显式声明 updated_at(前端请求的实际排序字段)与 delivery_time(deliveryOrder 模块默认排序字段)两份索引。
*
* 除无条件列表外,工作台列表还常带 logistics_code 等值过滤(如 order.deliveryOrder.list 的
* `where.logistics_code = 'JD'`)。该列区分度很低(几十个承运商编码覆盖整表),单列索引
* 会把大面积行捞出来再逐行回表核对仓库/子域,反而比全表扫更慢;因此把等值列插在 scopeFields
* 与排序列之间,让「等值过滤 + 排序」命中同一个索引:过滤区间内索引天然按排序列有序,
* 列表无需 filesort,COUNT(*) 也退化为纯索引范围扫描。
*
* @param {number|string} warehouseId 仓库ID。
* @param {string} type 模型类型。
* @return {Array<Object>} 列表排序索引选项。
@ -568,17 +579,21 @@ function buildListSortIndexes(warehouseId, type) {
// dispatch 分表的 where 恒定带 dispatch_lane(m4/m5),把该常量列放进索引前缀
// 才能让 COUNT(*) 变成纯索引扫描;非 dispatch 分表没有该列。
const scopeFields = dispatchType ? [ 'warehouse_id', 'dispatch_lane' ] : [ 'warehouse_id' ];
const sortFields = [ 'updated_at', 'delivery_time' ];
return [
{
name: `${namePrefix}_list_updated_at`,
fields: [ ...scopeFields, 'updated_at', 'id' ],
},
{
name: `${namePrefix}_list_delivery_time`,
fields: [ ...scopeFields, 'delivery_time', 'id' ],
},
];
const indexes = sortFields.map(sortField => ({
name: `${namePrefix}_list_${sortField}`,
fields: [ ...scopeFields, sortField, 'id' ],
}));
for (const sortField of sortFields) {
indexes.push({
name: `${namePrefix}_list_logistics_code_${sortField}`,
fields: [ ...scopeFields, 'logistics_code', sortField, 'id' ],
});
}
return indexes;
}
/**
@ -663,6 +678,14 @@ function buildIndexOptions(warehouseId, type) {
name: `idx_${prefix}_wave`,
fields: [ 'status', 'wave_id' ],
},
// 波次号是列表高频过滤条件(如 order.deliveryOrder.list 的 where.wave_no 等值查询),
// 而 idx_{prefix}_wave 的前导列 status 不在谓词中,无法支撑 wave_no 过滤;
// wave_no 在表内近似唯一(生产 1000028_dispatch 约 1.49M 行 / 10.25 万个波次号),
// 单列索引即可把「扫半个表再逐行过滤」降为十几行的点查。
{
name: `idx_${prefix}_wave_no`,
fields: [ 'wave_no' ],
},
{
name: `idx_${prefix}_statuses`,
fields: [

View File

@ -6,7 +6,7 @@
* @return {import('sequelize').ModelCtor<import('sequelize').Model>} Sequelize 模型。
*/
module.exports = app => {
const { BIGINT, STRING, DATE, ENUM, INTEGER, TINYINT, TEXT, DECIMAL } = app.Sequelize;
const { BIGINT, STRING, DATE, ENUM, INTEGER, TINYINT, TEXT } = app.Sequelize;
const OrderExpress = app.model.define('order_express', {
id: {
@ -117,6 +117,16 @@ module.exports = app => {
allowNull: true,
comment: '加密令牌',
},
print_history_id: {
type: STRING(64),
allowNull: true,
comment: 'OMS 打印记录ID(打印前换新 params 用)',
},
cai_niao_auth_info_id: {
type: BIGINT(20).UNSIGNED,
allowNull: true,
comment: '菜鸟授权网点ID(OMS 取号返回,打印按店铺分组用)',
},
warehouse_id: {
type: BIGINT(20).UNSIGNED,
allowNull: true,

View File

@ -15,6 +15,7 @@ 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 = {
@ -120,6 +121,185 @@ async function markGroupRetryOrFailed(ctx, rows, errorMessage, failImmediately)
}
}
/**
* 消费需要回 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 上下文。
@ -148,6 +328,19 @@ async function consumePendingRows(ctx) {
},
});
// 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({
@ -220,8 +413,8 @@ async function consumePendingRows(ctx) {
warehouseId: group.warehouseId,
});
// 需要回 OMS 取号的订单已由流水线分流:对应队列行置等待终态,不参与本地重试,
// 等外部 OMS 取号能力开发完成后再统一处理。
// 需要回 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
@ -231,9 +424,11 @@ async function consumePendingRows(ctx) {
);
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 取号,已置等待态 warehouse_id=${group.warehouseId} store_id=${group.storeId} waiting=${waitingRows.length}`);
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)的订单视为完成(仅统计参与本地取号的队列行)
@ -262,7 +457,7 @@ async function consumePendingRows(ctx) {
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}`);
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);

View File

@ -2409,11 +2409,39 @@ class PendingOrderService extends OrderBaseService {
warehouseId: capturedWarehouseId,
});
// 需要回 OMS 取号的订单会被本地流水线整体跳过(不分配本地快递),
// 这里改由 OMS 取号并回写运单号。
// 与原设计保持一致:取号结果(含失败)不影响任务状态——失败已通过订单异常与
// 操作日志体现,任务只表示「分配流程已执行完毕」。
const skippedOmsOrderIds = Array.isArray(result && result.skipped_need_oms_waybill_order_ids)
? result.skipped_need_oms_waybill_order_ids.map(orderId => Number(orderId))
: [];
let omsResult = null;
let omsError = '';
if (skippedOmsOrderIds.length > 0) {
try {
omsResult = await service.systems.express.requestOmsWaybills(
orders.filter(order => skippedOmsOrderIds.includes(Number(order.order_id))),
{ warehouseId: capturedWarehouseId }
);
} catch (error) {
omsError = error && error.message ? String(error.message) : String(error);
this.ctx.logger.warn('[PendingOrderService.allocateExpressCode] 回 OMS 取号失败,已按订单异常记录', {
orderIds: skippedOmsOrderIds,
error: omsError,
});
}
}
await this.setAllocateExpressTaskProgress(taskId, {
status: 'success',
progress: ALLOCATE_EXPRESS_PROGRESS_MAP.success,
message: '快递分配完成',
result,
result: {
...(result || {}),
...(omsResult ? { oms: omsResult } : {}),
...(omsError ? { oms_error: omsError } : {}),
},
});
} catch (error) {
const errorMessage = error && error.message ? error.message : String(error);
@ -7199,6 +7227,30 @@ class PendingOrderService extends OrderBaseService {
{ awaitTasks: true }
);
// 需要回 OMS 取号的订单会被本地流水线整体跳过(含全部 business_key),
// 这里改由 OMS 取号;OMS 每次真实取号并透传 printinfo/signature,
// 因此同一订单的不同包裹能各自拿到面单。
const skippedSubOmsOrderIds = Array.isArray(pipelineResult && pipelineResult.skipped_need_oms_waybill_order_ids)
? pipelineResult.skipped_need_oms_waybill_order_ids.map(orderId => Number(orderId))
: [];
let omsSubResult = null;
let omsSubError = '';
if (skippedSubOmsOrderIds.length > 0) {
try {
omsSubResult = await this.service.systems.express.requestOmsWaybills(
ordersForPipeline.filter(order => skippedSubOmsOrderIds.includes(Number(order.order_id))),
{ warehouseId: orderWarehouseId }
);
} catch (error) {
omsSubError = error && error.message ? String(error.message) : String(error);
this.ctx.logger.warn('[pendingOrder.allocateSubOrderNumber] 回 OMS 取号失败,已按订单异常记录', {
orderId,
businessKeys,
error: omsSubError,
});
}
}
// 回查最终分配结果
const updatedDetails = await this.buildSubOrderDetails(orderId, orderWarehouseId, businessKeys);
@ -7215,6 +7267,8 @@ class PendingOrderService extends OrderBaseService {
result: {
...taskPayload.result,
pipeline_result: pipelineResult,
...(omsSubResult ? { oms: omsSubResult } : {}),
...(omsSubError ? { oms_error: omsSubError } : {}),
details: updatedDetails,
},
});

View File

@ -2924,20 +2924,24 @@ class DeliveryOrderService extends Service {
});
}
// 需要回 OMS 取号的订单(仅 openapi 创建来源可能为 1)先做分流:
// 不进入本地自动分配快递队列,写「等待外部 OMS 取号」日志;
// 等外部 OMS 取号能力开发完成后,再在此处接入实际取号/运单回写。
// 需要回 OMS 取号的订单(仅 openapi 创建来源可能为 1):本地流水线不会给它分配快递,
// 这里仅记录标记;是否自动发起取号仍由货主-仓库「自动分配快递」开关决定,
// 开关关闭时只能由人工触发「分配快递号」。
const needOmsWaybill = this.normalizeNeedOmsWaybill(context.order && context.order.need_oms_waybill) === 1;
if (needOmsWaybill) {
this.ctx.logger.info('[DeliveryOrderService.asyncProcessOrderCreated] 订单需要回 OMS 取号,跳过本地自动分配快递', {
orderId: context.orderId,
warehouseId: context.warehouseId,
storeId: context.storeId,
autoAllocateExpressEnabled: isAutoAllocateExpressEnabled,
});
await this.runAsyncProcessStep('appendNeedOmsWaybillWaitLog', async () => {
await this.appendNeedOmsWaybillWaitLog(context);
});
} else if (isAutoAllocateExpressEnabled) {
}
if (isAutoAllocateExpressEnabled) {
await this.runAsyncProcessStep('allocateExpress', async () => {
// 不再逐单直接取号:改为写入消费队列,由定时任务按仓库+货主串行批量消费,
// 避免大批量订单创建时 N 条并发取号事务的 AB-BA 死锁。截单/未审核订单在此处不会入队。

View File

@ -2341,6 +2341,178 @@ class ExpressService extends Service {
}
}
/**
* 对指定订单回 OMS 取号(need_oms_waybill=1)。
*
* 内部完成「查订单 → 组报文 → 调 oms 平台方法 → 写回运单号」,
* 供队列消费与手工「分配快递号」共用。
*
* @param {Array<Object>} orders 订单列表([{ order_id, business_key }])。
* @param {Object} [options={}] 上下文。
* @param {number} options.warehouseId 仓库 ID。
* @return {Promise<Object>} 平台返回结果。
*/
async requestOmsWaybills(orders = [], options = {}) {
const warehouseId = Number(options.warehouseId) || 0;
const normalizedOrders = (Array.isArray(orders) ? orders : [])
.map(order => ({
order_id: Number(order && (order.order_id || order.id)) || 0,
business_key: Number(order && order.business_key) || 1,
}))
.filter(order => Number.isInteger(order.order_id) && order.order_id > 0);
if (warehouseId <= 0 || normalizedOrders.length === 0) {
return { flag: 'success', code: 0, message: '无需要回 OMS 取号的订单' };
}
const { Op } = this.app.Sequelize;
const orderModel = await this.getWaybillAsyncOrderDeliveryModel(warehouseId);
if (!orderModel || typeof orderModel.findAll !== 'function') {
throw new Error('订单主表模型未初始化,无法回 OMS 取号');
}
const orderRows = await orderModel.findAll({
where: {
warehouse_id: warehouseId,
id: {
[Op.in]: Array.from(new Set(normalizedOrders.map(order => order.order_id))),
},
},
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 payloadOrders = [];
const missingOrderIds = [];
for (const order of normalizedOrders) {
const orderRow = orderMap.get(order.order_id);
if (!orderRow) {
missingOrderIds.push(order.order_id);
continue;
}
payloadOrders.push({
order_id: order.order_id,
business_key: order.business_key,
order_delivery: orderRow,
});
}
if (payloadOrders.length === 0) {
throw new Error(`订单不存在或已流转到其它分表: ${missingOrderIds.join(',')}`);
}
try {
return await this.dispatchWaybillAsyncPlatform('oms', {
warehouse_id: warehouseId,
store_id: Number(payloadOrders[0].order_delivery.store_id) || 0,
orders: payloadOrders,
});
} catch (error) {
// 与本地平台失败口径一致:标记订单异常、写失败日志、清理未完成占位行
await this.handleWaybillAllocateFailure(
payloadOrders.map(order => ({
order_id: order.order_id,
business_key: order.business_key,
})),
{ warehouse_id: warehouseId, sys_platform_code: 'oms' },
error && error.message ? error.message : String(error)
).catch(failureError => {
this.ctx.logger.error('[ExpressService.requestOmsWaybills] 失败收尾处理异常', {
error: failureError && failureError.message ? failureError.message : `${failureError}`,
});
});
throw error;
}
}
/**
* 统一的运单分配失败收尾:标记订单异常、写失败日志、清理未完成的快递占位行。
*
* 本地平台(asyncTask)与回 OMS 取号(requestOmsWaybills / 队列消费)共用同一口径,
* 避免第三方取号失败只体现在任务进度里、订单侧看不到异常。
*
* @param {Array<Object>} orders 订单列表([{ order_id, business_key }])。
* @param {Object} [options={}] 上下文。
* @param {number} [options.warehouse_id] 仓库 ID。
* @param {string} [options.sys_platform_code] 平台编码(OMS 场景传 'oms')。
* @param {string} errorMessage 失败原因。
* @return {Promise<Array<number>>} 被标记失败的订单 ID 列表。
*/
async handleWaybillAllocateFailure(orders = [], options = {}, errorMessage = '') {
const errOrders = (Array.isArray(orders) ? orders : []).map(order => ({
order_id: order && (order.order_id || order.id),
business_key: order && order.business_key,
}));
const allOrderIds = errOrders
.map(order => Number(order && order.order_id))
.filter(orderId => Number.isFinite(orderId) && orderId > 0);
if (allOrderIds.length === 0) {
return [];
}
// 查询已成功分配(status=1)的订单,从失败列表中排除,避免误标异常
const successOrderIdSet = new Set();
const orderExpressModel = this.ctx.model && this.ctx.model.OrderExpress;
if (orderExpressModel) {
try {
// 强制同仓校验:读取运单状态必须同时限定订单ID与仓库ID,禁止跨仓串读
const statusCheckWarehouseId = Number(options && options.warehouse_id)
|| this.getCurrentWarehouseId();
const successRows = await orderExpressModel.findAll({
where: {
order_id: { [this.app.Sequelize.Op.in]: allOrderIds },
status: 1,
warehouse_id: statusCheckWarehouseId,
},
attributes: [ 'order_id' ],
raw: true,
});
for (const row of Array.isArray(successRows) ? successRows : []) {
const successOrderId = Number(row && row.order_id);
if (Number.isFinite(successOrderId) && successOrderId > 0) {
successOrderIdSet.add(successOrderId);
}
}
} catch (statusCheckError) {
this.ctx.logger.error('[ExpressService.handleWaybillAllocateFailure] 查询已成功订单状态失败,降级为全量标记', {
error: statusCheckError && statusCheckError.message ? statusCheckError.message : `${statusCheckError}`,
});
}
}
const failedOrderIds = allOrderIds.filter(orderId => !successOrderIdSet.has(orderId));
if (failedOrderIds.length === 0) {
return [];
}
const logWarehouseId = this.getOrderLogWarehouseId(options && options.warehouse_id);
const message = String(errorMessage || '运单分配失败');
this.service.systems.orderPendingException.markPendingException({
ids: failedOrderIds,
exception_text: `订单快递分配失败: ${message}`,
});
await this.createAllocateExpressFailureLogs(failedOrderIds, message, logWarehouseId);
await this.hardDeleteWaybillAsyncOrderExpressRows(errOrders, {
platform_code: options && options.sys_platform_code,
error_message: message,
});
return failedOrderIds;
}
/**
* 解析运单后处理分发平台编码。
*
@ -2832,68 +3004,19 @@ class ExpressService extends Service {
error: error && error.message ? error.message : `${error}`,
stack: error && error.stack ? error.stack : '',
});
const err_orders = batch.map(order => ({
order_id: order && (order.id || order.order_id),
business_key: order.business_key,
}));
const allOrderIds = err_orders
.map(order => Number(order && order.order_id))
.filter(orderId => Number.isFinite(orderId) && orderId > 0);
// 查询已成功分配(status=1)的订单,从失败列表中排除,避免误标异常
const successOrderIdSet = new Set();
const orderExpressModel = this.ctx.model && this.ctx.model.OrderExpress;
if (orderExpressModel && allOrderIds.length > 0) {
try {
// 强制同仓校验:读取运单状态必须同时限定订单ID与仓库ID,禁止跨仓串读
const statusCheckWarehouseId = Number(options && options.warehouse_id)
|| this.getCurrentWarehouseId();
const successRows = await orderExpressModel.findAll({
where: {
order_id: { [this.app.Sequelize.Op.in]: allOrderIds },
status: 1,
warehouse_id: statusCheckWarehouseId,
},
attributes: [ 'order_id' ],
raw: true,
});
for (const row of Array.isArray(successRows) ? successRows : []) {
const successOrderId = Number(row && row.order_id);
if (Number.isFinite(successOrderId) && successOrderId > 0) {
successOrderIdSet.add(successOrderId);
}
}
if (successOrderIdSet.size > 0) {
this.ctx.logger.info('[ExpressService.asyncTask] 排除已成功分配的订单,不标记异常', {
traceId,
successOrderIds: Array.from(successOrderIdSet),
});
}
} catch (statusCheckError) {
this.ctx.logger.error('[ExpressService.asyncTask] 查询已成功订单状态失败,降级为全量标记', {
traceId,
error: statusCheckError && statusCheckError.message ? statusCheckError.message : `${statusCheckError}`,
});
}
}
const failedOrderIds = allOrderIds.filter(orderId => !successOrderIdSet.has(orderId));
const logWarehouseId = this.getOrderLogWarehouseId(options && options.warehouse_id);
const errorMessage = error && error.message ? String(error.message) : String(error);
if (failedOrderIds.length > 0) {
this.service.systems.orderPendingException.markPendingException(
{
ids: failedOrderIds,
exception_text: `订单快递分配失败: ${errorMessage}`,
}
);
}
await this.createAllocateExpressFailureLogs(failedOrderIds, errorMessage, logWarehouseId);
await this.hardDeleteWaybillAsyncOrderExpressRows(err_orders, {
platform_code: options.sys_platform_code,
error_message: errorMessage,
});
await this.handleWaybillAllocateFailure(
batch.map(order => ({
order_id: order && (order.id || order.order_id),
business_key: order && order.business_key,
})),
{
warehouse_id: options && options.warehouse_id,
sys_platform_code: options && options.sys_platform_code,
},
errorMessage
);
}
}
/**

View File

@ -3,9 +3,83 @@
const MainService = require('../main');
const PddClient = require('../../lib/pdd_client');
const crypto = require('node:crypto');
const DEFAULT_PDD_ROUTER_URL = 'https://gw-api.pinduoduo.com/api/router';
const DEFAULT_TB_ROUTER_URL = 'https://eco.taobao.com/router/rest';
/** OMS 取号方法名(OMS 侧 openapi method)。 */
const OMS_WAYBILL_EVENT = 'deliveryorder.getwaybill';
/** OMS 单次取号上限(printinfo 单张可达数百 KB,文档建议不超过 20 单)。 */
const OMS_WAYBILL_MAX_BATCH_SIZE = 20;
/**
* OMS 逐单失败原因中「不可重试」的关键词(对应 OMS 文档 4.4)。
* 命中即认为重试无意义,避免反复打平台。
*/
const OMS_WAYBILL_NON_RETRYABLE_KEYWORDS = Object.freeze([
'缺少发货单号',
'缺少快递编码',
'未找到发货单',
'不属于当前 app_key',
'已取消',
'已作废',
'已归档',
'订单已配送',
'订单已完成',
'缺少来源平台',
'未关联店铺',
'未关联外部打单账号',
'外部打单账号或平台配置不存在',
'未绑定到快递接口',
'不存在(系统快递编码',
'已停用',
'未开启回平台取号',
'真实取号已关闭',
'未找到订单',
'面单余额不足',
'已发货或已终结',
]);
/**
* 上游未返回时必须保留已有值的运单报文字段。
*
* OMS 回源取号只透传 signature/encryptprintinfo/ver,不返回 printinfo/encryptedtoken,
* 若按 null 写入会把本地取号已落库的有效报文冲掉,故这类字段统一改为「有值才覆盖」。
*/
const ORDER_EXPRESS_UPSTREAM_OPTIONAL_FIELDS = Object.freeze([
'printinfo',
'signature',
'encryptprintinfo',
'encryptedtoken',
'ver',
]);
/** OMS 取号请求/返回报文缓存 TTL(秒):覆盖一个工作日,便于事后核对报文格式。 */
const OMS_WAYBILL_PAYLOAD_CACHE_TTL = 24 * 60 * 60;
/** OMS 取号报文缓存单键上限(字节):超出时按字段脱敏,避免 Redis 单键过大。 */
const OMS_WAYBILL_PAYLOAD_CACHE_MAX_BYTES = 256 * 1024;
/**
* 报文超限时按字段名脱敏的密文/大字段。
* 只截断取值,保留字段名与总长度,保证缓存里仍能看清报文结构。
*/
const OMS_WAYBILL_PAYLOAD_HEAVY_FIELDS = Object.freeze([
'printinfo',
'printdata',
'encryptprintinfo',
'encryptedtoken',
'signature',
]);
/** 大字段脱敏后保留的字符数(用于人工核对字段内容前缀)。 */
const OMS_WAYBILL_PAYLOAD_HEAVY_FIELD_KEEP_CHARS = 64;
/** OMS 取号报文缓存键前缀。 */
const OMS_WAYBILL_PAYLOAD_CACHE_KEY_PREFIX = 'oms:waybill';
/**
* 将任意值标准化为数组。
*
@ -2433,7 +2507,7 @@ class ExpressPlatformService extends MainService {
throw new Error('平台接口返回 object_id 无法解析');
}
await this.ctx.model.OrderExpress.update(item.orderExpressValues, {
await this.ctx.model.OrderExpress.update(this.dropEmptyOrderExpressFields(item.orderExpressValues), {
where: {
order_id: item.order_id,
business_key: item.business_key,
@ -2869,6 +2943,796 @@ class ExpressPlatformService extends MainService {
throw error;
}
}
/**
* 规范化 OMS 取号订单项。
*
* 兼容两种入参形态:
* - 队列/流水线风格:`{ order_id, business_key, order_delivery: {...} }`
* - 展平风格:直接把订单字段放在数组项上
*
* @param {Array<Object>} orders 订单列表。
* @return {Array<Object>} 规范化后的订单项。
*/
normalizeOmsWaybillOrders(orders = []) {
const normalized = [];
for (const item of normalizeArray(orders)) {
if (!item || typeof item !== 'object') {
continue;
}
const orderDelivery = item.order_delivery && typeof item.order_delivery === 'object'
? item.order_delivery
: item;
const orderId = Number(orderDelivery.id || orderDelivery.order_id || item.order_id) || 0;
if (!Number.isInteger(orderId) || orderId <= 0) {
continue;
}
const businessKey = Number(item.business_key || item.businessKey) || 1;
const expressCode = String(
orderDelivery.logistics_code
|| orderDelivery.sys_logistics_code
|| orderDelivery.express_code
|| ''
).trim().toUpperCase();
normalized.push({
order_id: orderId,
business_key: businessKey,
delivery_order_code: String(
orderDelivery.delivery_order_code || orderDelivery.deliveryOrderCode || ''
).trim(),
express_code: expressCode,
store_id: Number(orderDelivery.store_id) || 0,
warehouse_id: Number(orderDelivery.warehouse_id) || 0,
source_order_codes: orderDelivery.source_order_codes || null,
from_key: String(orderDelivery.from_key || '').trim(),
// 打印按来源平台(wms_print_platform.platform_code)匹配模板,不能落 'oms'
platform_code: String(
orderDelivery.sys_platform_code || orderDelivery.source_platform_code || ''
).trim().toUpperCase(),
});
}
return normalized;
}
/**
* 加载「系统平台编码 -> 打印平台编码」映射。
*
* 与 `express.js createWaybillCodePipeline` 同源同参(`getPlatformList(true, true)`),
* 保证本地取号与 OMS 回源取号算出的平台码口径一致。
* 映射缺失时返回空 Map,调用方按原值兜底,不影响取号主流程。
*
* @return {Promise<Map<string, string>>} 系统平台编码(大写)到打印平台编码(大写)的映射。
*/
async buildWaybillPrintPlatformCodeMap() {
const platformService = this.service && this.service.systems && this.service.systems.platform;
if (!platformService || typeof platformService.getPlatformList !== 'function') {
this.ctx.logger.warn('[ExpressPlatformService.buildWaybillPrintPlatformCodeMap] platform 服务不可用,平台码不做归一');
return new Map();
}
let platformList = [];
try {
platformList = await platformService.getPlatformList(true, true);
} catch (error) {
this.ctx.logger.warn('[ExpressPlatformService.buildWaybillPrintPlatformCodeMap] 平台列表加载失败,平台码不做归一', {
error: error && error.message ? error.message : `${error}`,
});
return new Map();
}
const printPlatformCodeMap = new Map();
for (const platform of normalizeArray(platformList)) {
const platformCode = String(platform && platform.code || '').trim().toUpperCase();
const printPlatformCode = String(platform && platform.default_express_interface || '').trim().toUpperCase();
if (platformCode && printPlatformCode) {
printPlatformCodeMap.set(platformCode, printPlatformCode);
}
}
return printPlatformCodeMap;
}
/**
* 将平台码归一为打印平台编码。
*
* 落库口径统一为 `wms_print_platform.platform_code`:
* 本地取号取的是 `wms_express_interface.platform_code`(已是打印平台码),
* OMS 回源取号的来源平台码(如 DYXD)必须映射成同一编码后再落库。
*
* @param {string} platformCode 原始平台码。
* @param {Map<string, string>} [printPlatformCodeMap] 平台码映射。
* @return {string} 归一后的平台码;无法归一或为空时返回原值(大写)。
*/
resolveWaybillPrintPlatformCode(platformCode, printPlatformCodeMap = new Map()) {
const normalizedPlatformCode = String(platformCode || '').trim().toUpperCase();
if (!normalizedPlatformCode) {
return '';
}
const mappedPlatformCode = printPlatformCodeMap instanceof Map
? printPlatformCodeMap.get(normalizedPlatformCode)
: '';
return String(mappedPlatformCode || normalizedPlatformCode).trim().toUpperCase();
}
/**
* 就地归一 OMS 取号订单项的平台码。
*
* @param {Array<Object>} items 归一化后的 OMS 取号订单项。
* @param {Map<string, string>} [printPlatformCodeMap] 平台码映射。
* @return {Array<Object>} 归一后的订单项。
*/
applyWaybillPrintPlatformCodes(items = [], printPlatformCodeMap = new Map()) {
for (const item of normalizeArray(items)) {
if (!item || typeof item !== 'object') {
continue;
}
item.platform_code = this.resolveWaybillPrintPlatformCode(item.platform_code, printPlatformCodeMap);
}
return items;
}
/**
* 为 OMS 淘宝(菜鸟)运单回拼 printinfo 整包。
*
* OMS 取号把菜鸟加密面单拆成 `signature` / `encryptprintinfo` / `ver` 平铺返回,
* `printinfo` 恒为空串;而 WMS 菜鸟打印只认 `printinfo` 里的
* `{ encryptedData, signature, ver }`(见 app/lib/printer_data_builder.js 的菜鸟分支)。
* 不回拼会导致打印退化成模板兜底(`_dataFrom='template'`),与菠萝派取号单输出不一致。
*
* 取值原样保留密文前缀(`AES:` / `MD:`),不做解码。
*
* @param {string} platformCode 归一后的平台编码。
* @param {Object} orderExpressValues 待写入的运单字段集合。
* @return {string} 可直接落库的 printinfo JSON;不适用时返回空字符串。
*/
buildOmsCainiaoPrintinfo(platformCode, orderExpressValues = {}) {
const normalizedPlatformCode = String(platformCode || '').trim().toUpperCase();
// CAINIAO 为归一后的打印平台码;TB 为归一失败时的原始码(打印侧同样归一为菜鸟)
if (normalizedPlatformCode !== 'CAINIAO' && normalizedPlatformCode !== 'TB') {
return '';
}
const values = orderExpressValues && typeof orderExpressValues === 'object' ? orderExpressValues : {};
if (String(values.printinfo || '').trim()) {
return '';
}
const encryptedData = String(values.encryptprintinfo || '').trim();
if (!encryptedData) {
return '';
}
const payload = { encryptedData };
const signature = String(values.signature || '').trim();
if (signature) {
payload.signature = signature;
}
const version = String(values.ver || '').trim();
if (version) {
payload.ver = version;
}
return JSON.stringify(payload);
}
/**
* 剔除上游未返回(null/undefined/空串)的运单报文字段。
*
* 命中的字段不做写入,交由数据库保留已有值,避免 OMS 回源取号把本地已落库的报文冲成空。
*
* @param {Object} [values={}] 待写入 `wms_order_express` 的字段集合。
* @return {Object} 剔除空值后的字段集合。
*/
dropEmptyOrderExpressFields(values = {}) {
const source = values && typeof values === 'object' ? values : {};
const sanitized = { ...source };
for (const field of ORDER_EXPRESS_UPSTREAM_OPTIONAL_FIELDS) {
if (!Object.prototype.hasOwnProperty.call(sanitized, field)) {
continue;
}
const value = sanitized[field];
if (value === null || value === undefined || String(value).trim() === '') {
delete sanitized[field];
}
}
return sanitized;
}
/**
* 按货主 + 仓库反查可用于回调 OMS 的 OpenAPI 应用配置。
*
* @param {number} storeId 货主 ID。
* @param {number} warehouseId 仓库 ID。
* @return {Promise<Object|null>} { appKey, selfAppSecret, callbackUrl } 或 null。
*/
async resolveOmsWaybillAppConfig(storeId, warehouseId) {
const orderAsyncService = this.service && this.service.systems && this.service.systems.orderAsync;
if (!orderAsyncService || typeof orderAsyncService.resolveOpenApiAppByStore !== 'function') {
return null;
}
return await orderAsyncService.resolveOpenApiAppByStore(storeId, warehouseId);
}
/**
* 为待取号订单解析回 OMS 用的 appKey。
*
* 优先使用订单 `from_key`(建单时落库的来源应用 appKey,即 api_app.self_app_key),
* 缺失时按「货主 + 仓库」反查授权关系下的启用 ApiApp。
*
* @param {Array<Object>} items 归一化后的订单项(就地写入 app_key)。
* @return {Promise<void>} 无返回值。
*/
async attachOmsWaybillAppKeys(items = []) {
const cache = new Map();
for (const item of normalizeArray(items)) {
if (item.from_key) {
item.app_key = item.from_key;
item.app_key_source = 'order.from_key';
continue;
}
const cacheKey = `${item.store_id}:${item.warehouse_id}`;
if (!cache.has(cacheKey)) {
const appConfig = await this.resolveOmsWaybillAppConfig(item.store_id, item.warehouse_id);
cache.set(cacheKey, String(appConfig && appConfig.appKey || '').trim());
}
item.app_key = cache.get(cacheKey);
item.app_key_source = 'store_warehouse.api_app';
}
}
/**
* 生成稳定的 OMS 幂等 request_id。
*
* 摘要输入采用与菠萝派/菜鸟/拼多多一致的包裹标识
* `warehouse_id(16进制)_order_id(16进制)_business_key`,
* 同批(同一组包裹)在重试时得到相同值,OMS 会直接回放上次响应,
* 避免重复调用打单平台产生重复面单。
*
* @param {number} warehouseId 仓库 ID(订单项缺 warehouse_id 时兜底)。
* @param {Array<Object>} items 本批订单项。
* @return {string} request_id(不超过 OMS 100 字符上限)。
*/
buildOmsWaybillRequestId(warehouseId, items = []) {
const objectIds = normalizeArray(items)
.map(item => {
const warehouseHex = (Number(item.warehouse_id) || Number(warehouseId) || 0).toString(16);
const orderHex = (Number(item.order_id) || 0).toString(16);
const businessKey = Number(item.business_key) || 1;
return `${warehouseHex}_${orderHex}_${businessKey}`;
})
.sort();
return `oms-getwaybill-${crypto.createHash('md5').update(objectIds.join(','), 'utf8').digest('hex')}`;
}
/**
* 生成 OMS 取号报文缓存的键集合。
*
* 除固定 latest 键外,再按 request_id 落一份,便于用日志里的 request_id 直接定位。
*
* @param {string} [requestId=''] 本次取号的 request_id。
* @return {{ request: string, response: string, responseByRequestId: string }} 缓存键集合。
*/
buildOmsWaybillPayloadCacheKeys(requestId = '') {
const normalizedRequestId = String(requestId || '').trim();
return {
request: `${OMS_WAYBILL_PAYLOAD_CACHE_KEY_PREFIX}:request:latest`,
response: `${OMS_WAYBILL_PAYLOAD_CACHE_KEY_PREFIX}:response:latest`,
responseByRequestId: normalizedRequestId
? `${OMS_WAYBILL_PAYLOAD_CACHE_KEY_PREFIX}:response:${normalizedRequestId}`
: '',
};
}
/**
* 按字段名截断报文中的密文/大字段。
*
* 仅改写命中 {@link OMS_WAYBILL_PAYLOAD_HEAVY_FIELDS} 且超长的字符串取值,
* 其余字段(含层级结构)原样保留,确保缓存内容仍能反映真实报文格式。
*
* @param {*} value 报文节点。
* @param {number} [depth=0] 当前递归深度,超过 10 层不再下钻。
* @return {*} 处理后的报文节点。
*/
truncateOmsWaybillPayloadFields(value, depth = 0) {
if (depth > 10 || value === null || typeof value !== 'object') {
return value;
}
if (Array.isArray(value)) {
return value.map(item => this.truncateOmsWaybillPayloadFields(item, depth + 1));
}
const truncated = {};
for (const [ key, item ] of Object.entries(value)) {
const isHeavyField = OMS_WAYBILL_PAYLOAD_HEAVY_FIELDS.includes(String(key).toLowerCase());
if (isHeavyField
&& typeof item === 'string'
&& item.length > OMS_WAYBILL_PAYLOAD_HEAVY_FIELD_KEEP_CHARS) {
truncated[key] = `${item.slice(0, OMS_WAYBILL_PAYLOAD_HEAVY_FIELD_KEEP_CHARS)}...(truncated,total=${item.length})`;
continue;
}
truncated[key] = this.truncateOmsWaybillPayloadFields(item, depth + 1);
}
return truncated;
}
/**
* 缓存 OMS 回源取号的请求与返回报文。
*
* OMS 返回的报文格式是与 OMS 对齐的唯一依据,落缓存后可直接用
* `script/redis-cache.ps1 -Action get -Key <key>` 取回核对,不再依赖日志留存。
* 缓存失败只告警,绝不影响取号结果。
*
* 缓存键(TTL 见 {@link OMS_WAYBILL_PAYLOAD_CACHE_TTL}):
* - `oms:waybill:request:latest`:最近一次请求体
* - `oms:waybill:response:latest`:最近一次完整快照(元信息 + 请求 + 返回报文)
* - `oms:waybill:response:<request_id>`:按 request_id 归档的同一份快照
*
* @param {Object} [payload={}] 快照数据。
* @param {string} [payload.request_id] 本次取号 request_id。
* @param {string} [payload.app_key] 目标 OMS 应用 appKey。
* @param {number} [payload.warehouse_id] 仓库 ID。
* @param {number} [payload.store_id] 货主 ID。
* @param {number} [payload.order_count] 本次批次订单数。
* @param {Array<string>} [payload.delivery_order_codes] 本次批次发货单号。
* @param {Object} [payload.transport] 传输层结果(success/retry_count/error)。
* @param {Object} [payload.request] 实际下发的请求体。
* @param {*} [payload.response] OMS 原始返回报文。
* @return {Promise<Object|null>} 缓存键与字节数;未缓存时返回 null。
*/
async cacheOmsWaybillPayload(payload = {}) {
const app = this.ctx && this.ctx.app;
const cacheKeys = this.buildOmsWaybillPayloadCacheKeys(payload.request_id);
if (!app || typeof app.setCacheAsync !== 'function') {
return null;
}
const snapshot = {
cached_at: new Date().toISOString(),
request_id: String(payload.request_id || ''),
app_key: String(payload.app_key || ''),
event: OMS_WAYBILL_EVENT,
warehouse_id: Number(payload.warehouse_id) || 0,
store_id: Number(payload.store_id) || 0,
order_count: Number(payload.order_count) || 0,
delivery_order_codes: normalizeArray(payload.delivery_order_codes),
transport: payload.transport || {},
request: payload.request === undefined ? null : payload.request,
response: payload.response === undefined ? null : payload.response,
payload_truncated: false,
};
let payloadBytes = Buffer.byteLength(JSON.stringify(snapshot), 'utf8');
if (payloadBytes > OMS_WAYBILL_PAYLOAD_CACHE_MAX_BYTES) {
snapshot.request = this.truncateOmsWaybillPayloadFields(snapshot.request);
snapshot.response = this.truncateOmsWaybillPayloadFields(snapshot.response);
snapshot.payload_truncated = true;
payloadBytes = Buffer.byteLength(JSON.stringify(snapshot), 'utf8');
}
if (payloadBytes > OMS_WAYBILL_PAYLOAD_CACHE_MAX_BYTES) {
// 脱敏后仍超限:响应只留元信息,避免把 Redis 打爆
snapshot.response = `<omitted: ${Buffer.byteLength(JSON.stringify(payload.response === undefined ? null : payload.response), 'utf8')} bytes,超过报文缓存上限>`;
payloadBytes = Buffer.byteLength(JSON.stringify(snapshot), 'utf8');
}
try {
await app.setCacheAsync(cacheKeys.request, snapshot.request, OMS_WAYBILL_PAYLOAD_CACHE_TTL);
await app.setCacheAsync(cacheKeys.response, snapshot, OMS_WAYBILL_PAYLOAD_CACHE_TTL);
if (cacheKeys.responseByRequestId) {
await app.setCacheAsync(cacheKeys.responseByRequestId, snapshot, OMS_WAYBILL_PAYLOAD_CACHE_TTL);
}
} catch (error) {
this.ctx.logger.warn('[ExpressPlatformService.oms] OMS 取号报文缓存写入失败', {
requestId: snapshot.request_id,
error: error && error.message ? error.message : `${error}`,
});
return null;
}
this.ctx.logger.info('[ExpressPlatformService.oms] OMS 取号报文已缓存', {
requestId: snapshot.request_id,
cacheKey: cacheKeys.response,
requestIdCacheKey: cacheKeys.responseByRequestId,
ttlSeconds: OMS_WAYBILL_PAYLOAD_CACHE_TTL,
payloadBytes,
truncated: snapshot.payload_truncated,
});
return {
...cacheKeys,
payloadBytes,
};
}
/**
* 判断 OMS 逐单失败原因是否值得重试。
*
* @param {string} message OMS 返回的失败原因。
* @return {boolean} 可重试返回 true。
*/
isRetryableOmsWaybillMessage(message) {
const text = String(message || '').trim();
if (!text) {
return false;
}
return !OMS_WAYBILL_NON_RETRYABLE_KEYWORDS.some(keyword => text.includes(keyword));
}
/**
* 为回 OMS 取号的订单补齐 order_express 占位行。
*
* 这类订单被本地取号流水线整体跳过(不建占位行),若不补齐,
* 取号成功后的 update 会因无匹配行而丢失运单号。
*
* @param {Array<Object>} items 本次取号成功的订单项。
* @param {number} warehouseId 仓库 ID。
* @return {Promise<void>} 无返回值。
*/
async ensureOmsWaybillOrderExpressRows(items = [], warehouseId = 0) {
const orderExpressModel = this.ctx.model && this.ctx.model.OrderExpress;
const normalizedItems = normalizeArray(items).filter(item => item.order_id);
if (!orderExpressModel
|| typeof orderExpressModel.findAll !== 'function'
|| typeof orderExpressModel.bulkCreate !== 'function'
|| normalizedItems.length === 0) {
return;
}
const { Op } = this.app.Sequelize;
const existingRows = await orderExpressModel.findAll({
where: {
order_id: {
[Op.in]: normalizedItems.map(item => item.order_id),
},
},
attributes: [ 'order_id', 'business_key' ],
raw: true,
});
const existingKeySet = new Set(
normalizeArray(existingRows).map(row => `${Number(row.order_id)}:${Number(row.business_key) || 1}`)
);
const rowsToCreate = normalizedItems
.filter(item => !existingKeySet.has(`${item.order_id}:${item.business_key}`))
.map(item => ({
order_id: item.order_id,
business_key: item.business_key,
status: 0,
retry_count: 0,
warehouse_id: item.warehouse_id || warehouseId,
store_id: item.store_id || null,
logistics_code: item.express_code || '',
delivery_order_code: item.delivery_order_code || null,
source_order_codes: item.source_order_codes || null,
}));
if (rowsToCreate.length === 0) {
return;
}
await orderExpressModel.bulkCreate(rowsToCreate, { ignoreDuplicates: true });
}
/**
* OMS 回源取号(deliveryorder.getwaybill)。
*
* 处理 need_oms_waybill=1 的订单:不调本地快递平台,改由 OMS 侧的
* 外部打单账号取号,回传运单号后写入 order_express 并把订单置为已分配快递。
*
* 约定:
* - 单批最多 100 单(OMS 硬上限),超出自动切分;
* - request_id 由订单集合推导,重试复用同一值以命中 OMS 幂等;
* - 有失败时抛错,`error.retryable` 标记是否值得重试,由队列消费侧决定状态。
*
* @param {Object} data 业务数据骨架(orders / warehouse_id / store_id)。
* @return {Promise<Object>} 处理结果。
*/
async oms(data = {}) {
const source = data && typeof data === 'object' ? data : {};
const warehouseIdHint = Number(source.warehouse_id || source.warehouseId) || 0;
const storeIdHint = Number(source.store_id || source.storeId) || 0;
const rawOrders = normalizeArray(source.orders);
const items = this.normalizeOmsWaybillOrders(rawOrders);
if (items.length === 0) {
return {
flag: 'success',
code: 0,
message: '没有需要回 OMS 取号的订单',
data: { total: 0, success: 0, failed: 0, results: [] },
};
}
// 平台码统一归一为打印平台码,与本地取号落库口径保持一致(避免把 DYXD 这类来源平台码写进 order_express)
const printPlatformCodeMap = await this.buildWaybillPrintPlatformCodeMap();
this.applyWaybillPrintPlatformCodes(items, printPlatformCodeMap);
const startedAt = Date.now();
const callbackService = this.service && this.service.openapi && this.service.openapi.callback;
const ExpressList = await this.service.systems.express.loadExpressList();
const aggregatedResults = [];
const failedMessages = [];
const successUpdates = [];
const successItems = [];
let retryable = false;
let successCount = 0;
await this.attachOmsWaybillAppKeys(items);
// 按 appKey 分组后再按 100 切分:不同 OMS 应用/仓库不能混在同一批
const appKeyGroupMap = new Map();
for (const item of items) {
const groupKey = String(item.app_key || '').trim();
if (!appKeyGroupMap.has(groupKey)) {
appKeyGroupMap.set(groupKey, []);
}
appKeyGroupMap.get(groupKey).push(item);
}
const batches = [];
for (const [ groupAppKey, groupItems ] of appKeyGroupMap.entries()) {
for (let offset = 0; offset < groupItems.length; offset += OMS_WAYBILL_MAX_BATCH_SIZE) {
batches.push({
appKey: groupAppKey,
orders: groupItems.slice(offset, offset + OMS_WAYBILL_MAX_BATCH_SIZE),
});
}
}
for (const { appKey, orders: batch } of batches) {
if (!appKey) {
for (const item of batch) {
failedMessages.push(
`${item.delivery_order_code || item.order_id}: 订单缺少来源应用 appKey,且货主 ${item.store_id} 仓库 ${item.warehouse_id} 未配置可用的 OpenAPI 应用`
);
}
continue;
}
const warehouseId = Number(batch[0].warehouse_id) || warehouseIdHint;
if (!callbackService || typeof callbackService.sendByAppKey !== 'function') {
throw new Error('OpenAPI 回调服务不可用,无法回 OMS 取号');
}
const requestId = this.buildOmsWaybillRequestId(warehouseId, batch);
const callbackRequest = {
orders: batch.map(item => ({
deliveryOrderCode: item.delivery_order_code,
expressCode: item.express_code,
// 包裹序号(0 基):不传时 OMS 按单号尾部 _N 推导,主单子单会撞成同一个包裹
packageIndex: Math.max((Number(item.business_key) || 1) - 1, 0),
})),
request_id: requestId,
};
const callbackResult = await callbackService.sendByAppKey({
appKey,
event: OMS_WAYBILL_EVENT,
request: callbackRequest,
});
// 无论成功失败都缓存请求/返回报文:OMS 返回格式是与 OMS 对齐的唯一依据
await this.cacheOmsWaybillPayload({
request_id: requestId,
app_key: appKey,
warehouse_id: warehouseId,
store_id: Number(batch[0].store_id) || storeIdHint,
order_count: batch.length,
delivery_order_codes: batch.map(item => item.delivery_order_code).filter(Boolean),
transport: {
success: Boolean(callbackResult && callbackResult.success),
retry_count: Number(callbackResult && callbackResult.retryCount) || 0,
error: callbackResult && callbackResult.error ? String(callbackResult.error) : null,
},
request: callbackRequest,
response: callbackResult ? callbackResult.response : null,
});
if (!callbackResult || callbackResult.success !== true) {
const transportError = new Error(
`OMS 取号请求失败: ${callbackResult && callbackResult.error ? callbackResult.error : '未知错误'}`
);
transportError.retryable = true;
throw transportError;
}
const responseBody = callbackResult.response && typeof callbackResult.response === 'object'
? callbackResult.response
: {};
const responseCode = Number(responseBody.code) || 0;
const responseFlag = String(responseBody.flag || '').trim().toLowerCase();
if (responseFlag !== 'success' || responseCode !== 1) {
const responseMessage = String(responseBody.message || 'OMS 取号返回失败').trim();
const businessError = new Error(`OMS 取号返回失败(code=${responseCode}): ${responseMessage}`);
businessError.retryable = responseCode === 409 || responseCode >= 500;
throw businessError;
}
const responseResults = normalizeArray(responseBody.data && responseBody.data.results);
if (responseResults.length !== batch.length) {
const countError = new Error(
`OMS 取号返回数量异常: 请求 ${batch.length} 条,返回 ${responseResults.length} 条`
);
countError.retryable = true;
throw countError;
}
for (let index = 0; index < batch.length; index++) {
const item = batch[index];
const row = responseResults[index] || {};
const isSuccess = row.success === true;
const waybillCode = String(row.waybill_code || '').trim();
const rowMessage = String(row.message || '').trim();
aggregatedResults.push({
delivery_order_code: item.delivery_order_code,
success: isSuccess,
message: rowMessage || (isSuccess ? 'ok' : 'OMS 未返回失败原因'),
waybill_code: waybillCode,
});
if (!isSuccess) {
failedMessages.push(`${item.delivery_order_code || item.order_id}: ${rowMessage || '取号失败'}`);
if (this.isRetryableOmsWaybillMessage(rowMessage)) {
retryable = true;
}
continue;
}
if (!waybillCode) {
failedMessages.push(`${item.delivery_order_code || item.order_id}: OMS 返回成功但运单号为空`);
retryable = true;
continue;
}
// 回显的包裹序号必须与请求一致,否则运单号可能被落到错误的包裹行
const expectedPackageIndex = Math.max((Number(item.business_key) || 1) - 1, 0);
const responsePackageIndex = Number(row.package_index);
if (typeof row.package_index !== 'undefined'
&& row.package_index !== null
&& row.package_index !== ''
&& Number.isInteger(responsePackageIndex)
&& responsePackageIndex !== expectedPackageIndex) {
failedMessages.push(
`${item.delivery_order_code || item.order_id}: OMS 返回包裹序号不一致(期望 ${expectedPackageIndex},返回 ${responsePackageIndex})`
);
retryable = true;
continue;
}
const expressCode = String(row.express_code || item.express_code || '').trim().toUpperCase();
const expressName = (ExpressList.find(entry => entry.code === expressCode) || {}).name || '';
// 平台码先归一为打印平台码再落库:item.platform_code 已在入口归一,
// OMS 回传的 platform_code 同样要过一遍映射,禁止落 'oms' 或来源平台码。
const responsePlatformCode = this.resolveWaybillPrintPlatformCode(row.platform_code, printPlatformCodeMap);
const platformCode = String(item.platform_code || responsePlatformCode || '').trim().toUpperCase();
// 打印定位字段:print_history_id 用于打印前回 OMS 换新 params,
// cai_niao_auth_info_id 用于打印时按店铺(授权网点)分组,一组只换一次 params
const printHistoryId = String(
row.print_history_id
|| (Array.isArray(row.print_history_ids) && row.print_history_ids[0] && row.print_history_ids[0].id)
|| ''
).trim();
const caiNiaoAuthInfoId = Number(row.cai_niao_auth_info_id) || 0;
// 面单打印数据与签名:OMS 原样透传,打印时必须原样下发(不要解码 base64/URL 编码);
// 上游未返回的报文字段不写库,避免把已有有效值冲成空。
const orderExpressValues = this.dropEmptyOrderExpressFields({
waybill_code: waybillCode,
status: 1,
retry_count: 0,
create_at: new Date(),
delivery_order_code: item.delivery_order_code || null,
source_order_codes: item.source_order_codes || null,
store_id: item.store_id || null,
logistics_code: expressCode,
logistics_name: expressName,
printinfo: String(row.printinfo || '') || null,
signature: String(row.signature || '') || null,
// 快手口径的加密密钥由 encryptprintinfo 承载;OMS 新版另给 key 字段,作为兜底
encryptprintinfo: String(
row.encryptprintinfo
|| (platformCode === 'KS' ? row.key : '')
|| ''
) || null,
ver: String(row.ver || '') || null,
});
if (platformCode) {
orderExpressValues.platform_code = platformCode;
}
if (printHistoryId) {
orderExpressValues.print_history_id = printHistoryId;
}
if (caiNiaoAuthInfoId > 0) {
orderExpressValues.cai_niao_auth_info_id = caiNiaoAuthInfoId;
}
// 菜鸟(淘宝)打印只认 printinfo 整包:OMS 恒回空串、把密文拆成平铺字段,
// 这里按菜鸟规范回拼,保证与菠萝派取号落库结构一致
const omsCainiaoPrintinfo = this.buildOmsCainiaoPrintinfo(platformCode, orderExpressValues);
if (omsCainiaoPrintinfo) {
orderExpressValues.printinfo = omsCainiaoPrintinfo;
}
successCount += 1;
successItems.push(item);
successUpdates.push({
warehouse_id: item.warehouse_id || warehouseId,
order_id: item.order_id,
business_key: item.business_key,
waybill_code: waybillCode,
orderExpressValues,
orderDeliveryValues: {
post_status: 'fin',
order_flag: '',
},
});
}
}
if (successUpdates.length > 0) {
const successWarehouseId = Number(successUpdates[0].warehouse_id) || warehouseIdHint;
await this.ensureOmsWaybillOrderExpressRows(successItems, successWarehouseId);
await this.persistWaybillSuccessUpdates(successUpdates, rawOrders);
}
this.ctx.logger.info('[ExpressPlatformService.oms] 回 OMS 取号完成', {
warehouseId: warehouseIdHint,
storeId: storeIdHint,
total: items.length,
success: successCount,
failed: failedMessages.length,
retryable,
durationMs: Date.now() - startedAt,
firstFailure: failedMessages[0] || '',
});
if (failedMessages.length > 0) {
const failureError = new Error(failedMessages.join('; '));
failureError.retryable = retryable;
throw failureError;
}
return {
flag: 'success',
code: 0,
message: `回 OMS 取号成功 ${successCount} 单`,
data: {
total: items.length,
success: successCount,
failed: 0,
results: aggregatedResults,
},
};
}
}
// 静态挂载 Uint64 工具:供回包解析(SPH object_Id 反解)等场景复用

View File

@ -975,8 +975,10 @@ async function markApiSyncFailedInternal(service, orderModel, orderId, warehouse
* 与奇门渠道(processQmOrderInternal)口径对齐:
* ① 组装与奇门 taobao.qimen.deliveryorder.confirm 完全一致的 request 报文
* (缺奇门货主编码等组装数据时直接失败);
* ② 解析发送用 appKey:context.appKey 非空优先,缺失时按订单货主合约反查 ApiApp
* ② 解析发送用 appKey:context.appKey 优先,其次订单来源键 from_key(与回 OMS 取号同口径,
* 从哪里来回哪里去),都缺失时按订单货主合约反查 ApiApp
* (同一批次按货主缓存,N 单 / M 货主 → 2M 次查询,无 N+1);
* API(OMS) 渠道的 deliveryorder.confirm 不校验 ownerCode,故不再要求奇门货主编码;
* ③ 发送回调(sendByAppKey,内部最多 4 次尝试),成功才算平台已确认;
* ④ 回调成功 → 扣减库存 + sync_status='fin';失败/异常 → sync_status='error'
* + 待处理异常,不扣减库存,可重新派发;
@ -1033,22 +1035,24 @@ async function processApiOrderInternal(service, orderModel, orderItemsModel, ord
packageExpressCodes: waybillCodes,
});
if (!requestData || !requestData.ownerCode || !requestData.request) {
if (!requestData || !requestData.request) {
service.ctx.logger.warn(`[OrderAsyncService.processApiOrder] 回调报文组装数据缺失 orderId=${orderId}`, {
deliveryOrderCode,
ownerCode: requestData && requestData.ownerCode,
});
return await failSync('发货单缺少奇门货主编码');
return await failSync('发货单回传报文组装失败');
}
// ② 解析发送用 appKey
// ② 解析发送用 appKey:按订单来源键(from_key)回原通道,与回 OMS 取号口径一致。
// API(OMS) 渠道的 deliveryorder.confirm 不校验 ownerCode,故不再要求奇门货主编码。
const callbackService = service.ctx.service.openapi && service.ctx.service.openapi.callback;
if (!callbackService || typeof callbackService.sendByAppKey !== 'function') {
return await failSync('OpenAPI 回调服务未初始化');
}
let resolvedAppKey = String(context.appKey || '').trim();
const orderFromKey = String(orderRow.from_key || '').trim();
let resolvedAppKey = String(context.appKey || orderFromKey).trim();
if (!resolvedAppKey) {
const storeId = Number(orderRow.store_id) || 0;
if (storeId <= 0) {

View File

@ -2,10 +2,23 @@
const fs = require('node:fs');
const path = require('node:path');
const crypto = require('node:crypto');
const { Op } = require('sequelize');
const OrderBaseService = require('../systemMethods/order/base');
const PrinterDataBuilder = require('../../lib/printer_data_builder');
// 抖音临时 key(logistics.getShopKey)有效期约 5 分钟,缓存留 30 秒冗余
const DOUYIN_PRINT_PARAMS_CACHE_TTL_SECONDS = 270;
/** OMS 换新打印数据(只读、不消耗面单)的入站方法名。 */
const OMS_PRINT_DATA_EVENT = 'deliveryorder.getprintdata';
// OMS 返回的抖音 params 有效期 600 秒,缓存留 300 秒余量
const OMS_DOUYIN_PARAMS_CACHE_TTL_SECONDS = 300;
/** OMS 抖音 params 缓存键前缀(按 cai_niao_auth_info_id 即店铺/授权网点维度)。 */
const OMS_DOUYIN_PARAMS_CACHE_KEY_PREFIX = 'oms:douyin:params';
class OrderPrintSystemService extends OrderBaseService {
/**
* 判断订单是否处于锁定状态。
@ -1304,6 +1317,8 @@ class OrderPrintSystemService extends OrderBaseService {
'signature',
'encryptprintinfo',
'ver',
'print_history_id',
'cai_niao_auth_info_id',
],
where: {
waybill_code: {
@ -1868,6 +1883,7 @@ class OrderPrintSystemService extends OrderBaseService {
'freight',
'create_at',
'pay_time',
'from_key',
],
where: {
wave_id: {
@ -1934,6 +1950,8 @@ class OrderPrintSystemService extends OrderBaseService {
'freight',
'create_at',
'pay_time',
// OMS 取号订单的回源应用 appKey,打印时回 OMS 换新 params 要用
'from_key',
];
// 第一步:查询 current 主表(wms_order_delivery_{warehouseId})
@ -2036,6 +2054,8 @@ class OrderPrintSystemService extends OrderBaseService {
'signature',
'encryptprintinfo',
'ver',
'print_history_id',
'cai_niao_auth_info_id',
],
where: {
order_id: {
@ -2812,6 +2832,420 @@ ${waveFilterSql}
return null;
}
/**
* 解析抖音打印应使用的快递接口配置(按取号店铺隔离)。
*
* 优先用运单落库的 logistics_interface_id 精确命中取号时使用的接口配置,
* 避免同仓同平台多店铺时按快递编码误取到其他店铺的 token;
* 仅当运单缺少 interface_id(历史数据)且同仓抖音接口只绑定了一个店铺时,才回退到按快递编码匹配。
*
* @param {Array<Object>} interfaceList 接口配置列表。
* @param {Object} orderRow 订单行。
* @param {Object} orderExpress 快递行。
* @return {Object} 命中结果,interfaceRow 为命中的接口配置(未命中为 null),matchedBy 为命中方式。
*/
resolveDouyinExpressInterface(interfaceList = [], orderRow = {}, orderExpress = {}) {
const safeList = (Array.isArray(interfaceList) ? interfaceList : [])
.filter(item => this.isPlainObject(item));
const interfaceId = Number(orderExpress && orderExpress.logistics_interface_id || 0);
if (Number.isInteger(interfaceId) && interfaceId > 0) {
const exactInterface = safeList.find(item => Number(item.id) === interfaceId);
return {
interfaceRow: exactInterface || null,
matchedBy: exactInterface ? 'logistics_interface_id' : 'logistics_interface_id_missed',
};
}
const shopIds = new Set(safeList
.map(item => Number(item.shop_id || 0))
.filter(shopId => Number.isInteger(shopId) && shopId > 0));
if (shopIds.size > 1) {
// 多店铺场景下无法判定归属,宁可不取临时 key,也不能串用其他店铺的 token
return {
interfaceRow: null,
matchedBy: 'shop_ambiguous',
};
}
const fallbackInterface = this.pickDouyinExpressInterface(safeList, orderRow, orderExpress);
return {
interfaceRow: fallbackInterface || null,
matchedBy: fallbackInterface ? 'express_code' : 'unmatched',
};
}
/**
* 构建抖音临时 key 缓存键。
*
* 抖音 logistics.getShopKey 以店铺(wms_express_interface.shop_id + token)为维度,
* 缓存键必须带店铺标识与 token 指纹,避免不同店铺之间串用临时 key。
*
* @param {Object} interfaceRow 命中的快递接口配置。
* @return {string} 缓存键,无法定位归属时返回空字符串(表示不缓存)。
*/
buildDouyinParamsCacheKey(interfaceRow = {}) {
const safeInterfaceRow = this.isPlainObject(interfaceRow) ? interfaceRow : {};
const token = String(safeInterfaceRow.token || '').trim();
if (!token || !this.app || typeof this.app.getCacheAsync !== 'function') {
return '';
}
const tokenFingerprint = crypto.createHash('md5').update(token, 'utf8').digest('hex')
.slice(0, 8);
const shopId = Number(safeInterfaceRow.shop_id || 0);
if (Number.isInteger(shopId) && shopId > 0) {
return `douyin:params:shop${shopId}:${tokenFingerprint}`;
}
const interfaceId = Number(safeInterfaceRow.id || 0);
return Number.isInteger(interfaceId) && interfaceId > 0
? `douyin:params:if${interfaceId}:${tokenFingerprint}`
: '';
}
/**
* 获取抖音打印临时 key 的 params 串(按店铺缓存)。
*
* 同一批次内相同缓存键的并发请求复用同一个 Promise,避免波次打印重复调用菠萝派。
*
* @param {Object} interfaceRow 命中的快递接口配置。
* @param {Map<string, Promise<string>>} [inflightMap] 并发去重表。
* @return {Promise<string>} params 串,获取失败返回空字符串。
*/
async getDouyinPrintParams(interfaceRow = {}, inflightMap = new Map()) {
const safeInterfaceRow = this.isPlainObject(interfaceRow) ? interfaceRow : {};
const token = String(safeInterfaceRow.token || '').trim();
if (!token) {
this.ctx.logger.warn('[orderPrint.getDouyinPrintParams] 快递接口缺少 token,跳过临时 key 获取', {
interfaceId: Number(safeInterfaceRow.id || 0),
shopId: Number(safeInterfaceRow.shop_id || 0),
});
return '';
}
const cacheKey = this.buildDouyinParamsCacheKey(safeInterfaceRow);
if (cacheKey) {
try {
const cachedParams = await this.app.getCacheAsync(cacheKey);
if (cachedParams && typeof cachedParams === 'string') {
return cachedParams;
}
} catch (error) {
this.ctx.logger.warn('[orderPrint.getDouyinPrintParams] 读取缓存失败: %s', error.message);
}
if (inflightMap.has(cacheKey)) {
return inflightMap.get(cacheKey);
}
}
const requestTask = this.requestDouyinPrintParams(safeInterfaceRow, token, cacheKey);
if (!cacheKey) {
return requestTask;
}
inflightMap.set(cacheKey, requestTask);
try {
return await requestTask;
} finally {
inflightMap.delete(cacheKey);
}
}
/**
* 调用菠萝派获取抖音临时 key,并仅对有效返回回写缓存。
*
* @param {Object} interfaceRow 命中的快递接口配置。
* @param {string} token 店铺访问令牌。
* @param {string} cacheKey 缓存键,为空时不回写缓存。
* @return {Promise<string>} params 串,失败返回空字符串。
*/
async requestDouyinPrintParams(interfaceRow = {}, token = '', cacheKey = '') {
const poloConfig = this.config.poloApi || {};
const interfaceId = Number(interfaceRow.id || 0);
const shopId = Number(interfaceRow.shop_id || 0);
try {
const PoloApiClient = require('../../lib/polo_api_client');
const poloClient = new PoloApiClient(this.ctx);
const result = await poloClient.request({
method: 'Differ.JH.Business.GetPlatApiRequestInfo',
bizcontent: { platMethod: '/logistics/getShopKey' },
config: {
interface_url: 'https://api.polyapi.com/openapi/do',
appkey: poloConfig.appKey || '',
app_secret: poloConfig.appSecret || '',
platid: 126,
version: '1.5',
token,
},
token,
});
const requestInfo = String(result && result.requestinfo || '').trim();
this.ctx.logger.info('[orderPrint.requestDouyinPrintParams] 菠萝派临时 key 响应', {
interfaceId,
shopId,
cacheKey,
code: String(result && result.code || ''),
hasRequestInfo: !!requestInfo,
});
if (!requestInfo) {
this.ctx.logger.warn('[orderPrint.requestDouyinPrintParams] 菠萝派未返回 requestinfo,params 置空', {
interfaceId,
shopId,
});
return '';
}
if (cacheKey && this.app && typeof this.app.setCacheAsync === 'function') {
try {
await this.app.setCacheAsync(cacheKey, requestInfo, DOUYIN_PRINT_PARAMS_CACHE_TTL_SECONDS);
} catch (error) {
this.ctx.logger.warn('[orderPrint.requestDouyinPrintParams] 写入缓存失败: %s', error.message);
}
}
return requestInfo;
} catch (error) {
this.ctx.logger.warn('[orderPrint.requestDouyinPrintParams] 获取抖音临时 key 失败: %s', error.message);
return '';
}
}
/**
* 构建 OMS 抖音 params 缓存键。
*
* params 有效期 600 秒,且同一店铺(cai_niao_auth_info_id)共用一份,
* 因此按店铺维度缓存,避免波次内每单都回 OMS 换一次。
*
* @param {number|string} caiNiaoAuthInfoId 菜鸟授权网点ID(店铺维度,每个店铺唯一)。
* @return {string} 缓存键,无法定位时返回空字符串(表示不缓存)。
*/
buildOmsDouyinParamsCacheKey(caiNiaoAuthInfoId) {
const normalizedAuthInfoId = Number(caiNiaoAuthInfoId) || 0;
return normalizedAuthInfoId > 0
? `${OMS_DOUYIN_PARAMS_CACHE_KEY_PREFIX}:${normalizedAuthInfoId}`
: '';
}
/**
* 按 cai_niao_auth_info_id 归集抖音打印参数换取的样本。
*
* 同一店铺(授权网点)内任取一条带 print_history_id 的运单即可完成换取,
* 换取到的 params 供该店铺下所有运单共用。
*
* @param {Array<Object>} orderRows 订单行列表。
* @param {Array<Object>} orderExpressRows 运单行列表。
* @return {Map<number, Object>} 授权网点ID → 换取样本。
*/
buildDouyinOmsParamsSampleMap(orderRows = [], orderExpressRows = []) {
const orderRowMap = new Map((Array.isArray(orderRows) ? orderRows : [])
.map(row => [ Number(row && row.id || 0), row ]));
const sampleMap = new Map();
for (const row of Array.isArray(orderExpressRows) ? orderExpressRows : []) {
const caiNiaoAuthInfoId = Number(row && row.cai_niao_auth_info_id || 0);
const printHistoryId = String(row && row.print_history_id || '').trim();
if (caiNiaoAuthInfoId <= 0 || !printHistoryId || sampleMap.has(caiNiaoAuthInfoId)) {
continue;
}
const orderRow = orderRowMap.get(Number(row && row.order_id || 0)) || {};
sampleMap.set(caiNiaoAuthInfoId, {
caiNiaoAuthInfoId,
printHistoryId,
waybillCode: String(row && row.waybill_code || '').trim(),
storeId: Number(orderRow.store_id || row.store_id || 0),
warehouseId: Number(orderRow.warehouse_id || row.warehouse_id || 0),
fromKey: String(orderRow.from_key || '').trim(),
});
}
return sampleMap;
}
/**
* 解析回 OMS 换打印数据所用的 appKey。
*
* 优先使用订单建单时的来源应用 `from_key`,缺失时按「货主 + 仓库」反查授权关系下的 ApiApp。
*
* @param {Object} sample 换取样本。
* @param {Map<string, string>} [cache] appKey 缓存(按货主+仓库)。
* @return {Promise<string>} appKey,取不到返回空字符串。
*/
async resolveOmsPrintAppKey(sample = {}, cache = new Map()) {
const safeSample = this.isPlainObject(sample) ? sample : {};
if (safeSample.fromKey) {
return String(safeSample.fromKey).trim();
}
const cacheKey = `${Number(safeSample.storeId) || 0}:${Number(safeSample.warehouseId) || 0}`;
if (cache.has(cacheKey)) {
return cache.get(cacheKey);
}
const orderAsyncService = this.service
&& this.service.systems
&& this.service.systems.orderAsync;
let appKey = '';
if (orderAsyncService && typeof orderAsyncService.resolveOpenApiAppByStore === 'function') {
try {
const appConfig = await orderAsyncService.resolveOpenApiAppByStore(
safeSample.storeId,
safeSample.warehouseId
);
appKey = String(appConfig && appConfig.appKey || '').trim();
} catch (error) {
this.ctx.logger.warn('[orderPrint.resolveOmsPrintAppKey] 反查 ApiApp 失败: %s', error.message);
}
}
cache.set(cacheKey, appKey);
return appKey;
}
/**
* 调 OMS `deliveryorder.getprintdata` 换取新的抖音打印数据(params)。
*
* 只读接口,不消耗面单;请求只带一个 printHistoryId,OMS 自行按取号记录回源平台。
*
* @param {Object} params 入参。
* @param {string} params.appKey 目标 OMS 应用 appKey。
* @param {string} params.printHistoryId 打印记录ID。
* @param {number} params.caiNiaoAuthInfoId 菜鸟授权网点ID(仅用于日志)。
* @return {Promise<{params: string, template_url: string}|null>} 换取结果,失败返回 null。
*/
async requestOmsDouyinPrintParams({ appKey = '', printHistoryId = '', caiNiaoAuthInfoId = 0 } = {}) {
const normalizedAppKey = String(appKey || '').trim();
const normalizedPrintHistoryId = String(printHistoryId || '').trim();
const callbackService = this.service && this.service.openapi && this.service.openapi.callback;
if (!normalizedAppKey || !normalizedPrintHistoryId) {
return null;
}
if (!callbackService || typeof callbackService.sendByAppKey !== 'function') {
this.ctx.logger.warn('[orderPrint.requestOmsDouyinPrintParams] OpenAPI 回调服务不可用,跳过 OMS 换取 params');
return null;
}
try {
const result = await callbackService.sendByAppKey({
appKey: normalizedAppKey,
event: OMS_PRINT_DATA_EVENT,
request: { printHistoryIds: [ normalizedPrintHistoryId ] },
});
if (!result || result.success !== true) {
this.ctx.logger.warn('[orderPrint.requestOmsDouyinPrintParams] OMS 换取打印数据请求失败', {
caiNiaoAuthInfoId,
printHistoryId: normalizedPrintHistoryId,
error: result && result.error ? String(result.error) : '未知错误',
});
return null;
}
const responseBody = this.isPlainObject(result.response) ? result.response : {};
const results = Array.isArray(responseBody.data && responseBody.data.results)
? responseBody.data.results
: [];
const matched = results.find(item => item && item.success === true && String(item.params || '').trim()) || null;
if (!matched) {
this.ctx.logger.warn('[orderPrint.requestOmsDouyinPrintParams] OMS 未返回可用 params', {
caiNiaoAuthInfoId,
printHistoryId: normalizedPrintHistoryId,
message: String(results[0] && results[0].message || responseBody.message || '').trim(),
});
return null;
}
const params = String(matched.params || '').trim();
const templateUrl = String(matched.template_url || '').trim();
this.ctx.logger.info('[orderPrint.requestOmsDouyinPrintParams] OMS 换取 params 成功', {
caiNiaoAuthInfoId,
printHistoryId: normalizedPrintHistoryId,
paramsLength: params.length,
hasTemplateUrl: !!templateUrl,
});
return { params, template_url: templateUrl };
} catch (error) {
this.ctx.logger.warn('[orderPrint.requestOmsDouyinPrintParams] OMS 换取 params 异常: %s', error.message);
return null;
}
}
/**
* 按店铺换取抖音打印 params。
*
* 仅处理通过 OMS 取号(有 cai_niao_auth_info_id + print_history_id)的运单;
* 每个店铺只取其中一个 print_history_id 回 OMS 换一次,结果按店铺缓存并共享。
*
* @param {Array<Object>} orderRows 订单行列表。
* @param {Array<Object>} orderExpressRows 运单行列表。
* @return {Promise<Map<number, {params: string, template_url: string}>>} 授权网点ID → 换取结果。
*/
async resolveOmsDouyinPrintParamsMap(orderRows = [], orderExpressRows = []) {
const sampleMap = this.buildDouyinOmsParamsSampleMap(orderRows, orderExpressRows);
const paramsMap = new Map();
if (sampleMap.size === 0) {
return paramsMap;
}
const appKeyCache = new Map();
for (const [ caiNiaoAuthInfoId, sample ] of sampleMap.entries()) {
const cacheKey = this.buildOmsDouyinParamsCacheKey(caiNiaoAuthInfoId);
if (cacheKey) {
try {
const cached = await this.app.getCacheAsync(cacheKey);
if (cached && typeof cached === 'object' && String(cached.params || '').trim()) {
paramsMap.set(caiNiaoAuthInfoId, {
params: String(cached.params).trim(),
template_url: String(cached.template_url || '').trim(),
});
continue;
}
} catch (error) {
this.ctx.logger.warn('[orderPrint.resolveOmsDouyinPrintParamsMap] 读取缓存失败: %s', error.message);
}
}
const appKey = await this.resolveOmsPrintAppKey(sample, appKeyCache);
if (!appKey) {
this.ctx.logger.warn('[orderPrint.resolveOmsDouyinPrintParamsMap] 未找到回 OMS 的 appKey,跳过换取 params', {
caiNiaoAuthInfoId,
storeId: sample.storeId,
warehouseId: sample.warehouseId,
});
continue;
}
const fetched = await this.requestOmsDouyinPrintParams({
appKey,
printHistoryId: sample.printHistoryId,
caiNiaoAuthInfoId,
});
if (!fetched) {
continue;
}
if (cacheKey && this.app && typeof this.app.setCacheAsync === 'function') {
try {
await this.app.setCacheAsync(cacheKey, fetched, OMS_DOUYIN_PARAMS_CACHE_TTL_SECONDS);
} catch (error) {
this.ctx.logger.warn('[orderPrint.resolveOmsDouyinPrintParamsMap] 写入缓存失败: %s', error.message);
}
}
paramsMap.set(caiNiaoAuthInfoId, fetched);
}
return paramsMap;
}
/**
* 为菜鸟打印订单补齐主面单专用来源字段。
*
@ -3033,6 +3467,11 @@ ${waveFilterSql}
const pddOrderIdSet = new Set(pddOrderIds);
const douyinOrderIdSet = new Set(douyinOrderIds);
const douyinParamsInflightMap = new Map();
// OMS 取号的抖音单:按 cai_niao_auth_info_id(店铺)分组,每组只换一次 params
const omsDouyinParamsMap = douyinOrderIds.length > 0
? await this.resolveOmsDouyinPrintParamsMap(normalizedOrderRows, normalizedOrderExpressRows)
: new Map();
return Promise.all(normalizedOrderExpressRows.map(async row => {
const orderId = Number(row && row.order_id || 0);
const orderRow = orderRowMap.get(orderId) || {};
@ -3051,64 +3490,51 @@ ${waveFilterSql}
}
if (douyinOrderIdSet.has(orderId)) {
const matchedInterface = this.pickDouyinExpressInterface(douyinInterfaceList, orderRow, row);
const resolvedInterface = this.resolveDouyinExpressInterface(douyinInterfaceList, orderRow, row);
const matchedInterface = resolvedInterface.interfaceRow;
const matchedTemplateUrl = String(matchedInterface && matchedInterface.template_url || '').trim();
const caiNiaoAuthInfoId = Number(row && row.cai_niao_auth_info_id || 0);
const omsDouyinParams = caiNiaoAuthInfoId > 0 ? omsDouyinParamsMap.get(caiNiaoAuthInfoId) : null;
// OMS 取号的抖音单:params 与模板地址都以 OMS 换取的为准,同店铺共用一份
if (omsDouyinParams && omsDouyinParams.params) {
this.ctx.logger.info('[enrichPddDyOrderExpressRows] douyin 使用 OMS 换取 params', {
orderId,
caiNiaoAuthInfoId,
printHistoryId: String(row && row.print_history_id || '').trim(),
hasOmsTemplateUrl: !!omsDouyinParams.template_url,
});
return {
...row,
dy_template_url: String(omsDouyinParams.template_url || matchedTemplateUrl || '').trim(),
dy_params: omsDouyinParams.params,
};
}
this.ctx.logger.info('[enrichPddDyOrderExpressRows] douyin 单笔匹配结果', {
orderId,
logisticsInterfaceId: Number(row && row.logistics_interface_id || 0),
matchedInterfaceId: Number(matchedInterface && matchedInterface.id || 0),
matchedShopId: Number(matchedInterface && matchedInterface.shop_id || 0),
matchedBy: resolvedInterface.matchedBy,
hasMatchedInterface: !!matchedInterface,
matchedTemplateUrl,
sysLogisticsCode: orderRow.sys_logistics_code,
logisticsCode: row.logistics_code,
});
if (!matchedTemplateUrl) {
if (!matchedInterface) {
this.ctx.logger.warn('[enrichPddDyOrderExpressRows] 未匹配到抖音快递接口,跳过临时 key 获取', {
orderId,
logisticsInterfaceId: Number(row && row.logistics_interface_id || 0),
matchedBy: resolvedInterface.matchedBy,
});
}
return row;
}
// 获取菠萝派 API 请求参数
let dyParams = '';
try {
if (matchedInterface) {
const cacheKey = `douyin:params:${orderRow.sys_logistics_code || orderRow.logistics_code || ''}:${row.logistics_interface_id || ''}`;
// 菠萝派配置:appkey/app_secret 来自项目 config.poloApi,token 来自快递接口
const poloConfig = this.config.poloApi || {};
// 先查 Redis 缓存
if (this.app && typeof this.app.getCacheAsync === 'function') {
const cached = await this.app.getCacheAsync(cacheKey);
if (cached && typeof cached === 'string') {
dyParams = cached;
}
}
// 缓存未命中,调菠萝派
if (!dyParams) {
const PoloApiClient = require('../../lib/polo_api_client');
const poloClient = new PoloApiClient(this.ctx);
const result = await poloClient.request({
method: 'Differ.JH.Business.GetPlatApiRequestInfo',
bizcontent: { platMethod: '/logistics/getShopKey' },
config: {
interface_url: 'https://api.polyapi.com/openapi/do',
appkey: poloConfig.appKey || '',
app_secret: poloConfig.appSecret || '',
platid: 126,
version: '1.5',
token: matchedInterface.token || '',
},
token: matchedInterface.token || '',
});
this.ctx.logger.info('[DouyinParams] 菠萝派 GetPlatApiRequestInfo 响应:', JSON.stringify(result));
dyParams = result.requestinfo || JSON.stringify(result);
if (this.app && typeof this.app.setCacheAsync === 'function') {
await this.app.setCacheAsync(cacheKey, dyParams, 270);
}
}
}
} catch (err) {
this.ctx.logger.warn('[enrichPddDyOrderExpressRows] 获取 Douyin params 失败: %s', err.message);
}
// 临时 key 按取号店铺维度获取与缓存,避免多店铺之间串用
const dyParams = await this.getDouyinPrintParams(matchedInterface, douyinParamsInflightMap);
return {
...row,

View File

@ -0,0 +1,166 @@
'use strict';
/**
* 为订单配送分表增加 wave_no 索引。
*
* 背景:
* - order.deliveryOrder.list(app/service/systemMethods/order/base.js#listByModule)的 SQL 形态为
* `WHERE warehouse_id=? AND dispatch_lane=? AND wave_no=? ORDER BY updated_at DESC, id DESC LIMIT ?`
* 且并行执行同条件 COUNT(*);
* - 生产 wms_order_delivery_1000028_dispatch(1,488,523 行 / 约 2.4GB)没有任何 wave_no 索引,
* 实测 COUNT(*) 26.4s、列表查询 21.1s:优化器只能走 (warehouse_id, dispatch_lane, updated_at, id)
* 或 (warehouse_id) 索引扫掉约 55 万行后逐行回表过滤 wave_no;
* - wave_no 在表内近似唯一(10.25 万个波次号 / 148.8 万行,平均约 14 行/波次),
* 单列索引即可把上述扫描降为十几行的点查。
*
* 本迁移枚举 current/dispatch/history/outsourced 四类分表补建索引,命名与
* app/model/order_delivery_table_schema.js#buildIndexOptions 的 idx_{prefix}_wave_no 一致
* (dispatch 分表沿用 current 前缀,与既有继承索引的命名习惯一致),
* 避免应用运行时 drift 同步再补建一次同名索引。
*
* 执行方式:ALTER TABLE ... ADD INDEX ..., ALGORITHM=INPLACE, LOCK=NONE
* (MySQL 5.6+ 在线加二级索引,不重建表、不锁写,建议低峰期执行)。
* 幂等:索引已存在、或表缺少 wave_no 列时跳过。
*
* ⚠️ 本文件未对任何环境执行,仅由迁移流程(npm run migration:single <文件名>)在确认后落库。
*/
const SHARD_PATTERNS = [
{ modelType: 'dispatch', pattern: /^wms_order_delivery_(\d+)_dispatch$/ },
{ modelType: 'history', pattern: /^wms_order_delivery_(\d+)_history$/ },
{ modelType: 'outsourced', pattern: /^wms_order_delivery_(\d+)_outsourced$/ },
{ modelType: 'current', pattern: /^wms_order_delivery_(\d+)$/ },
];
const INDEX_COLUMN = 'wave_no';
/**
* 解析分表名。
* @param {string} tableName 表名。
* @return {{ modelType: string, warehouseId: string }|null} 解析结果。
*/
function parseShardTableName(tableName) {
for (const item of SHARD_PATTERNS) {
const match = item.pattern.exec(tableName);
if (match) {
return { modelType: item.modelType, warehouseId: match[1] };
}
}
return null;
}
/**
* 构建 wave_no 索引名(与 buildIndexOptions 的 `idx_${prefix}_wave_no` 一致)。
* @param {string} warehouseId 仓库ID。
* @param {string} modelType 模型类型。
* @return {string} 索引名。
*/
function buildWaveNoIndexName(warehouseId, modelType) {
const prefix = modelType === 'dispatch'
? `${warehouseId}_current`
: `${warehouseId}_${modelType}`;
return `idx_${prefix}_${INDEX_COLUMN}`;
}
/**
* 列举库内所有订单配送分表。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @return {Promise<Array<string>>} 分表名列表。
*/
async function listManagedShards(queryInterface) {
const [ rows ] = await queryInterface.sequelize.query(
'SELECT TABLE_NAME AS tableName FROM INFORMATION_SCHEMA.TABLES '
+ 'WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME LIKE \'wms_order_delivery\\_%\' '
+ 'ORDER BY TABLE_NAME'
);
return (rows || [])
.map(row => String(row.tableName || row.TABLE_NAME || ''))
.filter(name => parseShardTableName(name));
}
/**
* 判断表是否存在指定列。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @param {string} tableName 表名。
* @param {string} columnName 列名。
* @return {Promise<boolean>} 是否存在。
*/
async function columnExists(queryInterface, tableName, columnName) {
const [ rows ] = await queryInterface.sequelize.query(
'SELECT COLUMN_NAME AS columnName FROM INFORMATION_SCHEMA.COLUMNS '
+ 'WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ? AND COLUMN_NAME = ?',
{ replacements: [ tableName, columnName ] }
);
return (rows || []).length > 0;
}
/**
* 判断索引是否已存在。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @param {string} tableName 表名。
* @param {string} indexName 索引名。
* @return {Promise<boolean>} 是否存在。
*/
async function indexExists(queryInterface, tableName, indexName) {
const indexes = await queryInterface.showIndex(tableName);
return (indexes || []).some(index => {
const name = String(index && (index.name || index.Key_name || index.key_name) || '');
return name.toLowerCase() === String(indexName).toLowerCase();
});
}
module.exports = {
/**
* 执行迁移:为订单配送分表增加 wave_no 索引。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @return {Promise<void>} 无返回值。
*/
async up(queryInterface) {
const shards = await listManagedShards(queryInterface);
const results = [];
for (const tableName of shards) {
const parsed = parseShardTableName(tableName);
const indexName = buildWaveNoIndexName(parsed.warehouseId, parsed.modelType);
if (!(await columnExists(queryInterface, tableName, INDEX_COLUMN))) {
results.push({ tableName, action: 'skip', reason: 'column-missing' });
continue;
}
if (await indexExists(queryInterface, tableName, indexName)) {
results.push({ tableName, action: 'skip', reason: 'exists' });
continue;
}
await queryInterface.sequelize.query(
`ALTER TABLE \`${tableName}\` ADD INDEX \`${indexName}\` (\`${INDEX_COLUMN}\`), ALGORITHM=INPLACE, LOCK=NONE`
);
results.push({ tableName, action: 'added', indexName });
}
console.log('[20261105190000-add-wave-no-index] up results:', JSON.stringify(results));
},
/**
* 回滚迁移:移除 wave_no 索引。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @return {Promise<void>} 无返回值。
*/
async down(queryInterface) {
const shards = await listManagedShards(queryInterface);
for (const tableName of shards) {
const parsed = parseShardTableName(tableName);
const indexName = buildWaveNoIndexName(parsed.warehouseId, parsed.modelType);
if (!(await indexExists(queryInterface, tableName, indexName))) {
continue;
}
await queryInterface.sequelize.query(
`ALTER TABLE \`${tableName}\` DROP INDEX \`${indexName}\`, ALGORITHM=INPLACE, LOCK=NONE`
);
}
},
};

View File

@ -0,0 +1,69 @@
'use strict';
/**
* 为运单表新增 OMS 取号返回的打印定位字段。
*
* - `print_history_id`:OMS 返回的打印记录ID(店管家 PrintHistoryIds[0].id),
* 打印前用它回 `deliveryorder.getprintdata` 换新的抖音 params;
* - `cai_niao_auth_info_id`:菜鸟授权网点ID(每个店铺唯一),
* 打印时按它做店铺分组,一组只需一个 print_history_id 换一次 params。
*/
module.exports = {
/**
* 执行迁移:新增 print_history_id / cai_niao_auth_info_id(幂等)。
*
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @param {import('sequelize')} Sequelize Sequelize 实例。
* @return {Promise<void>} 无返回值。
*/
async up(queryInterface, Sequelize) {
const tableName = 'wms_order_express';
const columns = await queryInterface.describeTable(tableName);
const definitions = [];
if (!columns.print_history_id) {
definitions.push(
`ADD COLUMN \`print_history_id\` ${Sequelize.STRING(64)} NULL DEFAULT NULL COMMENT 'OMS 打印记录ID(打印前换新 params 用)' AFTER \`encryptedtoken\``
);
}
if (!columns.cai_niao_auth_info_id) {
const afterColumn = definitions.length > 0 ? 'print_history_id' : 'encryptedtoken';
definitions.push(
`ADD COLUMN \`cai_niao_auth_info_id\` ${Sequelize.BIGINT(20).UNSIGNED} NULL DEFAULT NULL COMMENT '菜鸟授权网点ID(OMS 取号返回,打印按店铺分组用)' AFTER \`${afterColumn}\``
);
}
if (definitions.length === 0) {
return;
}
// 拿不到元数据锁时快速失败:MDL 排队会把后续所有读写一起堵住,
// 30 秒内拿不到锁就报错退出(可安全重试),避免拖垮生产可用性
await queryInterface.sequelize.query('SET SESSION lock_wait_timeout = 30');
// 两条 ADD COLUMN 合并为一次 ALTER:MySQL 5.7 加列需重建表,分两次会重建两遍
await queryInterface.sequelize.query(
`ALTER TABLE \`${tableName}\` ${definitions.join(', ')}`
);
},
/**
* 回滚迁移:删除新增字段(幂等)。
*
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @return {Promise<void>} 无返回值。
*/
async down(queryInterface) {
const tableName = 'wms_order_express';
const columns = await queryInterface.describeTable(tableName);
if (columns.cai_niao_auth_info_id) {
await queryInterface.removeColumn(tableName, 'cai_niao_auth_info_id');
}
if (columns.print_history_id) {
await queryInterface.removeColumn(tableName, 'print_history_id');
}
},
};