|
@@ -3,6 +3,7 @@ package com.persagy.proxy.report.controller;
|
|
import cn.hutool.core.collection.CollUtil;
|
|
import cn.hutool.core.collection.CollUtil;
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
+import cn.hutool.poi.excel.ExcelReader;
|
|
import cn.hutool.poi.excel.ExcelUtil;
|
|
import cn.hutool.poi.excel.ExcelUtil;
|
|
import cn.hutool.poi.excel.RowUtil;
|
|
import cn.hutool.poi.excel.RowUtil;
|
|
import cn.hutool.poi.excel.WorkbookUtil;
|
|
import cn.hutool.poi.excel.WorkbookUtil;
|
|
@@ -483,7 +484,7 @@ public class RelationReportController {
|
|
public AdmResponse importFile(@RequestParam("file") MultipartFile file, @RequestParam String relType,
|
|
public AdmResponse importFile(@RequestParam("file") MultipartFile file, @RequestParam String relType,
|
|
@RequestParam(required = false) String zoneType, HttpServletRequest request, HttpServletResponse response) {
|
|
@RequestParam(required = false) String zoneType, HttpServletRequest request, HttpServletResponse response) {
|
|
String failure = null;
|
|
String failure = null;
|
|
- XSSFWorkbook workbook = null;
|
|
|
|
|
|
+ Workbook workbook = null;
|
|
FileOutputStream fileOutputStream = null;
|
|
FileOutputStream fileOutputStream = null;
|
|
try {
|
|
try {
|
|
if (file == null) {
|
|
if (file == null) {
|
|
@@ -503,86 +504,77 @@ public class RelationReportController {
|
|
// 批量更新数据
|
|
// 批量更新数据
|
|
List<ObjectNode> relationObjects = new ArrayList<ObjectNode>();
|
|
List<ObjectNode> relationObjects = new ArrayList<ObjectNode>();
|
|
InstanceUrlParam context = AdmContextUtil.toDmpContext();
|
|
InstanceUrlParam context = AdmContextUtil.toDmpContext();
|
|
- workbook = new XSSFWorkbook(file.getInputStream());
|
|
|
|
- XSSFSheet sheet = workbook.getSheet("关系维护");
|
|
|
|
- // 获取识别码的属性
|
|
|
|
- String code = sheet.getRow(2).getCell(1).toString();
|
|
|
|
- int lastRowNum = sheet.getLastRowNum();
|
|
|
|
- for (int i = 5; i <= lastRowNum; i++) {
|
|
|
|
- XSSFRow xssfRow = sheet.getRow(i);
|
|
|
|
- if (xssfRow == null) {
|
|
|
|
- continue;
|
|
|
|
|
|
+ ExcelReader excelReader = ExcelUtil.getReader(file.getInputStream(), "关系维护");
|
|
|
|
+ // 获取真实行数
|
|
|
|
+ int rowCount = excelReader.getRowCount();
|
|
|
|
+ if (rowCount<=5){
|
|
|
|
+ // 没有数据,直接返回成功
|
|
|
|
+ return AdmResponse.success(Lists.newArrayList(AdmExcelUtil.getImportResult(graphId, successNum, failurNum)));
|
|
|
|
+ }
|
|
|
|
+ String code = StrUtil.utf8Str(excelReader.readCellValue(1,2));
|
|
|
|
+ if (StrUtil.isBlank(code)){
|
|
|
|
+ return AdmResponse.failure("未选择识别编码");
|
|
|
|
+ }
|
|
|
|
+ for (int i = 5; i < rowCount; i=i+1000) {
|
|
|
|
+ List<AdmRelationObject> admRelationObjects = new ArrayList<>();
|
|
|
|
+ int index = Math.min(i + 1000, rowCount);
|
|
|
|
+ for (int j = i; j < index; j++) {
|
|
|
|
+ String masterCodeCell = StrUtil.utf8Str(excelReader.readCellValue(0,j));
|
|
|
|
+ String slaveCodeCell = StrUtil.utf8Str(excelReader.readCellValue(8,j));
|
|
|
|
+ String masterIdCell = StrUtil.utf8Str(excelReader.readCellValue(3,j));
|
|
|
|
+ String slaveIdCell = StrUtil.utf8Str(excelReader.readCellValue(11,j));
|
|
|
|
+ AdmRelationObject relationObject = new AdmRelationObject();
|
|
|
|
+ relationObject.setMasterCode(masterCodeCell);
|
|
|
|
+ relationObject.setSlaveCode(slaveCodeCell);
|
|
|
|
+ relationObject.setMasterId(masterIdCell);
|
|
|
|
+ relationObject.setSlaveId(slaveIdCell);
|
|
|
|
+ admRelationObjects.add(relationObject);
|
|
}
|
|
}
|
|
- // 封装cell数据
|
|
|
|
- XSSFCell masterCodeCell = xssfRow.getCell(0);
|
|
|
|
- XSSFCell slaveCodeCell = xssfRow.getCell(8);
|
|
|
|
- XSSFCell masterIdCell = xssfRow.getCell(3);
|
|
|
|
- XSSFCell slaveIdCell = xssfRow.getCell(11);
|
|
|
|
-
|
|
|
|
- AdmRelationObject relationObject = new AdmRelationObject();
|
|
|
|
- relationObject.setMasterCode(masterCodeCell == null ? null : masterCodeCell.getStringCellValue());
|
|
|
|
- relationObject.setSlaveCode(slaveCodeCell == null ? null : slaveCodeCell.getStringCellValue());
|
|
|
|
- relationObject.setMasterId(masterIdCell == null ? null : masterIdCell.getStringCellValue());
|
|
|
|
- relationObject.setSlaveId(slaveIdCell == null ? null : slaveIdCell.getStringCellValue());
|
|
|
|
-
|
|
|
|
- // 校验数据
|
|
|
|
- Object result = this.relationObjectContext.checkRelationObject(relationObject, context.getGroupCode(),
|
|
|
|
|
|
+ Map<String, Object> resultMap = this.relationObjectContext.checkRelationObjects(admRelationObjects, context.getGroupCode(),
|
|
context.getProjectId(), graphAndRelKey, code, typeEnum);
|
|
context.getProjectId(), graphAndRelKey, code, typeEnum);
|
|
- if (result instanceof String) {
|
|
|
|
- failurNum++;
|
|
|
|
- xssfRow.createCell(16).setCellValue((String)result);
|
|
|
|
- continue;
|
|
|
|
|
|
+ for (int j = i; j < index; j++) {
|
|
|
|
+ String masterCodeCell = StrUtil.utf8Str(excelReader.readCellValue(0, j));
|
|
|
|
+ String slaveCodeCell = StrUtil.utf8Str(excelReader.readCellValue(8, j));
|
|
|
|
+ String key = StrUtil.toString(masterCodeCell) + StrUtil.UNDERLINE +StrUtil.toString(slaveCodeCell);
|
|
|
|
+ Object str = resultMap.getOrDefault(key, "对象不存在");
|
|
|
|
+ if (str instanceof String) {
|
|
|
|
+ failurNum++;
|
|
|
|
+ CellUtil.setCellValue(CellUtil.getOrCreateCell(RowUtil.getOrCreateRow(excelReader.getSheet(),j),16),str,null);
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ ObjectNode relation = (ObjectNode) str;
|
|
|
|
+ graphId = relation.get("graphId") == null ? graphId : relation.get("graphId").asText();
|
|
|
|
+ // 每一千条数据,批量新增一次
|
|
|
|
+ relationObjects.add(relation);
|
|
|
|
+ rowNums.add(j);
|
|
}
|
|
}
|
|
- ObjectNode relation = (ObjectNode) result;
|
|
|
|
- graphId = relation.get("graphId") == null ? graphId : relation.get("graphId").asText();
|
|
|
|
-
|
|
|
|
- // 每一千条数据,批量新增一次
|
|
|
|
- rowNums.add(i);
|
|
|
|
- relationObjects.add(relation);
|
|
|
|
- if (relationObjects.size() >= 1000) {
|
|
|
|
|
|
+ if (CollUtil.isNotEmpty(relationObjects)) {
|
|
boolean objects = this.relationObjectContext.saveRelationObjects(relationObjects, context.getGroupCode(),
|
|
boolean objects = this.relationObjectContext.saveRelationObjects(relationObjects, context.getGroupCode(),
|
|
context.getProjectId(), graphAndRelKey);
|
|
context.getProjectId(), graphAndRelKey);
|
|
if (objects) {
|
|
if (objects) {
|
|
successNum += relationObjects.size();
|
|
successNum += relationObjects.size();
|
|
for (int rowNum : rowNums) {
|
|
for (int rowNum : rowNums) {
|
|
- sheet.getRow(rowNum).createCell(16).setCellValue("关联成功");
|
|
|
|
|
|
+ CellUtil.setCellValue(CellUtil.getOrCreateCell(RowUtil.getOrCreateRow(excelReader.getSheet(),rowNum),16),"关联成功",null);
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
failurNum += relationObjects.size();
|
|
failurNum += relationObjects.size();
|
|
for (int rowNum : rowNums) {
|
|
for (int rowNum : rowNums) {
|
|
- sheet.getRow(rowNum).createCell(16).setCellValue("关联失败");
|
|
|
|
|
|
+ CellUtil.setCellValue(CellUtil.getOrCreateCell(RowUtil.getOrCreateRow(excelReader.getSheet(),rowNum),16),"关联失败",null);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
rowNums.clear();
|
|
rowNums.clear();
|
|
relationObjects.clear();
|
|
relationObjects.clear();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- // 最后一次遗留数据录入
|
|
|
|
- if (CollectionUtil.isNotEmpty(relationObjects)) {
|
|
|
|
- boolean objects = this.relationObjectContext.saveRelationObjects(relationObjects, context.getGroupCode(), context.getProjectId(), graphAndRelKey);
|
|
|
|
- if (objects) {
|
|
|
|
- successNum += relationObjects.size();
|
|
|
|
- for (int rowNum : rowNums) {
|
|
|
|
- sheet.getRow(rowNum).createCell(16).setCellValue("关联成功");
|
|
|
|
- }
|
|
|
|
- } else {
|
|
|
|
- failurNum += relationObjects.size();
|
|
|
|
- for (int rowNum : rowNums) {
|
|
|
|
- sheet.getRow(rowNum).createCell(16).setCellValue("关联失败");
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- try {
|
|
|
|
|
|
+ try {
|
|
// 调用中台存储完成之后,此文件存储至本地
|
|
// 调用中台存储完成之后,此文件存储至本地
|
|
String fileName = context.getProjectId() + AdmCommonConstant.LINE_THROUGH + relType + AdmCommonConstant.EXCEL_SUFFIX_XLSX;
|
|
String fileName = context.getProjectId() + AdmCommonConstant.LINE_THROUGH + relType + AdmCommonConstant.EXCEL_SUFFIX_XLSX;
|
|
File templateFile = AdmExcelUtil.createTemplateFile(AdmCommonConstant.SERVER_ROOT_PATH + AdmExcelUtil.TEMP_PAHT, fileName);
|
|
File templateFile = AdmExcelUtil.createTemplateFile(AdmCommonConstant.SERVER_ROOT_PATH + AdmExcelUtil.TEMP_PAHT, fileName);
|
|
fileOutputStream = new FileOutputStream(templateFile);
|
|
fileOutputStream = new FileOutputStream(templateFile);
|
|
- workbook.write(fileOutputStream);
|
|
|
|
|
|
+ excelReader.getWorkbook().write(fileOutputStream);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
log.error("文件存储失败", e);
|
|
log.error("文件存储失败", e);
|
|
}
|
|
}
|
|
-
|
|
|
|
return AdmResponse.success(Lists.newArrayList(AdmExcelUtil.getImportResult(graphId, successNum, failurNum)));
|
|
return AdmResponse.success(Lists.newArrayList(AdmExcelUtil.getImportResult(graphId, successNum, failurNum)));
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
log.error("文件导入失败", e);
|
|
log.error("文件导入失败", e);
|