330 lines
9.8 KiB
JavaScript

'use strict';
const Service = require('egg').Service;
const REVIEWED_INSPECTION_STATUS = 'fin';
class PickSystemService extends Service {
/**
* 按仓库获取订单分表模型。
*
* @param {number} warehouseId 仓库ID。
* @return {Promise<Object|null>} 订单模型对象。
*/
async getOrderModel(warehouseId) {
const factory = this.ctx.model && this.ctx.model.OrderDeliveryFactory;
if (!factory || typeof factory.getCurrentModel !== 'function') {
throw this.createBusinessError('订单分表模型工厂不可用', 500);
}
const model = await factory.getCurrentModel(warehouseId);
if (!model || typeof model.findOne !== 'function' || typeof model.findAll !== 'function') {
throw this.createBusinessError('订单分表模型不可用', 500);
}
return model;
}
/**
* 生成带业务状态码的错误对象。
*
* @param {string} message 错误文案。
* @param {number} [code=400] 错误码。
* @return {Error} 错误对象。
*/
createBusinessError(message, code = 400) {
const error = new Error(message);
error.code = code;
return error;
}
/**
* 解析订单所属波次信息。
*
* @param {Object} order 订单记录。
* @return {{waveId: number, waveNo: string}} 波次信息。
*/
resolveWaveInfo(order = {}) {
const waveId = Number(order.wave_id || 0);
const waveNo = String(order.wave_no || '').trim();
return {
waveId: Number.isFinite(waveId) ? waveId : 0,
waveNo,
};
}
/**
* 判断订单是否已复核。
*
* 当前使用 `inspection_status === "fin"` 作为已复核判定。
*
* @param {Object} order 订单记录。
* @return {boolean} 是否已复核。
*/
isOrderReviewed(order = {}) {
return String(order.inspection_status || '').trim() === REVIEWED_INSPECTION_STATUS;
}
/**
* 根据波次信息构建订单查询条件。
*
* @param {Object} waveInfo 波次信息。
* @param {number} warehouseId 仓库ID。
* @return {Object} Sequelize 查询条件。
*/
buildWaveOrderWhere(waveInfo = {}, warehouseId) {
const { Op } = this.app.Sequelize;
const where = {
warehouse_id: warehouseId,
};
if (waveInfo.waveId > 0 && waveInfo.waveNo) {
where[Op.or] = [
{ wave_id: waveInfo.waveId },
{ wave_no: waveInfo.waveNo },
];
return where;
}
if (waveInfo.waveId > 0) {
where.wave_id = waveInfo.waveId;
return where;
}
where.wave_no = waveInfo.waveNo;
return where;
}
/**
* 按波次信息查询拣货汇总。
*
* @param {Object} params 查询参数。
* @param {number} params.orderId 当前订单ID。
* @param {number} params.warehouseId 仓库ID。
* @param {string} [params.waveNo] 波次号。
* @param {number} [params.waveId] 波次ID。
* @param {number} [params.storeId] 货主ID。
* @param {boolean} params.fullDisplay 是否完整显示。
* @return {Promise<Object>} 波次拣货汇总结果。
*/
async queryPickingInfo(params = {}) {
const { Op } = this.app.Sequelize;
const { ctx } = this;
const {
orderId,
warehouseId,
waveNo,
waveId,
storeId,
fullDisplay,
} = params;
const normalizedOrderId = Number(orderId || 0);
if (!Number.isFinite(normalizedOrderId) || normalizedOrderId <= 0) {
throw this.createBusinessError('order_id 参数不合法', 400);
}
const normalizedWarehouseId = Number(warehouseId || 0);
if (!Number.isFinite(normalizedWarehouseId) || normalizedWarehouseId <= 0) {
throw this.createBusinessError('缺少当前用户仓库上下文 store_id', 400);
}
const waveInfo = {
waveId: Number(waveId || 0),
waveNo: String(waveNo || '').trim(),
};
if (waveInfo.waveId <= 0 && !waveInfo.waveNo) {
throw this.createBusinessError('订单未分配波次号,暂无拣货信息', 400);
}
const orderModel = await this.getOrderModel(normalizedWarehouseId);
const waveOrderRows = await orderModel.findAll({
where: this.buildWaveOrderWhere(waveInfo, normalizedWarehouseId),
attributes: [ 'id', 'inspection_status', 'store_id' ],
raw: true,
});
const orderIds = Array.isArray(waveOrderRows)
? waveOrderRows
.map(item => Number(item && item.id || 0))
.filter(id => id > 0)
: [];
if (orderIds.length === 0) {
throw this.createBusinessError('未找到波次对应订单', 404);
}
const orderStatusMap = new Map();
for (const row of waveOrderRows) {
const id = Number(row && row.id || 0);
if (id <= 0) {
continue;
}
orderStatusMap.set(id, {
inspection_status: String(row && row.inspection_status || '').trim(),
});
}
const pickingRows = await ctx.model.Picking.findAll({
where: {
order_id: {
[Op.in]: orderIds,
},
},
order: [[ 'id', 'ASC' ]],
raw: true,
});
const spaceIds = Array.isArray(pickingRows)
? Array.from(new Set(pickingRows
.map(item => Number(item && item.space_id || 0))
.filter(id => id > 0)))
: [];
const locationRows = spaceIds.length > 0
? await ctx.model.LocationSpace.findAll({
where: {
id: {
[Op.in]: spaceIds,
},
warehouse_id: normalizedWarehouseId,
},
attributes: [
'id',
'space_code',
'custom_name',
'custom_barcode',
'zone_id',
'space_status',
'is_enabled',
'pick_sequence',
'pick_priority',
],
raw: true,
})
: [];
const locationMap = new Map();
for (const location of Array.isArray(locationRows) ? locationRows : []) {
const locationId = Number(location && location.id || 0);
if (locationId > 0) {
locationMap.set(locationId, location);
}
}
const currentStoreId = Number(storeId || (waveOrderRows[0] && waveOrderRows[0].store_id) || 0);
const storeWarehouse = await ctx.model.StoreWarehouse.findOne({
where: {
store_id: currentStoreId,
warehouse_id: normalizedWarehouseId,
},
attributes: [ 'id', 'store_id', 'warehouse_id', 'store_remask', 'warehouse_remask', 'auth_status', 'use_status' ],
raw: true,
});
const fullDisplayFlag = Boolean(fullDisplay);
const allDetails = (Array.isArray(pickingRows) ? pickingRows : []).map(row => {
const currentOrderId = Number(row && row.order_id || 0);
const orderStatus = orderStatusMap.get(currentOrderId) || { inspection_status: 'none' };
const reviewed = this.isOrderReviewed(orderStatus);
const spaceId = Number(row && row.space_id || 0);
return {
id: Number(row && row.id || 0),
order_id: currentOrderId,
order_item_id: Number(row && row.order_item_id || 0),
storage_id: Number(row && row.storage_id || 0),
quantity: Number(row && row.quantity || 0),
space_id: spaceId,
space_name: String(row && row.space_name || ''),
picking_status: String(row && row.status || ''),
inspection_status: orderStatus.inspection_status,
reviewed,
location_space: locationMap.get(spaceId) || null,
store_warehouse: storeWarehouse || null,
};
});
const pendingReviewDetails = allDetails.filter(item => !item.reviewed);
const reviewedDetails = allDetails.filter(item => item.reviewed);
const displayDetails = fullDisplayFlag ? allDetails : pendingReviewDetails;
const sumQuantity = rows => rows.reduce((sum, item) => sum + Number(item && item.quantity || 0), 0);
return {
order_id: normalizedOrderId,
wave_id: waveInfo.waveId,
wave_no: waveInfo.waveNo,
full_display: fullDisplayFlag,
summary: {
total_count: allDetails.length,
pending_review_count: pendingReviewDetails.length,
reviewed_count: reviewedDetails.length,
quantity_total: sumQuantity(allDetails),
display_count: displayDetails.length,
display_quantity_total: sumQuantity(displayDetails),
},
list: displayDetails,
};
}
/**
* 按订单ID查询其波次拣货信息。
*
* @param {Object} params 查询参数。
* @param {number} params.orderId 订单ID。
* @param {number} params.warehouseId 仓库ID。
* @param {boolean} params.fullDisplay 是否完整显示。
* @return {Promise<Object>} 波次拣货汇总结果。
*/
async queryByOrder(params = {}) {
const {
orderId,
warehouseId,
fullDisplay,
} = params;
const normalizedOrderId = Number(orderId || 0);
if (!Number.isFinite(normalizedOrderId) || normalizedOrderId <= 0) {
throw this.createBusinessError('order_id 参数不合法', 400);
}
const normalizedWarehouseId = Number(warehouseId || 0);
if (!Number.isFinite(normalizedWarehouseId) || normalizedWarehouseId <= 0) {
throw this.createBusinessError('缺少当前用户仓库上下文 store_id', 400);
}
const orderModel = await this.getOrderModel(normalizedWarehouseId);
const order = await orderModel.findOne({
where: {
id: normalizedOrderId,
warehouse_id: normalizedWarehouseId,
},
attributes: [ 'id', 'store_id', 'wave_no', 'wave_id' ],
raw: true,
});
if (!order) {
throw this.createBusinessError('订单不存在', 404);
}
const waveInfo = this.resolveWaveInfo(order);
if (waveInfo.waveId <= 0 && !waveInfo.waveNo) {
throw this.createBusinessError('订单未分配波次号,暂无拣货信息', 400);
}
return await this.queryPickingInfo({
orderId: normalizedOrderId,
warehouseId: normalizedWarehouseId,
waveNo: waveInfo.waveNo,
waveId: waveInfo.waveId,
storeId: Number(order.store_id || 0),
fullDisplay,
});
}
}
module.exports = PickSystemService;