From f8915a5733b08f99bd837d9ebfc606971a33d5ca Mon Sep 17 00:00:00 2001 From: liufumingren Date: Thu, 27 Aug 2026 17:58:52 +0800 Subject: [PATCH] =?UTF-8?q?fix(openapi/storage):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=91=BD=E5=90=8D=E5=86=B2=E7=AA=81=E3=80=81?= =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=AF=BC=E5=85=A5=E5=85=A5=E5=BA=93=E6=94=B6?= =?UTF-8?q?=E8=B4=A7=E5=8D=95=E5=8D=A0=E7=94=A8=E4=B8=8E=E5=85=A5=E5=BA=93?= =?UTF-8?q?=E5=8D=95=E8=AF=A6=E6=83=85=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - openapi.js 重命名为 openapiEntry.js,消除与 openapi/ 目录同名冲突,恢复 ctx.service.openapi.callback 可用,修复 quickInbound API 回传报 Cannot read properties of undefined (reading 'sendWithRetry') - 导入/PDA 入库 commit 新增非绑定模式收货单占用:带 receiving_detail_id 的记录优先按明细扣减,剩余导入量按 stock_id 匹配绑定收货单明细占用并回写收货单入库状态 - storage.entry.get 响应新增 total_order_lines / total_quantity / received_quantity 字段 - 同步 AI_METHOD_ROUTING.md 与 methods/changes 变更记录 --- AI_METHOD_ROUTING.md | 2 +- app/controller/openapi.js | 2 +- app/service/{openapi.js => openapiEntry.js} | 0 app/service/systems/inbound_direct_log.js | 70 ++++++++++++++++++++- app/service/systems/storage/entry.js | 7 ++- methods/changes/openapi.json | 12 ++++ methods/changes/system.json | 35 +++++++++++ 7 files changed, 124 insertions(+), 4 deletions(-) rename app/service/{openapi.js => openapiEntry.js} (100%) diff --git a/AI_METHOD_ROUTING.md b/AI_METHOD_ROUTING.md index 75ff282..f268883 100644 --- a/AI_METHOD_ROUTING.md +++ b/AI_METHOD_ROUTING.md @@ -147,7 +147,7 @@ rg "class .*Service|module.exports" app/service/qimenMethods - receive - app/service/main.js (18) - success, fail, normalizeDate, setInfo, getInfo, isPlainObject, normalizeWhereCondition, normalizeWhereParams, extractWhereValues, prepareWhereContext, findQimenAppWithContract, queryFromDatabase, checkAuth, checkApiPermission, extractApiPermissionState, collectApiPermissionValue, setUserContext, systemInit -- app/service/openapi.js (9) +- app/service/openapiEntry.js (9) - resolveOpenapiMethod, getOpenapiMethodService, resolveOpenapiContract, getInfo, dispatchOpenapiMethod, validateOpenapiParams, parseJsonBody, normalizeSignFields, processRequest - app/service/openapi/callback.js (4) - signCallback, sendWithRetry, sendByAppKey, logCallback diff --git a/app/controller/openapi.js b/app/controller/openapi.js index 8a9495d..24a959b 100644 --- a/app/controller/openapi.js +++ b/app/controller/openapi.js @@ -30,7 +30,7 @@ class OpenapiController extends Controller { const { ctx } = this; try { - const result = await ctx.service.openapi.processRequest(); + const result = await ctx.service.openapiEntry.processRequest(); if (result.ok) { ctx.status = Number(result.httpStatus) || 200; ctx.body = toSuccess(result.data, result.message || 'ok'); diff --git a/app/service/openapi.js b/app/service/openapiEntry.js similarity index 100% rename from app/service/openapi.js rename to app/service/openapiEntry.js diff --git a/app/service/systems/inbound_direct_log.js b/app/service/systems/inbound_direct_log.js index 77d6cc4..0b9e309 100644 --- a/app/service/systems/inbound_direct_log.js +++ b/app/service/systems/inbound_direct_log.js @@ -1156,9 +1156,12 @@ class InboundDirectLogService extends Service { ); } - // 回写收货单和收货明细的入库状态 + // 回写收货单和收货明细的入库状态(绑定模式:按 receiving_detail_id 优先扣减) await this._syncReceivingStorageStatus({ entryItemIds, transaction }); + // 非绑定模式剩余导入量:按 stock_id 匹配绑定收货单明细占用(绑定模式优先,剩余兜底) + await this._applyReceivingStockOccupancy({ entryOrder: eo, directLogs, transaction }); + // 更新入库单状态为 COMPLETED await eo.update({ order_status: 'COMPLETED', @@ -1682,6 +1685,71 @@ class InboundDirectLogService extends Service { ); } } + + /** + * 非绑定模式收货单占用:导入记录未携带 receiving_detail_id 时, + * 将剩余导入量按 stock_id(=收货明细 system_product_id)匹配到绑定收货单明细上, + * 依次占用剩余可入数量(actual_quantity - stored_quantity), + * 回写 stored_quantity/storage_status 并汇总收货单主表 storage_status。 + * 携带 receiving_detail_id 的导入记录已由 _syncReceivingStorageStatus 优先处理, + * 本方法只处理未携带 receiving_detail_id 的剩余部分(必须先调用前者)。 + * + * @param {Object} params 参数 + * @param {Object} params.entryOrder 入库单实例 + * @param {Array} params.directLogs 入库单下所有未删除的导入/PDA 入库记录 + * @param {Object} params.transaction Sequelize 事务对象 + * @return {Promise} + */ + async _applyReceivingStockOccupancy({ entryOrder, directLogs, transaction }) { + const { ctx, app } = this; + + // 1. 入库单未绑定收货单 → 跳过 + const bindings = await ctx.model.EntryOrderReceiving.findAll({ + where: { entry_order_id: entryOrder.id, deleted_at: null }, + attributes: [ 'receiving_id' ], + raw: true, + transaction, + }); + const receivingIds = [ ...new Set((bindings || []).map(b => Number(b.receiving_id)).filter(Boolean)) ]; + if (receivingIds.length === 0) return; + + // 2. 汇总未携带 receiving_detail_id 的导入量(绑定模式已优先处理,这里只取剩余部分) + const stockInboundQtyMap = new Map(); + for (const log of directLogs) { + if (log.receiving_detail_id) continue; + const stockId = Number(log.stock_id); + const qty = Number(log.quantity) || 0; + if (!stockId || qty <= 0) continue; + stockInboundQtyMap.set( + stockId, + { inboundQty: (stockInboundQtyMap.get(stockId)?.inboundQty || 0) + qty } + ); + } + if (stockInboundQtyMap.size === 0) return; + + // 3. 查询绑定收货单明细(事务内读取,stored_quantity 已包含绑定模式扣减结果) + const boundDetails = await ctx.model.ReceivingDetail.findAll({ + where: { + receiving_id: { [app.Sequelize.Op.in]: receivingIds }, + system_product_id: { [app.Sequelize.Op.ne]: null }, + }, + attributes: [ 'id', 'receiving_id', 'system_product_id', 'actual_quantity', 'stored_quantity' ], + raw: true, + transaction, + }); + if (!boundDetails || boundDetails.length === 0) return; + + // 4. 复用 storage.entry 的收货单占用逻辑(与 quickInbound 一致,事务内行锁防并发超分) + await ctx.service.systems.storage.entry._applyReceivingInboundUsage({ + receivingDetailRows: boundDetails, + stockInboundQtyMap, + transaction, + }); + + ctx.logger.info( + `[InboundDirectLog] 非绑定模式导入已按 stock_id 占用收货单明细,entryOrderId=${entryOrder.id}` + ); + } } module.exports = InboundDirectLogService; diff --git a/app/service/systems/storage/entry.js b/app/service/systems/storage/entry.js index 956e6ad..644a9d3 100644 --- a/app/service/systems/storage/entry.js +++ b/app/service/systems/storage/entry.js @@ -576,7 +576,9 @@ class EntryService extends Service { * @param {number} data.id 入库单主键 ID(必填) * @param {string} [mode='warehouse'] 查询维度:'warehouse' 按仓库校验权限(默认),'store' 按货主校验权限 * @return {Promise} code/data/message 结构 - * - data.entryOrder: { id, entry_order_code, order_status, store_remask } + * - data.entryOrder: { id, entry_order_code, order_status, total_order_lines, store_remask } + * - data.total_quantity: 应入库总数量(明细 planned 之和) + * - data.received_quantity: 已入库总数量(明细 completed 之和) * - data.items: [{ id, stock_id, planned, completed, stock, bindings }] * - data.receivingBindings: [{ id, receiving_id, receiving }] */ @@ -827,8 +829,11 @@ class EntryService extends Service { id: entryOrder.id, entry_order_code: entryOrder.entry_order_code, order_status: entryOrder.order_status, + total_order_lines: entryOrder.total_order_lines, store_remask: (storeWarehouse && storeWarehouse.store_remask) || '', }, + total_quantity: items.reduce((sum, item) => sum + (Number(item.planned) || 0), 0), + received_quantity: items.reduce((sum, item) => sum + (Number(item.completed) || 0), 0), items, receivingBindings, }, diff --git a/methods/changes/openapi.json b/methods/changes/openapi.json index f7485fb..02014e4 100644 --- a/methods/changes/openapi.json +++ b/methods/changes/openapi.json @@ -1,4 +1,16 @@ [ + { + "version": "2026.08.27-openapi-service-conflict-fix", + "date": "2026-08-27", + "summary": "修复 Egg 服务命名冲突:app/service/openapi.js 重命名为 openapiEntry.js,恢复 ctx.service.openapi.callback 可用", + "methods": [], + "notes": [ + "根因:app/service/openapi.js 文件与 app/service/openapi/ 目录同名,Egg ContextLoader 把目录里的 callback/callbackRegistry 挂成 OpenapiService 类的静态属性,实例上不可见,导致运行时 ctx.service.openapi.callback 为 undefined", + "修复:openapi.js 重命名为 openapiEntry.js(服务路径 ctx.service.openapiEntry),app/controller/openapi.js 的 processRequest 引用同步更新,AI_METHOD_ROUTING.md 同步", + "影响:storage.entry.quickInbound 对 API 来源直入单回传 entry_order_direct_inbound_completed 事件不再报 Cannot read properties of undefined (reading 'sendWithRetry'),库存两段式(先回传后写库)可正常执行" + ], + "type": "fixed" + }, { "version": "2026.06.04-openapi-04", "date": "2026-06-04", diff --git a/methods/changes/system.json b/methods/changes/system.json index 92698d6..2d28289 100644 --- a/methods/changes/system.json +++ b/methods/changes/system.json @@ -1,4 +1,39 @@ [ + { + "version": "2026.08.27-storage-entry-get-total-order-lines", + "date": "2026-08-27", + "type": "enhanced", + "summary": "storage.entry.get 响应新增 total_order_lines(entryOrder 内)、total_quantity(应入库总数量)、received_quantity(已入库总数量)字段", + "methods": [ + "storage.entry.get", + "storage.entry.getStore" + ], + "notes": [ + "app/service/systems/storage/entry.js get:返回 data.entryOrder 新增 total_order_lines(直接取 wms_entry_order.total_order_lines)", + "data 顶层新增 total_quantity = 明细 planned 之和(应入库总数量)、received_quantity = 明细 completed 之和(已入库总数量)", + "getStore 共用同一底层实现,自动获得这些字段", + "同步更新 get 方法 JSDoc 注释" + ], + "module": "storage.entry" + }, + { + "version": "2026.08.27-directInbound-receiving-stock-occupancy", + "date": "2026-08-27", + "type": "enhanced", + "summary": "storage.entry.commit 导入/PDA 入库支持绑定收货单占用:带 receiving_detail_id 的导入记录优先按明细扣减,未带 receiving_detail_id 的剩余导入量按 stock_id 匹配收货单明细占用并回写收货单入库状态", + "methods": [ + "storage.entry.commit", + "storage.directInbound.commit" + ], + "notes": [ + "app/service/systems/inbound_direct_log.js commit onSuccess 事务内新增 _applyReceivingStockOccupancy:绑定收货单(wms_entry_order_receiving)存在时,汇总未携带 receiving_detail_id 的导入记录数量,按 stock_id(=收货明细 system_product_id)匹配占用", + "优先级:携带 receiving_detail_id 的记录先由 _syncReceivingStorageStatus 按明细扣减,随后剩余量复用 storage.entry._applyReceivingInboundUsage 按 stock_id 依次占用收货明细剩余可入数量(actual_quantity - stored_quantity),事务内行锁防并发超分", + "占用后回写收货明细 stored_quantity/storage_status(all/partial),并汇总更新收货单主表 storage_status", + "未绑定收货单或无剩余导入量时跳过,不影响原有非绑定模式入库行为", + "storage.entry._applyReceivingInboundUsage 日志前缀由 [storage.entry.quickInbound] 改为 [storage.entry](方法现被 quickInbound 与 commit 两个流程复用)" + ], + "module": "storage.entry" + }, { "version": "2026.08.26-wave-batchList-store-id-filter", "date": "2026-08-26",