From 3f0ad6f923b070a84bad245ac063a0e7ef07c6b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9B=85?= <1669139609@qq.com> Date: Mon, 24 Jan 2022 17:09:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=BC=E5=85=A5=E7=BB=9F=E4=B8=80=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/jero/common/util/ValidUtil.java | 142 ++++++++++++++++++ .../controller/${entityName}Controller.javai | 14 +- 2 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/common/util/ValidUtil.java diff --git a/jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/common/util/ValidUtil.java b/jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/common/util/ValidUtil.java new file mode 100644 index 00000000..b8fae457 --- /dev/null +++ b/jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/common/util/ValidUtil.java @@ -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> set = validator.validate(object, groups); + for (ConstraintViolation 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 validateReturnList(Object object, Class... groups){ + List list = new ArrayList<>(); + Set> set = validator.validate(object, groups); + for (ConstraintViolation constraintViolation : set) { + list.add(constraintViolation.getMessage()); + } + return list; + } + + /** + * 手动校验@Vaild并抛出异常信息list + * + * @param object 待校验对象 + * @param groups 待校验的组 + * @return errorList + */ + public static List validateReturnListWithNum(Object object, int errorRowNum, Class... groups){ + List list = new ArrayList<>(); + Set> set = validator.validate(object, groups); + for (ConstraintViolation 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> set = validatorAll.validate(object); + if(set != null && set.size() >0 ){ + for(ConstraintViolation constraintViolation : set){ + // 这里循环获取错误信息,可以自定义格式 + msg.append(constraintViolation.getMessage()).append("
"); + } + } + 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> set = validatorAll.validate(object); + if(set != null && set.size() > 0 ){ + for(ConstraintViolation constraintViolation : set){ + // 这里循环获取错误信息,可以自定义格式 + msg.append("第").append(count).append("行:").append(constraintViolation.getMessage()).append(",请修改。
"); + } + } + 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> set = validatorAll.validate(object); + if(set != null && set.size() > 0 ){ + for(ConstraintViolation constraintViolation : set){ + // 这里循环获取错误信息,可以自定义格式 + msg.append("第").append(count).append("行:").append(num).append(constraintViolation.getMessage()).append(",请修改。
"); + } + } + return msg; + } +} diff --git a/jero-boot/jero-boot-single-startup/src/main/resources/jero/code-template-online/inner-table/onetomany/java/${bussiPackage}/${entityPackage}/controller/${entityName}Controller.javai b/jero-boot/jero-boot-single-startup/src/main/resources/jero/code-template-online/inner-table/onetomany/java/${bussiPackage}/${entityPackage}/controller/${entityName}Controller.javai index 782e23f7..a6631f30 100644 --- a/jero-boot/jero-boot-single-startup/src/main/resources/jero/code-template-online/inner-table/onetomany/java/${bussiPackage}/${entityPackage}/controller/${entityName}Controller.javai +++ b/jero-boot/jero-boot-single-startup/src/main/resources/jero/code-template-online/inner-table/onetomany/java/${bussiPackage}/${entityPackage}/controller/${entityName}Controller.javai @@ -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>,); + try { + List 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>,); + } catch (Exception e) { + // 通过索引名判断出错信息 + errorMessage.add("第 " + lineNumber + " 行:未知错误"); + log.error(e.getMessage(), e); + } } return Result.OK("文件导入成功!数据行数:" + list.size()); } catch (Exception e) {