1. 增加字典数据的缓存与 util

2. 引入 Excel 组件
This commit is contained in:
YunaiV
2021-01-13 01:09:26 +08:00
parent ec00936532
commit fc444728c9
22 changed files with 277 additions and 98 deletions

View File

@ -0,0 +1,18 @@
package cn.iocoder.dashboard.framework.dict.config;
import cn.iocoder.dashboard.framework.dict.core.service.DictDataFrameworkService;
import cn.iocoder.dashboard.framework.dict.core.util.DictUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class DictConfiguration {
@Bean
@SuppressWarnings("InstantiationOfUtilityClass")
public DictUtils dictUtils(DictDataFrameworkService service) {
DictUtils.init(service);
return new DictUtils();
}
}

View File

@ -1,6 +1,7 @@
package cn.iocoder.dashboard.framework.dict.core.util;
import cn.iocoder.dashboard.framework.dict.core.service.DictDataFrameworkService;
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.dict.SysDictDataDO;
import lombok.extern.slf4j.Slf4j;
/**
@ -16,4 +17,12 @@ public class DictUtils {
log.info("[init][初始化 DictUtils 成功]");
}
public static SysDictDataDO getDictDataFromCache(String type, String value) {
return service.getDictDataFromCache(type, value);
}
public static SysDictDataDO parseDictDataFromCache(String type, String label) {
return service.getDictDataFromCache(type, label);
}
}

View File

@ -11,6 +11,7 @@ import java.math.BigDecimal;
*
* @author ruoyi
*/
@Deprecated
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Excel {

View File

@ -10,6 +10,7 @@ import java.lang.annotation.Target;
*
* @author ruoyi
*/
@Deprecated
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Excels

View File

@ -0,0 +1,19 @@
package cn.iocoder.dashboard.framework.excel.core.annotations;
import cn.iocoder.dashboard.modules.system.enums.dict.DictTypeEnum;
import java.lang.annotation.*;
/**
* 字典格式化
*
* 实现将字典数据的值,格式化成字典数据的标签
*/
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface DictFormat {
DictTypeEnum value();
}

View File

@ -0,0 +1,64 @@
package cn.iocoder.dashboard.framework.excel.core.convert;
import cn.hutool.core.convert.Convert;
import cn.iocoder.dashboard.framework.dict.core.util.DictUtils;
import cn.iocoder.dashboard.framework.excel.core.annotations.DictFormat;
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.dict.SysDictDataDO;
import cn.iocoder.dashboard.modules.system.enums.dict.DictTypeEnum;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class DictConvert implements Converter<Object> {
@Override
public Class<?> supportJavaTypeKey() {
throw new UnsupportedOperationException("暂不支持,也不需要");
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
throw new UnsupportedOperationException("暂不支持,也不需要");
}
@Override
public Object convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
// 使用字典解析
DictTypeEnum type = getType(contentProperty);
String label = cellData.getStringValue();
SysDictDataDO dictData = DictUtils.parseDictDataFromCache(type.getValue(), label);
if (dictData == null) {
log.error("[convertToJavaData][type({}) 解析不掉 label({})]", type, label);
return null;
}
// 将 String 的 value 转换成对应的属性
Class<?> fieldClazz = contentProperty.getField().getType();
return Convert.convert(fieldClazz, dictData.getValue());
}
@Override
public CellData<String> convertToExcelData(Object object, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
// 使用字典格式化
DictTypeEnum type = getType(contentProperty);
String value = String.valueOf(object);
SysDictDataDO dictData = DictUtils.getDictDataFromCache(type.getValue(), value);
if (dictData == null) {
log.error("[convertToExcelData][type({}) 转换不了 label({})]", type, value);
return null;
}
// 生成 Excel 小表格
return new CellData<>(dictData.getValue());
}
private static DictTypeEnum getType(ExcelContentProperty contentProperty) {
return contentProperty.getField().getAnnotation(DictFormat.class).value();
}
}

View File

@ -0,0 +1 @@
package cn.iocoder.dashboard.framework.excel.core;

View File

@ -0,0 +1,4 @@
/**
* 基于 EasyExcel 实现 Excel 相关的操作
*/
package cn.iocoder.dashboard.framework.excel;