package com.ruoyi.other.service.impl; import java.math.BigDecimal; import java.math.RoundingMode; /** *

* 公司优惠设置校验与规范化(纯逻辑,便于单测) *

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