diff --git a/app/service/systems/storage/receiving.js b/app/service/systems/storage/receiving.js index dae9b18..5082c4a 100644 --- a/app/service/systems/storage/receiving.js +++ b/app/service/systems/storage/receiving.js @@ -368,6 +368,12 @@ class ReceivingService extends Service { }); } + // 补充货主名称:合约表 store_remask 优先、公司全称兜底(快照 store_name 可能缺失或过期) + const storeNameMap = await this._resolveReceivingStoreNameMap( + data.map(item => item.store_id), + ctx.user.store_id + ); + const processedData = data.map(item => { // 注意:wms_receiving 表中没有 locked_quantity 字段,total_quantity 为商品总数量 const availableQuantity = Math.max(0, item.total_quantity || 0); @@ -377,6 +383,8 @@ class ReceivingService extends Service { .map(row => row.file.file_url); return { ...item, + // 货主名称:实时解析优先(货主备注 → 公司全称),缺失时回退主表快照 + store_name: storeNameMap.get(Number(item.store_id)) || item.store_name || '', images: itemImages, available_quantity: availableQuantity, lock_rate: parseFloat(lockRate), @@ -504,6 +512,49 @@ class ReceivingService extends Service { return [ ...new Set(detailRows.map(row => row.receiving_id)) ]; } + /** + * 批量解析收货单货主名称映射。 + * 货主名称优先取合约表 wms_store_warehouse.store_remask(货主备注名称,按 store_id + warehouse_id 维度), + * 无合约或备注为空时回退 wms_store.company_name(公司全称)。 + * @param {Array} storeIds 货主ID列表 + * @param {number} warehouseId 仓库ID + * @return {Promise>} store_id → 货主名称 + */ + async _resolveReceivingStoreNameMap(storeIds, warehouseId) { + const { ctx } = this; + const { Op } = ctx.app.Sequelize; + const normalizedIds = [ ...new Set((storeIds || []).map(v => Number(v)).filter(v => v > 0)) ]; + const resultMap = new Map(); + if (normalizedIds.length === 0) return resultMap; + + // 1. 合约表货主备注名称 + const contractRows = await ctx.model.StoreWarehouse.findAll({ + where: { + store_id: { [Op.in]: normalizedIds }, + warehouse_id: warehouseId, + }, + attributes: [ 'store_id', 'store_remask' ], + raw: true, + }); + for (const row of contractRows) { + resultMap.set(Number(row.store_id), row.store_remask || ''); + } + + // 2. 货主公司全称兜底 + const storeRows = await ctx.model.Store.findAll({ + where: { id: { [Op.in]: normalizedIds } }, + attributes: [ 'id', 'company_name' ], + raw: true, + }); + for (const row of storeRows) { + if (!resultMap.get(Number(row.id))) { + resultMap.set(Number(row.id), row.company_name || ''); + } + } + + return resultMap; + } + /** * 货主侧收货单列表查询。 * 以 store_id 为维度查询收货单,支持分页、多条件筛选与排序。 @@ -721,7 +772,8 @@ class ReceivingService extends Service { * @param {Object} data 请求体 * @param {number} data.id 收货单ID * @param {Object} data.pagination 分页参数 - * @return {Promise} 详情数据,明细中的 space_id 会优先展示自定义库位名称,其次展示库位编码,并补充 space_id_raw 兼容原始ID + * @return {Promise} 详情数据,明细中的 space_id 会优先展示自定义库位名称,其次展示库位编码, + * 并补充 space_id_raw 兼容原始ID;每个明细项同时返回 stock_id(商品/库存ID,语义与 system_product_id 一致,缺失时为 null) */ async getReceivingDetail(data) { const { id, pagination } = data; @@ -739,6 +791,17 @@ class ReceivingService extends Service { if (!receiving) { return { code: 0, message: '无记录或无权访问', data: {} }; } + // 补充货主名称:合约表 store_remask 优先、公司全称兜底(快照 store_name 可能缺失或过期) + if (Number(receiving.store_id || 0) > 0) { + const detailStoreNameMap = await this._resolveReceivingStoreNameMap( + [ receiving.store_id ], + Number(receiving.warehouse_id || warehouseId) + ); + const resolvedStoreName = detailStoreNameMap.get(Number(receiving.store_id)); + if (resolvedStoreName) { + receiving.store_name = resolvedStoreName; + } + } // 2. 查询收货单明细,按分页 const receivingItems = await app.model.ReceivingDetail.findAll({ where: { receiving_id: id }, @@ -798,6 +861,8 @@ class ReceivingService extends Service { const spaceName = receivingSpace?.custom_name || receivingSpace?.space_code || null; return { ...detailBase, + // 商品/库存ID,语义与 system_product_id 一致,缺失时兜底为 null + stock_id: detailBase.system_product_id ?? null, space_id_raw: detailBase.space_id ?? null, space_name: spaceName, custom_space_name: receivingSpace?.custom_name ?? null, @@ -3449,7 +3514,7 @@ class ReceivingService extends Service { * 删除收货单明细并回算统计。 * @param {Object} data 请求体 * @param {number} data.detail_id 明细ID - * @return {Promise} 删除结果 + * @return {Promise} 删除结果,data.details 为删除后剩余明细列表(字段与 getReceivingDetail 明细项保持一致,含 stock_id) */ async deleteDetail(data) { const ctx = this.ctx; @@ -3529,6 +3594,9 @@ class ReceivingService extends Service { } } + // 商品/库存ID,与 getReceivingDetail 明细项字段保持一致,缺失时兜底为 null + detailData.stock_id = detailData.system_product_id ?? null; + return detailData; }); diff --git a/test/app/service/systems/storage.receiving.getDetail.test.js b/test/app/service/systems/storage.receiving.getDetail.test.js index 290d0f4..2bb142e 100644 --- a/test/app/service/systems/storage.receiving.getDetail.test.js +++ b/test/app/service/systems/storage.receiving.getDetail.test.js @@ -140,4 +140,109 @@ describe('test/app/service/systems/storage.receiving.getDetail.test.js', () => { assert.equal(details[1].custom_space_name, null); assert.equal(Number(details[1].space_id_raw), 3002); }); + + it('should resolve store_name from contract store_remask with company_name fallback', async () => { + const receivingRow = { + id: 123, + warehouse_id: 100000, + store_id: 77, + store_name: '', + receiving_number: 'RCV202605060001', + dataValues: {}, + }; + + const detailRows = []; + const model = { + Receiving: { + async findOne() { + return receivingRow; + }, + }, + ReceivingDetail: { + async findAll() { + return detailRows; + }, + async count() { + return 0; + }, + }, + FileReference: { + async findAll() { + return []; + }, + }, + File: {}, + StoreWarehouse: { + async findAll() { + return []; + }, + }, + Store: { + async findAll() { + return [{ id: 77, company_name: '货主公司全称' }]; + }, + }, + }; + + const ctx = { + user: { + store_id: 100000, + }, + app: { + Sequelize: { + Op: { + in: 'in', + }, + }, + model, + }, + model, + }; + + const service = new ReceivingService(ctx); + const result = await service.getReceivingDetail({ + id: 123, + pagination: { + page: 1, + pageSize: 10, + }, + }); + + assert.equal(result.code, 200); + assert.equal(result.data.receiving.store_name, '货主公司全称'); + }); + + it('should resolve store_name remask first over company_name', async () => { + const model = { + StoreWarehouse: { + async findAll({ where }) { + if (where.store_id.in.includes(1)) { + return [{ store_id: 1, store_remask: '货主备注A' }]; + } + return []; + }, + }, + Store: { + async findAll({ where }) { + return where.id.in.map(id => ({ id, company_name: id === 1 ? '公司A' : '公司B' })); + }, + }, + }; + const ctx = { + app: { + Sequelize: { + Op: { + in: 'in', + }, + }, + }, + model, + }; + + const service = new ReceivingService(ctx); + const map = await service._resolveReceivingStoreNameMap([ 1, 2 ], 100000); + + assert.equal(map.get(1), '货主备注A'); + assert.equal(map.get(2), '公司B'); + }); });