From a3ec20098f7fbc2ef0d5f763fefc1c24865d4899 Mon Sep 17 00:00:00 2001 From: liufumingren Date: Sun, 20 Sep 2026 16:01:01 +0800 Subject: [PATCH] =?UTF-8?q?fix(stock):=20getStoreStocks=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=20store=5Fid=20=E8=BF=87=E6=BB=A4=E5=A4=B1=E6=95=88?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E8=B7=A8=E8=B4=A7=E4=B8=BB/=E8=B7=A8?= =?UTF-8?q?=E4=BB=93=E5=BA=93=E6=95=B0=E6=8D=AE=E6=B3=84=E6=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - store_id/warehouse_id/软删条件原先放在 LEFT JOIN(required:false) 的 include.where 上,只进 ON 子句无法过滤主表 wms_stock_store,传 store_id 仍返回跨货主商品 - 过滤条件改为直接落在主表 where,count/findAll 同条件,total 恢复准确 - 返回的 store_id 字段补取 warehouse_stock.store_id,并加空列表访问保护 - 订单创建报错"库存记录不存在"补上商品编码 item_code,并区分场景改为"仓库已解绑,请联系仓库关联绑定" --- app/service/systems/delivery_order.js | 3 ++- app/service/systems/stock_store.js | 22 +++++++++++----------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/app/service/systems/delivery_order.js b/app/service/systems/delivery_order.js index 6f4d64d..066a215 100644 --- a/app/service/systems/delivery_order.js +++ b/app/service/systems/delivery_order.js @@ -1647,7 +1647,8 @@ class DeliveryOrderService extends Service { const stockId = Number(wms_stock.stock_id) || 0; if (!stockId) { - errors.push(`第${i + 1}行: 商品《${wms_stock.item_name}》库存记录不存在`); + // 此时 stock_store 存在但未绑定库存记录(stock_id 为空),即仓库侧已解绑 + errors.push(`第${i + 1}行: 商品《${wms_stock.item_name}》编码《${wms_stock.item_code}》仓库已解绑,请联系仓库关联绑定`); continue; } diff --git a/app/service/systems/stock_store.js b/app/service/systems/stock_store.js index 684440e..1d7f2b6 100644 --- a/app/service/systems/stock_store.js +++ b/app/service/systems/stock_store.js @@ -2832,9 +2832,15 @@ class stockStoreService extends Service { deleted_at: null, is_valid: 'Y', }; - const StockStoreWhere = {}; + // 过滤条件必须落在主表(wms_stock_store),include 为 LEFT JOIN 时 + // 条件只会进 ON 子句,无法过滤主表,会导致跨货主/跨仓库数据泄漏。 + const StockStoreWhere = { + warehouse_id: warehouseId, + delete_at: null, + is_valid: 'Y', + }; if (store_id) { - StockWarehouseWhere.store_id = store_id; + StockStoreWhere.store_id = store_id; } if (item_code) { const processedValue = item_code.split(','); @@ -2858,19 +2864,13 @@ class stockStoreService extends Service { [ total, barcodes ] = await Promise.all([ ctx.model.StockStore.count({ where: StockStoreWhere, - attributes: [ - 'item_code', 'item_name', 'app_key', 'create_from', 'id', 'stock_id', - ], include: [{ model: ctx.model.StockWarehouse, as: 'warehouse_stock', - attributes: [ 'id' ], + attributes: [ 'id', 'store_id' ], where: StockWarehouseWhere, required: false, }], - limit: parseInt(pageSize), - offset: parseInt(offset), - raw: true, }), ctx.model.StockStore.findAll({ where: StockStoreWhere, @@ -2880,7 +2880,7 @@ class stockStoreService extends Service { include: [{ model: ctx.model.StockWarehouse, as: 'warehouse_stock', - attributes: [ 'id' ], + attributes: [ 'id', 'store_id' ], where: StockWarehouseWhere, required: false, }], @@ -2916,7 +2916,7 @@ class stockStoreService extends Service { item_name: item.item_name, }; }); - const get_store_id = warehouseStock ? warehouseStock.store_id : barcodes[0]['warehouse_stock.store_id']; + const get_store_id = warehouseStock ? warehouseStock.store_id : (barcodes[0] ? barcodes[0]['warehouse_stock.store_id'] : null); return { rows: results, store_id: get_store_id,