mirror of
https://github.com/continew-org/continew-admin.git
synced 2025-09-25 06:57:15 +08:00
fix(system/dict): 修复枚举字典初始化时可能存在空字典的问题
This commit is contained in:
@@ -17,6 +17,7 @@
|
|||||||
package top.continew.admin.system.service.impl;
|
package top.continew.admin.system.service.impl;
|
||||||
|
|
||||||
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.collection.CollUtil;
|
||||||
|
import cn.hutool.core.util.ArrayUtil;
|
||||||
import cn.hutool.core.util.ClassUtil;
|
import cn.hutool.core.util.ClassUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import jakarta.annotation.PostConstruct;
|
import jakarta.annotation.PostConstruct;
|
||||||
@@ -40,7 +41,6 @@ import top.continew.starter.extension.crud.service.BaseServiceImpl;
|
|||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 字典项业务实现
|
* 字典项业务实现
|
||||||
@@ -114,6 +114,9 @@ public class DictItemServiceImpl extends BaseServiceImpl<DictItemMapper, DictIte
|
|||||||
*/
|
*/
|
||||||
private List<LabelValueResp> toEnumDict(Class<?> enumClass) {
|
private List<LabelValueResp> toEnumDict(Class<?> enumClass) {
|
||||||
Object[] enumConstants = enumClass.getEnumConstants();
|
Object[] enumConstants = enumClass.getEnumConstants();
|
||||||
|
if (ArrayUtil.isEmpty(enumConstants)) {
|
||||||
|
return List.of();
|
||||||
|
}
|
||||||
return Arrays.stream(enumConstants).map(e -> {
|
return Arrays.stream(enumConstants).map(e -> {
|
||||||
BaseEnum baseEnum = (BaseEnum)e;
|
BaseEnum baseEnum = (BaseEnum)e;
|
||||||
return new LabelValueResp(baseEnum.getDescription(), baseEnum.getValue(), baseEnum.getColor());
|
return new LabelValueResp(baseEnum.getDescription(), baseEnum.getValue(), baseEnum.getColor());
|
||||||
@@ -126,9 +129,14 @@ public class DictItemServiceImpl extends BaseServiceImpl<DictItemMapper, DictIte
|
|||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void init() {
|
public void init() {
|
||||||
Set<Class<?>> classSet = ClassUtil.scanPackageBySuper(projectProperties.getBasePackage(), BaseEnum.class);
|
Set<Class<?>> classSet = ClassUtil.scanPackageBySuper(projectProperties.getBasePackage(), BaseEnum.class);
|
||||||
ENUM_DICT_CACHE.putAll(classSet.stream()
|
for (Class<?> cls : classSet) {
|
||||||
.collect(Collectors.toMap(cls -> StrUtil.toUnderlineCase(cls.getSimpleName())
|
List<LabelValueResp> value = this.toEnumDict(cls);
|
||||||
.toLowerCase(), this::toEnumDict)));
|
if (CollUtil.isEmpty(value)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
String key = StrUtil.toUnderlineCase(cls.getSimpleName()).toLowerCase();
|
||||||
|
ENUM_DICT_CACHE.put(key, value);
|
||||||
|
}
|
||||||
log.debug("枚举字典已缓存到内存:{}", ENUM_DICT_CACHE.keySet());
|
log.debug("枚举字典已缓存到内存:{}", ENUM_DICT_CACHE.keySet());
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user