1. 程式人生 > >電商平臺搭建--購物車功能模組開發(二)

電商平臺搭建--購物車功能模組開發(二)

Hi,大家好。在上一篇博文中,我們完成了搭建一個高複用的購物車時需要準備的搭建環境,封裝了一個高複用的購物車計算方法,定義了兩個與購物車商品有關的Value-Object值物件,那麼接下來就進入核心功能的開發。

一、購物車模組-獲取購物車商品列表功能的實現

      獲取購車商品列表,其實就是把當前使用者的購物車中的商品資訊,包括商品縮圖、商品數量、商品價格、購買數量、商品單價、商品總價都顯示出來方便使用者檢視。

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

    將泛型指定為前面提到的CartVo型別,表示該方法會返回CartVo中的欄位資訊。傳遞一個userId,表示通過userId來查詢屬於當前使用者的商品資訊,再呼叫前面封裝好的購物車通用方法getCartVoLimit來對購物車的整體邏輯進行重新計算。

    @RequestMapping(value = "list.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<CartVo> list(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.list(user.getId());
    }

    在Controller層,判斷使用者是否登入,如果沒登入則強制使用者登入,否則返回list方法即可。

二、購物車模組-向購物車中新增商品功能的實現

public ServerResponse<CartVo> add(Integer userId, Integer productId, Integer count){
        if(productId == null || count == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGALARGUMENT.getCode(), ResponseCode.ILLEGALARGUMENT.getDesc());
        }
        Cart cart = cartMapper.selectCartByUserIdProductId(userId, productId);
        if(cart == null){
            Cart cartItem = new Cart();
            cartItem.setQuantity(count);
            cartItem.setChecked(Const.Cart.CHECKED);
            cartItem.setUserId(userId);
            cartItem.setProductId(productId);
            cartMapper.insert(cartItem);
        }
        if(cart != null){
            count = cart.getQuantity() + count;
            cart.setQuantity(count);
            cartMapper.updateByPrimaryKeySelective(cart);
        }
        return this.list(userId);
    }

   向購物車中新增商品,其實就是把商品的id和數量新增到當前使用者中(通過userId進行區分)。如果前臺傳遞過來的productId或者商品數量count都為null,那表示引數錯誤,無法進行新增。如果兩者中其中一個不為null,則表示前臺想向購物車中新增商品,此時需要在資料庫中查詢當前使用者下的商品資訊,包括商品的id、userId、product_id、quantity、checked、create_time、uptate_time。如果查詢出來的結果為null,表示在當前使用者下沒有商品資訊,所以應該把當前使用者選擇的商品資訊插入到資料庫中,如果查詢出來的結果不為null,表示在資料庫中存在商品資訊,此時表示使用者已經向購物車中新增過該商品,只是修改商品數量,所以對應的邏輯操作就是把當前使用者操作的商品數量cart.getQuantity()和已經存在的購物車數量進行累計計算(count)並更新最終的商品數量,然後執行更新資料操作。最後,因為當前使用者的商品資訊已經發生改變,會直接影響購物車中商品的顯示結果,所以還需要再呼叫一下list方法,重新對購物車中的商品進行排列。

    接著是Controller層

    @RequestMapping(value = "add.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<CartVo> add(Integer count, Integer productId, 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.add(user.getId(), productId, count);
    }

    add方法中的userId是登入使用者的userId別搞混淆了。

三、購物車模組-更新購物車商品數量功能的實現

      因為購物車中商品數量是要單獨顯示的,所以把這個功能單獨提出來做成一個介面,供前臺呼叫顯示。

public ServerResponse<CartVo> update(Integer userId,Integer productId,Integer count){
        if(productId == null || count == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGALARGUMENT.getCode(),ResponseCode.ILLEGALARGUMENT.getDesc());
        }
        Cart cart = cartMapper.selectCartByUserIdProductId(userId,productId);
        if(cart != null){
            cart.setQuantity(count);
        }
        cartMapper.updateByPrimaryKey(cart);
        return this.list(userId);
    }

    和新增商品功能的處理方式相同,不再贅述。

    @RequestMapping(value = "update.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<CartVo> update(HttpSession session, Integer count, 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.update(user.getId(),productId,count);
    }

四、購物車模組-刪除購物車中的商品功能的實現

public ServerResponse<CartVo> deleteProduct(Integer userId,String productIds){
        List<String> productList = Splitter.on(",").splitToList(productIds);
        if(org.apache.commons.collections.CollectionUtils.isEmpty(productList)){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGALARGUMENT.getCode(),ResponseCode.ILLEGALARGUMENT.getDesc());
        }
        cartMapper.deleteByUserIdProductIds(userId,productList);
        return this.list(userId);
    }

    刪除購物車中的商品支援單個刪除和批量刪除,傳入productIds在其為一個時表示刪除單個,如果大於等於兩個表示刪除多個,根據語法規範,應該在多引數傳遞時在其中間使用逗號進行引數分割,這樣MyBatis是能夠識別的。如果獲取到的productIds為空,則表示引數錯誤,沒有傳遞將要刪除的商品資訊,否則直接使用deleteByUserIdProductIds刪除更新資料庫即可。

    @RequestMapping(value = "delete_product.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<CartVo> deleteProduct(HttpSession session,String productIds){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user ==null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iCartService.deleteProduct(user.getId(),productIds);
    }

五、購物車模組-獲取購物車中商品數量功能的實現

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

    獲取購物車中商品數量仍然需要使用者登入,否則預設設定為0。

    @RequestMapping(value = "get_cart_product_count.do", method = RequestMethod.POST)
    @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());
    }
寫到這裡,本篇博文已經結束了。在本篇博文中,我們實現了購物車的五大核心功能。在下一篇博文中,我們將完成購物車剩下的功能點,下期再見!