perf[storage]: use ExcelFieldName annotation as the column name

This commit is contained in:
sun
2023-01-10 20:03:33 +08:00
parent 1371bf51d3
commit 051660e3a2
@@ -67,7 +67,7 @@ public class ResourceInterpreter {
var result = new ArrayList<T>();
//获取所有字段
var cellFieldMap = getFieldMap(resource, clazz);
var cellFieldMap = getCellFieldMap(resource, clazz);
var fieldInfos = getFieldInfos(cellFieldMap, clazz);
var iterator = resource.getRows().iterator();
@@ -98,40 +98,29 @@ public class ResourceInterpreter {
}
}
// 优先使用ExcelFieldName注解表示的值当作列名
private static String getFieldName(Field field) {
return field.isAnnotationPresent(ExcelFieldName.class) ? field.getAnnotation(ExcelFieldName.class).value() : field.getName();
}
// 只读取代码里写的字段
private static Collection<FieldInfo> getFieldInfos(Map<String, Integer> fieldMap, Class<?> clazz) {
private static Collection<FieldInfo> getFieldInfos(Map<String, Integer> cellFieldMap, Class<?> clazz) {
var fieldList = ReflectionUtils.notStaticAndTransientFields(clazz);
// 检测field的合法性,field必须可以在excel中找到对应的列,有找不到的列在启动时候就发现
for (var field : fieldList) {
var fieldName = getFieldName(field);
if (!cellFieldMap.containsKey(fieldName)) {
throw new RunException("The declaration attribute [filed:{}] of the resource class [class:{}] cannot be obtained, please check the format of the configuration table", field.getName(), clazz);
}
if (field.isAnnotationPresent(Id.class)) {
var cellIndex = fieldMap.get(field.getName());
var cellIndex = cellFieldMap.get(fieldName);
if (cellIndex != 0) {
throw new RunException("The primary key [Id:{}] of the resource class [class:{}] must be placed in the first column of the Excel configuration table, please check the format of the configuration table", field.getName(), clazz);
}
}
}
Collection<FieldInfo> fieldInfos=new ArrayList<>();
for(var field : fieldList){
var ans=false;
for(var fieldMapKey:fieldMap.keySet()){
if(field.isAnnotationPresent(ExcelFieldName.class)){
ans|=fieldMapKey.equals(field.getAnnotation(ExcelFieldName.class).value());
}
ans|=fieldMapKey.equals(field.getName());
}
if(ans==false)
continue;
String excelFieldName;
List<String> list=null;
if(field.isAnnotationPresent(ExcelFieldName.class)){
list=fieldMap.keySet().stream().filter(it->it.equals(field.getAnnotation(ExcelFieldName.class).value())).collect(Collectors.toList());
}
if(list==null||list.size()==0){
list=fieldMap.keySet().stream().filter(it->it.equals(field.getName())).collect(Collectors.toList());
}
excelFieldName=list.get(0);
fieldInfos.add(new FieldInfo(fieldMap.get(excelFieldName), field));
}
return fieldInfos;
return fieldList.stream().map(it -> new FieldInfo(cellFieldMap.get(getFieldName(it)), it)).collect(Collectors.toList());
}
private static class FieldInfo {
@@ -144,7 +133,7 @@ public class ResourceInterpreter {
}
}
public static Map<String, Integer> getFieldMap(ResourceData resource, Class<?> clazz) {
public static Map<String, Integer> getCellFieldMap(ResourceData resource, Class<?> clazz) {
var header = resource.getHeaders();
if (header == null) {
throw new RunException("Failed to get attribute control column from excel file of resource [class:{}]", clazz.getSimpleName());