From 4f4128f337b51a8ac512db69b10129237d031ab8 Mon Sep 17 00:00:00 2001 From: liufumingren Date: Tue, 15 Sep 2026 16:14:46 +0800 Subject: [PATCH] =?UTF-8?q?fix(returnUnpack):=20=E6=89=B9=E9=87=8F?= =?UTF-8?q?=E8=BD=AC=E5=85=A5=E9=80=80=E4=BB=B6=E7=99=BB=E8=AE=B0=E5=85=A5?= =?UTF-8?q?=E5=8F=82=E6=94=B9=E4=B8=BA=E8=BF=90=E5=8D=95=E8=A1=A8=E4=B8=BB?= =?UTF-8?q?=E9=94=AE=20ids?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 入参由 waybill_codes(运单号)改为 ids(wms_order_express.id,单个数值或数组):Number 归一化 + 过滤非正整数 + 去重,上限 200 - 反查改为一次批量查询 id IN (ids) + warehouse_id,未命中(不存在或非本仓库)计入 failed;单条失败不阻断主流程 - _transferOneWaybill 改为接收批量反查后的运单行;运单号为空时报「运单号为空,无法转入退件登记」(拆包单 waybill_code 必填) - 出参:success 项新增 waybill_id;failed 项为 waybill_id / waybill_code(运单命中时,否则 null)/ reason - 回归:test/return_unpack_transfer_regression.test.js 76/76 通过(含 ids 边界、他仓隔离、运单号为空等新口径) --- app/service/systemMethods/returnUnpack.js | 6 +- app/service/systems/return_unpack.js | 89 ++++++++++++++--------- 2 files changed, 59 insertions(+), 36 deletions(-) diff --git a/app/service/systemMethods/returnUnpack.js b/app/service/systemMethods/returnUnpack.js index a094698..b17fc14 100644 --- a/app/service/systemMethods/returnUnpack.js +++ b/app/service/systemMethods/returnUnpack.js @@ -69,13 +69,13 @@ class ReturnUnpackMethodService extends MainService { * 新方法需鉴权 + 需登录 + 记录日志,不在任何免检清单内。 * * @param {Object} data 请求体。 - * @param {string|Array} data.waybill_codes 运单号(单个字符串或数组,去重后上限 200)。 + * @param {number|Array} data.ids 运单表主键ID(wms_order_express.id,单个数值或数组,去重后上限 200)。 * @return {Promise} 统一 success 包装,data 含 total/success_count/failed_count/success/failed。 */ async transferByWaybills(data = {}) { - const { waybill_codes } = data; + const { ids } = data; return this._call( - () => this.service.systems.returnUnpack.transferByWaybills({ waybill_codes }), + () => this.service.systems.returnUnpack.transferByWaybills({ ids }), '运单批量转入退件登记失败' ); } diff --git a/app/service/systems/return_unpack.js b/app/service/systems/return_unpack.js index d37dc2f..d7cacab 100644 --- a/app/service/systems/return_unpack.js +++ b/app/service/systems/return_unpack.js @@ -269,30 +269,32 @@ class ReturnUnpackService extends Service { /** * 运单批量转入退件登记(退回拆包,批量)。 - * 批量入参运单号数组:去重 + trim + 过滤空值,单次上限 200;逐单处理,单条失败不阻断主流程。 + * 批量入参运单表主键 ID(wms_order_express.id):过滤非正整数 + 去重,单次上限 200; + * 逐单处理,单条失败不阻断主流程。 * 命中的运单统一标记 wms_order_express.is_back='Y'(不做其他前置条件限制); * 幂等:同仓库 + 同运单号已存在有效(status != 'cancelled')拆包单时直接复用, * 复用单明细为空时按订单明细补写(优先 items JSON,为空回退明细分表)。 * * @param {Object} params 批量转入参数。 - * @param {string|Array} params.waybill_codes 运单号(单个字符串或数组)。 + * @param {Array|number} params.ids 运单表主键ID(wms_order_express.id,单个数值或数组)。 * @return {Promise} { total, success_count, failed_count, success[], failed[] }, - * 成功项含 waybill_code/unpack_id/unpack_no/reused/detail_count/total_quantity, - * 失败项含 waybill_code/reason。 + * 成功项含 waybill_id/waybill_code/unpack_id/unpack_no/reused/detail_count/total_quantity, + * 失败项含 waybill_id/waybill_code(运单命中时)/reason。 */ - async transferByWaybills({ waybill_codes }) { - const { ctx } = this; + async transferByWaybills({ ids }) { + const { ctx, app } = this; + const { Op } = app.Sequelize; - // 入参归一化:单个字符串转数组,trim + 去重 + 过滤空值 - const rawCodes = Array.isArray(waybill_codes) ? waybill_codes : [ waybill_codes ]; - const waybillCodes = [ ...new Set( - rawCodes.map(code => String(code == null ? '' : code).trim()).filter(Boolean) + // 入参归一化:单个数值转数组,过滤非正整数 + 去重 + const rawIds = Array.isArray(ids) ? ids : [ ids ]; + const waybillIds = [ ...new Set( + rawIds.map(id => Number(id)).filter(id => Number.isInteger(id) && id > 0) ) ]; - if (waybillCodes.length === 0) { - throw this.createBusinessError('waybill_codes 不能为空', 400); + if (waybillIds.length === 0) { + throw this.createBusinessError('ids 不能为空', 400); } - if (waybillCodes.length > 200) { + if (waybillIds.length > 200) { throw this.createBusinessError('单次最多批量转入 200 个运单', 400); } @@ -311,15 +313,33 @@ class ReturnUnpackService extends Service { throw this.createBusinessError('缺少拆包人员工号上下文', 400); } + // 批量反查运单:一次查询完成 运单ID + 仓库 命中判定(未命中的 ID 记为失败) + const expressMap = new Map(); + try { + const expressRows = await ctx.model.OrderExpress.findAll({ + where: { id: { [Op.in]: waybillIds }, warehouse_id: warehouseId }, + attributes: [ 'id', 'waybill_code', 'order_id', 'store_id', 'source_order_codes', 'business_key' ], + raw: true, + }); + for (const row of expressRows) { + expressMap.set(Number(row.id), row); + } + } catch (err) { + ctx.logger.warn(`[returnUnpack.transferByWaybills] 批量反查运单失败: ${err.message}`); + throw this.createBusinessError('反查运单失败,请稍后重试', 500); + } + /** @type {Array} 成功项列表 */ const success = []; /** @type {Array} 失败项列表 */ const failed = []; - for (const waybillCode of waybillCodes) { + for (const waybillId of waybillIds) { + const orderExpress = expressMap.get(waybillId) || null; try { const item = await this._transferOneWaybill({ - waybillCode, + orderExpress, + waybillId, warehouseId, unpackerId, unpackerName, @@ -327,13 +347,17 @@ class ReturnUnpackService extends Service { success.push(item); } catch (err) { const reason = (err && err.message) || '转入失败'; - ctx.logger.warn(`[returnUnpack.transferByWaybills] 运单 ${waybillCode} 转入失败: ${reason}`); - failed.push({ waybill_code: waybillCode, reason }); + ctx.logger.warn(`[returnUnpack.transferByWaybills] 运单ID ${waybillId} 转入失败: ${reason}`); + failed.push({ + waybill_id: waybillId, + waybill_code: orderExpress ? String(orderExpress.waybill_code || '') : null, + reason, + }); } } return { - total: waybillCodes.length, + total: waybillIds.length, success_count: success.length, failed_count: failed.length, success, @@ -2008,31 +2032,22 @@ class ReturnUnpackService extends Service { } /** - * 处理单个运单的批量转入:反查原运单 → 标记退回 → 幂等复用/新建拆包单 → 复制订单明细。 + * 处理单个运单的批量转入:校验运单 → 标记退回 → 幂等复用/新建拆包单 → 复制订单明细。 * 子单(business_key > 1)跳过;一个运单只允许一条有效拆包单。 * * @param {Object} params 单运单处理参数。 - * @param {string} params.waybillCode 运单号。 + * @param {Object|null} params.orderExpress 调用方批量反查得到的运单行;null=不存在或不属于当前仓库。 + * @param {number} params.waybillId 运单表主键ID。 * @param {number} params.warehouseId 仓库ID。 * @param {number} params.unpackerId 拆包人员工号ID。 * @param {string} params.unpackerName 拆包人员工号名称。 - * @return {Promise} 成功项:{ waybill_code, unpack_id, unpack_no, reused, detail_count, total_quantity }。 + * @return {Promise} 成功项:{ waybill_id, waybill_code, unpack_id, unpack_no, reused, detail_count, total_quantity }。 */ - async _transferOneWaybill({ waybillCode, warehouseId, unpackerId, unpackerName }) { + async _transferOneWaybill({ orderExpress, waybillId, warehouseId, unpackerId, unpackerName }) { const { ctx, app } = this; const { Op } = app.Sequelize; - // 1. 反查原运单(不做其他前置条件限制;按仓库隔离,避免跨仓运单被转入本仓拆包单) - let orderExpress = null; - try { - orderExpress = await ctx.model.OrderExpress.findOne({ - where: { waybill_code: waybillCode, warehouse_id: warehouseId }, - attributes: [ 'id', 'order_id', 'store_id', 'source_order_codes', 'business_key' ], - raw: true, - }); - } catch (err) { - ctx.logger.warn(`[returnUnpack.transferByWaybills] 反查原运单失败 ${waybillCode}: ${err.message}`); - } + // 1. 校验运单(调用方已按 运单ID + 仓库 批量反查,避免跨仓运单被转入本仓拆包单) if (!orderExpress) { throw this.createBusinessError('运单不存在', 404); } @@ -2042,6 +2057,12 @@ class ReturnUnpackService extends Service { throw this.createBusinessError('运单为子单(business_key>1),跳过', 400); } + // 运单号是拆包单的必填业务键:未取号的运单无可登记的运单号 + const waybillCode = String(orderExpress.waybill_code || '').trim(); + if (!waybillCode) { + throw this.createBusinessError('运单号为空,无法转入退件登记', 400); + } + // 2. 命中的运单统一标记退回(不做其他前置条件限制) try { await ctx.model.OrderExpress.update( @@ -2120,6 +2141,7 @@ class ReturnUnpackService extends Service { } return { + waybill_id: waybillId, waybill_code: waybillCode, unpack_id: unpackId, unpack_no: existing.unpack_no || '', @@ -2183,6 +2205,7 @@ class ReturnUnpackService extends Service { const summary = await this._recalcMain(unpackId, warehouseId); return { + waybill_id: waybillId, waybill_code: waybillCode, unpack_id: unpackId, unpack_no: createdRow.unpack_no || '',