Browse Source

购物车操作

chao 3 years ago
parent
commit
f0978c4154

+ 125 - 0
src/main/java/com/caimei365/order/controller/CartClubApi.java

@@ -0,0 +1,125 @@
+package com.caimei365.order.controller;
+
+import com.caimei365.order.model.ResponseJson;
+import com.caimei365.order.model.dto.CartDto;
+import com.caimei365.order.service.CartService;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiImplicitParam;
+import io.swagger.annotations.ApiOperation;
+import lombok.RequiredArgsConstructor;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.Map;
+
+/**
+ * 机构购物车API
+ *
+ * @author : Charles
+ * @date : 2021/6/25
+ */
+@Api(tags="机构购物车API")
+@RestController
+@RequiredArgsConstructor
+@RequestMapping("/order")
+public class CartClubApi {
+
+    private final CartService cartService;
+
+    /**
+     * 购物车列表详细数据
+     */
+    @ApiOperation("购物车列表详细数据(旧:/shoppingCart/list)")
+    @ApiImplicitParam(required = true, name = "userId", value = "用户Id")
+    @GetMapping("/cart/list")
+    public ResponseJson<Map<String,Object>> getShoppingCartList(Integer userId) {
+        if (null == userId) {
+            return ResponseJson.error("用户Id不能为空!", null);
+        }
+        return cartService.getShoppingCartList(userId);
+    }
+
+    /**
+     * 网站顶部购物车数据
+     */
+    @ApiOperation("网站顶部购物车数据(旧:/shoppingCart/header/cart)")
+    @ApiImplicitParam(required = true, name = "userId", value = "用户Id")
+    @GetMapping("/cart/head")
+    public ResponseJson<Map<String, Object>> getShoppingCartHead(Integer userId) {
+        if (null == userId) {
+            return ResponseJson.error("用户Id不能为空!", null);
+        }
+        return cartService.getShoppingCartHead(userId);
+    }
+
+    /**
+     * 添加购物车
+     *
+     * @param cartDto {
+     *                userId       用户ID
+     *                productId    商品id
+     *                productCount 商品数量
+     * }
+     */
+    @ApiOperation("添加购物车(旧:/shoppingCart/addCart)")
+    @PostMapping("/cart/add")
+    public ResponseJson<Integer> addShoppingCart(CartDto cartDto){
+        if (null == cartDto.getUserId()) {
+            return ResponseJson.error("用户Id不能为空!", null);
+        }
+        if (null == cartDto.getProductId()) {
+            return ResponseJson.error("商品Id不能为空!", null);
+        }
+        if (null == cartDto.getProductCount()) {
+            return ResponseJson.error("商品数量不能为空!", null);
+        }
+        return cartService.addShoppingCart(cartDto);
+    }
+
+    /**
+     * 更新购物车
+     *
+     * @param cartDto {
+     *                userId       用户ID
+     *                productId    商品id
+     *                productCount 商品数量
+     * }
+     */
+    @ApiOperation("更新购物车(旧:/shoppingCart/update)")
+    @PostMapping("/cart/update")
+    public ResponseJson<Integer> updateShoppingCart(CartDto cartDto){
+        if (null == cartDto.getUserId()) {
+            return ResponseJson.error("用户Id不能为空!", null);
+        }
+        if (null == cartDto.getProductId()) {
+            return ResponseJson.error("商品Id不能为空!", null);
+        }
+        if (null == cartDto.getProductCount()) {
+            return ResponseJson.error("商品数量不能为空!", null);
+        }
+        return cartService.updateShoppingCart(cartDto);
+    }
+
+    /**
+     * 删除购物车
+     *
+     * @param cartDto {
+     *                userId       用户ID
+     *                productIds   商品ids,逗号隔开
+     * }
+     */
+    @ApiOperation("删除购物车(旧:/shoppingCart/delete)")
+    @PostMapping("/cart/delete")
+    public ResponseJson<Integer> deleteShoppingCart(CartDto cartDto){
+        if (null == cartDto.getUserId()) {
+            return ResponseJson.error("用户Id不能为空!", null);
+        }
+        if (StringUtils.isEmpty(cartDto.getProductIds())) {
+            return ResponseJson.error("商品Id集合不能为空!", null);
+        }
+        return cartService.deleteShoppingCart(cartDto);
+    }
+}

+ 20 - 0
src/main/java/com/caimei365/order/controller/CartSellerApi.java

@@ -0,0 +1,20 @@
+package com.caimei365.order.controller;
+
+import io.swagger.annotations.Api;
+import lombok.RequiredArgsConstructor;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * 协销购物车API
+ *
+ * @author : Charles
+ * @date : 2021/6/28
+ */
+@Api(tags="协销购物车API")
+@RestController
+@RequiredArgsConstructor
+@RequestMapping("/order")
+public class CartSellerApi {
+
+}

+ 0 - 42
src/main/java/com/caimei365/order/controller/ShoppingCartApi.java

@@ -1,42 +0,0 @@
-package com.caimei365.order.controller;
-
-import com.caimei365.order.model.ResponseJson;
-import com.caimei365.order.service.CartService;
-import io.swagger.annotations.Api;
-import io.swagger.annotations.ApiImplicitParam;
-import io.swagger.annotations.ApiOperation;
-import lombok.RequiredArgsConstructor;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
-
-import java.util.Map;
-
-/**
- * 购物车API
- *
- * @author : Charles
- * @date : 2021/6/25
- */
-@Api(tags="购物车API")
-@RestController
-@RequiredArgsConstructor
-@RequestMapping("/order/cart")
-public class ShoppingCartApi {
-
-    private final CartService cartService;
-
-    /**
-     * 购物车列表详细数据
-     */
-    @ApiOperation("购物车列表详细数据(旧:/shoppingCart/list)")
-    @ApiImplicitParam(required = true, name = "userId", value = "用户Id")
-    @GetMapping("/list")
-    public ResponseJson<Map<String,Object>> getShoppingCartList(Integer userId) {
-        if (null == userId) {
-            return ResponseJson.error("用户Id不能为空!", null);
-        }
-        return cartService.getShoppingCartList(userId);
-    }
-
-}

+ 30 - 1
src/main/java/com/caimei365/order/mapper/CartMapper.java

@@ -1,9 +1,12 @@
 package com.caimei365.order.mapper;
 
+import com.caimei365.order.model.dto.CartDto;
+import com.caimei365.order.model.po.CartPo;
 import com.caimei365.order.model.vo.CartItemVo;
 import com.caimei365.order.model.vo.CartShopVo;
 import com.caimei365.order.model.vo.CartPromotionsVo;
 import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
 
 import java.util.List;
 
@@ -25,7 +28,7 @@ public interface CartMapper {
      * @param shopId 供应商Id
      * @param userId 用户Id
      */
-    List<CartItemVo> getCartProductsByShopId(Integer shopId, Integer userId);
+    List<CartItemVo> getCartProductsByShopId(@Param("shopId") Integer shopId, @Param("userId") Integer userId);
     /**
      * 购物车供应商促销优惠活动
      * @param shopId 供应商Id
@@ -41,4 +44,30 @@ public interface CartMapper {
      * @param promotionsId 促销Id
      */
     List<CartItemVo> getPromotionGifts(Integer promotionsId);
+    /**
+     * 获取购物车商品列表(不区分供应商)
+     * @param userId 用户Id
+     */
+    List<CartItemVo> getCartProductList(Integer userId);
+    /**
+     * 获取购物车
+     * @param cartDto userId,productId
+     */
+    CartPo getCartPo(CartDto cartDto);
+    /**
+     * 更新购物车
+     * @param cart CartPo
+     */
+    void updateCart(CartPo cart);
+    /**
+     * 新增购物车
+     * @param cart CartPo
+     */
+    void insertCart(CartPo cart);
+    /**
+     * 删除购物车
+     * @param userId     用户Id
+     * @param productIds 商品Id集合
+     */
+    void deleteCartByProductIds(@Param("userId") Integer userId, @Param("productIds") List<Integer> productIds);
 }

