Преглед изворни кода

新增深化点导入接口

zhangqiankun пре 3 година
родитељ
комит
ed15df4de5

+ 188 - 0
src/main/java/com/persagy/proxy/adm/utils/AdmExcelUtil.java

@@ -0,0 +1,188 @@
+package com.persagy.proxy.adm.utils;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.springframework.util.ResourceUtils;
+
+import com.alibaba.fastjson.JSONObject;
+import com.google.common.collect.Lists;
+import com.persagy.proxy.adm.constant.AdmCommonConstant;
+import com.persagy.proxy.adm.request.AdmResponse;
+
+import lombok.extern.slf4j.Slf4j;
+
+/**
+ * 
+ * @version 1.0.0
+ * @company persagy 
+ * @author zhangqiankun
+ * @date 2021年10月21日 下午12:24:28
+ */
+@Slf4j
+public class AdmExcelUtil {
+
+	/**
+	 * 文件写入至本地
+	 * 
+	 * @param workbook
+	 * @param projectId
+	 * @param orgFileName
+	 * @return 
+	 */
+	public static String writeFile2Local(XSSFWorkbook workbook, String projectId, String orgFileName) {
+		FileOutputStream fileOutputStream = null;
+		String date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
+		// 重组文件名
+        String fileName = projectId + date + "." + orgFileName.substring(orgFileName.lastIndexOf(".") + 1);
+        try {
+    		// 调用中台存储完成之后,此文件存储至本地
+    		File templateFile = createTemplateFile(fileName);
+    		fileOutputStream = new FileOutputStream(templateFile);
+    		workbook.write(fileOutputStream);
+		} catch (Exception e) {
+			log.error("文件存储失败", e);
+		} finally {
+			if (fileOutputStream != null) {
+				try {
+					fileOutputStream.close();
+				} catch (IOException e) {}
+			}
+		}
+        return fileName;
+	}
+	
+	/**
+	 * 获取对应的文件,或者默认文件
+	 * 不同路径,防止镜像部署时获取不到
+	 * 
+	 * @param templateName 带后缀的文件名
+	 * @return
+	 * @throws FileNotFoundException 
+	 */
+	public static File getTemplateOrDefaultFile(String templateName) throws FileNotFoundException {
+		File file = null;
+		try {
+			file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "template/" + templateName);
+		} catch (FileNotFoundException e) {
+			file = ResourceUtils.getFile(AdmCommonConstant.SERVER_ROOT_PATH + "template/" + templateName);
+		}
+		if (file == null || !file.exists()) {
+			try {
+				file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "template/relation-template.xlsx");
+			} catch (FileNotFoundException e) {
+				file = ResourceUtils.getFile(AdmCommonConstant.SERVER_ROOT_PATH + "/template/relation-template.xlsx");
+			}
+		}
+		return file;
+	}
+	
+	/**
+	 * 获取对应的模板文件,不同路径,防止镜像内获取不到
+	 * 
+	 * @param templateName 带后缀的文件名
+	 * @return
+	 * @throws FileNotFoundException 
+	 */
+	public static File getTemplateFile(String templateName) throws FileNotFoundException {
+		File file = null;
+		try {
+			file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "template/" + templateName);
+		} catch (FileNotFoundException e) {}
+		if (file == null || !file.exists()) {
+			file = ResourceUtils.getFile(AdmCommonConstant.SERVER_ROOT_PATH + "/template/" + templateName);
+		}
+		return file;
+	}
+	
+	/**
+	 * 创建本地文件,这里不再在classpath下创建
+	 * 
+	 * @param uploadFile 文件名称,带后缀
+	 * @throws IOException 
+	 */
+	public static File createTemplateFile(String uploadFile) throws IOException {
+		String path = AdmCommonConstant.SERVER_ROOT_PATH + "/template";
+		File file = new File(path);
+		if (!file.exists()) {
+			file.mkdirs();
+		}
+		file = new File(path + "/" + uploadFile);
+		if (!file.exists()) {
+			file.createNewFile();
+		}
+		return file;
+	}
+	
+	/**
+	 * 获取导入结果
+	 * 
+	 * @return
+	 */
+	public static JSONObject getImportResult(String graphId, int successNum, int failureNum) {
+		JSONObject result = new JSONObject();
+		result.put("relationType", graphId);
+		result.put("successCount", successNum);
+		result.put("failCount", failureNum);
+		String state = null;
+        if (successNum > 0 && failureNum > 0) {
+        	state = "1";
+        } else if (successNum > 0 && failureNum == 0) {
+            state = "0";
+        } else {
+            state = "2";
+        }
+        result.put("state", state);
+		return result;
+	}
+	
+	/**
+	 * 获取导入结果
+	 * 
+	 * @param message
+	 * @param successCount
+	 * @param failCount
+	 * @return
+	 */
+	public static AdmResponse getImportFailResult(String message, String state, int successCount, int failCount) {
+		JSONObject content = new JSONObject();
+		content.put("state", state);
+		content.put("successCount", successCount);
+		content.put("failCount", failCount);
+		
+		AdmResponse failure = AdmResponse.failure(message);
+		failure.setContent(Lists.newArrayList(content));
+		return failure;
+	}
+	
+	/**
+	 * 获取导入结果
+	 * 
+	 * @param successCount
+	 * @param failCount
+	 * @return
+	 */
+	public static AdmResponse getImportSuccessResult(String message, int successCount, int failCount) {
+		JSONObject content = new JSONObject();
+		String state = null;
+        if (successCount > 0 && failCount > 0) {
+        	state = "1";
+        } else if (successCount > 0 && failCount == 0) {
+            state = "0";
+        } else {
+            state = "2";
+        }
+		content.put("state", state);
+		content.put("successCount", successCount);
+		content.put("failCount", failCount);
+		AdmResponse success = AdmResponse.success(Lists.newArrayList(content));
+		success.setMessage(message);
+		return success;
+	}
+	
+}

