• 近期将进行后台系统升级,如有访问不畅,请稍后再试!
  • 极客文库-知识库上线!
  • 极客文库小编@勤劳的小蚂蚁,为您推荐每日资讯,欢迎关注!
  • 每日更新优质编程文章!
  • 更多功能模块开发中。。。

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

文章目录[隐藏]

CartController.java

//全选
@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(), ResponseCode.NEED_LOGIN.getDesc());
    }
    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(), ResponseCode.NEED_LOGIN.getDesc());
    }
    return iCartService.selectOrUnSelect(user.getId(), null, 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(), ResponseCode.NEED_LOGIN.getDesc());
    }
    return iCartService.selectOrUnSelect(user.getId(), productId, Const.Cart.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(), 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<Integer> 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<CartVo> list (Integer userId);

ServerResponse<CartVo> selectOrUnSelect (Integer userId,Integer productId,Integer checked);

ServerResponse<Integer> getCartProductCount(Integer userId);

CartServiceImpl.java

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));
}

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 id="checkedOrUncheckedProduct" parameterType="map">
  UPDATE  mmall_cart
  set checked = #{checked},
  update_time = now()
  where user_id = #{userId}
  <if test="productId != null">
    and product_id = #{productId}
  </if>
</update>

<select id="selectCartProductCount" parameterType="int" resultType="int">
  select IFNULL(sum(quantity),0) as count from mmall_cart where user_id = #{userId}
</select>

喜欢 (0)
[247507792@qq.com]
分享 (0)
Geekerstar
关于作者:
本站技术支持

欢迎 注册账号 登录 发表评论!

  • 精品技术教程
  • 编程资源分享
  • 问答交流社区
  • 极客文库知识库

客服QQ


QQ:2248886839


工作时间:09:00-23:00