!295 新增EasyExcel枚举类数据翻译注解
* Merge remote-tracking branch 'origin/dev' into dev * 利用ReflectUtil反射工具类获取Enum属性 * 利用ReflectUtil反射工具类获取Enum属性 * 新增EasyExcel枚举类数据翻译注解
This commit is contained in:
parent
b465972e7c
commit
d6c49b915f
@ -0,0 +1,30 @@
|
|||||||
|
package com.ruoyi.common.annotation;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 枚举格式化
|
||||||
|
*
|
||||||
|
* @author Liang
|
||||||
|
*/
|
||||||
|
@Target({ElementType.FIELD})
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Inherited
|
||||||
|
public @interface ExcelEnumFormat {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 字典枚举类型
|
||||||
|
*/
|
||||||
|
Class<? extends Enum<?>> enumClass();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 字典枚举类中对应的code属性名称,默认为code
|
||||||
|
*/
|
||||||
|
String codeField() default "code";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 字典枚举类中对应的text属性名称,默认为text
|
||||||
|
*/
|
||||||
|
String textField() default "text";
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,75 @@
|
|||||||
|
package com.ruoyi.common.convert;
|
||||||
|
|
||||||
|
import cn.hutool.core.annotation.AnnotationUtil;
|
||||||
|
import cn.hutool.core.convert.Convert;
|
||||||
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
|
import com.alibaba.excel.converters.Converter;
|
||||||
|
import com.alibaba.excel.enums.CellDataTypeEnum;
|
||||||
|
import com.alibaba.excel.metadata.GlobalConfiguration;
|
||||||
|
import com.alibaba.excel.metadata.data.ReadCellData;
|
||||||
|
import com.alibaba.excel.metadata.data.WriteCellData;
|
||||||
|
import com.alibaba.excel.metadata.property.ExcelContentProperty;
|
||||||
|
import com.ruoyi.common.annotation.ExcelEnumFormat;
|
||||||
|
import com.ruoyi.common.utils.reflect.ReflectUtils;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 枚举格式化转换处理
|
||||||
|
*
|
||||||
|
* @author Liang
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class ExcelEnumConvert implements Converter<Object> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<Object> supportJavaTypeKey() {
|
||||||
|
return Object.class;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CellDataTypeEnum supportExcelTypeKey() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
|
||||||
|
Object codeValue = cellData.getData();
|
||||||
|
// 如果是空值
|
||||||
|
if (ObjectUtil.isNull(codeValue)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Map<Object, String> enumValueMap = beforeConvert(contentProperty);
|
||||||
|
String textValue = enumValueMap.get(codeValue);
|
||||||
|
return Convert.convert(contentProperty.getField().getType(), textValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public WriteCellData<String> convertToExcelData(Object object, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
|
||||||
|
if (ObjectUtil.isNull(object)) {
|
||||||
|
return new WriteCellData<>("");
|
||||||
|
}
|
||||||
|
Map<Object, String> enumValueMap = beforeConvert(contentProperty);
|
||||||
|
String value = Convert.toStr(enumValueMap.get(object), "");
|
||||||
|
return new WriteCellData<>(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<Object, String> beforeConvert(ExcelContentProperty contentProperty) {
|
||||||
|
ExcelEnumFormat anno = getAnnotation(contentProperty.getField());
|
||||||
|
Map<Object, String> enumValueMap = new HashMap<>();
|
||||||
|
Enum<?>[] enumConstants = anno.enumClass().getEnumConstants();
|
||||||
|
for (Enum<?> enumConstant : enumConstants) {
|
||||||
|
Object codeValue = ReflectUtils.invokeGetter(enumConstant, anno.codeField());
|
||||||
|
String textValue = ReflectUtils.invokeGetter(enumConstant, anno.textField());
|
||||||
|
enumValueMap.put(codeValue, textValue);
|
||||||
|
}
|
||||||
|
return enumValueMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
private ExcelEnumFormat getAnnotation(Field field) {
|
||||||
|
return AnnotationUtil.getAnnotation(field, ExcelEnumFormat.class);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user