ssm实战(6)------购物车管理
购物车管理
功能:
加入商品; 更新商品数; 查询商品数; 移除商品; 单选/取消; 全选/取消; 购物车列表
接口
门户:购物车list列表; 购物车添加商品; 更新购物车某个产品数量; 移除购物车某个产品; 购物车选中某个商品; 取消选中某个产品; 查询购物车产品数量;购物车全选;购物车取消全选
注
guava截取字符串:
productIds=“zhangshang,lisi”//多个用户id的字符串 List<String> productList = Splitter.on(",").splitToList(productIds);
计算商品数量的一个隐患:当查询到的数量为空时,无法将null赋值给int。 解决方法:IFNULL(sum(quantity),0) Mysql内置函数,如果sum(quantity)为空,则赋值为0
//mapper.java int selectCartProductCount(@Param(value = "userId") Integer userId); //mapper.xml <select id="selectCartProductCount" parameterType="int" resultType="int"> select IFNULL(sum(quantity),0) as count from eshop_cart where user_id=#{userId} </select>
学习目标:
1 购物车模块的设计思想:封装一个购物车的核心方法;每当对购物车进行操作后,都调用该方法,该方法会对CartVo进行 装配 和数量的校验,并返回一个CartVo
2 封装一个购物车核心方法
/** * 限制购物车,其中会判断数量是否正确 * @param userId * @return */ private CartVo getCartVoLimit(Integer userId){ CartVo cartVo=new CartVo(); //获得该用户的购物车list List<Cart> cartList=cartMapper.selectCartByUserId(userId); //创建一个CarProductVoList List<CartProductVo> cartProductVoList= Lists.newArrayList(); BigDecimal cartTotalPrice=new BigDecimal("0"); //判断购物车是否为空 if(CollectionUtils.isNotEmpty(cartList)){ //将Cart的项装配到CarProductVo,再add到CarProductVoList 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; } /** * 判断购物车是否全选 * @param userId * @return */ private boolean getAllCheckedStatus(Integer userId){ if(userId==null){ return false; } return cartMapper.selectCartProductCheckedStatusByUserId(userId)==0; }
cartProductVo:
package com.eshop.vo; import java.math.BigDecimal; /** * 将产品和购物车结合在一起 的一个抽象对象 */ public class CartProductVo { private Integer id;//购物车id private Integer userId; private Integer productId; private Integer quantity;//产品购买数量 private String productName; private String productSubtitle; private String productMainImage; private BigDecimal productPrice; private Integer productStatus; private BigDecimal productTotalPrice;//产品总价 private Integer productStock;//产品库存数量 private Integer productChecked; private String limitQuantity;//限制商品的数量的返回结果 public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getUserId() { return userId; } public void setUserId(Integer userId) { this.userId = userId; } public Integer getProductId() { return productId; } public void setProductId(Integer productId) { this.productId = productId; } public Integer getQuantity() { return quantity; } public void setQuantity(Integer quantity) { this.quantity = quantity; } public String getProductName() { return productName; } public void setProductName(String productName) { this.productName = productName; } public String getProductSubtitle() { return productSubtitle; } public void setProductSubtitle(String productSubtitle) { this.productSubtitle = productSubtitle; } public String getProductMainImage() { return productMainImage; } public void setProductMainImage(String productMainImage) { this.productMainImage = productMainImage; } public BigDecimal getProductPrice() { return productPrice; } public void setProductPrice(BigDecimal productPrice) { this.productPrice = productPrice; } public Integer getProductStatus() { return productStatus; } public void setProductStatus(Integer productStatus) { this.productStatus = productStatus; } public BigDecimal getProductTotalPrice() { return productTotalPrice; } public void setProductTotalPrice(BigDecimal productTotalPrice) { this.productTotalPrice = productTotalPrice; } public Integer getProductStock() { return productStock; } public void setProductStock(Integer productStock) { this.productStock = productStock; } public Integer getProductChecked() { return productChecked; } public void setProductChecked(Integer productChecked) { this.productChecked = productChecked; } public String getLimitQuantity() { return limitQuantity; } public void setLimitQuantity(String limitQuantity) { this.limitQuantity = limitQuantity; } }
CartVo:
package com.eshop.vo; import java.math.BigDecimal; import java.util.List; public class CartVo { private List<CartProductVo> cartProductVoList; private BigDecimal cartTotalPrice; private Boolean allChecked; private String imageHost; public List<CartProductVo> getCartProductVoList() { return cartProductVoList; } public void setCartProductVoList(List<CartProductVo> cartProductVoList) { this.cartProductVoList = cartProductVoList; } public BigDecimal getCartTotalPrice() { return cartTotalPrice; } public void setCartTotalPrice(BigDecimal cartTotalPrice) { this.cartTotalPrice = cartTotalPrice; } public Boolean getAllChecked() { return allChecked; } public void setAllChecked(Boolean allChecked) { this.allChecked = allChecked; } public String getImageHost() { return imageHost; } public void setImageHost(String imageHost) { this.imageHost = imageHost; } }
3 解决浮点型商业运算中精度丢失: 使用BigDecimal ,注意一定要使用 BigDecimal(String str )字符串的构造器
public static BigDecimal add(double v1,double v2){ BigDecimal b1=new BigDecimal(Double.toString(v1)); BigDecimal b2=new BigDecimal(Double.toString(v2)); return b1.add(b2); }
该方法:
package com.eshop.util; import java.math.BigDecimal; import java.math.RoundingMode; public class BigDecimalUtil { private BigDecimalUtil(){ //私有构造器防止被实例化 } public static BigDecimal add(double v1,double v2){ BigDecimal b1=new BigDecimal(Double.toString(v1)); BigDecimal b2=new BigDecimal(Double.toString(v2)); return b1.add(b2); } public static BigDecimal sub(double v1,double v2){ BigDecimal b1=new BigDecimal(Double.toString(v1)); BigDecimal b2=new BigDecimal(Double.toString(v2)); return b1.subtract(b2); } public static BigDecimal mul(double v1,double v2){ BigDecimal b1=new BigDecimal(Double.toString(v1)); BigDecimal b2=new BigDecimal(Double.toString(v2)); return b1.multiply(b2); } public static BigDecimal div(double v1,double v2){ BigDecimal b1=new BigDecimal(Double.toString(v1)); BigDecimal b2=new BigDecimal(Double.toString(v2)); return b1.divide(b2,2, RoundingMode.HALF_UP); //除不尽的情况:取两位小数,四舍五入 } }