java.util.stream

 

 

java.util.stream

 @ApiOperation(value = "查详情")
    @GetMapping("/getDetail")
    public R getDetail(PurchaseOrderQueryDto query) {
//        PurchaseOrderDto orderDto = purchaseOrderService.getDetail(query);
//        orderDto.setTotalQuantity(orderDto.getDetails().size());
        List<PurchaseOrderDto> dtos = purchaseOrderService.selectList(query);
        if (CollectionUtils.isEmpty(dtos)) {
            return R.fail("该订单不存在,请检查");
        }
        PurchaseOrderDto orderDto = dtos.get(0);
        List<String> purchaseOrderNos = dtos.stream().map(PurchaseOrderDto::getPurchaseOrderNo).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(purchaseOrderNos) && purchaseOrderNos.size() == 1) {
            List<PurchaseOrderSkuDto> skuDtos = purchaseOrderService.selectSkuList(purchaseOrderNos);
//            orderDto.setDetails(skuDtos);
//            orderDto.setTotalQuantity(skuDtos.size());

            //每单按照sku 聚合
            Map<String, List<PurchaseOrderSkuDto>> map = skuDtos.stream().filter(skuDto -> orderDto.getPurchaseOrderNo().equals(skuDto.getPurchaseOrderNo()))
                    .collect(Collectors.groupingBy(PurchaseOrderSkuDto::getPurchaseSkuCode));
            List<PurchaseOrderSkuDto> details = new ArrayList<>();

            map.forEach((key, value) -> {
                PurchaseOrderSkuDto dto = new PurchaseOrderSkuDto();
                BeanUtils.copyProperties(value.get(0), dto);
                BigDecimal quantitySum = value.stream().filter(t -> null != t.getQuantity())
                        .map(PurchaseOrderSkuDto::getQuantity).reduce(BigDecimal.ZERO, BigDecimal::add);
                BigDecimal saleQtySum = value.stream().filter(t -> null != t.getSaleQty())
                        .map(PurchaseOrderSkuDto::getSaleQty).reduce(BigDecimal.ZERO, BigDecimal::add);
                BigDecimal receiveQtySum = value.stream().filter(t -> null != t.getReceiveQty())
                        .map(PurchaseOrderSkuDto::getReceiveQty).reduce(BigDecimal.ZERO, BigDecimal::add);

                dto.setQuantity(quantitySum);
                dto.setSaleQty(saleQtySum);
                dto.setReceiveQty(receiveQtySum);
                details.add(dto);
            });
            orderDto.setDetails(details);
            orderDto.setTotalQuantity(details.size());
        }
        return R.ok(orderDto);
    }

 

posted @ 2022-07-16 11:02  yufeiyunsui  阅读(46)  评论(0编辑  收藏  举报