优化代码
This commit is contained in:
parent
c3539f4e51
commit
f19765f6d0
|
@ -1,9 +1,11 @@
|
||||||
package net.maku.framework.common.utils;
|
package net.maku.framework.common.utils;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.CollectionUtil;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.hutool.core.util.URLUtil;
|
import cn.hutool.core.util.URLUtil;
|
||||||
|
import cn.hutool.extra.spring.SpringUtil;
|
||||||
import com.alibaba.excel.EasyExcel;
|
import com.alibaba.excel.EasyExcel;
|
||||||
import com.alibaba.excel.support.ExcelTypeEnum;
|
import com.alibaba.excel.support.ExcelTypeEnum;
|
||||||
import com.fhs.common.spring.SpringContextUtil;
|
|
||||||
import com.fhs.common.utils.ConverterUtils;
|
import com.fhs.common.utils.ConverterUtils;
|
||||||
import com.fhs.core.trans.anno.Trans;
|
import com.fhs.core.trans.anno.Trans;
|
||||||
import com.fhs.core.trans.constant.TransType;
|
import com.fhs.core.trans.constant.TransType;
|
||||||
|
@ -32,7 +34,6 @@ import java.util.stream.Collectors;
|
||||||
*/
|
*/
|
||||||
public class ExcelUtils {
|
public class ExcelUtils {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 读取excel文件
|
* 读取excel文件
|
||||||
*
|
*
|
||||||
|
@ -151,42 +152,39 @@ public class ExcelUtils {
|
||||||
* 解析字典数据到字段上
|
* 解析字典数据到字段上
|
||||||
* 比如 T中有 genderLabel字段 为男 需要给 gender 字段自动设置为0
|
* 比如 T中有 genderLabel字段 为男 需要给 gender 字段自动设置为0
|
||||||
*
|
*
|
||||||
* @param datas 需要被反向解析的数据
|
* @param dataList 需要被反向解析的数据
|
||||||
* @param <T>
|
|
||||||
*/
|
*/
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
public static <T extends TransPojo> void parseDict(List<T> datas) {
|
public static <T extends TransPojo> void parseDict(List<T> dataList) {
|
||||||
//没有数据就不需要初始化
|
//没有数据就不需要初始化
|
||||||
if (datas == null || datas.isEmpty() || datas.get(0) == null) {
|
if (CollectionUtil.isEmpty(dataList)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Class clazz = datas.get(0).getClass();
|
Class<? extends TransPojo> clazz = dataList.get(0).getClass();
|
||||||
//拿到所有需要反向翻译的字段
|
//拿到所有需要反向翻译的字段
|
||||||
List<Field> fields = ReflectUtils.getAnnotationField(clazz, Trans.class);
|
List<Field> fields = ReflectUtils.getAnnotationField(clazz, Trans.class);
|
||||||
//过滤出字典翻译
|
//过滤出字典翻译
|
||||||
fields = fields.stream().filter(field -> {
|
fields = fields.stream().filter(field -> TransType.DICTIONARY.equals(field.getAnnotation(Trans.class).type())).collect(Collectors.toList());
|
||||||
return TransType.DICTIONARY.equals(field.getAnnotation(Trans.class).type());
|
DictionaryTransService dictionaryTransService = SpringUtil.getBean(DictionaryTransService.class);
|
||||||
}).collect(Collectors.toList());
|
for (T data : dataList) {
|
||||||
DictionaryTransService dictionaryTransService = SpringContextUtil.getBeanByClass(DictionaryTransService.class);
|
|
||||||
for (T data : datas) {
|
|
||||||
for (Field field : fields) {
|
for (Field field : fields) {
|
||||||
Trans trans = field.getAnnotation(Trans.class);
|
Trans trans = field.getAnnotation(Trans.class);
|
||||||
//key不能为空并且ref不为空的才自动处理
|
// key不能为空并且ref不为空的才自动处理
|
||||||
if (!StringUtils.isBlank(trans.key()) && !StringUtils.isBlank(trans.ref())) {
|
if (StrUtil.isAllNotBlank(trans.key(), trans.ref())) {
|
||||||
Field ref = ReflectUtils.getDeclaredField(clazz, trans.ref());
|
Field ref = ReflectUtils.getDeclaredField(clazz, trans.ref());
|
||||||
ref.setAccessible(true);
|
ref.setAccessible(true);
|
||||||
//获取字典反向值
|
// 获取字典反向值
|
||||||
String value = dictionaryTransService.getUnTransMap().get(trans.key() + "_" + ref.get(data));
|
String value = dictionaryTransService.getUnTransMap().get(trans.key() + "_" + ref.get(data));
|
||||||
if (StringUtils.isBlank(value)) {
|
if (StringUtils.isBlank(value)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
//一般目标字段是int或者string字段 后面有添加单独抽离方法
|
// 一般目标字段是int或者string字段 后面有添加单独抽离方法
|
||||||
if(Integer.class.equals(field.getType())){
|
if (Integer.class.equals(field.getType())) {
|
||||||
field.setAccessible(true);
|
field.setAccessible(true);
|
||||||
field.set(data,ConverterUtils.toInteger(value));
|
field.set(data, ConverterUtils.toInteger(value));
|
||||||
}else{
|
} else {
|
||||||
field.setAccessible(true);
|
field.setAccessible(true);
|
||||||
field.set(data,ConverterUtils.toString(value));
|
field.set(data, ConverterUtils.toString(value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user