create table 'mmall_cart'(
'id' int(11) NOT NULL AUTO_INCREMENT,
'user_id' int(11) NOT NULL COMMENT '用户id',
'product_id' int(11) DEFAULT NULL COMMENT '商品id',
'quantity' int(11) DEFAULT NULL COMMENT '数量',
'checked' int(11) DEFAULT NULL COMMENT '是否选择:0-未勾选,1-已勾选',
'create_time' datetime DEFAULT NULL COMMENT '创建时间',
'update_time' datetime DEFAULT NULL COMMENT '更新时间',
PRIMARY KEY ('id'),
KEY 'user_id_index' ('user_id') USING BTREE
)ENGINE=InnoDB AUTO_INCREMENT=121 DEFAULT CHARSET=utf8
public class CartVo {
private List<CartProductVo> cartProductVoList;
private BigDecimal cartTotalPrice; //购物车选中商品的总价格
private Boolean allChecked;//是否已经都勾选
private String imageHost; //商品图片地址
}
CartProductVo类,省略了set,get方法
public class CartProductVo {
//结合了产品和购物车的一个抽象对象
private Integer 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 class Const {
public static final String CURRENT_USER = "currentUser";
public static final String EMAIL="email";
public static final String USERNAME="username";
public interface Role{
int ROLE_CUSTOMER=0;//普通用户
int ROLE_ADMIN=1;//管理员
}
public interface Cart{
int CHECKED = 1;//即购物车选中状态
int UN_CHECKED = 0;//购物车中未选中状态
String LIMIT_NUM_FAIL = "LIMIT_NUM_FAIL"; //库存不充足
String LIMIT_NUM_SUCCESS = "LIMIT_NUM_SUCCESS"; //库存充足
}
}
//给购物车做限制,限制购买的数量不能超过库存量,精确商品总价
private CartVo getCartVoLimit(Integer userId) {
CartVo cartVo = new CartVo();
//用List存储该用户购物车里的信息
List<Cart> cartList = cartMapper.selectByUserId(userId);
List<CartProductVo> cartProductVoList = Lists.newArrayList();
//public BigDecimal(String val),String构造器。将String表示形式转换成BigDecimal
BigDecimal cartTotalPrice = new BigDecimal("0");
//如果该用户购物车里的商品不为空,CartProductVo为展示在前端的数据
if (CollectionUtils.isNotEmpty(cartList)) {
for (Cart cartItem : cartList) {
CartProductVo cartProductVo = new CartProductVo();
cartProductVo.setId(cartItem.getId());
cartProductVo.setProductId(cartItem.getProductId());
cartProductVo.setUserId(userId);
//查询该商品的信息
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);
}
}
car