Browse Source

协销购物车结算优惠券

chao 3 years ago
parent
commit
22e7154e95

+ 0 - 9
src/main/java/com/caimei365/order/mapper/CartClubMapper.java

@@ -66,13 +66,4 @@ public interface CartClubMapper {
      * @param userId 用户Id
      */
     List<CartItemVo> getCartProductsByShopIdAndProductIds(@Param("userId") Integer userId,@Param("shopId")  Integer shopId, @Param("productIds") List<String> productIds);
-    /**
-     * 获取用户可用优惠券列表
-     * @param userId 用户Id
-     */
-    List<CouponVo> getClubCouponList(Integer userId);
-    /**
-     * 活动券商品Ids
-     */
-    List<Integer> getCouponProductIds(Integer couponId, Integer source);
 }

+ 9 - 0
src/main/java/com/caimei365/order/mapper/OrderCommonMapper.java

@@ -121,4 +121,13 @@ public interface OrderCommonMapper {
      * @param logisticsBatchId 物流批次Id
      */
     List<LogisticsInformationVo> getLogisticsInfoList(Integer logisticsBatchId);
+    /**
+     * 获取用户可用优惠券列表
+     * @param userId 用户Id
+     */
+    List<CouponVo> getClubCouponList(Integer userId);
+    /**
+     * 活动券商品Ids
+     */
+    List<Integer> getCouponProductIds(Integer couponId, Integer source);
 }

+ 15 - 11
src/main/java/com/caimei365/order/service/impl/CartClubServiceImpl.java

@@ -3,6 +3,7 @@ package com.caimei365.order.service.impl;
 import com.caimei365.order.components.ProductService;
 import com.caimei365.order.mapper.BaseMapper;
 import com.caimei365.order.mapper.CartClubMapper;
+import com.caimei365.order.mapper.OrderCommonMapper;
 import com.caimei365.order.model.ResponseJson;
 import com.caimei365.order.model.dto.CartDto;
 import com.caimei365.order.model.po.CartPo;
