| | |
| | | .eq(MemberCoupon::getStatus,Constants.ZERO) |
| | | .eq(MemberCoupon::getMemberId,memberId) |
| | | .apply(" LIMIT_PRICE <= " + totalAmount) |
| | | .apply(" now() between START_DATE and END_DATE ")); |
| | | .apply(" now() between START_DATE and END_DATE ") |
| | | .orderByDesc(MemberCoupon::getMaxPrice) |
| | | .orderByAsc(MemberCoupon::getEndDate) |
| | | ) |
| | | ; |
| | | |
| | | if(CollectionUtils.isNotEmpty(allCoupon)){ |
| | | for (MemberCoupon memberCoupon:allCoupon) { |
| | | //适用类型:0=全场;1=品类;2=指定商品 |
| | | if(this.calculateCouponRata(memberCoupon,goodsCalculateList,totalAmount).compareTo(memberCoupon.getPrice())<=Constants.ZERO){ |
| | | continue; |
| | | } |
| | | memberCouponList.add(memberCoupon); |
| | | /*//适用类型:0=全场;1=品类;2=指定商品 |
| | | if(Constants.equalsInteger(memberCoupon.getApplyType(),Constants.ZERO)){ |
| | | memberCoupon.setValidAmount(memberCoupon.getPrice()); |
| | | memberCoupon.setValidAmount(memberCoupon.getMaxPrice()); |
| | | memberCouponList.add(memberCoupon); |
| | | }else{ |
| | | List<String> applyIdList = Arrays.asList(memberCoupon.getApplyIds().split(",")); |
| | |
| | | isVaildUse(applyIdList,goodsCalculateList,memberCoupon.getApplyType()); |
| | | BigDecimal skuAmount = goodsCalculateList.stream().filter(i->i.getUseCoupon()).map(i->i.getSkuAmount()).reduce(BigDecimal.ZERO,BigDecimal::add); |
| | | if(skuAmount.compareTo(memberCoupon.getLimitPrice())>=Constants.ZERO){ |
| | | //根据金额计算实际折扣金额 |
| | | BigDecimal couponPrice = skuAmount.multiply(memberCoupon.getPrice()).divide(new BigDecimal("100"),2, RoundingMode.HALF_UP); |
| | | //根据金额计算实际折扣金额 PRICE 存储折扣值 例如 5折 存储值 5 |
| | | BigDecimal couponPrice = skuAmount.multiply(memberCoupon.getPrice()).divide(new BigDecimal("10"),2, RoundingMode.HALF_UP); |
| | | //折扣金额大于最大可折扣金额 则使用最大可折扣金额 |
| | | memberCoupon.setValidAmount(couponPrice.compareTo(memberCoupon.getMaxPrice())>Constants.ZERO?memberCoupon.getMaxPrice():couponPrice); |
| | | memberCouponList.add(memberCoupon); |
| | | } |
| | | } |
| | | }*/ |
| | | } |
| | | } |
| | | // 使用 Stream 排序 reversed() 表示倒序 |
| | |
| | | |
| | | |
| | | /** |
| | | * 计算当前已选择优惠券每个商品占比金额 |
| | | * 计算优惠券每个商品占比金额 |
| | | * @param coupon 优惠券信息 |
| | | * @param goodsCalculateList 商品集合 |
| | | * @param totalAmount 订单总金额 |
| | | * @return 可以使用优惠券的商品总金额 |
| | | */ |
| | | public void calculateCouponRata(MemberCoupon coupon,List<OrderGoodsCalculateResponse> goodsCalculateList,BigDecimal totalAmount){ |
| | | public BigDecimal calculateCouponRata(MemberCoupon coupon,List<OrderGoodsCalculateResponse> goodsCalculateList,BigDecimal totalAmount){ |
| | | //总余额 |
| | | BigDecimal validAmount = coupon.getValidAmount(); |
| | | BigDecimal validAmount = coupon.getMaxPrice(); |
| | | //剩余可分配金额 |
| | | BigDecimal surplusValidAmount = validAmount; |
| | | //已用占比比例值 |
| | |
| | | if(!Constants.equalsInteger(coupon.getApplyType(),Constants.ZERO)){ |
| | | List<String> applyIdList = Arrays.asList(coupon.getApplyIds().split(",")); |
| | | isVaildUse(applyIdList,goodsCalculateList,coupon.getApplyType()); |
| | | //Constants.equalsInteger(coupon.getApplyType(),Constants.ONE)?Constants.ONE:Constants.ZERO); |
| | | }else{ |
| | | isVaildUse(null,goodsCalculateList,coupon.getApplyType()); |
| | | } |
| | | //使用了优惠券的商品数量 |
| | | Integer countCouponNum = goodsCalculateList.stream().filter(i->i.getUseCoupon()).collect(Collectors.toList()).size(); |
| | | //使用优惠券的总金额 |
| | | BigDecimal useCouponPrice = goodsCalculateList.stream().filter(i->i.getUseCoupon()) |
| | | .map(i->i.getSkuAmount()).reduce(BigDecimal.ZERO,BigDecimal::add); |
| | | if(Constants.equalsInteger(coupon.getCouponType(),Constants.ONE)){ |
| | | //最大可优惠金额 |
| | | BigDecimal couponAmount = useCouponPrice.multiply(coupon.getPrice()).divide(new BigDecimal("10"),2,BigDecimal.ROUND_DOWN); |
| | | if(couponAmount.compareTo(validAmount)<=Constants.ZERO){ |
| | | validAmount = couponAmount; |
| | | surplusValidAmount = validAmount; |
| | | } |
| | | } |
| | | Integer useCouponNum = Constants.ZERO; |
| | | for (int i = 0; i < goodsCalculateList.size(); i++) { |
| | | if(!goodsCalculateList.get(i).getUseCoupon()){ |
| | |
| | | if(Constants.equalsInteger(useCouponNum+1,countCouponNum)){ |
| | | goodsCalculateList.get(i).setOrderCouponRata(new BigDecimal("1").subtract(rata)); |
| | | goodsCalculateList.get(i).setCouponDeductCash(surplusValidAmount); |
| | | surplusValidAmount = BigDecimal.ZERO; |
| | | break; |
| | | } |
| | | goodsCalculateList.get(i).setOrderCouponRata(goodsCalculateList.get(i).getSkuAmount().divide(totalAmount,2,BigDecimal.ROUND_DOWN)); |
| | |
| | | rata = rata.add(goodsCalculateList.get(i).getOrderCouponRata()); |
| | | useCouponNum = useCouponNum + 1; |
| | | } |
| | | coupon.setValidAmount(validAmount.subtract(surplusValidAmount)); |
| | | return useCouponPrice; |
| | | } |
| | | |
| | | |