package com.itgwl.excel.util;
/**
* @description <功能描述>
* @author: 60258 gwl
* @Date: 2023/2/28 16:42
*/
import java.beans.PropertyDescriptor;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.stream.Collector;
import java.util.stream.Stream;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.cglib.core.Converter;
import org.springframework.util.CollectionUtils;
/**
* bean复制工具类
*
* @author 陈春亮
* @version 1.0
* @date 2021/11/29 14:54
*/
public abstract class BeanCopyUtils {
public BeanCopyUtils() {
}
public static <T> List<T> copyList(Class<T> dest, List orig) {
if(CollectionUtils.isEmpty(orig)) {
return new ArrayList();
} else {
BeanCopier beanCopier = BeanCopier.create(orig.get(0).getClass(), dest, false);
ArrayList resultList = new ArrayList(orig.size());
try {
Iterator var4 = orig.iterator();
while(var4.hasNext()) {
Object o = var4.next();
if(o != null) {
T destObject = dest.newInstance();
beanCopier.copy(o, destObject, (Converter)null);
resultList.add(destObject);
}
}
return resultList;
} catch (Exception var7) {
return resultList;
}
}
}
public static <D, O> List<D> copyList(Class<D> dest, List<O> orig, Consumer<D> d) {
if(CollectionUtils.isEmpty(orig)) {
return new ArrayList();
} else {
BeanCopier beanCopier = BeanCopier.create(orig.get(0).getClass(), dest, false);
ArrayList resultList = new ArrayList(orig.size());
try {
Iterator var5 = orig.iterator();
while(var5.hasNext()) {
Object o = var5.next();
if(o != null) {
D destObject = dest.newInstance();
beanCopier.copy(o, destObject, (Converter)null);
if(d != null) {
d.accept(destObject);
}
resultList.add(destObject);
}
}
} catch (Exception var8) {
}
return resultList;
}
}
public static <D, O> List<D> copyList(Class<D> dest, List<O> orig, BiConsumer<O, D> d) {
if(CollectionUtils.isEmpty(orig)) {
return new ArrayList();
} else {
BeanCopier beanCopier = BeanCopier.create(orig.get(0).getClass(), dest, false);
ArrayList resultList = new ArrayList(orig.size());
try {
Iterator var5 = orig.iterator();
while(var5.hasNext()) {
O o = (O) var5.next();
if(o != null) {
D destObject = dest.newInstance();
beanCopier.copy(o, destObject, (Converter)null);
if(d != null) {
d.accept(o, destObject);
}
resultList.add(destObject);
}
}
} catch (Exception var8) {
// log.error("copyList error", var8);
}
return resultList;
}
}
public static <T> List<T> copyListAndDetail(Class<T> dest, List orig, Map<String, String> transformKey) {
return copyListAndDetail(dest, orig, transformKey, (BiConsumer)null);
}
public static <T, C> List<T> copyListAndDetail(Class<T> dest, List orig, Map<String, String> transformKey, BiConsumer<T, C> consumer) {
try {
List<T> resultList = new ArrayList();
if(orig != null && orig.size() > 0) {
Field[] originFields = null;
Field[] destFields = null;
int i = 0;
if(!CollectionUtils.isEmpty(transformKey)) {
originFields = new Field[transformKey.size()];
destFields = new Field[transformKey.size()];
Class origClass = orig.get(0).getClass();
Set<Entry<String, String>> entries = transformKey.entrySet();
for(Iterator iterator = entries.iterator(); iterator.hasNext(); ++i) {
Entry<String, String> entry = (Entry)iterator.next();
String key = (String)entry.getKey();
String value = (String)entry.getValue();
originFields[i] = origClass.getDeclaredField(key);
destFields[i] = dest.getDeclaredField(value);
}
AccessibleObject.setAccessible(originFields, true);
AccessibleObject.setAccessible(destFields, true);
}
Iterator var18 = orig.iterator();
while(true) {
Object o;
Object destObject;
do {
if(!var18.hasNext()) {
if(originFields != null && destFields != null) {
AccessibleObject.setAccessible(originFields, false);
AccessibleObject.setAccessible(destFields, false);
}
return resultList;
}
o = var18.next();
destObject = dest.newInstance();
} while(o == null);
copyProperties(destObject, o);
for(int a = 0; a < i; ++a) {
Field originField = originFields[a];
List o1 = (List)originField.get(o);
Field destField = destFields[a];
Class childClass = (Class)((ParameterizedType)destField.getGenericType()).getActualTypeArguments()[0];
List destList = copyList(childClass, o1);
destField.set(destObject, destList);
}
resultList.add((T) destObject);
}
} else {
return resultList;
}
} catch (Exception var17) {
// log.error("copyListAndDetail error", var17);
return null;
}
}
private static <T, O> List<T> copyList(Class<T> dest, List orig, O out, BiConsumer<O, T> consumer) {
if(CollectionUtils.isEmpty(orig)) {
return new ArrayList();
} else {
BeanCopier beanCopier = BeanCopier.create(orig.get(0).getClass(), dest, false);
ArrayList resultList = new ArrayList(orig.size());
try {
Iterator var6 = orig.iterator();
while(var6.hasNext()) {
Object o = var6.next();
if(o != null) {
T destObject = dest.newInstance();
beanCopier.copy(o, destObject, (Converter)null);
resu