购物车模块

1.添加购物车

Controller层

@RequestMapping("add.do")
	@ResponseBody
	public ServerResponse add(HttpSession session,Integer count,Integer productId){
		User user = (User) session.getAttribute(Const.CURRENT_USER);
		if(user==null){
			return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
		}
		return iCartService.add(user.getId(), productId, count);
	}

Service层

public ServerResponse add(Integer userId,Integer productId,Integer count){
		if(productId==null||count==null){
			return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getDesc());
		}
		Cart cart = cartMapper.selectCartByUserIdProductId(userId, productId);
		if(cart==null){
			//这个产品不在这个购物车里,需要新增一个这个产品的记录
			Cart cartItem = new Cart();
			cartItem.setQuantity(count);
			cartItem.setChecked(Const.Cart.CHECKED);
			cartItem.setProductId(productId);
			cartItem.setUserId(userId);
			cartMapper.insert(cartItem);
		}else{
			//这个产品已经在购物车里了.
            //如果产品已存在,数量相加
			count=cart.getQuantity()+count;
			cart.setQuantity(count);
			cartMapper.updateByPrimaryKeySelective(cart);
		}
		CartVo cartVo = this.getCartVoLimit(userId);
		return ServerResponse.createBySuccess(cartVo);
	}
	
	private CartVo getCartVoLimit(Integer userId){
		CartVo cartVo = new CartVo();
		List cartList = cartMapper.selectCartByUserId(userId);
		List 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;
	}

业务逻辑:首先判断用户是否登陆,如果登陆的话,然后通过userId和productId查询购物车表判断购物车里是否有这个商品,如果没有则新增一个这个商品记录,如果购物车中有这个商品,则数量相加。然后通过userId查询所有商品的集合,然后判断这个集合是否为空,如果不为空则循环遍历这个集合,将商品重新组装到新创建的CartProductVo对象中,然后通过循环遍历出的商品id查询出商品,然后判断这个商品是否为空,如果不为空,继续将这个商品的属性添加到carProducVo对象中,然后判断库存,当库存充足的时候,就将购买商品的数量添加到cartProduct中,如果库存不充足则将库存中的所以商品添加到CartProductVo对象中,然后通过封装一个BigDecimalUtil对商品总价进行计算添加到CartProductVo对象中,然后判断这个商品是否被勾选,如果被勾选则增加到整个购物车总价中,然后将cartProductVo对象添加到一个集合中,然后将总价格和这个购买商品的集合添加到CartVo对象中,最后返回购物车对象。



你可能感兴趣的:(购物车模块)