springboot项目之计算购物车

商城购物车

商城购物车计算,感觉还有很多的优化空间,后期再做优化方案

代码实现 

public AjaxResult calCart(@RequestParam("bgroupId") String bgroupId,@RequestParam("cartIds") String cartIds){

        **********************略******************
        Long[] ids= Convert.toLongArray(cartIds);
        //List cartList=new ArrayList<>();
        final BigDecimal stotal = BigDecimal.valueOf(0);
        List cartList=Arrays.stream(ids).map(e->{
            CartTotalDto cartTotalDto=new CartTotalDto();
            DdBgroupCart cart=iDdBgroupCartService.selectDdBgroupCartByCartId(e);
            cartTotalDto.setCartId(cart.getCartId());
            cartTotalDto.setAmount(cart.getAmount());
            BigDecimal uniPrice=commService.getUnitPrice(cart.getWeighId());
            cartTotalDto.setUnitPrice(uniPrice);
            DdGoodWeigh ddGoodWeigh=iDdGoodWeighService.selectDdGoodWeighByGoodWeighTreeId(cart.getWeighId());
            //计算总价
            BigDecimal total=BigDecimal.valueOf(0);
            if(cart.getWeighUnit()==null){
                total=commService.getTotalPrice(cart.getAmount(),uniPrice,ddGoodWeigh.getUpWeigh(), BigDecimal.valueOf(1));
                total = total.setScale(2, BigDecimal.ROUND_HALF_UP);//四舍五入
                cartTotalDto.setProductPrice(total);
                //中转费
                BigDecimal zhongPrice=commService.getZhongPrice(cart.getAmount(),ddGoodWeigh.getZhongPrice(),ddGoodWeigh.getUpWeigh(), BigDecimal.valueOf(1));
                zhongPrice = zhongPrice.setScale(2, BigDecimal.ROUND_HALF_UP);//四舍五入
                cartTotalDto.setZhongPrice(zhongPrice);
                //配站费
                BigDecimal platPrice=commService.getPlatPrice(cart.getAmount(),ddGoodWeigh.getStationPrice(),ddGoodWeigh.getUpWeigh(), BigDecimal.valueOf(1));
                platPrice = platPrice.setScale(2, BigDecimal.ROUND_HALF_UP);//四舍五入
                cartTotalDto.setPlatePrice(platPrice);
                //配送费
                BigDecimal peiPrice=commService.getDeliverPrice(cart.getAmount(),ddGoodWeigh.getStationPrice(),ddGoodWeigh.getUpWeigh(),BigDecimal.valueOf(1),101l,bgroupAddr.getLat(),bgroupAddr.getLng());
                //计算总计
                peiPrice = peiPrice.setScale(2, BigDecimal.ROUND_HALF_UP);//四舍五入
                cartTotalDto.setDeliverPrice(peiPrice);
                BigDecimal sum=NumberUtil.add(total,zhongPrice).add(peiPrice).add(platPrice);//total.add(zhongPrice).add(platPrice).add(peiPrice);
                sum = sum.setScale(2, BigDecimal.ROUND_HALF_UP);//四舍五入
                cartTotalDto.setTotalPrice(sum);
                return cartTotalDto;
            }else {
                BigDecimal pate=commService.getWeighPate(cart.getWeighUnit());
                total=commService.getTotalPrice(cart.getAmount(),uniPrice,ddGoodWeigh.getUpWeigh(), pate);
                total = total.setScale(2, BigDecimal.ROUND_HALF_UP);//四舍五入
                cartTotalDto.setProductPrice(total);

                //中转费
                BigDecimal zhongPrice=commService.getZhongPrice(cart.getAmount(),ddGoodWeigh.getZhongPrice(),ddGoodWeigh.getUpWeigh(), pate);
                zhongPrice = zhongPrice.setScale(2, BigDecimal.ROUND_HALF_UP);//四舍五入
                cartTotalDto.setZhongPrice(zhongPrice);
                //配站费
                BigDecimal platPrice=commService.getPlatPrice(cart.getAmount(),ddGoodWeigh.getStationPrice(),ddGoodWeigh.getUpWeigh(), pate);
                platPrice = platPrice.setScale(2, BigDecimal.ROUND_HALF_UP);//四舍五入
                cartTotalDto.setPlatePrice(platPrice);

                //配送费
                BigDecimal peiPrice=commService.getDeliverPrice(cart.getAmount(),ddGoodWeigh.getStationPrice(),ddGoodWeigh.getUpWeigh(),pate,101l,bgroupAddr.getLat(),bgroupAddr.getLng());
                //计算总计
                peiPrice = peiPrice.setScale(2, BigDecimal.ROUND_HALF_UP);//四舍五入
                cartTotalDto.setDeliverPrice(peiPrice);
                //计算总计
                BigDecimal sum=NumberUtil.add(total,zhongPrice).add(peiPrice).add(platPrice);//total.add(zhongPrice).add(platPrice).add(peiPrice);
                sum = sum.setScale(2, BigDecimal.ROUND_HALF_UP);//四舍五入
                cartTotalDto.setTotalPrice(sum);
                return cartTotalDto;
            }

        }).collect(Collectors.toList());
        
***********************************************************略
    }

你可能感兴趣的:(spring,boot,java,后端)