From 23b2feaf72509e9745ddf567ee3f728be571971f Mon Sep 17 00:00:00 2001 From: gaoqr <13665037151@163.com> Date: Thu, 17 Oct 2024 15:19:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E6=95=B0=E5=AD=97=E5=9E=8B=E5=85=A5=E5=8F=82=E8=83=BD=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/validation/NumberValid.java | 62 ++++++++ .../common/validation/NumberValidator.java | 144 ++++++++++++++++++ 2 files changed, 206 insertions(+) create mode 100644 cf-framework/cf-common/src/main/java/com/cf/imes/framework/common/validation/NumberValid.java create mode 100644 cf-framework/cf-common/src/main/java/com/cf/imes/framework/common/validation/NumberValidator.java diff --git a/cf-framework/cf-common/src/main/java/com/cf/imes/framework/common/validation/NumberValid.java b/cf-framework/cf-common/src/main/java/com/cf/imes/framework/common/validation/NumberValid.java new file mode 100644 index 000000000..c8a7023a4 --- /dev/null +++ b/cf-framework/cf-common/src/main/java/com/cf/imes/framework/common/validation/NumberValid.java @@ -0,0 +1,62 @@ +package com.cf.imes.framework.common.validation; + +import javax.validation.Constraint; +import javax.validation.Payload; +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 自定义Integer校验注解 + * + * @author Gqr + * @since 2024/10/17 10:58 + */ +@Target({ + ElementType.METHOD, + ElementType.FIELD, + ElementType.ANNOTATION_TYPE, + ElementType.CONSTRUCTOR, + ElementType.PARAMETER, + ElementType.TYPE_USE +}) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Constraint( + validatedBy = NumberValidator.class +) +public @interface NumberValid { + /** + * 字段中文名 + * + * @return + */ + String name(); + + /** + * 校验提示信息 + * + * @return + */ + String message() default "数字不合法"; + + Class[] groups() default {}; + + Class[] payload() default {}; + + /** + * 整数最大位数 + * + * @return + */ + int integer() default 5; + + /** + * 小数点后的最大位数 + * + * @return + */ + int fraction() default 3; +} diff --git a/cf-framework/cf-common/src/main/java/com/cf/imes/framework/common/validation/NumberValidator.java b/cf-framework/cf-common/src/main/java/com/cf/imes/framework/common/validation/NumberValidator.java new file mode 100644 index 000000000..b8b80d4f0 --- /dev/null +++ b/cf-framework/cf-common/src/main/java/com/cf/imes/framework/common/validation/NumberValidator.java @@ -0,0 +1,144 @@ +package com.cf.imes.framework.common.validation; + +import cn.hutool.core.util.ObjectUtil; +import org.apache.commons.lang3.StringUtils; + +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorContext; +import java.math.BigDecimal; +import java.text.DecimalFormat; + +/** + * @author Gqr + * @since 2024/10/17 11:27 + */ +public class NumberValidator implements ConstraintValidator { + private static final String ZERO = "0"; + + private static final String GREATER_THAN_ZERO_NOTIFICATION = "%s必须大于等于0"; + + private static final String FLOAT_NUMBER_LENGTH_ERROR_NOTIFICATION = "%s长度不合法,总长度不超过8位,小数点后不超过3位"; + + private String name; + + private int integer; + + private int fraction; + + @Override + public void initialize(NumberValid constraintAnnotation) { + ConstraintValidator.super.initialize(constraintAnnotation); + this.name = constraintAnnotation.name(); + this.integer = constraintAnnotation.integer(); + this.fraction = constraintAnnotation.fraction(); + ; + } + + @Override + public boolean isValid(Number value, ConstraintValidatorContext context) { + // 空的不校验,由原生@NotNull之类的进行判断 + if (ObjectUtil.isNull(value)) { + return true; + } + // 提示信息 + String notification = null; + if (value instanceof Integer) { + notification = handleInteger(value.intValue()); + } else if (value instanceof Long) { + notification = handleLong(value.longValue()); + } else if (value instanceof Double) { + notification = handleDouble(value.doubleValue()); + } else if (value instanceof BigDecimal) { + notification = handleBigDecimal((BigDecimal) value); + } + if (StringUtils.isNotEmpty(notification)) { + context.disableDefaultConstraintViolation(); // 禁用默认的 message 的值 + context.buildConstraintViolationWithTemplate(notification).addConstraintViolation(); + return false; + } + return true; + } + + /** + * 校验整型 + * + * @param value + * @return + */ + private String handleInteger(Integer value) { + if (value < Integer.parseInt(ZERO)) { + return String.format(GREATER_THAN_ZERO_NOTIFICATION, name); + } + return null; + } + + /** + * 校验长整型 + * + * @param value + * @return + */ + private String handleLong(Long value) { + if (value < Long.parseLong(ZERO)) { + return String.format(GREATER_THAN_ZERO_NOTIFICATION, name); + } + return null; + } + + /** + * 校验浮点型 + * + * @param value + * @return + */ + private String handleDouble(Double value) { + if (value <= Double.parseDouble(ZERO)) { + return String.format(GREATER_THAN_ZERO_NOTIFICATION, name); + } + // 处理科学计数法,保留小数点后四位 + DecimalFormat decimalFormat = new DecimalFormat("#.####"); + String valueString = decimalFormat.format(value); + + String[] split = valueString.split("\\."); + // 获取小数点前的位数 + int integerPartLength = split[0].length(); + // 获取小数点后的位数 + int fractionalPartLength = 0; + if (split.length > 2) { + fractionalPartLength = split[1].length(); + } + + // 总位数不超过 8 位,小数点后位数不超过 3 位 + if (integerPartLength > integer || fractionalPartLength > fraction) { + return String.format(FLOAT_NUMBER_LENGTH_ERROR_NOTIFICATION, name); + } + return null; + } + + /** + * 校验大浮点型 + * + * @param value + * @return + */ + private String handleBigDecimal(BigDecimal value) { + if (value.compareTo(BigDecimal.ZERO) <= 0) { + return String.format(GREATER_THAN_ZERO_NOTIFICATION, name); + } + + String[] split = value.toPlainString().split("\\."); + // 获取小数点前的位数 + int integerPartLength = split[0].length(); + // 获取小数点后的位数 + int fractionalPartLength = 0; + if (split.length > 2) { + fractionalPartLength = split[1].length(); + } + + // 总位数不超过 8 位,小数点后位数不超过 3 位 + if (integerPartLength > integer || fractionalPartLength > fraction) { + return String.format(FLOAT_NUMBER_LENGTH_ERROR_NOTIFICATION, name); + } + return null; + } +}