diff --git a/diboot-file-starter/src/main/java/com/diboot/file/excel/cache/DictTempCache.java b/diboot-file-starter/src/main/java/com/diboot/file/excel/cache/DictTempCache.java index 1b3f19c..2fd80c7 100644 --- a/diboot-file-starter/src/main/java/com/diboot/file/excel/cache/DictTempCache.java +++ b/diboot-file-starter/src/main/java/com/diboot/file/excel/cache/DictTempCache.java @@ -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> MODEL_DICTS = new HashMap<>(); /** - * 刷新model字典缓存 + * 刷新指定泛型model字典缓存 + * * @param modelClass * @param */ - public static void refreshDictCache(Class modelClass){ + public static void refreshGenericDictCache(Class modelClass) { // 无字典model - if(NO_DICT_MODELS.contains(modelClass.getName())){ + if (NO_DICT_MODELS.contains(modelClass.getName())) { + return; + } + List 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 list = ContextHelper.getBean(DictionaryService.class).getKeyValueList(dictType); + Map 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 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 list = ContextHelper.getBean(DictionaryService.class).getKeyValueList(dictType); Map 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 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 String getDictLabel(String dictType, String dictValue, Class modelClass) throws Exception { Map 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 entry : map.entrySet()){ - if(dictValue.equals(entry.getValue())){ + for (Map.Entry entry : map.entrySet()) { + if (dictValue.equals(entry.getValue())) { return entry.getKey(); } } @@ -116,29 +149,58 @@ public class DictTempCache { /** * 提取注解绑定 + * * @param modelClass * @param * @return */ - private static List extractDictTypes(Class modelClass){ - if(MODEL_DICTS.containsKey(modelClass.getName())){ + private static List extractGenericDictTypes(Class modelClass) { + if (MODEL_DICTS.containsKey(modelClass.getName())) { return MODEL_DICTS.get(modelClass.getName()); } // 检测model是否包含dict注解 List dictTypes = new ArrayList<>(); List 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 extractDictTypes(Class modelClass) { + if (MODEL_DICTS.containsKey(modelClass.getName())) { + return MODEL_DICTS.get(modelClass.getName()); + } + // 检测model是否包含dict注解 + List dictTypes = new ArrayList<>(); + List 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; diff --git a/diboot-file-starter/src/main/java/com/diboot/file/excel/converter/DictConverter.java b/diboot-file-starter/src/main/java/com/diboot/file/excel/converter/DictConverter.java index 116afb2..14e2725 100644 --- a/diboot-file-starter/src/main/java/com/diboot/file/excel/converter/DictConverter.java +++ b/diboot-file-starter/src/main/java/com/diboot/file/excel/converter/DictConverter.java @@ -47,12 +47,12 @@ public class DictConverter implements Converter { 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 { 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())); } } diff --git a/diboot-file-starter/src/main/java/com/diboot/file/excel/listener/FixedHeadExcelListener.java b/diboot-file-starter/src/main/java/com/diboot/file/excel/listener/FixedHeadExcelListener.java index d68aad1..e483578 100644 --- a/diboot-file-starter/src/main/java/com/diboot/file/excel/listener/FixedHeadExcelListener.java +++ b/diboot-file-starter/src/main/java/com/diboot/file/excel/listener/FixedHeadExcelListener.java @@ -141,7 +141,7 @@ public abstract class FixedHeadExcelListener extends A this.headMap = headMap; // 刷新字典缓存 Class modelClass = BeanUtils.getGenericityClass(this, 0); - DictTempCache.refreshDictCache(modelClass); + DictTempCache.refreshGenericDictCache(modelClass); ExcelReadHeadProperty excelReadHeadProperty = context.currentReadHolder().excelReadHeadProperty(); fieldHeadMap = new LinkedHashMap<>(); diff --git a/diboot-file-starter/src/main/java/com/diboot/file/starter/FileAutoConfiguration.java b/diboot-file-starter/src/main/java/com/diboot/file/starter/FileAutoConfiguration.java index 935858f..3fae193 100644 --- a/diboot-file-starter/src/main/java/com/diboot/file/starter/FileAutoConfiguration.java +++ b/diboot-file-starter/src/main/java/com/diboot/file/starter/FileAutoConfiguration.java @@ -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; + } } diff --git a/diboot-file-starter/src/main/java/com/diboot/file/starter/FileProperties.java b/diboot-file-starter/src/main/java/com/diboot/file/starter/FileProperties.java index f348039..0e46682 100644 --- a/diboot-file-starter/src/main/java/com/diboot/file/starter/FileProperties.java +++ b/diboot-file-starter/src/main/java/com/diboot/file/starter/FileProperties.java @@ -29,4 +29,9 @@ public class FileProperties { * 是否初始化,默认true自动安装SQL */ private boolean initSql = true; + + /** + * 最大上传大小(默认10M) + */ + private Long maxUploadSize = 10 * 1024 * 1024L; }