diff --git a/app/service/systems/storage/entry.js b/app/service/systems/storage/entry.js index 260a70a..26b619d 100644 --- a/app/service/systems/storage/entry.js +++ b/app/service/systems/storage/entry.js @@ -490,12 +490,37 @@ class EntryService extends Service { } } - // 为每行附加收货单绑定信息 + // 批量聚合当前页入库单的商品总数量(计划数)与已入库数量 + /** @type {Map} */ + const quantityMap = new Map(); + if (entryOrderIds.length > 0) { + const quantityRows = await ctx.model.EntryItems.findAll({ + attributes: [ + 'order_id', + [ Sequelize.fn('SUM', Sequelize.col('count')), 'total_quantity' ], + [ Sequelize.fn('SUM', Sequelize.col('received_quantity')), 'received_quantity' ], + ], + where: { order_id: { [Op.in]: entryOrderIds } }, + group: [ 'order_id' ], + raw: true, + }); + for (const item of quantityRows) { + quantityMap.set(Number(item.order_id), { + total_quantity: Number(item.total_quantity) || 0, + received_quantity: Number(item.received_quantity) || 0, + }); + } + } + + // 为每行附加收货单绑定信息与数量汇总 const enrichedRows = rows.map(row => { const rowJSON = row.toJSON ? row.toJSON() : row; const bindings = receivingBindingsMap.get(row.id) || []; + const quantity = quantityMap.get(row.id) || { total_quantity: 0, received_quantity: 0 }; return { ...rowJSON, + total_quantity: quantity.total_quantity, + received_quantity: quantity.received_quantity, receivingBindings: bindings, hasReceiving: bindings.length > 0, };