AdmEquipmentController.java 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. package com.persagy.proxy.object.controller;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.collection.CollectionUtil;
  4. import cn.hutool.core.date.DateUtil;
  5. import cn.hutool.core.map.MapUtil;
  6. import cn.hutool.core.util.IdUtil;
  7. import cn.hutool.core.util.StrUtil;
  8. import cn.hutool.json.JSONUtil;
  9. import com.alibaba.fastjson.JSONObject;
  10. import com.fasterxml.jackson.databind.ObjectMapper;
  11. import com.fasterxml.jackson.databind.node.JsonNodeFactory;
  12. import com.fasterxml.jackson.databind.node.ObjectNode;
  13. import com.google.common.collect.Sets;
  14. import com.persagy.dmp.basic.dto.RequestData;
  15. import com.persagy.dmp.basic.model.QueryCriteria;
  16. import com.persagy.dmp.basic.utils.JsonNodeUtils;
  17. import com.persagy.dmp.common.context.AppContext;
  18. import com.persagy.dmp.common.exception.BusinessException;
  19. import com.persagy.dmp.common.helper.SpringHelper;
  20. import com.persagy.dmp.common.model.entity.BaseEntity;
  21. import com.persagy.dmp.common.model.response.CommonResult;
  22. import com.persagy.dmp.define.client.DigitalDefineFacade;
  23. import com.persagy.dmp.define.entity.ObjectInfoDefine;
  24. import com.persagy.dmp.define.entity.ObjectTypeDefine;
  25. import com.persagy.dmp.digital.client.DigitalObjectFacade;
  26. import com.persagy.dmp.digital.entity.ObjectDigital;
  27. import com.persagy.dmp.simple.client.DigitalObjectSimpleFacade;
  28. import com.persagy.proxy.adm.constant.AdmCommonConstant;
  29. import com.persagy.proxy.adm.constant.AdmDictCategoryEnum;
  30. import com.persagy.proxy.adm.constant.AdmRelationTypeEnum;
  31. import com.persagy.proxy.adm.request.AdmCreateRequest;
  32. import com.persagy.proxy.adm.request.AdmQueryCriteria;
  33. import com.persagy.proxy.adm.request.AdmResponse;
  34. import com.persagy.proxy.adm.service.IAdmRelationService;
  35. import com.persagy.proxy.adm.utils.AdmContextUtil;
  36. import com.persagy.proxy.adm.utils.AdmExcelUtil;
  37. import com.persagy.proxy.adm.utils.ObjectNameUtil;
  38. import com.persagy.proxy.common.entity.RelationDTO;
  39. import com.persagy.proxy.dictionary.model.AdmDict;
  40. import com.persagy.proxy.dictionary.model.AdmObjectType;
  41. import com.persagy.proxy.dictionary.service.IAdmDictService;
  42. import com.persagy.proxy.object.model.AdmEquipment;
  43. import com.persagy.proxy.object.service.IAdmEquipmentService;
  44. import com.persagy.proxy.report.service.IRelationReportService;
  45. import lombok.extern.slf4j.Slf4j;
  46. import org.apache.poi.ss.usermodel.CellType;
  47. import org.apache.poi.ss.usermodel.FillPatternType;
  48. import org.apache.poi.xssf.usermodel.*;
  49. import org.springframework.beans.factory.annotation.Autowired;
  50. import org.springframework.web.bind.annotation.*;
  51. import org.springframework.web.multipart.MultipartFile;
  52. import javax.servlet.http.HttpServletRequest;
  53. import javax.servlet.http.HttpServletResponse;
  54. import java.awt.*;
  55. import java.io.FileOutputStream;
  56. import java.io.IOException;
  57. import java.util.List;
  58. import java.util.*;
  59. /**
  60. * Adm 楼层 API
  61. * @author lvxy
  62. * @date 2021/8/16
  63. */
  64. @Slf4j
  65. @RestController
  66. @RequestMapping("/object/equip")
  67. public class AdmEquipmentController {
  68. @Autowired
  69. private IAdmEquipmentService service;
  70. @Autowired
  71. private IAdmRelationService relationService;
  72. @Autowired
  73. private IRelationReportService relationReportService;
  74. @Autowired
  75. private IAdmDictService dictService;
  76. /**
  77. * 统计
  78. *
  79. * @param request
  80. * @return
  81. * @throws Exception
  82. */
  83. @PostMapping("/count")
  84. public AdmResponse count(@RequestBody AdmQueryCriteria request) throws Exception {
  85. request.setName(AdmEquipment.OBJ_TYPE);
  86. request.setOnlyCount(true);
  87. return service.query(request);
  88. }
  89. /**
  90. * 创建
  91. * @param createRequest
  92. * @return
  93. * @throws Exception
  94. */
  95. @PostMapping("/create")
  96. public AdmResponse create(@RequestBody AdmCreateRequest<AdmEquipment> createRequest) throws Exception {
  97. List<AdmEquipment> vos = CollUtil.newArrayList(createRequest.getContent());
  98. if(CollUtil.isEmpty(vos)){
  99. return AdmResponse.success();
  100. }
  101. // 查询所有设备类型
  102. AdmDict dictCond = AdmDict.builder().type(AdmDictCategoryEnum.EQUIPMENT.getValue()).build();
  103. AdmResponse dictResponse = dictService.queryCategory(AdmContextUtil.toDmpContext(), dictCond, AdmObjectType.class);
  104. List<AdmObjectType> dicts = (List<AdmObjectType>) dictResponse.getContent();
  105. // 转换为Map,key为编码后4位。用于适配旧类型编码
  106. Map<String, AdmObjectType> shortDictMap = new HashMap<>();
  107. Map<String, AdmObjectType> fullDictMap = new HashMap<>();
  108. for(int i = 0,j = CollUtil.size(dicts);i < j;i++) {
  109. AdmObjectType dict = dicts.get(i);
  110. shortDictMap.put(StrUtil.subSufByLength(dict.getCode(), 4), dict);
  111. fullDictMap.put(dict.getCode(), dict);
  112. }
  113. // 设备属性调整
  114. vos.stream().forEach(equipment -> {
  115. // 如果不是Eq开头,则重新生成ID
  116. if(StrUtil.startWith(equipment.getId(), "Eq")) {
  117. equipment.setId("Eq"+ IdUtil.simpleUUID());
  118. }
  119. // 类型编码适配
  120. String classCode = equipment.getClassCode();
  121. // 如果编码为空,或在4位、6位里都不存在,则报错
  122. if(StrUtil.isBlank(classCode) ||
  123. (shortDictMap.get(classCode) == null && fullDictMap.get(classCode) == null)) {
  124. throw new BusinessException(StrUtil.format("没有找到对象类型【{}】!", equipment.getClassCode()));
  125. }
  126. // 如果少于6位,则需要适配新编码
  127. if(classCode.length() < 6) {
  128. AdmObjectType dict = MapUtil.get(shortDictMap, classCode, AdmObjectType.class);
  129. equipment.setClassCode(dict.getCode());
  130. }
  131. // 如果名称为空,则提供默认名称
  132. if (StrUtil.isEmpty(equipment.getName())){
  133. AdmObjectType dict = MapUtil.get(fullDictMap, equipment.getClassCode(), AdmObjectType.class);
  134. String preName = dict == null ? "设备" : dict.getName();
  135. equipment.setName(ObjectNameUtil.objectName(preName + "-"));
  136. }
  137. });
  138. vos = service.doInsert(AdmContextUtil.toDmpContext(), AdmEquipment.class, vos);
  139. return AdmResponse.success(vos);
  140. }
  141. /**
  142. * 根据ID删除
  143. * @param vos
  144. * @return
  145. * @throws Exception
  146. */
  147. @PostMapping("/delete")
  148. public AdmResponse delete( @RequestBody List<AdmEquipment> vos) throws Exception {
  149. // 组装上下文条件
  150. service.doDelete(AdmContextUtil.toDmpContext(), vos);
  151. return AdmResponse.success();
  152. }
  153. /**
  154. * 更新
  155. * @param createRequest
  156. * @return
  157. * @throws Exception
  158. */
  159. @PostMapping("/update")
  160. public AdmResponse update(@RequestBody AdmCreateRequest<AdmEquipment> createRequest) throws Exception {
  161. List<AdmEquipment> vos = CollUtil.newArrayList(createRequest.getContent());
  162. //vos = service.doUpdateEuip(AdmContextUtil.toDmpContext(), AdmEquipment.class, vos);
  163. //针对设备更新时,所有更新的字段都放到信息点中的情况特殊处理
  164. if(CollUtil.isNotEmpty(vos)){
  165. Map<String, AdmEquipment> admEquipmentMap = CollUtil.fieldValueMap(vos, BaseEntity.PROP_ID);
  166. AdmRelationTypeEnum typeEnum = AdmRelationTypeEnum.EQ2BD;
  167. String graphCode = typeEnum.getGraphCode();
  168. QueryCriteria criteria = new QueryCriteria();
  169. ObjectNode node = JsonNodeFactory.instance.objectNode();
  170. node.put("graphCode", graphCode);
  171. admEquipmentMap.keySet().stream().forEach(equipId -> {
  172. node.put("relCode", "Eq2Bd");
  173. node.put("objFrom", equipId);
  174. criteria.setCriteria(node);
  175. relationService.doDelete(AdmContextUtil.toDmpContext(), criteria);
  176. node.put("relCode", "Eq2Fl");
  177. node.put("objFrom", equipId);
  178. criteria.setCriteria(node);
  179. relationService.doDelete(AdmContextUtil.toDmpContext(), criteria);
  180. });
  181. List<RelationDTO> relationDTOList = new ArrayList<>(vos.size());
  182. admEquipmentMap.keySet().stream().forEach(equipId -> {
  183. if(StrUtil.isNotEmpty(admEquipmentMap.get(equipId).getBuildingId())){
  184. RelationDTO relationbd = new RelationDTO(null, graphCode, "Eq2Bd", null, equipId, admEquipmentMap.get(equipId).getBuildingId());
  185. relationDTOList.add(relationbd);
  186. }
  187. if(StrUtil.isNotEmpty(admEquipmentMap.get(equipId).getFloorId())) {
  188. RelationDTO relationfl = new RelationDTO(null, graphCode, "Eq2Fl", null, equipId, admEquipmentMap.get(equipId).getFloorId());
  189. relationDTOList.add(relationfl);
  190. }
  191. });
  192. if(CollUtil.isNotEmpty(relationDTOList)){
  193. relationService.doSave(AdmContextUtil.toDmpContext(), relationDTOList);
  194. }
  195. //处理设备更新特殊数据
  196. List<AdmEquipment> equipmentList = new ArrayList<>(vos.size());
  197. vos.stream().forEach(equipment -> {
  198. if(CollUtil.isNotEmpty(equipment.getInfos())){
  199. AdmEquipment admEquipment = new AdmEquipment();
  200. admEquipment.setId(equipment.getId());
  201. admEquipment.setInfos(equipment.getInfos());
  202. ObjectNode objectNode = JsonNodeUtils.toObjectNode(admEquipment, "infos", null);
  203. AdmEquipment admEquipmentNew = JsonNodeUtils.toEntity(objectNode, AdmEquipment.class, "infos");
  204. if(CollUtil.isNotEmpty(equipment.getNullList())){
  205. admEquipmentNew.setNullList(equipment.getNullList());
  206. }
  207. if(StrUtil.isNotEmpty(equipment.getFloorId())){
  208. admEquipmentNew.setFloorId(equipment.getFloorId());
  209. }
  210. if(StrUtil.isNotEmpty(equipment.getBuildingId())){
  211. admEquipmentNew.setBuildingId(equipment.getBuildingId());
  212. }
  213. equipmentList.add(admEquipmentNew);
  214. }
  215. });
  216. vos = equipmentList;
  217. }
  218. vos = service.doUpdate(AdmContextUtil.toDmpContext(), AdmEquipment.class, vos);
  219. return AdmResponse.success(vos);
  220. }
  221. /**
  222. * 查询
  223. *
  224. * @param request
  225. * @return
  226. * @throws Exception
  227. */
  228. @PostMapping("/query")
  229. public AdmResponse query(@RequestBody AdmQueryCriteria request) throws Exception {
  230. // 组装上下文条件
  231. request.setName(AdmEquipment.OBJ_TYPE);
  232. if(CollUtil.isEmpty(request.getCascade()) && CollUtil.isNotEmpty(request.getProjection()) && request.getDistinct() && request.getProjection().size() == 1 && request.getProjection().get(0).equals("classCode")) {
  233. /** 特殊处理
  234. * distinct: true
  235. * pageNumber: 1
  236. * pageSize: 1000
  237. * projection: ["classCode"]
  238. * 0: "classCode"
  239. */
  240. RequestData requestData = new RequestData();
  241. requestData.setProjection(request.getProjection());
  242. requestData.setObjType(AdmEquipment.OBJ_TYPE);
  243. requestData.setPage(request.getPageNumber().longValue());
  244. requestData.setSize(request.getPageSize().longValue());
  245. CommonResult<List<ObjectNode>> result = DigitalObjectSimpleFacade.queryObjListByConditions(AdmContextUtil.toDmpContext().getGroupCode(), AdmContextUtil.toDmpContext().getProjectId(), AdmContextUtil.toDmpContext().getAppId(), null, requestData);
  246. if (result == null || CollUtil.isEmpty(result.getData())) {
  247. return AdmResponse.success();
  248. }else{
  249. List<AdmEquipment> admEquipments = JSONUtil.toList(result.getData().toString(), AdmEquipment.class);
  250. service.ensureSpecialValues(AdmContextUtil.toDmpContext(), admEquipments);
  251. return AdmResponse.success(admEquipments);
  252. }
  253. }else{
  254. return service.query(request);
  255. }
  256. }
  257. /**
  258. * 设备绑定的动态信息点或静态信息点
  259. * @param admEquipment
  260. * @return
  261. * @throws Exception
  262. */
  263. @GetMapping("/export")
  264. public AdmResponse exportInfos(@RequestBody AdmCreateRequest<AdmEquipment> admEquipment) throws Exception {
  265. List<AdmEquipment> vos = CollUtil.newArrayList(admEquipment.getContent());
  266. vos = service.doUpdate(AdmContextUtil.toDmpContext(), AdmEquipment.class, vos);
  267. return AdmResponse.success(vos);
  268. }
  269. /**
  270. * 根据项目id查询所有的设备和部件
  271. *
  272. * @param request
  273. * @return
  274. * @throws Exception
  275. */
  276. @GetMapping("/query-equip")
  277. public AdmResponse queryEquip(@RequestBody AdmQueryCriteria request) throws Exception {
  278. //request.setName("equipment");
  279. if(StrUtil.isNotEmpty(request.getFilters())){
  280. request.setFilters(String.join(";",request.getFilters(),"objType in ['equipment','component']"));
  281. }else{
  282. request.setFilters("objType in ['equipment','component']");
  283. }
  284. return service.query(request);
  285. }
  286. /**
  287. * 批量创建设备同时创建资产、创建设备和系统的关系
  288. * @param admEquipment
  289. * @return
  290. * @throws Exception
  291. */
  292. @PostMapping("/creat-equip-pro")
  293. public AdmResponse createExt(@RequestBody AdmCreateRequest<AdmEquipment> admEquipment) throws Exception {
  294. List<AdmEquipment> vos = admEquipment.getContent();
  295. if(CollUtil.isEmpty(vos)){
  296. return AdmResponse.success();
  297. }
  298. //创建设备
  299. vos.stream().forEach(equipment -> {
  300. if (StrUtil.isEmpty(equipment.getName())){
  301. equipment.setName(ObjectNameUtil.objectName("设备-"));
  302. }
  303. });
  304. List<AdmEquipment> reVos = new ArrayList<>(vos.size());
  305. List<RelationDTO> relationDTOS = new ArrayList<>(16);
  306. vos.stream().forEach(admEq -> {
  307. //创建设备
  308. List<AdmEquipment> vosR = service.doInsert(AdmContextUtil.toDmpContext(), AdmEquipment.class, CollUtil.newArrayList(admEq));
  309. //TODO 创建资产
  310. //添加设备和系统、建筑、楼层的关系
  311. if(CollUtil.isNotEmpty(vosR)){
  312. String equipId = vosR.get(0).getId();
  313. if(StrUtil.isNotEmpty(equipId)){
  314. if(CollUtil.isNotEmpty(admEq.getLinkSystem())){
  315. admEq.getLinkSystem().stream().forEach(admSystem -> {
  316. relationDTOS.add(new RelationDTO(null, "MechSubset", "Sy2Eq", null , admSystem.getId(), equipId));
  317. });
  318. }
  319. if(StrUtil.isNotEmpty(admEq.getBuildingId())){
  320. relationDTOS.add(new RelationDTO(null, "MechInArch", "Eq2Bd", null , equipId, admEq.getBuildingId()));
  321. }
  322. if(StrUtil.isNotEmpty(admEq.getFloorId())){
  323. relationDTOS.add(new RelationDTO(null, "MechInArch", "Eq2Fl", null , equipId, admEq.getFloorId()));
  324. }
  325. }
  326. }
  327. reVos.addAll(vosR);
  328. });
  329. relationService.doSave(AdmContextUtil.toDmpContext(),relationDTOS);
  330. return AdmResponse.success(reVos);
  331. }
  332. /**
  333. * 设备上传excel识别静态信息点
  334. *
  335. * @param file
  336. * @param request
  337. * @param response
  338. */
  339. @RequestMapping("/import")
  340. public AdmResponse importFile(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response) {
  341. if (file == null) {
  342. return AdmResponse.failure("未选择文件");
  343. }
  344. String path = AdmCommonConstant.SERVER_ROOT_PATH + AdmExcelUtil.TEMP_UPLOAD_PAHT;
  345. int failurNum = 0;
  346. int successCount = 0;
  347. String failure = null;
  348. String primaryKey = null;
  349. String classCodeCloumn = null;
  350. XSSFWorkbook workbook = null;
  351. FileOutputStream fileOutputStream = null;
  352. String groupCode = AppContext.getContext().getGroupCode();
  353. String projectId = AppContext.getContext().getProjectId();
  354. try {
  355. workbook = new XSSFWorkbook(file.getInputStream());
  356. XSSFSheet sheet = workbook.getSheetAt(0); // 第一个sheet
  357. XSSFRow row1 = sheet.getRow(1);
  358. if (row1 == null) {
  359. AdmExcelUtil.writeFile2Local(workbook, projectId, path, file.getOriginalFilename());
  360. return AdmExcelUtil.getImportFailResult("数据不存在", "2", 0, ++failurNum);
  361. }
  362. /** 获取识别码 */
  363. XSSFCell markCell = row1.getCell(0);
  364. if (markCell == null) {
  365. AdmExcelUtil.writeFile2Local(workbook, projectId, path, file.getOriginalFilename());
  366. return AdmExcelUtil.getImportFailResult("未填写识别码", "2", 0, ++failurNum);
  367. }
  368. primaryKey = markCell.getStringCellValue().trim(); // 识别码
  369. boolean containsKey = AdmCommonConstant.codeColMap.containsKey(primaryKey);
  370. if (!containsKey) {
  371. AdmExcelUtil.writeFile2Local(workbook, projectId, path, file.getOriginalFilename());
  372. return AdmExcelUtil.getImportFailResult("未找到识别码", "2", 0, ++failurNum);
  373. }
  374. primaryKey = AdmCommonConstant.codeColMap.get(primaryKey);
  375. /** 获取类型 */
  376. XSSFCell cell = row1.getCell(1);
  377. if (cell == null) {
  378. AdmExcelUtil.writeFile2Local(workbook, projectId, path, file.getOriginalFilename());
  379. return AdmExcelUtil.getImportFailResult("未填写设备类型", "2", 0, ++failurNum);
  380. }
  381. classCodeCloumn = cell.getStringCellValue().trim();
  382. if (!"ClassCode".equals(classCodeCloumn)) {
  383. AdmExcelUtil.writeFile2Local(workbook, projectId, path, file.getOriginalFilename());
  384. return AdmExcelUtil.getImportFailResult("缺少设备类型(ClassCode)", "2", 0, ++failurNum);
  385. }
  386. // 存取所有的信息点
  387. Map<Integer, String> infoMap = new HashMap<Integer, String>();
  388. short lastCellNum = row1.getLastCellNum();
  389. for (int i = 1; i <= lastCellNum; i++) {
  390. XSSFCell xssfCell = row1.getCell(i);
  391. if (xssfCell != null && xssfCell.getStringCellValue() != null) {
  392. infoMap.put(i, xssfCell.getStringCellValue().trim());
  393. }
  394. }
  395. // 录入数据获取
  396. int lastRowNum = sheet.getLastRowNum();
  397. boolean isClassCode = "classCode".equals(primaryKey);
  398. for (int i = 2; i <= lastRowNum; i++) {
  399. XSSFRow xssfRow = sheet.getRow(i);
  400. if (xssfRow == null) {
  401. continue;
  402. }
  403. String value = null;
  404. String classCode = null;
  405. ObjectNode equipment = null;
  406. List<ObjectNode> equipmentList = null;
  407. Map<String, Object> tempMap = new HashMap<String, Object>();
  408. lastCellNum = xssfRow.getLastCellNum();
  409. for (int j = 0; j <= lastCellNum; j++) {
  410. cell = xssfRow.getCell(j);
  411. if (cell == null || StrUtil.isBlank(cell.getRawValue())) {
  412. xssfRow.createCell(j);
  413. if (j == 0) {
  414. this.setColor(workbook, 252, 255, 77, xssfRow.getCell(j));
  415. } else {
  416. this.setColor(workbook, 77, 178, 255, xssfRow.getCell(j));
  417. }
  418. continue;
  419. }
  420. if (j == 0) {
  421. cell.setCellType(CellType.STRING);
  422. value = cell.getStringCellValue().trim();
  423. String codeType = isClassCode ? xssfRow.getCell(0).toString().trim() : xssfRow.getCell(1).toString().trim();
  424. // 获取类型定义
  425. ObjectTypeDefine define = this.getObjectTypeDefine(groupCode, projectId, codeType);
  426. if (define == null) {
  427. this.setColor(workbook, 56, 94, 15, cell);
  428. continue;
  429. }
  430. classCode = define.getCode();
  431. equipmentList = isClassCode ? this.getEquipmentList(groupCode, projectId, null, null, classCode)
  432. : this.getEquipmentList(groupCode, projectId, primaryKey, value, classCode);
  433. if (CollectionUtil.isEmpty(equipmentList)) {
  434. failurNum++;
  435. this.setColor(workbook, 153, 225, 77, cell);
  436. continue;
  437. }
  438. if (isClassCode) {
  439. equipment = equipmentList.get(0);
  440. } else {
  441. if (equipmentList.size() > 1) {
  442. failurNum++;
  443. this.setColor(workbook, 249, 203, 77, cell);
  444. } else {
  445. equipment = equipmentList.get(0);
  446. }
  447. }
  448. continue;
  449. }
  450. // 剩余列数据处理
  451. if (j != 1 && equipment != null && infoMap.containsKey(j)) {
  452. // 获取设备类的信息点
  453. List<ObjectInfoDefine> funidList = this.relationReportService.queryFunidList(groupCode, projectId, Sets.newHashSet(classCode), infoMap.get(j), "STATIC");
  454. if (CollectionUtil.isEmpty(funidList)) {
  455. failurNum++;
  456. this.setColor(workbook, 77, 178, 255, cell);
  457. continue;
  458. }
  459. ObjectInfoDefine define = funidList.get(0);
  460. if ("STRING".equalsIgnoreCase(define.getDataType())) {
  461. tempMap.put(define.getCode(), cell.getStringCellValue().trim());
  462. } else if ("INTEGER".equalsIgnoreCase(define.getDataType())) {
  463. try {
  464. if (!CellType.NUMERIC.equals(cell.getCellTypeEnum()) || StrUtil.isBlank(cell.getRawValue())) {
  465. failurNum++;
  466. this.setColor(workbook, 155, 227, 255, cell);
  467. continue;
  468. }
  469. cell.setCellType(CellType.STRING);
  470. tempMap.put(define.getCode(), cell.getStringCellValue().trim());
  471. } catch (Exception e) {
  472. failurNum++;
  473. this.setColor(workbook, 155, 227, 255, cell);
  474. continue;
  475. }
  476. } else if ("ENUM".equalsIgnoreCase(define.getDataType())) {
  477. if (CellType.NUMERIC.equals(cell.getCellTypeEnum()) && StrUtil.isNotBlank(cell.getRawValue())) {
  478. cell.setCellType(CellType.STRING);
  479. tempMap.put(define.getCode(), cell.getStringCellValue().trim());
  480. } else if (CellType.STRING.equals(cell.getCellTypeEnum()) && StrUtil.isNotBlank(cell.getRawValue())) {
  481. tempMap.put(define.getCode(), cell.getStringCellValue().trim());
  482. } else {
  483. failurNum++;
  484. this.setColor(workbook, 155, 227, 255, cell);
  485. continue;
  486. }
  487. } else if ("DATETIME".equalsIgnoreCase(define.getDataType())) {
  488. try {
  489. cell.setCellType(CellType.STRING);
  490. String trim = cell.getStringCellValue().trim();
  491. tempMap.put(define.getCode(), DateUtil.parseByPatterns(trim, "yyyyMMdd"));
  492. } catch (Exception e) {
  493. failurNum++;
  494. this.setColor(workbook, 155, 227, 255, cell);
  495. continue;
  496. }
  497. } else if ("DOUBLE".equalsIgnoreCase(define.getDataType())) {
  498. try {
  499. cell.setCellType(CellType.STRING);
  500. String trim = cell.getStringCellValue().trim();
  501. tempMap.put(define.getCode(), Double.parseDouble(trim));
  502. } catch (Exception e) {
  503. failurNum++;
  504. this.setColor(workbook, 155, 227, 255, cell);
  505. continue;
  506. }
  507. } else if ("BOOLEAN".equalsIgnoreCase(define.getDataType())) {
  508. cell.setCellType(CellType.STRING);
  509. String trim = cell.getStringCellValue() == null ? null : cell.getStringCellValue().trim();
  510. if ("1".equals(trim) || "0".equals(trim)) {
  511. tempMap.put(define.getCode(), Double.parseDouble(trim));
  512. } else {
  513. failurNum++;
  514. this.setColor(workbook, 155, 227, 255, cell);
  515. continue;
  516. }
  517. } else {
  518. failurNum++;
  519. this.setColor(workbook, 204, 99, 233, cell);
  520. continue;
  521. }
  522. }
  523. }
  524. if (isClassCode) {
  525. if (CollectionUtil.isNotEmpty(equipmentList) && tempMap.size() > 0) {
  526. for (ObjectNode equipNode : equipmentList) {
  527. int success = this.updateEquipInfo(tempMap, groupCode, projectId, equipNode.get("id").asText());
  528. if (success == 0) {
  529. failurNum++;
  530. } else {
  531. successCount++;
  532. }
  533. }
  534. }
  535. } else {
  536. if (StrUtil.isNotBlank(value) && equipment != null && tempMap.size() > 0) {
  537. int success = this.updateEquipInfo(tempMap, groupCode, projectId, equipment.get("id").asText());
  538. if (success == 0) {
  539. failurNum++;
  540. } else {
  541. successCount++;
  542. }
  543. }
  544. }
  545. }
  546. // 写入错误报告对比值颜色
  547. this.wirteReport(workbook, sheet);
  548. // 返回正确结果
  549. String fileName = AdmExcelUtil.writeFile2Local(workbook, projectId, path, file.getOriginalFilename());
  550. return AdmExcelUtil.getImportSuccessResult(fileName, successCount, failurNum);
  551. } catch (Exception e) {
  552. log.error("台账信息导入失败", e);
  553. failure = e.getMessage();
  554. } finally {
  555. if (fileOutputStream != null) {
  556. try {
  557. fileOutputStream.flush();
  558. fileOutputStream.close();
  559. } catch (IOException e) {}
  560. }
  561. if (workbook != null) {
  562. try {
  563. workbook.close();
  564. } catch (IOException e) {}
  565. }
  566. }
  567. return AdmResponse.failure(failure);
  568. }
  569. /**
  570. * 更新设备信息
  571. *
  572. * @param tempMap
  573. * @param groupCode
  574. * @param projectId
  575. * @param equipId
  576. * @return
  577. */
  578. private int updateEquipInfo(Map<String, Object> tempMap, String groupCode, String projectId, String equipId) {
  579. ObjectMapper objectMapper = SpringHelper.getBean(ObjectMapper.class);
  580. Set<String> keySet = tempMap.keySet();
  581. ObjectNode updateEquip = objectMapper.createObjectNode();
  582. for (String key : keySet) {
  583. Object obj = tempMap.get(key);
  584. updateEquip.put("id", equipId);
  585. if (obj instanceof Date) {
  586. updateEquip.putPOJO(key, obj);
  587. } else {
  588. updateEquip.put(key, obj.toString());
  589. }
  590. }
  591. ObjectDigital updateOne = DigitalObjectFacade.updateOne(groupCode, projectId, AdmCommonConstant.APP_ID, AdmCommonConstant.USER_ID, updateEquip);
  592. return updateOne == null ? 0 : 1;
  593. }
  594. /**
  595. * 获取类型定义
  596. *
  597. * @param code
  598. * @return
  599. */
  600. private ObjectTypeDefine getObjectTypeDefine(String groupCode, String projectId, String code) {
  601. QueryCriteria queryCriteria = new QueryCriteria();
  602. ObjectMapper objectMapper = SpringHelper.getBean(ObjectMapper.class);
  603. ObjectNode condition = objectMapper.createObjectNode();
  604. JSONObject arrays = new JSONObject();
  605. arrays.put("$ne", "system");
  606. condition.putPOJO("parentCode", arrays);
  607. condition.put("code", code);
  608. queryCriteria.setCriteria(condition);
  609. List<ObjectTypeDefine> objectType = DigitalDefineFacade.queryObjectType(groupCode, projectId, AdmCommonConstant.APP_ID, AdmCommonConstant.USER_ID, queryCriteria);
  610. return CollectionUtil.isEmpty(objectType) ? null : objectType.get(0);
  611. }
  612. /**
  613. * 获取设备
  614. *
  615. * @param groupCode
  616. * @param projectId
  617. * @param primaryKey
  618. * @param code
  619. * @param classCode
  620. * @return
  621. */
  622. private List<ObjectNode> getEquipmentList(String groupCode, String projectId, String primaryKey, String code, String classCode) {
  623. QueryCriteria queryCriteria = new QueryCriteria();
  624. ObjectMapper objectMapper = SpringHelper.getBean(ObjectMapper.class);
  625. ObjectNode condition = objectMapper.createObjectNode();
  626. if (StrUtil.isAllNotBlank(primaryKey, code)) {
  627. condition.put(primaryKey, code);
  628. }
  629. condition.put("classCode", classCode);
  630. queryCriteria.setCriteria(condition);
  631. List<ObjectNode> objectNodes = DigitalObjectFacade.query(groupCode, projectId, AdmCommonConstant.APP_ID, AdmCommonConstant.USER_ID, queryCriteria);
  632. return objectNodes;
  633. }
  634. /**
  635. * 设置填充色
  636. *
  637. * @param workbook
  638. * @param r
  639. * @param g
  640. * @param b
  641. * @param cell
  642. */
  643. private void setColor(XSSFWorkbook workbook, int r, int g, int b, XSSFCell cell) {
  644. XSSFCellStyle createCellStyle = workbook.createCellStyle();
  645. createCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  646. createCellStyle.setFillForegroundColor(new XSSFColor(new Color(r, g, b)));
  647. cell.setCellStyle(createCellStyle);
  648. }
  649. /**
  650. * 写入错误报告对比值颜色
  651. *
  652. * @param workbook
  653. * @param sheet
  654. */
  655. private void wirteReport(XSSFWorkbook workbook, XSSFSheet sheet) {
  656. XSSFRow row = sheet.getRow(1);
  657. short column = (short) (row.getLastCellNum() + 2);
  658. XSSFRow row4 = sheet.getRow(4);
  659. if (row4 == null) {
  660. row4 = sheet.createRow(4);
  661. }
  662. XSSFCell createCell = row4.createCell(column);
  663. createCell.setCellValue("导入失败,识别码超出范围( 本地编码,本地名称,数字化交付码 )");
  664. createCell = row4.createCell(column + 1);
  665. this.setColor(workbook,233,99,99,createCell);
  666. XSSFRow row5 = sheet.getRow(5);
  667. if (row5 == null) {
  668. row5 = sheet.createRow(5);
  669. }
  670. createCell = row5.createCell(column);
  671. createCell.setCellValue("导入失败,识别码为空");
  672. createCell = row5.createCell(column + 1);
  673. this.setColor(workbook,252,147,77,createCell);
  674. XSSFRow row6 = sheet.getRow(6);
  675. if (row6 == null) {
  676. row6 = sheet.createRow(6);
  677. }
  678. createCell = row6.createCell(column);
  679. createCell.setCellValue("导入失败,多个实例");
  680. createCell = row6.createCell(column + 1);
  681. this.setColor(workbook,249,203,77,createCell);
  682. XSSFRow row7 = sheet.getRow(7);
  683. if (row7 == null) {
  684. row7 = sheet.createRow(7);
  685. }
  686. createCell = row7.createCell(column);
  687. createCell.setCellValue("导入失败,设备不存在");
  688. createCell = row7.createCell(column + 1);
  689. this.setColor(workbook,153,225,77,createCell);
  690. XSSFRow row8 = sheet.getRow(8);
  691. if (row8 == null) {
  692. row8 = sheet.createRow(8);
  693. }
  694. createCell = row8.createCell(column);
  695. createCell.setCellValue("导入失败,识别码值为空");
  696. createCell = row8.createCell(column + 1);
  697. this.setColor(workbook,252,255,77,createCell);
  698. XSSFRow row9 = sheet.getRow(9);
  699. if (row9 == null) {
  700. row9 = sheet.createRow(9);
  701. }
  702. createCell = row9.createCell(column);
  703. createCell.setCellValue("导入失败,输入类型与字典定义不一致");
  704. createCell = row9.createCell(column + 1);
  705. this.setColor(workbook,155,227,255,createCell);
  706. XSSFRow row10 = sheet.getRow(10);
  707. if (row10 == null) {
  708. row10 = sheet.createRow(10);
  709. }
  710. createCell = row10.createCell(column);
  711. createCell.setCellValue("导入失败,未支持的输入类型");
  712. createCell = row10.createCell(column + 1);
  713. this.setColor(workbook,204,99,233,createCell);
  714. XSSFRow row11 = sheet.getRow(11);
  715. if (row11 == null) {
  716. row11 = sheet.createRow(11);
  717. }
  718. createCell = row11.createCell(column);
  719. createCell.setCellValue("导入失败,信息点不存在");
  720. createCell = row11.createCell(column + 1);
  721. this.setColor(workbook,77,178,255,createCell);
  722. XSSFRow row12 = sheet.getRow(12);
  723. if (row12 == null) {
  724. row12 = sheet.createRow(12);
  725. }
  726. createCell = row12.createCell(column);
  727. createCell.setCellValue("导入失败,设备类型不在字典定义中");
  728. createCell = row12.createCell(column + 1);
  729. this.setColor(workbook,56,94,15,createCell);
  730. }
  731. }