|
@ -10,12 +10,8 @@ import lombok.Data; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
|
|
|
|
import java.lang.reflect.Field; |
|
|
import java.lang.reflect.Field; |
|
|
import java.lang.reflect.InvocationTargetException; |
|
|
|
|
|
import java.lang.reflect.Method; |
|
|
|
|
|
import java.lang.reflect.ParameterizedType; |
|
|
import java.lang.reflect.ParameterizedType; |
|
|
import java.util.ArrayList; |
|
|
import java.util.*; |
|
|
import java.util.Collection; |
|
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
|
|
@Data |
|
|
@Data |
|
|
@Slf4j |
|
|
@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) { |
|
|
public ConvertUtil<T> invoke(List<?> dataList) { |
|
|
if(this.methodName == null) { |
|
|
if(this.methodNames == null || this.methodNames.length == 0) { |
|
|
String methodName = this.getOnlyOneAnnotation(); |
|
|
String[] methodNames = this.getOnlyOneAnnotation(); |
|
|
if(methodName == null) { |
|
|
if(methodNames == null || methodNames.length == 0) { |
|
|
throw new ServiceException().setMessage("未发现OnlyOne注解属性"); |
|
|
throw new ServiceException().setMessage("未发现OnlyOne注解属性"); |
|
|
} |
|
|
} |
|
|
this.methodName = methodName; |
|
|
this.methodNames = methodNames; |
|
|
} |
|
|
} |
|
|
if(this.subObjectMethodName == null) { |
|
|
if(this.subObjectMethodName == null) { |
|
|
String subObjectMethodName = this.getSubObjectAnnotation(); |
|
|
String subObjectMethodName = this.getSubObjectAnnotation(); |
|
@ -75,20 +71,6 @@ public class ConvertUtil<T> { |
|
|
return this; |
|
|
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 |
|
|
* @param vo |
|
@ -110,37 +92,50 @@ public class ConvertUtil<T> { |
|
|
throw new RuntimeException(e); |
|
|
throw new RuntimeException(e); |
|
|
} |
|
|
} |
|
|
BeanUtil.copyProperties(vo, mainObject); |
|
|
BeanUtil.copyProperties(vo, mainObject); |
|
|
Method setMethod = null; |
|
|
|
|
|
List<Object> subClassList = new ArrayList<>(); |
|
|
List<Object> subClassList = new ArrayList<>(); |
|
|
subClassList.add(subObject); |
|
|
subClassList.add(subObject); |
|
|
try { |
|
|
ReflectUtil.invoke(mainObject, "set" + this.subObjectMethodName, subClassList); |
|
|
setMethod = this.mainClass.getMethod("set" + this.subObjectMethodName, List.class); |
|
|
|
|
|
setMethod.invoke(mainObject, subClassList); |
|
|
|
|
|
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { |
|
|
|
|
|
throw new RuntimeException(e); |
|
|
|
|
|
} |
|
|
|
|
|
dataList.add(mainObject); |
|
|
dataList.add(mainObject); |
|
|
} else {//list中存在主数据,只添加子数据
|
|
|
} else {//list中存在主数据,只添加子数据
|
|
|
Method getMethod = ReflectUtil.getMethod(this.mainClass, "get" + this.subObjectMethodName); |
|
|
List<Object> subClassList = ReflectUtil.invoke(mainObject, "get" + this.subObjectMethodName); |
|
|
List<Object> subClassList = ReflectUtil.invoke(mainObject, getMethod); |
|
|
|
|
|
subClassList.add(subObject); |
|
|
subClassList.add(subObject); |
|
|
Method setMethod = ReflectUtil.getMethod(this.mainClass, "set" + this.subObjectMethodName); |
|
|
ReflectUtil.invoke(mainObject, "set" + this.subObjectMethodName, subClassList); |
|
|
ReflectUtil.invoke(mainObject, setMethod, 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); |
|
|
Field[] fields = this.getAllFields(this.mainClass); |
|
|
|
|
|
List<String> methodList = new ArrayList<>(); |
|
|
for (Field field : fields) { |
|
|
for (Field field : fields) { |
|
|
// 只判断该字段拥有非空注解
|
|
|
// 只判断该字段拥有非空注解
|
|
|
if (field.isAnnotationPresent(OnlyOne.class)) { |
|
|
if (field.isAnnotationPresent(OnlyOne.class)) { |
|
|
//获取属性的名字
|
|
|
//获取属性的名字
|
|
|
String attributeName = field.getName(); |
|
|
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() { |
|
|
private String getSubObjectAnnotation() { |
|
|