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;