+ 38 - 0
src/main/java/com/caimei365/order/model/dto/CartDto.java

@@ -0,0 +1,38 @@
+package com.caimei365.order.model.dto;
+
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+import java.io.Serializable;
+
+/**
+ * Description
+ *
+ * @author : Charles
+ * @date : 2021/6/28
+ */
+@Data
+public class CartDto implements Serializable {
+    private static final long serialVersionUID = 1L;
+    /**
+     * 用户id
+     */
+    @ApiModelProperty("用户id")
+    private Integer userId;
+    /**
+     * 商品id
+     */
+    @ApiModelProperty("商品id")
+    private Integer productId;
+    /**
+     * 商品数量
+     */
+    @ApiModelProperty("商品数量")
+    private Integer productCount;
+    /**
+     * 商品ids
+     */
+    @ApiModelProperty("商品ids,删除购物车用,逗号分隔")
+    private String productIds;
+
+}

+ 43 - 0
src/main/java/com/caimei365/order/model/po/CartPo.java

@@ -0,0 +1,43 @@
+package com.caimei365.order.model.po;
+
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+import java.io.Serializable;
+import java.util.Date;
+
+/**
+ * Description
+ *
+ * @author : Charles
+ * @date : 2021/6/28
+ */
+@Data
+public class CartPo implements Serializable {
+    private static final long serialVersionUID = 1L;
+    /**
+     * 购物车 cm_cartID
+     */
+    private Integer id;
+    /**
+     * 用户id
+     */
+    private Integer userId;
+    /**
+     * 商品id
+     */
+    private Integer productId;
+    /**
+     * 商品数量
+     */
+    private Integer productCount;
+    /**
+     * 添加时间
+     */
+    private Date addTime;
+    /**
+     * 是否再次购买: 1再次购买   0不是再次购买
+     */
+    private Integer reBuyFlag;
+
+}

+ 39 - 0
src/main/java/com/caimei365/order/service/CartService.java

@@ -1,6 +1,7 @@
 package com.caimei365.order.service;
 
 import com.caimei365.order.model.ResponseJson;
+import com.caimei365.order.model.dto.CartDto;
 
 import java.util.Map;
 
@@ -17,4 +18,42 @@ public interface CartService {
      * @param userId 用户Id
      */
     ResponseJson<Map<String, Object>> getShoppingCartList(Integer userId);
+
+    /**
+     * 网站顶部购物车数据
+     * @param userId 用户Id
+     */
+    ResponseJson<Map<String, Object>> getShoppingCartHead(Integer userId);
+
+    /**
+     * 添加购物车
+     *
+     * @param cartDto {
+     *                userId       用户ID
+     *                productId    商品id
+     *                productCount 商品数量
+     * }
+     */
+    ResponseJson<Integer> addShoppingCart(CartDto cartDto);
+
+    /**
+     * 更新购物车
+     *
+     * @param cartDto {
+     *                userId       用户ID
+     *                productId    商品id
+     *                productCount 商品数量
+     * }
+     */
+    ResponseJson<Integer> updateShoppingCart(CartDto cartDto);
+
+    /**
+     * 删除购物车
+     *
+     * @param cartDto {
+     *                userId       用户ID
+     *                productIds   商品ids,逗号隔开
+     * }
+     */
+    ResponseJson<Integer> deleteShoppingCart(CartDto cartDto);
 }

+ 479 - 232
src/main/java/com/caimei365/order/service/impl/CartServiceImpl.java

@@ -3,10 +3,13 @@ package com.caimei365.order.service.impl;
 import com.caimei365.order.mapper.BaseMapper;
 import com.caimei365.order.mapper.CartMapper;
 import com.caimei365.order.model.ResponseJson;
+import com.caimei365.order.model.dto.CartDto;
+import com.caimei365.order.model.po.CartPo;
 import com.caimei365.order.model.vo.*;
 import com.caimei365.order.service.CartService;
 import com.caimei365.order.utils.MathUtil;
 import com.caimei365.order.utils.ProductUtil;
+import com.google.common.collect.Lists;
 import com.google.common.util.concurrent.AtomicDouble;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Value;
