From eca309d41f496830daa971f0a13a0a155bb66593 Mon Sep 17 00:00:00 2001 From: liufumingren Date: Fri, 21 Aug 2026 02:20:26 +0800 Subject: [PATCH] =?UTF-8?q?fix(print):=20=E6=89=93=E5=8D=B0=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E5=8C=BA=E5=90=8C=E5=BA=93=E4=BD=8D=E4=B8=8D?= =?UTF-8?q?=E5=90=8C=E5=95=86=E5=93=81=E4=B8=8D=E5=86=8D=E5=90=88=E5=B9=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 聚合键由库位改为 库位+stock_id 复合键, 仅同库位同商品合并数量 - stock_id 缺失时依次回退 order_item_id、商品名称+条码, 无法识别商品身份时不再跨行合并 --- app/service/systems/order_print.js | 52 ++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 7 deletions(-) diff --git a/app/service/systems/order_print.js b/app/service/systems/order_print.js index 96925d7..8a0940b 100644 --- a/app/service/systems/order_print.js +++ b/app/service/systems/order_print.js @@ -172,6 +172,40 @@ class OrderPrintSystemService extends OrderBaseService { return ''; } + /** + * 构建打印项聚合键。 + * + * 同一库位下的拣货行只有商品身份一致时才允许合并: + * - 优先 stock_id(>0):同一库位同一商品合并数量; + * - stock_id 缺失时回退 order_item_id(同一订单商品行); + * - 再缺失时回退 商品名称+条码 兜底; + * - 均无法识别时不做跨行合并(每行独立)。 + * + * @param {Object} item 打印项。 + * @return {string} 聚合键。 + */ + buildPrintItemAggregationKey(item = {}) { + const source = this.isPlainObject(item) ? item : {}; + const location = String(source.pick_location || '').trim(); + const stockId = Number(source.stock_id || 0); + if (stockId > 0) { + return `${location}::stock:${stockId}`; + } + + const orderItemId = Number(source.order_item_id || 0); + if (orderItemId > 0) { + return `${location}::order_item:${orderItemId}`; + } + + const name = String(source.item_name || '').trim(); + const barcode = String(source.barcode || '').trim(); + if (name || barcode) { + return `${location}::name:${name}|${barcode}`; + } + + return `${location}::id:${Math.random()}`; + } + /** * 批量补查打印所需的拣货明细,并注入商品多条码。 * @@ -377,20 +411,19 @@ class OrderPrintSystemService extends OrderBaseService { // 为每条打印项注入 barcodes 数组,供 resolveItemBarcodeText 优先读取 for (const item of printItems) { - const stockId = item.stock_id; + const stockId = Number(item.stock_id || 0); + item.stock_id = stockId > 0 ? stockId : 0; - if (stockId && barcodeMap.has(stockId)) { + if (stockId > 0 && barcodeMap.has(stockId)) { item.barcodes = barcodeMap.get(stockId); } - - // 清理内部字段,避免污染 JSON 输出 - delete item.stock_id; } - // 按库位(pick_location)聚合,同一库位的商品合并为一行 + // 按「库位 + 商品(stock_id)」聚合:同一库位的同一商品合并为一行, + // 同一库位的不同商品保持独立行,避免数量、条码被错误合并。 const aggregatedMap = new Map(); for (const item of printItems) { - const key = item.pick_location || ''; + const key = this.buildPrintItemAggregationKey(item); const existing = aggregatedMap.get(key); if (existing) { // 累加数量 @@ -414,6 +447,11 @@ class OrderPrintSystemService extends OrderBaseService { } const mergedPrintItems = Array.from(aggregatedMap.values()); + // 聚合完成后清理内部字段,避免污染 JSON 输出 + for (const item of mergedPrintItems) { + delete item.stock_id; + } + // 为聚合后的商品行添加序号(从 1 开始) for (let i = 0; i < mergedPrintItems.length; i++) { mergedPrintItems[i].seq_no = i + 1;