日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

20、【购物车模块】——更新、删除、查询购物车功能开发

發布時間:2024/4/17 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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:

//刪除購物車中的商品int deleteByUserIdProductIds(@Param("userId") Integer userId,@Param("productIdList")List<String> productIdList);

*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:

//根據用戶Id查詢商品是否被選中int selectCartProductCheckedStatusByUserId(Integer userId);

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.png
2、更新商品數量到購物車接口測試:
image.png
3、移除購物車某個產品接口測試:我們可以傳入商品信息id單個或多個進行刪除~
image.png

總結

以上是生活随笔為你收集整理的20、【购物车模块】——更新、删除、查询购物车功能开发的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。