导入统一校验

This commit is contained in:
2022-01-24 17:09:55 +08:00
parent b40371a84f
commit 3f0ad6f923
2 changed files with 155 additions and 1 deletions
@@ -0,0 +1,142 @@
package com.jero.common.util;
import com.jero.common.exception.JeroBootException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
/**
* 手动校验@Vaild方法
* @author liJiaRao
* @date 2021-08-05 15:09
*/
@Slf4j
public class ValidUtil {
private static Validator validator;
static {
validator = Validation.buildDefaultValidatorFactory()
.getValidator();
}
private static Validator validatorAll;
static {
validatorAll = Validation.buildDefaultValidatorFactory()
.getValidator();
}
/**
* 手动校验@Vaild并抛出异常信息
*
* @param object 待校验对象
* @param groups 待校验的组
*/
public static void validate(Object object, Class<?>... groups){
StringBuilder msg = new StringBuilder();
Set<ConstraintViolation<Object>> set = validator.validate(object, groups);
for (ConstraintViolation<Object> constraintViolation : set) {
msg.append(constraintViolation.getMessage()).append("");
}
if (StringUtils.isNotBlank(msg)) {
throw new JeroBootException(msg.substring(0,msg.length()-1));
}
}
/**
* 手动校验@Vaild并抛出异常信息list
*
* @param object 待校验对象
* @param groups 待校验的组
* @return errorList
*/
public static List<String> validateReturnList(Object object, Class<?>... groups){
List<String> list = new ArrayList<>();
Set<ConstraintViolation<Object>> set = validator.validate(object, groups);
for (ConstraintViolation<Object> constraintViolation : set) {
list.add(constraintViolation.getMessage());
}
return list;
}
/**
* 手动校验@Vaild并抛出异常信息list
*
* @param object 待校验对象
* @param groups 待校验的组
* @return errorList
*/
public static List<String> validateReturnListWithNum(Object object, int errorRowNum, Class<?>... groups){
List<String> list = new ArrayList<>();
Set<ConstraintViolation<Object>> set = validator.validate(object, groups);
for (ConstraintViolation<Object> constraintViolation : set) {
list.add("" + errorRowNum + "行:"+constraintViolation.getMessage());
}
return list;
}
/**
* @Description 校验全部数据格式
* @Author lqt
* @Date 2021/8/26
* @Param
* @Return
* @Exception
*/
public static StringBuilder validateAll(Object object){
StringBuilder msg = new StringBuilder();
Set<ConstraintViolation<Object>> set = validatorAll.validate(object);
if(set != null && set.size() >0 ){
for(ConstraintViolation<Object> constraintViolation : set){
// 这里循环获取错误信息,可以自定义格式
msg.append(constraintViolation.getMessage()).append("<br/>");
}
}
return msg;
}
/**
* @Description 校验excel 每行数据格式
* @Author lqt
* @Date 2021/8/26
* @Param count excel 行数
* @Return
* @Exception
*/
public static StringBuilder validateExcel(Object object, int count){
StringBuilder msg = new StringBuilder();
Set<ConstraintViolation<Object>> set = validatorAll.validate(object);
if(set != null && set.size() > 0 ){
for(ConstraintViolation<Object> constraintViolation : set){
// 这里循环获取错误信息,可以自定义格式
msg.append("").append(count).append("行:").append(constraintViolation.getMessage()).append(",请修改。<br/>");
}
}
return msg;
}
/**
* @Description 校验excel 每行数据格式(只用于支出合同导入校验)
* @Author lqt
* @Date 2021/8/26
* @Param count excel 行数
* @Return
* @Exception
*/
public static StringBuilder validateSpendingContractExcel(Object object, int count,int num){
StringBuilder msg = new StringBuilder();
Set<ConstraintViolation<Object>> set = validatorAll.validate(object);
if(set != null && set.size() > 0 ){
for(ConstraintViolation<Object> constraintViolation : set){
// 这里循环获取错误信息,可以自定义格式
msg.append("").append(count).append("行:").append(num).append(constraintViolation.getMessage()).append(",请修改。<br/>");
}
}
return msg;
}
}
@@ -253,9 +253,21 @@ public class ${entityName}Controller {
try {
List<${entityName}Page> list = ExcelImportUtil.importExcel(file.getInputStream(), ${entityName}Page.class, params);
for (${entityName}Page page : list) {
int lineNumber = i + 1;
${entityName} po = new ${entityName}();
BeanUtils.copyProperties(page, po);
${entityName?uncap_first}Service.saveMain(po, <#list subTables as sub>page.get${sub.entityName}List()<#if sub_has_next>,</#if></#list>);
try {
List<String> errorList = ValidUtil.validateReturnList(po);
if (!errorList.isEmpty()){
errorLines++;
errorMessage.add("第 " + lineNumber + " 行:"+ StringUtils.join(errorList,""));
}
${entityName?uncap_first}Service.saveMain(po, <#list subTables as sub>page.get${sub.entityName}List()<#if sub_has_next>,</#if></#list>);
} catch (Exception e) {
// 通过索引名判断出错信息
errorMessage.add("第 " + lineNumber + " 行:未知错误");
log.error(e.getMessage(), e);
}
}
return Result.OK("文件导入成功!数据行数:" + list.size());
} catch (Exception e) {