购物车模块的开发

需求分析:

实现一个购物城模块,并具有电商平台的功能,比如添加商品,删除商品,显示当前购物车中的商品列表,在列表上单选/单反选,全选/全不选等

项目使用MVC的设计,首先是CartController类

public class CartController {
@Autowired
	private ICartService iCartService;

@RequestMapping("add.do")
@ResponseBody
	public ServerResponse<CartVo> add(HttpSession session,Integer userId,Integer productId,Integer count) {
		User user =(User)session.getAttribute(Const.CURRENT_USER);
		if(user==null) {
			return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请登录");
		}
		return iCartService.add(userId, productId, count);
	}
//更新购物车	
@RequestMapping("update.do")
@ResponseBody
	public ServerResponse<CartVo> update(HttpSession session,Integer userId,Integer productId,Integer count) {
		User user =(User)session.getAttribute(Const.CURRENT_USER);
		if(user==null) {
			return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请登录");
		}
		return iCartService.update(userId, productId, count);
	}
//删除购物车	
@RequestMapping("delete_product.do")
@ResponseBody
	public ServerResponse<CartVo> deleteProduct(HttpSession session,Integer userId,String productIds) {
		User user =(User)session.getAttribute(Const.CURRENT_USER);
		if(user==null) {
			return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请登录");
		}
		return iCartService.deleteProduct(userId, productIds);
	}
//查询
@RequestMapping("list.do")
@ResponseBody
	public ServerResponse<CartVo> list(HttpSession session) {
		User user =(User)session.getAttribute(Const.CURRENT_USER);
		if(user==null) {
			return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请登录");
		}
		return iCartService.list(user.getId());
	}
//全选
@RequestMapping("select_all.do")
@ResponseBody
	public ServerResponse<CartVo> selectAll(HttpSession session) {
		User user =(User)session.getAttribute(Const.CURRENT_USER);
		if(user==null) {
			return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请登录");
		}
		return iCartService.selectOrUnSelect(user.getId(),null, Const.Cart.CHECKED);
	}
//全不选
@RequestMapping("un_select_all.do")
@ResponseBody
	public ServerResponse<CartVo> unSelectAll(HttpSession session) {
		User user =(User)session.getAttribute(Const.CURRENT_USER);
		if(user==null) {
			return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请登录");
		}
		return iCartService.selectOrUnSelect(user.getId(), null,Const.Cart.UN_CHECKED);
	}

//单独不选
@RequestMapping("un_select.do")
@ResponseBody
	public ServerResponse<CartVo> unSelect(HttpSession session,Integer productId) {
		User user =(User)session.getAttribute(Const.CURRENT_USER);
		if(user==null) {
			return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请登录");
		}
		return iCartService.selectOrUnSelect(user.getId(), productId,Const.Cart.UN_CHECKED);
	}

//单独选
@RequestMapping("select.do")
@ResponseBody
	public ServerResponse<CartVo> select(HttpSession session,Integer productId) {
		User user =(User)session.getAttribute(Const.CURRENT_USER);
		if(user==null) {
			return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请登录");
		}
		return iCartService.selectOrUnSelect(user.getId(), productId,Const.Cart.CHECKED);
	}

//查询购物车产品数量
@RequestMapping("get_cart_product_count.do")
@ResponseBody
	public ServerResponse<Integer> getCartProductCount(HttpSession session,Integer userId) {
		User user =(User)session.getAttribute(Const.CURRENT_USER);
		if(user==null) {
			return ServerResponse.createBySuccess(0);
		}
		return iCartService.getCartProductCount(userId);
	}

Controller层中的方法去调用Service层

public interface ICartService {
	ServerResponse add(Integer userId,Integer productId,Integer count );
	ServerResponse<CartVo> update(Integer userId,Integer productId,Integer count);
	ServerResponse<CartVo> deleteProduct(Integer userId,String productIds);
	ServerResponse<CartVo> list(Integer userId);
	ServerResponse<CartVo> selectOrUnSelect(Integer userId,Integer productId,Integer checked);
	ServerResponse<Integer> getCartProductCount(Integer userId);
	
}

ICartService主要是定义一些接口,具体的实现在cartServiceImpl

@Service("iCartService")
public class cartServiceImpl implements ICartService{
	@Autowired
	private CartMapper cartMapper;
	@Autowired
	private ProductMapper productMapper;
	
