feat(credit): 改进用户导入功能并过滤空行
- 在尝试导入时增加对空行的过滤处理 - 添加 filterEmptyRows 方法以移除完全空白的导入行 - 扩展 fallback 导入逻辑至三种表头情况 - 移除对唯一标识字段非空的强制校验 - 提升导入稳定性与容错能力
This commit is contained in:
@@ -146,16 +146,18 @@ public class CreditUserController extends BaseController {
|
|||||||
int successCount = 0;
|
int successCount = 0;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
List<CreditUserImportParam> list = filterEmptyRows(tryImport(file, 1, 1));
|
List<CreditUserImportParam> list = null;
|
||||||
System.out.println("list = " + list);
|
int usedTitleRows = 0;
|
||||||
if (CollectionUtils.isEmpty(list)) {
|
int usedHeadRows = 0;
|
||||||
list = filterEmptyRows(tryImport(file, 0, 1)); // fallback:无标题行,只有表头
|
int[][] tryConfigs = new int[][]{{1, 1}, {0, 1}, {0, 2}, {0, 3}};
|
||||||
}
|
|
||||||
if (CollectionUtils.isEmpty(list)) {
|
for (int[] config : tryConfigs) {
|
||||||
list = filterEmptyRows(tryImport(file, 0, 2)); // fallback:两行表头
|
list = filterEmptyRows(tryImport(file, config[0], config[1]));
|
||||||
}
|
if (!CollectionUtils.isEmpty(list)) {
|
||||||
if (CollectionUtils.isEmpty(list)) {
|
usedTitleRows = config[0];
|
||||||
list = filterEmptyRows(tryImport(file, 0, 3)); // fallback:三行表头
|
usedHeadRows = config[1];
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (CollectionUtils.isEmpty(list)) {
|
if (CollectionUtils.isEmpty(list)) {
|
||||||
return fail("未读取到数据,请确认模板表头与示例格式一致", null);
|
return fail("未读取到数据,请确认模板表头与示例格式一致", null);
|
||||||
@@ -189,20 +191,25 @@ public class CreditUserController extends BaseController {
|
|||||||
if (item.getDeleted() == null) {
|
if (item.getDeleted() == null) {
|
||||||
item.setDeleted(0);
|
item.setDeleted(0);
|
||||||
}
|
}
|
||||||
|
int excelRowNumber = i + 1 + usedTitleRows + usedHeadRows;
|
||||||
// 验证必填字段
|
// 验证必填字段
|
||||||
if (item.getName() == null || item.getName().trim().isEmpty()) {
|
if (item.getName() == null || item.getName().trim().isEmpty()) {
|
||||||
errorMessages.add("第" + (i + 1) + "行:项目名称不能为空");
|
errorMessages.add("第" + excelRowNumber + "行:项目名称不能为空");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (item.getCode() == null || item.getCode().trim().isEmpty()) {
|
||||||
|
errorMessages.add("第" + excelRowNumber + "行:唯一标识不能为空");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
System.out.println("item = " + item);
|
|
||||||
|
|
||||||
if (creditUserService.save(item)) {
|
if (creditUserService.save(item)) {
|
||||||
successCount++;
|
successCount++;
|
||||||
} else {
|
} else {
|
||||||
errorMessages.add("第" + (i + 1) + "行:保存失败");
|
errorMessages.add("第" + excelRowNumber + "行:保存失败");
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
errorMessages.add("第" + (i + 1) + "行:" + e.getMessage());
|
int excelRowNumber = i + 1 + usedTitleRows + usedHeadRows;
|
||||||
|
errorMessages.add("第" + excelRowNumber + "行:" + e.getMessage());
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user