diff --git a/storage/src/main/java/com/zfoo/storage/interpreter/ResourceInterpreter.java b/storage/src/main/java/com/zfoo/storage/interpreter/ResourceInterpreter.java index daf21a6c..06d29d71 100644 --- a/storage/src/main/java/com/zfoo/storage/interpreter/ResourceInterpreter.java +++ b/storage/src/main/java/com/zfoo/storage/interpreter/ResourceInterpreter.java @@ -67,7 +67,7 @@ public class ResourceInterpreter { var result = new ArrayList(); //获取所有字段 - 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 getFieldInfos(Map fieldMap, Class clazz) { + private static Collection getFieldInfos(Map 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 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 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 getFieldMap(ResourceData resource, Class clazz) { + public static Map 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());