20、【购物车模块】——更新、删除、查询购物车功能开发
生活随笔
收集整理的這篇文章主要介紹了
20、【购物车模块】——更新、删除、查询购物车功能开发
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
更新購物車,即修改購物車中的每個商品的參數,
1、接口編寫:
1、更新購物車:
*Controller:
// 更新商品到購物車@RequestMapping("update.do")@ResponseBodypublic 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);}*Service:
//更新商品到購物車ServerResponse<CartVo> update(Integer userId,Integer productId,Integer count);*ServiceImpl:
// 更新商品到購物車public ServerResponse<CartVo> update(Integer userId, Integer productId, Integer count) {if (productId == null || count == null) {return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());}Cart cart = cartMapper.selectCatByUserIdProductId(userId, productId);if (cart != null) {cart.setQuantity(count);}cartMapper.updateByPrimaryKeySelective(cart);return this.list(userId);}selectCatByUserIdProductId方法:
*Mapper:
//根據用戶Id和產品Id去查購物車Cart selectCatByUserIdProductId(@Param("userId") Integer userId, @Param("productId") Integer productId);*Mappler.xml:
<!--根據用戶Id和產品Id來查詢購物車--><select id="selectCatByUserIdProductId" resultMap="BaseResultMap" parameterType="map">select<include refid="Base_Column_List"/>from mmall_cartwhere user_id=#{userId}and product_id=#{productId}</select>2、刪除商品:
*Controller:
// 移除購物車某個產品@RequestMapping("delete_product.do")@ResponseBodypublic 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);}*Service:
//刪除購物車中的商品ServerResponse<CartVo> deleteProduct(Integer userId,String productIds);*ServiceImpl:
//刪除購物車中的商品public ServerResponse<CartVo> deleteProduct(Integer userId, String productIds) {List<String> productList = Splitter.on(",").splitToList(productIds);if (CollectionUtils.isEmpty(productList)) {return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());}cartMapper.deleteByUserIdProductIds(userId, productList);return this.list(userId);}deleteByUserIdProductIds方法:
*Mapper:
*Mappler.xml:
<delete id="deleteByUserIdProductIds" parameterType="map">delete from mmall_cartwhere user_id = #{userId}<if test="productIdList != null">and product_id in<foreach collection="productIdList" item="item" index="index" open="(" separator="," close=")">#{item}</foreach></if></delete>其中上面的list是我們封裝的一個返回購物車列表信息的接口,,
*Controller:
//查詢購物車中商品@RequestMapping("list.do")@ResponseBodypublic 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());}*Service:
//查詢購物車中商品ServerResponse<CartVo> list(Integer userId);*ServiceImpl:
//查詢購物車中商品public ServerResponse<CartVo> list(Integer userId) {CartVo cartVo = this.getCartVoLimit(userId);return ServerResponse.createBySuccess(cartVo);}重點看一下getCartVoLimit這個方法:
//封裝的一個根據用戶Id來獲取購物車信息的方法private CartVo getCartVoLimit(Integer userId) {CartVo cartVo = new CartVo();List<Cart> cartList = cartMapper.selectCartByUserId(userId);List<CartProductVo> cartProductVoList = Lists.newArrayList();//設置購物車初始總價BigDecimal cartTotalPrice = new BigDecimal("0.0");if (CollectionUtils.isNotEmpty(cartList)) {for (Cart cartItem : cartList) {CartProductVo cartProductVo = new CartProductVo();cartProductVo.setId(cartItem.getId());cartProductVo.setUserId(userId);cartProductVo.setProductId(cartItem.getProductId());Product product = productMapper.selectByPrimaryKey(cartItem.getProductId());if (product != null) {cartProductVo.setProductMainImage(product.getMainImage());cartProductVo.setProductName(product.getName());cartProductVo.setProductSubtitle(product.getSubtitle());cartProductVo.setProductStatus(product.getStatus());cartProductVo.setProductPrice(product.getPrice());cartProductVo.setProductStock(product.getStock());//判斷庫存int buyLimitCount =0;if (product.getStock()>=cartItem.getQuantity()) {//庫存充足的時候buyLimitCount = cartItem.getQuantity();cartProductVo.setLimitQuantity(Const.Cart.LIMIT_MUM_SUCCESS);} else {//庫存的不足的時候buyLimitCount = product.getStock();cartProductVo.setLimitQuantity(Const.Cart.LIMIT_MUM_FAIL);//購物車中更新有效庫存Cart cartForQuantity = new Cart();cartForQuantity.setId(cartItem.getId());cartForQuantity.setQuantity(buyLimitCount);cartMapper.updateByPrimaryKeySelective(cartForQuantity);}cartProductVo.setQuantity(buyLimitCount);//計算總價cartProductVo.setProductTotalPrice(BigDecimalUtil.mul(product.getPrice().doubleValue(), cartProductVo.getQuantity()));cartProductVo.setProductChecked(cartItem.getChecked());}if (cartItem.getChecked() == Const.Cart.CHECKED) {//如果已經勾選,增加到整個購物車總價中if(cartProductVo.getProductTotalPrice() == null){}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;}上面封裝的getAllCheckedStatus方法:
//封裝的購物車中商品的是否選中狀態private boolean getAllCheckedStatus(Integer userId) {if (userId == null) {return false;}return cartMapper.selectCartProductCheckedStatusByUserId(userId) == 0;}selectCartProductCheckedStatusByUserId:
cartMapper:
cartMapper.xml:
<select id="selectCartProductCheckedStatusByUserId" resultType="int" parameterType="int" >select count(1) from mmall_cart where checked = 0 and user_id = #{userId}</select>上面方法中使用的CartVo:
package com.mmall.vo;import java.math.BigDecimal; import java.util.List;public class CartVo {private List<CartProductVo> cartProductVoList;private BigDecimal cartTotalPrice;private Boolean allChecked; //是否都勾選private String imageHost;public List<CartProductVo> getCartProductVoList() {return cartProductVoList;}public void setCartProductVoList(List<CartProductVo> cartProductVoList) {this.cartProductVoList = cartProductVoList;}public BigDecimal getCartTotalPrice() {return cartTotalPrice;}public void setCartTotalPrice(BigDecimal cartTotalPrice) {this.cartTotalPrice = cartTotalPrice;}public Boolean getAllChecked() {return allChecked;}public void setAllChecked(Boolean allChecked) {this.allChecked = allChecked;}public String getImageHost() {return imageHost;}public void setImageHost(String imageHost) {this.imageHost = imageHost;} }ProductVo:
package com.mmall.vo;import java.math.BigDecimal;public class CartProductVo {//結合了產品和購物車的一個抽象對象private Integer Id;private Integer userId;private Integer productId;private Integer quantity;//購物車中該商品的數量private String productName;private String productSubtitle;private String productMainImage;private BigDecimal productPrice;private Integer productStatus;private BigDecimal productTotalPrice;private Integer productStock;private Integer productChecked; //該產品是否勾選private String limitQuantity; //限制數量的一個返回結果public Integer getId() {return Id;}public void setId(Integer id) {Id = id;}public Integer getUserId() {return userId;}public void setUserId(Integer userId) {this.userId = userId;}public Integer getProductId() {return productId;}public void setProductId(Integer productId) {this.productId = productId;}public Integer getQuantity() {return quantity;}public void setQuantity(Integer quantity) {this.quantity = quantity;}public String getProductName() {return productName;}public void setProductName(String productName) {this.productName = productName;}public String getProductSubtitle() {return productSubtitle;}public void setProductSubtitle(String productSubtitle) {this.productSubtitle = productSubtitle;}public String getProductMainImage() {return productMainImage;}public void setProductMainImage(String productMainImage) {this.productMainImage = productMainImage;}public BigDecimal getProductPrice() {return productPrice;}public void setProductPrice(BigDecimal productPrice) {this.productPrice = productPrice;}public Integer getProductStatus() {return productStatus;}public void setProductStatus(Integer productStatus) {this.productStatus = productStatus;}public BigDecimal getProductTotalPrice() {return productTotalPrice;}public void setProductTotalPrice(BigDecimal productTotalPrice) {this.productTotalPrice = productTotalPrice;}public Integer getProductStock() {return productStock;}public void setProductStock(Integer productStock) {this.productStock = productStock;}public Integer getProductChecked() {return productChecked;}public void setProductChecked(Integer productChecked) {this.productChecked = productChecked;}public String getLimitQuantity() {return limitQuantity;}public void setLimitQuantity(String limitQuantity) {this.limitQuantity = limitQuantity;} }2、接口測試:
1、用戶購物車信息接口測試:
image.png2、更新商品數量到購物車接口測試:
image.png3、移除購物車某個產品接口測試:我們可以傳入商品信息id單個或多個進行刪除~
image.png總結
以上是生活随笔為你收集整理的20、【购物车模块】——更新、删除、查询购物车功能开发的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NIO通信,仿QQ基本聊天业务,交流篇
- 下一篇: 关于PChar(@string)的疑惑