mitao
2 天以前 e8e30e5474c1fd0c14390710066e40c17155ad37
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
package com.ruoyi.system.utils;
 
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.annotation.ExcelValid;
 
import java.lang.reflect.Field;
import java.util.Objects;
public class ExcelImportValid {
    /**
     * Excel导入字段非空校验
     *
     * @param object 校验的JavaBean 其属性须有自定义注解
     */
    public static void valid(Object object) throws Exception {
        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 ServiceException("导入参数检查失败!");
            }
            // 是否包含必填校验注解
            boolean isRequiredValid = field.isAnnotationPresent(ExcelValid.class);
            if (isRequiredValid && Objects.isNull(fieldValue)) {
                throw new ServiceException(field.getAnnotation(ExcelValid.class).message());
            }
        }
    }
}