Browse Source

支持多个onlyOne注解

master
刘忱 2 years ago
parent
commit
b3fc4b140e
  1. 71
      win-framework/win-spring-boot-starter-excel/src/main/java/com/win/framework/excel/core/util/ConvertUtil.java

71
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<T> {
/**
* 判断重复的属性方法
*/
private String methodName;
private String[] methodNames;
/**
* 子类
@ -55,12 +51,12 @@ public class ConvertUtil<T> {
}
public ConvertUtil<T> 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<T> {
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<T> {
throw new RuntimeException(e);
}
BeanUtil.copyProperties(vo, mainObject);
Method setMethod = null;
List<Object> 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<Object> subClassList = ReflectUtil.invoke(mainObject, getMethod);
List<Object> 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<String> 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() {

Loading…
Cancel
Save