|  | @@ -1,5 +1,6 @@
 | 
	
		
			
				|  |  |  package org.jeecg.modules.baseCode.controller;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +import java.io.IOException;
 | 
	
		
			
				|  |  |  import java.util.Arrays;
 | 
	
		
			
				|  |  |  import java.util.HashMap;
 | 
	
		
			
				|  |  |  import java.util.List;
 | 
	
	
		
			
				|  | @@ -21,8 +22,12 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 | 
	
		
			
				|  |  |  import lombok.extern.slf4j.Slf4j;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  import org.jeecg.common.system.base.controller.JeecgController;
 | 
	
		
			
				|  |  | +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 io.swagger.annotations.Api;
 | 
	
		
			
				|  |  |  import io.swagger.annotations.ApiOperation;
 | 
	
	
		
			
				|  | @@ -212,7 +217,72 @@ public class BaseShipArchiveController extends JeecgController<BaseShipArchive,
 | 
	
		
			
				|  |  |      @RequiresPermissions("baseCode:base_ship_archive:importExcel")
 | 
	
		
			
				|  |  |      @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
 | 
	
		
			
				|  |  |      public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
 | 
	
		
			
				|  |  | -        return super.importExcel(request, response, BaseShipArchive.class);
 | 
	
		
			
				|  |  | +        return  importExcel(request, response, BaseShipArchive.class);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 通过excel导入数据
 | 
	
		
			
				|  |  | +     *
 | 
	
		
			
				|  |  | +     * @param request
 | 
	
		
			
				|  |  | +     * @param response
 | 
	
		
			
				|  |  | +     * @return
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    protected Result<?> importExcel(HttpServletRequest request, HttpServletResponse response, Class<BaseShipArchive> 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<BaseShipArchive> list = ExcelImportUtil.importExcel(file.getInputStream(), clazz, params);
 | 
	
		
			
				|  |  | +                //update-begin-author:taoyan date:20190528 for:批量插入数据
 | 
	
		
			
				|  |  | +                long start = System.currentTimeMillis();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                for(BaseShipArchive o:list){
 | 
	
		
			
				|  |  | +                    String imo = o.getImo();
 | 
	
		
			
				|  |  | +                    QueryWrapper<BaseShipArchive> queryWrapper = new QueryWrapper();
 | 
	
		
			
				|  |  | +                    queryWrapper.eq("imo", imo);
 | 
	
		
			
				|  |  | +                    queryWrapper.eq("del_flag", "0");
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                    List<BaseShipArchive> list2 = baseShipArchiveService.list(queryWrapper);
 | 
	
		
			
				|  |  | +                    if (list2.size() != 0) {
 | 
	
		
			
				|  |  | +                        continue;
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                    service.save(o);
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +//                service.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("文件导入失败!");
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  }
 |