yanderong
6 天以前 8eb27484bbda096906fe0f5dd49c67b50099c59e
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.ruoyi.other.service.impl;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
 
/**
 * <p>
 * 公司优惠设置校验与规范化(纯逻辑,便于单测)
 * </p>
 *
 * @author mxcd
 * @since 2026-09-28
 */
final class CompanyPreferentialSupport {
 
    /** 优惠方式:不优惠 */
    static final int TYPE_NONE = 1;
 
    /** 优惠方式:按比例优惠 */
    static final int TYPE_RATE = 2;
 
    /** 优惠方式:按优惠单价收费(需绑定计费模板) */
    static final int TYPE_TEMPLATE = 3;
 
    /** 服务费优惠比例上限(%,与前端 InputNumber 上限保持一致) */
    static final BigDecimal MAX_DISCOUNT_RATE = new BigDecimal("100");
 
    /** 比例保留小数位(与 t_company_preferential.service_discount_rate 一致) */
    private static final int RATE_SCALE = 2;
 
    private CompanyPreferentialSupport() {
    }
 
    /**
     * 校验优惠方式(必填,1=不优惠,2=按比例优惠,3=按优惠单价收费)
     */
    static int normalizePreferentialType(Integer preferentialType) {
        if (preferentialType == null) {
            throw new IllegalArgumentException("请选择优惠方式");
        }
        if (preferentialType != TYPE_NONE && preferentialType != TYPE_RATE && preferentialType != TYPE_TEMPLATE) {
            throw new IllegalArgumentException("优惠方式不合法");
        }
        return preferentialType;
    }
 
    /**
     * 校验并规范化服务费优惠比例:按比例优惠时必填(0~100,保留2位小数),其他方式返回 null
     */
    static BigDecimal normalizeDiscountRate(int preferentialType, BigDecimal serviceDiscountRate) {
        if (preferentialType != TYPE_RATE) {
            return null;
        }
        if (serviceDiscountRate == null) {
            throw new IllegalArgumentException("请输入服务费优惠比例");
        }
        if (serviceDiscountRate.compareTo(BigDecimal.ZERO) < 0
                || serviceDiscountRate.compareTo(MAX_DISCOUNT_RATE) > 0) {
            throw new IllegalArgumentException("服务费优惠比例需在0~100之间");
        }
        return serviceDiscountRate.setScale(RATE_SCALE, RoundingMode.HALF_UP);
    }
 
    /**
     * 校验计费模板:按优惠单价收费时必填,其他方式返回 null
     */
    static Integer normalizeBillingTemplateId(int preferentialType, Integer billingTemplateId) {
        if (preferentialType != TYPE_TEMPLATE) {
            return null;
        }
        if (billingTemplateId == null) {
            throw new IllegalArgumentException("请选择计费模板");
        }
        if (billingTemplateId <= 0) {
            throw new IllegalArgumentException("计费模板不合法");
        }
        return billingTemplateId;
    }
 
}