作者 雷海东

工资导入报错提示

@@ -20,6 +20,7 @@ import org.jeecgframework.poi.excel.entity.ExportParams; @@ -20,6 +20,7 @@ import org.jeecgframework.poi.excel.entity.ExportParams;
20 import org.jeecgframework.poi.excel.entity.ImportParams; 20 import org.jeecgframework.poi.excel.entity.ImportParams;
21 import org.jeecgframework.poi.excel.entity.enmus.ExcelType; 21 import org.jeecgframework.poi.excel.entity.enmus.ExcelType;
22 import org.jeecgframework.poi.excel.view.JeecgEntityExcelView; 22 import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
  23 +import org.jeecgframework.poi.exception.excel.ExcelImportException;
23 import org.springframework.beans.factory.annotation.Autowired; 24 import org.springframework.beans.factory.annotation.Autowired;
24 import org.springframework.beans.factory.annotation.Value; 25 import org.springframework.beans.factory.annotation.Value;
25 import org.springframework.transaction.interceptor.TransactionAspectSupport; 26 import org.springframework.transaction.interceptor.TransactionAspectSupport;
@@ -264,12 +265,13 @@ public class JeecgController<T, S extends IService<T>> { @@ -264,12 +265,13 @@ public class JeecgController<T, S extends IService<T>> {
264 } else { 265 } else {
265 return Result.error("文件导入失败:没有有效的数据需要保存!"); 266 return Result.error("文件导入失败:没有有效的数据需要保存!");
266 } 267 }
  268 + }catch (ExcelImportException e) {
  269 + return Result.error("文件导入失败,导入数据格式错误");
267 } catch (Exception e) { 270 } catch (Exception e) {
268 //update-begin-author:taoyan date:20211124 for: 导入数据重复增加提示 271 //update-begin-author:taoyan date:20211124 for: 导入数据重复增加提示
269 String msg = e.getMessage(); 272 String msg = e.getMessage();
270 log.error(msg, e); 273 log.error(msg, e);
271 if(msg!=null && msg.indexOf("Duplicate entry")>=0){ 274 if(msg!=null && msg.indexOf("Duplicate entry")>=0){
272 - TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); // 手动回滚事务  
273 return Result.error("文件导入失败:有重复数据!"); 275 return Result.error("文件导入失败:有重复数据!");
274 }else{ 276 }else{
275 return Result.error("文件导入失败:" + e.getMessage()); 277 return Result.error("文件导入失败:" + e.getMessage());
@@ -314,7 +314,6 @@ public class JeecgController2<T, t, b, V extends IService<t>, S extends IService @@ -314,7 +314,6 @@ public class JeecgController2<T, t, b, V extends IService<t>, S extends IService
314 314
315 // params.setKeyIndex(1); 315 // params.setKeyIndex(1);
316 params.setNeedSave(true); 316 params.setNeedSave(true);
317 - int i = 0;  
318 try { 317 try {
319 List<T> list = ExcelImportUtil.importExcel(file.getInputStream(), clazz, params); 318 List<T> list = ExcelImportUtil.importExcel(file.getInputStream(), clazz, params);
320 319
@@ -323,28 +322,71 @@ public class JeecgController2<T, t, b, V extends IService<t>, S extends IService @@ -323,28 +322,71 @@ public class JeecgController2<T, t, b, V extends IService<t>, S extends IService
323 long start = System.currentTimeMillis(); 322 long start = System.currentTimeMillis();
324 323
325 // 遍历Excel数据 324 // 遍历Excel数据
326 - for (T data :list) {  
327 - // 使用工号和姓名去基本信息表中检查是否存在对应的员工信息  
328 - b employeeExists = checkEmployeeExists(data);  
329 -  
330 - if (employeeExists != null) {  
331 - // 如果员工存在,则保存这条数据到数据库中  
332 - shuju.add(data);  
333 - i++;  
334 - } else {  
335 - // 如果员工不存在,则跳过这条数据或进行其他相应的处理  
336 - // 这里可以添加日志记录或其他操作  
337 - continue; 325 + for (int rowIndex = 0; rowIndex < list.size(); rowIndex++) {
  326 + T data = list.get(rowIndex);
  327 + if (data != null) {
  328 + // 获取对象的所有字段
  329 + Field[] fields = data.getClass().getDeclaredFields();
  330 + boolean hasNonNullValue = false;
  331 + for (Field field : fields) {
  332 + // 排除 serialVersionUID 字段
  333 + if ("serialVersionUID".equals(field.getName())) {
  334 + continue;
  335 + }
  336 + field.setAccessible(true); // 设置字段可访问
  337 + try {
  338 + // 获取字段值,并检查是否为空
  339 + Object value = field.get(data);
  340 + if (value != null) {
  341 +
  342 + hasNonNullValue = true;
  343 + break;
  344 + }
  345 + } catch (IllegalAccessException e) {
  346 + e.printStackTrace();
  347 + }
  348 + }
  349 + if (hasNonNullValue) {
  350 + // 如果至少有一个字段值不为空,则保存数据到待保存列表
  351 + // 使用工号和姓名去基本信息表中检查是否存在对应的员工信息
  352 + b employeeExists = checkEmployeeExists(data);
  353 +
  354 + if (employeeExists != null) {
  355 + // 如果员工存在,则保存这条数据到数据库中
  356 + shuju.add(data);
  357 + } else {
  358 + // 获取 data 对象的类
  359 + Class<?> clazz2 = data.getClass();
  360 +
  361 + // 获取 工号 字段的值
  362 + Field jobId = clazz2.getDeclaredField("jobId");
  363 + jobId.setAccessible(true); // 设置可访问性,因为字段可能是私有的
  364 + String jobid = (String) jobId.get(data);
  365 + // 获取 姓名 字段的值
  366 + Field name = clazz2.getDeclaredField("userName");
  367 + name.setAccessible(true); // 设置可访问性,因为字段可能是私有的
  368 + String userName = (String) name.get(data);
  369 + // 如果员工不存在,则记录下当前数据的位置信息,比如行号
  370 + int rowNumber = rowIndex + 1; // 行号从1开始
  371 + // 这里可以根据需要将数据位置信息存储起来,比如记录到日志中或者构建一个错误信息列表等
  372 + // 示例:errorList.add("第 " + rowNumber + " 行的工号 " + data.get工号() + "、姓名 " + data.get姓名() + " 不存在对应的员工");
  373 + // 或者直接在此处输出错误信息
  374 + return Result.error("导入失败,第 " + rowNumber + " 行的工号 " + jobid+ "、姓名 " + userName + " 不存在对应的员工");
  375 + }
  376 + }
338 } 377 }
  378 +
  379 +
339 } 380 }
340 //400条 saveBatch消耗时间1592毫秒 循环插入消耗时间1947毫秒 381 //400条 saveBatch消耗时间1592毫秒 循环插入消耗时间1947毫秒
341 //1200条 saveBatch消耗时间3687毫秒 循环插入消耗时间5212毫秒 382 //1200条 saveBatch消耗时间3687毫秒 循环插入消耗时间5212毫秒
  383 + if(shuju.size()<=0){
  384 + return Result.error("文件数据为空");
  385 + }
  386 + System.out.println("导入的数据:"+shuju);
342 service.saveBatch(shuju); 387 service.saveBatch(shuju);
343 log.info("消耗时间" + (System.currentTimeMillis() - start) + "毫秒"); 388 log.info("消耗时间" + (System.currentTimeMillis() - start) + "毫秒");
344 //update-end-author:taoyan date:20190528 for:批量插入数据 389 //update-end-author:taoyan date:20190528 for:批量插入数据
345 - if(i==0){  
346 - return Result.error("导入的数据不存在基础信息表");  
347 - }  
348 return Result.ok("文件导入成功!数据行数:" + shuju.size()); 390 return Result.ok("文件导入成功!数据行数:" + shuju.size());
349 }catch (ExcelImportException e){ 391 }catch (ExcelImportException e){
350 return Result.error("文件导入失败,导入数据格式错误"); 392 return Result.error("文件导入失败,导入数据格式错误");