@@ -68,223 +71,146 @@ public class CartServiceImpl implements CartService {
         List<Integer> promotionsIds = new ArrayList<>();
         // 用户身份
         Integer userIdentity = baseMapper.getIdentityByUserId(userId);
-        // 遍历供应商列表
-        shopInfoList.forEach(shop -> {
-            // 该供应商下商品种类
-            AtomicInteger shopKindCount = new AtomicInteger(0);
-            // 该供应商总价
-            AtomicDouble shopPrice = new AtomicDouble(0);
-            // 该供应商满减金额(供应商满减,单品满减)
-            AtomicDouble shopReducedPrice = new AtomicDouble(0);
-            // 供应商促销优惠活动
-            CartPromotionsVo shopPromotion = cartMapper.getPromotionByShopId(shop.getShopId());
-            // 供应商下商品列表
-            List<CartItemVo> productList = cartMapper.getCartProductsByShopId(shop.getShopId(), userId);
+        if (null != shopInfoList && shopInfoList.size()>0) {
+            // 遍历供应商列表
+            shopInfoList.forEach(shop -> {
+                // 该供应商下商品种类
+                AtomicInteger shopKindCount = new AtomicInteger(0);
+                // 该供应商总价
+                AtomicDouble shopPrice = new AtomicDouble(0);
+                // 该供应商满减金额(供应商满减,单品满减)
+                AtomicDouble shopReducedPrice = new AtomicDouble(0);
+                // 供应商促销优惠活动
+                CartPromotionsVo shopPromotion = cartMapper.getPromotionByShopId(shop.getShopId());
+                // 供应商下商品列表
+                List<CartItemVo> productList = cartMapper.getCartProductsByShopId(shop.getShopId(), userId);
 
-            // 迭代器设置商品信息
-            Iterator<CartItemVo> productIterator = productList.iterator();
-            while (productIterator.hasNext()) {
-                CartItemVo cartItemVo = productIterator.next();
-                // 图片路径
-                String image = ProductUtil.getImageUrl("product", cartItemVo.getImage(), domain);
-                cartItemVo.setImage(image);
-                // 是否添加税费,不含税商品 开票需添加税费
-                boolean taxFlag = "0".equals(cartItemVo.getIncludedTax()) && ("1".equals(cartItemVo.getInvoiceType()) || "2".equals(cartItemVo.getInvoiceType()));
-                BigDecimal cartItemTax = MathUtil.div(MathUtil.mul(cartItemVo.getPrice(), cartItemVo.getTaxRate()), 100, 2);
-                cartItemVo.setPrice(MathUtil.add(cartItemVo.getPrice(), cartItemTax).doubleValue());
-                // 已上架商品
-                if (cartItemVo.getValidFlag() == 2) {
-                    // 设置商品有效
-                    cartItemVo.setStatus(0);
-                    // 价格是否可见
-                    boolean priceVisible = (cartItemVo.getPriceFlag() == 0 || (cartItemVo.getPriceFlag() == 2 && userIdentity == 2));
-                    // 是否库存充足
-                    boolean isStocked = (cartItemVo.getStock() != null && cartItemVo.getStock() > 0 && cartItemVo.getStock() >= cartItemVo.getMin() && cartItemVo.getStock() >= cartItemVo.getNumber());
-                    if (priceVisible && isStocked) {
-                        // 获取商品促销信息
-                        CartPromotionsVo promotions = null;
-                        // 没有店铺促销时,商品促销才有效
-                        if (null == shopPromotion) {
+                // 迭代器设置商品信息
+                Iterator<CartItemVo> productIterator = productList.iterator();
+                while (productIterator.hasNext()) {
+                    CartItemVo cartItemVo = productIterator.next();
+                    // 设置商品图片及税费
+                    boolean taxFlag = setCartItemImgAndTax(cartItemVo);
+                    // 已上架商品
+                    if (cartItemVo.getValidFlag() == 2) {
+                        // 设置商品有效
+                        cartItemVo.setStatus(0);
+                        // 价格是否可见
+                        boolean priceVisible = (cartItemVo.getPriceFlag() == 0 || (cartItemVo.getPriceFlag() == 2 && userIdentity == 2));
+                        // 是否库存充足
+                        boolean isStocked = (cartItemVo.getStock() != null && cartItemVo.getStock() > 0 && cartItemVo.getStock() >= cartItemVo.getMin() && cartItemVo.getStock() >= cartItemVo.getNumber());
+                        if (priceVisible && isStocked) {
                             // 获取商品促销信息
-                            promotions = cartMapper.getPromotionByProductId(cartItemVo.getProductId());
-                            /*
-                             * 设置商品促销优惠
-                             */
-                            if (null != promotions) {
-                                cartItemVo.setPromotions(promotions);
-                                Integer promotionsId = promotions.getId();
-                                List<CartPromotionPriceVo> promotionPriceList = new ArrayList<>();
-                                CartPromotionPriceVo promotionPrice = new CartPromotionPriceVo();
-                                promotionPrice.setProductId(cartItemVo.getProductId());
-                                promotionPrice.setNumber(cartItemVo.getNumber());
-                                if (promotions.getType() == 1 && promotions.getMode() == 1) {
-                                    // 单品优惠价添加税费
-                                    if (taxFlag) {
-                                        BigDecimal addedValueTax = MathUtil.div(MathUtil.mul(promotions.getTouchPrice(), cartItemVo.getTaxRate()), BigDecimal.valueOf(100), 2);
-                                        promotions.setTouchPrice(MathUtil.add(promotions.getTouchPrice(), addedValueTax).doubleValue());
-                                    }
-                                    promotionPrice.setPrice(promotions.getTouchPrice());
-                                    promotionPrice.setOriginalPrice(cartItemVo.getOriginalPrice());
-                                    // 设置商品活动价
-                                    cartItemVo.setPrice(promotions.getTouchPrice());
-                                } else {
-                                    // 其他优惠商品添加税费
-                                    if (taxFlag) {
-                                        BigDecimal addedValueTax = MathUtil.div(MathUtil.mul(cartItemVo.getPrice(), cartItemVo.getTaxRate()), 100, 2);
-                                        promotionPrice.setPrice(MathUtil.add(cartItemVo.getPrice(), addedValueTax).doubleValue());
-                                    } else {
-                                        promotionPrice.setPrice(cartItemVo.getPrice());
-                                    }
-                                }
-                                promotionPriceList.add(promotionPrice);
-                                if (promotionsIds.contains(promotionsId)) {
-                                    // 列表已有该促销活动
-                                    totalPromotions.forEach(item -> {
-                                        if (item.getId().equals(promotionsId)) {
-                                            promotionPriceList.addAll(item.getProductList());
-                                            item.setProductList(promotionPriceList);
+                            CartPromotionsVo promotions = null;
+                            // 没有店铺促销时,商品促销才有效
+                            if (null == shopPromotion) {
+                                // 获取商品促销信息
+                                promotions = cartMapper.getPromotionByProductId(cartItemVo.getProductId());
+                                /*
+                                 * 设置商品促销优惠
+                                 */
+                                if (null != promotions) {
+                                    // 当前促销活动的价格计算列表
+                                    List<CartPromotionPriceVo> promotionPriceList = getPromotionProducts(promotions, cartItemVo, taxFlag);
+                                    // 更新到总促销列表
+                                    updateTotalPromotions(totalPromotions, promotionsIds, promotions, promotionPriceList);
+                                    //单品满减-计算供应商总价/满减金额
+                                    if (promotions.getType() == 1 && promotions.getMode() == 2) {
+                                        BigDecimal totalAmount = MathUtil.mul(cartItemVo.getNumber(), cartItemVo.getPrice());
+                                        if (MathUtil.compare(totalAmount, promotions.getTouchPrice()) > -1) {
+                                            // 如果满足促销条件,设置供应商价格-满减金额,满减总额 + 当前促销满减金额
+                                            shopPrice.set(MathUtil.sub(shopPrice.get(), promotions.getReducedPrice()).doubleValue());
+                                            shopReducedPrice.set(MathUtil.add(shopReducedPrice, promotions.getReducedPrice()).doubleValue());
                                         }
-                                    });
-                                } else {
-                                    // 新的促销活动
-                                    promotionsIds.add(promotions.getId());
-                                    if (promotions.getMode() == 3) {
-                                        // 获取赠品
-                                        List<CartItemVo> giftList = cartMapper.getPromotionGifts(promotions.getId());
-                                        promotions.setGiftList(giftList);
                                     }
-                                    promotions.setProductList(promotionPriceList);
-                                    // 添加到总促销
-                                    totalPromotions.add(promotions);
+                                    cartItemVo.setPromotions(promotions);
                                 }
-                                //单品满减-计算供应商总价/满减金额
-                                if (promotions.getType() == 1 && promotions.getMode() == 2) {
-                                    BigDecimal totalAmount = MathUtil.mul(cartItemVo.getNumber(), cartItemVo.getPrice());
-                                    if (MathUtil.compare(totalAmount, promotions.getTouchPrice()) > -1) {
-                                        // 如果满足促销条件,设置供应商价格-满减金额,满减总额 + 当前促销满减金额
-                                        shopPrice.set(MathUtil.sub(shopPrice.get(), promotions.getReducedPrice()).doubleValue());
-                                        shopReducedPrice.set(MathUtil.add(shopReducedPrice, promotions.getReducedPrice()).doubleValue());
+                            }
+                            if (null != promotions || null != shopPromotion) {
+                                // 商品处于活动状态
+                                cartItemVo.setActStatus(1);
+                                // 关闭阶梯价格,活动优先
+                                cartItemVo.setLadderFlag(0);
+                            } else {
+                                if (cartItemVo.getLadderFlag() == 1) {
+                                    // 设置阶梯价
+                                    setCartLadderPrices(cartItemVo, taxFlag);
+                                } else {
+                                    // 复购价
+                                    Double repurchase = baseMapper.getRepurchasePrice(cartItemVo.getProductId(), userId);
+                                    if (null != repurchase && repurchase > 0) {
+                                        cartItemVo.setPrice(repurchase);
                                     }
                                 }
                             }
-                        }
-                        if (null != promotions || null != shopPromotion) {
-                            // 商品处于活动状态
-                            cartItemVo.setActStatus(1);
-                            // 关闭阶梯价格,活动优先
-                            cartItemVo.setLadderFlag(0);
+                            // 该供应商下价格累加
+                            shopPrice.set(MathUtil.add(shopPrice, MathUtil.mul(cartItemVo.getNumber(), cartItemVo.getPrice())).doubleValue());
+                            // 该供应商下商品种类 +1
+                            shopKindCount.incrementAndGet();
+                            // 购物车总数量 + 当前商品购买数量
+                            totalCount.updateAndGet(v -> v + cartItemVo.getNumber());
                         } else {
-                            /*
-                             * 设置阶梯价
-                             */
-                            if (cartItemVo.getLadderFlag() == 1) {
-                                // 阶梯价
-                                List<LadderPriceVo> ladderPrices = baseMapper.getLadderPriceList(cartItemVo.getProductId());
-                                if (!CollectionUtils.isEmpty(ladderPrices)) {
-                                    IntStream.range(0, ladderPrices.size()).forEach(i -> {
-                                        boolean isThisLadder;
-                                        // 添加税费
-                                        if (taxFlag) {
-                                            BigDecimal addedValueTax = MathUtil.div(MathUtil.mul(ladderPrices.get(i).getBuyPrice(), cartItemVo.getTaxRate()), 100, 2);
-                                            ladderPrices.get(i).setBuyPrice(MathUtil.add(ladderPrices.get(i).getBuyPrice(), addedValueTax).doubleValue());
-                                        }
-                                        if (i == ladderPrices.size() - 1) {
-                                            ladderPrices.get(i).setMaxNum(0);
-                                            ladderPrices.get(i).setNumRange("≥" + ladderPrices.get(i).getBuyNum());
-                                            isThisLadder = (cartItemVo.getNumber() >= ladderPrices.get(i).getBuyNum());
-                                        } else {
-                                            ladderPrices.get(i).setMaxNum(ladderPrices.get(i + 1).getBuyNum());
-                                            String buyNumRangeShow = ladderPrices.get(i).getBuyNum() + "~" + (ladderPrices.get(i + 1).getBuyNum() - 1);
-                                            ladderPrices.get(i).setNumRange(buyNumRangeShow);
-                                            isThisLadder = (cartItemVo.getNumber() >= ladderPrices.get(i).getBuyNum() && cartItemVo.getNumber() <= ladderPrices.get(i).getMaxNum());
-                                        }
-                                        if (isThisLadder) {
-                                            cartItemVo.setPrice(ladderPrices.get(i).getBuyPrice());
-                                            cartItemVo.setOriginalPrice(ladderPrices.get(i).getBuyPrice());
-                                        }
-                                    });
-                                    cartItemVo.setMin(ladderPrices.get(0).getBuyNum());
-                                    cartItemVo.setLadderPrices(ladderPrices);
-                                } else {
-                                    cartItemVo.setLadderFlag(0);
-                                }
-                            } else {
-                                // 复购价
-                                Double repurchase = baseMapper.getRepurchasePrice(cartItemVo.getProductId(), userId);
-                                if (null != repurchase && repurchase > 0) {
-                                    cartItemVo.setPrice(repurchase);
-                                }
+                            // 失效商品
+                            if (cartItemVo.getPriceFlag() == 1) {
+                                // 未公开价格
+                                cartItemVo.setStatus(6);
+                            } else if (cartItemVo.getPriceFlag() == 2 && userIdentity == 4) {
+                                // 价格仅会员可见
+                                cartItemVo.setStatus(5);
+                            } else if (cartItemVo.getStock() == null || cartItemVo.getStock() == 0) {
+                                // 售罄
+                                cartItemVo.setStatus(4);
+                            } else if (cartItemVo.getStock() != null && (cartItemVo.getStock() < cartItemVo.getMin() || cartItemVo.getStock() < cartItemVo.getNumber())) {
+                                // 库存不足
+                                cartItemVo.setStatus(7);
                             }
+                            invalidList.add(cartItemVo);
+                            productIterator.remove();
                         }
-                        // 该供应商下价格累加
-                        shopPrice.set(MathUtil.add(shopPrice, MathUtil.mul(cartItemVo.getNumber(), cartItemVo.getPrice())).doubleValue());
-                        // 该供应商下商品种类 +1
-                        shopKindCount.incrementAndGet();
-                        // 购物车总数量 + 当前商品购买数量
-                        totalCount.updateAndGet(v -> v + cartItemVo.getNumber());
                     } else {
                         // 失效商品
-                        if (cartItemVo.getPriceFlag() == 1) {
-                            // 未公开价格
-                            cartItemVo.setStatus(6);
-                        } else if (cartItemVo.getPriceFlag() == 2 && userIdentity == 4) {
-                            // 价格仅会员可见
-                            cartItemVo.setStatus(5);
-                        } else if (cartItemVo.getStock() == null || cartItemVo.getStock() == 0) {
-                            // 售罄
-                            cartItemVo.setStatus(4);
-                        } else if (cartItemVo.getStock() != null && (cartItemVo.getStock() < cartItemVo.getMin() || cartItemVo.getStock() < cartItemVo.getNumber())) {
-                            // 库存不足
-                            cartItemVo.setStatus(7);
+                        if (cartItemVo.getValidFlag() == 0) {
+                            // 后台逻辑删除,已停售
+                            cartItemVo.setStatus(1);
+                            invalidList.add(cartItemVo);
+                        } else if (cartItemVo.getValidFlag() == 10) {
+                            // 已冻结,已丢失
+                            cartItemVo.setStatus(2);
+                            invalidList.add(cartItemVo);
+                        } else if (cartItemVo.getValidFlag() == 3) {
+                            // 已下架
+                            cartItemVo.setStatus(3);
+                            invalidList.add(cartItemVo);
                         }
-                        invalidList.add(cartItemVo);
+                        //隐身商品validFlag = 9 不加入失效商品,直接去除
                         productIterator.remove();
                     }
-                } else {
-                    // 失效商品
-                    if (cartItemVo.getValidFlag() == 0) {
-                        // 后台逻辑删除,已停售
-                        cartItemVo.setStatus(1);
-                        invalidList.add(cartItemVo);
-                    } else if (cartItemVo.getValidFlag() == 10) {
-                        // 已冻结,已丢失
-                        cartItemVo.setStatus(2);
-                        invalidList.add(cartItemVo);
-                    } else if (cartItemVo.getValidFlag() == 3) {
-                        // 已下架
-                        cartItemVo.setStatus(3);
-                        invalidList.add(cartItemVo);
-                    }
-                    //隐身商品validFlag = 9 不加入失效商品,直接去除
-                    productIterator.remove();
                 }
-            }
-            // 店铺促销
-            if (null != shopPromotion) {
-                shop.setPromotions(shopPromotion);
-                if (!promotionsIds.contains(shopPromotion.getId())) {
-                    promotionsIds.add(shopPromotion.getId());
-                    // 店铺满赠
-                    if (shopPromotion.getMode() == 3) {
-                        // 获取赠品
-                        List<CartItemVo> giftList = cartMapper.getPromotionGifts(shopPromotion.getId());
-                        shopPromotion.setGiftList(giftList);
-                    }
-                    // 设置该优惠下的商品列表
-                    List<CartPromotionPriceVo> promotionPriceList = new ArrayList<>();
-                    productList.forEach(item -> {
-                        CartPromotionPriceVo promotionPrice = new CartPromotionPriceVo();
-                        promotionPrice.setProductId(item.getProductId());
-                        promotionPrice.setNumber(item.getNumber());
-                        promotionPrice.setPrice(item.getPrice());
-                        promotionPriceList.add(promotionPrice);
-                    });
-                    shopPromotion.setProductList(promotionPriceList);
-                    // 添加到总促销
-                    totalPromotions.add(shopPromotion);
-                    // 店铺满减-计算供应商总价/满减金额
-                    if (shopPromotion.getMode() == 2) {
-                        if (MathUtil.compare(shopPrice, shopPromotion.getTouchPrice()) > -1) {
+                // 店铺促销
+                if (null != shopPromotion) {
+                    shop.setPromotions(shopPromotion);
+                    if (!promotionsIds.contains(shopPromotion.getId())) {
+                        promotionsIds.add(shopPromotion.getId());
+                        // 店铺满赠
+                        if (shopPromotion.getMode() == 3) {
+                            // 获取赠品
+                            List<CartItemVo> giftList = cartMapper.getPromotionGifts(shopPromotion.getId());
+                            shopPromotion.setGiftList(giftList);
+                        }
+                        // 设置该优惠下的商品列表
+                        List<CartPromotionPriceVo> promotionPriceList = new ArrayList<>();
+                        productList.forEach(item -> {
+                            CartPromotionPriceVo promotionPrice = new CartPromotionPriceVo();
+                            promotionPrice.setProductId(item.getProductId());
+                            promotionPrice.setNumber(item.getNumber());
+                            promotionPrice.setPrice(item.getPrice());
+                            promotionPriceList.add(promotionPrice);
+                        });
+                        shopPromotion.setProductList(promotionPriceList);
+                        // 添加到总促销
+                        totalPromotions.add(shopPromotion);
+                        // 店铺满减-计算供应商总价/满减金额
+                        if (shopPromotion.getMode() == 2 && MathUtil.compare(shopPrice, shopPromotion.getTouchPrice()) > -1) {
                             // 该供应商总价 - 满减金额
                             shopPrice.set(MathUtil.sub(shopPrice.get(), shopPromotion.getReducedPrice()).doubleValue());
                             // 该供应商优惠总额 + 满减金额
@@ -292,41 +218,41 @@ public class CartServiceImpl implements CartService {
                         }
                     }
                 }
-            }
-            shop.setCartList(productList);
-            // 供应商总价
-            shop.setTotalPrice(shopPrice.get());
-            // 供应商总优惠
-            shop.setReducedPrice(shopReducedPrice.get());
-            // 供应商划线价
-            shop.setOriginalPrice(MathUtil.add(shopPrice.get(), shopReducedPrice.get()).doubleValue());
-            // 供应商下商品种类
-            shop.setCount(shopKindCount.get());
-            // 计算总价
-            totalPrice.set(MathUtil.add(totalPrice, shop.getTotalPrice()).doubleValue());
-            // 优惠总额
-            reducedPrice.set(MathUtil.add(reducedPrice, shop.getReducedPrice()).doubleValue());
-            // 总划线价
-            totalOriginalPrice.set(MathUtil.add(totalOriginalPrice, shop.getOriginalPrice()).doubleValue());
-            // 商品种类
-            kindCount.updateAndGet(v -> v + shopKindCount.get());
-        });
-        // 删除空数据
-        shopInfoList.removeIf(shop -> (null != shop && shop.getCount() == 0));
-        // 总促销计算
-        totalPromotions.forEach(promotions -> {
-            // 该促销内商品总价
-            double touchPrice = promotions.getProductList().stream().mapToDouble(product -> product.getNumber() * product.getPrice()).sum();
-            if (MathUtil.compare(touchPrice, promotions.getTouchPrice()) > -1) {
+                shop.setCartList(productList);
+                // 供应商总价
+                shop.setTotalPrice(shopPrice.get());
+                // 供应商总优惠
+                shop.setReducedPrice(shopReducedPrice.get());
+                // 供应商划线价
+                shop.setOriginalPrice(MathUtil.add(shopPrice.get(), shopReducedPrice.get()).doubleValue());
+                // 供应商下商品种类
+                shop.setCount(shopKindCount.get());
+                // 计算总价
+                totalPrice.set(MathUtil.add(totalPrice, shop.getTotalPrice()).doubleValue());
+                // 优惠总额
+                reducedPrice.set(MathUtil.add(reducedPrice, shop.getReducedPrice()).doubleValue());
+                // 总划线价
+                totalOriginalPrice.set(MathUtil.add(totalOriginalPrice, shop.getOriginalPrice()).doubleValue());
+                // 商品种类
+                kindCount.updateAndGet(v -> v + shopKindCount.get());
+            });
+            // 删除空数据
+            shopInfoList.removeIf(shop -> (null != shop && shop.getCount() == 0));
+            // 总促销计算
+            totalPromotions.forEach(promotions -> {
                 // 凑单满减
                 if (promotions.getType() == 2 && promotions.getMode() == 2) {
-                    // 总价 - 满减金额
-                    totalPrice.set(MathUtil.sub(totalPrice, promotions.getReducedPrice()).doubleValue());
-                    // 优惠总额 + 满减金额
-                    reducedPrice.set(MathUtil.add(reducedPrice, promotions.getReducedPrice()).doubleValue());
+                    // 该促销内商品总价
+                    double touchPrice = promotions.getProductList().stream().mapToDouble(product -> product.getNumber() * product.getPrice()).sum();
+                    if (MathUtil.compare(touchPrice, promotions.getTouchPrice()) > -1) {
+                        // 总价 - 满减金额
+                        totalPrice.set(MathUtil.sub(totalPrice, promotions.getReducedPrice()).doubleValue());
+                        // 优惠总额 + 满减金额
+                        reducedPrice.set(MathUtil.add(reducedPrice, promotions.getReducedPrice()).doubleValue());
+                    }
                 }
-            }
-        });
+            });
+        }
         /*
          * 返回结果
          */
@@ -342,4 +268,325 @@ public class CartServiceImpl implements CartService {
         // 返回数据
         return ResponseJson.success(resultMap);
     }
+
+    /**
+     * 网站顶部购物车数据
+     *
+     * @param userId 用户Id
+     */
+    @Override
+    public ResponseJson<Map<String, Object>> getShoppingCartHead(Integer userId) {
+        /*
+         * 初始化返回数据
+         */
+        // 商品总数量
+        AtomicInteger totalCount = new AtomicInteger(0);
+        // 商品种类
+        AtomicInteger kindCount = new AtomicInteger(0);
+        // 统计商品总金额
+        AtomicDouble totalPrice = new AtomicDouble(0);
+        // 购物车商品列表
+        List<CartItemVo> cartList = null;
+        /*
+         * 处理数据
+         */
+        // 促销活动(总)
+        List<CartPromotionsVo> totalPromotions = new ArrayList<>();
+        List<Integer> promotionsIds = new ArrayList<>();
+        // 用户身份
+        Integer userIdentity = baseMapper.getIdentityByUserId(userId);
+        // 获取购物车商品列表(不区分供应商)
+        cartList = cartMapper.getCartProductList(userId);
+        if (null != cartList && cartList.size()>0) {
+            // 移除价格不可见商品
+            cartList.removeIf(cartItemVo -> !(cartItemVo.getPriceFlag() == 0 || (cartItemVo.getPriceFlag() == 2 && userIdentity == 2)));
+            cartList.forEach(cartItemVo -> {
+                // 设置商品图片及税费
+                boolean taxFlag = setCartItemImgAndTax(cartItemVo);
+                // 获取商品促销信息
+                CartPromotionsVo promotions = cartMapper.getPromotionByProductId(cartItemVo.getProductId());
+                /*
+                 * 设置商品促销优惠
+                 */
+                if (null != promotions) {
+                    // 关闭阶梯价格,活动优先
+                    cartItemVo.setLadderFlag(0);
+                    // 商品处于活动状态
+                    cartItemVo.setActStatus(1);
+                    // 当前促销活动的价格计算列表
+                    List<CartPromotionPriceVo> promotionPriceList = getPromotionProducts(promotions, cartItemVo, taxFlag);
+                    // 更新到总促销列表
+                    updateTotalPromotions(totalPromotions, promotionsIds, promotions, promotionPriceList);
+                } else {
+                    if (cartItemVo.getLadderFlag() == 1) {
+                        // 设置阶梯价
+                        setCartLadderPrices(cartItemVo, taxFlag);
+                        // 头部只展示简略信息,不显示详细阶梯价格
+                        cartItemVo.setLadderPrices(null);
+                    } else {
+                        // 复购价
+                        Double repurchase = baseMapper.getRepurchasePrice(cartItemVo.getProductId(), userId);
+                        if (null != repurchase && repurchase > 0) {
+                            cartItemVo.setPrice(repurchase);
+                        }
+                    }
+                }
+                // 商品总金额累加
+                totalPrice.set(MathUtil.add(totalPrice, MathUtil.mul(cartItemVo.getNumber(), cartItemVo.getPrice())).doubleValue());
+                // 商品种类 +1
+                kindCount.incrementAndGet();
+                // 购物车总数量 + 当前商品购买数量
+                totalCount.updateAndGet(v -> v + cartItemVo.getNumber());
+            });
+            // 总促销-满减计算
+            totalPromotions.forEach(promotions -> {
+                // 满减
+                if (promotions.getMode() == 2) {
+                    // 该促销内商品总价
+                    double touchPrice = promotions.getProductList().stream().mapToDouble(product -> product.getNumber() * product.getPrice()).sum();
+                    if (MathUtil.compare(touchPrice, promotions.getTouchPrice()) > -1) {
+                        // 总价 - 满减金额
+                        totalPrice.set(MathUtil.sub(totalPrice, promotions.getReducedPrice()).doubleValue());
+                    }
+                }
+            });
+        }
+        /*
+         * 返回结果
+         */
+        Map<String, Object> resultMap = new HashMap<>(4);
+        resultMap.put("list", cartList);
+        resultMap.put("kindCount", kindCount);
+        resultMap.put("totalCount", totalCount);
+        resultMap.put("totalPrice", totalPrice);
+        // 返回数据
+        return ResponseJson.success(resultMap);
+    }
+
+    /**
+     * 添加购物车
+     *
+     * @param cartDto {
+     *                userId       用户ID
+     *                productId    商品id
+     *                productCount 商品数量
+     *                }
+     */
+    @Override
+    public ResponseJson<Integer> addShoppingCart(CartDto cartDto) {
+        CartPo cart = cartMapper.getCartPo(cartDto);
+        if (cart != null) {
+            // 购物车已存在该商品,更新数量
+            cart.setProductCount(cart.getProductCount() + cartDto.getProductCount());
+            cart.setAddTime(new Date());
+            cartMapper.updateCart(cart);
+        } else {
+            // 添加新购物车
+            cart = new CartPo();
+            cart.setUserId(cartDto.getUserId());
+            cart.setProductId(cartDto.getProductId());
+            cart.setProductCount(cartDto.getProductCount());
+            //判断是否是再次购买
+            Double repurchase = baseMapper.getRepurchasePrice(cartDto.getProductId(), cartDto.getUserId());
+            if (null != repurchase && repurchase > 0) {
+                // 再次购买
+                cart.setReBuyFlag(1);
+            } else {
+                cart.setReBuyFlag(0);
+            }
+            cart.setAddTime(new Date());
+            cartMapper.insertCart(cart);
+        }
+        // 获取购物车数量(商品种类数)
+        int cartCount = getCartCount(cartDto.getUserId());
+        return ResponseJson.success("添加成功!返回购物车数量", cartCount);
+    }
+
+    /**
+     * 更新购物车
+     *
+     * @param cartDto {
+     *                userId       用户ID
+     *                productId    商品id
+     *                productCount 商品数量
+     *                }
+     */
+    @Override
+    public ResponseJson<Integer> updateShoppingCart(CartDto cartDto) {
+        CartPo cart = new CartPo();
+        cart.setUserId(cartDto.getUserId());
+        cart.setProductId(cartDto.getProductId());
+        cart.setProductCount(cartDto.getProductCount());
+        cart.setAddTime(new Date());
+        cartMapper.updateCart(cart);
+        // 获取购物车数量(商品种类数)
+        int cartCount = getCartCount(cartDto.getUserId());
+        return ResponseJson.success("更新成功!返回购物车数量", cartCount);
+    }
+
+    /**
+     * 删除购物车
+     *
+     * @param cartDto {
+     *                userId       用户ID
+     *                productIds   商品ids,逗号隔开
+     *                }
+     */
+    @Override
+    public ResponseJson<Integer> deleteShoppingCart(CartDto cartDto) {
+        List<Integer> productIdList = Lists.newArrayList();
+        if (cartDto.getProductIds().contains(",")) {
+            String[] productArr = cartDto.getProductIds().split(",");
+            for (String id : productArr) {
+                productIdList.add(Integer.parseInt(id));
+            }
+        } else {
+            productIdList.add(Integer.parseInt(cartDto.getProductIds()));
+        }
+        cartMapper.deleteCartByProductIds(cartDto.getUserId(), productIdList);
+        // 获取购物车数量(商品种类数)
+        int cartCount = getCartCount(cartDto.getUserId());
+        return ResponseJson.success("删除成功!返回购物车数量", cartCount);
+    }
+
+    /**
+     * 获取购物车数量(商品种类数)
+     * @param userId 用户ID
+     * @return int
+     */
+    private int getCartCount(Integer userId){
+        if (null == userId) {
+            return 0;
+        }
+        // 获取购物车商品列表(不区分供应商)
+        List<CartItemVo> cartList = cartMapper.getCartProductList(userId);
+        if (null != cartList && cartList.size()>0) {
+            // 用户身份
+            Integer userIdentity = baseMapper.getIdentityByUserId(userId);
+            // 移除价格不可见商品
+            cartList.removeIf(cartItemVo -> !(cartItemVo.getPriceFlag() == 0 || (cartItemVo.getPriceFlag() == 2 && userIdentity == 2)));
+            return cartList.size();
+        } else {
+            return 0;
+        }
+    }
+
+    /**
+     * 设置商品图片及税费
+     * @param cartItemVo 购物车商品
+     * @return taxFlag 是否需要设置税费
+     */
+    private boolean setCartItemImgAndTax(CartItemVo cartItemVo) {
+        // 图片路径
+        String image = ProductUtil.getImageUrl("product", cartItemVo.getImage(), domain);
+        cartItemVo.setImage(image);
+        // 是否添加税费,不含税商品 开票需添加税费
+        boolean taxFlag = "0".equals(cartItemVo.getIncludedTax()) && ("1".equals(cartItemVo.getInvoiceType()) || "2".equals(cartItemVo.getInvoiceType()));
+        if (taxFlag) {
+            BigDecimal cartItemTax = MathUtil.div(MathUtil.mul(cartItemVo.getPrice(), cartItemVo.getTaxRate()), 100, 2);
+            cartItemVo.setPrice(MathUtil.add(cartItemVo.getPrice(), cartItemTax).doubleValue());
+        }
+        return taxFlag;
+    }
+
+    /**
+     * 设置购物车阶梯价
+     * @param cartItemVo
+     * @param taxFlag
+     */
+    private void setCartLadderPrices(CartItemVo cartItemVo, boolean taxFlag) {
+        // 阶梯价列表
+        List<LadderPriceVo> ladderPrices = baseMapper.getLadderPriceList(cartItemVo.getProductId());
+        if (!CollectionUtils.isEmpty(ladderPrices)) {
+            IntStream.range(0, ladderPrices.size()).forEach(i -> {
+                boolean isThisLadder;
+                // 添加税费
+                if (taxFlag) {
+                    BigDecimal addedValueTax = MathUtil.div(MathUtil.mul(ladderPrices.get(i).getBuyPrice(), cartItemVo.getTaxRate()), 100, 2);
+                    ladderPrices.get(i).setBuyPrice(MathUtil.add(ladderPrices.get(i).getBuyPrice(), addedValueTax).doubleValue());
+                }
+                if (i == ladderPrices.size() - 1) {
+                    ladderPrices.get(i).setMaxNum(0);
+                    ladderPrices.get(i).setNumRange("≥" + ladderPrices.get(i).getBuyNum());
+                    isThisLadder = (cartItemVo.getNumber() >= ladderPrices.get(i).getBuyNum());
+                } else {
+                    ladderPrices.get(i).setMaxNum(ladderPrices.get(i + 1).getBuyNum());
+                    String buyNumRangeShow = ladderPrices.get(i).getBuyNum() + "~" + (ladderPrices.get(i + 1).getBuyNum() - 1);
+                    ladderPrices.get(i).setNumRange(buyNumRangeShow);
+                    isThisLadder = (cartItemVo.getNumber() >= ladderPrices.get(i).getBuyNum() && cartItemVo.getNumber() <= ladderPrices.get(i).getMaxNum());
+                }
+                if (isThisLadder) {
+                    cartItemVo.setPrice(ladderPrices.get(i).getBuyPrice());
+                    cartItemVo.setOriginalPrice(ladderPrices.get(i).getBuyPrice());
+                }
+            });
+            cartItemVo.setMin(ladderPrices.get(0).getBuyNum());
+            cartItemVo.setLadderPrices(ladderPrices);
+        } else {
+            cartItemVo.setLadderFlag(0);
+        }
+    }
+
+    /**
+     * 当前促销活动的价格计算列表
+     *
+     * @param promotions 促销活动
+     * @param cartItemVo 当前购物车商品
+     * @param taxFlag    计算税费标志
+     * @return 价格计算列表
+     */
+    private List<CartPromotionPriceVo> getPromotionProducts(CartPromotionsVo promotions, CartItemVo cartItemVo, boolean taxFlag) {
+        List<CartPromotionPriceVo> promotionPriceList = new ArrayList<>();
+        CartPromotionPriceVo promotionPrice = new CartPromotionPriceVo();
+        promotionPrice.setProductId(cartItemVo.getProductId());
+        promotionPrice.setNumber(cartItemVo.getNumber());
+        // 设置价格
+        if (promotions.getType() == 1 && promotions.getMode() == 1) {
+            // 单品优惠价添加税费
+            if (taxFlag) {
+                BigDecimal addedValueTax = MathUtil.div(MathUtil.mul(promotions.getTouchPrice(), cartItemVo.getTaxRate()), BigDecimal.valueOf(100), 2);
+                promotions.setTouchPrice(MathUtil.add(promotions.getTouchPrice(), addedValueTax).doubleValue());
+            }
+            promotionPrice.setPrice(promotions.getTouchPrice());
+            promotionPrice.setOriginalPrice(cartItemVo.getOriginalPrice());
+            // 设置商品活动价
+            cartItemVo.setPrice(promotions.getTouchPrice());
+        } else {
+            promotionPrice.setPrice(cartItemVo.getPrice());
+        }
+        promotionPriceList.add(promotionPrice);
+        return promotionPriceList;
+    }
+
+    /**
+     * 把当前促销更新购物车总促销
+     * @param totalPromotions 总促销列表
+     * @param promotionsIds   总促销Id集合
+     * @param promotions      当前促销
+     * @param promotionPriceList  当前促销商品价格计算列表
+     */
+    private void updateTotalPromotions(List<CartPromotionsVo> totalPromotions, List<Integer> promotionsIds, CartPromotionsVo promotions, List<CartPromotionPriceVo> promotionPriceList) {
+        if (promotionsIds.contains(promotions.getId())) {
+            // 列表已有该促销活动
+            totalPromotions.forEach(item -> {
+                if (item.getId().equals(promotions.getId())) {
+                    promotionPriceList.addAll(item.getProductList());
+                    item.setProductList(promotionPriceList);
+                }
+            });
+        } else {
+            // 新的促销活动
+            promotionsIds.add(promotions.getId());
+            if (promotions.getMode() == 3) {
+                // 获取赠品
+                List<CartItemVo> giftList = cartMapper.getPromotionGifts(promotions.getId());
+                promotions.setGiftList(giftList);
+            }
+            promotions.setProductList(promotionPriceList);
+            // 添加到总促销
+            totalPromotions.add(promotions);
+        }
+    }
+
+
 }

+ 12 - 0
src/main/resources/backup.sql

@@ -0,0 +1,12 @@
+-- =========== 订单微服务模块
+
+-- 废弃 sku表
+
+ALTER TABLE `cm_cart`
+MODIFY COLUMN `skuID` varchar(20) DEFAULT NULL COMMENT '【2021-06已废弃】',
+MODIFY COLUMN `isOutOfTime` char(1) DEFAULT '0' COMMENT '【2021-06已废弃】',
+add column `shopID` int(11) default null comment '商品供应商Id' after `productID`;
+
+update `cm_cart` c left join `product` p on c.`productID`=p.`productID` set c.`shopID`=p.`shopID`;
+
+

+ 118 - 62
src/main/resources/mapper/CartMapper.xml

@@ -3,43 +3,43 @@
 <mapper namespace="com.caimei365.order.mapper.CartMapper">
     <!-- 购物车供应商列表 -->
     <select id="getCartShops" resultType="com.caimei365.order.model.vo.CartShopVo">
-        select
-            c.shopID as shopId,
-            s.name as shopName,
-            s.logo as shopLogo
-        from cm_cart c
-        left join shop s on c.shopID = s.shopID
-        where c.userID = #{userId}
-        group by c.shopID
-        order by c.cm_cartID desc
+        SELECT
+            c.shopID AS shopId,
+            s.name AS shopName,
+            s.logo AS shopLogo
+        FROM cm_cart c
+        LEFT JOIN shop s ON c.shopID = s.shopID
+        WHERE c.userID = #{userId}
+        GROUP BY c.shopID
+        ORDER BY c.cm_cartID DESC
     </select>
     <select id="getCartProductsByShopId" resultType="com.caimei365.order.model.vo.CartItemVo">
-        select
-            c.cm_cartID as id,
-            c.productCount as number,
-            p.productID as productId,
-            p.shopID as shopId,
-            p.`name` as `name`,
-            p.mainImage as image,
-            p.price1 as price,
-            p.price1 as originalPrice,
-            p.unit as unit,
-            p.stock as stock,
-            p.step as step,
-            p.minBuyNumber as min,
-            p.price1TextFlag as priceFlag,
-            p.ladderPriceFlag as ladderFlag,
-            p.includedTax as includedTax,
-            p.invoiceType as invoiceType,
-            p.taxPoint as taxRate,
-            p.validFlag as validFlag
-        from cm_cart c
-        left join product p on c.productID = p.productID
-        where c.userID = #{userId} and p.shopID = #{shopId}
-        order by c.cm_cartID desc
+        SELECT
+            c.cm_cartID AS id,
+            c.productCount AS number,
+            p.productID AS productId,
+            p.shopID AS shopId,
+            p.`name` AS `name`,
+            p.mainImage AS image,
+            p.price1 AS price,
+            p.price1 AS originalPrice,
+            p.unit AS unit,
+            p.stock AS stock,
+            p.step AS step,
+            p.minBuyNumber AS MIN,
+            p.price1TextFlag AS priceFlag,
+            p.ladderPriceFlag AS ladderFlag,
+            p.includedTax AS includedTax,
+            p.invoiceType AS invoiceType,
+            p.taxPoint AS taxRate,
+            p.validFlag AS validFlag
+        FROM cm_cart c
+        LEFT JOIN product p ON c.productID = p.productID
+        WHERE c.userID = #{userId} and p.shopID = #{shopId}
+        ORDER BY c.cm_cartID DESC
     </select>
     <select id="getPromotionByShopId" resultType="com.caimei365.order.model.vo.CartPromotionsVo">
-        select
+        SELECT
             pr.id,
             pr.name,
             pr.description,
@@ -51,15 +51,15 @@
             pr.endTime,
             pr.status,
             prp.productId,
-            prp.supplierId as shopId
-        from cm_promotions pr
-        left join cm_promotions_product prp on pr.id = prp.promotionsId
-        where prp.supplierId = #{shopId} and pr.delFlag=0 and pr.type=3
-        and (pr.status = 1 or ( pr.status = 2 and (NOW() between pr.beginTime and pr.endTime)))
-        order by pr.type desc limit 1
+            prp.supplierId AS shopId
+        FROM cm_promotions pr
+        LEFT JOIN cm_promotions_product prp ON pr.id = prp.promotionsId
+        WHERE prp.supplierId = #{shopId} and pr.delFlag=0 and pr.type=3
+        AND (pr.status = 1 OR ( pr.status = 2 AND (NOW() BETWEEN pr.beginTime AND pr.endTime)))
+        ORDER BY pr.type DESC LIMIT 1
     </select>
     <select id="getPromotionByProductId" resultType="com.caimei365.order.model.vo.CartPromotionsVo">
-        select
+        SELECT
             pr.id,
             pr.name,
             pr.description,
@@ -71,29 +71,85 @@
             pr.endTime,
             pr.status,
             prp.productId,
-            prp.supplierId as shopId
-        from cm_promotions pr
-        left join cm_promotions_product prp on pr.id = prp.promotionsId
-        where prp.productId = #{productId} and pr.delFlag=0 and pr.type in (1,2)
-        and (pr.status = 1 or ( pr.status = 2 and (NOW() between pr.beginTime and pr.endTime)))
-        order by pr.type desc limit 1
+            prp.supplierId AS shopId
+        FROM cm_promotions pr
+        LEFT JOIN cm_promotions_product prp ON pr.id = prp.promotionsId
+        WHERE prp.productId = #{productId} and pr.delFlag=0 and pr.type in (1,2)
+        AND (pr.status = 1 OR ( pr.status = 2 AND (NOW() BETWEEN pr.beginTime AND pr.endTime)))
+        ORDER BY pr.type DESC LIMIT 1
     </select>
     <select id="getPromotionGifts" resultType="com.caimei365.order.model.vo.CartItemVo">
-        select
-            cpg.id as id,
-            p.productID as productId,
-            p.shopID as shopId,
-            p.`name` as `name`,
-            p.mainImage as image,
-            cpg.number as number,
-            0 as price,
-            p.price1 as originalPrice,
-            p.unit as unit,
-            p.stock as stock,
-            p.validFlag as validFlag
-        from cm_promotions_gift cpg
-        left join from product p on cpg.productId = p.productID
-        where cpg.promotionsId = #{promotionsId}
-        order by cpg.id desc
+        SELECT
+            cpg.id AS id,
+            p.productID AS productId,
+            p.shopID AS shopId,
+            p.`name` AS `name`,
+            p.mainImage AS image,
+            cpg.number AS number,
+            0 AS price,
+            p.price1 AS originalPrice,
+            p.unit AS unit,
+            p.stock AS stock,
+            p.validFlag AS validFlag
+        FROM cm_promotions_gift cpg
+        LEFT JOIN FROM product p ON cpg.productId = p.productID
+        WHERE cpg.promotionsId = #{promotionsId}
+        ORDER BY cpg.id DESC
     </select>
+    <select id="getCartProductList" resultType="com.caimei365.order.model.vo.CartItemVo">
+        SELECT
+            c.cm_cartID AS id,
+            c.productCount AS number,
+            p.productID AS productId,
+            p.shopID AS shopId,
+            p.`name` AS `name`,
+            p.mainImage AS image,
+            p.price1 AS price,
+            p.price1 AS originalPrice,
+            p.unit AS unit,
+            p.stock AS stock,
+            p.step AS step,
+            p.minBuyNumber AS MIN,
+            p.price1TextFlag AS priceFlag,
+            p.ladderPriceFlag AS ladderFlag,
+            p.includedTax AS includedTax,
+            p.invoiceType AS invoiceType,
+            p.taxPoint AS taxRate,
+            p.validFlag AS validFlag
+        FROM cm_cart c
+        LEFT JOIN product p ON c.productID = p.productID
+        WHERE p.validFlag='2' AND c.userID = #{userId}
+        AND p.price1TextFlag != '1'
+        AND p.stock != '0' AND p.stock <![CDATA[ >= ]]> c.productCount
+        ORDER BY c.cm_cartID DESC
+    </select>
+    <select id="getCartPo" resultType="com.caimei365.order.model.po.CartPo">
+        SELECT
+            c.cm_cartID AS id,
+            c.productCount,
+            c.productID AS productId,
+            c.userID AS userId
+        FROM cm_cart c
+        WHERE c.productID = #{productId} and c.userID = #{userId}
+        ORDER BY c.cm_cartID DESC
+        limit 1
+    </select>
+    <update id="updateCart" parameterType="com.caimei365.order.model.po.CartPo">
+        UPDATE cm_cart set
+           productCount = #{productCount},
+           addTime = #{addTime}
+        WHERE userID = #{userId} AND productID = #{productId}
+    </update>
+    <insert id="insertCart" keyColumn="cm_cartID" keyProperty="id"  parameterType="com.caimei365.order.model.po.CartPo" useGeneratedKeys="true">
+        INSERT INTO cm_cart (productID, userID, productCount, addTime, reBuyFlag)
+        VALUES (#{productId}, #{userId}, #{productCount}, #{addTime}, #{reBuyFlag})
+    </insert>
+    <delete id="deleteCartByProductIds">
+        DELETE FROM cm_cart
+        WHERE userID = #{userId}
+        AND productID in
+        <foreach collection="productIds" open="(" separator="," close=")" item="productId">
+            #{productId}
+        </foreach>
+    </delete>
 </mapper>