21、【购物车模块】——全选、全反选、单选、单反选、查询购物车商品数量功能开发

  1. 云栖社区>
  2. 博客>
  3. 正文

21、【购物车模块】——全选、全反选、单选、单反选、查询购物车商品数量功能开发

一只叉叉 2018-10-12 16:07:00 浏览1096
展开阅读全文

1、接口开发:

1、全选接口开发:

*Controller:

 //全选
    @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.selectOrUnselectAll(user.getId(),Const.Cart.CHECKED,null);
    }

*Service:

    //购物车选或不选
    ServerResponse<CartVo> selectOrUnselectAll(Integer userId,Integer checked,Integer productId);

*ServiceImpl:

    //购物车全选或全不选
    public ServerResponse<CartVo> selectOrUnselectAll(Integer userId,Integer checked,Integer productId){
        cartMapper.checkedOrUncheckedProduct(userId,checked,productId);
        return this.list(userId);
    }

checkedOrUncheckedProduct方法:
*Mapper:

 //全选购物车中的商品或者不全选购物车中的商品
    int checkedOrUncheckedProduct(@Param("userId") Integer userId,@Param("checked") Integer checked,@Param("productId") Integer productId);

*Mappler.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>

上面Impl方法中的list方法在上一篇文章中:20、【购物车模块】——更新、删除、查询购物车功能开发获取购物车信息列表的时候又讲~

2、全反选接口开发:

*Controller:

    //全反选
    @RequestMapping("un_select_all.do")
    @ResponseBody
    public ServerResponse<CartVo> unSelectAll(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.selectOrUnselectAll(user.getId(),Const.Cart.UN_CHECKED,null);
    }

*Service:

    //购物车选或不选
    ServerResponse<CartVo> selectOrUnselectAll(Integer userId,Integer checked,Integer productId);

*ServiceImpl:

    //购物车全选或全不选
    public ServerResponse<CartVo> selectOrUnselectAll(Integer userId,Integer checked,Integer productId){
        cartMapper.checkedOrUncheckedProduct(userId,checked,productId);
        return this.list(userId);
    }

checkedOrUncheckedProduct方法:
*Mapper:

 //全选购物车中的商品或者不全选购物车中的商品
    int checkedOrUncheckedProduct(@Param("userId") Integer userId,@Param("checked") Integer checked,@Param("productId") Integer productId);

*Mappler.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>

其实全选不全反选接口是差不多的,只是在现在的是否选中的状态进行了判断~

3、单选接口开发:

*Controller:

   //单独选
    @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.selectOrUnselectAll(user.getId(),Const.Cart.CHECKED,productId);
    }
4、单反选接口开发:
    //单独反选
    @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.selectOrUnselectAll(user.getId(),Const.Cart.UN_CHECKED,productId);
    }

其中单选,单反选同意也是用了上面selectOrUnselectAll方法,只是传递的参数不同~

5、查询购物车数量接口开发:

*Controller:


    //查询当前用户购物车的数量,如果一个产品数量有十个,我们显示是1个
    @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());
    }

*Service:

   //获取用户购物车中商品数量
    ServerResponse<Integer> getCartProductCount(Integer userId);

*ServiceImpl:

    //获取用户购物车中商品数量
    public  ServerResponse<Integer> getCartProductCount(Integer userId){
        if(userId == null){
            return ServerResponse.createBySuccess(0);
        }
        return ServerResponse.createBySuccess(cartMapper.selectProductCountByUserId(userId));
    }

*Mapper:

    //根据用户Id查询购物车中商品的数量
    int selectProductCountByUserId(Integer userId);

*Mappler.xml:


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

    </select>

2、接口测试:

1、全选接口测试:
img_6f2829e649b3f2e01a5476a6d6211d98.png
image.png
2、全反选接口测试:
img_c0026f843abd5c8263308e95e737a4a3.png
image.png
3、单选接口测试:
img_5e4b4fb6a8efeb243e83636e9d9d7390.png
image.png
4、单反选接口测试:
img_1689ad9ce7399c1bc4b002e658cc439c.png
image.png
5、获取购物车数量接口测试:
img_15c51e58c615dcc880d16fb3dc821cb2.png
image.png

网友评论

登录后评论
0/500
评论
一只叉叉
+ 关注