从零到企业级SSM电商项目实战教程(三十四)全选,全反选,单选,单反选,查询购物车商品数量功能开发

CartController.java

//全选
@RequestMapping("select_all.do")
@ResponseBody
public ServerResponse selectAll(HttpSession session) {
    User user = (User) session.getAttribute(Const.CURRENT_USER);
    if (user == null) {
        return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
    }
    return iCartService.selectOrUnSelect(user.getId(), null, Const.Cart.CHECKED);
}

//全反选
@RequestMapping("un_select_all.do")
@ResponseBody
public ServerResponse unSelectAll(HttpSession session) {
    User user = (User) session.getAttribute(Const.CURRENT_USER);
    if (user == null) {
        return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
    }
    return iCartService.selectOrUnSelect(user.getId(), null, Const.Cart.UN_CHECKED);
}

//单独选
@RequestMapping("select.do")
@ResponseBody
public ServerResponse select(HttpSession session, 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.selectOrUnSelect(user.getId(), productId, Const.Cart.CHECKED);
}


//单独反选
@RequestMapping("un_select.do")
@ResponseBody
public ServerResponse unSelect(HttpSession session, 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.selectOrUnSelect(user.getId(), productId, Const.Cart.UN_CHECKED);
}

//查询当前用户的购物车里面的产品数量,如果一个产品有10个,那么数量就是10.不安商品种类算,按数量算,这是电商的通用做法
@RequestMapping("get_cart_product_count.do")
@ResponseBody
public ServerResponse getCartProductCount(HttpSession session) {
    User user = (User) session.getAttribute(Const.CURRENT_USER);
    if (user == null) {
        return ServerResponse.createBySuccess(0);
    }
    return iCartService.getCartProductCount(user.getId());
}

ICartService.java

ServerResponse list (Integer userId);

ServerResponse selectOrUnSelect (Integer userId,Integer productId,Integer checked);

ServerResponse getCartProductCount(Integer userId);

CartServiceImpl.java

public ServerResponse list (Integer userId){
    CartVo cartVo = this.getCartVoLimit(userId);
    return ServerResponse.createBySuccess(cartVo);
}

public ServerResponse selectOrUnSelect (Integer userId,Integer productId,Integer checked){
    cartMapper.checkedOrUncheckedProduct(userId,productId,checked);
    return this.list(userId);
}

public ServerResponse getCartProductCount(Integer userId){
    if(userId == null){
        return ServerResponse.createBySuccess(0);
    }
    return ServerResponse.createBySuccess(cartMapper.selectCartProductCount(userId));
}

CartMapper.java

int checkedOrUncheckedProduct(@Param("userId") Integer userId,@Param("productId")Integer productId,@Param("checked") Integer checked);

int selectCartProductCount(@Param("userId") Integer userId);

CartMapper.xml


  UPDATE  mmall_cart
  set checked = #{checked},
  update_time = now()
  where user_id = #{userId}
  
    and product_id = #{productId}
  



本站所有文章均来自互联网,如有侵权,请联系站长删除。极客文库 » 从零到企业级SSM电商项目实战教程(三十四)全选,全反选,单选,单反选,查询购物车商品数量功能开发
分享到:
赞(0)

评论抢沙发

评论前必须登录!