新增 app/lib/order_lookup.js 共享辅助函数(preloadOrderModels/findOrderAcrossTables/ findOrderByCodeAcrossTables),按 current→dispatch→history→outsourced 顺序命中即停; 模型预取置于事务外(工厂首次获取可能执行 DDL 隐式提交),事务内查询支持透传行锁。 改造 4 处接口: - qimen deliveryorder.query / openapi deliveryorder.query:跨表查单, 订单流转到配送/历史表后不再误报"未找到订单"(避免货主误判重推); - qimen order.cancel / openapi order.cancel:跨表查找,命中非 current 表 且 status=cancel 时幂等返回取消成功,其余情况返回"未找到对应的订单"。 回归验证(真实网关,脚本在 test/ 不入库): - openapi:query 命中 dispatch、cancel 幂等成功、不存在单号报错 - qimen:同 4 场景全部符合预期
106 lines
4.2 KiB
JavaScript
106 lines
4.2 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* 订单分表跨表查询共享工具。
|
||
*
|
||
* 订单按仓库分表且按生命周期流转于多张物理表之间:
|
||
* current(待出库)→ dispatch(配送)→ history(历史归档),另有 outsourced(外包)。
|
||
* 各分表唯一索引均为 (delivery_order_code, store_id, warehouse_id),跨表间约束独立。
|
||
* 任何"按单号判断订单是否存在/可用"的场景都必须跨表遍历,否则会误判
|
||
* (如订单已流转到 dispatch 后,仅查 current 会返回"未找到订单",诱导调用方重推)。
|
||
*
|
||
* 注意事项:
|
||
* - 分表模型首次获取可能执行 DDL(隐式提交),因此必须在事务外调用 preloadOrderModels
|
||
* 完成模型预取,再在事务内用 findOrderAcrossTables 查询;
|
||
* - findOrderByCodeAcrossTables 内部会预取模型,仅适用于非事务场景。
|
||
*/
|
||
|
||
/** 默认跨表查找顺序:current → dispatch → history → outsourced。 */
|
||
const TABLE_TYPE_ORDER = [ 'current', 'dispatch', 'history', 'outsourced' ];
|
||
|
||
/** 表位 → OrderDeliveryFactory 模型获取方法名。 */
|
||
const MODEL_GETTER_BY_TYPE = {
|
||
current: 'getCurrentModel',
|
||
dispatch: 'getDispatchModel',
|
||
history: 'getHistoryModel',
|
||
outsourced: 'getOutsourcedModel',
|
||
};
|
||
|
||
/**
|
||
* 预取订单分表模型。
|
||
* 必须在事务外调用:模型首次获取可能执行 DDL(隐式提交)。
|
||
*
|
||
* @param {Object} ctx egg ctx。
|
||
* @param {string|number} warehouseId 仓库ID(分表后缀)。
|
||
* @param {string[]} [tableTypes] 需要预取的表位,默认四表。
|
||
* @return {Promise<Object>} 表位 → 模型 的映射(缺失模型不会出现在结果中)。
|
||
*/
|
||
async function preloadOrderModels(ctx, warehouseId, tableTypes = TABLE_TYPE_ORDER) {
|
||
const factory = ctx && ctx.model && ctx.model.OrderDeliveryFactory;
|
||
if (!factory) {
|
||
throw new Error('OrderDeliveryFactory 不可用,无法跨表查询订单');
|
||
}
|
||
const models = {};
|
||
for (const tableType of tableTypes) {
|
||
const getter = MODEL_GETTER_BY_TYPE[tableType];
|
||
if (!getter || typeof factory[getter] !== 'function') {
|
||
continue;
|
||
}
|
||
const model = await factory[getter](warehouseId);
|
||
if (model && typeof model.findOne === 'function') {
|
||
models[tableType] = model;
|
||
}
|
||
}
|
||
return models;
|
||
}
|
||
|
||
/**
|
||
* 跨表按条件查找订单,按 tableTypes 顺序命中即停。
|
||
*
|
||
* @param {Object} models preloadOrderModels 返回的表位 → 模型映射。
|
||
* @param {Object} where 查询条件(findOne where,例如 delivery_order_code + store_id + warehouse_id)。
|
||
* @param {Object} [options] 可选配置。
|
||
* @param {string[]} [options.tableTypes] 查找表位顺序,默认四表。
|
||
* @param {Object} [options.findOptions] 透传给 findOne 的其余选项(transaction/lock/attributes 等)。
|
||
* @return {Promise<{ order: Object|null, location: string|null }>} 命中订单与所在表位;未命中 order=null。
|
||
*/
|
||
async function findOrderAcrossTables(models, where, options = {}) {
|
||
const { tableTypes = TABLE_TYPE_ORDER, findOptions = {} } = options;
|
||
for (const tableType of tableTypes) {
|
||
const model = models && models[tableType];
|
||
if (!model || typeof model.findOne !== 'function') {
|
||
continue;
|
||
}
|
||
const order = await model.findOne({
|
||
where,
|
||
...findOptions,
|
||
});
|
||
if (order) {
|
||
return { order, location: tableType };
|
||
}
|
||
}
|
||
return { order: null, location: null };
|
||
}
|
||
|
||
/**
|
||
* 便捷入口:预取模型后按条件跨表查找(仅限非事务场景)。
|
||
* 事务内请使用 preloadOrderModels + findOrderAcrossTables。
|
||
*
|
||
* @param {Object} ctx egg ctx。
|
||
* @param {string|number} warehouseId 仓库ID。
|
||
* @param {Object} where 查询条件。
|
||
* @param {Object} [options] 同 findOrderAcrossTables 的 options,另支持 tableTypes 用于预取过滤。
|
||
* @return {Promise<{ order: Object|null, location: string|null }>} 命中订单与所在表位;未命中 order 为 null。
|
||
*/
|
||
async function findOrderByCodeAcrossTables(ctx, warehouseId, where, options = {}) {
|
||
const models = await preloadOrderModels(ctx, warehouseId, options.tableTypes);
|
||
return findOrderAcrossTables(models, where, options);
|
||
}
|
||
|
||
module.exports = {
|
||
TABLE_TYPE_ORDER,
|
||
preloadOrderModels,
|
||
findOrderAcrossTables,
|
||
findOrderByCodeAcrossTables,
|
||
};
|