public List<Picking> getBeginInfo(String beginNo) { List<Picking> list = new ArrayList<Picking>(); String[] arr = pickingMapper.selectMachineNoList(beginNo); partList = new ArrayList<Long>(); for (String item : arr) { List<Long> aa = pickingMapper.selectPartNo(item); for (Long index : aa) { partList.add(index); getPartList(index); List<Long> myList = partList.stream().distinct().collect(Collectors.toList()); myList.remove(index); for (Long ii : myList) { String partNo = pickingMapper.selectPartNoS(ii); String partDesc = pickingMapper.selectPartDesc(ii); Long sum = pickingMapper.selectSumNumber(ii, SecurityUtils.getLocalDeptId()); Long use = 0L; if (pickingMapper.selectUsedNumber(beginNo, ii) != null) { use = pickingMapper.selectUsedNumber(beginNo, ii); } Picking pp = new Picking(); pp.setBeginNo(beginNo); pp.setCustodyNo(item); pp.setPartNo(partNo); pp.setPartDesc(partDesc); pp.setInventoryNumber(sum); pp.setClaimed(use); pp.setRequirementsNumber(pickingMapper.selectRequireNumber(index, ii)); list.add(pp); } partList.clear(); myList.clear(); } } return list; } 如何优化可以让这个方法简洁
时间: 2024-03-08 10:46:57 浏览: 132
SAP Picking List-ZCO27
这段代码看起来比较冗长,可以考虑简化和优化:
1. 可以将 `List<Long> myList` 和 `List<Long> partList` 声明为局部变量,避免在方法中多处使用,并且应该在使用后及时清空。
2. 可以使用 Java 8 的 Stream API 简化代码,如下所示:
```
public List<Picking> getBeginInfo(String beginNo) {
List<Picking> list = new ArrayList<>();
String[] machineNoList = pickingMapper.selectMachineNoList(beginNo);
for (String machineNo : machineNoList) {
List<Long> partNoList = pickingMapper.selectPartNo(machineNo);
for (Long partNo : partNoList) {
List<Long> relatedPartNoList = pickingMapper.selectRelatedPartNo(partNo);
relatedPartNoList.remove(partNo);
relatedPartNoList.stream().distinct().forEach(relatedPartNo -> {
String partNoS = pickingMapper.selectPartNoS(relatedPartNo);
String partDesc = pickingMapper.selectPartDesc(relatedPartNo);
Long inventoryNumber = pickingMapper.selectInventoryNumber(relatedPartNo, SecurityUtils.getLocalDeptId());
Long claimed = pickingMapper.selectClaimedNumber(beginNo, relatedPartNo);
Long requirementsNumber = pickingMapper.selectRequirementsNumber(partNo, relatedPartNo);
Picking picking = new Picking();
picking.setBeginNo(beginNo);
picking.setCustodyNo(machineNo);
picking.setPartNo(partNoS);
picking.setPartDesc(partDesc);
picking.setInventoryNumber(inventoryNumber);
picking.setClaimed(claimed);
picking.setRequirementsNumber(requirementsNumber);
list.add(picking);
});
}
}
return list;
}
```
这样可以避免使用多个循环和临时变量,让代码更加简洁和易读。同时,使用 Stream API 可以提高程序运行效率。
阅读全文