+ 26 - 117
src/main/java/com/persagy/proxy/object/controller/AdmEquipmentController.java

@@ -1,11 +1,8 @@
 package com.persagy.proxy.object.controller;
 
 import java.awt.Color;
-import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
-import java.time.LocalDateTime;
-import java.time.format.DateTimeFormatter;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.HashMap;
@@ -36,7 +33,6 @@ import org.springframework.web.multipart.MultipartFile;
 import com.alibaba.fastjson.JSONObject;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.node.ObjectNode;
-import com.google.common.collect.Lists;
 import com.google.common.collect.Sets;
 import com.persagy.dmp.basic.model.QueryCriteria;
 import com.persagy.dmp.basic.utils.JsonNodeUtils;
@@ -47,16 +43,17 @@ import com.persagy.dmp.define.entity.ObjectInfoDefine;
 import com.persagy.dmp.define.entity.ObjectTypeDefine;
 import com.persagy.dmp.digital.client.DigitalObjectFacade;
 import com.persagy.proxy.adm.constant.AdmCommonConstant;
-import com.persagy.proxy.object.model.AdmEquipment;
 import com.persagy.proxy.adm.request.AdmCreateRequest;
 import com.persagy.proxy.adm.request.AdmQueryCriteria;
 import com.persagy.proxy.adm.request.AdmResponse;
-import com.persagy.proxy.object.service.IAdmEquipmentService;
 import com.persagy.proxy.adm.service.IAdmRelationService;
-import com.persagy.proxy.report.service.IRelationReportService;
 import com.persagy.proxy.adm.utils.AdmContextUtil;
+import com.persagy.proxy.adm.utils.AdmExcelUtil;
 import com.persagy.proxy.adm.utils.ObjectNameUtil;
 import com.persagy.proxy.common.entity.RelationDTO;
