stream流

if (StringUtils.isNotEmpty(vo.getType())) {
switch (vo.getType()) {
case "1":
vo.setFinishId("N");
break;
case "2":
vo.setFinishId("Y");
break;
case "3":
vo.setFinishId("Y");
vo.setAccFini("0");
vo.setCheckId("N");
break;
case "4":
vo.setAccFini("1");
vo.setCheckId("Y");
break;
default:
break;
}
}
List<ContainerStatisticalItem> itemList = analysisContainerMapper.getContainerStatisticalList(vo);
if (!CollectionUtils.isEmpty(itemList)) {
Long[] ctVslVoyIds = itemList.stream().filter(e -> e.getCtVslvoyId() != null).map(ContainerStatisticalItem::getCtVslvoyId).toArray(Long[]::new);
if (ctVslVoyIds.length > 1000){
throw new RuntimeException("最多只能查询1000条船,请缩短时间查询范围!");
}
List<ContainerStatisticalItem> statisticalDataList = analysisContainerMapper.getContainerDataForId(ctVslVoyIds);
if (!CollectionUtils.isEmpty(statisticalDataList)) {
Map<Long, List<ContainerStatisticalItem>> statisticalDataMap = statisticalDataList.stream().collect(Collectors.groupingBy(ContainerStatisticalItem::getCtVslvoyId));
itemList.forEach(item -> {
if (statisticalDataMap.containsKey(item.getCtVslvoyId())){
ContainerStatisticalItem statisticalData = statisticalDataMap.get(item.getCtVslvoyId()).get(0);
item.setCtncount(statisticalData.getCtncount());
item.setTeuCount(statisticalData.getTeuCount());
item.setCount20E(statisticalData.getCount20E());
item.setCount40E(statisticalData.getCount40E());
item.setCountOthE(statisticalData.getCountOthE());
item.setCount20F(statisticalData.getCount20F());
item.setCount40F(statisticalData.getCount40F());
item.setCountOthF(statisticalData.getCountOthF());
item.setCount20Turn(statisticalData.getCount20Turn());
item.setCount40Turn(statisticalData.getCount40Turn());
item.setCountOthTurn(statisticalData.getCountOthTurn());
item.setCount20Danger(statisticalData.getCount20Danger());
item.setCount40Danger(statisticalData.getCount40Danger());
item.setCountOthDanger(statisticalData.getCountOthDanger());
}
});
}

// 箱量、TEU
Long ctnCount = itemList.stream().map(e -> Optional.ofNullable(e.getCtncount()).orElse(0l)).reduce(Long::sum).get();
BigDecimal teuCount = itemList.stream().map(e -> Optional.ofNullable(e.getTeuCount()).orElse(BigDecimal.ZERO)).reduce(BigDecimal.ZERO, BigDecimal::add);
// 空箱
Long count20E = itemList.stream().map(e -> Optional.ofNullable(e.getCount20E()).orElse(0l)).reduce(Long::sum).get();
Long count40E = itemList.stream().map(e -> Optional.ofNullable(e.getCount40E()).orElse(0l)).reduce(Long::sum).get();
Long countOthE = itemList.stream().map(e -> Optional.ofNullable(e.getCountOthE()).orElse(0l)).reduce(Long::sum).get();
// 重箱
Long count20F = itemList.stream().map(e -> Optional.ofNullable(e.getCount20F()).orElse(0l)).reduce(Long::sum).get();
Long count40F = itemList.stream().map(e -> Optional.ofNullable(e.getCount40F()).orElse(0l)).reduce(Long::sum).get();
Long countOthF = itemList.stream().map(e -> Optional.ofNullable(e.getCountOthF()).orElse(0l)).reduce(Long::sum).get();
// 翻舱箱
Long count20Turn = itemList.stream().map(e -> Optional.ofNullable(e.getCount20Turn()).orElse(0l)).reduce(Long::sum).get();
Long count40Turn = itemList.stream().map(e -> Optional.ofNullable(e.getCount40Turn()).orElse(0l)).reduce(Long::sum).get();
Long countOthTurn = itemList.stream().map(e -> Optional.ofNullable(e.getCountOthTurn()).orElse(0l)).reduce(Long::sum).get();
// 危险箱
Long count20Danger = itemList.stream().map(e -> Optional.ofNullable(e.getCount20Danger()).orElse(0l)).reduce(Long::sum).get();
Long count40Danger = itemList.stream().map(e -> Optional.ofNullable(e.getCount40Danger()).orElse(0l)).reduce(Long::sum).get();
Long countOthDanger = itemList.stream().map(e -> Optional.ofNullable(e.getCountOthDanger()).orElse(0l)).reduce(Long::sum).get();
// 总收入
BigDecimal totalAmount = itemList.stream().map(e -> Optional.ofNullable(e.getTotalAmount()).orElse(BigDecimal.ZERO)).reduce(BigDecimal.ZERO, BigDecimal::add);
//要返回的数据List集合
List<AnalysisContainerStatisticalVO.StatisticalItemListVo> resultList = new ArrayList<>();
//查询到的所有数据根据部门的分组map
Map<String, List<ContainerStatisticalItem>> itemListMap = itemList.stream().collect(Collectors.groupingBy(e -> Optional.ofNullable(e.getDeptname()).orElse("null")));
Set<Map.Entry<String, List<ContainerStatisticalItem>>> entrySetMap1 = itemListMap.entrySet();
for (Map.Entry<String, List<ContainerStatisticalItem>> entry1 : entrySetMap1) {
//要返回的整合数据中按分组区分的数据
AnalysisContainerStatisticalVO.StatisticalItemListVo itemListVo = new AnalysisContainerStatisticalVO.StatisticalItemListVo();
//具体分组数据中存放船期对应多条计费的数据集合
List<List<ContainerStatisticalItem>> multipleChargeList = new ArrayList<>();
//具体分组数据中存放船期对应单条计费的数据集合
List<ContainerStatisticalItem> otherList = new ArrayList<>();
//查询到的所有数据中某个部门的数据集合
List<ContainerStatisticalItem> valueList1 = entry1.getValue();
//将部门数据集合根据船名航次二次分组数据map
Map<String, List<ContainerStatisticalItem>> vslVoyContainerMap = valueList1.stream().collect(Collectors.groupingBy(e -> e.getCvslname() + e.getVoyage()));
Set<Map.Entry<String, List<ContainerStatisticalItem>>> entrySetMap2 = vslVoyContainerMap.entrySet();
//二次分组数据map 判断其对应计费数据数量 分别存入 要返回的整合数据中具体分组数据集合
for (Map.Entry<String, List<ContainerStatisticalItem>> entry2 : entrySetMap2) {
List<ContainerStatisticalItem> valueList2 = entry2.getValue();
if (!CollectionUtils.isEmpty(valueList2)) {
if (valueList2.size() > 1) {
//船期对应多条计费数据时,存入多条单独计费的集合
multipleChargeList.add(valueList2);
//总计中去除多条计费数据的重复箱数据影响
ctnCount = ctnCount - Optional.ofNullable(valueList2.get(0).getCtncount()).orElse(0l) * (valueList2.size() - 1);
teuCount = teuCount.subtract(Optional.ofNullable(valueList2.get(0).getTeuCount()).orElse(BigDecimal.ZERO).multiply(BigDecimal.valueOf(valueList2.size() - 1)));
count20E = count20E - Optional.ofNullable(valueList2.get(0).getCount20E()).orElse(0l) * (valueList2.size() - 1);
count40E = count40E - Optional.ofNullable(valueList2.get(0).getCount40E()).orElse(0l) * (valueList2.size() - 1);
countOthE = countOthE - Optional.ofNullable(valueList2.get(0).getCountOthE()).orElse(0l) * (valueList2.size() - 1);
count20F = count20F - Optional.ofNullable(valueList2.get(0).getCount20F()).orElse(0l) * (valueList2.size() - 1);
count40F = count40F - Optional.ofNullable(valueList2.get(0).getCount40F()).orElse(0l) * (valueList2.size() - 1);
countOthF = countOthF - Optional.ofNullable(valueList2.get(0).getCountOthF()).orElse(0l) * (valueList2.size() - 1);
count20Turn = count20Turn - Optional.ofNullable(valueList2.get(0).getCount20Turn()).orElse(0l) * (valueList2.size() - 1);
count40Turn = count40Turn - Optional.ofNullable(valueList2.get(0).getCount40Turn()).orElse(0l) * (valueList2.size() - 1);
countOthTurn = countOthTurn - Optional.ofNullable(valueList2.get(0).getCountOthTurn()).orElse(0l) * (valueList2.size() - 1);
count20Danger = count20Danger - Optional.ofNullable(valueList2.get(0).getCount20Danger()).orElse(0l) * (valueList2.size() - 1);
count40Danger = count40Danger - Optional.ofNullable(valueList2.get(0).getCount40Danger()).orElse(0l) * (valueList2.size() - 1);
countOthDanger = countOthDanger - Optional.ofNullable(valueList2.get(0).getCountOthDanger()).orElse(0l) * (valueList2.size() - 1);
} else {
//单条计费数据不需要单独合计
otherList.add(valueList2.get(0));
}
}
}
itemListVo.setMultipleChargeList(multipleChargeList);
itemListVo.setOtherList(otherList);
resultList.add(itemListVo);
}
result.setCtncount(ctnCount);
result.setTeuCount(teuCount);
result.setCount20E(count20E);
result.setCount40E(count40E);
result.setCountOthE(countOthE);
result.setCount20F(count20F);
result.setCount40F(count40F);
result.setCountOthF(countOthF);
result.setCount20Turn(count20Turn);
result.setCount40Turn(count40Turn);
result.setCountOthTurn(countOthTurn);
result.setCount20Danger(count20Danger);
result.setCount40Danger(count40Danger);
result.setCountOthDanger(countOthDanger);
result.setTotalAmount(totalAmount);
result.setList(resultList);
}
return result;

posted @ 2023-07-23 16:49  流浪Coder  阅读(19)  评论(0)    收藏  举报