|
@@ -3,7 +3,10 @@ package com.gqy.common.utils.bean;
|
|
|
import java.beans.BeanInfo;
|
|
|
import java.beans.IntrospectionException;
|
|
|
import java.beans.Introspector;
|
|
|
+import java.lang.reflect.Field;
|
|
|
import java.lang.reflect.Method;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.text.ParseException;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
@@ -32,6 +35,205 @@ public class BeanUtils extends org.springframework.beans.BeanUtils
|
|
|
/** * 匹配setter方法的正则表达式 */
|
|
|
private static final Pattern SET_PATTERN = Pattern.compile("set(\\p{javaUpperCase}\\w*)");
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Map属性填充到Bean对象
|
|
|
+ */
|
|
|
+ public static <T> T mapToBean(Map<String, Object> map, T bean) {
|
|
|
+ if (map == null || bean == null) {
|
|
|
+ return bean;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ Class<?> beanClass = bean.getClass();
|
|
|
+ Field[] fields = beanClass.getDeclaredFields();
|
|
|
+
|
|
|
+ for (Field field : fields) {
|
|
|
+ field.setAccessible(true);
|
|
|
+ String fieldName = field.getName();
|
|
|
+ Object value = map.get(fieldName);
|
|
|
+
|
|
|
+ // 尝试下划线命名
|
|
|
+ if (value == null) {
|
|
|
+ value = map.get(camelToUnderscore(fieldName));
|
|
|
+ }
|
|
|
+
|
|
|
+ if (value != null) {
|
|
|
+ Object convertedValue = convertValueToFieldType(value, field.getType());
|
|
|
+ if (convertedValue != null) {
|
|
|
+ field.set(bean, convertedValue);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return bean;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 转换值到指定的类型,增强的数值处理
|
|
|
+ */
|
|
|
+ private static Object convertValueToFieldType(Object value, Class<?> targetType) {
|
|
|
+ if (value == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ // BigDecimal类型转换增强处理
|
|
|
+ if (targetType == BigDecimal.class) {
|
|
|
+ return toBigDecimal(value);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 其他基本类型转换
|
|
|
+ if (targetType == Integer.class || targetType == int.class) {
|
|
|
+ return toInteger(value);
|
|
|
+ }
|
|
|
+ if (targetType == Long.class || targetType == long.class) {
|
|
|
+ return toLong(value);
|
|
|
+ }
|
|
|
+ if (targetType == Double.class || targetType == double.class) {
|
|
|
+ return toDouble(value);
|
|
|
+ }
|
|
|
+ if (targetType == String.class) {
|
|
|
+ return value.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果值的类型就是目标类型,直接返回
|
|
|
+ if (targetType.isAssignableFrom(value.getClass())) {
|
|
|
+ return value;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 增强的BigDecimal转换,处理各种可能的格式
|
|
|
+ */
|
|
|
+ public static BigDecimal toBigDecimal(Object value) {
|
|
|
+ if (value == null) {
|
|
|
+ return BigDecimal.ZERO;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (value instanceof BigDecimal) {
|
|
|
+ return (BigDecimal) value;
|
|
|
+ }
|
|
|
+
|
|
|
+ String strValue = value.toString().trim();
|
|
|
+
|
|
|
+ // 如果为空字符串,返回0
|
|
|
+ if (strValue.isEmpty()) {
|
|
|
+ return BigDecimal.ZERO;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 处理百分比格式
|
|
|
+ if (strValue.endsWith("%")) {
|
|
|
+ strValue = strValue.substring(0, strValue.length() - 1);
|
|
|
+ return new BigDecimal(strValue).divide(new BigDecimal("100"));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理金额格式,移除货币符号和逗号
|
|
|
+ strValue = strValue.replaceAll("[¥$,]", "")
|
|
|
+ .replaceAll("\\s+", "");
|
|
|
+
|
|
|
+ // 处理科学计数法
|
|
|
+ if (strValue.toLowerCase().contains("e")) {
|
|
|
+ return new BigDecimal(new BigDecimal(strValue).toPlainString());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理普通数字格式
|
|
|
+ return new BigDecimal(strValue);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return BigDecimal.ZERO;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Integer转换
|
|
|
+ */
|
|
|
+ private static Integer toInteger(Object value) {
|
|
|
+ if (value == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (value instanceof Number) {
|
|
|
+ return ((Number) value).intValue();
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ String strValue = value.toString().trim();
|
|
|
+ if (strValue.contains(".")) {
|
|
|
+ return (int) Double.parseDouble(strValue);
|
|
|
+ }
|
|
|
+ return Integer.parseInt(strValue);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Long转换
|
|
|
+ */
|
|
|
+ private static Long toLong(Object value) {
|
|
|
+ if (value == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (value instanceof Number) {
|
|
|
+ return ((Number) value).longValue();
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ String strValue = value.toString().trim();
|
|
|
+ if (strValue.contains(".")) {
|
|
|
+ return (long) Double.parseDouble(strValue);
|
|
|
+ }
|
|
|
+ return Long.parseLong(strValue);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Double转换
|
|
|
+ */
|
|
|
+ private static Double toDouble(Object value) {
|
|
|
+ if (value == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (value instanceof Number) {
|
|
|
+ return ((Number) value).doubleValue();
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return Double.parseDouble(value.toString().trim());
|
|
|
+ } catch (Exception e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 驼峰转下划线
|
|
|
+ */
|
|
|
+ private static String camelToUnderscore(String str) {
|
|
|
+ if (str == null || str.isEmpty()) {
|
|
|
+ return str;
|
|
|
+ }
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for (int i = 0; i < str.length(); i++) {
|
|
|
+ char c = str.charAt(i);
|
|
|
+ if (Character.isUpperCase(c)) {
|
|
|
+ if (i > 0) {
|
|
|
+ sb.append('_');
|
|
|
+ }
|
|
|
+ sb.append(Character.toLowerCase(c));
|
|
|
+ } else {
|
|
|
+ sb.append(c);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* Bean属性复制工具方法。
|
|
|
*
|