购物车管理模块

来源:互联网 发布:域名如何做泛解析 编辑:程序博客网 时间:2024/06/05 07:41

加入购物车功能

查找用户ID和产品ID号,如果还没加购物车,则设为选中状态,并新增一个,已经加入了就使购买数量加1,同时对库存和购买数量作联动校验,最后记得更新到数据库中。

@Autowired    private CartMapper cartMapper;    @Autowired    private ProductMapper productMapper;    //投入一个商品到购物车    public ServerResponse<CartVo> add(Integer userId,Integer count,Integer productId){        if(productId==null || count==null){            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getDesc());        }        Cart cart = cartMapper.selectCartByUserIdProductId(userId, productId);        if(cart==null){            //购物车没有当前商品 需要新增一个            Cart cartNew=new Cart();            cartNew.setChecked(Const.Cart.CHECKD); //购物车选中状态            cartNew.setProductId(productId);            cartNew.setQuantity(count);            cartNew.setUserId(userId);            cartMapper.insert(cartNew);        }else{ //产品已经存在购物车了 如果已经存在 则数量增加            cart.setQuantity(cart.getQuantity()+count);            cartMapper.updateByPrimaryKeySelective(cart); //还需要库存和数量的联动校验、判断        }        CartVo cartVo=this.getCartVoLimit(userId);        return ServerResponse.createBySuccess(cartVo);    }

这里封装了一个BigDecimal类,用于计算加减乘除的运算,相比原生的类,这个可以保证浮点操作的精度最佳,

//获取用户的一个购物车列表 并对库存和购买数量做联动校验    public CartVo getCartVoLimit(int userId){        CartVo cartVo=new CartVo();        List<CartProductVo> cartProductVoList= Lists.newArrayList();        List<Cart> cartList = cartMapper.selectCartListByUserId(userId); //查找购物车里所有商品        BigDecimal cartTotalPrice=new BigDecimal("0"); //购物车选中的商品总价        if(CollectionUtils.isNotEmpty(cartList)){            for(Cart cartItem:cartList){                CartProductVo cartProductVo=new CartProductVo(); //购物车商品                cartProductVo.setUserId(userId);                cartProductVo.setId(cartItem.getId());                cartProductVo.setProductId(cartItem.getProductId());                //查询购物车里面的产品                Product product = productMapper.selectByPrimaryKey(cartItem.getProductId());                if(product!=null){ //如果产品不为空 则继续组装ProductVo                    cartProductVo.setProductMainImage(product.getMainImage());                    cartProductVo.setProductSubtitle(product.getSubtitle());                    cartProductVo.setProductName(product.getName());                    cartProductVo.setProductPrice(product.getPrice());                    cartProductVo.setProductStock(product.getStock());                    //判断库存                    int buyLimitCount=0;                    if(product.getStock()>=cartItem.getQuantity()){ //库存充足                        cartProductVo.setLimitQuatity(Const.Cart.LIMIT_NUM_SUCCESS);                        buyLimitCount=cartItem.getQuantity(); //购买数量不设限 买多少就是多少                    }else{                        cartProductVo.setLimitQuatity(Const.Cart.LIMIT_NUM_FAIL);                        buyLimitCount=cartProductVo.getProductStock(); //库存不足时 购买数量限制为库存总量                        //库存不足时 更新购物车中产品数量                        Cart cartForQuatity=new Cart();                        cartForQuatity.setId(cartItem.getId());                        cartForQuatity.setQuantity(buyLimitCount);                        cartMapper.updateByPrimaryKeySelective(cartForQuatity);                    }                    cartProductVo.setQuantity(buyLimitCount);                    //计算总价                    cartProductVo.setProductTotalPrice(BigDecimalUtil.mul(cartProductVo.getQuantity(),product.getPrice().doubleValue()));                    cartProductVo.setProductChecked(cartItem.getChecked());            }                if(cartItem.getChecked()==Const.Cart.CHECKD){                    //如果已经勾选 累加到整个的购物车总价中                    cartTotalPrice=BigDecimalUtil.add(cartTotalPrice.doubleValue(),cartProductVo.getProductTotalPrice().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;    }

更新、删除购物车商品功能

数量的操作都必须对库存进行检查,并注意更新库存。

    public ServerResponse<CartVo> update(Integer userId,Integer count,Integer productId){        if(productId==null || count==null){            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getDesc());        }        Cart cart = cartMapper.selectCartByUserIdProductId(userId, productId);        if(cart!=null){            cart.setQuantity(count);        }        cartMapper.updateByPrimaryKeySelective(cart); //更改数据后必须立即更新数据库        CartVo cartVoLimit = getCartVoLimit(userId);//判断库存是否足够        return ServerResponse.createBySuccess(cartVoLimit);    }    public ServerResponse<CartVo> deleteProduct(Integer userId,String productIds){        List<String> productIdList = Splitter.on(",").splitToList(productIds);        if(CollectionUtils.isEmpty(productIdList)){            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getDesc());        }        cartMapper.deleteByUserIdProductIds(userId, productIdList); //删除数据库数据才是真正删除        CartVo cartVoLimit = getCartVoLimit(userId);//判断库存是否足够        return ServerResponse.createBySuccess(cartVoLimit);    }

Mybatis CartMapper.xml

  <delete id="deleteByUserIdProductIds" parameterType="map">    DELETE FROM mmall_cart    WHERE user_id=#{userId}    <if test="productIdList!=null">      AND product_id IN      <foreach collection="productIdList" index="index" item="item" open="(" separator="," close=")">        #{item}      </foreach>    </if>  </delete>

全选、全反选、单选、多选和查询购物车商品功能

    //全选    //全反选    //单独选 通过productId判断选中产品    //单独反选    public ServerResponse<CartVo> selectOrUnSelect(Integer userId,Integer checked,Integer productId){        cartMapper.CheckedOrUncheckedByUserId(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));    }

Mybatis CartMapper.xml

  <update id="CheckedOrUncheckedByUserId" 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">    /*注意!如果user_id的查询结果不存在  sum()就会返回NULL 要么返回值改为Integer 由service层处理 要么直接mybatis报错 这里用内置函数转换*/    select NULLIF(sum(quantity),0) as count from mmall_cart where user_id=#{userId}  </select>
原创粉丝点击