370 lines
11 KiB
JavaScript
370 lines
11 KiB
JavaScript
'use strict';
|
||
|
||
const { Op } = require('sequelize');
|
||
const OrderBaseService = require('../systemMethods/order/base');
|
||
|
||
class OrderPrintSystemService extends OrderBaseService {
|
||
/**
|
||
* 获取波次打印数据。
|
||
*
|
||
* @param {Object} params 请求参数。
|
||
* @param {Array<number|string>} params.waveIds 波次ID数组。
|
||
* @param {number|string} params.warehouseId 仓库ID。
|
||
* @return {Promise<Object>} 通用打印数据结构。
|
||
*/
|
||
async getWavePrintData(params = {}) {
|
||
const requestParams = this.isPlainObject(params) ? params : {};
|
||
const {
|
||
waveIds,
|
||
warehouseId,
|
||
} = requestParams;
|
||
|
||
const normalizedWaveIds = this.normalizeWaveIds(waveIds);
|
||
if (normalizedWaveIds.length === 0) {
|
||
throw new Error('wave_ids 必填,且必须为非空数组');
|
||
}
|
||
|
||
const normalizedWarehouseId = this.resolveWarehouseId(warehouseId);
|
||
const waveRows = await this.queryWavesByIds(normalizedWarehouseId, normalizedWaveIds);
|
||
if (waveRows.length === 0) {
|
||
return this.buildEmptyResult(normalizedWaveIds);
|
||
}
|
||
|
||
const hitWaveIdSet = new Set(waveRows.map(row => Number(row.wave_id)));
|
||
const missingWaveIds = normalizedWaveIds.filter(waveId => !hitWaveIdSet.has(waveId));
|
||
const hitWaveIds = normalizedWaveIds.filter(waveId => hitWaveIdSet.has(waveId));
|
||
const orderRows = await this.queryOrdersByWaveIds(hitWaveIds, normalizedWarehouseId);
|
||
const {
|
||
validWaveIds,
|
||
filteredWaveIds,
|
||
} = this.filterFullyAssignedExpressWaves(hitWaveIds, orderRows);
|
||
|
||
return this.buildWavePrintResult({
|
||
waveRows,
|
||
orderRows,
|
||
hitWaveIds: validWaveIds,
|
||
missingWaveIds,
|
||
filteredWaveIds,
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 规范化波次ID数组(去重与数字化)。
|
||
*
|
||
* @param {Array<number|string>} waveIds 原始波次ID数组。
|
||
* @return {Array<number>} 规范化后的波次ID数组。
|
||
*/
|
||
normalizeWaveIds(waveIds = []) {
|
||
if (!Array.isArray(waveIds)) {
|
||
return [];
|
||
}
|
||
|
||
const normalizedSet = new Set();
|
||
|
||
for (const rawId of waveIds) {
|
||
const normalizedId = Number(String(rawId ?? '').trim());
|
||
if (Number.isInteger(normalizedId) && normalizedId > 0) {
|
||
normalizedSet.add(normalizedId);
|
||
}
|
||
}
|
||
|
||
return Array.from(normalizedSet);
|
||
}
|
||
|
||
/**
|
||
* 解析并校验仓库ID。
|
||
*
|
||
* @param {number|string} warehouseId 入参仓库ID。
|
||
* @return {number} 规范化后的仓库ID。
|
||
*/
|
||
resolveWarehouseId(warehouseId) {
|
||
const contextWarehouseId = Number(this.getWarehouseId() || 0);
|
||
if (!Number.isInteger(contextWarehouseId) || contextWarehouseId <= 0) {
|
||
throw new Error('缺少当前用户仓库上下文 store_id');
|
||
}
|
||
|
||
const normalizedWarehouseId = Number(String(warehouseId ?? '').trim());
|
||
if (!Number.isInteger(normalizedWarehouseId) || normalizedWarehouseId <= 0) {
|
||
throw new Error('warehouse_id 必填且必须为正整数');
|
||
}
|
||
|
||
if (normalizedWarehouseId !== contextWarehouseId) {
|
||
throw new Error('warehouse_id 必须与当前用户上下文 store_id 保持一致');
|
||
}
|
||
|
||
return normalizedWarehouseId;
|
||
}
|
||
|
||
/**
|
||
* 按波次ID批量查询波次信息。
|
||
*
|
||
* @param {number} warehouseId 仓库ID。
|
||
* @param {Array<number>} waveIds 波次ID数组。
|
||
* @return {Promise<Array<Object>>} 波次基础信息。
|
||
*/
|
||
async queryWavesByIds(warehouseId, waveIds = []) {
|
||
if (!Array.isArray(waveIds) || waveIds.length === 0) {
|
||
return [];
|
||
}
|
||
|
||
const querySql = `
|
||
SELECT wave_id, wave_no
|
||
FROM wms_wave_batch
|
||
WHERE warehouse_id = :warehouseId
|
||
AND wave_id IN (:waveIds)
|
||
AND wave_status <> -1
|
||
`;
|
||
|
||
const rows = await this.ctx.model.query(querySql, {
|
||
replacements: {
|
||
warehouseId,
|
||
waveIds,
|
||
},
|
||
type: this.ctx.model.QueryTypes.SELECT,
|
||
});
|
||
|
||
return Array.isArray(rows) ? rows : [];
|
||
}
|
||
|
||
/**
|
||
* 按波次ID批量查询订单明细。
|
||
*
|
||
* @param {Array<number>} waveIds 波次ID数组。
|
||
* @param {number} warehouseId 仓库ID。
|
||
* @return {Promise<Array<Object>>} 订单明细列表。
|
||
*/
|
||
async queryOrdersByWaveIds(waveIds = [], warehouseId) {
|
||
if (!Array.isArray(waveIds) || waveIds.length === 0) {
|
||
return [];
|
||
}
|
||
|
||
const orderModel = await this.getOrderModel('order', warehouseId);
|
||
const rows = await orderModel.findAll({
|
||
attributes: [
|
||
'id',
|
||
'delivery_order_code',
|
||
'source_order_codes',
|
||
'wave_id',
|
||
'wave_no',
|
||
'name',
|
||
'tel',
|
||
'mobile',
|
||
'province',
|
||
'city',
|
||
'area',
|
||
'town',
|
||
'detail_address',
|
||
'sys_platform_code',
|
||
'sys_platform_name',
|
||
'logistics_name',
|
||
'post_status',
|
||
'status',
|
||
'outbound_status',
|
||
'print_status',
|
||
'items_count',
|
||
'items_total',
|
||
'items_weight',
|
||
'create_at',
|
||
'pay_time',
|
||
],
|
||
where: {
|
||
wave_id: {
|
||
[Op.in]: waveIds,
|
||
},
|
||
},
|
||
order: [[ 'wave_id', 'ASC' ], [ 'id', 'ASC' ]],
|
||
raw: true,
|
||
});
|
||
|
||
return Array.isArray(rows) ? rows : [];
|
||
}
|
||
|
||
/**
|
||
* 过滤快递未完全分配的波次。
|
||
*
|
||
* 规则:仅当波次下全部订单的 post_status 都为 fin 时,才视为快递已完全分配。
|
||
*
|
||
* @param {Array<number>} hitWaveIds 命中的波次ID数组。
|
||
* @param {Array<Object>} orderRows 波次订单列表。
|
||
* @return {{ validWaveIds: Array<number>, filteredWaveIds: Array<Object> }} 过滤结果。
|
||
*/
|
||
filterFullyAssignedExpressWaves(hitWaveIds = [], orderRows = []) {
|
||
const statsMap = new Map();
|
||
for (const waveId of hitWaveIds) {
|
||
statsMap.set(Number(waveId), {
|
||
total: 0,
|
||
fin: 0,
|
||
});
|
||
}
|
||
|
||
for (const row of orderRows) {
|
||
const waveId = Number(row && row.wave_id || 0);
|
||
const stats = statsMap.get(waveId);
|
||
if (!stats) {
|
||
continue;
|
||
}
|
||
|
||
const postStatus = String(row && row.post_status || '').trim();
|
||
stats.total += 1;
|
||
if (postStatus === 'fin') {
|
||
stats.fin += 1;
|
||
}
|
||
}
|
||
|
||
const validWaveIds = [];
|
||
const filteredWaveIds = [];
|
||
for (const waveId of hitWaveIds) {
|
||
const stats = statsMap.get(waveId) || { total: 0, fin: 0 };
|
||
const isFullyAssigned = stats.total > 0 && stats.fin === stats.total;
|
||
if (isFullyAssigned) {
|
||
validWaveIds.push(waveId);
|
||
} else {
|
||
filteredWaveIds.push({
|
||
wave_id: waveId,
|
||
reason: '快递未完全分配',
|
||
});
|
||
}
|
||
}
|
||
|
||
return {
|
||
validWaveIds,
|
||
filteredWaveIds,
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 生成空结果结构。
|
||
*
|
||
* @param {Array<number>} normalizedWaveIds 规范化后的波次ID数组。
|
||
* @return {Object} 空结果。
|
||
*/
|
||
buildEmptyResult(normalizedWaveIds = []) {
|
||
return {
|
||
summary: {
|
||
total_waves: 0,
|
||
total_orders: 0,
|
||
},
|
||
wave_list: [],
|
||
ext: {
|
||
missing_wave_ids: normalizedWaveIds,
|
||
filtered_wave_ids: [],
|
||
},
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 组装通用波次打印返回结构。
|
||
*
|
||
* @param {Object} payload 聚合输入。
|
||
* @param {Array<Object>} payload.waveRows 波次列表。
|
||
* @param {Array<Object>} payload.orderRows 订单列表。
|
||
* @param {Array<number>} payload.hitWaveIds 命中的波次ID数组。
|
||
* @param {Array<number>} payload.missingWaveIds 未命中的波次ID数组。
|
||
* @param {Array<Object>} payload.filteredWaveIds 被过滤的波次及原因。
|
||
* @return {Object} 波次打印返回结果。
|
||
*/
|
||
buildWavePrintResult(payload = {}) {
|
||
const {
|
||
waveRows = [],
|
||
orderRows = [],
|
||
hitWaveIds = [],
|
||
missingWaveIds = [],
|
||
filteredWaveIds = [],
|
||
} = this.isPlainObject(payload) ? payload : {};
|
||
|
||
const waveMetaMap = new Map();
|
||
for (const row of waveRows) {
|
||
const waveId = Number(row && row.wave_id || 0);
|
||
if (!Number.isInteger(waveId) || waveId <= 0) {
|
||
continue;
|
||
}
|
||
|
||
waveMetaMap.set(waveId, {
|
||
wave_id: waveId,
|
||
wave_no: String(row && row.wave_no || '').trim(),
|
||
});
|
||
}
|
||
|
||
const groupedWaveMap = new Map();
|
||
for (const waveId of hitWaveIds) {
|
||
const waveMeta = waveMetaMap.get(waveId);
|
||
if (!waveMeta) {
|
||
continue;
|
||
}
|
||
|
||
groupedWaveMap.set(waveId, {
|
||
wave_id: waveMeta.wave_id,
|
||
wave_no: waveMeta.wave_no,
|
||
order_count: 0,
|
||
orders: [],
|
||
});
|
||
}
|
||
|
||
for (const order of orderRows) {
|
||
const waveId = Number(order && order.wave_id || 0);
|
||
const groupedWave = groupedWaveMap.get(waveId);
|
||
if (!groupedWave) {
|
||
continue;
|
||
}
|
||
|
||
groupedWave.orders.push({
|
||
order_id: Number(order && order.id || 0),
|
||
order_no: String(order && (order.delivery_order_code || order.source_order_codes) || '').trim(),
|
||
wave_id: waveId,
|
||
wave_no: String(order && order.wave_no || '').trim() || groupedWave.wave_no,
|
||
receiver: {
|
||
name: String(order && order.name || '').trim(),
|
||
mobile: String(order && order.mobile || '').trim(),
|
||
tel: String(order && order.tel || '').trim(),
|
||
province: String(order && order.province || '').trim(),
|
||
city: String(order && order.city || '').trim(),
|
||
area: String(order && order.area || '').trim(),
|
||
town: String(order && order.town || '').trim(),
|
||
detail_address: String(order && order.detail_address || '').trim(),
|
||
},
|
||
channel: {
|
||
sys_platform_code: String(order && order.sys_platform_code || '').trim(),
|
||
sys_platform_name: String(order && order.sys_platform_name || '').trim(),
|
||
logistics_name: String(order && order.logistics_name || '').trim(),
|
||
},
|
||
status: {
|
||
order_status: String(order && order.status || '').trim(),
|
||
outbound_status: String(order && order.outbound_status || '').trim(),
|
||
print_status: String(order && order.print_status || '').trim(),
|
||
},
|
||
metrics: {
|
||
items_count: Number(order && order.items_count || 0),
|
||
items_total: Number(order && order.items_total || 0),
|
||
items_weight: Number(order && order.items_weight || 0),
|
||
},
|
||
times: {
|
||
create_at: order && order.create_at || null,
|
||
pay_time: order && order.pay_time || null,
|
||
},
|
||
});
|
||
}
|
||
|
||
const waveList = Array.from(groupedWaveMap.values());
|
||
waveList.sort((a, b) => String(a.wave_no || '').localeCompare(String(b.wave_no || '')));
|
||
let totalOrders = 0;
|
||
for (const wave of waveList) {
|
||
wave.order_count = wave.orders.length;
|
||
totalOrders += wave.order_count;
|
||
}
|
||
|
||
return {
|
||
summary: {
|
||
total_waves: waveList.length,
|
||
total_orders: totalOrders,
|
||
},
|
||
wave_list: waveList,
|
||
ext: {
|
||
missing_wave_ids: missingWaveIds,
|
||
filtered_wave_ids: filteredWaveIds,
|
||
},
|
||
};
|
||
}
|
||
}
|
||
|
||
module.exports = OrderPrintSystemService;
|