!91 feat(excel): 导出模板自动生成示例数据行
* feat(excel): 导出模板自动生成示例数据行 * fix(excel): 字典导入兼容中英文逗号分隔多值,修复BgPartymatter导入Map.of参数错位 * fix(import): Excel导入时注入bpmStatus和delFlag默认值,避免新增记录缺少流程状态
This commit is contained in:
+69
-8
@@ -111,6 +111,54 @@ public final class ExcelAnnotationUtils {
|
||||
return columns;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 @ExcelColumn 注解自动构建一个填充了示例数据的实体实例,用于导出模板。
|
||||
* String 字段填 "示例" + 列名,dict 字段填 "示例文本",日期填当天,数字填 1。
|
||||
*/
|
||||
public static <T> T buildExample(Class<T> clazz) {
|
||||
try {
|
||||
T obj = clazz.getDeclaredConstructor().newInstance();
|
||||
for (Field field : getAllFields(clazz)) {
|
||||
ExcelColumn col = field.getAnnotation(ExcelColumn.class);
|
||||
if (col == null) continue;
|
||||
field.setAccessible(true);
|
||||
setExampleValue(field, obj, col);
|
||||
}
|
||||
return obj;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("【Excel工具】构建示例数据失败, class: " + clazz.getSimpleName(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private static void setExampleValue(Field field, Object obj, ExcelColumn col) throws Exception {
|
||||
Class<?> type = field.getType();
|
||||
boolean hasDict = StringUtils.isNotEmpty(col.dictTable()) || StringUtils.isNotEmpty(col.dicCode());
|
||||
|
||||
if (type == String.class) {
|
||||
field.set(obj, hasDict ? "示例文本" : "示例" + col.name());
|
||||
} else if (type == Date.class) {
|
||||
field.set(obj, new Date());
|
||||
} else if (type == LocalDate.class) {
|
||||
field.set(obj, LocalDate.now());
|
||||
} else if (type == LocalDateTime.class) {
|
||||
field.set(obj, LocalDateTime.now());
|
||||
} else if (type == Integer.class || type == int.class) {
|
||||
field.set(obj, 1);
|
||||
} else if (type == Long.class || type == long.class) {
|
||||
field.set(obj, 1L);
|
||||
} else if (type == Double.class || type == double.class) {
|
||||
field.set(obj, 1.0);
|
||||
} else if (type == Float.class || type == float.class) {
|
||||
field.set(obj, 1.0f);
|
||||
} else if (type == Short.class || type == short.class) {
|
||||
field.set(obj, (short) 1);
|
||||
} else if (type == BigDecimal.class) {
|
||||
field.set(obj, BigDecimal.ONE);
|
||||
} else if (type == Boolean.class || type == boolean.class) {
|
||||
field.set(obj, false);
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== 导出 ====================
|
||||
|
||||
/**
|
||||
@@ -214,15 +262,16 @@ public final class ExcelAnnotationUtils {
|
||||
// ==================== 内部辅助方法 ====================
|
||||
|
||||
static boolean isValidDictDisplay(String[] replace, String value) {
|
||||
String normalized = normalizeComma(value);
|
||||
for (String s : replace) {
|
||||
String[] arr = s.split(",");
|
||||
if (arr.length >= 2 && arr[0].equals(value)) {
|
||||
if (arr.length >= 2 && arr[0].equals(normalized)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// 逗号分隔多值:逐个验证每个部分是否在字典范围内
|
||||
if (value.contains(",")) {
|
||||
String[] parts = value.split(",");
|
||||
// 逗号分隔多值:逐个验证每个部分是否在字典范围内(兼容中英文逗号)
|
||||
if (normalized.contains(",")) {
|
||||
String[] parts = normalized.split(",");
|
||||
for (String part : parts) {
|
||||
String trimmed = part.trim();
|
||||
if (trimmed.isEmpty()) continue;
|
||||
@@ -234,7 +283,10 @@ public final class ExcelAnnotationUtils {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) return false;
|
||||
if (!found) {
|
||||
log.warn("【Excel导入-字典校验】多值中'{}'不在字典范围, 输入值: {}, replace长度: {}", trimmed, value, replace.length);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -249,9 +301,10 @@ public final class ExcelAnnotationUtils {
|
||||
String resolved = displayToCode.getOrDefault(value, value);
|
||||
|
||||
if (type == String.class) {
|
||||
// 逗号分隔多值:逐个翻译后再拼回(如 "部门A,部门B" → "id_a,id_b")
|
||||
if (resolved.equals(value) && value.contains(",")) {
|
||||
String[] parts = value.split(",");
|
||||
// 逗号分隔多值:逐个翻译后再拼回(如 "部门A,部门B" → "id_a,id_b"),兼容中英文逗号
|
||||
String normalized = normalizeComma(value);
|
||||
if (resolved.equals(value) && normalized.contains(",")) {
|
||||
String[] parts = normalized.split(",");
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < parts.length; i++) {
|
||||
if (i > 0) sb.append(",");
|
||||
@@ -406,6 +459,14 @@ public final class ExcelAnnotationUtils {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将中文逗号归一化为英文逗号,兼容 Excel 中两种逗号混用。
|
||||
*/
|
||||
private static String normalizeComma(String value) {
|
||||
if (value == null) return null;
|
||||
return value.replace(',', ',');
|
||||
}
|
||||
|
||||
private static String[] mergeReplace(String[] original, String[] dictReplace) {
|
||||
if (original == null || original.length == 0) return dictReplace;
|
||||
if (dictReplace == null || dictReplace.length == 0) return original;
|
||||
|
||||
Reference in New Issue
Block a user