关于购物车的getCartVoLimit方法
来源:9-3 加入购物车功能开发2
tidhy
2017-09-09
private CartVo getCartVoLimit(Integer userId){
CartVo cartVo = new CartVo();
List<Cart> cartList = cartMapper.selectCartByUserId(userId);
List<CartProductVo> cartProductVoList = Lists.newArrayList();
BigDecimal cartTotalPrice = new BigDecimal("0");
if(CollectionUtils.isNotEmpty(cartList)){
for(Cart cartItem : cartList){
CartProductVo cartProductVo = new CartProductVo();
cartProductVo.setId(cartItem.getId());
cartProductVo.setUserId(userId);
cartProductVo.setProductId(cartItem.getProductId());
Product product = productMapper.selectByPrimaryKey(cartItem.getProductId());
if(product != null){
cartProductVo.setProductMainImage(product.getMainImage());
cartProductVo.setProductName(product.getName());
cartProductVo.setProductSubtitle(product.getSubtitle());
cartProductVo.setProductStatus(product.getStatus());
cartProductVo.setProductPrice(product.getPrice());
cartProductVo.setProductStock(product.getStock());
//判断库存
int buyLimitCount = 0;
if(product.getStock() >= cartItem.getQuantity()){
//库存充足的时候
buyLimitCount = cartItem.getQuantity();
cartProductVo.setLimitQuantity(Const.Cart.LIMIT_NUM_SUCCESS);
}else{
buyLimitCount = product.getStock();
cartProductVo.setLimitQuantity(Const.Cart.LIMIT_NUM_FAIL);
//购物车中更新有效库存
Cart cartForQuantity = new Cart();
cartForQuantity.setId(cartItem.getId());
cartForQuantity.setQuantity(buyLimitCount);
cartMapper.updateByPrimaryKeySelective(cartForQuantity);
}
cartProductVo.setQuantity(buyLimitCount);
//计算总价
cartProductVo.setProductTotalPrice(BigDecimalUtil.mul(product.getPrice().doubleValue(),cartProductVo.getQuantity()));
cartProductVo.setProductChecked(cartItem.getChecked());
}
if(cartItem.getChecked() == Const.Cart.CHECKED){
//如果已经勾选,增加到整个的购物车总价中
cartTotalPrice = BigDecimalUtil.add(cartTotalPrice.doubleValue(),cartProductVo.getProductTotalPrice().doubleValue());
}
cartProductVoList.add(cartProductVo);
}
}
cartVo.setCartTotalPrice(cartTotalPrice);
cartVo.setCartProductVoList(cartProductVoList);
cartVo.setAllChecked(this.getAllCheckedStatus(userId));
cartVo.setImageHost(PropertiesUtil.getProperty("ftp.server.http.prefix"));
return cartVo;
}
private boolean getAllCheckedStatus(Integer userId){
if(userId == null){
return false;
}
return cartMapper.selectCartProductCheckedStatusByUserId(userId) == 0;
}老师,您说这个方法才是购物车的重点,但我只看到一个查询购物车list方法调用过他,上面这方法我大概明白是什么意思,但是没有地方在调用他,感到很不解。。。。
写回答
2回答
-
你好,这里面的逻辑主要是获取购物车中的数据,同时还要组装好购物车数据例如图片的http地址等信息。
还有一个是购物车里的数量的教研,并把限制数量是否成功返回给前端。做出对应的提示。
例如
product.getStock() >= cartItem.getQuantity()){
//库存充足的时候
buyLimitCount = cartItem.getQuantity();
cartProductVo.setLimitQuantity(Const.Cart.LIMIT_NUM_SUCCESS);
}else{
buyLimitCount = product.getStock();
cartProductVo.setLimitQuantity(Const.Cart.LIMIT_NUM_FAIL);
这里面就有对应的判断~~所以,所有展示购物车列表都会调用这个方法。
00 -
tidhy
提问者
2017-09-09
额。。。用到的是这个泛型接收数据,然后再进行判断?
012017-09-11
相似问题