✅P238_商城业务-购物车-VO编写
2024/2/14大约 2 分钟
购物车整体与购物项
购物车整体 = 购物项组合

购物项VO编写
不用@Data,自己生成getter和setter方法,主要为了数量、金额等属性自定义计算方法。
cfmall-cart/src/main/java/com/gyz/cfmall/vo/CartItemVo.java
package com.gyz.cfmall.vo;
import java.math.BigDecimal;
import java.util.List;
public class CartItemVo {
    private Long skuId;
    /**
     * 是否被选中
     */
    private Boolean check = true;
    /**
     * 标题
     */
    private String title;
    /**
     * 默认图片
     */
    private String image;
    /**
     * 商品套餐属性
     */
    private List<String> skuAttrValues;
    /**
     * 商品单价
     */
    private BigDecimal price;
    /**
     * 商品数量
     */
    private Integer count;
    /**
     * 总价
     */
    private BigDecimal totalPrice;
    public Long getSkuId() {
        return skuId;
    }
    public void setSkuId(Long skuId) {
        this.skuId = skuId;
    }
    public Boolean getCheck() {
        return check;
    }
    public void setCheck(Boolean check) {
        this.check = check;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getImage() {
        return image;
    }
    public void setImage(String image) {
        this.image = image;
    }
    public List<String> getSkuAttrValues() {
        return skuAttrValues;
    }
    public void setSkuAttrValues(List<String> skuAttrValues) {
        this.skuAttrValues = skuAttrValues;
    }
    public BigDecimal getPrice() {
        return price;
    }
    public void setPrice(BigDecimal price) {
        this.price = price;
    }
    public Integer getCount() {
        return count;
    }
    public void setCount(Integer count) {
        this.count = count;
    }
    /**
     * 计算当前购物项总价
     *
     * @return
     */
    public BigDecimal getTotalPrice() {
        return this.price.multiply(new BigDecimal("" + this.count));
    }
    public void setTotalPrice(BigDecimal totalPrice) {
        this.totalPrice = totalPrice;
    }
}整体购物车VO编写
CartVo不用@Data,需要计算的属性,必须重写它的get方法,保证每次获取属性都会进行计算
- 计算商品的总数量
- 计算商品类型数量
- 计算商品的总价
cfmall-cart/src/main/java/com/gyz/cfmall/vo/CartVo.java
package com.gyz.cfmall.vo;
import org.springframework.util.CollectionUtils;
import java.math.BigDecimal;
import java.util.List;
public class CartVo {
    /**
     * 购物车子项信息
     */
    List<CartItemVo> items;
    /**
     * 商品数量
     */
    private Integer countNum;
    /**
     * 商品类型数量
     */
    private Integer countType;
    /**
     * 商品总价
     */
    private BigDecimal totalAmount;
    /**
     * 减免价格
     */
    private BigDecimal reduce = new BigDecimal("0.00");
    public List<CartItemVo> getItems() {
        return items;
    }
    public void setItems(List<CartItemVo> items) {
        this.items = items;
    }
    public Integer getCountNum() {
        int count = 0;
        if (items != null && items.size() > 0) {
            for (CartItemVo item : items) {
                count += item.getCount();
            }
        }
        return count;
    }
    public Integer getCountType() {
        int count = 0;
        if (items != null && items.size() > 0) {
            for (CartItemVo item : items) {
                count += 1;
            }
        }
        return count;
    }
    public BigDecimal getTotalAmount() {
        BigDecimal amount = new BigDecimal("0");
        // 计算购物项总价
        if (!CollectionUtils.isEmpty(items)) {
            for (CartItemVo cartItem : items) {
                if (cartItem.getCheck()) {
                    amount = amount.add(cartItem.getTotalPrice());
                }
            }
        }
        // 计算优惠后的价格
        return amount.subtract(getReduce());
    }
    public BigDecimal getReduce() {
        return reduce;
    }
    public void setReduce(BigDecimal reduce) {
        this.reduce = reduce;
    }
}