From 27f7b8f0459ed7c91cd532ae04c9aa3d15d11d84 Mon Sep 17 00:00:00 2001
From: yuxc <653031404@qq.com>
Date: 星期四, 25 五月 2023 17:22:29 +0800
Subject: [PATCH] 接口联调以及接口测试等上传

---
 Source/UBCS/ubcs-service-api/ubcs-util-api/src/main/java/com/vci/ubcs/starter/web/util/BeanUtilForVCI.java |  142 +++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 142 insertions(+), 0 deletions(-)

diff --git a/Source/UBCS/ubcs-service-api/ubcs-util-api/src/main/java/com/vci/ubcs/starter/web/util/BeanUtilForVCI.java b/Source/UBCS/ubcs-service-api/ubcs-util-api/src/main/java/com/vci/ubcs/starter/web/util/BeanUtilForVCI.java
index 880ca54..abaaa10 100644
--- a/Source/UBCS/ubcs-service-api/ubcs-util-api/src/main/java/com/vci/ubcs/starter/web/util/BeanUtilForVCI.java
+++ b/Source/UBCS/ubcs-service-api/ubcs-util-api/src/main/java/com/vci/ubcs/starter/web/util/BeanUtilForVCI.java
@@ -7,9 +7,19 @@
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.beans.BeanUtils;
+import org.springframework.beans.BeanWrapper;
+import org.springframework.beans.BeanWrapperImpl;
 import org.springframework.util.CollectionUtils;
+import org.springframework.util.ObjectUtils;
 
+import java.beans.BeanInfo;
+import java.beans.IntrospectionException;
+import java.beans.Introspector;
+import java.beans.PropertyDescriptor;
 import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.math.BigDecimal;
 import java.util.*;
 
 public class BeanUtilForVCI {
@@ -159,4 +169,136 @@
 			return result;
 		}
 	}
