123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724 |
- package com.persagy.proxy.adm.controller;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.net.URLEncoder;
- import java.nio.charset.StandardCharsets;
- import java.util.ArrayList;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Set;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.poi.xssf.usermodel.XSSFCell;
- import org.apache.poi.xssf.usermodel.XSSFRow;
- import org.apache.poi.xssf.usermodel.XSSFSheet;
- import org.apache.poi.xssf.usermodel.XSSFWorkbook;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.util.ResourceUtils;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.multipart.MultipartFile;
- import com.alibaba.excel.EasyExcel;
- import com.alibaba.excel.ExcelWriter;
- import com.alibaba.fastjson.JSONObject;
- 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.persagy.dmp.basic.model.QueryCriteria;
- import com.persagy.dmp.common.helper.SpringHelper;
- import com.persagy.proxy.adm.constant.AdmCommonConstant;
- import com.persagy.proxy.adm.constant.AdmRelationTypeEnum;
- import com.persagy.proxy.adm.handler.RelationReportHandler;
- import com.persagy.proxy.adm.model.AdmRelationObject;
- import com.persagy.proxy.adm.model.EquipmentBindPointExcel;
- import com.persagy.proxy.adm.model.EquipmentCountExcel;
- import com.persagy.proxy.adm.model.EquipmentExcel;
- import com.persagy.proxy.adm.request.AdmQueryCriteria;
- import com.persagy.proxy.adm.request.AdmResponse;
- import com.persagy.proxy.adm.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;
- import lombok.RequiredArgsConstructor;
- import lombok.extern.slf4j.Slf4j;
- /**
- * 报表/模板下载
- *
- * @version 1.0.0
- * @company persagy
- * @author zhangqiankun
- * @date 2021年8月30日 下午7:41:56
- */
- @Slf4j
- @RestController
- @RequiredArgsConstructor
- public class RelationReportController {
- public static final String EXPORT_SHEET_NAME = "关系维护";
-
- @Value("${middleware.group.code}")
- private String defaultCode;
-
- private final RelationReportHandler relationReportHandler;
-
- private final RelationObjectContext relationObjectContext;
-
- private final IRelationReportService relationReportService;
-
- /**
- * 查询总数量
- *
- * @param queryCriteria
- * @param request
- * @return
- */
- @RequestMapping("/graphic/query-count")
- public AdmResponse queryCount(HttpServletRequest request) {
- String groupCode = request.getHeader(AdmCommonConstant.GROUP_CODE_HEADER);
- String projectId = request.getHeader(AdmCommonConstant.PROJECT_ID_HEADER);
-
- List<JSONObject> relationProjectCal = this.relationReportService.getAllRelationProjectCalTree(groupCode, projectId);
- return AdmResponse.success(relationProjectCal);
- }
-
- /**
- * 查询总览数量-根据类型查询关系数量
- *
- * @param type relationType
- */
- @RequestMapping("/graphic/type/query-count")
- public AdmResponse countRelationObjects(@RequestParam String type, HttpServletRequest request) {
- String groupCode = request.getHeader(AdmCommonConstant.GROUP_CODE_HEADER);
- String projectId = request.getHeader(AdmCommonConstant.PROJECT_ID_HEADER);
- JSONObject objectNode = this.relationReportService.getRelationProjectCal(groupCode, projectId, type);
- return AdmResponse.success(Lists.newArrayList(objectNode));
- }
-
- /**
- * 查询关系总览(空表,先插入)
- *
- * @param content:模糊查询卡片名称关键字
- * @return
- */
- @RequestMapping("/graphic/overview")
- public JSONObject overview(@RequestParam(required = false) String content) throws JsonProcessingException {
- JSONObject overview = this.relationReportService.overview(content);
- return overview;
- }
-
- /**
- * 项目关系类型查询
- * {"filters":"relationType in [\"bd2sp\",\"fl2sp\"];computationalState>1"}
- *
- * @param queryCriteria
- * @param request
- * @return
- */
- @RequestMapping("/graphic/relation_type_project")
- public AdmResponse queryRelationObjects(@RequestBody AdmQueryCriteria requestCriteria, HttpServletRequest request) {
- String groupCode = request.getHeader(AdmCommonConstant.GROUP_CODE_HEADER);
- String projectId = request.getHeader(AdmCommonConstant.PROJECT_ID_HEADER);
- String filters = requestCriteria.getFilters();
-
- QueryCriteria queryCriteria = new QueryCriteria();
- ObjectMapper objectMapper = SpringHelper.getBean(ObjectMapper.class);
- ObjectNode criteria = objectMapper.createObjectNode();
- criteria.put("projectId", projectId);
-
- Set<String> graphs = new HashSet<String>();
- Set<String> relCodes = new HashSet<String>();
- if (StrUtil.isNotBlank(filters)) {
- if (filters.contains("relationType")) {
- String condition = filters.substring(filters.indexOf("in [") + 4, filters.lastIndexOf("]"));
- String[] split = condition.split(",");
- for (String relType : split) {
- relType = relType.replaceAll("\"", "");
- AdmRelationTypeEnum typeEnum = AdmCommonConstant.RELATION_TYPE_MAP.get(relType);
- if (typeEnum != null) {
- graphs.add(typeEnum.getGraphCode());
- relCodes.add(typeEnum.getRelCode());
- }
- }
- }
-
- String[] split = filters.split(";");
- for (int i = 0; i < split.length; i++) {
- if (i == 0) {
- continue;
- }
- String express = split[i];
- String cond = StrUtil.getContainsStr(express, AdmQueryCriteriaHelper.COND_STR);
- cond = StrUtil.isBlank(cond) ? StrUtil.getContainsStr(express, AdmQueryCriteriaHelper.COND_STR_TRIM) : cond;
- if(StrUtil.isBlank(cond)) {
- continue;
- }
- int condIndex = StrUtil.indexOfIgnoreCase(express, cond, 0);
- String condLeft = StrUtil.subPre(express, condIndex).trim();
- String condRight = StrUtil.subSuf(express, condIndex+cond.length()).trim();
- // condLeft 替换为中台的字段名称
- condLeft = this.convert2BDTPField(condLeft);
- if ("=".equals(cond)) {
- criteria.put(condLeft, condRight);
- } else {
- JSONObject condition = new JSONObject();
- condition.put(AdmQueryCriteriaHelper.OPERATOR_MAP.get(cond), condRight);
- criteria.putPOJO(condLeft, condition);
- }
- }
- }
- if (CollectionUtil.isNotEmpty(graphs)) {
- JSONObject graphObject = new JSONObject();
- graphObject.put("$in", graphs);
- criteria.putPOJO("graphCode", graphObject);
- }
- if (CollectionUtil.isNotEmpty(relCodes)) {
- JSONObject relObject = new JSONObject();
- relObject.put("$in", relCodes);
- criteria.putPOJO("relCode", relObject);
- }
-
- queryCriteria.setCriteria(criteria);
- List<JSONObject> relationProjectCal = this.relationReportService.getAllRelationProjectCal(queryCriteria, groupCode, projectId);
- return AdmResponse.success(relationProjectCal);
- }
-
- /**
- * 下载报告-查询key值
- *
- * @param queryCriteria
- * @param request
- * @return
- */
- @RequestMapping("/graphic/downloads/point/key")
- public AdmResponse downloadsPointKey(@RequestBody AdmQueryCriteria queryCriteria, HttpServletRequest request) {
- //String groupCode = request.getHeader(AdmCommonConstant.GROUP_CODE_HEADER);
- //String projectId = request.getHeader(AdmCommonConstant.PROJECT_ID_HEADER);
- //List<ObjectNode> relationProjectCal = this.relationReportService.getAllRelationProjectCal(groupCode, projectId);
- return AdmResponse.success(null);
- }
-
- /**
- * 下载报告-设备-未关联空间的设备
- * 设备.xlsx 是个空模板,所以这里未使用
- *
- * @param projectId
- * @param request
- * @param response
- * @return
- */
- @RequestMapping("/graphic/downloads/eq/not/sp")
- public void downloadsEqNotSp(@RequestParam(required = false) String projectId, HttpServletRequest request, HttpServletResponse response) {
- String groupCode = request.getHeader(AdmCommonConstant.GROUP_CODE_HEADER);
- if (StrUtil.isBlank(projectId)) {
- projectId = request.getHeader(AdmCommonConstant.PROJECT_ID_HEADER);
- }
- try {
- response.setContentType("application/vnd.ms-excel");
- String encode = StandardCharsets.UTF_8.name();
- response.setCharacterEncoding(encode);
- // 防止中文乱码
- String fileName = URLEncoder.encode("equipment", encode);
- response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
-
- List<EquipmentExcel> equipByNotSpace = this.relationReportHandler.findEquipByNotSpace(groupCode, projectId);
- EasyExcel.write(response.getOutputStream(), EquipmentExcel.class).sheet("设备").doWrite(equipByNotSpace);
- } catch(Exception e) {
- log.error("设备数据导出异常", e);
- }
- }
-
- /**
- * 下载报告-统计项目下已有的设备,信息点,每个设备
- * 设备.xlsx 是个空模板,所以这里未使用
- *
- * @param projectId
- * @param request
- * @param response
- * @return
- */
- @RequestMapping("/graphic/downloads/project/binding/point")
- public void downloadsProjectBindingPoint(@RequestParam(required = false) String projectId, HttpServletRequest request, HttpServletResponse response) {
- String groupCode = request.getHeader(AdmCommonConstant.GROUP_CODE_HEADER);
- if (StrUtil.isBlank(projectId)) {
- projectId = request.getHeader(AdmCommonConstant.PROJECT_ID_HEADER);
- }
- try {
- response.setContentType("application/vnd.ms-excel");
- String encode = StandardCharsets.UTF_8.name();
- response.setCharacterEncoding(encode);
- // 防止中文乱码
- String fileName = URLEncoder.encode("设备实例交付", encode);
- response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
-
- List<EquipmentBindPointExcel> equipmentBindPointExcel = this.relationReportHandler.queryProjectBindPoint(groupCode, projectId);
- EasyExcel.write(response.getOutputStream(), EquipmentBindPointExcel.class).sheet(0).doWrite(equipmentBindPointExcel);
- } catch(Exception e) {
- log.error("设备数据导出异常", e);
- }
- }
-
- /**
- * 下载报告-统计项目下已有的设备,以及静态和iot 信息点使用情况
- * 设备.xlsx 是个空模板,所以这里未使用
- *
- * @param projectId
- * @param request
- * @param response
- * @return
- */
- @RequestMapping("/graphic/downloads/project/equip/count")
- public void downloadsProjectEquipPoint(@RequestParam(required = false) String projectId, HttpServletRequest request, HttpServletResponse response) {
- String groupCode = request.getHeader(AdmCommonConstant.GROUP_CODE_HEADER);
- if (StrUtil.isBlank(projectId)) {
- projectId = request.getHeader(AdmCommonConstant.PROJECT_ID_HEADER);
- }
- try {
- response.setContentType("application/vnd.ms-excel");
- String encode = StandardCharsets.UTF_8.name();
- response.setCharacterEncoding(encode);
- // 防止中文乱码
- String fileName = URLEncoder.encode("equipment", encode);
- response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
-
- List<EquipmentCountExcel> equipmentCountExcel = this.relationReportHandler.countClassCodeEquip(groupCode, projectId);
- EasyExcel.write(response.getOutputStream(), EquipmentCountExcel.class).sheet(0).doWrite(equipmentCountExcel);
- } catch(Exception e) {
- log.error("设备数据导出异常", e);
- }
- }
-
- /**
- * 下载报告-设备静态信息维护模板
- *
- * @param request
- * @param response
- */
- @RequestMapping("/graphic/downloads/equip/template")
- public void downloadEquipTemplate(HttpServletRequest request, HttpServletResponse response) {
- ExcelWriter excelWriter = null;
- try {
- response.setContentType("application/vnd.ms-excel");
- String encode = StandardCharsets.UTF_8.name();
- response.setCharacterEncoding(encode);
- // 防止中文乱码
- String fileName = URLEncoder.encode("台账信息导入模板", encode);
- response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
-
- File templateFile = this.getTemplateFile("equip_template.xlsx");
- excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(templateFile).build();
- } catch (Exception e) {
- log.error("设备静态信息维护模板下载失败", e);
- } finally {
- if (excelWriter != null) {
- excelWriter.finish();
- }
- }
- }
- /**
- * 下载报告-点表
- *
- * @param request
- * @param response
- */
- @RequestMapping("/graphic/downloads/point")
- public void downloadPoint(@RequestParam(required = false) String key, HttpServletRequest request, HttpServletResponse response) {
- ExcelWriter excelWriter = null;
- try {
- response.setContentType("application/vnd.ms-excel");
- String encode = StandardCharsets.UTF_8.name();
- response.setCharacterEncoding(encode);
- response.setHeader("Content-disposition", "attachment;filename=" + key);
-
- File file = null;
- try {
- file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "template/upload/" + key);
- } catch (FileNotFoundException e) {
- file = ResourceUtils.getFile(AdmCommonConstant.SERVER_ROOT_PATH + "template/upload/" + key);
- }
- 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");
- }
- }
-
- excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(file).build();
- } catch (Exception e) {
- log.error("下载报告-点表失败", e);
- } finally {
- if (excelWriter != null) {
- excelWriter.finish();
- }
- }
- }
-
- /**
- * 下载模板
- *
- * @param request
- * @param response
- */
- @RequestMapping("/graphic/template-downloads")
- public void templateDownloads(HttpServletRequest request, HttpServletResponse response) {
- ExcelWriter excelWriter = null;
- try {
- response.setContentType("application/vnd.ms-excel");
- String encode = StandardCharsets.UTF_8.name();
- response.setCharacterEncoding(encode);
- response.setHeader("Content-disposition", "attachment;filename=relation-template.xlsx");
-
- File templateFile = this.getTemplateFile("relation-template.xlsx");
- excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(templateFile).build();
- } catch (Exception e) {
- log.error("模板下载失败", e);
- } finally {
- if (excelWriter != null) {
- excelWriter.finish();
- }
- }
- }
-
- /**
- * 下载报告
- *
- * @param request
- * @param response
- */
- @RequestMapping("/graphic/report-downloads")
- public void reportDownloads(@RequestParam String projectId, @RequestParam String relType, @RequestParam(required = false) String zoneType,
- HttpServletRequest request, HttpServletResponse response) {
- ExcelWriter excelWriter = null;
- try {
- response.setContentType("application/vnd.ms-excel");
- String encode = StandardCharsets.UTF_8.name();
- response.setCharacterEncoding(encode);
-
- String templateFile = projectId + AdmCommonConstant.LINE_THROUGH + relType + AdmCommonConstant.EXCEL_SUFFIX_XLSX;
- response.setHeader("Content-disposition", "attachment;filename=" + templateFile);
-
- File file = this.getTemplateOrDefaultFile(templateFile);
- excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(file).build();
- } catch (Exception e) {
- log.error("下载报告失败", e);
- } finally {
- if (excelWriter != null) {
- excelWriter.finish();
- }
- }
- }
-
- /**
- * 导出-点位模板
- *
- * @param request
- * @param response
- */
- @RequestMapping("/graphic/export/point")
- public void exportPoint(@RequestParam String buildingId, @RequestParam String floorId, @RequestParam String category,
- HttpServletRequest request, HttpServletResponse response) {
- ExcelWriter excelWriter = null;
- try {
- response.setContentType("application/vnd.ms-excel");
- String encode = StandardCharsets.UTF_8.name();
- response.setCharacterEncoding(encode);
-
- //excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(file).build();
- } catch (Exception e) {
- log.error("导出-点位模板失败", e);
- } finally {
- if (excelWriter != null) {
- excelWriter.finish();
- }
- }
- }
-
- /**
- * 导出关系
- *
- * @param request
- * @param response
- */
- @RequestMapping("/graphic/export")
- public void export(@RequestParam String projectId, @RequestParam String relType, @RequestParam(required = false) String code,
- @RequestParam(required = false) String zoneType, HttpServletRequest request, HttpServletResponse response) {
- ExcelWriter excelWriter = null;
- try {
- String groupCode = request.getHeader(AdmCommonConstant.GROUP_CODE_HEADER);
- groupCode = StrUtil.isBlank(groupCode) ? defaultCode : groupCode;
- code = StrUtil.isBlank(code) ? "对象ID" : code;
-
- response.setContentType("application/vnd.ms-excel");
- String encode = StandardCharsets.UTF_8.name();
- response.setCharacterEncoding(encode);
- response.setHeader("Content-disposition", "attachment;filename=relation-template.xlsx");
-
- log.info("下载报表: groupCode[{}], projectId[{}], relType[{}], code[{}]", groupCode, projectId, relType, code);
- AdmRelationTypeEnum typeEnum = AdmCommonConstant.RELATION_TYPE_MAP.get(relType);
-
- List<AdmRelationObject> relationObjects = null;
- if (typeEnum != null) {
- String graphAndRelKey = typeEnum.getGraphCode() + AdmCommonConstant.UNDERLINE + typeEnum.getRelCode();
- relationObjects = this.relationObjectContext.exportRelationObject(groupCode, projectId, graphAndRelKey);
- }
-
- File templateFile = this.getTemplateFile("relation-template.xlsx");
- EasyExcel.write(response.getOutputStream()).withTemplate(templateFile).sheet(EXPORT_SHEET_NAME).needHead(false).doWrite(relationObjects);
- } catch (Exception e) {
- log.error("导出关系失败", e);
- } finally {
- if (excelWriter != null) {
- excelWriter.finish();
- }
- }
- }
-
-
- /************************************************** 文件上传 ***********************************************/
-
- /**
- * 文件导入,这里由于需要回写文件,且存储到本地,采用原始POI文件解析、上传、回写、存储
- *
- * @param relType
- * @param code
- * @param zoneType
- * @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;
- FileOutputStream fileOutputStream = null;
- try {
- if (file == null) {
- return AdmResponse.failure("未选择文件");
- }
- AdmRelationTypeEnum typeEnum = AdmCommonConstant.RELATION_TYPE_MAP.get(relType);
- if (typeEnum == null) {
- return AdmResponse.failure("不支持的关系类型");
- }
- String graphAndRelKey = typeEnum.getGraphCode() + AdmCommonConstant.UNDERLINE + typeEnum.getRelCode();
-
- int failurNum = 0;
- int successNum = 0;
- String graphId = null;
- // 临时存储行号
- Set<Integer> rowNums = new HashSet<Integer>();
- // 批量更新数据
- List<ObjectNode> relationObjects = new ArrayList<ObjectNode>();
-
- String groupCode = request.getHeader(AdmCommonConstant.GROUP_CODE_HEADER);
- String projectId = request.getHeader(AdmCommonConstant.PROJECT_ID_HEADER);
-
- XSSFWorkbook 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;
- }
- // 封装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, groupCode, projectId, graphAndRelKey, code);
- if (result instanceof String) {
- failurNum++;
- xssfRow.createCell(16).setCellValue((String)result);
- continue;
- }
- ObjectNode relation = (ObjectNode) result;
- graphId = relation.get("graphId") == null ? graphId : relation.get("graphId").asText();
-
- // 每一千条数据,批量新增一次
- rowNums.add(i);
- relationObjects.add(relation);
- if (relationObjects.size() >= 1000) {
- boolean objects = this.relationObjectContext.saveRelationObjects(relationObjects, groupCode, projectId, graphAndRelKey);
- if (objects) {
- successNum += relationObjects.size();
- } else {
- failurNum += relationObjects.size();
- for (int rowNum : rowNums) {
- sheet.getRow(rowNum).createCell(16).setCellValue("调用中台,存储失败");
- }
- }
- rowNums.clear();
- relationObjects.clear();
- }
- }
- // 最后一次遗留数据录入
- if (CollectionUtil.isNotEmpty(relationObjects)) {
- boolean objects = this.relationObjectContext.saveRelationObjects(relationObjects, groupCode, projectId, graphAndRelKey);
- if (objects) {
- successNum += relationObjects.size();
- } else {
- failurNum += relationObjects.size();
- for (int rowNum : rowNums) {
- sheet.getRow(rowNum).createCell(16).setCellValue("调用中台,存储失败");
- }
- }
- }
-
- try {
- // 调用中台存储完成之后,此文件存储至本地
- String uploadFile = projectId + AdmCommonConstant.LINE_THROUGH + relType + AdmCommonConstant.EXCEL_SUFFIX_XLSX;
- File templateFile = this.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)));
- } catch (Exception e) {
- log.error("文件导入失败", e);
- failure = e.getMessage();
- } finally {
- if (fileOutputStream != null) {
- try {
- fileOutputStream.flush();
- fileOutputStream.close();
- } catch (IOException e) {}
- }
- }
- return AdmResponse.failure(failure);
- }
-
-
-
-
- /************************************************** 本地文件获取、创建 ***********************************************/
-
-
- /**
- * 获取对应的文件,或者默认文件
- * 不同路径,防止镜像部署时获取不到
- *
- * @param templateName 带后缀的文件名
- * @return
- * @throws FileNotFoundException
- */
- private 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
- */
- 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
- */
- private 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
- */
- 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;
- }
-
- /**
- * 将前端所传的字段,转为BDTP中台所需要的字段
- *
- * @param condLeft
- * @return 转换后的,或原字段
- */
- private String convert2BDTPField(String condLeft) {
- if ("automatic".equals(condLeft)) {
- return "automaticFlag";
- }
- if ("source".equals(condLeft)) {
- return "sourceFlag";
- }
- /*if ("graphicId".equals(condLeft)) {
- return "graphCode";
- }*/
- return condLeft;
- }
- }
|