+import com.persagy.proxy.object.model.AdmEquipment;
+import com.persagy.proxy.object.service.IAdmEquipmentService;
+import com.persagy.proxy.report.service.IRelationReportService;
 
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.collection.CollectionUtil;
@@ -273,42 +270,43 @@ public class AdmEquipmentController {
 		String failure = null;
 		String primaryKey = null;
 		String classCodeCloumn = null;
+		XSSFWorkbook workbook = null;
 		FileOutputStream fileOutputStream = null;
 		String groupCode = AppContext.getContext().getGroupCode();
 		String projectId = AppContext.getContext().getProjectId();
 		try {
 			
-			XSSFWorkbook workbook = new XSSFWorkbook(file.getInputStream());
+			workbook = new XSSFWorkbook(file.getInputStream());
 		    XSSFSheet sheet = workbook.getSheetAt(0);	// 第一个sheet
 		    XSSFRow row1 = sheet.getRow(1);
 		    if (row1 == null) {
-		    	this.writeFile2Local(workbook, projectId, file.getOriginalFilename());
-                return this.getImportFailResult("数据不存在", "2", 0, ++failurNum);
+		    	AdmExcelUtil.writeFile2Local(workbook, projectId, file.getOriginalFilename());
+                return AdmExcelUtil.getImportFailResult("数据不存在", "2", 0, ++failurNum);
 			}
 		    
 	    	/** 获取识别码 */
             XSSFCell markCell = row1.getCell(0);
             if (markCell == null) {
-            	this.writeFile2Local(workbook, projectId, file.getOriginalFilename());
-                return this.getImportFailResult("未填写识别码", "2", 0, ++failurNum);
+            	AdmExcelUtil.writeFile2Local(workbook, projectId, file.getOriginalFilename());
+                return AdmExcelUtil.getImportFailResult("未填写识别码", "2", 0, ++failurNum);
 			}
         	primaryKey = markCell.getStringCellValue().trim(); 	// 识别码
 			boolean containsKey = AdmCommonConstant.codeColMap.containsKey(primaryKey);
 			if (!containsKey) {
-				this.writeFile2Local(workbook, projectId, file.getOriginalFilename());
-                return this.getImportFailResult("未找到识别码", "2", 0, ++failurNum);
+				AdmExcelUtil.writeFile2Local(workbook, projectId, file.getOriginalFilename());
+                return AdmExcelUtil.getImportFailResult("未找到识别码", "2", 0, ++failurNum);
 			}
 			
             /** 获取类型 */
             XSSFCell cell = row1.getCell(1);
             if (cell == null) {
-            	this.writeFile2Local(workbook, projectId, file.getOriginalFilename());
-                return this.getImportFailResult("未填写设备类型", "2", 0, ++failurNum);
+            	AdmExcelUtil.writeFile2Local(workbook, projectId, file.getOriginalFilename());
+                return AdmExcelUtil.getImportFailResult("未填写设备类型", "2", 0, ++failurNum);
 			}
             classCodeCloumn = cell.getStringCellValue().trim();
         	if (!"ClassCode".equals(classCodeCloumn)) {
-        		this.writeFile2Local(workbook, projectId, file.getOriginalFilename());
-                return this.getImportFailResult("缺少设备类型(ClassCode)", "2", 0, ++failurNum);
+        		AdmExcelUtil.writeFile2Local(workbook, projectId, file.getOriginalFilename());
+                return AdmExcelUtil.getImportFailResult("缺少设备类型(ClassCode)", "2", 0, ++failurNum);
 			}
 		    
         	// 存取所有的信息点
@@ -415,7 +413,7 @@ public class AdmEquipmentController {
                             }
 						} else if ("DATETIME".equalsIgnoreCase(define.getDataType())) {
                             try {
-                            	String trim = cell.getStringCellValue().trim();
+                            	String trim = cell.getRawValue().trim();
                             	tempMap.put(define.getCode(), DateUtil.parseByPatterns(trim, "yyyyMMdd"));
                             } catch (Exception e) {
                             	failurNum++;
@@ -424,7 +422,7 @@ public class AdmEquipmentController {
                             }
 						} else if ("DOUBLE".equalsIgnoreCase(define.getDataType())) {
 							try {
-								String trim = cell.getStringCellValue().trim();
+								String trim = cell.getRawValue().trim();
 								tempMap.put(define.getCode(), Double.parseDouble(trim));
 							} catch (Exception e) {
                             	failurNum++;
@@ -467,10 +465,10 @@ public class AdmEquipmentController {
 		    this.wirteReport(workbook, sheet);
 		    
 		    // 返回正确结果
-		    String fileName = this.writeFile2Local(workbook, projectId, file.getOriginalFilename());
-		    return this.getImportSuccessResult(fileName, successCount, failurNum);
+		    String fileName = AdmExcelUtil.writeFile2Local(workbook, projectId, file.getOriginalFilename());
+		    return AdmExcelUtil.getImportSuccessResult(fileName, successCount, failurNum);
 		} catch (Exception e) {
-			log.error("文件导入失败", e);
+			log.error("台账信息导入失败", e);
 			failure = e.getMessage();
 		} finally {
 			if (fileOutputStream != null) {
@@ -479,6 +477,11 @@ public class AdmEquipmentController {
 					fileOutputStream.close();
 				} catch (IOException e) {}
 			}
+			if (workbook != null) {
+				try {
+					workbook.close();
+				} catch (IOException e) {}
+			}
 		}
 		return AdmResponse.failure(failure);
 	}
@@ -519,56 +522,6 @@ public class AdmEquipmentController {
 		return successCount;
 	}
 	
-	
-	/**
-	 * 文件写入至本地
-	 * 
-	 * @param workbook
-	 * @param projectId
-	 * @param orgFileName
-	 * @return 
-	 */
-	private String writeFile2Local(XSSFWorkbook workbook, String projectId, String orgFileName) {
-		FileOutputStream fileOutputStream = null;
-		String date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
-		// 重组文件名
-        String fileName = projectId + date + "." + orgFileName.substring(orgFileName.lastIndexOf(".") + 1);
-        try {
-    		// 调用中台存储完成之后,此文件存储至本地
-    		File templateFile = this.createTemplateFile(fileName);
-    		fileOutputStream = new FileOutputStream(templateFile);
-    		workbook.write(fileOutputStream);
-		} catch (Exception e) {
-			log.error("文件存储失败", e);
-		} finally {
-			if (fileOutputStream != null) {
-				try {
-					fileOutputStream.close();
-				} catch (IOException e) {}
-			}
-		}
-        return fileName;
-	}
-	
-	/**
-	 * 创建本地文件,这里不再在classpath下创建
-	 * 
-	 * @param fileName 文件名称,带后缀
-	 * @throws IOException 
-	 */
-	private File createTemplateFile(String fileName) throws IOException {
-		String path = AdmCommonConstant.SERVER_ROOT_PATH + "/template/upload/";
-		File file = new File(path);
-		if (!file.exists()) {
-			file.mkdirs();
-		}
-		file = new File(path + fileName);
-		if (!file.exists()) {
-			file.createNewFile();
-		}
-		return file;
-	}
-	
 	/**
 	 * 获取类型定义
 	 * 
@@ -722,48 +675,4 @@ public class AdmEquipmentController {
 		this.setColor(workbook,56,94,15,createCell);
 	}
 	
-	/**
-	 * 获取导入结果
-	 * 
-	 * @param message
-	 * @param successCount
-	 * @param failCount
-	 * @return
-	 */
-	private AdmResponse getImportFailResult(String message, String state, int successCount, int failCount) {
-		JSONObject content = new JSONObject();
-		content.put("state", state);
-		content.put("successCount", successCount);
-		content.put("failCount", failCount);
-		
-		AdmResponse failure = AdmResponse.failure(message);
-		failure.setContent(Lists.newArrayList(content));
-		return failure;
-	}
-	
-	/**
-	 * 获取导入结果
-	 * 
-	 * @param successCount
-	 * @param failCount
-	 * @return
-	 */
-	private AdmResponse getImportSuccessResult(String message, int successCount, int failCount) {
-		JSONObject content = new JSONObject();
-		String state = null;
-        if (successCount > 0 && failCount > 0) {
-        	state = "1";
-        } else if (successCount > 0 && failCount == 0) {
-            state = "0";
-        } else {
-            state = "2";
-        }
-		content.put("state", state);
-		content.put("successCount", successCount);
-		content.put("failCount", failCount);
-		AdmResponse success = AdmResponse.success(Lists.newArrayList(content));
-		success.setMessage(message);
-		return success;
-	}
-	
 }

+ 247 - 80
src/main/java/com/persagy/proxy/report/controller/RelationReportController.java

@@ -33,20 +33,29 @@ import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.node.ObjectNode;
 import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
 import com.persagy.dmp.basic.model.QueryCriteria;
+import com.persagy.dmp.common.context.AppContext;
 import com.persagy.dmp.common.helper.SpringHelper;
+import com.persagy.dmp.define.client.DigitalDefineFacade;
+import com.persagy.dmp.define.entity.ObjectInfoDefine;
+import com.persagy.dmp.define.entity.ObjectTypeDefine;
+import com.persagy.dmp.digital.client.DigitalObjectFacade;
+import com.persagy.dmp.digital.entity.ObjectDigital;
 import com.persagy.proxy.adm.constant.AdmCommonConstant;
+import com.persagy.proxy.adm.constant.AdmObjectType;
 import com.persagy.proxy.adm.constant.AdmRelationTypeEnum;
 import com.persagy.proxy.adm.handler.RelationReportHandler;
+import com.persagy.proxy.adm.request.AdmQueryCriteria;
+import com.persagy.proxy.adm.request.AdmResponse;
+import com.persagy.proxy.adm.strategy.RelationObjectContext;
+import com.persagy.proxy.adm.utils.AdmExcelUtil;
+import com.persagy.proxy.adm.utils.AdmQueryCriteriaHelper;
 import com.persagy.proxy.report.model.AdmRelationObject;
 import com.persagy.proxy.report.model.EquipmentBindPointExcel;
 import com.persagy.proxy.report.model.EquipmentCountExcel;
 import com.persagy.proxy.report.model.EquipmentExcel;
-import com.persagy.proxy.adm.request.AdmQueryCriteria;
-import com.persagy.proxy.adm.request.AdmResponse;
 import com.persagy.proxy.report.service.IRelationReportService;
-import com.persagy.proxy.adm.strategy.RelationObjectContext;
-import com.persagy.proxy.adm.utils.AdmQueryCriteriaHelper;
 
 import cn.hutool.core.collection.CollectionUtil;
 import cn.hutool.core.util.StrUtil;
@@ -305,7 +314,7 @@ public class RelationReportController {
 			String fileName = URLEncoder.encode("台账信息导入模板", encode);
 			response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
 			
-			File templateFile = this.getTemplateFile("equip_template.xlsx");
+			File templateFile = AdmExcelUtil.getTemplateFile("equip_template.xlsx");
 			excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(templateFile).build();
 		} catch (Exception e) {
 			log.error("设备静态信息维护模板下载失败", e);
@@ -370,7 +379,7 @@ public class RelationReportController {
 			response.setCharacterEncoding(encode);
 			response.setHeader("Content-disposition", "attachment;filename=relation-template.xlsx");
 			
-			File templateFile = this.getTemplateFile("relation-template.xlsx");
+			File templateFile = AdmExcelUtil.getTemplateFile("relation-template.xlsx");
 			excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(templateFile).build();
 		} catch (Exception e) {
 			log.error("模板下载失败", e);
@@ -399,7 +408,7 @@ public class RelationReportController {
 			String templateFile = projectId + AdmCommonConstant.LINE_THROUGH + relType + AdmCommonConstant.EXCEL_SUFFIX_XLSX;
 			response.setHeader("Content-disposition", "attachment;filename=" + templateFile);
 			
-			File file = this.getTemplateOrDefaultFile(templateFile);
+			File file = AdmExcelUtil.getTemplateOrDefaultFile(templateFile);
 			excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(file).build();
 		} catch (Exception e) {
 			log.error("下载报告失败", e);
@@ -464,10 +473,10 @@ public class RelationReportController {
 				relationObjects = this.relationObjectContext.exportRelationObject(groupCode, projectId, graphAndRelKey);
 			}
 			
-			File templateFile = this.getTemplateFile("relation-template.xlsx");
+			File templateFile = AdmExcelUtil.getTemplateFile("relation-template.xlsx");
 			EasyExcel.write(response.getOutputStream()).withTemplate(templateFile).sheet(EXPORT_SHEET_NAME).needHead(false).doWrite(relationObjects);
 		} catch (Exception e) {
-			log.error("导出关系失败", e);
+			log.error("深化点导入失败", e);
 		} finally {
 			if (excelWriter != null) {
 				excelWriter.finish();
@@ -487,11 +496,11 @@ public class RelationReportController {
 	 * @param request
 	 * @param response
 	 */
-	@SuppressWarnings("resource")
 	@RequestMapping("/graphic/import")
 	public AdmResponse importFile(@RequestParam("file") MultipartFile file, @RequestParam String relType, @RequestParam(required = false) String zoneType, 
 			HttpServletRequest request, HttpServletResponse response) {
 		String failure = null;
+		XSSFWorkbook workbook = null;
 		FileOutputStream fileOutputStream = null;
 		try {
 			if (file == null) {
@@ -514,7 +523,7 @@ public class RelationReportController {
 			String groupCode = request.getHeader(AdmCommonConstant.GROUP_CODE_HEADER);
 			String projectId = request.getHeader(AdmCommonConstant.PROJECT_ID_HEADER);
 			
-			XSSFWorkbook workbook = new XSSFWorkbook(file.getInputStream());
+			workbook = new XSSFWorkbook(file.getInputStream());
 		    XSSFSheet sheet = workbook.getSheet("关系维护");
 		    // 获取识别码的属性
 		    String code = sheet.getRow(2).getCell(1).toString();
@@ -579,14 +588,14 @@ public class RelationReportController {
         	try {
         		// 调用中台存储完成之后,此文件存储至本地
         		String uploadFile = projectId + AdmCommonConstant.LINE_THROUGH + relType + AdmCommonConstant.EXCEL_SUFFIX_XLSX;
-        		File templateFile = this.createTemplateFile(uploadFile);
+        		File templateFile = AdmExcelUtil.createTemplateFile(uploadFile);
         		fileOutputStream = new FileOutputStream(templateFile);
         		workbook.write(fileOutputStream);
 			} catch (Exception e) {
 				log.error("文件存储失败", e);
 			}
         	
-        	return AdmResponse.success(Lists.newArrayList(this.getImportResult(graphId, successNum, failurNum)));
+        	return AdmResponse.success(Lists.newArrayList(AdmExcelUtil.getImportResult(graphId, successNum, failurNum)));
 		} catch (Exception e) {
 			log.error("文件导入失败", e);
 			failure = e.getMessage();
@@ -597,98 +606,256 @@ public class RelationReportController {
 					fileOutputStream.close();
 				} catch (IOException e) {}
 			}
+			if (workbook != null) {
+				try {
+					workbook.close();
+				} catch (IOException e) {}
+			}
 		}
 		return AdmResponse.failure(failure);
 	}
 	
-	
-	
-	
-	/************************************************** 本地文件获取、创建 ***********************************************/
-	
-	
 	/**
-	 * 获取对应的文件,或者默认文件
-	 * 不同路径,防止镜像部署时获取不到
+	 * 深化点信息导入
 	 * 
-	 * @param templateName 带后缀的文件名
+	 * @param file
+	 * @param request
+	 * @param response
 	 * @return
-	 * @throws FileNotFoundException 
 	 */
-	private File getTemplateOrDefaultFile(String templateName) throws FileNotFoundException {
-		File file = null;
+	@RequestMapping("/graphic/import/point")
+	public AdmResponse importFile(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response) {
+		int failCount = 0;
+		int successCount = 0;
+		String failure = null;
+		XSSFWorkbook workbook = null;
+		String groupCode = AppContext.getContext().getGroupCode();
+		String projectId = AppContext.getContext().getProjectId();
+		ObjectMapper objectMapper = SpringHelper.getBean(ObjectMapper.class);
 		try {
-			file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "template/" + templateName);
-		} catch (FileNotFoundException e) {
-			file = ResourceUtils.getFile(AdmCommonConstant.SERVER_ROOT_PATH + "template/" + templateName);
-		}
-		if (file == null || !file.exists()) {
-			try {
-				file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "template/relation-template.xlsx");
-			} catch (FileNotFoundException e) {
-				file = ResourceUtils.getFile(AdmCommonConstant.SERVER_ROOT_PATH + "/template/relation-template.xlsx");
+			if (file == null) {
+				return AdmResponse.failure("未选择文件");
+			}
+			
+			workbook = new XSSFWorkbook(file.getInputStream());
+		    XSSFSheet sheet = workbook.getSheetAt(0);
+		    XSSFRow row = sheet.getRow(0);
+		    if (row == null) {
+		    	return AdmResponse.failure("文件标题行不存在");
+			}
+		    
+		    int identificationCode = -1, infosCode = -1, tableCode = -1, functionCode = -1, infosFeedback = -1, 
+		    		classCodeIndexes = -1, bimIdIndexes = -1;
+		    
+		    short lastCellNum = row.getLastCellNum();
+		    for (int i = 0; i <= lastCellNum; i++) {
+				XSSFCell cell = row.getCell(i);
+				if (cell != null && cell.getStringCellValue() != null && "本地编码".equals(cell.getStringCellValue().trim())) {
+                    identificationCode = i;
+                } else if (cell != null && cell.getStringCellValue() != null && "信息点编码".equals(cell.getStringCellValue().trim())) {
+                    infosCode = i;
+                } else if (cell != null && cell.getStringCellValue() != null && "表号".equals(cell.getStringCellValue().trim())) {
+                    tableCode = i;
+                } else if (cell != null && cell.getStringCellValue() != null && "功能号".equals(cell.getStringCellValue().trim())) {
+                    functionCode = i;
+                } else if (cell != null && cell.getStringCellValue() != null && "反馈信息".equals(cell.getStringCellValue().trim())) {
+                    infosFeedback = i;
+                } else if (cell != null && cell.getStringCellValue() != null && "设备类型".equals(cell.getStringCellValue().trim())) {
+                    classCodeIndexes = i;
+                } else if (cell != null && cell.getStringCellValue() != null && "BIMID".equals(cell.getStringCellValue().trim())) {
+                    bimIdIndexes = i;
+                }
+			}
+		    
+			if (identificationCode == -1) {
+				failure = "缺少识别码:本地编码";
+			} else if (infosCode == -1) {
+				failure = "缺少信息点编码";
+			} else if (tableCode == -1) {
+				failure = "缺少表号";
+			} else if (functionCode == -1) {
+				failure = "缺少功能号";
+			} else if (infosFeedback == -1) {
+				failure = "缺少反馈信息";
+			} else if (classCodeIndexes == -1) {
+				failure = "缺少设备类型";
+			} else if (bimIdIndexes == -1) {
+				failure = "缺少BIMID";
+			}
+			if (StrUtil.isNotBlank(failure)) {
+				return AdmExcelUtil.getImportFailResult(failure, "3", 0, 0);
+			}
+			
+			// 业务数据获取
+		    int lastRowNum = sheet.getLastRowNum();
+		    for (int i = 1; i <= lastRowNum; i++) {
+		    	XSSFRow xssfRow = sheet.getRow(i);
+		    	if (xssfRow == null) {
+		    		sheet.getRow(0).createCell(16).setCellValue("第" + i + " 行数据为空");
+					continue;
+				}
+		    	XSSFCell identificationCodeCell = xssfRow.getCell(identificationCode);
+		    	if (identificationCodeCell == null || StrUtil.isBlank(identificationCodeCell.getStringCellValue())) {
+		    		failCount++;
+		    		xssfRow.createCell(infosFeedback).setCellValue("本地编码不可以为空");
+                    continue;
+				}
+		    	XSSFCell infosCodeCell = xssfRow.getCell(infosCode);
+		    	if (infosCodeCell == null || StrUtil.isBlank(infosCodeCell.getStringCellValue())) {
+		    		failCount++;
+		    		xssfRow.createCell(infosFeedback).setCellValue("信息点编码不可以为空");
+                    continue;
+				}
+		    	XSSFCell tableCodeCell = xssfRow.getCell(tableCode);
+		    	if (tableCodeCell == null || StrUtil.isBlank(tableCodeCell.getRawValue())) {
+		    		failCount++;
+		    		xssfRow.createCell(infosFeedback).setCellValue("表号不可以为空");
+                    continue;
+				}
+		    	XSSFCell functionCodeCell = xssfRow.getCell(functionCode);
+		    	if (functionCodeCell == null || StrUtil.isBlank(functionCodeCell.getRawValue())) {
+		    		failCount++;
+		    		xssfRow.createCell(infosFeedback).setCellValue("功能号不可以为空");
+                    continue;
+				}
+		    	XSSFCell classCodeIndexesCell = xssfRow.getCell(classCodeIndexes);
+		    	if (classCodeIndexesCell == null || StrUtil.isBlank(classCodeIndexesCell.getStringCellValue())) {
+		    		failCount++;
+		    		xssfRow.createCell(infosFeedback).setCellValue("设备类型不可以为空");
+                    continue;
+				}
+		    	String classCode = classCodeIndexesCell.getStringCellValue().trim();
+		    	// 验证此classCode是否存在
+		    	ObjectTypeDefine typeDefine = this.queryClassList(groupCode, projectId, classCode, AdmObjectType.SYSTEM.getIndex());
+		    	if (typeDefine == null) {
+		    		failCount++;
+		    		xssfRow.createCell(infosFeedback).setCellValue("设备类型未在数据字典定义范围");
+                    continue;
+				}
+		    	
+		    	// 获取实际cell值
+		    	String val0 = identificationCodeCell.getStringCellValue().trim();
+		    	String val1 = infosCodeCell.getStringCellValue().trim();
+		    	String val2 = tableCodeCell.getRawValue().trim();
+		    	String val3 = functionCodeCell.getRawValue().trim();
+		    	
+		    	// 获取设备信息
+		    	ObjectNode criteria = objectMapper.createObjectNode();
+		    	criteria.put("localId", val0);
+		    	criteria.put("classCode", classCode);
+		    	List<ObjectNode> equipments = this.relationReportService.getObjectNode(criteria, groupCode, projectId, null);
+		    	if (CollectionUtil.isEmpty(equipments)) {
+		    		failCount++;
+		    		xssfRow.createCell(infosFeedback).setCellValue("设备类型下本地编码不存在");
+                    continue;
+				} else if (equipments.size() > 1) {
+					String temp = null;
+					for (ObjectNode objectNode : equipments) {
+						String bimId = this.getObjectBimId(objectNode);
+						if (StrUtil.isNotBlank(bimId)) {
+							temp = (StrUtil.isBlank(temp) ? "" : (temp + ",")) + bimId;
+						}
+					}
+					failCount++;
+		    		xssfRow.createCell(infosFeedback).setCellValue("本地编码重复");
+		    		if (StrUtil.isNotBlank(temp)) {
+		    			xssfRow.createCell(bimIdIndexes).setCellValue(temp);
+					}
+                    continue;
+				}
+		    	
+		    	// 获取信息点定义
+		    	List<ObjectInfoDefine> funidList = this.relationReportService.queryFunidList(groupCode, projectId, Sets.newHashSet(classCode), val1, null);
+		    	if (CollectionUtil.isEmpty(funidList)) {
+		    		failCount++;
+		    		xssfRow.createCell(infosFeedback).setCellValue("信息点不存在");
+                    continue;
+				}
+		    	ObjectInfoDefine infoDefine = funidList.get(0);
+		    	
+		    	 // 数据更新,更新对应code值为,表号和功能号
+		    	ObjectNode objectNode = objectMapper.createObjectNode();
+		    	objectNode.put("id", equipments.get(0).get("id").asText());
+		    	objectNode.put(infoDefine.getCode(), val2 + "-" +val3);
+		    	
+		    	ObjectDigital updateOne = DigitalObjectFacade.updateOne(groupCode, projectId, AdmCommonConstant.APP_ID, AdmCommonConstant.USER_ID, objectNode);
+		    	if (updateOne == null) {
+		    		failCount++;
+		    		xssfRow.createCell(infosFeedback).setCellValue("失败");
+		    		String bimId = this.getObjectBimId(equipments.get(0));
+		    		if (StrUtil.isNotBlank(bimId)) {
+		    			xssfRow.createCell(bimIdIndexes).setCellValue(bimId);
+					}
+				} else {
+					xssfRow.createCell(infosFeedback).setCellValue("成功");
+					successCount++;
+				}
+			}
+
+		    // 文件存储及返回
+		    String fileName = AdmExcelUtil.writeFile2Local(workbook, projectId, file.getOriginalFilename());
+		    return AdmExcelUtil.getImportSuccessResult(fileName, successCount, failCount);
+		} catch (Exception e) {
+			log.error("文件导入失败", e);
+			failure = e.getMessage();
+		} finally {
+			if (workbook != null) {
+				try {
+					workbook.close();
+				} catch (IOException e) {}
 			}
 		}
-		return file;
+		return AdmResponse.failure(failure);
 	}
 	
-	/**
-	 * 获取对应的模板文件,不同路径,防止镜像内获取不到
-	 * 
-	 * @param templateName 带后缀的文件名
-	 * @return
-	 * @throws FileNotFoundException 
-	 */
-	private File getTemplateFile(String templateName) throws FileNotFoundException {
-		File file = null;
-		try {
-			file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "template/" + templateName);
-		} catch (FileNotFoundException e) {}
-		if (file == null || !file.exists()) {
-			file = ResourceUtils.getFile(AdmCommonConstant.SERVER_ROOT_PATH + "/template/" + templateName);
-		}
-		return file;
-	}
+	
+	/***************************************************************************************************************/
 	
 	/**
-	 * 创建本地文件,这里不再在classpath下创建
+	 * 获取类定义信息
 	 * 
-	 * @param uploadFile 文件名称,带后缀
-	 * @throws IOException 
+	 * @param groupCode
+	 * @param projectId
+	 * @param classCode
+	 * @param objType 不等于条件
+	 * @return
 	 */
-	private File createTemplateFile(String uploadFile) throws IOException {
-		String path = AdmCommonConstant.SERVER_ROOT_PATH + "/template";
-		File file = new File(path);
-		if (!file.exists()) {
-			file.mkdirs();
+	private ObjectTypeDefine queryClassList(String groupCode, String projectId, String classCode, String objType) {
+		groupCode = StrUtil.isBlank(groupCode) ? defaultCode : groupCode;
+		
+		ObjectMapper objectMapper = SpringHelper.getBean(ObjectMapper.class);
+		QueryCriteria classCriteria = new QueryCriteria();
+		ObjectNode condition = objectMapper.createObjectNode();
+		if (StrUtil.isNotBlank(classCode)) {
+			condition.put("code", classCode);
 		}
-		file = new File(path + "/" + uploadFile);
-		if (!file.exists()) {
-			file.createNewFile();
+		if (StrUtil.isNotBlank(objType)) {
+			JSONObject arrays = new JSONObject();
+			arrays.put("$ne", objType);
+			condition.putPOJO("objType", arrays);
 		}
-		return file;
+		classCriteria.setCriteria(condition);
+		List<ObjectTypeDefine> objectNodes = DigitalDefineFacade.queryObjectType(groupCode, projectId, AdmCommonConstant.APP_ID, AdmCommonConstant.USER_ID, classCriteria);
+		return CollectionUtil.isEmpty(objectNodes) ? null : objectNodes.get(0);
 	}
 	
 	/**
-	 * 获取导入结果
+	 * 获取BimId
 	 * 
+	 * @param objectNode
 	 * @return
 	 */
-	private JSONObject getImportResult(String graphId, int successNum, int failureNum) {
-		JSONObject result = new JSONObject();
-		result.put("relationType", graphId);
-		result.put("successCount", successNum);
-		result.put("failCount", failureNum);
-		String state = null;
-        if (successNum > 0 && failureNum > 0) {
-        	state = "1";
-        } else if (successNum > 0 && failureNum == 0) {
-            state = "0";
-        } else {
-            state = "2";
-        }
-        result.put("state", state);
-		return result;
+	private String getObjectBimId(ObjectNode objectNode) {
+		String infos = objectNode.has("infos") ? objectNode.get("infos").asText() : null;
+		if (StrUtil.isNotBlank(infos)) {
+			try {
+				JSONObject jsonObject = JSONObject.parseObject(infos);
+				return jsonObject.getString("bimId");
+			} catch (Exception e) {
+				e.printStackTrace();
+			}
+		}
+		return null;
 	}
 	
 }

+ 1 - 1
src/main/java/com/persagy/proxy/report/service/IRelationReportService.java

@@ -138,7 +138,7 @@ public interface IRelationReportService {
 	 * @param ids
 	 * @return 不会返回null
 	 */
-	List<ObjectNode> getObjectNode(String groupCode, String projectId, Set<String> ids);
+	List<ObjectNode> getObjectNode(ObjectNode criteria, String groupCode, String projectId, Set<String> ids);
 	
 	/**
 	 * 获取指定对象信息

+ 26 - 14
src/main/java/com/persagy/proxy/report/service/impl/RelationReportService.java

@@ -76,7 +76,7 @@ public class RelationReportService implements IRelationReportService {
 		if (CollectionUtil.isNotEmpty(objTypes)) {
 			JSONObject arrays = new JSONObject();
 			arrays.put("$in", objTypes);
-			condition.putPOJO("objType", objTypes);
+			condition.putPOJO("objType", arrays);
 		}
 		classCriteria.setCriteria(condition);
 		if (StrUtil.isNotBlank(ascOrder)) {
@@ -274,16 +274,18 @@ public class RelationReportService implements IRelationReportService {
 	}
 	
 	@Override
-	public List<ObjectNode> getObjectNode(String groupCode, String projectId, Set<String> ids) {
+	public List<ObjectNode> getObjectNode(ObjectNode criteria, String groupCode, String projectId, Set<String> ids) {
 		groupCode = StrUtil.isBlank(groupCode) ? defaultCode : groupCode;
 		
-		ObjectMapper objectMapper = SpringHelper.getBean(ObjectMapper.class);
 		QueryCriteria queryCriteria = new QueryCriteria();
-		ObjectNode criteria = objectMapper.createObjectNode();
+		if (criteria == null) {
+			ObjectMapper objectMapper = SpringHelper.getBean(ObjectMapper.class);
+			criteria = objectMapper.createObjectNode();
+		}
 		if (CollectionUtil.isNotEmpty(ids)) {
 			JSONObject arrays = new JSONObject();
 			arrays.put("$in", ids);
-			criteria.putPOJO("id", ids);
+			criteria.putPOJO("id", arrays);
 		}
 		queryCriteria.setCriteria(criteria);
 		List<ObjectNode> objectNodes = DigitalObjectFacade.query(groupCode, projectId, AdmCommonConstant.APP_ID, AdmCommonConstant.USER_ID, queryCriteria);
@@ -342,7 +344,7 @@ public class RelationReportService implements IRelationReportService {
 		// 获取关联对象的本地名称
 		ObjectRelation objectRelation = objectRelations.get(0);
 		String relationId = StrUtil.isBlank(objFrom) ? objectRelation.getObjFrom() : objectRelation.getObjTo();
-		List<ObjectNode> objectNodes = this.getObjectNode(groupCode, projectId, Sets.newHashSet(relationId));
+		List<ObjectNode> objectNodes = this.getObjectNode(null, groupCode, projectId, Sets.newHashSet(relationId));
 		return CollectionUtil.isEmpty(objectNodes) ? objectMapper.createObjectNode() : objectNodes.get(0);
 	}
 
@@ -359,17 +361,17 @@ public class RelationReportService implements IRelationReportService {
 		if (CollectionUtil.isNotEmpty(ids)) {
 			JSONObject arrays = new JSONObject();
 			arrays.put("$in", ids);
-			criteria.putPOJO("id", ids);
+			criteria.putPOJO("id", arrays);
 		}
 		if (CollectionUtil.isNotEmpty(classCodes)) {
 			JSONObject arrays = new JSONObject();
 			arrays.put("$in", classCodes);
-			criteria.putPOJO("classCode", classCodes);
+			criteria.putPOJO("classCode", arrays);
 		}
 		if (CollectionUtil.isNotEmpty(objTypes)) {
 			JSONObject arrays = new JSONObject();
 			arrays.put("$in", objTypes);
-			criteria.putPOJO("objType", objTypes);
+			criteria.putPOJO("objType", arrays);
 		}
 		objectCriteria.setPage(1L);
 		objectCriteria.setSize(500L);
@@ -400,12 +402,12 @@ public class RelationReportService implements IRelationReportService {
 		if (CollectionUtil.isNotEmpty(objFroms)) {
 			JSONObject arrays = new JSONObject();
 			arrays.put("$in", objFroms);
-			criteria.putPOJO("objFrom", objFroms);
+			criteria.putPOJO("objFrom", arrays);
 		}
 		if (CollectionUtil.isNotEmpty(objTos)) {
 			JSONObject arrays = new JSONObject();
 			arrays.put("$in", objTos);
-			criteria.putPOJO("objTo", objTos);
+			criteria.putPOJO("objTo", arrays);
 		}
 		
 		queryCriteria.setPage(1L);
@@ -837,13 +839,23 @@ public class RelationReportService implements IRelationReportService {
 	 * @param projectId
 	 */
 	private void queryPageFunids(List<ObjectInfoDefine> all, QueryCriteria queryCriteria, String groupCode, String projectId) {
-		List<ObjectInfoDefine> infos = DigitalObjectInfoFacade.query(groupCode, projectId, AdmCommonConstant.APP_ID, AdmCommonConstant.USER_ID, queryCriteria);
+		CommonResult<List<ObjectInfoDefine>> queryPrototype = DigitalObjectInfoFacade.queryPrototype(groupCode, projectId, AdmCommonConstant.APP_ID, AdmCommonConstant.USER_ID, queryCriteria);
+		Long count = queryPrototype.getCount();
+		List<ObjectInfoDefine> infos = queryPrototype.getData();
 		if (CollectionUtil.isEmpty(infos)) {
 			return;
 		}
+		
 		all.addAll(infos);
-		queryCriteria.setPage((queryCriteria.getPage() + 1));
-		this.queryPageFunids(all, queryCriteria, groupCode, projectId);
+		
+		if (count == null || count < 500) {
+			return;
+		} else if (infos == null || (infos.size()*queryCriteria.getPage()) > count) {
+			return;
+		} else {
+			queryCriteria.setPage((queryCriteria.getPage() + 1));
+			this.queryPageFunids(all, queryCriteria, groupCode, projectId);
+		}
 	}
 	
 }