浏览代码

bugfix-商品结算优惠券

Aslee 3 年之前
父节点
当前提交
1dc6d8254b
共有 1 个文件被更改,包括 24 次插入11 次删除
  1. 24 11
      src/main/java/com/caimei/service/impl/OrderSubmitServiceImpl.java

+ 24 - 11
src/main/java/com/caimei/service/impl/OrderSubmitServiceImpl.java

@@ -58,6 +58,7 @@ public class OrderSubmitServiceImpl implements OrderSubmitService {
         AtomicReference<BigDecimal> totalPrice = new AtomicReference<>(BigDecimal.ZERO);
         List<CartProductVo> totalProducts = new ArrayList<>();
         List<ShopVo> shopList = new ArrayList<>();
+        List<String> productIdList = new ArrayList<>();
         Integer onlinePay = 0;
         if (productId != null && productId > 0) {
             //立即购买
@@ -92,6 +93,7 @@ public class OrderSubmitServiceImpl implements OrderSubmitService {
             BigDecimal shopTotalPrice = BigDecimal.ZERO;
             for (CartProductVo product : shopProducts) {
                 shoppingCartService.setPrice(product, userId, collageFlag);
+                productIdList.add(product.getProductId().toString());
                 shopTotalPrice = MathUtil.add(shopTotalPrice, MathUtil.mul(product.getNum(), product.getPrice()));
             }
             shop.setShopTotalPrice(shopTotalPrice);
@@ -99,8 +101,9 @@ public class OrderSubmitServiceImpl implements OrderSubmitService {
             totalProducts.addAll(shopProducts);
             totalPrice.set(MathUtil.add(totalPrice.get(), shopTotalPrice));
         });
-        // 用户所有可用优惠券
-        List<CouponVo> receiveCouponList = couponMapper.findReceiveCouponList(userId, null, null, 1);
+        String[] productIdArr = productIdList.toArray(new String[0]);
+        // 已领取商品可用优惠券
+        List<CouponVo> receiveCouponList = couponMapper.findReceiveCouponList(userId, productIdArr, null, 1);
         if (null != couponId) {
             // 领券购买对应优惠券领取标识
             AtomicBoolean couponReceiveFlag = new AtomicBoolean(false);
@@ -119,18 +122,23 @@ public class OrderSubmitServiceImpl implements OrderSubmitService {
                 receiveCoupon.setReceiveTime(new Date());
                 receiveCoupon.setDelFlag(0);
                 couponMapper.insertReceiveCoupon(receiveCoupon);
-                // 重新查询已领取优惠券列表
-                receiveCouponList = couponMapper.findReceiveCouponList(userId, null, null, 1);
+                // 重新查询已领取商品可用优惠券
+                receiveCouponList = couponMapper.findReceiveCouponList(userId, productIdArr, null, 1);
                 // 再将该领取的优惠券删除,未提交订单不算真正领取
                 couponMapper.deleteReceiveCoupon(receiveCoupon.getReceiveCouponId());
             }
         }
-        receiveCouponList.forEach(coupon -> {
+        /*ListIterator<CouponVo> iterator = receiveCouponList.listIterator();
+        while (iterator.hasNext()) {
+            CouponVo coupon = iterator.next();
             // 能参与该优惠券的总金额
             BigDecimal touchCouponAmount = BigDecimal.ZERO;
+            // 是否包含优惠券可用商品
+            boolean useFlag = false;
             if (coupon.getProductType() == 1) {
                 //全商城商品
                 touchCouponAmount = totalPrice.get();
+                useFlag = true;
             } else {
                 // 参与优惠券的所有商品id
                 String couponProductIds = couponMapper.getCouponProductIds(coupon.getCouponId());
@@ -138,31 +146,36 @@ public class OrderSubmitServiceImpl implements OrderSubmitService {
                     // 立即购买
                     CartProductVo cartProduct = totalProducts.get(0);
                     if (couponProductIds.contains(cartProduct.getProductId().toString())) {
+                        useFlag = true;
                         touchCouponAmount = totalPrice.get();
                     }
                 } else {
                     // 购物车结算
                     for (CartProductVo product : totalProducts) {
                         if (couponProductIds.contains(product.getProductId().toString())) {
+                            useFlag = true;
                             touchCouponAmount = MathUtil.add(touchCouponAmount, MathUtil.mul(product.getNum(), product.getPrice()));
                         }
                     }
                 }
             }
-            if (coupon.getNoThresholdFlag() == 1 || touchCouponAmount.compareTo(coupon.getTouchPrice()) > 0) {
+            if ((useFlag && coupon.getNoThresholdFlag() == 1) || touchCouponAmount.compareTo(coupon.getTouchPrice()) > 0) {
                 // 满足优惠条件
                 coupon.setTouchFlag(1);
             } else {
                 coupon.setTouchFlag(0);
             }
-        });
+            if (!useFlag) {
+                iterator.remove();
+            }
+        }*/
         receiveCouponList.sort((o1, o2) -> {
             // 按满足优惠条件/优惠金额进行排序
-            if (!o1.getTouchFlag().equals(o2.getTouchFlag())) {
+            /*if (!o1.getTouchFlag().equals(o2.getTouchFlag())) {
                 return o1.getTouchFlag() - o2.getTouchFlag();
-            } else {
-                return o2.getCouponAmount().compareTo(o1.getCouponAmount());
-            }
+            } else {*/
+            return o2.getCouponAmount().compareTo(o1.getCouponAmount());
+//            }
         });
         // 可参与的分享减免活动
         ReductionVo currentReduction = orderSubmitMapper.findCurrentReduction();