liufumingren 5d7964132e 1
2026-07-03 16:56:39 +08:00

132 lines
3.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
const MainService = require('../main');
/**
* 业务逻辑层:waybill(运单号快速查询)。
*/
class WaybillService extends MainService {
/**
* 根据运单号查询有效订单ID,并返回该订单所属波次下所有订单ID列表。
*
* 查询流程:
* 1. 通过运单号查询 wms_order_express 表获取 order_id 和 warehouse_id。
* 2. 通过 order_id 遍历多表(current/history/dispatch)获取 wave_id 和 wave_no。
* 3. 若有有效波次(wave_id > 0),遍历多表查询该波次下所有订单的 ID 列表。
*
* @param {Object} params 查询参数。
* @param {string} params.waybill_code 运单号,不可为空。
* @return {Promise<Object>} 包含 order_id 和 waves 数组的对象。
* @throws {Error} 当 waybill_code 为空或未找到匹配记录时抛出业务错误。
*/
async toOrder({ waybill_code: waybillCode }) {
const { ctx } = this;
if (!waybillCode || !String(waybillCode).trim()) {
this.throwError('运单号不能为空');
}
const record = await ctx.model.OrderExpress.findOne({
where: {
waybill_code: String(waybillCode).trim(),
status: 1,
},
attributes: [ 'order_id', 'warehouse_id' ],
raw: true,
});
if (!record) {
this.throwError('未找到该运单号对应的订单');
}
const { order_id: orderId, warehouse_id: expressWarehouseId } = record;
// 获取 warehouse_id:优先从 ctx.user.store_id,回退到运单记录中的 warehouse_id
const warehouseId =
(ctx.user && ctx.user.store_id) ? ctx.user.store_id : expressWarehouseId;
// 构建多表遍历查询队列:current → history → dispatch
const orderDeliveryFactory =
ctx.model && ctx.model.OrderDeliveryFactory;
const tableOrders = [
{
label: 'current',
getModel: () => orderDeliveryFactory.getCurrentModel(warehouseId),
},
{
label: 'history',
getModel: () => orderDeliveryFactory.getHistoryModel(warehouseId),
},
{
label: 'dispatch',
getModel: () => orderDeliveryFactory.getDispatchModel(warehouseId),
},
];
// 遍历多表查询订单的 wave_id 和 wave_no
let orderRow = null;
for (const entry of tableOrders) {
try {
const model = await entry.getModel();
if (!model || typeof model.findOne !== 'function') {
continue;
}
const row = await model.findOne({
where: { id: orderId, warehouse_id: warehouseId },
attributes: [ 'id', 'wave_id', 'wave_no' ],
raw: true,
});
if (row) {
orderRow = row;
break;
}
} catch (_err) {
continue;
}
}
// 无订单记录或 wave_id 无效,返回空 waves
if (!orderRow || !orderRow.wave_id || Number(orderRow.wave_id) <= 0) {
return { order_id: orderId, waves: [] };
}
const { wave_id: waveId, wave_no: waveNo } = orderRow;
// 遍历多表查询该波次下所有订单的 ID 列表
const orderIds = [];
for (const entry of tableOrders) {
try {
const model = await entry.getModel();
if (!model || typeof model.findAll !== 'function') {
continue;
}
const rows = await model.findAll({
where: { wave_id: waveId, warehouse_id: warehouseId },
attributes: [ 'id' ],
raw: true,
});
for (const row of rows) {
if (!orderIds.includes(row.id)) {
orderIds.push(row.id);
}
}
} catch (_err) {
continue;
}
}
return {
order_id: orderId,
waves: [
{
wave_id: waveId,
wave_no: waveNo,
order_ids: orderIds,
},
],
};
}
}
module.exports = WaybillService;