更新購物車,即修改購物車中的每個商品的參數,
1、接口編寫:
1、更新購物車:
*Controller
:
// 更新商品到購物車
@RequestMapping("update.do")
@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);
}
*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_cart
where user_id=#{userId}
and product_id=#{productId}
</select>
2、删除商品:
*Controller
// 移除購物車某個産品
@RequestMapping("delete_product.do")
@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);
}
*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_cart
where 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")
@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());
}
*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