|
@@ -15,15 +15,18 @@ import org.jeecg.modules.itdmDevice.convert.ItdmDeviceConvert1;
|
|
|
import org.jeecg.modules.itdmDevice.entity.ItdmDevice;
|
|
|
import org.jeecg.modules.itdmDevice.service.IItdmDeviceService;
|
|
|
import org.jeecg.modules.itdmDevice.vo.ItdmDeviceXLListVO;
|
|
|
+import org.jeecgframework.poi.excel.ExcelImportUtil;
|
|
|
+import org.jeecgframework.poi.excel.entity.ImportParams;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+import org.springframework.web.multipart.MultipartHttpServletRequest;
|
|
|
import org.springframework.web.servlet.ModelAndView;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.List;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
@@ -206,7 +209,65 @@ public class ItdmDeviceController extends JeecgController<ItdmDevice, IItdmDevic
|
|
|
//@RequiresPermissions("itdm_device:importExcel")
|
|
|
@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
|
|
|
public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
|
|
|
- return super.importExcel(request, response, ItdmDevice.class);
|
|
|
+ return importExcel1(request, response, ItdmDevice.class);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 通过excel导入数据
|
|
|
+ *
|
|
|
+ * @param request
|
|
|
+ * @param response
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Result<?> importExcel1(HttpServletRequest request, HttpServletResponse response, Class<ItdmDevice> clazz) {
|
|
|
+ MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
|
|
|
+ Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
|
|
|
+ for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
|
|
|
+ // 获取上传文件对象
|
|
|
+ MultipartFile file = entity.getValue();
|
|
|
+ ImportParams params = new ImportParams();
|
|
|
+ params.setTitleRows(2);
|
|
|
+ params.setHeadRows(1);
|
|
|
+ params.setNeedSave(true);
|
|
|
+ try {
|
|
|
+ List<ItdmDevice> list1 = ExcelImportUtil.importExcel(file.getInputStream(), clazz, params);
|
|
|
+ List<ItdmDevice> list = list1.stream().filter(i->{
|
|
|
+ Map<String, Object> columnMap = new HashMap<>();
|
|
|
+ columnMap.put("device_name",i.getDeviceName());
|
|
|
+ List<ItdmDevice> deviceList = itdmDeviceService.listByMap(columnMap);
|
|
|
+ if(deviceList==null || deviceList.isEmpty()) return true;
|
|
|
+ return false;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ //update-begin-author:taoyan date:20190528 for:批量插入数据
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
+ if(list!=null && !list.isEmpty()){
|
|
|
+ System.out.println("=============================================================");
|
|
|
+ itdmDeviceService.saveBatch(list);
|
|
|
+ }
|
|
|
+ //400条 saveBatch消耗时间1592毫秒 循环插入消耗时间1947毫秒
|
|
|
+ //1200条 saveBatch消耗时间3687毫秒 循环插入消耗时间5212毫秒
|
|
|
+ log.info("消耗时间" + (System.currentTimeMillis() - start) + "毫秒");
|
|
|
+ //update-end-author:taoyan date:20190528 for:批量插入数据
|
|
|
+ return Result.ok("文件导入成功!数据行数:" + list.size());
|
|
|
+ } 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){
|
|
|
+ return Result.error("文件导入失败:有重复数据!");
|
|
|
+ }else{
|
|
|
+ return Result.error("文件导入失败:" + e.getMessage());
|
|
|
+ }
|
|
|
+ //update-end-author:taoyan date:20211124 for: 导入数据重复增加提示
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ file.getInputStream().close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return Result.error("文件导入失败!");
|
|
|
+ }
|
|
|
+
|
|
|
}
|