及时库存查询逻辑修改;

This commit is contained in:
王奎兴
2026-02-03 14:02:42 +08:00
parent 54631a0101
commit 0e87b0d4b1
@@ -1,12 +1,17 @@
package com.mhd.wms.application.service.statementStatistics;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.mhd.common.core.utils.StringUtils;
import com.mhd.common.core.web.domain.AjaxResult;
import com.mhd.common.security.utils.SecurityUtils;
import com.mhd.system.api.SystemServiceFeign;
import com.mhd.system.api.domain.BatchDetailFeignPO;
import com.mhd.system.api.domain.cache.AssociationWarehouseCacheDO;
import com.mhd.system.api.domain.cache.SystemServiceCacheUtil;
import com.mhd.system.api.model.LoginUser;
import com.mhd.wms.domain.inventoryStandingDetail.service.InventoryStandingDetailDomainService;
import com.mhd.wms.domain.investigetionManageDetail.repository.po.InvestigetionManageDetailPO;
import com.mhd.wms.domain.materialInventory.repository.mapper.MaterialInventoryMapper;
import com.mhd.wms.domain.materialInventory.repository.po.MaterialInventoryPO;
import com.mhd.wms.domain.materialInventory.service.MaterialInventoryDomainService;
@@ -23,6 +28,7 @@ import org.springframework.util.CollectionUtils;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
/**
* 报表管理ApplicationService
@@ -39,6 +45,8 @@ public class StatementStatisticsService {
private InventoryStandingDetailDomainService inventoryStandingDetailDomainService;
@Autowired
private MaterialInventoryMapper materialInventoryMapper;
@Autowired
private SystemServiceFeign systemServiceFeign;
/**
* 分页查询即时库存列表
@@ -78,7 +86,36 @@ public class StatementStatisticsService {
}
}
List<ImmediateInventoryPO> immediateInventoryPOS = materialInventoryDomainService.queryImmediateInventoryList(immediateInventoryDO);
// 性能优化:批量获取批次属性,避免N+1查询问题
// 1. 收集所有唯一的materialBaseInfoId
Set<Long> materialBaseInfoIdSet = immediateInventoryPOS.stream()
.map(ImmediateInventoryPO::getMaterialBaseInfoId)
.filter(Objects::nonNull)
.collect(Collectors.toSet());
// 2. 批量查询批次属性(使用Map缓存,避免重复查询相同的materialBaseInfoId
Map<Long, List<BatchDetailFeignPO>> batchDetailMap = new HashMap<>();
for (Long materialBaseInfoId : materialBaseInfoIdSet) {
try {
AjaxResult ajaxResult = systemServiceFeign.getBatchDetailListByMaterialBaseInfoId(materialBaseInfoId);
if (ajaxResult != null && "200".equals(String.valueOf(ajaxResult.get("code")))) {
List<BatchDetailFeignPO> batchDetailFeignPOList = JSON.parseArray(
JSONObject.toJSONString(ajaxResult.get("data")), BatchDetailFeignPO.class);
if (!CollectionUtils.isEmpty(batchDetailFeignPOList)) {
// 过滤isDisplay=1的属性
List<BatchDetailFeignPO> filteredList = batchDetailFeignPOList.stream()
.filter(batchDetail -> batchDetail.getIsDisplay() != null && batchDetail.getIsDisplay() == 1)
.collect(Collectors.toList());
batchDetailMap.put(materialBaseInfoId, filteredList);
}
}
} catch (Exception e) {
log.warn("获取物料批次属性失败,物料基础信息ID:{},错误:{}", materialBaseInfoId, e.getMessage());
}
}
// 3. 为每个物料明细设置更多属性(从库存表中获取值)
setMaterialMoreDetailListFromInventory(immediateInventoryPOS, batchDetailMap);
return immediateInventoryPOS;
}
@@ -88,7 +125,7 @@ public class StatementStatisticsService {
* @param materialInventoryPOList 物料库存列表
* @param batchDetailMap 批次属性Map
*/
private void setMaterialMoreDetailListFromInventory(List<MaterialInventoryPO> materialInventoryPOList,
private void setMaterialMoreDetailListFromInventory(List<ImmediateInventoryPO> materialInventoryPOList,
Map<Long, List<BatchDetailFeignPO>> batchDetailMap) {
if (CollectionUtils.isEmpty(materialInventoryPOList)) {
return;