做的物资管理系统涉及到很多数据的分组统计,之前都是直接在数据库里面查询的,直到看到大佬写的代码,自愧不如。下面就上一段代码,也算是做个笔记吧。
private Map<String,Double> getPickingAmountMap(final List<Long> taskIds){
final List<PmsPickLineEntity> pmsPickLineEntities = taskManagementLedgerMapper.getPickingAmount(taskIds);
final Map<String, List<PmsPickLineEntity>> map = pmsPickLineEntities.stream().collect(Collectors.groupingBy(o -> o.getProjectCode() + "_" + o.getTaskCode() + "_" + o.getDesignName() + "_" + o.getDesignSpecifications() + "_" + o.getMeasurementUnit()));
final Map<String,Double> pickingAmountMap = new HashMap<>();
map.forEach((key, value) -> {
if (null !=value && !value.isEmpty()){
pickingAmountMap.put(key,value.stream().filter(p -> null != p.getPickingAmount()).
map(pmsPickLineEntity -> BigDecimal.valueOf(pmsPickLineEntity.getPickingAmount())).reduce(BigDecimal::add).orElse(BigDecimal.ZERO).doubleValue());
}
});
return pickingAmountMap;
}