XimoAddOwnerTransactionIotAdapt.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright 2017-2020 吴学文 and java110 team.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.java110.job.adapt.ximoIot;
  17. import com.alibaba.fastjson.JSONArray;
  18. import com.alibaba.fastjson.JSONObject;
  19. import com.java110.dto.RoomDto;
  20. import com.java110.dto.file.FileDto;
  21. import com.java110.dto.file.FileRelDto;
  22. import com.java110.dto.machine.MachineDto;
  23. import com.java110.entity.order.Business;
  24. import com.java110.intf.common.IFileInnerServiceSMO;
  25. import com.java110.intf.common.IFileRelInnerServiceSMO;
  26. import com.java110.intf.common.IMachineInnerServiceSMO;
  27. import com.java110.intf.community.IRoomInnerServiceSMO;
  28. import com.java110.job.adapt.DatabusAdaptImpl;
  29. import com.java110.job.adapt.ximoIot.asyn.IXimoMachineAsyn;
  30. import com.java110.po.owner.OwnerPo;
  31. import com.java110.utils.util.Assert;
  32. import com.java110.utils.util.BeanConvertUtil;
  33. import org.springframework.beans.factory.annotation.Autowired;
  34. import org.springframework.stereotype.Component;
  35. import org.springframework.util.LinkedMultiValueMap;
  36. import org.springframework.util.MultiValueMap;
  37. import java.util.ArrayList;
  38. import java.util.List;
  39. /**
  40. * HC iot 添加业主同步细末
  41. *
  42. * @desc add by 吴学文 18:58
  43. */
  44. @Component(value = "ximoAddOwnerTransactionIotAdapt")
  45. public class XimoAddOwnerTransactionIotAdapt extends DatabusAdaptImpl {
  46. @Autowired
  47. private IXimoMachineAsyn ximoMachineAsynImpl;
  48. @Autowired
  49. IMachineInnerServiceSMO machineInnerServiceSMOImpl;
  50. @Autowired
  51. private IRoomInnerServiceSMO roomInnerServiceSMOImpl;
  52. @Autowired
  53. private IFileRelInnerServiceSMO fileRelInnerServiceSMOImpl;
  54. @Autowired
  55. private IFileInnerServiceSMO fileInnerServiceSMOImpl;
  56. /**
  57. * accessToken={access_token}
  58. * &extCommunityUuid=01000
  59. * &extCommunityId=1
  60. * &devSn=111111111
  61. * &name=设备名称
  62. * &positionType=0
  63. * &positionUuid=1
  64. *
  65. * @param business 当前处理业务
  66. * @param businesses 所有业务信息
  67. */
  68. @Override
  69. public void execute(Business business, List<Business> businesses) {
  70. JSONObject data = business.getData();
  71. if (data.containsKey(OwnerPo.class.getSimpleName())) {
  72. Object bObj = data.get(OwnerPo.class.getSimpleName());
  73. JSONArray businessMachines = null;
  74. if (bObj instanceof JSONObject) {
  75. businessMachines = new JSONArray();
  76. businessMachines.add(bObj);
  77. } else if (bObj instanceof List) {
  78. businessMachines = JSONArray.parseArray(JSONObject.toJSONString(bObj));
  79. } else {
  80. businessMachines = (JSONArray) bObj;
  81. }
  82. for (int bOwnerIndex = 0; bOwnerIndex < businessMachines.size(); bOwnerIndex++) {
  83. JSONObject businessOwner = businessMachines.getJSONObject(bOwnerIndex);
  84. doSendMachine(business, businessOwner);
  85. }
  86. }
  87. }
  88. private void doSendMachine(Business business, JSONObject businessOwner) {
  89. OwnerPo ownerPo = BeanConvertUtil.covertBean(businessOwner, OwnerPo.class);
  90. FileRelDto fileRelDto = new FileRelDto();
  91. fileRelDto.setObjId(ownerPo.getMemberId());
  92. fileRelDto.setRelTypeCd("10000");
  93. List<FileRelDto> fileRelDtos = fileRelInnerServiceSMOImpl.queryFileRels(fileRelDto);
  94. if (fileRelDtos == null || fileRelDtos.size() != 1) {
  95. return;
  96. }
  97. FileDto fileDto = new FileDto();
  98. fileDto.setFileId(fileRelDtos.get(0).getFileSaveName());
  99. fileDto.setFileSaveName(fileRelDtos.get(0).getFileSaveName());
  100. fileDto.setCommunityId(ownerPo.getCommunityId());
  101. List<FileDto> fileDtos = fileInnerServiceSMOImpl.queryFiles(fileDto);
  102. if (fileDtos == null || fileDtos.size() != 1) {
  103. return;
  104. }
  105. RoomDto roomDto = new RoomDto();
  106. roomDto.setOwnerId(ownerPo.getOwnerId());
  107. //这种情况说明 业主已经删掉了 需要查询状态为 1 的数据
  108. List<RoomDto> rooms = roomInnerServiceSMOImpl.queryRoomsByOwner(roomDto);
  109. //拿到小区ID
  110. String communityId = ownerPo.getCommunityId();
  111. //根据小区ID查询现有设备
  112. MachineDto machineDto = new MachineDto();
  113. machineDto.setCommunityId(communityId);
  114. //String[] locationObjIds = new String[]{communityId};
  115. List<String> locationObjIds = new ArrayList<>();
  116. locationObjIds.add(communityId);
  117. for (RoomDto tRoomDto : rooms) {
  118. locationObjIds.add(tRoomDto.getUnitId());
  119. locationObjIds.add(tRoomDto.getRoomId());
  120. }
  121. machineDto.setLocationObjIds(locationObjIds.toArray(new String[locationObjIds.size()]));
  122. List<MachineDto> machineDtos = machineInnerServiceSMOImpl.queryMachines(machineDto);
  123. Assert.listOnlyOne(machineDtos, "未找到设备");
  124. for (MachineDto tmpMachineDto : machineDtos) {
  125. if (!"9999".equals(tmpMachineDto.getMachineTypeCd())) {
  126. continue;
  127. }
  128. MultiValueMap<String, Object> postParameters = new LinkedMultiValueMap<>();
  129. postParameters.add("extCommunityUuid", ownerPo.getCommunityId());
  130. postParameters.add("addAuthorizationDevSn", tmpMachineDto.getMachineCode());
  131. postParameters.add("uuid", ownerPo.getMemberId());
  132. postParameters.add("name", ownerPo.getName());
  133. postParameters.add("faceFileBase64Array", fileDtos.get(0).getContext());
  134. ximoMachineAsynImpl.sendOwner(postParameters);
  135. }
  136. }
  137. }