feat(order): 小团购拣货篮号打印,批量查询订单级与波次级 box_no 映射并透传到各平台面单模板,PDA 扫码与计划创建两种写入场景兼容
This commit is contained in:
parent
007babb6c6
commit
0610f2528d
@ -468,9 +468,11 @@ function resolveItemQuantity(item) {
|
||||
* @param {number} [options.fallbackItemsCount] 订单级商品种类数(items_count),仅当商品总数与明细累加均为 0 时兜底。
|
||||
* @param {number} [options.waveSource] 波次来源(wms_wave_batch.source),与 waveType 共同决定商品明细行尾是否追加「波次总单数×数量」。
|
||||
* @param {number} [options.waveType] 波次类型(wms_wave_batch.wave_type)。
|
||||
* @param {number} [options.boxNo] 小团购篮号(box_no),waveType=1 且 waveSource∈{1,2}(策略任务波次)时前置到波次号前。
|
||||
* @param {string} [options.packageMaterialName] 推荐包材名称,非空时追加到波次号前。
|
||||
* @return {Array<string>} 自定义区文本行数组。首行头部分在快递渠道名后追加 商品总数【n】(优先取订单级商品总数 items_total,其次商品明细数量累加,最后 items_count 兜底);
|
||||
* 当 waveSource=0 且 waveType=1 时,商品明细行尾追加「波次总单数×该行商品数量」。
|
||||
* 当 waveSource=0 且 waveType=1 时,商品明细行尾追加「波次总单数×该行商品数量」;
|
||||
* 小团购(waveType=1 且 waveSource∈{1,2})时波次号前追加篮号「{boxNo}-」。
|
||||
*/
|
||||
function buildWaybillCustomProductInfoLines(items, policyConfig = {}, options = {}) {
|
||||
const safeItems = Array.isArray(items) ? items : [];
|
||||
@ -555,12 +557,17 @@ function buildWaybillCustomProductInfoLines(items, policyConfig = {}, options =
|
||||
const hasWaveCustomPrefix = waveTotalOrders > 0
|
||||
&& waveIndex > 0
|
||||
&& Boolean(waveNo);
|
||||
// 小团购波次(wave_type=1 且 source∈{1,2} 策略任务):篮号前置到波次号前,其他波次类型不动
|
||||
const boxNo = toNumber(safeOptions.boxNo, 0);
|
||||
const isSmallGroupWave = waveType === 1 && (waveSource === 1 || waveSource === 2) && boxNo > 0;
|
||||
const packageMaterialName = String(safeOptions.packageMaterialName || '').trim();
|
||||
const packageMaterialLabel = packageMaterialName ? `【${packageMaterialName}】` : '';
|
||||
const waveBodyText = hasWaveCustomPrefix
|
||||
? `${waveNo} [${waveTotalOrders}-${waveIndex}]`
|
||||
: waveNo;
|
||||
const waveHeaderText = packageMaterialLabel + waveBodyText;
|
||||
const waveHeaderText = isSmallGroupWave
|
||||
? packageMaterialLabel + `${boxNo}-${waveBodyText}`
|
||||
: packageMaterialLabel + waveBodyText;
|
||||
|
||||
// 商品总数:优先取订单级商品总数(items_total,权威字段),
|
||||
// 其次累加所有商品明细的数量,最后用商品种类数(items_count)兜底。
|
||||
@ -1330,6 +1337,7 @@ function applyKuaishouExpressFieldMapping(platform, skeleton, orderExpress, orde
|
||||
fallbackItemsCount: Number(safeOrderDelivery.items_count || 0),
|
||||
waveSource: Number(safeOrderDelivery.wave_source || 0),
|
||||
waveType: Number(safeOrderDelivery.wave_type || 0),
|
||||
boxNo: Number(safeOrderDelivery.box_no || 0),
|
||||
}
|
||||
);
|
||||
const customProductInfo = appendExtraCustomLines(
|
||||
@ -1483,6 +1491,7 @@ function applyPddExpressFieldMapping(platform, skeleton, orderExpress, orderDeli
|
||||
fallbackItemsCount: Number(safeOrderDelivery.items_count || 0),
|
||||
waveSource: Number(safeOrderDelivery.wave_source || 0),
|
||||
waveType: Number(safeOrderDelivery.wave_type || 0),
|
||||
boxNo: Number(safeOrderDelivery.box_no || 0),
|
||||
}
|
||||
);
|
||||
const customProductInfo = appendExtraCustomLines(
|
||||
@ -1679,6 +1688,7 @@ function applyDouyinExpressFieldMapping(platform, skeleton, orderExpress, orderD
|
||||
fallbackItemsCount: Number(safeOrderDelivery.items_count || 0),
|
||||
waveSource: Number(safeOrderDelivery.wave_source || 0),
|
||||
waveType: Number(safeOrderDelivery.wave_type || 0),
|
||||
boxNo: Number(safeOrderDelivery.box_no || 0),
|
||||
}
|
||||
);
|
||||
const customProductInfo = appendExtraCustomLines(
|
||||
@ -1822,6 +1832,7 @@ function applyCainiaoExpressFieldMapping(
|
||||
fallbackItemsCount: Number(safeOrderDelivery.items_count || 0),
|
||||
waveSource: Number(safeOrderDelivery.wave_source || 0),
|
||||
waveType: Number(safeOrderDelivery.wave_type || 0),
|
||||
boxNo: Number(safeOrderDelivery.box_no || 0),
|
||||
}
|
||||
);
|
||||
const customControlText = appendExtraCustomLines(
|
||||
@ -2032,6 +2043,7 @@ function applyXhsExpressFieldMapping(
|
||||
fallbackItemsCount: Number(safeOrderDelivery.items_count || 0),
|
||||
waveSource: Number(safeOrderDelivery.wave_source || 0),
|
||||
waveType: Number(safeOrderDelivery.wave_type || 0),
|
||||
boxNo: Number(safeOrderDelivery.box_no || 0),
|
||||
}
|
||||
);
|
||||
const customProductInfo = appendExtraCustomLines(
|
||||
@ -2151,6 +2163,7 @@ function applySphExpressFieldMapping(
|
||||
fallbackItemsCount: Number(safeOrderDelivery.items_count || 0),
|
||||
waveSource: Number(safeOrderDelivery.wave_source || 0),
|
||||
waveType: Number(safeOrderDelivery.wave_type || 0),
|
||||
boxNo: Number(safeOrderDelivery.box_no || 0),
|
||||
}
|
||||
);
|
||||
const customProductInfo = appendExtraCustomLines(
|
||||
@ -2326,6 +2339,7 @@ function applyJdExpressFieldMapping(
|
||||
fallbackItemsCount: Number(safeOrderDelivery.items_count || 0),
|
||||
waveSource: Number(safeOrderDelivery.wave_source || 0),
|
||||
waveType: Number(safeOrderDelivery.wave_type || 0),
|
||||
boxNo: Number(safeOrderDelivery.box_no || 0),
|
||||
}
|
||||
);
|
||||
const customProductInfo = appendExtraCustomLines(
|
||||
|
||||
@ -665,6 +665,11 @@ class OrderPrintSystemService extends OrderBaseService {
|
||||
normalizedWarehouseId,
|
||||
validOrderWaveIds
|
||||
);
|
||||
const pickTaskBoxNoMap = await this.querySmallGroupBoxNoMap(
|
||||
orderIds,
|
||||
normalizedWarehouseId,
|
||||
new Map(unlockedOrderRows.map(row => [ Number(row && row.id || 0), Number(row && row.wave_id || 0) ]))
|
||||
);
|
||||
const waveIdsForCount = validOrderWaveIds;
|
||||
const waveTotalOrderCountMap = await this.queryWaveTotalOrderCountMap(waveIdsForCount, normalizedWarehouseId);
|
||||
const templateSampleCache = new Map();
|
||||
@ -684,6 +689,7 @@ class OrderPrintSystemService extends OrderBaseService {
|
||||
const waveIndex = Number(waveOrderIndexInfo.wave_index || 0);
|
||||
const platformsIndex = Number(waveOrderIndexInfo.platforms_index || 0);
|
||||
const waveMetaRow = waveMetaMap.get(waveIdForCount) || {};
|
||||
const pickTaskBoxInfo = pickTaskBoxNoMap.get(orderId) || {};
|
||||
const effectiveOrderRowWithWaveIndex = {
|
||||
...effectiveOrderRow,
|
||||
wave_total_orders: waveTotalOrders > 0 ? waveTotalOrders : 0,
|
||||
@ -691,6 +697,7 @@ class OrderPrintSystemService extends OrderBaseService {
|
||||
platforms_index: platformsIndex > 0 ? platformsIndex : 0,
|
||||
wave_source: Number(waveMetaRow.source || 0),
|
||||
wave_type: Number(waveMetaRow.wave_type || 0),
|
||||
box_no: Number(pickTaskBoxInfo.box_no || 0),
|
||||
};
|
||||
const storeId = Number(orderRow && orderRow.store_id || 0);
|
||||
const policyConfig = policyConfigMap.get(storeId) || {};
|
||||
@ -878,6 +885,11 @@ class OrderPrintSystemService extends OrderBaseService {
|
||||
normalizedWarehouseId,
|
||||
validOrderWaveIds
|
||||
);
|
||||
const pickTaskBoxNoMap = await this.querySmallGroupBoxNoMap(
|
||||
orderIdsForExpress,
|
||||
normalizedWarehouseId,
|
||||
new Map(validOrderRows.map(row => [ Number(row && row.id || 0), Number(row && row.wave_id || 0) ]))
|
||||
);
|
||||
const waveIdsForCount = validOrderWaveIds;
|
||||
const waveTotalOrderCountMap = await this.queryWaveTotalOrderCountMap(waveIdsForCount, normalizedWarehouseId);
|
||||
const templateSampleCache = new Map();
|
||||
@ -897,6 +909,7 @@ class OrderPrintSystemService extends OrderBaseService {
|
||||
const waveIndex = Number(waveOrderIndexInfo.wave_index || 0);
|
||||
const platformsIndex = Number(waveOrderIndexInfo.platforms_index || 0);
|
||||
const waveMetaRow = waveMetaMap.get(waveIdForCount) || {};
|
||||
const pickTaskBoxInfo = pickTaskBoxNoMap.get(orderId) || {};
|
||||
const effectiveOrderRowWithWaveIndex = {
|
||||
...effectiveOrderRow,
|
||||
wave_total_orders: waveTotalOrders > 0 ? waveTotalOrders : 0,
|
||||
@ -904,6 +917,7 @@ class OrderPrintSystemService extends OrderBaseService {
|
||||
platforms_index: platformsIndex > 0 ? platformsIndex : 0,
|
||||
wave_source: Number(waveMetaRow.source || 0),
|
||||
wave_type: Number(waveMetaRow.wave_type || 0),
|
||||
box_no: Number(pickTaskBoxInfo.box_no || 0),
|
||||
};
|
||||
const storeId = Number(orderRow && orderRow.store_id || 0);
|
||||
const policyConfig = policyConfigMap.get(storeId) || {};
|
||||
@ -1149,6 +1163,11 @@ class OrderPrintSystemService extends OrderBaseService {
|
||||
const waveOrderIndexMap = await this.queryWaveOrderIndexMapByOrderIds(
|
||||
orderIdsForIndex, normalizedWarehouseId, validOrderWaveIds
|
||||
);
|
||||
const pickTaskBoxNoMap = await this.querySmallGroupBoxNoMap(
|
||||
orderIdsForIndex,
|
||||
normalizedWarehouseId,
|
||||
new Map(validOrderRows.map(row => [ Number(row && row.id || 0), Number(row && row.wave_id || 0) ]))
|
||||
);
|
||||
const waveTotalOrderCountMap = await this.queryWaveTotalOrderCountMap(validOrderWaveIds, normalizedWarehouseId);
|
||||
const templateSampleCache = new Map();
|
||||
|
||||
@ -1166,6 +1185,7 @@ class OrderPrintSystemService extends OrderBaseService {
|
||||
const waveIndex = Number(waveOrderIndexInfo.wave_index || 0);
|
||||
const platformsIndex = Number(waveOrderIndexInfo.platforms_index || 0);
|
||||
const waveMetaRow = waveMetaMap.get(waveIdForCount) || {};
|
||||
const pickTaskBoxInfo = pickTaskBoxNoMap.get(orderId) || {};
|
||||
const effectiveOrderRowWithWaveIndex = {
|
||||
...effectiveOrderRow,
|
||||
wave_total_orders: waveTotalOrders > 0 ? waveTotalOrders : 0,
|
||||
@ -1173,6 +1193,7 @@ class OrderPrintSystemService extends OrderBaseService {
|
||||
platforms_index: platformsIndex > 0 ? platformsIndex : 0,
|
||||
wave_source: Number(waveMetaRow.source || 0),
|
||||
wave_type: Number(waveMetaRow.wave_type || 0),
|
||||
box_no: Number(pickTaskBoxInfo.box_no || 0),
|
||||
};
|
||||
|
||||
const storeId = Number(orderRow.store_id || 0);
|
||||
@ -2101,6 +2122,121 @@ ${waveFilterSql}
|
||||
return resultMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量查询小团购波次(wave_type=1 且 source∈{1,2} 策略任务)的拣货篮号映射。
|
||||
*
|
||||
* 小团购判定由调用方根据 wms_wave_batch.wave_type/source 完成,本方法只负责取篮号。
|
||||
* 篮号存在两种写入场景,均需兼容:
|
||||
* - PDA 扫码创建任务(systemMethods/pick.js createWavePickTask):订单级篮号写入
|
||||
* wms_wave_pick_task_order.box_no(订单所属最小篮号),订单级优先;
|
||||
* - 计划创建任务(systemMethods/order/wave.js createWavePickTaskFromPlan):篮号写入
|
||||
* wms_wave_pick_task_wave.box_no(每波次一篮),任务订单表不含小团购订单行,
|
||||
* 按订单所属 wave_id 兜底取波次级篮号。
|
||||
*
|
||||
* 同一订单/波次可能命中多个任务,按 task_id DESC 取最新任务的记录。
|
||||
* 查询失败时优雅降级返回空映射。
|
||||
*
|
||||
* @param {Array<number>} orderIds 订单ID数组。
|
||||
* @param {number} warehouseId 仓库ID。
|
||||
* @param {Map<number, number>} [orderWaveIdMap] 订单ID到波次ID的映射,用于波次级篮号兜底。
|
||||
* @return {Promise<Map<number, {box_no:number}>>} 订单ID到拣货篮号映射。
|
||||
*/
|
||||
async querySmallGroupBoxNoMap(orderIds = [], warehouseId, orderWaveIdMap = null) {
|
||||
const resultMap = new Map();
|
||||
const normalizedOrderIds = Array.from(new Set((Array.isArray(orderIds) ? orderIds : [])
|
||||
.map(id => Number(id))
|
||||
.filter(id => Number.isInteger(id) && id > 0)));
|
||||
if (normalizedOrderIds.length === 0) {
|
||||
return resultMap;
|
||||
}
|
||||
|
||||
const queryMethod = this.ctx.model && this.ctx.model.query;
|
||||
if (typeof queryMethod !== 'function') {
|
||||
return resultMap;
|
||||
}
|
||||
|
||||
const safeOrderWaveIdMap = orderWaveIdMap instanceof Map ? orderWaveIdMap : new Map();
|
||||
|
||||
try {
|
||||
// 1) 订单级篮号(PDA 扫码创建任务):同一订单多条时取最新任务(task_id DESC)
|
||||
const orderRows = await queryMethod.call(this.ctx.model, `
|
||||
SELECT order_id, box_no
|
||||
FROM wms_wave_pick_task_order
|
||||
WHERE warehouse_id = :warehouseId
|
||||
AND order_id IN (:orderIds)
|
||||
ORDER BY task_id DESC
|
||||
`, {
|
||||
replacements: {
|
||||
warehouseId,
|
||||
orderIds: normalizedOrderIds,
|
||||
},
|
||||
type: this.ctx.model.QueryTypes && this.ctx.model.QueryTypes.SELECT,
|
||||
});
|
||||
|
||||
for (const row of Array.isArray(orderRows) ? orderRows : []) {
|
||||
const orderId = Number(row && row.order_id || 0);
|
||||
const boxNo = Number(row && row.box_no || 0);
|
||||
if (orderId > 0 && boxNo > 0 && !resultMap.has(orderId)) {
|
||||
resultMap.set(orderId, { box_no: boxNo });
|
||||
}
|
||||
}
|
||||
|
||||
// 2) 波次级篮号兜底(计划创建任务):未命中订单按其 wave_id 取波次篮号
|
||||
const backfillWaveIds = new Set();
|
||||
for (const orderId of normalizedOrderIds) {
|
||||
if (resultMap.has(orderId)) {
|
||||
continue;
|
||||
}
|
||||
const waveId = Number(safeOrderWaveIdMap.get(orderId) || 0);
|
||||
if (waveId > 0) {
|
||||
backfillWaveIds.add(waveId);
|
||||
}
|
||||
}
|
||||
|
||||
if (backfillWaveIds.size > 0) {
|
||||
const waveRows = await queryMethod.call(this.ctx.model, `
|
||||
SELECT wave_id, box_no
|
||||
FROM wms_wave_pick_task_wave
|
||||
WHERE warehouse_id = :warehouseId
|
||||
AND wave_id IN (:waveIds)
|
||||
ORDER BY task_id DESC
|
||||
`, {
|
||||
replacements: {
|
||||
warehouseId,
|
||||
waveIds: Array.from(backfillWaveIds),
|
||||
},
|
||||
type: this.ctx.model.QueryTypes && this.ctx.model.QueryTypes.SELECT,
|
||||
});
|
||||
|
||||
const waveBoxNoMap = new Map();
|
||||
for (const row of Array.isArray(waveRows) ? waveRows : []) {
|
||||
const waveId = Number(row && row.wave_id || 0);
|
||||
const boxNo = Number(row && row.box_no || 0);
|
||||
if (waveId > 0 && boxNo > 0 && !waveBoxNoMap.has(waveId)) {
|
||||
waveBoxNoMap.set(waveId, boxNo);
|
||||
}
|
||||
}
|
||||
|
||||
for (const orderId of normalizedOrderIds) {
|
||||
if (resultMap.has(orderId)) {
|
||||
continue;
|
||||
}
|
||||
const waveId = Number(safeOrderWaveIdMap.get(orderId) || 0);
|
||||
const boxNo = waveBoxNoMap.get(waveId) || 0;
|
||||
if (waveId > 0 && boxNo > 0) {
|
||||
resultMap.set(orderId, { box_no: boxNo });
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
this.ctx.logger.warn(
|
||||
`[orderPrint.querySmallGroupBoxNoMap] 查询失败 warehouseId=${warehouseId} message=${error.message}`
|
||||
);
|
||||
}
|
||||
|
||||
return resultMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* 按波次ID批量查询波次总订单数。
|
||||
*
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user