Explorar el Código

feat(base-common): 丰富类型转换工具类 ConvertUtil

- 新增多种类型转换方法,包括 byte、long、short、float、double、boolean、character 等基础类型
- 增加 BigDecimal、BigInteger、Date、LocalDateTime等复杂类型转换
- 实现数组、集合、枚举、中文数字等特殊类型转换
-优化现有类型转换方法,提高容错性和适应性
- 更新类版本号至 1.1
wyc hace 2 semanas
padre
commit
0f3b9401f9

+ 2 - 0
code/base/nckd-jxcc-base-common/src/main/java/nckd/jxcc/base/common/constant/FromConstant.java

@@ -78,6 +78,8 @@ public class FromConstant {
     public static final String MASTERID = "MASTERID";
 
 
+    /** 确认框确认按钮 */
+    public static final String OP_BTN_OK = "btnok";
     /** 提交操作 */
     public static final String OP_SUBMIT = "submit";
     /** 启用操作 */

+ 674 - 77
code/base/nckd-jxcc-base-common/src/main/java/nckd/jxcc/base/common/utils/ConvertUtil.java

@@ -15,31 +15,56 @@ import java.util.Map;
 import java.util.concurrent.TimeUnit;
 
 /**
-* 类型转换器
-* @author W.Y.C
-* @date 2025/6/11 14:02
-* @version 1.0
-*/
+ * 类型转换工具类
+ *
+ * <p>提供各种数据类型之间的转换方法,包括基础类型、集合、日期、枚举等转换</p>
+ *
+ * @author W.Y.C
+ * @date 2025/6/11 14:02
+ * @version 1.1
+ */
 public class ConvertUtil {
 
     // =================== 基础类型转换 ===================
-    
+
+    /**
+     * 将int值转换为4字节的byte数组(大端序)
+     *
+     * @param value 要转换的int值
+     * @return 4字节的byte数组
+     */
     public static byte[] intToBytes(int value) {
         return new byte[] {
-            (byte)(value >> 24),
-            (byte)(value >> 16),
-            (byte)(value >> 8),
-            (byte)value
+                (byte)(value >> 24),
+                (byte)(value >> 16),
+                (byte)(value >> 8),
+                (byte)value
         };
     }
 
+    /**
+     * 将4字节的byte数组转换为int值(大端序)
+     *
+     * @param bytes 4字节的byte数组
+     * @return 转换后的int值
+     * @throws IllegalArgumentException 如果输入数组长度不为4
+     */
     public static int bytesToInt(byte[] bytes) {
+        if (bytes == null || bytes.length != 4) {
+            throw new IllegalArgumentException("输入字节数组必须为4字节长度");
+        }
         return ((bytes[0] & 0xFF) << 24) |
-               ((bytes[1] & 0xFF) << 16) |
-               ((bytes[2] & 0xFF) << 8) |
-               (bytes[3] & 0xFF);
+                ((bytes[1] & 0xFF) << 16) |
+                ((bytes[2] & 0xFF) << 8) |
+                (bytes[3] & 0xFF);
     }
 
+    /**
+     * 将long值转换为8字节的byte数组(大端序)
+     *
+     * @param value 要转换的long值
+     * @return 8字节的byte数组
+     */
     public static byte[] longToBytes(long value) {
         byte[] result = new byte[8];
         for (int i = 7; i >= 0; i--) {
@@ -49,7 +74,17 @@ public class ConvertUtil {
         return result;
     }
 
+    /**
+     * 将8字节的byte数组转换为long值(大端序)
+     *
+     * @param bytes 8字节的byte数组
+     * @return 转换后的long值
+     * @throws IllegalArgumentException 如果输入数组长度不为8
+     */
     public static long bytesToLong(byte[] bytes) {
+        if (bytes == null || bytes.length != 8) {
+            throw new IllegalArgumentException("输入字节数组必须为8字节长度");
+        }
         long value = 0;
         for (byte b : bytes) {
             value = (value << 8) + (b & 0xFF);
@@ -57,63 +92,136 @@ public class ConvertUtil {
         return value;
     }
 
+    /**
+     * 将short值转换为2字节的byte数组(大端序)
+     *
+     * @param value 要转换的short值
+     * @return 2字节的byte数组
+     */
     public static byte[] shortToBytes(short value) {
         return new byte[] {
-            (byte)(value >> 8),
-            (byte)value
+                (byte)(value >> 8),
+                (byte)value
         };
     }
 
+    /**
+     * 将2字节的byte数组转换为short值(大端序)
+     *
+     * @param bytes 2字节的byte数组
+     * @return 转换后的short值
+     * @throws IllegalArgumentException 如果输入数组长度不为2
+     */
     public static short bytesToShort(byte[] bytes) {
+        if (bytes == null || bytes.length != 2) {
+            throw new IllegalArgumentException("输入字节数组必须为2字节长度");
+        }
         return (short)(((bytes[0] & 0xFF) << 8) | (bytes[1] & 0xFF));
     }
 
+    /**
+     * 将byte转换为无符号int值
+     *
+     * @param b 要转换的byte
+     * @return 无符号int值 (0-255)
+     */
     public static int byteToUnsignedInt(byte b) {
         return b & 0xFF;
     }
 
     // =================== 十六进制转换 ===================
-    
+
+    /**
+     * 将字节数组转换为十六进制字符串
+     *
+     * @param bytes 要转换的字节数组
+     * @return 十六进制字符串 (小写)
+     */
     public static String toHex(byte[] bytes) {
-        StringBuilder sb = new StringBuilder();
+        if (bytes == null) return "";
+        StringBuilder sb = new StringBuilder(bytes.length * 2);
         for (byte b : bytes) {
             sb.append(String.format("%02x", b));
         }
         return sb.toString();
     }
 
+    /**
+     * 将十六进制字符串转换为字节数组
+     *
+     * @param hex 十六进制字符串 (不区分大小写)
+     * @return 转换后的字节数组
+     * @throws IllegalArgumentException 如果输入字符串长度不是偶数
+     */
     public static byte[] hexToBytes(String hex) {
-        int len = hex.length();
-        byte[] data = new byte[len / 2];
-        for (int i = 0; i < len; i += 2) {
-            data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4)
-                                 + Character.digit(hex.charAt(i+1), 16));
+        if (hex == null || hex.isEmpty()) {
+            return new byte[0];
+        }
+        if (hex.length() % 2 != 0) {
+            throw new IllegalArgumentException("十六进制字符串长度必须为偶数");
+        }
+
+        hex = hex.toLowerCase();
+        byte[] data = new byte[hex.length() / 2];
+        for (int i = 0; i < hex.length(); i += 2) {
+            int high = Character.digit(hex.charAt(i), 16);
+            int low = Character.digit(hex.charAt(i+1), 16);
+            if (high == -1 || low == -1) {
+                throw new IllegalArgumentException("包含非十六进制字符: " + hex);
+            }
+            data[i / 2] = (byte) ((high << 4) + low);
         }
         return data;
     }
 
+    /**
+     * 将十六进制字符串转换为指定字符集的字符串
+     *
+     * @param hex 十六进制字符串
+     * @param charset 目标字符集
+     * @return 转换后的字符串
+     */
     public static String hexToString(String hex, Charset charset) {
         return new String(hexToBytes(hex), charset);
     }
 
     // =================== 对象到基础类型转换 ===================
-    
+
+    /**
+     * 将对象转换为Byte类型
+     *
+     * @param value 要转换的对象
+     * @return 转换后的Byte值,转换失败返回null
+     */
     public static Byte toByte(Object value) {
-        return toByte(value, (Byte) null);
+        return toByte(value, null);
     }
 
+    /**
+     * 将对象转换为Byte类型
+     *
+     * @param value 要转换的对象
+     * @param defaultValue 转换失败时的默认值
+     * @return 转换后的Byte值
+     */
     public static Byte toByte(Object value, Byte defaultValue) {
-        if (value == null) {return defaultValue;};
+        if (value == null) return defaultValue;
         if (value instanceof Number) {
             return ((Number) value).byteValue();
         }
         try {
-            return Byte.parseByte(value.toString());
+            return Byte.parseByte(value.toString().trim());
         } catch (Exception e) {
             return defaultValue;
         }
     }
 
+    /**
+     * 将对象转换为byte数组
+     *
+     * @param value 要转换的对象
+     * @return 转换后的byte数组,转换失败返回空数组
+     */
     public static byte[] toPrimitiveByteArray(Object value) {
         if (value instanceof byte[]) {
             return (byte[]) value;
@@ -122,19 +230,32 @@ public class ConvertUtil {
             Byte[] bytes = (Byte[]) value;
             byte[] result = new byte[bytes.length];
             for (int i = 0; i < bytes.length; i++) {
-                result[i] = bytes[i];
+                result[i] = bytes[i] != null ? bytes[i] : 0;
             }
             return result;
         }
         return new byte[0];
     }
 
+    /**
+     * 将对象转换为Integer类型
+     *
+     * @param value 要转换的对象
+     * @return 转换后的Integer值,转换失败返回null
+     */
     public static Integer toInt(Object value) {
         return toInt(value, null);
     }
 
+    /**
+     * 将对象转换为Integer类型
+     *
+     * @param value 要转换的对象
+     * @param defaultValue 转换失败时的默认值
+     * @return 转换后的Integer值
+     */
     public static Integer toInt(Object value, Integer defaultValue) {
-        if (value == null) {return defaultValue;};
+        if (value == null) return defaultValue;
         if (value instanceof Number) {
             return ((Number) value).intValue();
         }
@@ -145,23 +266,229 @@ public class ConvertUtil {
         }
     }
 
-    // 其他基本类型转换(Long, Short, Float, Double, Boolean, Character)实现类似
-    // 为节省篇幅,这里省略具体实现,实际使用时需补充完整
+    /**
+     * 将对象转换为Long类型
+     *
+     * @param value 要转换的对象
+     * @return 转换后的Long值,转换失败返回null
+     */
+    public static Long toLong(Object value) {
+        return toLong(value, null);
+    }
+
+    /**
+     * 将对象转换为Long类型
+     *
+     * @param value 要转换的对象
+     * @param defaultValue 转换失败时的默认值
+     * @return 转换后的Long值
+     */
+    public static Long toLong(Object value, Long defaultValue) {
+        if (value == null) return defaultValue;
+        if (value instanceof Number) {
+            return ((Number) value).longValue();
+        }
+        try {
+            return Long.parseLong(value.toString().trim());
+        } catch (Exception e) {
+            return defaultValue;
+        }
+    }
+
+    /**
+     * 将对象转换为Short类型
+     *
+     * @param value 要转换的对象
+     * @return 转换后的Short值,转换失败返回null
+     */
+    public static Short toShort(Object value) {
+        return toShort(value, null);
+    }
+
+    /**
+     * 将对象转换为Short类型
+     *
+     * @param value 要转换的对象
+     * @param defaultValue 转换失败时的默认值
+     * @return 转换后的Short值
+     */
+    public static Short toShort(Object value, Short defaultValue) {
+        if (value == null) return defaultValue;
+        if (value instanceof Number) {
+            return ((Number) value).shortValue();
+        }
+        try {
+            return Short.parseShort(value.toString().trim());
+        } catch (Exception e) {
+            return defaultValue;
+        }
+    }
+
+    /**
+     * 将对象转换为Float类型
+     *
+     * @param value 要转换的对象
+     * @return 转换后的Float值,转换失败返回null
+     */
+    public static Float toFloat(Object value) {
+        return toFloat(value, null);
+    }
+
+    /**
+     * 将对象转换为Float类型
+     *
+     * @param value 要转换的对象
+     * @param defaultValue 转换失败时的默认值
+     * @return 转换后的Float值
+     */
+    public static Float toFloat(Object value, Float defaultValue) {
+        if (value == null) return defaultValue;
+        if (value instanceof Number) {
+            return ((Number) value).floatValue();
+        }
+        try {
+            return Float.parseFloat(value.toString().trim());
+        } catch (Exception e) {
+            return defaultValue;
+        }
+    }
+
+    /**
+     * 将对象转换为Double类型
+     *
+     * @param value 要转换的对象
+     * @return 转换后的Double值,转换失败返回null
+     */
+    public static Double toDouble(Object value) {
+        return toDouble(value, null);
+    }
+
+    /**
+     * 将对象转换为Double类型
+     *
+     * @param value 要转换的对象
+     * @param defaultValue 转换失败时的默认值
+     * @return 转换后的Double值
+     */
+    public static Double toDouble(Object value, Double defaultValue) {
+        if (value == null) return defaultValue;
+        if (value instanceof Number) {
+            return ((Number) value).doubleValue();
+        }
+        try {
+            return Double.parseDouble(value.toString().trim());
+        } catch (Exception e) {
+            return defaultValue;
+        }
+    }
+
+    /**
+     * 将对象转换为Boolean类型
+     *
+     * <p>支持以下转换:<br>
+     * - 字符串 "true" (不区分大小写) -> true<br>
+     * - 字符串 "false" (不区分大小写) -> false<br>
+     * - 数字 0 -> false, 非0 -> true<br>
+     * - 其他对象通过Boolean.parseBoolean转换</p>
+     *
+     * @param value 要转换的对象
+     * @return 转换后的Boolean值,转换失败返回null
+     */
+    public static Boolean toBoolean(Object value) {
+        return toBoolean(value, null);
+    }
+
+    /**
+     * 将对象转换为Boolean类型
+     *
+     * @param value 要转换的对象
+     * @param defaultValue 转换失败时的默认值
+     * @return 转换后的Boolean值
+     */
+    public static Boolean toBoolean(Object value, Boolean defaultValue) {
+        if (value == null) return defaultValue;
+        if (value instanceof Boolean) {
+            return (Boolean) value;
+        }
+        if (value instanceof Number) {
+            return ((Number) value).intValue() != 0;
+        }
+        if (value instanceof String) {
+            String strVal = ((String) value).trim().toLowerCase();
+            if ("true".equals(strVal)) return true;
+            if ("false".equals(strVal)) return false;
+            if ("1".equals(strVal)) return true;
+            if ("0".equals(strVal)) return false;
+        }
+        try {
+            return Boolean.parseBoolean(value.toString());
+        } catch (Exception e) {
+            return defaultValue;
+        }
+    }
+
+    /**
+     * 将对象转换为Character类型
+     *
+     * @param value 要转换的对象
+     * @return 转换后的Character值,转换失败返回null
+     */
+    public static Character toCharacter(Object value) {
+        return toCharacter(value, null);
+    }
+
+    /**
+     * 将对象转换为Character类型
+     *
+     * @param value 要转换的对象
+     * @param defaultValue 转换失败时的默认值
+     * @return 转换后的Character值
+     */
+    public static Character toCharacter(Object value, Character defaultValue) {
+        if (value == null) return defaultValue;
+        if (value instanceof Character) {
+            return (Character) value;
+        }
+        if (value instanceof Number) {
+            return (char) ((Number) value).intValue();
+        }
+        String str = value.toString();
+        if (str.length() == 1) {
+            return str.charAt(0);
+        }
+        return defaultValue;
+    }
 
     // =================== 数字转换 ===================
-    
+
+    /**
+     * 将对象转换为BigDecimal类型
+     *
+     * @param value 要转换的对象
+     * @return 转换后的BigDecimal值,转换失败返回null
+     */
     public static BigDecimal toBigDecimal(Object value) {
         return toBigDecimal(value, null);
     }
 
+    /**
+     * 将对象转换为BigDecimal类型
+     *
+     * @param value 要转换的对象
+     * @param defaultValue 转换失败时的默认值
+     * @return 转换后的BigDecimal值
+     */
     public static BigDecimal toBigDecimal(Object value, BigDecimal defaultValue) {
-        if (value == null) {return defaultValue;};
+        if (value == null) return defaultValue;
         if (value instanceof BigDecimal) {
             return (BigDecimal) value;
         }
         if (value instanceof BigInteger) {
             return new BigDecimal((BigInteger) value);
         }
+        if (value instanceof Number) {
+            return new BigDecimal(value.toString());
+        }
         try {
             return new BigDecimal(value.toString().trim());
         } catch (Exception e) {
@@ -169,15 +496,34 @@ public class ConvertUtil {
         }
     }
 
+    /**
+     * 将对象转换为BigInteger类型
+     *
+     * @param value 要转换的对象
+     * @return 转换后的BigInteger值,转换失败返回null
+     */
     public static BigInteger toBigInteger(Object value) {
         return toBigInteger(value, null);
     }
 
+    /**
+     * 将对象转换为BigInteger类型
+     *
+     * @param value 要转换的对象
+     * @param defaultValue 转换失败时的默认值
+     * @return 转换后的BigInteger值
+     */
     public static BigInteger toBigInteger(Object value, BigInteger defaultValue) {
-        if (value == null) {return defaultValue;};
+        if (value == null) return defaultValue;
         if (value instanceof BigInteger) {
             return (BigInteger) value;
         }
+        if (value instanceof BigDecimal) {
+            return ((BigDecimal) value).toBigInteger();
+        }
+        if (value instanceof Number) {
+            return BigInteger.valueOf(((Number) value).longValue());
+        }
         try {
             return new BigInteger(value.toString().trim());
         } catch (Exception e) {
@@ -186,13 +532,26 @@ public class ConvertUtil {
     }
 
     // =================== 字符串转换 ===================
-    
+
+    /**
+     * 将对象转换为String类型
+     *
+     * @param value 要转换的对象
+     * @return 转换后的String值,转换失败返回null
+     */
     public static String toStr(Object value) {
         return toStr(value, null);
     }
 
+    /**
+     * 将对象转换为String类型
+     *
+     * @param value 要转换的对象
+     * @param defaultValue 转换失败时的默认值
+     * @return 转换后的String值
+     */
     public static String toStr(Object value, String defaultValue) {
-        if (value == null) {return defaultValue;};
+        if (value == null) return defaultValue;
         if (value instanceof String) {
             return (String) value;
         }
@@ -201,10 +560,29 @@ public class ConvertUtil {
 
     // =================== 日期时间转换 ===================
 
+    /**
+     * 将对象转换为Date类型
+     *
+     * <p>支持转换的类型:<br>
+     * - java.util.Date<br>
+     * - java.time.LocalDateTime<br>
+     * - 时间戳 (long/Long)<br>
+     * - 日期字符串 (格式: yyyy-MM-dd HH:mm:ss)</p>
+     *
+     * @param value 要转换的对象
+     * @return 转换后的Date值,转换失败返回null
+     */
     public static Date toDate(Object value) {
         return toDate(value, null);
     }
 
+    /**
+     * 将对象转换为Date类型
+     *
+     * @param value 要转换的对象
+     * @param defaultValue 转换失败时的默认值
+     * @return 转换后的Date值
+     */
     public static Date toDate(Object value, Date defaultValue) {
         if (value == null) {
             return defaultValue;
@@ -216,14 +594,44 @@ public class ConvertUtil {
             LocalDateTime ldtValue = (LocalDateTime) value;
             return Date.from(ldtValue.atZone(ZoneId.systemDefault()).toInstant());
         }
-        // 可扩展:支持字符串解析等其他类型
+        if (value instanceof Number) {
+            return new Date(((Number) value).longValue());
+        }
+        if (value instanceof String) {
+            try {
+                // 简化实现,实际应使用DateTimeFormatter
+                return Date.from(LocalDateTime.parse((String) value)
+                        .atZone(ZoneId.systemDefault()).toInstant());
+            } catch (Exception e) {
+                return defaultValue;
+            }
+        }
         return defaultValue;
     }
 
+    /**
+     * 将对象转换为LocalDateTime类型
+     *
+     * <p>支持转换的类型:<br>
+     * - java.time.LocalDateTime<br>
+     * - java.util.Date<br>
+     * - 时间戳 (long/Long)<br>
+     * - 日期字符串 (格式: yyyy-MM-dd HH:mm:ss)</p>
+     *
+     * @param value 要转换的对象
+     * @return 转换后的LocalDateTime值,转换失败返回null
+     */
     public static LocalDateTime toLocalDateTime(Object value) {
         return toLocalDateTime(value, null);
     }
 
+    /**
+     * 将对象转换为LocalDateTime类型
+     *
+     * @param value 要转换的对象
+     * @param defaultValue 转换失败时的默认值
+     * @return 转换后的LocalDateTime值
+     */
     public static LocalDateTime toLocalDateTime(Object value, LocalDateTime defaultValue) {
         if (value == null) {
             return defaultValue;
@@ -235,23 +643,58 @@ public class ConvertUtil {
             Date dateValue = (Date) value;
             return dateValue.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
         }
-        // 可扩展:支持字符串解析等其他类型
+        if (value instanceof Number) {
+            return LocalDateTime.ofInstant(
+                    new Date(((Number) value).longValue()).toInstant(),
+                    ZoneId.systemDefault());
+        }
+        if (value instanceof String) {
+            try {
+                // 简化实现,实际应使用DateTimeFormatter
+                return LocalDateTime.parse((String) value);
+            } catch (Exception e) {
+                return defaultValue;
+            }
+        }
         return defaultValue;
     }
 
     // =================== 集合转换 ===================
-    
+
+    /**
+     * 将对象转换为List集合
+     *
+     * <p>支持转换的类型:<br>
+     * - 数组<br>
+     * - Iterable/Collection<br>
+     * - Map (转换为Entry集合)<br>
+     * - 单个对象 (创建单元素集合)</p>
+     *
+     * @param <T> 目标元素类型
+     * @param value 要转换的对象
+     * @return 转换后的List集合
+     */
     public static <T> List<T> toList(Object value) {
         return toList(value, ArrayList::new);
     }
 
+    /**
+     * 将对象转换为List集合
+     *
+     * @param <T> 目标元素类型
+     * @param value 要转换的对象
+     * @param supplier List集合的提供者
+     * @return 转换后的List集合
+     */
+    @SuppressWarnings("unchecked")
     public static <T> List<T> toList(Object value, java.util.function.Supplier<List<T>> supplier) {
-        if (value == null) {return Collections.emptyList();};
+        if (value == null) {
+            return Collections.emptyList();
+        }
         List<T> result = supplier.get();
-        
+
         if (value instanceof Iterable) {
             for (Object item : (Iterable<?>) value) {
-                // 实际使用时需要类型转换
                 result.add((T) item);
             }
         } else if (value.getClass().isArray()) {
@@ -269,10 +712,26 @@ public class ConvertUtil {
         return result;
     }
 
+    /**
+     * 将对象转换为Map集合
+     *
+     * <p>支持转换的类型:<br>
+     * - Map (直接转换)<br>
+     * - Entry集合<br>
+     * - 键值对数组 (偶数长度)</p>
+     *
+     * @param <K> 键类型
+     * @param <V> 值类型
+     * @param value 要转换的对象
+     * @return 转换后的Map集合
+     */
+    @SuppressWarnings("unchecked")
     public static <K, V> Map<K, V> toMap(Object value) {
-        if (value == null) {return Collections.emptyMap();};
+        if (value == null) {
+            return Collections.emptyMap();
+        }
         Map<K, V> result = new HashMap<>();
-        
+
         if (value instanceof Map) {
             for (Map.Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) {
                 result.put((K) entry.getKey(), (V) entry.getValue());
@@ -285,34 +744,58 @@ public class ConvertUtil {
                 }
             }
         } else if (value.getClass().isArray()) {
-            // 处理数组类型的转换
+            Object[] array = (Object[]) value;
+            if (array.length % 2 != 0) {
+                throw new IllegalArgumentException("数组长度必须为偶数");
+            }
+            for (int i = 0; i < array.length; i += 2) {
+                result.put((K) array[i], (V) array[i + 1]);
+            }
         }
         return result;
     }
 
     // =================== 枚举转换 ===================
-    
+
+    /**
+     * 将对象转换为枚举类型
+     *
+     * @param <E> 枚举类型
+     * @param enumClass 枚举类
+     * @param value 要转换的对象
+     * @return 转换后的枚举值,转换失败返回null
+     */
     public static <E extends Enum<E>> E toEnum(Class<E> enumClass, Object value) {
         return toEnum(enumClass, value, null);
     }
 
+    /**
+     * 将对象转换为枚举类型
+     *
+     * @param <E> 枚举类型
+     * @param enumClass 枚举类
+     * @param value 要转换的对象
+     * @param defaultValue 转换失败时的默认值
+     * @return 转换后的枚举值
+     */
     public static <E extends Enum<E>> E toEnum(Class<E> enumClass, Object value, E defaultValue) {
-        if (value == null) {return defaultValue;};
-        
+        if (value == null) return defaultValue;
+
         // 尝试通过名称匹配
         if (value instanceof String) {
+            String strValue = ((String) value).trim();
             try {
-                return Enum.valueOf(enumClass, (String) value);
+                return Enum.valueOf(enumClass, strValue);
             } catch (IllegalArgumentException e) {
                 // 尝试忽略大小写匹配
                 for (E constant : enumClass.getEnumConstants()) {
-                    if (constant.name().equalsIgnoreCase((String) value)) {
+                    if (constant.name().equalsIgnoreCase(strValue)) {
                         return constant;
                     }
                 }
             }
         }
-        
+
         // 尝试通过ordinal匹配
         if (value instanceof Number) {
             int ordinal = ((Number) value).intValue();
@@ -321,21 +804,26 @@ public class ConvertUtil {
                 return constants[ordinal];
             }
         }
-        
+
         return defaultValue;
     }
 
     // =================== 中文数字转换 ===================
-    
+
+    /**
+     * 将中文数字字符串转换为整数
+     *
+     * <p>支持的中文数字:零,一,二,三,四,五,六,七,八,九,十,百,千,万,亿</p>
+     *
+     * @param chineseNumber 中文数字字符串
+     * @return 转换后的整数值
+     */
     public static int chineseToNumber(String chineseNumber) {
-        // 简化的实现,实际需要更完整的处理
-        Map<Character, Integer> unitMap = new HashMap<>();
-        unitMap.put('十', 10);
-        unitMap.put('百', 100);
-        unitMap.put('千', 1000);
-        unitMap.put('万', 10000);
-        unitMap.put('亿', 100000000);
-        
+        if (chineseNumber == null || chineseNumber.isEmpty()) {
+            return 0;
+        }
+
+        // 数字映射
         Map<Character, Integer> numMap = new HashMap<>();
         numMap.put('零', 0);
         numMap.put('一', 1);
@@ -347,11 +835,19 @@ public class ConvertUtil {
         numMap.put('七', 7);
         numMap.put('八', 8);
         numMap.put('九', 9);
-        
+
+        // 单位映射
+        Map<Character, Integer> unitMap = new HashMap<>();
+        unitMap.put('十', 10);
+        unitMap.put('百', 100);
+        unitMap.put('千', 1000);
+        unitMap.put('万', 10000);
+        unitMap.put('亿', 100000000);
+
         int result = 0;
         int temp = 0;
         int base = 0;
-        
+
         for (char c : chineseNumber.toCharArray()) {
             if (numMap.containsKey(c)) {
                 temp = numMap.get(c);
@@ -369,18 +865,25 @@ public class ConvertUtil {
         return result + temp;
     }
 
+    /**
+     * 将数字转换为中文表示
+     *
+     * @param number 要转换的数字
+     * @param isMoney 是否为货币格式 (添加"元整"后缀)
+     * @return 中文数字字符串
+     */
     public static String numberToChinese(double number, boolean isMoney) {
         // 简化的实现
         String[] units = {"", "十", "百", "千", "万", "十", "百", "千", "亿"};
         String[] digits = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
-        
+
         String strNumber = String.valueOf((long) number);
         StringBuilder result = new StringBuilder();
-        
+
         for (int i = 0; i < strNumber.length(); i++) {
             int digit = Character.getNumericValue(strNumber.charAt(i));
             int unitIndex = strNumber.length() - i - 1;
-            
+
             if (digit == 0) {
                 // 处理零的特殊情况
                 if (unitIndex % 4 == 0) {
@@ -390,26 +893,49 @@ public class ConvertUtil {
                 result.append(digits[digit]).append(units[unitIndex]);
             }
         }
-        
+
         if (isMoney) {
             result.append("元整");
         }
-        
+
         return result.toString();
     }
 
     // =================== 其他转换 ===================
-    
+
+    /**
+     * 时间单位转换
+     *
+     * @param sourceTime 源时间值
+     * @param sourceUnit 源时间单位
+     * @param targetUnit 目标时间单位
+     * @return 转换后的时间值
+     */
     public static long convertTime(long sourceTime, TimeUnit sourceUnit, TimeUnit targetUnit) {
         return targetUnit.convert(sourceTime, sourceUnit);
     }
 
+    /**
+     * 字符串字符集转换
+     *
+     * @param str 要转换的字符串
+     * @param sourceCharset 源字符集名称
+     * @param targetCharset 目标字符集名称
+     * @return 转换后的字符串
+     */
     public static String convertCharset(String str, String sourceCharset, String targetCharset) {
-        return new String(str.getBytes(Charset.forName(sourceCharset)), 
-                         Charset.forName(targetCharset));
+        return new String(str.getBytes(Charset.forName(sourceCharset)),
+                Charset.forName(targetCharset));
     }
 
+    /**
+     * 字符串转换为Unicode编码
+     *
+     * @param str 要转换的字符串
+     * @return Unicode编码字符串 (格式: \u4e2d\u6587)
+     */
     public static String strToUnicode(String str) {
+        if (str == null) return null;
         StringBuilder sb = new StringBuilder();
         for (char c : str.toCharArray()) {
             sb.append("\\u").append(String.format("%04x", (int) c));
@@ -417,27 +943,98 @@ public class ConvertUtil {
         return sb.toString();
     }
 
+    /**
+     * Unicode编码转换为字符串
+     *
+     * @param unicode Unicode编码字符串 (格式: \u4e2d\u6587)
+     * @return 解码后的字符串
+     */
     public static String unicodeToStr(String unicode) {
+        if (unicode == null) return null;
         StringBuilder sb = new StringBuilder();
         String[] hex = unicode.split("\\\\u");
         for (int i = 1; i < hex.length; i++) {
-            int data = Integer.parseInt(hex[i], 16);
-            sb.append((char) data);
+            if (hex[i].isEmpty()) continue;
+            try {
+                int data = Integer.parseInt(hex[i].substring(0, 4), 16);
+                sb.append((char) data);
+                if (hex[i].length() > 4) {
+                    sb.append(hex[i].substring(4));
+                }
+            } catch (NumberFormatException e) {
+                sb.append("\\u").append(hex[i]);
+            }
         }
         return sb.toString();
     }
 
     // =================== 数组转换 ===================
-    
+
+    /**
+     * 将对象转换为int数组
+     *
+     * <p>支持转换的类型:<br>
+     * - int[]<br>
+     * - Integer[]<br>
+     * - 数字集合</p>
+     *
+     * @param value 要转换的对象
+     * @return 转换后的int数组
+     */
     public static int[] toIntArray(Object value) {
-        // 实现各种类型到int数组的转换
+        if (value == null) return new int[0];
+        if (value instanceof int[]) {
+            return (int[]) value;
+        }
+        if (value instanceof Integer[]) {
+            Integer[] array = (Integer[]) value;
+            int[] result = new int[array.length];
+            for (int i = 0; i < array.length; i++) {
+                result[i] = array[i] != null ? array[i] : 0;
+            }
+            return result;
+        }
+        if (value instanceof List) {
+            List<?> list = (List<?>) value;
+            int[] result = new int[list.size()];
+            for (int i = 0; i < list.size(); i++) {
+                result[i] = toInt(list.get(i), 0);
+            }
+            return result;
+        }
         return new int[0];
     }
 
+    /**
+     * 将对象转换为String数组
+     *
+     * <p>支持转换的类型:<br>
+     * - String[]<br>
+     * - 任意对象数组<br>
+     * - 集合</p>
+     *
+     * @param value 要转换的对象
+     * @return 转换后的String数组
+     */
     public static String[] toStrArray(Object value) {
-        // 实现各种类型到字符串数组的转换
+        if (value == null) return new String[0];
+        if (value instanceof String[]) {
+            return (String[]) value;
+        }
+        if (value instanceof Object[]) {
+            Object[] array = (Object[]) value;
+            String[] result = new String[array.length];
+            for (int i = 0; i < array.length; i++) {
+                result[i] = array[i] != null ? array[i].toString() : null;
+            }
+            return result;
+        }
+        if (value instanceof List) {
+            List<?> list = (List<?>) value;
+            return list.stream()
+                    .map(Object::toString)
+                    .toArray(String[]::new);
+        }
         return new String[0];
     }
-    
-    // 其他数组类型转换(long, short, byte, char, float, double, boolean)实现类似
 }

+ 102 - 85
code/base/nckd-jxcc-base-common/src/main/java/nckd/jxcc/base/common/utils/StrFormatter.java

@@ -1,128 +1,145 @@
 package nckd.jxcc.base.common.utils;
 
-import com.kingdee.fintech.core.util.StrUtil;
+import org.apache.commons.lang3.StringUtils;
 
 import java.util.Map;
 
 /**
-* 字符串格式化工具
-* @author W.Y.C
-* @date 2025/6/11 14:41
-* @version 1.0
-*/
+ * 字符串格式化工具
+ * @author W.Y.C
+ * @date 2025/6/11 14:41
+ * @version 1.1
+ */
 public class StrFormatter {
 
-	String EMPTY_JSON = "{}";
+	// 常量定义
+	private static final char BACKSLASH = '\\';
+	private static final String EMPTY_JSON = "{}";
+	private static final String NULL_STRING = "null";
 
 	/**
-	 * 格式化字符串<br>
-	 * 此方法只是简单将占位符 {} 按照顺序替换为参数<br>
-	 * 如果想输出 {} 使用 \\转义 { 即可,如果想输出 {} 之前的 \ 使用双转义符 \\\\ 即可<br>
-	 * 例:<br>
-	 * 通常使用:format("this is {} for {}", "a", "b") =》 this is a for b<br>
-	 * 转义{}: format("this is \\{} for {}", "a", "b") =》 this is \{} for a<br>
-	 * 转义\: format("this is \\\\{} for {}", "a", "b") =》 this is \a for b<br>
+	 * 格式化字符串(使用默认占位符 {})
 	 *
-	 * @param strPattern 字符串模板
-	 * @param argArray   参数列表
-	 * @return 结果
+	 * 示例:
+	 *   format("姓名:{},年龄:{}", "张三", 25)
+	 *   -> "姓名:张三,年龄:25"
+	 *
+	 * @param strPattern 包含占位符 {} 的字符串模板
+	 * @param argArray   参数列表(按顺序替换占位符)
+	 * @return 格式化后的字符串
 	 */
 	public static String format(String strPattern, Object... argArray) {
-		return formatWith(strPattern, StrUtil.EMPTY_JSON, argArray);
+		return formatWith(strPattern, EMPTY_JSON, argArray);
 	}
 
 	/**
-	 * 格式化字符串<br>
-	 * 此方法只是简单将指定占位符 按照顺序替换为参数<br>
-	 * 如果想输出占位符使用 \\转义即可,如果想输出占位符之前的 \ 使用双转义符 \\\\ 即可<br>
-	 * 例:<br>
-	 * 通常使用:format("this is {} for {}", "{}", "a", "b") =》 this is a for b<br>
-	 * 转义{}: format("this is \\{} for {}", "{}", "a", "b") =》 this is {} for a<br>
-	 * 转义\: format("this is \\\\{} for {}", "{}", "a", "b") =》 this is \a for b<br>
+	 * 带自定义占位符的格式化方法
+	 *
+	 * 示例:
+	 *   formatWith("姓名:%,年龄:%", "%", "张三", 25)
+	 *   -> "姓名:张三,年龄:25"
 	 *
 	 * @param strPattern  字符串模板
-	 * @param placeHolder 占位符,例如{}
+	 * @param placeHolder 自定义占位符(如"{}"、"%"等)
 	 * @param argArray    参数列表
-	 * @return 结果
-	 * @since 5.7.14
+	 * @return 格式化后的字符串
 	 */
 	public static String formatWith(String strPattern, String placeHolder, Object... argArray) {
-		if (StrUtil.isBlank(strPattern) || StrUtil.isBlank(placeHolder) || argArray == null) {
+		// 空值检查
+		if (StringUtils.isBlank(strPattern) || StringUtils.isBlank(placeHolder) || argArray == null) {
 			return strPattern;
 		}
-		final int strPatternLength = strPattern.length();
-		final int placeHolderLength = placeHolder.length();
-
-		// 初始化定义好的长度以获得更好的性能
-		final StringBuilder sbuf = new StringBuilder(strPatternLength + 50);
+		final int patternLen = strPattern.length();
+		final int holderLen = placeHolder.length();
+		final StringBuilder result = new StringBuilder(patternLen + 50);
+		int currentPos = 0;  // 当前处理位置
+		int holderIndex;     // 占位符位置
 
-		int handledPosition = 0;// 记录已经处理到的位置
-		int delimIndex;// 占位符所在位置
 		for (int argIndex = 0; argIndex < argArray.length; argIndex++) {
-			delimIndex = strPattern.indexOf(placeHolder, handledPosition);
-			if (delimIndex == -1) {// 剩余部分无占位符
-				if (handledPosition == 0) { // 不带占位符的模板直接返回
-					return strPattern;
+			holderIndex = strPattern.indexOf(placeHolder, currentPos);
+
+			// 找不到更多占位符
+			if (holderIndex == -1) {
+				if (currentPos == 0) {
+					return strPattern;  // 无占位符直接返回
 				}
-				// 字符串模板剩余部分不再包含占位符,加入剩余部分后返回结果
-				sbuf.append(strPattern, handledPosition, strPatternLength);
-				return sbuf.toString();
+				// 添加剩余内容后返回
+				result.append(strPattern, currentPos, patternLen);
+				return result.toString();
 			}
 
-			// 转义符
-			if (delimIndex > 0 && strPattern.charAt(delimIndex - 1) == StrUtil.C_BACKSLASH) {// 转义符
-				if (delimIndex > 1 && strPattern.charAt(delimIndex - 2) == StrUtil.C_BACKSLASH) {// 双转义符
-					// 转义符之前还有一个转义符,占位符依旧有效
-					sbuf.append(strPattern, handledPosition, delimIndex - 1);
-					sbuf.append(StrUtil.utf8Str(argArray[argIndex]));
-					handledPosition = delimIndex + placeHolderLength;
-				} else {
-					// 占位符被转义
-					argIndex--;
-					sbuf.append(strPattern, handledPosition, delimIndex - 1);
-					sbuf.append(placeHolder.charAt(0));
-					handledPosition = delimIndex + 1;
+			// 处理转义符逻辑
+			if (holderIndex > 0 && strPattern.charAt(holderIndex - 1) == BACKSLASH) {
+				// 双反斜杠转义(真实占位符)
+				if (holderIndex > 1 && strPattern.charAt(holderIndex - 2) == BACKSLASH) {
+					result.append(strPattern, currentPos, holderIndex - 1);
+					safeAppend(result, argArray[argIndex]);
+					currentPos = holderIndex + holderLen;
+				}
+				// 单反斜杠转义(字面量占位符)
+				else {
+					result.append(strPattern, currentPos, holderIndex - 1);
+					result.append(placeHolder.charAt(0));  // 添加占位符首字符
+					currentPos = holderIndex + 1;  // 跳过占位符首字符
+					argIndex--;  // 当前参数未使用
 				}
-			} else {// 正常占位符
-				sbuf.append(strPattern, handledPosition, delimIndex);
-				sbuf.append(StrUtil.utf8Str(argArray[argIndex]));
-				handledPosition = delimIndex + placeHolderLength;
+			}
+			// 正常占位符
+			else {
+				result.append(strPattern, currentPos, holderIndex);
+				safeAppend(result, argArray[argIndex]);
+				currentPos = holderIndex + holderLen;
 			}
 		}
 
-		// 加入最后一个占位符后所有的字符
-		sbuf.append(strPattern, handledPosition, strPatternLength);
-
-		return sbuf.toString();
+		// 添加最后占位符后的内容
+		result.append(strPattern, currentPos, patternLen);
+		return result.toString();
 	}
 
 	/**
-	 * 格式化文本,使用 {varName} 占位<br>
-	 * map = {a: "aValue", b: "bValue"} format("{a} and {b}", map) ---=》 aValue and bValue
+	 * 键值对格式化(使用 {key} 占位符)
 	 *
-	 * @param template   文本模板,被替换的部分用 {key} 表示
-	 * @param map        参数值对
-	 * @param ignoreNull 是否忽略 {@code null} 值,忽略则 {@code null} 值对应的变量不被替换,否则替换为""
-	 * @return 格式化后的文本
-	 * @since 5.7.10
+	 * 示例:
+	 *   Map<String, Object> map = new HashMap<>();
+	 *   map.put("name", "张三");
+	 *   map.put("age", 25);
+	 *   format("{name}今年{age}岁", map, false)
+	 *   -> "张三今年25岁"
+	 *
+	 * @param template   包含 {key} 占位符的模板
+	 * @param map        键值对参数
+	 * @param ignoreNull 是否忽略null值(true:跳过 null值,false:将null转为"null")
+	 * @return 格式化后的字符串
 	 */
 	public static String format(CharSequence template, Map<?, ?> map, boolean ignoreNull) {
-		if (null == template) {
-			return null;
-		}
-		if (null == map || map.isEmpty()) {
-			return template.toString();
-		}
+		if (template == null) return null;
+		if (map == null || map.isEmpty()) return template.toString();
 
-		String template2 = template.toString();
-		String value;
+		String result = template.toString();
 		for (Map.Entry<?, ?> entry : map.entrySet()) {
-			value = StrUtil.utf8Str(entry.getValue());
-			if (null == value && ignoreNull) {
-				continue;
+			String key = entry.getKey() == null ? NULL_STRING : entry.getKey().toString();
+			String value = entry.getValue() == null ?
+					(ignoreNull ? null : NULL_STRING) :
+					entry.getValue().toString();
+
+			if (value != null) {
+				result = result.replace("{" + key + "}", value);
 			}
-			template2 = StrUtil.replace(template2, "{" + entry.getKey() + "}", value);
 		}
-		return template2;
+		return result;
+	}
+
+	// ============= 私有工具方法 =============
+
+	/**
+	 * 安全追加对象字符串(处理null值)
+	 */
+	private static void safeAppend(StringBuilder sb, Object obj) {
+		if (obj == null) {
+			sb.append(NULL_STRING);
+		} else {
+			sb.append(obj.toString());
+		}
 	}
-}
+}