perf(query): 运单定位优先序列表直查,减少三表循环扫描
This commit is contained in:
parent
37104079e5
commit
2e95fb19d4
@ -538,7 +538,7 @@ class InboxFunctionsService extends Service {
|
||||
|
||||
// ========== 第一步:运单号精确匹配 ==========
|
||||
const exactExpress = await ctx.model.OrderExpress.findOne({
|
||||
attributes: [ 'delivery_order_code' ],
|
||||
attributes: [ 'delivery_order_code', 'order_id' ],
|
||||
where: {
|
||||
waybill_code: keyword,
|
||||
store_id: storeId,
|
||||
@ -548,11 +548,38 @@ class InboxFunctionsService extends Service {
|
||||
});
|
||||
|
||||
if (exactExpress && exactExpress.delivery_order_code) {
|
||||
// 用运单关联的发货单号精确查找订单
|
||||
found = await findInDeliveryTables({
|
||||
store_id: storeId,
|
||||
delivery_order_code: exactExpress.delivery_order_code,
|
||||
});
|
||||
// 运单精确匹配:先通过序列表定位订单所在分表,命中只查一张表;
|
||||
// 未命中(存量订单无指针)回退三表按单号遍历
|
||||
const exactOrderId = Number(exactExpress.order_id || 0);
|
||||
if (exactOrderId > 0 && factory && typeof factory.getOrderLocation === 'function') {
|
||||
try {
|
||||
const locatedAt = await factory.getOrderLocation(exactOrderId, warehouseId);
|
||||
if (locatedAt) {
|
||||
const locatedCfg = tableConfigs.find(cfg => cfg.key === locatedAt);
|
||||
if (locatedCfg) {
|
||||
const locatedModel = await locatedCfg.getModel(factory, warehouseId);
|
||||
if (locatedModel) {
|
||||
const locatedRow = await locatedModel.findOne({
|
||||
attributes,
|
||||
where: { id: exactOrderId, store_id: storeId },
|
||||
raw: true,
|
||||
});
|
||||
if (locatedRow) {
|
||||
found = { table: locatedCfg.key, table_label: locatedCfg.label, ...locatedRow };
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (_err) { /* 定位失败,降级三表遍历 */ }
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
// 用运单关联的发货单号精确查找订单
|
||||
found = await findInDeliveryTables({
|
||||
store_id: storeId,
|
||||
delivery_order_code: exactExpress.delivery_order_code,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// ========== 第二步:运单未命中 → 模糊搜索发货单号 / 平台单号 ==========
|
||||
@ -3054,16 +3081,32 @@ class InboxFunctionsService extends Service {
|
||||
const orderMissingWbs = [];
|
||||
|
||||
for (const e of validList) {
|
||||
// 第二步:查订单行(当前表 → 历史表 → 配送表),取波次信息
|
||||
// 第二步:查订单行(优先序列表定位,未命中回退 当前→历史→配送 遍历)
|
||||
let orderRow = null;
|
||||
const factory = ctx.model.OrderDeliveryFactory;
|
||||
const tableGetters = [
|
||||
(f, wid) => f.getCurrentModel(wid),
|
||||
(f, wid) => f.getHistoryModel(wid),
|
||||
(f, wid) => f.getDispatchModel(wid),
|
||||
{ location: 'current', getModel: (f, wid) => f.getCurrentModel(wid) },
|
||||
{ location: 'history', getModel: (f, wid) => f.getHistoryModel(wid) },
|
||||
{ location: 'dispatch', getModel: (f, wid) => f.getDispatchModel(wid) },
|
||||
];
|
||||
for (const getModel of tableGetters) {
|
||||
|
||||
let location = null;
|
||||
try {
|
||||
if (factory && typeof factory.getOrderLocation === 'function') {
|
||||
location = await factory.getOrderLocation(e.order_id, warehouseId);
|
||||
}
|
||||
} catch (err) { /* ignore */ }
|
||||
|
||||
const orderedGetters = location
|
||||
? [
|
||||
...tableGetters.filter(entry => entry.location === location),
|
||||
...tableGetters.filter(entry => entry.location !== location),
|
||||
]
|
||||
: tableGetters;
|
||||
|
||||
for (const entry of orderedGetters) {
|
||||
try {
|
||||
const model = await getModel(ctx.model.OrderDeliveryFactory, warehouseId);
|
||||
const model = await entry.getModel(factory, warehouseId);
|
||||
if (!model) continue;
|
||||
orderRow = await model.findOne({
|
||||
attributes: [ 'id', 'wave_id', 'wave_no', 'pack_status' ],
|
||||
|
||||
@ -318,6 +318,16 @@ class OrderExpressService extends Service {
|
||||
{ label: 'dispatch', getModel: () => orderDeliveryFactory.getDispatchModel(warehouseId) },
|
||||
];
|
||||
|
||||
// 序列表定位优先:命中则先查对应分表,未命中(存量订单)按原顺序回退遍历
|
||||
try {
|
||||
if (orderDeliveryFactory && typeof orderDeliveryFactory.getOrderLocation === 'function') {
|
||||
const preferredLocation = await orderDeliveryFactory.getOrderLocation(normalizedOrderId, warehouseId);
|
||||
if (preferredLocation) {
|
||||
orderTableOrders.sort((a, b) => (a.label === preferredLocation ? -1 : b.label === preferredLocation ? 1 : 0));
|
||||
}
|
||||
}
|
||||
} catch (_err) { /* 定位失败,保持原遍历顺序 */ }
|
||||
|
||||
let orderRow = null;
|
||||
let orderDeliveryModel = null;
|
||||
for (const entry of orderTableOrders) {
|
||||
@ -440,6 +450,16 @@ class OrderExpressService extends Service {
|
||||
{ label: 'dispatch', getModel: () => orderDeliveryFactory.getDispatchModel(warehouseId) },
|
||||
];
|
||||
|
||||
// 序列表定位优先:命中则先查对应分表,未命中(存量订单)按原顺序回退遍历
|
||||
try {
|
||||
if (orderDeliveryFactory && typeof orderDeliveryFactory.getOrderLocation === 'function') {
|
||||
const preferredLocation = await orderDeliveryFactory.getOrderLocation(orderId, warehouseId);
|
||||
if (preferredLocation) {
|
||||
orderTableOrders.sort((a, b) => (a.label === preferredLocation ? -1 : b.label === preferredLocation ? 1 : 0));
|
||||
}
|
||||
}
|
||||
} catch (_err) { /* 定位失败,保持原遍历顺序 */ }
|
||||
|
||||
let orderRow = null;
|
||||
let orderDeliveryModel = null;
|
||||
for (const entry of orderTableOrders) {
|
||||
|
||||
@ -73,6 +73,16 @@ class WaybillService extends MainService {
|
||||
},
|
||||
];
|
||||
|
||||
// 序列表定位优先:命中则先查对应分表,未命中(存量订单)按原顺序回退遍历
|
||||
try {
|
||||
if (orderDeliveryFactory && typeof orderDeliveryFactory.getOrderLocation === 'function') {
|
||||
const preferredLocation = await orderDeliveryFactory.getOrderLocation(orderId, warehouseId);
|
||||
if (preferredLocation) {
|
||||
tableOrders.sort((a, b) => (a.label === preferredLocation ? -1 : b.label === preferredLocation ? 1 : 0));
|
||||
}
|
||||
}
|
||||
} catch (_err) { /* 定位失败,保持原遍历顺序 */ }
|
||||
|
||||
// 遍历多表查询订单的 wave_id 和 wave_no
|
||||
let orderRow = null;
|
||||
for (const entry of tableOrders) {
|
||||
|
||||
@ -848,6 +848,16 @@ class WorkbenchService extends Service {
|
||||
{ label: 'dispatch', getModel: () => orderDeliveryFactory.getDispatchModel(warehouseId) },
|
||||
];
|
||||
|
||||
// 序列表定位优先:命中则先查对应分表,未命中(存量订单)按原顺序回退遍历
|
||||
try {
|
||||
if (orderDeliveryFactory && typeof orderDeliveryFactory.getOrderLocation === 'function') {
|
||||
const preferredLocation = await orderDeliveryFactory.getOrderLocation(orderId, warehouseId);
|
||||
if (preferredLocation) {
|
||||
tableOrders.sort((a, b) => (a.label === preferredLocation ? -1 : b.label === preferredLocation ? 1 : 0));
|
||||
}
|
||||
}
|
||||
} catch (_err) { /* 定位失败,保持原遍历顺序 */ }
|
||||
|
||||
let orderRow = null;
|
||||
let orderDeliveryModel = null;
|
||||
for (const entry of tableOrders) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user