|
@@ -18,9 +18,14 @@ import org.jeecgframework.poi.excel.entity.ExportParams; |
|
@@ -18,9 +18,14 @@ import org.jeecgframework.poi.excel.entity.ExportParams; |
|
18
|
import org.jeecgframework.poi.excel.entity.ImportParams;
|
18
|
import org.jeecgframework.poi.excel.entity.ImportParams;
|
|
19
|
import org.jeecgframework.poi.excel.entity.enmus.ExcelType;
|
19
|
import org.jeecgframework.poi.excel.entity.enmus.ExcelType;
|
|
20
|
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
|
20
|
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
|
|
|
|
21
|
+import org.jeecgframework.poi.exception.excel.ExcelImportException;
|
|
21
|
import org.springframework.beans.factory.annotation.Autowired;
|
22
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
22
|
import org.springframework.beans.factory.annotation.Value;
|
23
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
24
|
+import org.springframework.transaction.TransactionStatus;
|
|
|
|
25
|
+import org.springframework.transaction.annotation.EnableTransactionManagement;
|
|
|
|
26
|
+import org.springframework.transaction.annotation.Transactional;
|
|
23
|
import org.springframework.transaction.interceptor.TransactionAspectSupport;
|
27
|
import org.springframework.transaction.interceptor.TransactionAspectSupport;
|
|
|
|
28
|
+import org.springframework.transaction.support.DefaultTransactionDefinition;
|
|
24
|
import org.springframework.web.multipart.MultipartFile;
|
29
|
import org.springframework.web.multipart.MultipartFile;
|
|
25
|
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
30
|
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
|
26
|
import org.springframework.web.servlet.ModelAndView;
|
31
|
import org.springframework.web.servlet.ModelAndView;
|
|
@@ -39,6 +44,7 @@ import java.util.stream.Collectors; |
|
@@ -39,6 +44,7 @@ import java.util.stream.Collectors; |
|
39
|
* @Version: 1.0
|
44
|
* @Version: 1.0
|
|
40
|
*/
|
45
|
*/
|
|
41
|
@Slf4j
|
46
|
@Slf4j
|
|
|
|
47
|
+
|
|
42
|
public class JeecgController2<T, t, b, V extends IService<t>, S extends IService<T>, B extends IService<b>> {
|
48
|
public class JeecgController2<T, t, b, V extends IService<t>, S extends IService<T>, B extends IService<b>> {
|
|
43
|
/**
|
49
|
/**
|
|
44
|
* issues/2933 JeecgController注入service时改用protected修饰,能避免重复引用service
|
50
|
* issues/2933 JeecgController注入service时改用protected修饰,能避免重复引用service
|
|
@@ -295,6 +301,7 @@ public class JeecgController2<T, t, b, V extends IService<t>, S extends IService |
|
@@ -295,6 +301,7 @@ public class JeecgController2<T, t, b, V extends IService<t>, S extends IService |
|
295
|
}
|
301
|
}
|
|
296
|
|
302
|
|
|
297
|
//工资导入
|
303
|
//工资导入
|
|
|
|
304
|
+
|
|
298
|
protected Result<?> importExcelgz(HttpServletRequest request, HttpServletResponse response, Class<T> clazz, Class<b> base) {
|
305
|
protected Result<?> importExcelgz(HttpServletRequest request, HttpServletResponse response, Class<T> clazz, Class<b> base) {
|
|
299
|
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
|
306
|
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
|
|
300
|
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
|
307
|
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
|
|
@@ -307,22 +314,22 @@ public class JeecgController2<T, t, b, V extends IService<t>, S extends IService |
|
@@ -307,22 +314,22 @@ public class JeecgController2<T, t, b, V extends IService<t>, S extends IService |
|
307
|
|
314
|
|
|
308
|
// params.setKeyIndex(1);
|
315
|
// params.setKeyIndex(1);
|
|
309
|
params.setNeedSave(true);
|
316
|
params.setNeedSave(true);
|
|
|
|
317
|
+ int i = 0;
|
|
310
|
try {
|
318
|
try {
|
|
311
|
List<T> list = ExcelImportUtil.importExcel(file.getInputStream(), clazz, params);
|
319
|
List<T> list = ExcelImportUtil.importExcel(file.getInputStream(), clazz, params);
|
|
312
|
|
320
|
|
|
313
|
-
|
321
|
+ List<T> shuju = new ArrayList<>();
|
|
314
|
//update-begin-author:taoyan date:20190528 for:批量插入数据
|
322
|
//update-begin-author:taoyan date:20190528 for:批量插入数据
|
|
315
|
long start = System.currentTimeMillis();
|
323
|
long start = System.currentTimeMillis();
|
|
316
|
- int i = 0;
|
|
|
|
317
|
- // 遍历Excel数据
|
|
|
|
318
|
- for (T data : list) {
|
|
|
|
319
|
|
324
|
|
|
|
|
325
|
+ // 遍历Excel数据
|
|
|
|
326
|
+ for (T data :list) {
|
|
320
|
// 使用工号和姓名去基本信息表中检查是否存在对应的员工信息
|
327
|
// 使用工号和姓名去基本信息表中检查是否存在对应的员工信息
|
|
321
|
b employeeExists = checkEmployeeExists(data);
|
328
|
b employeeExists = checkEmployeeExists(data);
|
|
322
|
|
329
|
|
|
323
|
if (employeeExists != null) {
|
330
|
if (employeeExists != null) {
|
|
324
|
// 如果员工存在,则保存这条数据到数据库中
|
331
|
// 如果员工存在,则保存这条数据到数据库中
|
|
325
|
- service.save(data);
|
332
|
+ shuju.add(data);
|
|
326
|
i++;
|
333
|
i++;
|
|
327
|
} else {
|
334
|
} else {
|
|
328
|
// 如果员工不存在,则跳过这条数据或进行其他相应的处理
|
335
|
// 如果员工不存在,则跳过这条数据或进行其他相应的处理
|
|
@@ -332,24 +339,33 @@ public class JeecgController2<T, t, b, V extends IService<t>, S extends IService |
|
@@ -332,24 +339,33 @@ public class JeecgController2<T, t, b, V extends IService<t>, S extends IService |
|
332
|
}
|
339
|
}
|
|
333
|
//400条 saveBatch消耗时间1592毫秒 循环插入消耗时间1947毫秒
|
340
|
//400条 saveBatch消耗时间1592毫秒 循环插入消耗时间1947毫秒
|
|
334
|
//1200条 saveBatch消耗时间3687毫秒 循环插入消耗时间5212毫秒
|
341
|
//1200条 saveBatch消耗时间3687毫秒 循环插入消耗时间5212毫秒
|
|
|
|
342
|
+ service.saveBatch(shuju);
|
|
335
|
log.info("消耗时间" + (System.currentTimeMillis() - start) + "毫秒");
|
343
|
log.info("消耗时间" + (System.currentTimeMillis() - start) + "毫秒");
|
|
336
|
//update-end-author:taoyan date:20190528 for:批量插入数据
|
344
|
//update-end-author:taoyan date:20190528 for:批量插入数据
|
|
337
|
- return Result.ok("文件导入成功!数据行数:" + i);
|
345
|
+ if(i==0){
|
|
|
|
346
|
+ return Result.error("导入的数据不存在基础信息表");
|
|
|
|
347
|
+ }
|
|
|
|
348
|
+ return Result.ok("文件导入成功!数据行数:" + shuju.size());
|
|
|
|
349
|
+ }catch (ExcelImportException e){
|
|
|
|
350
|
+ return Result.error("文件导入失败,导入数据格式错误");
|
|
338
|
} catch (Exception e) {
|
351
|
} catch (Exception e) {
|
|
339
|
//update-begin-author:taoyan date:20211124 for: 导入数据重复增加提示
|
352
|
//update-begin-author:taoyan date:20211124 for: 导入数据重复增加提示
|
|
340
|
String msg = e.getMessage();
|
353
|
String msg = e.getMessage();
|
|
341
|
log.error(msg, e);
|
354
|
log.error(msg, e);
|
|
342
|
- if (msg != null && msg.indexOf("Duplicate entry") >= 0) {
|
|
|
|
343
|
- TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); // 手动回滚事务
|
355
|
+ if (msg != null && (msg.indexOf("Duplicate entry") >= 0 || msg.indexOf("java.sql.SQLIntegrityConstraintViolationException: Duplicate entry")>=0)) {
|
|
344
|
return Result.error("文件导入失败:有重复数据!");
|
356
|
return Result.error("文件导入失败:有重复数据!");
|
|
345
|
- }else if (msg != null && msg.indexOf("Field 'violation_fines' doesn't have a default value") >= 0){
|
|
|
|
346
|
- return Result.error("文件导入失败,导入数据不能为空" );
|
|
|
|
347
|
- }
|
|
|
|
348
|
- else {
|
357
|
+ }else if (msg!=null && msg.indexOf("Field 'salary_month' doesn't have a default value")>=0) {
|
|
|
|
358
|
+ return Result.error("文件导入失败,导入月份不能为空");
|
|
|
|
359
|
+ } else if (msg!=null && msg.indexOf("Field 'day' doesn't have a default value")>=0) {
|
|
|
|
360
|
+ return Result.error("文件导入失败,导入出勤天数不能为空");
|
|
|
|
361
|
+ } else if (msg != null && msg.indexOf("Field 'working_hours' doesn't have a default value") >= 0) {
|
|
|
|
362
|
+ return Result.error("文件导入失败,导入标准工时数不能为空");
|
|
|
|
363
|
+ } else {
|
|
|
|
364
|
+
|
|
349
|
return Result.error("文件导入失败:" + e.getMessage());
|
365
|
return Result.error("文件导入失败:" + e.getMessage());
|
|
350
|
}
|
366
|
}
|
|
351
|
//update-end-author:taoyan date:20211124 for: 导入数据重复增加提示
|
367
|
//update-end-author:taoyan date:20211124 for: 导入数据重复增加提示
|
|
352
|
- } finally {
|
368
|
+ }finally {
|
|
353
|
try {
|
369
|
try {
|
|
354
|
file.getInputStream().close();
|
370
|
file.getInputStream().close();
|
|
355
|
} catch (IOException e) {
|
371
|
} catch (IOException e) {
|