`

流式计算

    博客分类:
  • java
阅读更多
private void postHandle(List<QtyPredictTask> taskList, List<SkuSolutionVo> skus) throws IOException, BusinessException {
        List<QtyTaskResult> results = taskList.stream().map(task -> task.getOutput()).collect(Collectors.toList());

        Map<String, Double> priceMap = results.parallelStream().collect(Collectors.groupingBy(e -> fetchGroupKey(e),
                Collectors.averagingDouble(QtyTaskResult::getPrice)));

        Map<String, Double> qtyMap = results.parallelStream().collect(Collectors.groupingBy(e -> fetchGroupKey(e),
                Collectors.summingDouble(QtyTaskResult::getQty)));

        Map<String, Double> revenueMap = results.parallelStream().collect(Collectors.groupingBy(e -> fetchGroupKey(e),
                Collectors.summingDouble(e -> e.getQty() * e.getPrice())));

        Map<String, Double> profitMap = results.parallelStream().collect(Collectors.groupingBy(e -> fetchGroupKey(e),
                Collectors.summingDouble(e -> e.getQty() * e.getPrice() - e.getQty() * e.getCostPrice())));

        for (SkuSolutionVo sku : skus) {
            String key = sku.getSkuCode() + PromConstant.seperator + sku.getLocationCode();
            sku.setPromotionPrice(OperatorUtil.format3(priceMap.get(key)));
            sku.setAllSalesQty(OperatorUtil.format3(qtyMap.get(key)));
            sku.setAllSalesRevenue(OperatorUtil.format3(revenueMap.get(key)));
            sku.setAllSalesProfit(OperatorUtil.format3(profitMap.get(key)));
            sku.setPromotionQty(OperatorUtil.format3(sku.getAllSalesQty() - sku.getBaselineQty()));
            sku.setPromotionRevenue(OperatorUtil.format3(sku.getAllSalesRevenue() - sku.getBaselineRevenue()));
            sku.setPromotionProfit(OperatorUtil.format3(sku.getAllSalesProfit() - sku.getBaselineProfit()));
        }
        // 启动新线程,完成子任务的更新
        thread(taskList);
    }

 

    完成方法的计算

 

    private String fetchGroupKey(QtyTaskResult qtyTaskResult) {
        return qtyTaskResult.getSkuCode() + PromConstant.seperator + qtyTaskResult.getLocationCode();
    }

   

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics