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; } }