AdmBuildingController.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package com.persagy.proxy.adm.controller;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.util.StrUtil;
  4. import com.persagy.dmp.common.model.entity.BaseEntity;
  5. import com.persagy.proxy.adm.model.*;
  6. import com.persagy.proxy.adm.request.AdmCreateRequest;
  7. import com.persagy.proxy.adm.request.AdmQueryCriteria;
  8. import com.persagy.proxy.adm.request.AdmResponse;
  9. import com.persagy.proxy.adm.service.*;
  10. import com.persagy.proxy.adm.utils.AdmContextUtil;
  11. import com.persagy.proxy.adm.utils.ObjectNameUtil;
  12. import com.persagy.proxy.common.entity.DmpResult;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.web.bind.annotation.PostMapping;
  15. import org.springframework.web.bind.annotation.RequestBody;
  16. import org.springframework.web.bind.annotation.RequestMapping;
  17. import org.springframework.web.bind.annotation.RestController;
  18. import javax.ws.rs.QueryParam;
  19. import java.util.List;
  20. import java.util.Map;
  21. /**
  22. * ADM 建筑 API
  23. * @author Charlie Yu
  24. * @date 2021-08-16
  25. */
  26. @RestController
  27. @RequestMapping("/object/building")
  28. public class AdmBuildingController {
  29. @Autowired
  30. private IAdmBuildingService service;
  31. @Autowired
  32. private IAdmEquipmentService equipmentService;
  33. @Autowired
  34. private IAdmFloorService floorService;
  35. @Autowired
  36. private IAdmISpaceService iAdmISpaceService;
  37. @Autowired
  38. private IAdmSpaceService spaceService;
  39. /**
  40. * 创建
  41. * @param createRequest
  42. * @return
  43. * @throws Exception
  44. */
  45. @PostMapping("/create")
  46. public AdmResponse create(@RequestBody AdmCreateRequest<AdmBuilding> createRequest) throws Exception {
  47. List<AdmBuilding> vos = CollUtil.newArrayList(createRequest.getContent());
  48. vos.stream().forEach(admBuilding -> {
  49. if (StrUtil.isEmpty(admBuilding.getName())){
  50. admBuilding.setName(ObjectNameUtil.objectName("建筑-"));
  51. }
  52. });
  53. vos = service.doInsert(AdmContextUtil.toDmpContext(), AdmBuilding.class, vos);
  54. return AdmResponse.success(vos);
  55. }
  56. /**
  57. * 根据ID删除
  58. * @param vos
  59. * @return
  60. * @throws Exception
  61. */
  62. @PostMapping("/delete")
  63. public AdmResponse delete(@RequestBody List<AdmBuilding> vos) throws Exception {
  64. if(CollUtil.isEmpty(vos)){
  65. return AdmResponse.failure("参数不能为空");
  66. }
  67. //验证楼层 空间 设备 等信息是否存在,不存在关系可以删除
  68. Map<String, AdmBuilding> admBuildingMap = CollUtil.fieldValueMap(vos, BaseEntity.PROP_ID);
  69. String msg = "建筑下包含其他对象实例,请解除关系后删除";
  70. AdmQueryCriteria admQueryCriteria = new AdmQueryCriteria();
  71. StringBuilder stringBuilder = new StringBuilder();
  72. stringBuilder.append("buildingId in [");
  73. admBuildingMap.keySet().stream().forEach(buildingId -> {
  74. stringBuilder.append("'").append(buildingId).append("'").append(",");
  75. });
  76. String filters = StrUtil.sub(stringBuilder.toString(),0, -1);
  77. filters+= "]";
  78. admQueryCriteria.setFilters(filters);
  79. admQueryCriteria.setName(AdmFloor.OBJ_TYPE);
  80. AdmResponse admResponseF = floorService.doQuery(AdmContextUtil.toDmpContext(), admQueryCriteria, AdmFloor.class);
  81. if(DmpResult.SUCCESS.equals(admResponseF.getResult()) && CollUtil.isNotEmpty(admResponseF.getContent())){
  82. return AdmResponse.failure(msg);
  83. }
  84. admQueryCriteria.setName(AdmSpace.OBJ_TYPE);
  85. AdmResponse admResponseSpace = spaceService.doQuery(AdmContextUtil.toDmpContext(), admQueryCriteria, AdmSpace.class);
  86. if(admResponseSpace.getResult().equals(DmpResult.SUCCESS) && CollUtil.isNotEmpty(admResponseSpace.getContent())){
  87. return AdmResponse.failure(msg);
  88. }
  89. admQueryCriteria.setName(AdmISpace.OBJ_TYPE);
  90. AdmResponse admResponseISpace = iAdmISpaceService.doQuery(AdmContextUtil.toDmpContext(), admQueryCriteria, AdmISpace.class);
  91. if(admResponseISpace.getResult().equals(DmpResult.SUCCESS) && CollUtil.isNotEmpty(admResponseISpace.getContent())){
  92. return AdmResponse.failure(msg);
  93. }
  94. admQueryCriteria.setName(AdmEquipment.OBJ_TYPE);
  95. AdmResponse admResponseEuip = equipmentService.doQuery(AdmContextUtil.toDmpContext(), admQueryCriteria, AdmEquipment.class);
  96. if(admResponseEuip.getResult().equals(DmpResult.SUCCESS) && CollUtil.isNotEmpty(admResponseEuip.getContent())){
  97. return AdmResponse.failure(msg);
  98. }
  99. service.doDelete(AdmContextUtil.toDmpContext(), vos);
  100. return AdmResponse.success();
  101. }
  102. /**
  103. * 根据ID删除 - 单条删除
  104. * @param vos
  105. * @return
  106. * @throws Exception
  107. */
  108. @PostMapping("/single/delete")
  109. public AdmResponse deleteSingle(@RequestBody List<AdmBuilding> vos) throws Exception {
  110. delete(vos);
  111. return AdmResponse.success();
  112. }
  113. /**
  114. * 更新
  115. * @param createRequest
  116. * @return
  117. * @throws Exception
  118. */
  119. @PostMapping("/update")
  120. public AdmResponse update(@RequestBody AdmCreateRequest<AdmBuilding> createRequest) throws Exception {
  121. List<AdmBuilding> vos = CollUtil.newArrayList(createRequest.getContent());
  122. vos.forEach(admBuilding -> {
  123. if(StrUtil.isEmpty(admBuilding.getName())){
  124. admBuilding.setName(ObjectNameUtil.objectName("建筑-"));
  125. }
  126. });
  127. vos = service.doUpdate(AdmContextUtil.toDmpContext(), AdmBuilding.class, vos);
  128. return AdmResponse.success(vos);
  129. }
  130. /**
  131. * 查询
  132. * @param projectId
  133. * @param request
  134. * @return
  135. * @throws Exception
  136. */
  137. @PostMapping("/query")
  138. public AdmResponse query(@QueryParam("projectId") String projectId,
  139. @RequestBody AdmQueryCriteria request) throws Exception {
  140. request.setName(AdmBuilding.OBJ_TYPE);
  141. return service.doQuery(AdmContextUtil.toDmpContext(), request, AdmBuilding.class);
  142. }
  143. /**
  144. * 统计
  145. * @param projectId
  146. * @param request
  147. * @return
  148. * @throws Exception
  149. */
  150. @PostMapping("/count")
  151. public AdmResponse count(@QueryParam("projectId") String projectId,
  152. @RequestBody AdmQueryCriteria request) throws Exception {
  153. request.setName(AdmBuilding.OBJ_TYPE);
  154. request.setOnlyCount(true);
  155. return service.doQuery(AdmContextUtil.toDmpContext(), request, AdmBuilding.class);
  156. }
  157. }