From 7836237d708f168a2b130a052fa587c84ee90183 Mon Sep 17 00:00:00 2001 From: Tyson <32171149+Tyson0314@users.noreply.github.com> Date: Fri, 26 Jun 2020 09:12:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BC=98=E6=83=A0=E5=88=B8?= =?UTF-8?q?=E4=BC=98=E6=83=A0=E9=87=91=E9=A2=9D=E8=AE=A1=E7=AE=97=E9=94=99?= =?UTF-8?q?=E8=AF=AFbug=20(#406)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../db/service/CouponVerifyService.java | 48 +++++++++++-------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/litemall-db/src/main/java/org/linlinjava/litemall/db/service/CouponVerifyService.java b/litemall-db/src/main/java/org/linlinjava/litemall/db/service/CouponVerifyService.java index b3668fd7..7072d67e 100644 --- a/litemall-db/src/main/java/org/linlinjava/litemall/db/service/CouponVerifyService.java +++ b/litemall-db/src/main/java/org/linlinjava/litemall/db/service/CouponVerifyService.java @@ -9,10 +9,7 @@ import org.springframework.stereotype.Service; import java.math.BigDecimal; import java.time.LocalDateTime; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; +import java.util.*; @Service public class CouponVerifyService { @@ -34,7 +31,7 @@ public class CouponVerifyService { */ public LitemallCoupon checkCoupon(Integer userId, Integer couponId, Integer userCouponId, BigDecimal checkedGoodsPrice, List cartList) { LitemallCoupon coupon = couponService.findById(couponId); - if (coupon == null) { + if (coupon == null || coupon.getDeleted()) { return null; } @@ -69,23 +66,36 @@ public class CouponVerifyService { } // 检测商品是否符合 - List goodsList = cartList.stream().map(item -> item.getGoodsId()).collect(Collectors.toList()); - for (LitemallCart cart : cartList) { - goodsList.add(cart.getGoodsId()); - } + Map> cartMap = new HashMap<>(); + //可使用优惠券的商品或分类 List goodsValueList = new ArrayList<>(Arrays.asList(coupon.getGoodsValue())); Short goodType = coupon.getGoodsType(); - if (goodType.equals(CouponConstant.GOODS_TYPE_ARRAY)) { - goodsValueList.retainAll(goodsList); - if (goodsValueList.size() <= 0) { - return null; + + if (goodType.equals(CouponConstant.GOODS_TYPE_CATEGORY) || + goodType.equals((CouponConstant.GOODS_TYPE_ARRAY))) { + for (LitemallCart cart : cartList) { + Integer key = goodType.equals(CouponConstant.GOODS_TYPE_ARRAY) ? cart.getGoodsId() : + goodsService.findById(cart.getGoodsId()).getCategoryId(); + List carts = cartMap.get(key); + if (carts == null) { + carts = new LinkedList<>(); + } + carts.add(cart); + cartMap.put(key, carts); } - } else if (goodType.equals(CouponConstant.GOODS_TYPE_CATEGORY)) { - List categoryList = cartList.stream() - .map(item -> goodsService.findById(item.getGoodsId()) - .getCategoryId()).collect(Collectors.toList()); - goodsValueList.retainAll(categoryList); - if (goodsValueList.size() <= 0) { + //购物车中可以使用优惠券的商品或分类 + goodsValueList.retainAll(cartMap.keySet()); + //可使用优惠券的商品的总价格 + BigDecimal total = new BigDecimal(0); + + for (Integer goodsId : goodsValueList) { + List carts = cartMap.get(goodsId); + for (LitemallCart cart : carts) { + total = total.add(cart.getPrice().multiply(new BigDecimal(cart.getNumber()))); + } + } + //是否达到优惠券满减金额 + if (total.compareTo(coupon.getMin()) == -1) { return null; } }