From b3fc4b140e71d00d9d44a61415c4f398ba751a9c Mon Sep 17 00:00:00 2001 From: liuchen864 <23082234@qq.com> Date: Wed, 1 Nov 2023 14:35:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=A4=9A=E4=B8=AAonlyOne?= =?UTF-8?q?=E6=B3=A8=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../excel/core/util/ConvertUtil.java | 71 +++++++++---------- 1 file changed, 33 insertions(+), 38 deletions(-) diff --git a/win-framework/win-spring-boot-starter-excel/src/main/java/com/win/framework/excel/core/util/ConvertUtil.java b/win-framework/win-spring-boot-starter-excel/src/main/java/com/win/framework/excel/core/util/ConvertUtil.java index 2509c6d2..5053ea23 100644 --- a/win-framework/win-spring-boot-starter-excel/src/main/java/com/win/framework/excel/core/util/ConvertUtil.java +++ b/win-framework/win-spring-boot-starter-excel/src/main/java/com/win/framework/excel/core/util/ConvertUtil.java @@ -10,12 +10,8 @@ import lombok.Data; import lombok.extern.slf4j.Slf4j; import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; +import java.util.*; @Data @Slf4j @@ -29,7 +25,7 @@ public class ConvertUtil { /** * 判断重复的属性方法 */ - private String methodName; + private String[] methodNames; /** * 子类 @@ -55,12 +51,12 @@ public class ConvertUtil { } public ConvertUtil invoke(List dataList) { - if(this.methodName == null) { - String methodName = this.getOnlyOneAnnotation(); - if(methodName == null) { + if(this.methodNames == null || this.methodNames.length == 0) { + String[] methodNames = this.getOnlyOneAnnotation(); + if(methodNames == null || methodNames.length == 0) { throw new ServiceException().setMessage("未发现OnlyOne注解属性"); } - this.methodName = methodName; + this.methodNames = methodNames; } if(this.subObjectMethodName == null) { String subObjectMethodName = this.getSubObjectAnnotation(); @@ -75,20 +71,6 @@ public class ConvertUtil { return this; } - /** - * 判断数据是否存在 - */ - private T checkDataIsExist(Object object) { - for(T obj : dataList) { - T methodValue1 = ReflectUtil.invoke(obj, "get" + this.methodName); - Object methodValue2 = ReflectUtil.invoke(object, "get" + this.methodName); - if(methodValue1.equals(methodValue2)) { - return obj; - } - } - return null; - } - /** * 添加数据 * @param vo @@ -110,37 +92,50 @@ public class ConvertUtil { throw new RuntimeException(e); } BeanUtil.copyProperties(vo, mainObject); - Method setMethod = null; List subClassList = new ArrayList<>(); subClassList.add(subObject); - try { - setMethod = this.mainClass.getMethod("set" + this.subObjectMethodName, List.class); - setMethod.invoke(mainObject, subClassList); - } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { - throw new RuntimeException(e); - } + ReflectUtil.invoke(mainObject, "set" + this.subObjectMethodName, subClassList); dataList.add(mainObject); } else {//list中存在主数据,只添加子数据 - Method getMethod = ReflectUtil.getMethod(this.mainClass, "get" + this.subObjectMethodName); - List subClassList = ReflectUtil.invoke(mainObject, getMethod); + List subClassList = ReflectUtil.invoke(mainObject, "get" + this.subObjectMethodName); subClassList.add(subObject); - Method setMethod = ReflectUtil.getMethod(this.mainClass, "set" + this.subObjectMethodName); - ReflectUtil.invoke(mainObject, setMethod, subClassList); + ReflectUtil.invoke(mainObject, "set" + this.subObjectMethodName, subClassList); + } + } + + /** + * 判断数据是否存在 + */ + private T checkDataIsExist(Object object) { + for(T obj : dataList) { + boolean flag = true; + for(String methodName : this.methodNames) { + T methodValue1 = ReflectUtil.invoke(obj, "get" + methodName); + Object methodValue2 = ReflectUtil.invoke(object, "get" + methodName); + if (!methodValue1.equals(methodValue2)) { + flag = false; + } + } + if(flag) { + return obj; + } } + return null; } - private String getOnlyOneAnnotation() { + private String[] getOnlyOneAnnotation() { Field[] fields = this.getAllFields(this.mainClass); + List methodList = new ArrayList<>(); for (Field field : fields) { // 只判断该字段拥有非空注解 if (field.isAnnotationPresent(OnlyOne.class)) { //获取属性的名字 String attributeName = field.getName(); //将属性名字的首字母大写 - return Character.toUpperCase(attributeName.charAt(0)) + attributeName.substring(1); + methodList.add(Character.toUpperCase(attributeName.charAt(0)) + attributeName.substring(1)); } } - return null; + return methodList.toArray(new String[0]); } private String getSubObjectAnnotation() {