|
...
|
...
|
@@ -18,9 +18,14 @@ 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.TransactionStatus;
|
|
|
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
import org.springframework.transaction.interceptor.TransactionAspectSupport;
|
|
|
|
import org.springframework.transaction.support.DefaultTransactionDefinition;
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
|
|
|
import org.springframework.web.servlet.ModelAndView;
|
|
...
|
...
|
@@ -39,6 +44,7 @@ import java.util.stream.Collectors; |
|
|
|
* @Version: 1.0
|
|
|
|
*/
|
|
|
|
@Slf4j
|
|
|
|
|
|
|
|
public class JeecgController2<T, t, b, V extends IService<t>, S extends IService<T>, B extends IService<b>> {
|
|
|
|
/**
|
|
|
|
* issues/2933 JeecgController注入service时改用protected修饰,能避免重复引用service
|
|
...
|
...
|
@@ -295,6 +301,7 @@ public class JeecgController2<T, t, b, V extends IService<t>, S extends IService |
|
|
|
}
|
|
|
|
|
|
|
|
//工资导入
|
|
|
|
|
|
|
|
protected Result<?> importExcelgz(HttpServletRequest request, HttpServletResponse response, Class<T> clazz, Class<b> base) {
|
|
|
|
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
|
|
|
|
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
|
|
...
|
...
|
@@ -307,22 +314,22 @@ 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);
|
|
|
|
|
|
|
|
|
|
|
|
List<T> shuju = new ArrayList<>();
|
|
|
|
//update-begin-author:taoyan date:20190528 for:批量插入数据
|
|
|
|
long start = System.currentTimeMillis();
|
|
|
|
int i = 0;
|
|
|
|
// 遍历Excel数据
|
|
|
|
for (T data : list) {
|
|
|
|
|
|
|
|
// 遍历Excel数据
|
|
|
|
for (T data :list) {
|
|
|
|
// 使用工号和姓名去基本信息表中检查是否存在对应的员工信息
|
|
|
|
b employeeExists = checkEmployeeExists(data);
|
|
|
|
|
|
|
|
if (employeeExists != null) {
|
|
|
|
// 如果员工存在,则保存这条数据到数据库中
|
|
|
|
service.save(data);
|
|
|
|
shuju.add(data);
|
|
|
|
i++;
|
|
|
|
} else {
|
|
|
|
// 如果员工不存在,则跳过这条数据或进行其他相应的处理
|
|
...
|
...
|
@@ -332,24 +339,33 @@ public class JeecgController2<T, t, b, V extends IService<t>, S extends IService |
|
|
|
}
|
|
|
|
//400条 saveBatch消耗时间1592毫秒 循环插入消耗时间1947毫秒
|
|
|
|
//1200条 saveBatch消耗时间3687毫秒 循环插入消耗时间5212毫秒
|
|
|
|
service.saveBatch(shuju);
|
|
|
|
log.info("消耗时间" + (System.currentTimeMillis() - start) + "毫秒");
|
|
|
|
//update-end-author:taoyan date:20190528 for:批量插入数据
|
|
|
|
return Result.ok("文件导入成功!数据行数:" + i);
|
|
|
|
if(i==0){
|
|
|
|
return Result.error("导入的数据不存在基础信息表");
|
|
|
|
}
|
|
|
|
return Result.ok("文件导入成功!数据行数:" + shuju.size());
|
|
|
|
}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(); // 手动回滚事务
|
|
|
|
if (msg != null && (msg.indexOf("Duplicate entry") >= 0 || msg.indexOf("java.sql.SQLIntegrityConstraintViolationException: Duplicate entry")>=0)) {
|
|
|
|
return Result.error("文件导入失败:有重复数据!");
|
|
|
|
}else if (msg != null && msg.indexOf("Field 'violation_fines' doesn't have a default value") >= 0){
|
|
|
|
return Result.error("文件导入失败,导入数据不能为空" );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
}else if (msg!=null && msg.indexOf("Field 'salary_month' doesn't have a default value")>=0) {
|
|
|
|
return Result.error("文件导入失败,导入月份不能为空");
|
|
|
|
} else if (msg!=null && msg.indexOf("Field 'day' doesn't have a default value")>=0) {
|
|
|
|
return Result.error("文件导入失败,导入出勤天数不能为空");
|
|
|
|
} else if (msg != null && msg.indexOf("Field 'working_hours' doesn't have a default value") >= 0) {
|
|
|
|
return Result.error("文件导入失败,导入标准工时数不能为空");
|
|
|
|
} else {
|
|
|
|
|
|
|
|
return Result.error("文件导入失败:" + e.getMessage());
|
|
|
|
}
|
|
|
|
//update-end-author:taoyan date:20211124 for: 导入数据重复增加提示
|
|
|
|
} finally {
|
|
|
|
}finally {
|
|
|
|
try {
|
|
|
|
file.getInputStream().close();
|
|
|
|
} catch (IOException e) {
|
...
|
...
|
|