From 75a248fdf6d6048c189607298f290a508d98817b Mon Sep 17 00:00:00 2001 From: liufumingren Date: Thu, 13 Aug 2026 21:43:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=B6=E8=B4=A7=E5=8D=95=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E6=96=B0=E5=A2=9E=20stock=5Fquery=5Fwhere=20=E5=95=86?= =?UTF-8?q?=E5=93=81=E7=BB=B4=E5=BA=A6=E8=BF=87=E6=BB=A4=EF=BC=88=E6=9D=A1?= =?UTF-8?q?=E7=A0=81/=E7=BC=96=E7=A0=81/ID=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../systemMethods/storage/receiving.js | 11 +- app/service/systems/storage/receiving.js | 119 +++++++++++++++--- 2 files changed, 110 insertions(+), 20 deletions(-) diff --git a/app/service/systemMethods/storage/receiving.js b/app/service/systemMethods/storage/receiving.js index db61b3f..22771d0 100644 --- a/app/service/systemMethods/storage/receiving.js +++ b/app/service/systemMethods/storage/receiving.js @@ -26,11 +26,12 @@ class configService extends MainService { * @param {Object} [data.where] 筛选条件 * @param {boolean} [data.filterDepleted=false] 是否过滤掉无可再用量的收货单(所有明细已被其他入库单 committed 绑定占满) * @param {number} [data.entry_order_id] 可选,按入库单商品交集过滤:只返回"至少有一条明细的 system_product_id 在入库单明细中"的收货单 + * @param {string} [data.stock_query_where] 可选,按商品条码/商品编码/商品ID搜索,返回明细中含匹配商品的收货单 * @return {Promise} 统一成功包装响应 */ async getReceivingList(data) { const { service } = this; - const { pagination, sort, filterDepleted = false, entry_order_id: entryOrderId } = data; + const { pagination, sort, filterDepleted = false, entry_order_id: entryOrderId, stock_query_where: stockQueryWhere } = data; const { where } = this.prepareWhereContext(data, { specialFieldMap: { quickWhere: { @@ -38,7 +39,7 @@ class configService extends MainService { }, }, }); - const result = await service.systems.storage.receiving.getReceivingList(where, pagination, sort, { filterDepleted, entry_order_id: entryOrderId }); + const result = await service.systems.storage.receiving.getReceivingList(where, pagination, sort, { filterDepleted, entry_order_id: entryOrderId, stock_query_where: stockQueryWhere }); return this.success('ok', result); } @@ -49,11 +50,12 @@ class configService extends MainService { * @param {Object} [data.pagination] 分页参数 * @param {Object} [data.where] 筛选条件 * @param {Object} [data.sort] 排序参数 + * @param {string|string[]} [data.stock_query_where] 可选,按商品条码/商品编码/商品ID搜索,返回明细中含匹配商品的收货单 * @return {Promise} 统一成功包装响应 */ async getStoreReceivingList(data) { const { ctx, app } = this; - const { pagination, sort } = data; + const { pagination, sort, stock_query_where: stockQueryWhere } = data; // 从平台激活上下文获取 store_id(使用原生查询避免 AccountPlatformActive 无 id 列兼容问题) const platformRows = await app.model.query( @@ -85,7 +87,8 @@ class configService extends MainService { where, storeId, pagination, - sort + sort, + { stock_query_where: stockQueryWhere } ); return this.success('ok', result); diff --git a/app/service/systems/storage/receiving.js b/app/service/systems/storage/receiving.js index 127315b..b49b3f2 100644 --- a/app/service/systems/storage/receiving.js +++ b/app/service/systems/storage/receiving.js @@ -47,6 +47,7 @@ class ReceivingService extends Service { * @param {Object} [options] 额外选项 * @param {boolean} [options.filterDepleted=false] 是否过滤掉无可再用量的收货单 * @param {number} [options.entry_order_id] 可选,按入库单商品交集过滤:只返回"至少有一条明细的 system_product_id 在入库单明细中"的收货单 + * @param {string|string[]} [options.stock_query_where] 可选,按商品维度过滤:搜索条码/商品编码/商品ID,返回明细中含匹配商品的收货单(支持字符串或字符串数组,多个关键字取并集) * @return {Promise} 列表、分页与汇总信息 */ async getReceivingList(where, pageParams = {}, sortParams = {}, options = {}) { @@ -70,7 +71,7 @@ class ReceivingService extends Service { sortField = 'id', sortOrder = 'DESC', } = sortParams; - const { filterDepleted = false, entry_order_id: entryOrderId } = options; + const { filterDepleted = false, entry_order_id: entryOrderId, stock_query_where: stockQueryWhere } = options; const queryOptions = { where: { @@ -301,18 +302,23 @@ class ReceivingService extends Service { } } - // 合并 filterDepleted 和 entry_order_id 的过滤结果 + // 可选过滤:按 stock_query_where(商品条码/编码/ID)过滤,支持字符串或字符串数组 + let stockQueryFilteredIds = null; + const stockQueryKeywords = Array.isArray(stockQueryWhere) + ? stockQueryWhere.map(v => String(v).trim()).filter(Boolean) + : (typeof stockQueryWhere === 'string' && stockQueryWhere.trim() !== '' ? [ stockQueryWhere.trim() ] : []); + if (stockQueryKeywords.length > 0) { + stockQueryFilteredIds = await this._resolveStockQueryReceivingIds(stockQueryKeywords); + } + + // 合并 filterDepleted / entry_order_id / stock_query_where 的过滤结果(多组启用时取交集) let combinedFilteredIds = null; - if (filteredReceivingIds !== null || intersectionFilteredIds !== null) { - if (filteredReceivingIds !== null && intersectionFilteredIds !== null) { - // 两个过滤都启用 → 取交集 - const intersectionSet = new Set(intersectionFilteredIds); - combinedFilteredIds = filteredReceivingIds.filter(id => intersectionSet.has(id)); - } else if (filteredReceivingIds !== null) { - combinedFilteredIds = filteredReceivingIds; - } else { - combinedFilteredIds = intersectionFilteredIds; - } + const idFilterSets = [ filteredReceivingIds, intersectionFilteredIds, stockQueryFilteredIds ] + .filter(arr => arr !== null) + .map(arr => new Set(arr)); + if (idFilterSets.length > 0) { + const [ first, ...rest ] = idFilterSets; + combinedFilteredIds = [ ...first ].filter(id => rest.every(set => set.has(id))); } // 如果启用了过滤,用过滤后的 ID 做最终查询 @@ -404,7 +410,7 @@ class ReceivingService extends Service { conditions: normalizedWhere, sortField: actualSortField, sortOrder: actualSortOrder, - hasBarcodeSearch: false, + hasBarcodeSearch: stockQueryKeywords.length > 0, }, }, message: '查询成功', @@ -434,6 +440,70 @@ class ReceivingService extends Service { }; } } + + /** + * 按商品关键字解析收货单 ID 列表。 + * 搜索范围: + * 1) wms_stock_warehouse.item_code(商品编码)模糊匹配 + * 2) wms_stock_warehouse.id 精确匹配(关键字为纯数字时) + * 3) wms_stock_barcodes.barcode(条码)模糊匹配 + * 再通过 wms_receiving_detail.system_product_id 关联回收货单。 + * @param {string|string[]} keywords 搜索关键字(字符串或字符串数组,多个关键字取并集) + * @return {Promise} 收货单 ID 数组;关键字为空返回 null + */ + async _resolveStockQueryReceivingIds(keywords) { + const { ctx, app } = this; + const { Sequelize } = app; + const { Op } = Sequelize; + + const keywordList = (Array.isArray(keywords) ? keywords : [ keywords ]) + .map(v => String(v).trim()) + .filter(Boolean); + if (keywordList.length === 0) return null; + + const stockIds = new Set(); + + // 1. 商品编码 / 商品ID 匹配(wms_stock_warehouse) + const stockWhere = { + [Op.or]: keywordList.map(k => ({ item_code: { [Op.like]: `%${k}%` } })), + }; + keywordList.forEach(k => { + if (/^\d+$/.test(k)) { + stockWhere[Op.or].push({ id: Number(k) }); + } + }); + const stockRows = await ctx.model.StockWarehouse.findAll({ + attributes: [ 'id' ], + where: stockWhere, + raw: true, + }); + stockRows.forEach(row => stockIds.add(Number(row.id))); + + // 2. 条码匹配(wms_stock_barcodes → stock_id) + const barcodeRows = await ctx.model.StockBarcode.findAll({ + attributes: [ 'stock_id' ], + where: { + [Op.or]: keywordList.map(k => ({ barcode: { [Op.like]: `%${k}%` } })), + }, + raw: true, + }); + barcodeRows.forEach(row => stockIds.add(Number(row.stock_id))); + + if (stockIds.size === 0) return []; + + // 3. 通过明细表关联回收货单 + const detailRows = await ctx.model.ReceivingDetail.findAll({ + attributes: [ 'receiving_id' ], + where: { + system_product_id: { [Op.in]: [ ...stockIds ] }, + }, + group: [ 'receiving_id' ], + raw: true, + }); + + return [ ...new Set(detailRows.map(row => row.receiving_id)) ]; + } + /** * 货主侧收货单列表查询。 * 以 store_id 为维度查询收货单,支持分页、多条件筛选与排序。 @@ -445,14 +515,17 @@ class ReceivingService extends Service { * @param {Object} [sortParams] 排序参数 * @param {string} [sortParams.sortField='created_at'] 排序字段 * @param {string} [sortParams.sortOrder='DESC'] 排序方向 + * @param {Object} [options] 额外选项 + * @param {string|string[]} [options.stock_query_where] 可选,按商品维度过滤:搜索条码/商品编码/商品ID,返回明细中含匹配商品的收货单(支持字符串或字符串数组,多个关键字取并集) * @return {Promise} 列表、分页与汇总信息 */ - async getStoreReceivingList(where, storeId, pageParams = {}, sortParams = {}) { + async getStoreReceivingList(where, storeId, pageParams = {}, sortParams = {}, options = {}) { const { ctx, app } = this; const { Sequelize } = app; const { Op } = Sequelize; const { page = 1, pageSize = 20 } = pageParams; const { sortField = 'created_at', sortOrder = 'DESC' } = sortParams; + const { stock_query_where: stockQueryWhere } = options; try { const queryOptions = { @@ -522,13 +595,27 @@ class ReceivingService extends Service { const offset = (page - 1) * pageSize; + // 可选过滤:按 stock_query_where(商品条码/编码/ID)过滤,支持字符串或字符串数组 + let stockQueryFilteredIds = null; + const stockQueryKeywords = Array.isArray(stockQueryWhere) + ? stockQueryWhere.map(v => String(v).trim()).filter(Boolean) + : (typeof stockQueryWhere === 'string' && stockQueryWhere.trim() !== '' ? [ stockQueryWhere.trim() ] : []); + if (stockQueryKeywords.length > 0) { + stockQueryFilteredIds = await this._resolveStockQueryReceivingIds(stockQueryKeywords); + } + + const finalWhere = stockQueryFilteredIds !== null + ? { id: { [Op.in]: stockQueryFilteredIds } } + : queryOptions.where; + const [ total, data ] = await Promise.all([ ctx.model.Receiving.count({ - where: queryOptions.where, + where: finalWhere, distinct: true, }), ctx.model.Receiving.findAll({ ...queryOptions, + where: finalWhere, limit: Number(pageSize), offset: Number(offset), distinct: true, @@ -611,7 +698,7 @@ class ReceivingService extends Service { conditions: normalizedWhere, sortField: actualSortField, sortOrder: actualSortOrder, - hasBarcodeSearch: false, + hasBarcodeSearch: stockQueryKeywords.length > 0, }, }, message: '查询成功',