|
|
|
@ -1,8 +1,12 @@
|
|
|
|
|
package org.jeecg.modules.ocr.controller;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.function.Function;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
@ -27,12 +31,16 @@ import org.jeecg.modules.ocr.service.IOcrMetadataConfigService;
|
|
|
|
|
import org.jeecg.common.system.base.controller.JeecgController;
|
|
|
|
|
import org.jeecg.modules.ocr.utils.DownloadTemplateUtil;
|
|
|
|
|
import org.jeecg.modules.ocr.vo.OcrMetadataConfigVo;
|
|
|
|
|
import org.jeecgframework.poi.excel.ExcelImportUtil;
|
|
|
|
|
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
|
|
|
|
|
import org.jeecgframework.poi.excel.entity.ExportParams;
|
|
|
|
|
import org.jeecgframework.poi.excel.entity.ImportParams;
|
|
|
|
|
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
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;
|
|
|
|
@ -219,16 +227,10 @@ public class OcrMetadataConfigController extends JeecgController<OcrMetadataConf
|
|
|
|
|
}
|
|
|
|
|
if(true){
|
|
|
|
|
LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
|
|
|
|
|
// Step.2 获取导出数据
|
|
|
|
|
// Step.3 AutoPoi 导出Excel
|
|
|
|
|
|
|
|
|
|
//此处设置的filename无效 ,前端会重更新设置一下
|
|
|
|
|
mv.addObject(NormalExcelConstants.FILE_NAME, title);
|
|
|
|
|
mv.addObject(NormalExcelConstants.CLASS, OcrMetadataConfigVo.class);
|
|
|
|
|
//update-begin--Author:liusq Date:20210126 for:图片导出报错,ImageBasePath未设置--------------------
|
|
|
|
|
ExportParams exportParams = new ExportParams(title + "报表", "导出人:" + sysUser.getRealname(), title);
|
|
|
|
|
exportParams.setImageBasePath(jeecgBaseConfig.getPath().getUpload());
|
|
|
|
|
//update-end--Author:liusq Date:20210126 for:图片导出报错,ImageBasePath未设置----------------------
|
|
|
|
|
mv.addObject(NormalExcelConstants.PARAMS, exportParams);
|
|
|
|
|
mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
|
|
|
|
|
}
|
|
|
|
@ -254,7 +256,75 @@ public class OcrMetadataConfigController extends JeecgController<OcrMetadataConf
|
|
|
|
|
// @RequiresPermissions("org.jeecg.modules.ocr:ocr_metadata_config:importExcel")
|
|
|
|
|
@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
|
|
|
|
|
public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
|
|
|
|
|
return super.importExcel(request, response, OcrMetadataConfig.class);
|
|
|
|
|
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<OcrMetadataConfigVo> list = ExcelImportUtil.importExcel(file.getInputStream(), OcrMetadataConfigVo.class, params);
|
|
|
|
|
AssertUtils.hasSize(list,"未获取到数据!");
|
|
|
|
|
//判断重复字段名
|
|
|
|
|
Map<String, Long> countMap = list.stream().map(o->o.getConfigName()).collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
|
|
|
|
|
AssertUtils.isTrue(!(countMap.keySet().size()>0),"不可有重复的配置名称!");
|
|
|
|
|
List<OcrMetadataConfigSaveModel> saveModelList=new ArrayList<>();
|
|
|
|
|
OcrMetadataConfigSaveModel ocrMetadataConfigSaveModel=new OcrMetadataConfigSaveModel();
|
|
|
|
|
List<OcrMetadataConfigDetail> ocrMetadataConfigDetailList=new ArrayList();
|
|
|
|
|
OcrMetadataConfigDetail ocrMetadataConfigDetail=new OcrMetadataConfigDetail();
|
|
|
|
|
for (OcrMetadataConfigVo configVo : list) {
|
|
|
|
|
ocrMetadataConfigSaveModel.setConfigName(configVo.getConfigName());
|
|
|
|
|
String resultName = configVo.getResultName();
|
|
|
|
|
String getField = configVo.getGetField();
|
|
|
|
|
List<String> resultList=new ArrayList<>();
|
|
|
|
|
List<String> getFieldList=new ArrayList<>();
|
|
|
|
|
if (StringUtils.isNotBlank(resultName)) {
|
|
|
|
|
resultList=Arrays.asList(resultName.split(","));
|
|
|
|
|
}
|
|
|
|
|
if (StringUtils.isNotBlank(getField)) {
|
|
|
|
|
getFieldList=Arrays.asList(getField.split(","));
|
|
|
|
|
}
|
|
|
|
|
AssertUtils.isTrue(resultList.size()==getFieldList.size(),String.format("配置名称[%s]-的字段信息不整齐",configVo.getConfigName()));
|
|
|
|
|
ocrMetadataConfigDetailList=new ArrayList<>();
|
|
|
|
|
for (int i = 0; i < resultList.size(); i++) {
|
|
|
|
|
ocrMetadataConfigDetail=new OcrMetadataConfigDetail();
|
|
|
|
|
ocrMetadataConfigDetail.setFieldName(resultList.get(i));
|
|
|
|
|
ocrMetadataConfigDetail.setGetField(getFieldList.get(i));
|
|
|
|
|
ocrMetadataConfigDetailList.add(ocrMetadataConfigDetail);
|
|
|
|
|
}
|
|
|
|
|
ocrMetadataConfigSaveModel.setOcrMetadataConfigDetailList(ocrMetadataConfigDetailList);
|
|
|
|
|
saveModelList.add(ocrMetadataConfigSaveModel);
|
|
|
|
|
}
|
|
|
|
|
//update-begin-author:taoyan date:20190528 for:批量插入数据
|
|
|
|
|
long start = System.currentTimeMillis();
|
|
|
|
|
ocrMetadataConfigService.saveModelBatch(saveModelList);
|
|
|
|
|
//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("文件导入失败!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|