教研工具类
注解定义:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* <p>Excel导入字符串中的汉字校验注解</p>
*
* @author
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ExcelChineseValid {
String message() default "导入的字符串中含有汉字";
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* <p>Excel导入Decimal类型校验</p>
*
* @author
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ExcelDecimalValid {
String min();
String max();
String message() default "小数类型数字填写超出范围";
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* * <p>Excel导入字符串长度校验</p>
*
* @author
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ExcelGbkValid {
int min() default 0;
int max() default Integer.MAX_VALUE;
String message() default "文字填写超出长度要求";
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* <p>Excel导入字符串中的汉字校验注解</p>
*
* @author
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ExcelSpecialCharValid {
String message() default "导入的字符串中含有特殊字符";
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* * <p>Excel导入字符串长度校验</p>
*
* @author
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ExcelUtfValid {
int min() default 0;
int max() default Integer.MAX_VALUE;
String message() default "文字填写超出长度要求";
}
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.context.AnalysisContext;
import com.cmbchina.ams.cloud.oms.common.validation.*;
import org.apache.commons.lang3.StringUtils;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.util.regex.Pattern.compile;
/**
* <p>Excel导入字段校验</p>
*
* @author XF
*/
public class ExcelImportValid {
/**
* Excel导入字段校验
*
* @param object 校验的JavaBean 其属性须有自定义注解
* @author
*/
public static void valid(Object object, AnalysisContext context) throws RuntimeException, UnsupportedEncodingException {
Field[] fields = object.getClass().getDeclaredFields();
for (Field field : fields) {
//设置可访问
field.setAccessible(true);
//属性的值
Object fieldValue = null;
try {
fieldValue = field.get(object);
} catch (IllegalAccessException e) {
throw new RuntimeException(String.format("第%s行%s列-"+field.getAnnotation(ExcelValid.class).message()+"有误,请核实", context.readRowHolder().getRowIndex() + 1,field.getAnnotation(ExcelProperty.class).index()+1));
}
//是否包含必填校验注解
boolean isExcelValid = field.isAnnotationPresent(ExcelValid.class);
if (isExcelValid && Objects.isNull(fieldValue)) {
throw new RuntimeException( String.format("第%s行%s列-"+field.getAnnotation(ExcelValid.class).message()+",请核实", context.readRowHolder().getRowIndex() + 1,field.getAnnotation(ExcelProperty.class).index()+1));
}
//utf-8长度校验注解
boolean isExcelUtfValid = field.isAnnotationPresent(ExcelUtfValid.class);
if (isExcelUtfValid && Objects.nonNull(fieldValue)) {
String cellStr = fieldValue.toString();
int strLength = cellStr.getBytes("UTF-8").length;
int length = field.getAnnotation(ExcelUtfValid.class).max();
if (StringUtils.isNotBlank(cellStr) && strLength > length) {
throw new RuntimeException( String.format("第%s行%s列-"+field.getAnnotation(ExcelUtfValid.class).message()+",请核实", context.readRowHolder().getRowIndex() + 1,field.getAnnotation(ExcelProperty.class).index()+1));
}
}
//字符串中是否有汉字校验
boolean isExcelChineseValid = field.isAnnotationPresent(ExcelChineseValid.class);
if (isExcelChineseValid && Objects.nonNull(fieldValue)) {
String cellStr = fieldValue.toString();
Pattern p = compile("[\u4e00-\u9fa5]");
Matcher m = p.matcher(cellStr);
if (m.find()) {
throw new RuntimeException( String.format("第%s行%s列-"+field.getAnnotation(ExcelChineseValid.class).message()+",请核实", context.readRowHolder().getRowIndex() + 1,field.getAnnotation(ExcelProperty.class).index()+1));
}
}
//特殊字符校验
boolean isExcelSpecialCharValid = field.isAnnotationPresent(ExcelSpecialCharValid.class);
if (isExcelSpecialCharValid && Objects.nonNull(fieldValue)) {
String cellStr = fieldValue.toString();
Pattern p = compile("^[\\u4E00-\\u9FA5A-Za-z0-9]+$");
Matcher m = p.matcher(cellStr);
if (!m.find()) {
throw new RuntimeException( String.format("第%s行%s列-"+field.getAnnotation(ExcelSpecialCharValid.class).message()+",请核实", context.readRowHolder().getRowIndex() + 1,field.getAnnotation(ExcelProperty.class).index()+1));
}
}
//GBK长度校验注解
boolean isExcelGbkValid = field.isAnnotationPresent(ExcelGbkValid.class);
if (isExcelGbkValid && Objects.nonNull(fieldValue)) {
String cellStr = fieldValue.toString();
int strLength = cellStr.getBytes("GBK").length;
int length = field.getAnnotation(ExcelGbkValid.class).max();
if (StringUtils.isNotBlank(cellStr) && strLength > length) {
throw new RuntimeException( String.format("第%s行%s列-"+field.getAnnotation(ExcelGbkValid.class).message()+",请核实", context.readRowHolder().getRowIndex() + 1,field.getAnnotation(ExcelProperty.class).index()+1));
}
}
//是否包含int类型校验注解
boolean isExcelIntValid = field.isAnnotationPresent(ExcelIntValid.class);
if (isExcelIntValid) {
if (fieldValue instanceof Integer) {
int cellInt = Integer.parseInt(fieldValue.toString());
int min = field.getAnnotation(ExcelIntValid.class).min();
int max = field.getAnnotation(ExcelIntValid.class).max();
if (cellInt < min || cellInt > max) {
throw new RuntimeException( field.getAnnotation(ExcelIntValid.class).message());
}
}
}
//是否包含decimal类型注解
boolean isExcelDecimalValid = field.isAnnotationPresent(ExcelDecimalValid.class);
if (isExcelDecimalValid && Objects.nonNull(fieldValue)) {
if (Objects.nonNull(fieldValue) && isBigDecimal(fieldValue.toString())) {
BigDecimal cellDecimal = new BigDecimal(fieldValue.toString());
BigDecimal min = new BigDecimal(field.getAnnotation(ExcelDecimalValid.class).min());
BigDecimal max = new BigDecimal(field.getAnnotation(ExcelDecimalValid.class).max());
/* Boolean flag = false;
// 判断是否是整数或者是携带一位或者两位的小数
Pattern pattern = compile("^[+]?([0-9]+(.[0-9]{1,2})?)$");
if (pattern.matcher(cellDecimal.toString()).matches()) {
flag = true;
}*/
int i =cellDecimal.compareTo(min);
if ( cellDecimal.compareTo(min) <= 0 || cellDecimal.compareTo(max) >= 0 ) {
throw new RuntimeException(String.format("第%s行%s列-"+field.getAnnotation(ExcelDecimalValid.class).message()+",请核实", context.readRowHolder().getRowIndex() + 1,field.getAnnotation(ExcelProperty.class).index()+1) );
}
BigDecimal bigDecimal = new BigDecimal(0.01);
int a = cellDecimal.compareTo(new BigDecimal("0.01"));
int b = cellDecimal.compareTo(bigDecimal);
if ( cellDecimal.compareTo(new BigDecimal("0.01")) < 0 ) {
throw new RuntimeException(String.format("第%s行%s列-"+field.getAnnotation(ExcelDecimalValid.class).message()+",请核实", context.readRowHolder().getRowIndex() + 1,field.getAnnotation(ExcelProperty.class).index()+1) );
}
} else {
throw new RuntimeException(String.format("第%s行%s列-"+field.getAnnotation(ExcelDecimalValid.class).message()+",不是小数数字类型请核实", context.readRowHolder().getRowIndex() + 1,field.getAnnotation(ExcelProperty.class).index()+1));
}
}
}
}
private static boolean isBigDecimal(String decimal) {
try {
BigDecimal bd = new BigDecimal(decimal);
return true;
} catch (NumberFormatException e) {
return false;
}
}
}