作者 雷海东

工资导入报错提示

... ... @@ -20,6 +20,7 @@ import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.entity.enmus.ExcelType;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecgframework.poi.exception.excel.ExcelImportException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
... ... @@ -264,12 +265,13 @@ public class JeecgController<T, S extends IService<T>> {
} else {
return Result.error("文件导入失败:没有有效的数据需要保存!");
}
}catch (ExcelImportException e) {
return Result.error("文件导入失败,导入数据格式错误");
} catch (Exception e) {
//update-begin-author:taoyan date:20211124 for: 导入数据重复增加提示
String msg = e.getMessage();
log.error(msg, e);
if(msg!=null && msg.indexOf("Duplicate entry")>=0){
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); // 手动回滚事务
return Result.error("文件导入失败:有重复数据!");
}else{
return Result.error("文件导入失败:" + e.getMessage());
... ...
... ... @@ -314,7 +314,6 @@ public class JeecgController2<T, t, b, V extends IService<t>, S extends IService
// params.setKeyIndex(1);
params.setNeedSave(true);
int i = 0;
try {
List<T> list = ExcelImportUtil.importExcel(file.getInputStream(), clazz, params);
... ... @@ -323,28 +322,71 @@ public class JeecgController2<T, t, b, V extends IService<t>, S extends IService
long start = System.currentTimeMillis();
// 遍历Excel数据
for (T data :list) {
// 使用工号和姓名去基本信息表中检查是否存在对应的员工信息
b employeeExists = checkEmployeeExists(data);
if (employeeExists != null) {
// 如果员工存在,则保存这条数据到数据库中
shuju.add(data);
i++;
} else {
// 如果员工不存在,则跳过这条数据或进行其他相应的处理
// 这里可以添加日志记录或其他操作
continue;
for (int rowIndex = 0; rowIndex < list.size(); rowIndex++) {
T data = list.get(rowIndex);
if (data != null) {
// 获取对象的所有字段
Field[] fields = data.getClass().getDeclaredFields();
boolean hasNonNullValue = false;
for (Field field : fields) {
// 排除 serialVersionUID 字段
if ("serialVersionUID".equals(field.getName())) {
continue;
}
field.setAccessible(true); // 设置字段可访问
try {
// 获取字段值,并检查是否为空
Object value = field.get(data);
if (value != null) {
hasNonNullValue = true;
break;
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
if (hasNonNullValue) {
// 如果至少有一个字段值不为空,则保存数据到待保存列表
// 使用工号和姓名去基本信息表中检查是否存在对应的员工信息
b employeeExists = checkEmployeeExists(data);
if (employeeExists != null) {
// 如果员工存在,则保存这条数据到数据库中
shuju.add(data);
} else {
// 获取 data 对象的类
Class<?> clazz2 = data.getClass();
// 获取 工号 字段的值
Field jobId = clazz2.getDeclaredField("jobId");
jobId.setAccessible(true); // 设置可访问性,因为字段可能是私有的
String jobid = (String) jobId.get(data);
// 获取 姓名 字段的值
Field name = clazz2.getDeclaredField("userName");
name.setAccessible(true); // 设置可访问性,因为字段可能是私有的
String userName = (String) name.get(data);
// 如果员工不存在,则记录下当前数据的位置信息,比如行号
int rowNumber = rowIndex + 1; // 行号从1开始
// 这里可以根据需要将数据位置信息存储起来,比如记录到日志中或者构建一个错误信息列表等
// 示例:errorList.add("第 " + rowNumber + " 行的工号 " + data.get工号() + "、姓名 " + data.get姓名() + " 不存在对应的员工");
// 或者直接在此处输出错误信息
return Result.error("导入失败,第 " + rowNumber + " 行的工号 " + jobid+ "、姓名 " + userName + " 不存在对应的员工");
}
}
}
}
//400条 saveBatch消耗时间1592毫秒 循环插入消耗时间1947毫秒
//1200条 saveBatch消耗时间3687毫秒 循环插入消耗时间5212毫秒
if(shuju.size()<=0){
return Result.error("文件数据为空");
}
System.out.println("导入的数据:"+shuju);
service.saveBatch(shuju);
log.info("消耗时间" + (System.currentTimeMillis() - start) + "毫秒");
//update-end-author:taoyan date:20190528 for:批量插入数据
if(i==0){
return Result.error("导入的数据不存在基础信息表");
}
return Result.ok("文件导入成功!数据行数:" + shuju.size());
}catch (ExcelImportException e){
return Result.error("文件导入失败,导入数据格式错误");
... ...