diff --git a/protocol/src/main/java/com/zfoo/protocol/util/StringUtils.java b/protocol/src/main/java/com/zfoo/protocol/util/StringUtils.java index 641c5c0a..38c5db04 100644 --- a/protocol/src/main/java/com/zfoo/protocol/util/StringUtils.java +++ b/protocol/src/main/java/com/zfoo/protocol/util/StringUtils.java @@ -145,6 +145,9 @@ public abstract class StringUtils { return cs == null || cs.length() == 0; } + public static boolean isNotEmpty(final CharSequence cs) { + return !isEmpty(cs); + } /** * StringUtils.isBlank(null)=true @@ -168,6 +171,10 @@ public abstract class StringUtils { return true; } + public static boolean isNotBlank(final CharSequence cs) { + return !isBlank(cs); + } + public static String trim(String str) { if (StringUtils.isBlank(str)) { return EMPTY; diff --git a/storage/src/main/java/com/zfoo/storage/interpreter/ExcelResourceReader.java b/storage/src/main/java/com/zfoo/storage/interpreter/ExcelResourceReader.java index fbe3d1cd..52543cee 100644 --- a/storage/src/main/java/com/zfoo/storage/interpreter/ExcelResourceReader.java +++ b/storage/src/main/java/com/zfoo/storage/interpreter/ExcelResourceReader.java @@ -76,27 +76,17 @@ public class ExcelResourceReader implements IResourceReader { var instance = ReflectionUtils.newInstance(clazz); var idCell = row.getCell(0); - if (idCell == null || StringUtils.isBlank(CellUtils.getCellStringValue(idCell))) { + if (StringUtils.isBlank(CellUtils.getCellStringValue(idCell))) { continue; } for (var fieldInfo : fieldInfos) { var cell = row.getCell(fieldInfo.index); - if (cell != null) { - var content = CellUtils.getCellStringValue(cell); - if (!StringUtils.isEmpty(content)) { - inject(instance, fieldInfo.field, content); - } - } - - // 如果读的是id列的单元格,则判断当前id是否为空 - if (fieldInfo.field.isAnnotationPresent(Id.class)) { - if (cell == null || StringUtils.isEmpty(CellUtils.getCellStringValue(cell))) { - throw new RunException("静态资源[resource:{}]存在id未配置的项", clazz.getSimpleName()); - } + var content = CellUtils.getCellStringValue(cell); + if (StringUtils.isNotEmpty(content)) { + inject(instance, fieldInfo.field, content); } } - result.add(instance); } return result; @@ -148,6 +138,13 @@ public class ExcelResourceReader implements IResourceReader { throw new RunException("资源类[class:{}]的声明属性[filed:{}]无法获取,请检查配置表的格式", clazz, field.getName()); } + if (field.isAnnotationPresent(Id.class)) { + var cellIndex = cellFieldMap.get(field.getName()); + if (cellIndex != 0) { + throw new RunException("资源类[class:{}]的主键[Id:{}]必须放在Excel配置表的第一列,请检查配置表的格式", clazz, field.getName()); + } + } + if (Modifier.isPublic(field.getModifiers())) { throw new RunException("因为静态资源类是不能被修改的,所以资源类[class:{}]的属性[filed:{}]不能被public修饰,请改为private修饰", clazz, field.getName()); } diff --git a/storage/src/main/java/com/zfoo/storage/util/CellUtils.java b/storage/src/main/java/com/zfoo/storage/util/CellUtils.java index 90af9cb3..566c68f6 100644 --- a/storage/src/main/java/com/zfoo/storage/util/CellUtils.java +++ b/storage/src/main/java/com/zfoo/storage/util/CellUtils.java @@ -24,25 +24,31 @@ import org.apache.poi.ss.usermodel.FormulaError; * @version 3.0 */ public abstract class CellUtils { + + public static String getCellStringValue(Cell cell) { + if (cell == null) { + return StringUtils.EMPTY; + } + return getCellValue(cell).toString().trim(); + } + /** * 获取单元格值 * * @return 值,类型可能为:Date、Double、Boolean、String */ public static Object getCellValue(Cell cell) { + if (cell == null) { + return StringUtils.EMPTY; + } return getCellValue(cell, cell.getCellType()); } - public static String getCellStringValue(Cell cell) { - return getCellValue(cell).toString().trim(); - } - - /** * 获取单元格值
* 如果单元格值为数字格式,则判断其格式中是否有小数部分,无则返回Long类型,否则返回Double类型 */ - public static Object getCellValue(Cell cell, CellType cellType) { + private static Object getCellValue(Cell cell, CellType cellType) { Object value; switch (cellType) { case NUMERIC: @@ -94,10 +100,7 @@ public abstract class CellUtils { // 普通数字 if (null != format && !format.contains(StringUtils.PERIOD)) { var longValue = (long) value; - if (longValue == value) { - // 对于无小数部分的数字类型,转为Long - return longValue; - } + // 对于无小数部分的数字类型,转为Long } return value; }