Commit 3330a43c57ac8cd1faf50b09d06b84db3245f901
1 parent
abbe2e07
feat:优化呆滞库存查询效率
Showing
2 changed files
with
49 additions
and
37 deletions
src/main/java/com/huaheng/pc/config/material/service/MaterialService.java
@@ -8,8 +8,10 @@ import com.baomidou.mybatisplus.extension.service.IService; | @@ -8,8 +8,10 @@ import com.baomidou.mybatisplus.extension.service.IService; | ||
8 | import com.huaheng.pc.config.material.domain.bo.MaterialComputeVariableBO; | 8 | import com.huaheng.pc.config.material.domain.bo.MaterialComputeVariableBO; |
9 | import com.huaheng.pc.inventory.inventoryTransaction.domain.InventoryTransaction; | 9 | import com.huaheng.pc.inventory.inventoryTransaction.domain.InventoryTransaction; |
10 | 10 | ||
11 | +import java.util.Collection; | ||
11 | import java.util.List; | 12 | import java.util.List; |
12 | import java.util.Map; | 13 | import java.util.Map; |
14 | +import java.util.Set; | ||
13 | 15 | ||
14 | public interface MaterialService extends IService<Material> { | 16 | public interface MaterialService extends IService<Material> { |
15 | 17 |
src/main/java/com/huaheng/pc/inventory/inventoryDetail/service/InventoryDetailServiceImpl.java
@@ -34,11 +34,14 @@ import org.springframework.stereotype.Service; | @@ -34,11 +34,14 @@ import org.springframework.stereotype.Service; | ||
34 | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | 34 | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
35 | import com.huaheng.pc.inventory.inventoryDetail.mapper.InventoryDetailMapper; | 35 | import com.huaheng.pc.inventory.inventoryDetail.mapper.InventoryDetailMapper; |
36 | import org.springframework.transaction.annotation.Transactional; | 36 | import org.springframework.transaction.annotation.Transactional; |
37 | +import org.springframework.util.CollectionUtils; | ||
37 | 38 | ||
38 | import javax.annotation.Resource; | 39 | import javax.annotation.Resource; |
39 | import java.lang.reflect.InvocationTargetException; | 40 | import java.lang.reflect.InvocationTargetException; |
40 | import java.math.BigDecimal; | 41 | import java.math.BigDecimal; |
41 | import java.util.*; | 42 | import java.util.*; |
43 | +import java.util.function.Function; | ||
44 | +import java.util.stream.Collectors; | ||
42 | 45 | ||
43 | @Service | 46 | @Service |
44 | public class InventoryDetailServiceImpl extends ServiceImpl<InventoryDetailMapper, InventoryDetail> implements InventoryDetailService { | 47 | public class InventoryDetailServiceImpl extends ServiceImpl<InventoryDetailMapper, InventoryDetail> implements InventoryDetailService { |
@@ -279,59 +282,66 @@ public class InventoryDetailServiceImpl extends ServiceImpl<InventoryDetailMappe | @@ -279,59 +282,66 @@ public class InventoryDetailServiceImpl extends ServiceImpl<InventoryDetailMappe | ||
279 | public List<InventoryDetail> expiringInventoryHandle(List<InventoryDetail> inventoryDetails, String expiring) { | 282 | public List<InventoryDetail> expiringInventoryHandle(List<InventoryDetail> inventoryDetails, String expiring) { |
280 | // 获取库存呆滞时间配置 | 283 | // 获取库存呆滞时间配置 |
281 | int days = Integer.parseInt(configService.getKey(QuantityConstant.DEAD_TIME)); | 284 | int days = Integer.parseInt(configService.getKey(QuantityConstant.DEAD_TIME)); |
282 | - int days_batch = Integer.parseInt(configService.getKey(QuantityConstant.DEAD_TIME_BATCH)); | ||
283 | - int days_flammable = Integer.parseInt(configService.getKey(QuantityConstant.DEAD_TIME_FLAMMABLE)); | 285 | + int daysBatch = Integer.parseInt(configService.getKey(QuantityConstant.DEAD_TIME_BATCH)); |
286 | + int daysFlammable = Integer.parseInt(configService.getKey(QuantityConstant.DEAD_TIME_FLAMMABLE)); | ||
287 | + | ||
284 | // 如果 inventoryDetails 为空,直接返回空列表 | 288 | // 如果 inventoryDetails 为空,直接返回空列表 |
285 | - if (inventoryDetails == null || inventoryDetails.isEmpty()) { | 289 | + if (CollectionUtils.isEmpty(inventoryDetails)) { |
286 | return Collections.emptyList(); | 290 | return Collections.emptyList(); |
287 | } | 291 | } |
288 | - // 初始化返回的库存列表 | ||
289 | - List<InventoryDetail> returnInventory = new ArrayList<>(); | ||
290 | - // 初始化呆滞库存和非呆滞库存列表 | ||
291 | - List<InventoryDetail> expiringInventory = new ArrayList<>(); | ||
292 | - List<InventoryDetail> noExpiringInventory = new ArrayList<>(); | ||
293 | - | ||
294 | - // 遍历库存明细 | ||
295 | - for (InventoryDetail inventoryDetail : inventoryDetails) { | 292 | + |
293 | + // 获取所有物料编码 | ||
294 | + Set<String> materialCodes = inventoryDetails.stream() | ||
295 | + .map(InventoryDetail::getMaterialCode) | ||
296 | + .collect(Collectors.toSet()); | ||
297 | + | ||
298 | + // 批量查询物料信息 | ||
299 | + List<Material> materials = getMaterialsByCodes(materialCodes); | ||
300 | + | ||
301 | + // 创建物料编码和物料对象的映射关系 | ||
302 | + Map<String, Material> materialMap = new HashMap<>(); | ||
303 | + for (Material material : materials) { | ||
304 | + materialMap.put(material.getCode(), material); | ||
305 | + } | ||
306 | + | ||
307 | + return inventoryDetails.stream().collect(Collectors.partitioningBy(inventoryDetail -> { | ||
296 | // 计算库存的更新时间和当前时间之间的差距 | 308 | // 计算库存的更新时间和当前时间之间的差距 |
297 | long difference = DateUtils.difference(DateUtils.getNowDate(), inventoryDetail.getLastUpdated()); | 309 | long difference = DateUtils.difference(DateUtils.getNowDate(), inventoryDetail.getLastUpdated()); |
298 | - Material material = materialService.getMaterialByCode(inventoryDetail.getMaterialCode()); | 310 | + |
311 | + // 从 Map 中获取物料信息 | ||
312 | + Material material = materialMap.get(inventoryDetail.getMaterialCode()); | ||
313 | + if (material == null) { | ||
314 | + return false; | ||
315 | + } | ||
299 | 316 | ||
300 | int materialDays = days; | 317 | int materialDays = days; |
301 | - if (StringUtils.isNotEmpty(material.getIsFlammable()) && material.getIsFlammable().equals("是")) { | ||
302 | - materialDays = Math.min(materialDays, days_flammable); | ||
303 | - } else if (StringUtils.isNotEmpty(material.getIsBatch()) && material.getIsBatch().equals("是")) { | ||
304 | - materialDays = Math.min(materialDays, days_batch); | 318 | + if (Objects.equals(material.getIsFlammable(), "是")) { |
319 | + materialDays = Math.min(materialDays, daysFlammable); | ||
320 | + } else if (Objects.equals(material.getIsBatch(), "是")) { | ||
321 | + materialDays = Math.min(materialDays, daysBatch); | ||
305 | } | 322 | } |
323 | + | ||
306 | // 计算呆滞时长 | 324 | // 计算呆滞时长 |
307 | int differenceDay = DateUtils.secondToDays(difference); | 325 | int differenceDay = DateUtils.secondToDays(difference); |
308 | int num = differenceDay - materialDays; | 326 | int num = differenceDay - materialDays; |
309 | - // 根据呆滞时长将库存明细添加到相应列表 | 327 | + |
328 | + // 判断是否为呆滞库存 | ||
310 | if (num > 0) { | 329 | if (num > 0) { |
311 | inventoryDetail.setDeadTime(num + "天"); | 330 | inventoryDetail.setDeadTime(num + "天"); |
312 | - expiringInventory.add(inventoryDetail); | ||
313 | - } else { | ||
314 | - noExpiringInventory.add(inventoryDetail); | 331 | + return true; |
315 | } | 332 | } |
316 | - } | ||
317 | - | 333 | + return false; |
334 | + })).getOrDefault(expiring.equals("1"), Collections.emptyList()); | ||
335 | + } | ||
318 | 336 | ||
319 | - switch (expiring) { | ||
320 | - //呆滞库存 | ||
321 | - case "1": | ||
322 | - returnInventory.addAll(expiringInventory); | ||
323 | - break; | ||
324 | - case "2": | ||
325 | - //未呆滞库存 | ||
326 | - returnInventory.addAll(noExpiringInventory); | ||
327 | - break; | ||
328 | - default: | ||
329 | - //全部库存 | ||
330 | - returnInventory.addAll(expiringInventory); | ||
331 | - returnInventory.addAll(noExpiringInventory); | ||
332 | - break; | 337 | + public List<Material> getMaterialsByCodes(Set<String> materialCodes) { |
338 | + if (CollectionUtils.isEmpty(materialCodes)) { | ||
339 | + return Collections.emptyList(); | ||
333 | } | 340 | } |
334 | - return returnInventory; | 341 | + //in 方法进行批量查询 |
342 | + LambdaQueryWrapper<Material> queryWrapper = new LambdaQueryWrapper<>(); | ||
343 | + queryWrapper.in(Material::getCode, materialCodes); | ||
344 | + return materialService.list(queryWrapper); | ||
335 | } | 345 | } |
336 | 346 | ||
337 | @Override | 347 | @Override |