parent
a980b290bb
commit
54710755df
|
@ -30,6 +30,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||
|
||||
/**
|
||||
* 数据字典临时缓存
|
||||
*
|
||||
* @author Mazc@dibo.ltd
|
||||
* @version v2.0
|
||||
* @date 2020/02/22
|
||||
|
@ -54,19 +55,43 @@ public class DictTempCache {
|
|||
private static Map<String, List<String>> MODEL_DICTS = new HashMap<>();
|
||||
|
||||
/**
|
||||
* 刷新model字典缓存
|
||||
* 刷新指定泛型model字典缓存
|
||||
*
|
||||
* @param modelClass
|
||||
* @param <T>
|
||||
*/
|
||||
public static <T extends BaseExcelModel> void refreshDictCache(Class<T> modelClass){
|
||||
public static <T extends BaseExcelModel> void refreshGenericDictCache(Class<T> modelClass) {
|
||||
// 无字典model
|
||||
if(NO_DICT_MODELS.contains(modelClass.getName())){
|
||||
if (NO_DICT_MODELS.contains(modelClass.getName())) {
|
||||
return;
|
||||
}
|
||||
List<String> dictTypes = extractGenericDictTypes(modelClass);
|
||||
if (V.notEmpty(dictTypes)) {
|
||||
for (String dictType : dictTypes) {
|
||||
if (DICT_TYPE_ITEMS_MAP.containsKey(dictType) == false || isExpired(DICT_TYPE_TIMESTAMP_MAP.get(dictType)) == true) {
|
||||
List<KeyValue> list = ContextHelper.getBean(DictionaryService.class).getKeyValueList(dictType);
|
||||
Map<String, Object> name2ValueMap = BeanUtils.convertKeyValueList2Map(list);
|
||||
DICT_TYPE_ITEMS_MAP.put(dictType, name2ValueMap);
|
||||
DICT_TYPE_TIMESTAMP_MAP.put(dictType, System.currentTimeMillis());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新model字典缓存
|
||||
*
|
||||
* @param modelClass
|
||||
*/
|
||||
public static void refreshDictCache(Class<?> modelClass) {
|
||||
// 无字典model
|
||||
if (NO_DICT_MODELS.contains(modelClass.getName())) {
|
||||
return;
|
||||
}
|
||||
List<String> dictTypes = extractDictTypes(modelClass);
|
||||
if(V.notEmpty(dictTypes)){
|
||||
for(String dictType : dictTypes){
|
||||
if(DICT_TYPE_ITEMS_MAP.containsKey(dictType) == false || isExpired(DICT_TYPE_TIMESTAMP_MAP.get(dictType)) == true){
|
||||
if (V.notEmpty(dictTypes)) {
|
||||
for (String dictType : dictTypes) {
|
||||
if (DICT_TYPE_ITEMS_MAP.containsKey(dictType) == false || isExpired(DICT_TYPE_TIMESTAMP_MAP.get(dictType)) == true) {
|
||||
List<KeyValue> list = ContextHelper.getBean(DictionaryService.class).getKeyValueList(dictType);
|
||||
Map<String, Object> name2ValueMap = BeanUtils.convertKeyValueList2Map(list);
|
||||
DICT_TYPE_ITEMS_MAP.put(dictType, name2ValueMap);
|
||||
|
@ -78,36 +103,44 @@ public class DictTempCache {
|
|||
|
||||
/**
|
||||
* 获取字典值
|
||||
*
|
||||
* @param dictType
|
||||
* @param dictName
|
||||
* @return
|
||||
*/
|
||||
public static String getDictValue(String dictType, String dictName){
|
||||
public static String getDictValue(String dictType, String dictName) {
|
||||
Map<String, Object> map = DICT_TYPE_ITEMS_MAP.get(dictType);
|
||||
if(map == null){
|
||||
log.warn("无法找到数据字典定义: "+dictType);
|
||||
if (map == null) {
|
||||
log.warn("无法找到数据字典定义: " + dictType);
|
||||
return dictName;
|
||||
}
|
||||
if(map.get(dictName) == null){
|
||||
if (map.get(dictName) == null) {
|
||||
return null;
|
||||
}
|
||||
return (String)map.get(dictName);
|
||||
return (String) map.get(dictName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取字典名label
|
||||
*
|
||||
* @param dictType
|
||||
* @param dictValue
|
||||
* @param modelClass
|
||||
* @return
|
||||
*/
|
||||
public static String getDictLabel(String dictType, String dictValue) throws Exception{
|
||||
public static <T extends BaseExcelModel> String getDictLabel(String dictType, String dictValue, Class<?> modelClass) throws Exception {
|
||||
Map<String, Object> map = DICT_TYPE_ITEMS_MAP.get(dictType);
|
||||
if(map == null){
|
||||
log.warn("无法找到数据字典定义: "+dictType);
|
||||
return dictValue;
|
||||
if (map == null) {
|
||||
//加载当前类
|
||||
refreshDictCache(modelClass);
|
||||
map = DICT_TYPE_ITEMS_MAP.get(dictType);
|
||||
if (map == null) {
|
||||
log.warn("无法找到数据字典定义: " + dictType);
|
||||
return dictValue;
|
||||
}
|
||||
}
|
||||
for( Map.Entry<String, Object> entry : map.entrySet()){
|
||||
if(dictValue.equals(entry.getValue())){
|
||||
for (Map.Entry<String, Object> entry : map.entrySet()) {
|
||||
if (dictValue.equals(entry.getValue())) {
|
||||
return entry.getKey();
|
||||
}
|
||||
}
|
||||
|
@ -116,29 +149,58 @@ public class DictTempCache {
|
|||
|
||||
/**
|
||||
* 提取注解绑定
|
||||
*
|
||||
* @param modelClass
|
||||
* @param <T>
|
||||
* @return
|
||||
*/
|
||||
private static <T extends BaseExcelModel> List<String> extractDictTypes(Class<T> modelClass){
|
||||
if(MODEL_DICTS.containsKey(modelClass.getName())){
|
||||
private static <T extends BaseExcelModel> List<String> extractGenericDictTypes(Class<T> modelClass) {
|
||||
if (MODEL_DICTS.containsKey(modelClass.getName())) {
|
||||
return MODEL_DICTS.get(modelClass.getName());
|
||||
}
|
||||
// 检测model是否包含dict注解
|
||||
List<String> dictTypes = new ArrayList<>();
|
||||
List<Field> fields = BeanUtils.extractFields(modelClass, BindDict.class);
|
||||
if(V.notEmpty(fields)){
|
||||
if (V.notEmpty(fields)) {
|
||||
fields.forEach(fld -> {
|
||||
if(fld.getAnnotation(BindDict.class) != null){
|
||||
if (fld.getAnnotation(BindDict.class) != null) {
|
||||
BindDict bindDict = fld.getAnnotation(BindDict.class);
|
||||
dictTypes.add(bindDict.type());
|
||||
}
|
||||
});
|
||||
}
|
||||
if(dictTypes.isEmpty()){
|
||||
if (dictTypes.isEmpty()) {
|
||||
NO_DICT_MODELS.add(modelClass.getName());
|
||||
} else {
|
||||
MODEL_DICTS.put(modelClass.getName(), dictTypes);
|
||||
}
|
||||
else{
|
||||
return dictTypes;
|
||||
}
|
||||
|
||||
/**
|
||||
* 提取注解绑定
|
||||
*
|
||||
* @param modelClass
|
||||
* @return
|
||||
*/
|
||||
private static List<String> extractDictTypes(Class<?> modelClass) {
|
||||
if (MODEL_DICTS.containsKey(modelClass.getName())) {
|
||||
return MODEL_DICTS.get(modelClass.getName());
|
||||
}
|
||||
// 检测model是否包含dict注解
|
||||
List<String> dictTypes = new ArrayList<>();
|
||||
List<Field> fields = BeanUtils.extractFields(modelClass, BindDict.class);
|
||||
if (V.notEmpty(fields)) {
|
||||
fields.forEach(fld -> {
|
||||
if (fld.getAnnotation(BindDict.class) != null) {
|
||||
BindDict bindDict = fld.getAnnotation(BindDict.class);
|
||||
dictTypes.add(bindDict.type());
|
||||
}
|
||||
});
|
||||
}
|
||||
if (dictTypes.isEmpty()) {
|
||||
NO_DICT_MODELS.add(modelClass.getName());
|
||||
} else {
|
||||
MODEL_DICTS.put(modelClass.getName(), dictTypes);
|
||||
}
|
||||
return dictTypes;
|
||||
|
@ -146,10 +208,11 @@ public class DictTempCache {
|
|||
|
||||
/**
|
||||
* 是否超期
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private static boolean isExpired(Long cacheTime){
|
||||
if(cacheTime == null){
|
||||
private static boolean isExpired(Long cacheTime) {
|
||||
if (cacheTime == null) {
|
||||
return true;
|
||||
}
|
||||
return (System.currentTimeMillis() - cacheTime) > 600000;
|
||||
|
|
|
@ -47,12 +47,12 @@ public class DictConverter implements Converter<String> {
|
|||
public String convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
|
||||
// 注解
|
||||
BindDict bindDict = contentProperty.getField().getAnnotation(BindDict.class);
|
||||
if(bindDict == null){
|
||||
if (bindDict == null) {
|
||||
throw new BusinessException("DictConverter依赖BindDict注解,请指定");
|
||||
}
|
||||
String dictValue = DictTempCache.getDictValue(bindDict.type(), cellData.getStringValue());
|
||||
if(dictValue == null){
|
||||
throw new BusinessException("'"+cellData.getStringValue()+"' 无匹配字典定义");
|
||||
if (dictValue == null) {
|
||||
throw new BusinessException("'" + cellData.getStringValue() + "' 无匹配字典定义");
|
||||
}
|
||||
return dictValue;
|
||||
}
|
||||
|
@ -61,9 +61,9 @@ public class DictConverter implements Converter<String> {
|
|||
public CellData convertToExcelData(String value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
|
||||
// 注解
|
||||
BindDict bindDict = contentProperty.getField().getAnnotation(BindDict.class);
|
||||
if(bindDict == null){
|
||||
if (bindDict == null) {
|
||||
throw new BusinessException("DictConverter依赖BindDict注解,请指定.");
|
||||
}
|
||||
return new CellData(DictTempCache.getDictLabel(bindDict.type(), value));
|
||||
return new CellData(DictTempCache.getDictLabel(bindDict.type(), value, contentProperty.getField().getDeclaringClass()));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -141,7 +141,7 @@ public abstract class FixedHeadExcelListener<T extends BaseExcelModel> extends A
|
|||
this.headMap = headMap;
|
||||
// 刷新字典缓存
|
||||
Class<T> modelClass = BeanUtils.getGenericityClass(this, 0);
|
||||
DictTempCache.refreshDictCache(modelClass);
|
||||
DictTempCache.refreshGenericDictCache(modelClass);
|
||||
|
||||
ExcelReadHeadProperty excelReadHeadProperty = context.currentReadHolder().excelReadHeadProperty();
|
||||
fieldHeadMap = new LinkedHashMap<>();
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
*/
|
||||
package com.diboot.file.starter;
|
||||
|
||||
import com.diboot.core.config.Cons;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -24,6 +25,8 @@ import org.springframework.context.annotation.Bean;
|
|||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.web.multipart.MultipartResolver;
|
||||
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
|
||||
|
||||
@Slf4j
|
||||
@Configuration
|
||||
|
@ -40,14 +43,29 @@ public class FileAutoConfiguration {
|
|||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(FilePluginManager.class)
|
||||
public FilePluginManager filePluginManager(){
|
||||
public FilePluginManager filePluginManager() {
|
||||
// 初始化SCHEMA
|
||||
SqlHandler.init(environment);
|
||||
FilePluginManager pluginManager = new FilePluginManager() {};
|
||||
FilePluginManager pluginManager = new FilePluginManager() {
|
||||
};
|
||||
// 检查数据库字典是否已存在
|
||||
if(fileProperties.isInitSql() && SqlHandler.checkIsFileTableExists() == false){
|
||||
if (fileProperties.isInitSql() && SqlHandler.checkIsFileTableExists() == false) {
|
||||
SqlHandler.initBootstrapSql(pluginManager.getClass(), environment, "file");
|
||||
}
|
||||
return pluginManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* 需要文件上传,开启此配置
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(MultipartResolver.class)
|
||||
public MultipartResolver multipartResolver() {
|
||||
CommonsMultipartResolver bean = new CommonsMultipartResolver();
|
||||
bean.setDefaultEncoding(Cons.CHARSET_UTF8);
|
||||
bean.setMaxUploadSize(fileProperties.getMaxUploadSize());
|
||||
return bean;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,4 +29,9 @@ public class FileProperties {
|
|||
* 是否初始化,默认true自动安装SQL
|
||||
*/
|
||||
private boolean initSql = true;
|
||||
|
||||
/**
|
||||
* 最大上传大小(默认10M)
|
||||
*/
|
||||
private Long maxUploadSize = 10 * 1024 * 1024L;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue