package com.engine.salary.util.valid;
import com.engine.salary.exception.SalaryRunTimeException;
import com.engine.salary.util.SalaryI18nUtil;
import org.apache.commons.lang3.StringUtils;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.List;
/**
* 参数校验
*
Copyright: Copyright (c) 2022
* Company: 泛微软件
*
* @author qiantao
* @version 1.0
**/
public class ValidUtil {
/**
* 通过反射来获取javaBean上的注解信息,判断属性值信息,然后通过注解元数据
* 来返回
*
* @param t
*/
public static void doValidator(T t) {
Class> clazz = t.getClass();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
DataCheck rule = field.getDeclaredAnnotation(DataCheck.class);
if (null != rule) {
//字段值
Object value = getValue(t, field.getName());
ValidTypeEnum type = rule.type();
int labelId = rule.labelId();
String message = rule.message();
boolean require = rule.require();
int max = rule.max();
int min = rule.min();
String pattern = rule.pattern();
if (require) {
if (!notNull(value)) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(labelId, message));
}
}
if (ValidTypeEnum.STRING == type) {
//区间值
if (value != null && max != -1) {
String s = String.valueOf(value);
if (s.length() > max) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(labelId, message));
}
}
if (value != null && min != -1) {
String s = String.valueOf(value);
if (s.length() < min) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(labelId, message));
}
}
}
if (ValidTypeEnum.DATE == type) {
if (value != null && StringUtils.isNotBlank(value.toString()) && isValidDate(value.toString(), pattern)) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(labelId, message));
}
}
if (ValidTypeEnum.NUMBER == type) {
if (value instanceof Integer) {
int v = (Integer) value;
if (max != -1) {
if (v > max) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(labelId, message));
}
}
if (min != -1) {
if (v < min) {
throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(labelId, message));
}
}
}
}
}
}
}
private static Object getValue(T t, String fieldName) {
Object value = null;
try {
BeanInfo beanInfo = Introspector.getBeanInfo(t.getClass());
PropertyDescriptor[] props = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : props) {
if (fieldName.equals(property.getName())) {
Method method = property.getReadMethod();
value = method.invoke(t, new Object[]{});
}
}
} catch (Exception e) {
e.printStackTrace();
}
return value;
}
private static boolean notNull(Object value) {
if (null == value) {
return false;
}
if (value instanceof String && isEmpty((String) value)) {
return false;
}
if (value instanceof List && isEmpty((Collection>) value)) {
return false;
}
return true;
}
private static boolean isEmpty(String str) {
return null == str || str.isEmpty();
}
private static boolean isEmpty(Collection> list) {
return null == list || list.isEmpty();
}
/**
* 校验日期
*/
private static boolean isValidDate(String str, String pattern) {
boolean convertSuccess = true;
if (str != null && str.length() == pattern.length()) {
// 指定日期格式为四位年/两位月份/两位日期,注意yyyy/MM/dd区分大小写;
SimpleDateFormat format = new SimpleDateFormat(pattern);
try {
// 设置lenient为false.
// 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01
format.setLenient(false);
Date dateStr = format.parse(str);
} catch (Exception e) {
// 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
convertSuccess = false;
}
} else {
convertSuccess = false;
}
return convertSuccess;
}
}