@@ -41,6 +42,8 @@ public class CartClubServiceImpl implements CartClubService {
     @Resource
     private CartClubMapper cartClubMapper;
     @Resource
+    private OrderCommonMapper orderCommonMapper;
+    @Resource
     private ProductService productService;
 
     /**
@@ -75,7 +78,7 @@ public class CartClubServiceImpl implements CartClubService {
         List<PromotionsVo> totalPromotions = new ArrayList<>();
         List<Integer> promotionsIds = new ArrayList<>();
         // 用户可用优惠券(总)
-        List<CouponVo> couponList = cartClubMapper.getClubCouponList(userId);
+        List<CouponVo> couponList = orderCommonMapper.getClubCouponList(userId);
         List<CartItemVo> cartAllProducts = new ArrayList<>();
         // 用户身份
         Integer userIdentity = baseMapper.getIdentityByUserId(userId);
@@ -302,7 +305,7 @@ public class CartClubServiceImpl implements CartClubService {
                 CouponVo coupon = iterator.next();
                 if (coupon.getCouponType() == 0 && coupon.getProductType() == 2) {
                     // 活动券商品Ids
-                    List<Integer> productIds = cartClubMapper.getCouponProductIds(coupon.getCouponId(), source);
+                    List<Integer> productIds = orderCommonMapper.getCouponProductIds(coupon.getCouponId(), source);
                     coupon.setProductIds(productIds);
                     for (CartItemVo item : cartItems) {
                         if (!productIds.contains(item.getProductId())) {
@@ -361,7 +364,7 @@ public class CartClubServiceImpl implements CartClubService {
                         break;
                     } else {
                         // 活动券商品Ids
-                        List<Integer> productIds = cartClubMapper.getCouponProductIds(coupon.getCouponId(), source);
+                        List<Integer> productIds = orderCommonMapper.getCouponProductIds(coupon.getCouponId(), source);
                         for (CartItemVo item : cartItems) {
                             if (productIds.contains(item.getProductId())) {
                                 couponsLogo = true;
@@ -807,8 +810,15 @@ public class CartClubServiceImpl implements CartClubService {
             }
         });
 
+        // 发票信息
+        InvoiceVo invoice = baseMapper.getUserInvoice(userId);
+        // 可用余额
+        Double availableMoney = baseMapper.getAbleUserMoney(userId);
+        // 可用采美豆
+        Integer userBeans = baseMapper.getUserBeans(userId);
+
         // 用户可用优惠券(总)
-        List<CouponVo> couponList = cartClubMapper.getClubCouponList(userId);
+        List<CouponVo> couponList = orderCommonMapper.getClubCouponList(userId);
         Iterator<CouponVo> iterator = couponList.iterator();
         while (iterator.hasNext()) {
             CouponVo coupon = iterator.next();
@@ -822,7 +832,7 @@ public class CartClubServiceImpl implements CartClubService {
                         totalAmount = MathUtil.add(price, totalAmount);
                     } else if (coupon.getProductType() == 2) {
                         //活动券-指定商品
-                        List<Integer> couponProductIds = cartClubMapper.getCouponProductIds(coupon.getCouponId(), source);
+                        List<Integer> couponProductIds = orderCommonMapper.getCouponProductIds(coupon.getCouponId(), source);
                         if (couponProductIds.contains(item.getProductId())) {
                             BigDecimal price = MathUtil.mul(item.getPrice(), item.getNumber());
                             totalAmount = MathUtil.add(price, totalAmount);
@@ -859,12 +869,6 @@ public class CartClubServiceImpl implements CartClubService {
         }
         couponList.sort((o1, o2) -> o2.getCouponAmount().compareTo(o1.getCouponAmount()));
 
-        // 发票信息
-        InvoiceVo invoice = baseMapper.getUserInvoice(userId);
-        // 可用余额
-        Double availableMoney = baseMapper.getAbleUserMoney(userId);
-        // 可用采美豆
-        Integer userBeans = baseMapper.getUserBeans(userId);
         // 包装返回数据
         Map<String, Object> resultData = new HashMap<>();
         resultData.put("list", shopList);

+ 60 - 1
src/main/java/com/caimei365/order/service/impl/CartSellerServiceImpl.java

@@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSONObject;
 import com.caimei365.order.components.ProductService;
 import com.caimei365.order.mapper.BaseMapper;
 import com.caimei365.order.mapper.CartSellerMapper;
+import com.caimei365.order.mapper.OrderCommonMapper;
 import com.caimei365.order.model.ResponseJson;
 import com.caimei365.order.model.dto.AgainBuyDto;
 import com.caimei365.order.model.dto.SellerCartDto;
@@ -17,6 +18,7 @@ import com.github.pagehelper.PageInfo;
 import com.google.common.collect.Lists;
 import com.google.common.util.concurrent.AtomicDouble;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang.StringUtils;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
@@ -46,6 +48,8 @@ public class CartSellerServiceImpl implements CartSellerService {
     @Resource
     private CartSellerMapper cartSellerMapper;
     @Resource
+    private OrderCommonMapper orderCommonMapper;
+    @Resource
     private ProductService productService;
 
     /**
@@ -364,7 +368,8 @@ public class CartSellerServiceImpl implements CartSellerService {
         AtomicDouble totalOriginalPrice = new AtomicDouble(0);
         // 是否包含充值商品,默认false
         AtomicBoolean includeRecharge = new AtomicBoolean(false);
-
+        //所有的商品数据
+        List<CartItemVo> totalProductList = new ArrayList<>();
         // 促销活动(总)
         List<PromotionsVo> totalPromotions = new ArrayList<>();
         // 促销活动Id集合,用于合并促销活动
@@ -509,6 +514,7 @@ public class CartSellerServiceImpl implements CartSellerService {
                         // 商品种类
                         kindCount.updateAndGet(v -> v + shopKindCount.get());
                     }
+                    totalProductList.addAll(productList);
                 }
                 // 供应商下商品种类
                 shop.setCount(shopKindCount.get());
@@ -556,6 +562,59 @@ public class CartSellerServiceImpl implements CartSellerService {
         Double availableMoney = baseMapper.getAbleUserMoney(clubUserId);
         // 可用采美豆
         Integer userBeans = baseMapper.getUserBeans(clubUserId);
+
+        //优惠券相关信息
+        List<CouponVo> couponList = orderCommonMapper.getClubCouponList(clubUserId);
+        Iterator<CouponVo> iterator = couponList.iterator();
+        while (iterator.hasNext()) {
+            CouponVo coupon = iterator.next();
+            BigDecimal totalAmount = BigDecimal.ZERO;
+            Integer couponType = coupon.getCouponType();
+            for (CartItemVo item : totalProductList) {
+                if (couponType == 0) {
+                    if (coupon.getProductType() == 1) {
+                        //活动券-全商城商品
+                        BigDecimal price = MathUtil.mul(item.getPrice(), item.getNumber());
+                        totalAmount = MathUtil.add(price, totalAmount);
+                    } else if (coupon.getProductType() == 2) {
+                        //活动券-指定商品
+                        List<Integer> couponProductIds = orderCommonMapper.getCouponProductIds(coupon.getCouponId(), 2);
+                        if (couponProductIds.contains(item.getProductId())) {
+                            BigDecimal price = MathUtil.mul(item.getPrice(), item.getNumber());
+                            totalAmount = MathUtil.add(price, totalAmount);
+                        }
+                    }
+                } else if (couponType == 1) {
+                    //品类券
+                    if (coupon.getCategoryType().equals(item.getCommodityType())) {
+                        BigDecimal price = MathUtil.mul(item.getPrice(), item.getNumber());
+                        totalAmount = MathUtil.add(price, totalAmount);
+                    }
+                } else if (couponType == 2 || couponType == 4) {
+                    //用户专享券或新用户券
+                    BigDecimal price = MathUtil.mul(item.getPrice(), item.getNumber());
+                    totalAmount = MathUtil.add(price, totalAmount);
+                } else if (coupon.getCouponType() == 3 && coupon.getShopId() != null) {
+                    //店铺券
+                    String shopName = baseMapper.getShopNameById(coupon.getShopId());
+                    if (StringUtils.isNotBlank(shopName)) {
+                        if (shopName.length() > 7) {
+                            shopName = shopName.substring(0, 6) + "...";
+                        }
+                        coupon.setShopName(shopName);
+                    }
+                    if (coupon.getShopId().equals(item.getShopId())) {
+                        BigDecimal price = MathUtil.mul(item.getPrice(), item.getNumber());
+                        totalAmount = MathUtil.add(price, totalAmount);
+                    }
+                }
+            }
+            if (MathUtil.compare(totalAmount, coupon.getTouchPrice()) < 0) {
+                iterator.remove();
+            }
+        }
+        couponList.sort((o1, o2) -> o2.getCouponAmount().compareTo(o1.getCouponAmount()));
+
         // 包装返回数据
         Map<String, Object> resultData = new HashMap<>();
         resultData.put("list", shopList);

+ 0 - 34
src/main/resources/mapper/CartClubMapper.xml

@@ -120,40 +120,6 @@
         ORDER BY c.cm_cartID DESC
         limit 1
     </select>
-    <select id="getClubCouponList" resultType="com.caimei365.order.model.vo.CouponVo">
-        SELECT
-            a.id AS clubCouponId,
-            cc.`id` AS couponId,
-            cc.`couponAmount`,
-            cc.`touchPrice`,
-            cc.`startDate`,
-            cc.`endDate`,
-            cc.`couponType`,
-            cc.`userId`,
-            cc.`shopId`,
-            cc.`productType`,
-            cc.`categoryType`
-        FROM cm_coupon_club a
-        LEFT JOIN cm_coupon cc ON a.couponId = cc.id
-        WHERE cc.delFlag = 0
-            AND a.delFlag = 0
-            AND a.userId = #{userId}
-            AND a.status = 1
-            AND NOW() BETWEEN cc.startDate
-            AND cc.endDate
-            AND cc.status != 2
-        ORDER BY a.createDate DESC
-    </select>
-    <select id="getCouponProductIds" resultType="java.lang.Integer">
-        SELECT productId FROM cm_coupon_product
-        WHERE couponId = #{couponId}
-        <if test="source == 1">
-            AND pcStatus = 1
-        </if>
-        <if test="source == 2">
-            AND appletsStatus = 1
-        </if>
-    </select>
     <update id="updateCart" parameterType="com.caimei365.order.model.po.CartPo">
         UPDATE cm_cart set
            productCount = #{productCount},

+ 34 - 0
src/main/resources/mapper/OrderCommonMapper.xml

@@ -369,4 +369,38 @@
         FROM logistics_information li
         WHERE li.logisticsBatchID = #{logisticsBatchId}
     </select>
+    <select id="getClubCouponList" resultType="com.caimei365.order.model.vo.CouponVo">
+        SELECT
+        a.id AS clubCouponId,
+        cc.`id` AS couponId,
+        cc.`couponAmount`,
+        cc.`touchPrice`,
+        cc.`startDate`,
+        cc.`endDate`,
+        cc.`couponType`,
+        cc.`userId`,
+        cc.`shopId`,
+        cc.`productType`,
+        cc.`categoryType`
+        FROM cm_coupon_club a
+        LEFT JOIN cm_coupon cc ON a.couponId = cc.id
+        WHERE cc.delFlag = 0
+        AND a.delFlag = 0
+        AND a.userId = #{userId}
+        AND a.status = 1
+        AND NOW() BETWEEN cc.startDate
+        AND cc.endDate
+        AND cc.status != 2
+        ORDER BY a.createDate DESC
+    </select>
+    <select id="getCouponProductIds" resultType="java.lang.Integer">
+        SELECT productId FROM cm_coupon_product
+        WHERE couponId = #{couponId}
+        <if test="source == 1">
+            AND pcStatus = 1
+        </if>
+        <if test="source == 2">
+            AND appletsStatus = 1
+        </if>
+    </select>
 </mapper>