1.直接使用hutool自带的 Convert 转换工具

This commit is contained in:
Du YinHong 2026-08-24 14:39:03 +08:00
parent d05dded6b2
commit 768a0d9801

View File

@ -1,5 +1,6 @@
package org.dromara.common.translation.core.impl; package org.dromara.common.translation.core.impl;
import cn.hutool.core.convert.Convert;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.dromara.common.core.service.DictService; import org.dromara.common.core.service.DictService;
import org.dromara.common.core.utils.StreamUtils; import org.dromara.common.core.utils.StreamUtils;
@ -8,7 +9,6 @@ import org.dromara.common.translation.annotation.TranslationType;
import org.dromara.common.translation.constant.TransConstant; import org.dromara.common.translation.constant.TransConstant;
import org.dromara.common.translation.core.TranslationInterface; import org.dromara.common.translation.core.TranslationInterface;
import java.math.BigDecimal;
import java.util.Arrays; import java.util.Arrays;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
@ -34,9 +34,8 @@ public class DictTypeTranslationImpl implements TranslationInterface<String> {
*/ */
@Override @Override
public String translation(Object key, String other) { public String translation(Object key, String other) {
if (StringUtils.isNotBlank(other)) { if (Convert.toStr(key) instanceof String dictValue && StringUtils.isNotBlank(other)) {
String dictValue = convertToString(key); return dictService.getDictLabel(other, dictValue);
return dictValue != null ? dictService.getDictLabel(other, dictValue) : null;
} }
return null; return null;
} }
@ -56,8 +55,7 @@ public class DictTypeTranslationImpl implements TranslationInterface<String> {
Map<String, String> dictMap = dictService.getAllDictByDictType(other); Map<String, String> dictMap = dictService.getAllDictByDictType(other);
Map<Object, String> result = new LinkedHashMap<>(keys.size()); Map<Object, String> result = new LinkedHashMap<>(keys.size());
for (Object key : keys) { for (Object key : keys) {
String dictValue = convertToString(key); if (Convert.toStr(key) instanceof String dictValue) {
if (dictValue != null) {
result.put(key, StreamUtils.join( result.put(key, StreamUtils.join(
StreamUtils.filter(Arrays.asList(dictValue.split(",")), StringUtils::isNotBlank), StreamUtils.filter(Arrays.asList(dictValue.split(",")), StringUtils::isNotBlank),
value -> dictMap.get(value.trim()) value -> dictMap.get(value.trim())
@ -66,28 +64,4 @@ public class DictTypeTranslationImpl implements TranslationInterface<String> {
} }
return result; return result;
} }
/**
* 类型转换为String
* @param key 转换对象
* @return string值
*/
private String convertToString(Object key) {
if (key instanceof String s) {
return s;
} else if (key == null) {
return null;
} else if (key instanceof BigDecimal bd) {
// 规范化数值
return bd.stripTrailingZeros().toPlainString();
} else if (key instanceof Integer || key instanceof Long || key instanceof Short || key instanceof Byte) {
return key.toString();
} else if (key instanceof Double || key instanceof Float) {
// 浮点也用 BigDecimal 规范化
return new BigDecimal(key.toString()).stripTrailingZeros().toPlainString();
} else {
// 其他类型按需处理或干脆返回 null
return key.toString();
}
}
} }