longz
2026-07-17 da9b7a52dbc6fcd2173efef90528d8fccc322554
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.ruoyi.order.service.impl;
 
import com.ruoyi.other.api.domain.TCoupon;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
 
final class ChargingCouponSupport {
    private static final BigDecimal TEN = new BigDecimal("10");
 
    private ChargingCouponSupport() {
    }
 
    static BigDecimal calculateUpfrontDiscount(TCoupon coupon, BigDecimal rechargeAmount) {
        if (coupon == null || rechargeAmount == null || rechargeAmount.compareTo(BigDecimal.ZERO) <= 0) {
            return BigDecimal.ZERO.setScale(2, RoundingMode.HALF_UP);
        }
        if (!Integer.valueOf(1).equals(coupon.getType())) {
            throw new IllegalArgumentException("请选择充电优惠券");
        }
        BigDecimal threshold = coupon.getMeetTheConditions() == null
                ? BigDecimal.ZERO : coupon.getMeetTheConditions();
        if (rechargeAmount.compareTo(threshold) < 0) {
            throw new IllegalArgumentException("预付金额未达到优惠券使用门槛");
        }
 
        BigDecimal discount;
        if (Integer.valueOf(1).equals(coupon.getPreferentialMode())) {
            discount = coupon.getDiscountAmount();
        } else if (Integer.valueOf(2).equals(coupon.getPreferentialMode())) {
            if (coupon.getDiscount() == null) {
                throw new IllegalArgumentException("优惠券折扣配置无效");
            }
            discount = rechargeAmount.multiply(TEN.subtract(coupon.getDiscount()))
                    .divide(TEN, 2, RoundingMode.HALF_UP);
            if (coupon.getMaximumDiscountAmount() != null) {
                discount = discount.min(coupon.getMaximumDiscountAmount());
            }
        } else {
            throw new IllegalArgumentException("优惠券优惠方式无效");
        }
        if (discount == null || discount.compareTo(BigDecimal.ZERO) <= 0) {
            throw new IllegalArgumentException("优惠券优惠金额无效");
        }
        return discount.min(rechargeAmount).setScale(2, RoundingMode.HALF_UP);
    }
}