mirror of
https://gitee.com/dromara/RuoYi-Vue-Plus.git
synced 2026-09-13 07:33:42 +08:00
1.直接使用hutool自带的 Convert 转换工具
This commit is contained in:
parent
d05dded6b2
commit
768a0d9801
@ -1,5 +1,6 @@
|
||||
package org.dromara.common.translation.core.impl;
|
||||
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.dromara.common.core.service.DictService;
|
||||
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.core.TranslationInterface;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
@ -34,9 +34,8 @@ public class DictTypeTranslationImpl implements TranslationInterface<String> {
|
||||
*/
|
||||
@Override
|
||||
public String translation(Object key, String other) {
|
||||
if (StringUtils.isNotBlank(other)) {
|
||||
String dictValue = convertToString(key);
|
||||
return dictValue != null ? dictService.getDictLabel(other, dictValue) : null;
|
||||
if (Convert.toStr(key) instanceof String dictValue && StringUtils.isNotBlank(other)) {
|
||||
return dictService.getDictLabel(other, dictValue);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@ -56,8 +55,7 @@ public class DictTypeTranslationImpl implements TranslationInterface<String> {
|
||||
Map<String, String> dictMap = dictService.getAllDictByDictType(other);
|
||||
Map<Object, String> result = new LinkedHashMap<>(keys.size());
|
||||
for (Object key : keys) {
|
||||
String dictValue = convertToString(key);
|
||||
if (dictValue != null) {
|
||||
if (Convert.toStr(key) instanceof String dictValue) {
|
||||
result.put(key, StreamUtils.join(
|
||||
StreamUtils.filter(Arrays.asList(dictValue.split(",")), StringUtils::isNotBlank),
|
||||
value -> dictMap.get(value.trim())
|
||||
@ -66,28 +64,4 @@ public class DictTypeTranslationImpl implements TranslationInterface<String> {
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user