+
+	/**
+	 * 灏嗕竴涓� Map 瀵硅薄杞寲涓轰竴涓� JavaBean
+	 * @param clazz 瑕佽浆鍖栫殑绫诲瀷
+	 * @param map 鍖呭惈灞炴�у�肩殑 map
+	 * @return 杞寲鍑烘潵鐨� JavaBean 瀵硅薄
+	 * @throws IntrospectionException
+	 *             濡傛灉鍒嗘瀽绫诲睘鎬уけ璐�
+	 * @throws IllegalAccessException
+	 *             濡傛灉瀹炰緥鍖� JavaBean 澶辫触
+	 * @throws InstantiationException
+	 *             濡傛灉瀹炰緥鍖� JavaBean 澶辫触
+	 * @throws InvocationTargetException
+	 *             濡傛灉璋冪敤灞炴�х殑 setter 鏂规硶澶辫触
+	 */
+	public static <T>T convertMap(Class<T> clazz, Map map) throws IntrospectionException, IllegalAccessException, InstantiationException, InvocationTargetException {
+		BeanInfo beanInfo = Introspector.getBeanInfo(clazz); // 鑾峰彇绫诲睘鎬�
+		T obj = clazz.newInstance();
+		// 缁� JavaBean 瀵硅薄鐨勫睘鎬ц祴鍊�
+		PropertyDescriptor[] propertyDescriptors =  beanInfo.getPropertyDescriptors();
+		for (int i = 0; i< propertyDescriptors.length; i++) {
+			PropertyDescriptor descriptor = propertyDescriptors[i];
+			String propertyName = descriptor.getName();
+
+			if (map.containsKey(propertyName)) {
+				// 涓嬮潰涓�鍙ュ彲浠� try 璧锋潵锛岃繖鏍峰綋涓�涓睘鎬ц祴鍊煎け璐ョ殑鏃跺�欏氨涓嶄細褰卞搷鍏朵粬灞炴�ц祴鍊笺��
+				Object value = map.get(propertyName);
+
+				Object[] args = new Object[1];
+				args[0] = value;
+				Field privateField = getPrivateField(propertyName, clazz);
+				if (privateField == null) {
+				}
+				privateField.setAccessible(true);
+				String type = privateField.getGenericType().toString();
+				if (type.equals("class java.lang.String")) {
+					privateField.set(obj, value);
+				} else if (type.equals("class java.lang.Boolean")) {
+					privateField.set(obj, Boolean.parseBoolean(String.valueOf(value)));
+				} else if (type.equals("class java.lang.Long")) {
+					privateField.set(obj, Long.parseLong(String.valueOf(value)));
+				} else if (type.equals("class java.lang.Integer")) {
+					privateField.set(obj, Integer.parseInt(String.valueOf(value)));
+				} else if (type.equals("class java.lang.Double")) {
+					privateField.set(obj,Double.parseDouble(String.valueOf(value)));
+				} else if (type.equals("class java.lang.Float")) {
+					privateField.set(obj,Float.parseFloat(String.valueOf(value)));
+				} else if (type.equals("class java.math.BigDecimal")){
+					privateField.set(obj,new BigDecimal(String.valueOf(value)));
+				}//鍙户缁拷鍔犵被鍨�
+			}
+		}
+		return obj;
+	}
+	/*鎷垮埌鍙嶅皠鐖剁被绉佹湁灞炴��*/
+	private static Field getPrivateField(String name, Class cls) {
+		Field declaredField = null;
+		try {
+			declaredField = cls.getDeclaredField(name);
+		} catch (NoSuchFieldException ex) {
+
+			if (cls.getSuperclass() == null) {
+				return declaredField;
+			} else {
+				declaredField = getPrivateField(name, cls.getSuperclass());
+			}
+		}
+		return declaredField;
+	}
+
+	/**
+	 * 鑾峰彇鍒板璞′腑灞炴�т负null鐨勫睘鎬у悕
+	 *
+	 * @param source
+	 * @return
+	 */
+	private static String[] getNullPropertyNames(Object source) {
+		final BeanWrapper src = new BeanWrapperImpl(source);
+		PropertyDescriptor[] pds = src.getPropertyDescriptors();
+
+		Set<String> emptyNames = new HashSet<>();
+		for (PropertyDescriptor pd : pds) {
+			Object srcValue = src.getPropertyValue(pd.getName());
+			if (ObjectUtils.isEmpty(srcValue)) {
+				emptyNames.add(pd.getName());
+			}
+		}
+		String[] result = new String[emptyNames.size()];
+		return emptyNames.toArray(result);
+	}
+
+	/**
+	 * 鎷疯礉闈炵┖瀵硅薄灞炴�у��
+	 *
+	 * @param source
+	 * @param target
+	 */
+	public static void copyPropertiesIgnoreNull(Object source, Object target) {
+		BeanUtils.copyProperties(source, target, getNullPropertyNames(source));
+	}
+
+	/**
+	 * 闆嗗悎鎷疯礉闈炵┖鏁版嵁
+	 *
+	 * @param source       鏁版嵁婧�
+	 * @param target       鐩爣
+	 * @param propertyName 瑕佸尮閰嶇殑灞炴�у悕,渚嬪涓や釜闆嗗悎浣跨敤id杩涜鍖归厤鎷疯礉 propertyName: "id"
+	 */
+	public static void copyListPropertiesIgnoreNull(List<?> source, List<?> target, String propertyName) {
+		if (CollectionUtils.isEmpty(source)) {
+			throw new NullPointerException("copyListPropertiesIgnoreNull source婧愭暟鎹负绌�!");
+		}
+		Map<Object, Object> map = new HashMap<>(source.size());
+		source.forEach(s -> {
+			final BeanWrapper sourceBean = new BeanWrapperImpl(s);
+			Object value = sourceBean.getPropertyValue(propertyName);
+			if (value == null) {
+				throw new NullPointerException("copyListPropertiesIgnoreNull鑾峰彇鍙傛暟寮傚父");
+			}
+			map.put(value, s);
+		});
+		target.forEach(s -> {
+			final BeanWrapper targetBean = new BeanWrapperImpl(s);
+			Object value = targetBean.getPropertyValue(propertyName);
+			if (value == null) {
+				throw new NullPointerException("copyListPropertiesIgnoreNull鑾峰彇鍙傛暟寮傚父");
+			}
+			Object o = map.get(value);
+			copyPropertiesIgnoreNull(o, s);
+		});
+	}
+
 }

--
Gitblit v1.9.3