RelationReportController.java 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. package com.persagy.proxy.adm.controller;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.net.URLEncoder;
  7. import java.nio.charset.StandardCharsets;
  8. import java.util.ArrayList;
  9. import java.util.HashSet;
  10. import java.util.List;
  11. import java.util.Set;
  12. import javax.servlet.http.HttpServletRequest;
  13. import javax.servlet.http.HttpServletResponse;
  14. import org.apache.poi.xssf.usermodel.XSSFCell;
  15. import org.apache.poi.xssf.usermodel.XSSFRow;
  16. import org.apache.poi.xssf.usermodel.XSSFSheet;
  17. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  18. import org.springframework.beans.factory.annotation.Value;
  19. import org.springframework.util.ResourceUtils;
  20. import org.springframework.web.bind.annotation.RequestBody;
  21. import org.springframework.web.bind.annotation.RequestMapping;
  22. import org.springframework.web.bind.annotation.RequestParam;
  23. import org.springframework.web.bind.annotation.RestController;
  24. import org.springframework.web.multipart.MultipartFile;
  25. import com.alibaba.excel.EasyExcel;
  26. import com.alibaba.excel.ExcelWriter;
  27. import com.alibaba.fastjson.JSONObject;
  28. import com.fasterxml.jackson.core.JsonProcessingException;
  29. import com.fasterxml.jackson.databind.node.ObjectNode;
  30. import com.google.common.collect.Lists;
  31. import com.persagy.proxy.adm.constant.AdmCommonConstant;
  32. import com.persagy.proxy.adm.constant.AdmRelationTypeEnum;
  33. import com.persagy.proxy.adm.handler.RelationReportHandler;
  34. import com.persagy.proxy.adm.model.AdmRelationObject;
  35. import com.persagy.proxy.adm.model.EquipmentBindPointExcel;
  36. import com.persagy.proxy.adm.model.EquipmentExcel;
  37. import com.persagy.proxy.adm.request.AdmQueryCriteria;
  38. import com.persagy.proxy.adm.request.AdmResponse;
  39. import com.persagy.proxy.adm.service.IRelationReportService;
  40. import com.persagy.proxy.adm.strategy.RelationObjectContext;
  41. import cn.hutool.core.collection.CollectionUtil;
  42. import cn.hutool.core.util.StrUtil;
  43. import lombok.RequiredArgsConstructor;
  44. import lombok.extern.slf4j.Slf4j;
  45. /**
  46. * 报表/模板下载
  47. *
  48. * @version 1.0.0
  49. * @company persagy
  50. * @author zhangqiankun
  51. * @date 2021年8月30日 下午7:41:56
  52. */
  53. @Slf4j
  54. @RestController
  55. @RequiredArgsConstructor
  56. public class RelationReportController {
  57. public static final String EXPORT_SHEET_NAME = "关系维护";
  58. @Value("${middleware.group.code}")
  59. private String defaultCode;
  60. private final RelationReportHandler relationReportHandler;
  61. private final RelationObjectContext relationObjectContext;
  62. private final IRelationReportService relationReportService;
  63. /**
  64. * 查询总数量
  65. *
  66. * @param queryCriteria
  67. * @param request
  68. * @return
  69. */
  70. @RequestMapping("/graphic/query-count")
  71. public AdmResponse queryCount(HttpServletRequest request) {
  72. String groupCode = request.getHeader(AdmCommonConstant.GROUP_CODE_HEADER);
  73. String projectId = request.getHeader(AdmCommonConstant.PROJECT_ID_HEADER);
  74. List<JSONObject> relationProjectCal = this.relationReportService.getAllRelationProjectCalTree(groupCode, projectId);
  75. return AdmResponse.success(relationProjectCal);
  76. }
  77. /**
  78. * 查询总览数量-根据类型查询关系数量
  79. *
  80. * @param type relationType
  81. */
  82. @RequestMapping("/graphic/type/query-count")
  83. public AdmResponse countRelationObjects(@RequestParam String type, HttpServletRequest request) {
  84. String groupCode = request.getHeader(AdmCommonConstant.GROUP_CODE_HEADER);
  85. String projectId = request.getHeader(AdmCommonConstant.PROJECT_ID_HEADER);
  86. JSONObject objectNode = this.relationReportService.getRelationProjectCal(groupCode, projectId, type);
  87. return AdmResponse.success(Lists.newArrayList(objectNode));
  88. }
  89. /**
  90. * 查询关系总览(空表,先插入)
  91. *
  92. * @param content:模糊查询卡片名称关键字
  93. * @return
  94. */
  95. @RequestMapping("/graphic/overview")
  96. public JSONObject overview(@RequestParam(required = false) String content) throws JsonProcessingException {
  97. JSONObject overview = this.relationReportService.overview(content);
  98. return overview;
  99. }
  100. /**
  101. * 项目关系类型查询
  102. *
  103. * @param queryCriteria
  104. * @param request
  105. * @return
  106. */
  107. @RequestMapping("/graphic/relation_type_project")
  108. public AdmResponse queryRelationObjects(@RequestBody AdmQueryCriteria queryCriteria, HttpServletRequest request) {
  109. String groupCode = request.getHeader(AdmCommonConstant.GROUP_CODE_HEADER);
  110. String projectId = request.getHeader(AdmCommonConstant.PROJECT_ID_HEADER);
  111. List<JSONObject> relationProjectCal = this.relationReportService.getAllRelationProjectCal(groupCode, projectId);
  112. return AdmResponse.success(relationProjectCal);
  113. }
  114. /**
  115. * 下载报告-查询key值
  116. *
  117. * @param queryCriteria
  118. * @param request
  119. * @return
  120. */
  121. @RequestMapping("/graphic/downloads/point/key")
  122. public AdmResponse downloadsPointKey(@RequestBody AdmQueryCriteria queryCriteria, HttpServletRequest request) {
  123. //String groupCode = request.getHeader(AdmCommonConstant.GROUP_CODE_HEADER);
  124. //String projectId = request.getHeader(AdmCommonConstant.PROJECT_ID_HEADER);
  125. //List<ObjectNode> relationProjectCal = this.relationReportService.getAllRelationProjectCal(groupCode, projectId);
  126. return AdmResponse.success(null);
  127. }
  128. /**
  129. * 下载报告-设备-未关联空间的设备
  130. * 设备.xlsx 是个空模板,所以这里未使用
  131. *
  132. * @param projectId
  133. * @param request
  134. * @param response
  135. * @return
  136. */
  137. @RequestMapping("/graphic/downloads/eq/not/sp")
  138. public void downloadsEqNotSp(@RequestParam(required = false) String projectId, HttpServletRequest request, HttpServletResponse response) {
  139. String groupCode = request.getHeader(AdmCommonConstant.GROUP_CODE_HEADER);
  140. if (StrUtil.isBlank(projectId)) {
  141. projectId = request.getHeader(AdmCommonConstant.PROJECT_ID_HEADER);
  142. }
  143. try {
  144. response.setContentType("application/vnd.ms-excel");
  145. String encode = StandardCharsets.UTF_8.name();
  146. response.setCharacterEncoding(encode);
  147. // 防止中文乱码
  148. String fileName = URLEncoder.encode("equipment", encode);
  149. response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
  150. List<EquipmentExcel> equipByNotSpace = this.relationReportHandler.findEquipByNotSpace(groupCode, projectId);
  151. EasyExcel.write(response.getOutputStream(), EquipmentExcel.class).sheet("设备").doWrite(equipByNotSpace);
  152. } catch(Exception e) {
  153. log.error("设备数据导出异常", e);
  154. }
  155. }
  156. /**
  157. * 下载报告-统计项目下已有的设备,信息点,每个设备
  158. * 设备.xlsx 是个空模板,所以这里未使用
  159. *
  160. * @param projectId
  161. * @param request
  162. * @param response
  163. * @return
  164. */
  165. @RequestMapping("/graphic/downloads/project/binding/point")
  166. public void downloadsProjectBindingPoint(@RequestParam(required = false) String projectId, HttpServletRequest request, HttpServletResponse response) {
  167. String groupCode = request.getHeader(AdmCommonConstant.GROUP_CODE_HEADER);
  168. if (StrUtil.isBlank(projectId)) {
  169. projectId = request.getHeader(AdmCommonConstant.PROJECT_ID_HEADER);
  170. }
  171. try {
  172. response.setContentType("application/vnd.ms-excel");
  173. String encode = StandardCharsets.UTF_8.name();
  174. response.setCharacterEncoding(encode);
  175. // 防止中文乱码
  176. String fileName = URLEncoder.encode("设备实例交付", encode);
  177. response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
  178. List<EquipmentBindPointExcel> equipmentBindPointExcel = this.relationReportHandler.queryProjectBindPoint(groupCode, projectId);
  179. EasyExcel.write(response.getOutputStream(), EquipmentBindPointExcel.class).sheet(0).doWrite(equipmentBindPointExcel);
  180. } catch(Exception e) {
  181. log.error("设备数据导出异常", e);
  182. }
  183. }
  184. /**
  185. * 下载报告-设备静态信息维护模板
  186. *
  187. * @param request
  188. * @param response
  189. */
  190. @RequestMapping("/graphic/downloads/equip/template")
  191. public void downloadEquipTemplate(HttpServletRequest request, HttpServletResponse response) {
  192. ExcelWriter excelWriter = null;
  193. try {
  194. response.setContentType("application/vnd.ms-excel");
  195. String encode = StandardCharsets.UTF_8.name();
  196. response.setCharacterEncoding(encode);
  197. // 防止中文乱码
  198. String fileName = URLEncoder.encode("台账信息导入模板", encode);
  199. response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
  200. File templateFile = this.getTemplateFile("equip_template.xlsx");
  201. excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(templateFile).build();
  202. } catch (Exception e) {
  203. log.error("设备静态信息维护模板下载失败", e);
  204. } finally {
  205. if (excelWriter != null) {
  206. excelWriter.finish();
  207. }
  208. }
  209. }
  210. /**
  211. * 下载报告-点表
  212. *
  213. * @param request
  214. * @param response
  215. */
  216. @RequestMapping("/graphic/downloads/point")
  217. public void downloadPoint(@RequestParam(required = false) String key, HttpServletRequest request, HttpServletResponse response) {
  218. ExcelWriter excelWriter = null;
  219. try {
  220. response.setContentType("application/vnd.ms-excel");
  221. String encode = StandardCharsets.UTF_8.name();
  222. response.setCharacterEncoding(encode);
  223. response.setHeader("Content-disposition", "attachment;filename=" + key);
  224. File file = null;
  225. try {
  226. file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "template/upload/" + key);
  227. } catch (FileNotFoundException e) {
  228. file = ResourceUtils.getFile(AdmCommonConstant.SERVER_ROOT_PATH + "template/upload/" + key);
  229. }
  230. if (file == null || !file.exists()) {
  231. try {
  232. file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "/template/relation-template.xlsx");
  233. } catch (FileNotFoundException e) {
  234. file = ResourceUtils.getFile(AdmCommonConstant.SERVER_ROOT_PATH + "/template/relation-template.xlsx");
  235. }
  236. }
  237. excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(file).build();
  238. } catch (Exception e) {
  239. log.error("下载报告-点表失败", e);
  240. } finally {
  241. if (excelWriter != null) {
  242. excelWriter.finish();
  243. }
  244. }
  245. }
  246. /**
  247. * 下载模板
  248. *
  249. * @param request
  250. * @param response
  251. */
  252. @RequestMapping("/graphic/template-downloads")
  253. public void templateDownloads(HttpServletRequest request, HttpServletResponse response) {
  254. ExcelWriter excelWriter = null;
  255. try {
  256. response.setContentType("application/vnd.ms-excel");
  257. String encode = StandardCharsets.UTF_8.name();
  258. response.setCharacterEncoding(encode);
  259. response.setHeader("Content-disposition", "attachment;filename=relation-template.xlsx");
  260. File templateFile = this.getTemplateFile("relation-template.xlsx");
  261. excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(templateFile).build();
  262. } catch (Exception e) {
  263. log.error("模板下载失败", e);
  264. } finally {
  265. if (excelWriter != null) {
  266. excelWriter.finish();
  267. }
  268. }
  269. }
  270. /**
  271. * 下载报告
  272. *
  273. * @param request
  274. * @param response
  275. */
  276. @RequestMapping("/graphic/report-downloads")
  277. public void reportDownloads(@RequestParam String projectId, @RequestParam String relType, @RequestParam(required = false) String zoneType,
  278. HttpServletRequest request, HttpServletResponse response) {
  279. ExcelWriter excelWriter = null;
  280. try {
  281. response.setContentType("application/vnd.ms-excel");
  282. String encode = StandardCharsets.UTF_8.name();
  283. response.setCharacterEncoding(encode);
  284. String templateFile = projectId + AdmCommonConstant.LINE_THROUGH + relType + AdmCommonConstant.EXCEL_SUFFIX_XLSX;
  285. response.setHeader("Content-disposition", "attachment;filename=" + templateFile);
  286. File file = this.getTemplateOrDefaultFile(templateFile);
  287. excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(file).build();
  288. } catch (Exception e) {
  289. log.error("下载报告失败", e);
  290. } finally {
  291. if (excelWriter != null) {
  292. excelWriter.finish();
  293. }
  294. }
  295. }
  296. /**
  297. * 导出-点位模板
  298. *
  299. * @param request
  300. * @param response
  301. */
  302. @RequestMapping("/graphic/export/point")
  303. public void exportPoint(@RequestParam String buildingId, @RequestParam String floorId, @RequestParam String category,
  304. HttpServletRequest request, HttpServletResponse response) {
  305. ExcelWriter excelWriter = null;
  306. try {
  307. response.setContentType("application/vnd.ms-excel");
  308. String encode = StandardCharsets.UTF_8.name();
  309. response.setCharacterEncoding(encode);
  310. //excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(file).build();
  311. } catch (Exception e) {
  312. log.error("导出-点位模板失败", e);
  313. } finally {
  314. if (excelWriter != null) {
  315. excelWriter.finish();
  316. }
  317. }
  318. }
  319. /**
  320. * 导出关系
  321. *
  322. * @param request
  323. * @param response
  324. */
  325. @RequestMapping("/graphic/export")
  326. public void export(@RequestParam String projectId, @RequestParam String relType, @RequestParam(required = false) String code,
  327. @RequestParam(required = false) String zoneType, HttpServletRequest request, HttpServletResponse response) {
  328. ExcelWriter excelWriter = null;
  329. try {
  330. String groupCode = request.getHeader(AdmCommonConstant.GROUP_CODE_HEADER);
  331. groupCode = StrUtil.isBlank(groupCode) ? defaultCode : groupCode;
  332. code = StrUtil.isBlank(code) ? "对象ID" : code;
  333. response.setContentType("application/vnd.ms-excel");
  334. String encode = StandardCharsets.UTF_8.name();
  335. response.setCharacterEncoding(encode);
  336. response.setHeader("Content-disposition", "attachment;filename=relation-template.xlsx");
  337. log.info("下载报表: groupCode[{}], projectId[{}], relType[{}], code[{}]", groupCode, projectId, relType, code);
  338. AdmRelationTypeEnum typeEnum = AdmCommonConstant.RELATION_TYPE_MAP.get(relType);
  339. List<AdmRelationObject> relationObjects = null;
  340. if (typeEnum != null) {
  341. String graphAndRelKey = typeEnum.getGraphCode() + AdmCommonConstant.UNDERLINE + typeEnum.getRelCode();
  342. relationObjects = this.relationObjectContext.exportRelationObject(groupCode, projectId, graphAndRelKey);
  343. }
  344. File templateFile = this.getTemplateFile("relation-template.xlsx");
  345. EasyExcel.write(response.getOutputStream()).withTemplate(templateFile).sheet(EXPORT_SHEET_NAME).needHead(false).doWrite(relationObjects);
  346. } catch (Exception e) {
  347. log.error("导出关系失败", e);
  348. } finally {
  349. if (excelWriter != null) {
  350. excelWriter.finish();
  351. }
  352. }
  353. }
  354. /************************************************** 文件上传 ***********************************************/
  355. /**
  356. * 文件导入,这里由于需要回写文件,且存储到本地,采用原始POI文件解析、上传、回写、存储
  357. *
  358. * @param relType
  359. * @param code
  360. * @param zoneType
  361. * @param request
  362. * @param response
  363. */
  364. @SuppressWarnings("resource")
  365. @RequestMapping("/graphic/import")
  366. public AdmResponse importFile(@RequestParam("file") MultipartFile file, @RequestParam String relType, @RequestParam(required = false) String zoneType,
  367. HttpServletRequest request, HttpServletResponse response) {
  368. String failure = null;
  369. FileOutputStream fileOutputStream = null;
  370. try {
  371. if (file == null) {
  372. return AdmResponse.failure("未选择文件");
  373. }
  374. AdmRelationTypeEnum typeEnum = AdmCommonConstant.RELATION_TYPE_MAP.get(relType);
  375. if (typeEnum == null) {
  376. return AdmResponse.failure("不支持的关系类型");
  377. }
  378. String graphAndRelKey = typeEnum.getGraphCode() + AdmCommonConstant.UNDERLINE + typeEnum.getRelCode();
  379. int failurNum = 0;
  380. int successNum = 0;
  381. String graphId = null;
  382. // 临时存储行号
  383. Set<Integer> rowNums = new HashSet<Integer>();
  384. // 批量更新数据
  385. List<ObjectNode> relationObjects = new ArrayList<ObjectNode>();
  386. String groupCode = request.getHeader(AdmCommonConstant.GROUP_CODE_HEADER);
  387. String projectId = request.getHeader(AdmCommonConstant.PROJECT_ID_HEADER);
  388. XSSFWorkbook workbook = new XSSFWorkbook(file.getInputStream());
  389. XSSFSheet sheet = workbook.getSheet("关系维护");
  390. // 获取识别码的属性
  391. String code = sheet.getRow(2).getCell(1).toString();
  392. int lastRowNum = sheet.getLastRowNum();
  393. for (int i = 5; i <= lastRowNum; i++) {
  394. XSSFRow xssfRow = sheet.getRow(i);
  395. if (xssfRow == null) {
  396. continue;
  397. }
  398. // 封装cell数据
  399. XSSFCell masterCodeCell = xssfRow.getCell(0);
  400. XSSFCell slaveCodeCell = xssfRow.getCell(8);
  401. XSSFCell masterIdCell = xssfRow.getCell(3);
  402. XSSFCell slaveIdCell = xssfRow.getCell(11);
  403. AdmRelationObject relationObject = new AdmRelationObject();
  404. relationObject.setMasterCode(masterCodeCell == null ? null : masterCodeCell.getStringCellValue());
  405. relationObject.setSlaveCode(slaveCodeCell == null ? null : slaveCodeCell.getStringCellValue());
  406. relationObject.setMasterId(masterIdCell == null ? null : masterIdCell.getStringCellValue());
  407. relationObject.setSlaveId(slaveIdCell == null ? null : slaveIdCell.getStringCellValue());
  408. // 校验数据
  409. Object result = this.relationObjectContext.checkRelationObject(relationObject, groupCode, projectId, graphAndRelKey, code);
  410. if (result instanceof String) {
  411. failurNum++;
  412. xssfRow.createCell(16).setCellValue((String)result);
  413. continue;
  414. }
  415. ObjectNode relation = (ObjectNode) result;
  416. graphId = relation.get("graphId") == null ? graphId : relation.get("graphId").asText();
  417. // 每一千条数据,批量新增一次
  418. rowNums.add(i);
  419. relationObjects.add(relation);
  420. if (relationObjects.size() >= 1000) {
  421. boolean objects = this.relationObjectContext.saveRelationObjects(relationObjects, groupCode, projectId, graphAndRelKey);
  422. if (objects) {
  423. successNum += relationObjects.size();
  424. } else {
  425. failurNum += relationObjects.size();
  426. for (int rowNum : rowNums) {
  427. sheet.getRow(rowNum).createCell(16).setCellValue("调用中台,存储失败");
  428. }
  429. }
  430. rowNums.clear();
  431. relationObjects.clear();
  432. }
  433. }
  434. // 最后一次遗留数据录入
  435. if (CollectionUtil.isNotEmpty(relationObjects)) {
  436. boolean objects = this.relationObjectContext.saveRelationObjects(relationObjects, groupCode, projectId, graphAndRelKey);
  437. if (objects) {
  438. successNum += relationObjects.size();
  439. } else {
  440. failurNum += relationObjects.size();
  441. for (int rowNum : rowNums) {
  442. sheet.getRow(rowNum).createCell(16).setCellValue("调用中台,存储失败");
  443. }
  444. }
  445. }
  446. try {
  447. // 调用中台存储完成之后,此文件存储至本地
  448. String uploadFile = projectId + AdmCommonConstant.LINE_THROUGH + relType + AdmCommonConstant.EXCEL_SUFFIX_XLSX;
  449. File templateFile = this.createTemplateFile(uploadFile);
  450. fileOutputStream = new FileOutputStream(templateFile);
  451. workbook.write(fileOutputStream);
  452. } catch (Exception e) {
  453. log.error("文件存储失败", e);
  454. }
  455. return AdmResponse.success(Lists.newArrayList(this.getImportResult(graphId, successNum, failurNum)));
  456. } catch (Exception e) {
  457. log.error("文件导入失败", e);
  458. failure = e.getMessage();
  459. } finally {
  460. if (fileOutputStream != null) {
  461. try {
  462. fileOutputStream.flush();
  463. fileOutputStream.close();
  464. } catch (IOException e) {}
  465. }
  466. }
  467. return AdmResponse.failure(failure);
  468. }
  469. /************************************************** 本地文件获取、创建 ***********************************************/
  470. /**
  471. * 获取对应的文件,或者默认文件
  472. * 不同路径,防止镜像部署时获取不到
  473. *
  474. * @param templateName 带后缀的文件名
  475. * @return
  476. * @throws FileNotFoundException
  477. */
  478. private File getTemplateOrDefaultFile(String templateName) throws FileNotFoundException {
  479. File file = null;
  480. try {
  481. file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "template/" + templateName);
  482. } catch (FileNotFoundException e) {
  483. file = ResourceUtils.getFile(AdmCommonConstant.SERVER_ROOT_PATH + "template/" + templateName);
  484. }
  485. if (file == null || !file.exists()) {
  486. try {
  487. file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "template/relation-template.xlsx");
  488. } catch (FileNotFoundException e) {
  489. file = ResourceUtils.getFile(AdmCommonConstant.SERVER_ROOT_PATH + "/template/relation-template.xlsx");
  490. }
  491. }
  492. return file;
  493. }
  494. /**
  495. * 获取对应的模板文件,不同路径,防止镜像内获取不到
  496. *
  497. * @param templateName 带后缀的文件名
  498. * @return
  499. * @throws FileNotFoundException
  500. */
  501. private File getTemplateFile(String templateName) throws FileNotFoundException {
  502. File file = null;
  503. try {
  504. file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "template/" + templateName);
  505. } catch (FileNotFoundException e) {}
  506. if (file == null || !file.exists()) {
  507. file = ResourceUtils.getFile(AdmCommonConstant.SERVER_ROOT_PATH + "/template/" + templateName);
  508. }
  509. return file;
  510. }
  511. /**
  512. * 创建本地文件,这里不再在classpath下创建
  513. *
  514. * @param uploadFile 文件名称,带后缀
  515. * @throws IOException
  516. */
  517. private File createTemplateFile(String uploadFile) throws IOException {
  518. String path = AdmCommonConstant.SERVER_ROOT_PATH + "/template";
  519. File file = new File(path);
  520. if (!file.exists()) {
  521. file.mkdirs();
  522. }
  523. file = new File(path + "/" + uploadFile);
  524. if (!file.exists()) {
  525. file.createNewFile();
  526. }
  527. return file;
  528. }
  529. /**
  530. * 获取导入结果
  531. *
  532. * @return
  533. */
  534. private JSONObject getImportResult(String graphId, int successNum, int failureNum) {
  535. JSONObject result = new JSONObject();
  536. result.put("relationType", graphId);
  537. result.put("successCount", successNum);
  538. result.put("failCount", failureNum);
  539. String state = null;
  540. if (successNum > 0 && failureNum > 0) {
  541. state = "1";
  542. } else if (successNum > 0 && failureNum == 0) {
  543. state = "0";
  544. } else {
  545. state = "2";
  546. }
  547. result.put("state", state);
  548. return result;
  549. }
  550. }