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

简介: 1、接口开发:1、全选接口开发:*Controller: //全选 @RequestMapping("select_all.do") @ResponseBody public ServerResponse selectAl...

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
相关文章
|
6月前
|
开发者
【 uniapp - 黑马优购 | 购物车页面(1)】如何创建购物车编译模式、 商品列表区域实现
【 uniapp - 黑马优购 | 购物车页面(1)】如何创建购物车编译模式、 商品列表区域实现
115 0
|
8月前
|
SQL 前端开发 测试技术
显示勾选的购物车数据【项目 商城】
显示勾选的购物车数据【项目 商城】
42 0
|
8月前
|
SQL 存储 前端开发
显示购物车列表【项目 商城】
显示购物车列表【项目 商城】
34 0
|
8月前
基础购物车
基础购物车
28 0
|
12月前
uniapp——添加购物车数据以及删除购物车数据
添加购物车数据以及删除购物车数据
122 0
|
JavaScript
jquery购物车增减商品数量2-修改商品小计
jquery购物车增减商品数量2-修改商品小计
93 0
jquery购物车增减商品数量2-修改商品小计
购物车增减商品数量2-修改商品小计parents34
购物车增减商品数量2-修改商品小计parents34
73 0
购物车增减商品数量2-修改商品小计35
购物车增减商品数量2-修改商品小计35
82 0
购物车增减商品数量2-修改商品小计35
购物车增减商品数量2-修改商品小计3-35
购物车增减商品数量2-修改商品小计3-35
73 0
购物车增减商品数量2-修改商品小计3-35