	public ServerResponse<CartVo> add(Integer userId,Integer productId,Integer count ) {
		if(productId==null || count==null) {
			return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_AGUMENT.getCode(), ResponseCode.ILLEGAL_AGUMENT.getDesc());
		}
		Cart cart =cartMapper.selectCartByUserIdProductId(userId, productId);
		if(cart==null) {
//			这个产品不在购物车中,应该新增一个
			Cart cartItem = new Cart();
			cartItem.setChecked(Const.Cart.CHECKED);
			cartItem.setQuantity(count);
			cartItem.setUserId(userId);
			cartItem.setProductId(productId);
			cartMapper.insert(cartItem);
		}else {
		//这个在购物车中
			count = count+cart.getQuantity();
			cart.setQuantity(count);
			cartMapper.updateByPrimaryKeySelective(cart);
		}
		return this.list(userId);
	}
	//更新购物车
	public ServerResponse<CartVo> update(Integer userId,Integer productId,Integer count){
		if(productId==null || count==null) {
			return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_AGUMENT.getCode(), ResponseCode.ILLEGAL_AGUMENT.getDesc());
		}
		Cart cart =cartMapper.selectCartByUserIdProductId(userId, productId);
		if(cart!=null) {
			cart.setQuantity(count);
		}
		cartMapper.updateByPrimaryKeySelective(cart);
		return this.list(userId);
		
}
	
	//删除购物车
		public ServerResponse<CartVo> deleteProduct(Integer userId,String productIds){
			List<String> productList=Splitter.on(",").splitToList(productIds);
			if(CollectionUtils.isEmpty(productList)) {
				return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_AGUMENT.getCode(), ResponseCode.ILLEGAL_AGUMENT.getDesc());
				
			}
			cartMapper.deleteByUserIdProductIds(userId,productList);
			return this.list(userId);
	}
		
		//查询
		public ServerResponse<CartVo> list(Integer userId){
			CartVo cartVo =this.getCartVoLimit(userId);
			return ServerResponse.createBySuccess(cartVo); 
	}
	//全选
		public ServerResponse<CartVo> selectOrUnSelect(Integer userId,Integer productId,Integer checked){
			cartMapper.checkedOrUncheckedProduct(userId,productId, checked);
			return this.list(userId);
		}
		
		//统计购物车中数量
		public ServerResponse<Integer> getCartProductCount(Integer userId){
			if(userId==null) {
				return ServerResponse.createBySuccess(0);
			}
			return ServerResponse.createBySuccess(cartMapper.selectCartProductCount(userId));
		}
	
	//主方法
	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.setProductId(cartItem.getProductId());
				cartProductVo.setUserId(cartItem.getUserId());
				
				Product product =productMapper.selectByPrimaryKey(cartItem.getProductId());
				if(product!=null) {
					cartProductVo.setProductName(product.getName());
					cartProductVo.setProductMainImage(product.getMainImage());
					cartProductVo.setProductSubtitle(product.getSubtitle());
					cartProductVo.setProductStock(product.getStock());
					cartProductVo.setProductPrice(product.getPrice());
					cartProductVo.setProductStatus(product.getStatus());
					
					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.updateByPrimaryKey(cartForQuantity);
					}
					cartProductVo.setQuantity(buyLimitCount);
//					某一产品的总价
					cartProductVo.setProductTotalPrice(BigDecimalUtil.mul(product.getPrice().doubleValue(), cartProductVo.getQuantity().doubleValue()));
					cartProductVo.setChecked(cartItem.getChecked());
				}
				if(cartItem.getChecked()==Const.Cart.CHECKED) {
					//如果已勾选,则增加到购物车中的总价中
					cartTotalPrice =BigDecimalUtil.add(cartTotalPrice.doubleValue(), cartProductVo.getProductPrice().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;
	}

Service层再去调用dao层

dao层主要是在xml中编写sql语句,来实现和数据库的交互。代码不给出,比较简单

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值