storageQuantityMap, })); } /** * 将单条盘点明细格式化为聚合输出行ã€? * 商品行:聚合作业记录输出已盘数量/差异/库位列表等; * 库位行:按商品聚合作业记录输å‡?stocks 列表(含 wms_storage 账面与差异)ã€? * * @param {Object} params 格式化参数ã€? * @param {Object} params.detailRow 明细原始行ã€? * @param {Map>} params.recordsByDetail 按明细ID分组的作业记录ã€? * @param {Map} params.stockMap 商品快照映射ã€? * @param {Map} params.storageQuantityMap 库位商品账面数量映射(键 `${space_id}_${stock_id}`)ã€? * @return {Object} 格式化后的明细行ã€? */ _formatDetailAggregationRow(params = {}) { const { detailRow, recordsByDetail, stockMap, storageQuantityMap } = params; const detailRowId = Number(detailRow.id); const scopeType = (detailRow.scope_type || 'goods') === 'location' ? 'location' : 'goods'; const detailRecords = recordsByDetail.get(detailRowId) || []; const locationCodes = [ ...new Set(detailRecords .map(record => String(record.location_code || '').trim()) .filter(Boolean)) ]; const checkers = [ ...new Set(detailRecords .map(record => String(record.checker_name || '').trim()) .filter(Boolean)) ]; const lastCheckedAt = detailRecords.length > 0 ? detailRecords[detailRecords.length - 1].checked_at : null; // 按库位聚合作业记录(库位为空归入"无库ä½?分组,location_code ä¸?null),按首条记å½?id 升序 const locationGroups = new Map(); for (const record of detailRecords) { const recordLocationCode = record.location_code !== null && record.location_code !== undefined ? String(record.location_code).trim() : ''; if (!locationGroups.has(recordLocationCode)) { locationGroups.set(recordLocationCode, { location_id: recordLocationCode ? (Number(record.location_id) || null) : null, location_code: recordLocationCode || null, quantity: 0, first_record_id: Number(record.id) || 0, }); } const locationGroup = locationGroups.get(recordLocationCode); locationGroup.quantity += Number(record.quantity) || 0; } const locations = [ ...locationGroups.values() ] .sort((left, right) => left.first_record_id - right.first_record_id) .map(locationGroup => ({ location_id: locationGroup.location_id, location_code: locationGroup.location_code, quantity: locationGroup.quantity, })); const base = { ...detailRow, scope_type: scopeType, location_codes: locationCodes, checkers, last_checked_at: lastCheckedAt, records: detailRecords, locations, }; if (scopeType === 'goods') { const checkedQuantity = detailRecords.reduce((sum, record) => sum + (Number(record.quantity) || 0), 0); const diffQuantity = checkedQuantity - Number(detailRow.system_quantity || 0); return { ...base, checked_quantity: detailRecords.length > 0 ? checkedQuantity : null, diff_quantity: detailRecords.length > 0 ? diffQuantity : null, is_diff: detailRecords.length > 0 && diffQuantity !== 0 ? 1 : 0, status: detailRecords.length > 0 ? 'checked' : 'unchecked', }; } // 库位行:按商品聚合记录,账面ä»?wms_storage 取(location_id 为空时账面按 0ï¼? const quantityByStock = new Map(); for (const record of detailRecords) { const stockId = Number(record.stock_id); if (!Number.isInteger(stockId) || stockId <= 0) { continue; } const agg = quantityByStock.get(stockId) || { checked: 0 }; agg.checked += Number(record.quantity) || 0; quantityByStock.set(stockId, agg); } const locationId = detailRow.location_id !== null && detailRow.location_id !== undefined ? Number(detailRow.location_id) : 0; let checkedQuantity = 0; let diffQuantitySum = 0; let hasDiff = false; const stocks = [ ...quantityByStock.keys() ] .sort((left, right) => left - right) .map(stockId => { const agg = quantityByStock.get(stockId); const stock = stockMap.get(stockId) || {}; const systemQuantity = locationId > 0 ? (storageQuantityMap.get(`${locationId}_${stockId}`) || 0) : 0; const diffQuantity = agg.checked - systemQuantity; checkedQuantity += agg.checked; diffQuantitySum += diffQuantity; if (diffQuantity !== 0) { hasDiff = true; } return { stock_id: stockId, item_code: stock.item_code || null, item_name: stock.item_name || null, system_quantity: systemQuantity, checked_quantity: agg.checked, diff_quantity: diffQuantity, }; }); return { ...base, stocks, checked_quantity: detailRecords.length > 0 ? checkedQuantity : null, diff_quantity: detailRecords.length > 0 ? diffQuantitySum : null, is_diff: hasDiff ? 1 : 0, status: detailRecords.length > 0 ? 'checked' : 'unchecked', }; } /**