CenterDataService.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package com.persagy.apm.diagnose.service;
  2. import java.util.*;
  3. import com.alibaba.excel.util.CollectionUtils;
  4. import com.alibaba.fastjson.JSON;
  5. import com.alibaba.fastjson.JSONArray;
  6. import com.alibaba.fastjson.JSONObject;
  7. import com.persagy.apm.diagnose.constant.EnumDataTimeType;
  8. import com.persagy.apm.diagnose.feign.client.CenterMiddlewareClient;
  9. import com.persagy.apm.diagnose.service.dto.ObjRelationDataDTO;
  10. import com.persagy.apm.diagnose.service.dto.TimeDataDTO;
  11. import com.persagy.apm.diagnose.utils.DataUtils;
  12. import com.persagy.apm.diagnose.utils.DateUtils;
  13. import lombok.extern.slf4j.Slf4j;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.beans.factory.annotation.Value;
  16. import org.springframework.stereotype.Service;
  17. import org.springframework.util.StringUtils;
  18. @Service
  19. @Slf4j
  20. public class CenterDataService{
  21. @Value(value = "${energy.eq.run.system.code}")
  22. private String systemCode;
  23. private static String SUCCESS = "success";
  24. @Autowired
  25. private CenterMiddlewareClient centerMiddlewareClient;
  26. public JSONArray queryObjListByObjType(String projectId, List<String> classCodeList) throws Exception {
  27. JSONArray resultArray = new JSONArray();
  28. Map<String, Object> requestMap = new HashMap<>();
  29. try {
  30. requestMap.put("projectId", projectId);
  31. requestMap.put("groupCode", systemCode);
  32. Map<String, Object> criteria = new HashMap<>();
  33. criteria.put("classCode", classCodeList);
  34. criteria.put("valid", 1);
  35. requestMap.put("criteria", criteria);
  36. String result = centerMiddlewareClient.queryObject(requestMap);
  37. resultArray = handleDataResult(result);
  38. } catch (Exception e) {
  39. log.error("获取类型对象列表数据错误,错误原因:" + e);
  40. log.error("查询数据中台queryObject参数:" + requestMap);
  41. throw e;
  42. }
  43. return resultArray;
  44. }
  45. private JSONArray handleDataResult(String result) throws Exception {
  46. if (org.springframework.util.StringUtils.isEmpty(result)) {
  47. throw new Exception("查询数据中台报错,无返回值");
  48. }
  49. JSONObject parseJsonObject = JSONObject.parseObject(result);
  50. if (null != parseJsonObject && !parseJsonObject.isEmpty()
  51. && SUCCESS.equals(parseJsonObject.getString("result"))) {
  52. if (parseJsonObject.getJSONArray("data") != null) {
  53. return parseJsonObject.getJSONArray("data");
  54. } else {
  55. return new JSONArray();
  56. }
  57. } else {
  58. throw new Exception("查询数据中台报错:");
  59. }
  60. }
  61. public List<JSONObject> queryObjListByObjId(String projectId, List<String> objIdList) throws Exception {
  62. List<JSONObject> list = new ArrayList<>();
  63. Map<String, Object> requestMap = new HashMap<>();
  64. try {
  65. requestMap.put("projectId", projectId);
  66. Map<String, Object> criteria = new HashMap<>();
  67. criteria.put("id", objIdList);
  68. criteria.put("valid", 1);
  69. requestMap.put("criteria", criteria);
  70. requestMap.put("groupCode", systemCode);
  71. String result = centerMiddlewareClient.queryObject(requestMap);
  72. JSONArray jsonArray = handleDataResult(result);
  73. List<JSONObject> jsonToList = JSON.parseArray(jsonArray.toJSONString(), JSONObject.class);
  74. if (!CollectionUtils.isEmpty(jsonToList)) {
  75. list.addAll(jsonToList);
  76. }
  77. } catch (Exception e) {
  78. log.error("获取对象列表数据错误,错误原因:" + e);
  79. log.error("查询数据中台queryObject参数:" + requestMap);
  80. throw e;
  81. }
  82. return list;
  83. }
  84. public List<TimeDataDTO> queryObjTimeDataList(String projectId, String objId, String infoCode, Date startTime,
  85. Date endTime, EnumDataTimeType dataTimeType) throws Exception {
  86. List<TimeDataDTO> list = new ArrayList<>();
  87. Map<String, Object> requestMap = new HashMap<>();
  88. try {
  89. requestMap.put("projectId", projectId);
  90. requestMap.put("groupCode", systemCode);
  91. Map<String, Object> criteria = new HashMap<>();
  92. criteria.put("period", dataTimeType.getCode());
  93. criteria.put("interpolation", false);
  94. criteria.put("code", infoCode);
  95. criteria.put("id", objId);
  96. Map<String, Object> receivetimeMap = new HashMap<>();
  97. receivetimeMap.put("$gte", DateUtils.date2Str(startTime, DateUtils.SDFSECOND));
  98. receivetimeMap.put("$lt", DateUtils.date2Str(endTime, DateUtils.SDFSECOND));
  99. criteria.put("receivetime", receivetimeMap);
  100. requestMap.put("criteria", criteria);
  101. String result = centerMiddlewareClient.queryPeriodData(requestMap);
  102. if (null != result) {
  103. JSONObject responseMap = JSONObject.parseObject(result);
  104. if (null != responseMap && !responseMap.isEmpty() && SUCCESS.equals(responseMap.get("Result"))) {
  105. List<Map> contentList = (List<Map>) responseMap.get("Content");
  106. if (CollectionUtils.isEmpty(contentList)) {
  107. return list;
  108. }
  109. for (Map map : contentList) {
  110. String data_time = (String) map.get("data_time");
  111. if (null == data_time || "".equals(data_time)) {
  112. continue;
  113. }
  114. TimeDataDTO dto = new TimeDataDTO();
  115. dto.setTime(data_time);
  116. dto.setData(DataUtils.parseDouble(map.get("data_value")));
  117. list.add(dto);
  118. }
  119. }
  120. }
  121. } catch (Exception e) {
  122. log.error("获取分精度数据错误,错误原因:" + e);
  123. log.error("查询数据中台queryObject参数:" + requestMap);
  124. throw e;
  125. }
  126. return list;
  127. }
  128. public List<JSONObject> queryObjRelationList(ObjRelationDataDTO dto) throws Exception {
  129. List<JSONObject> list = new ArrayList<>();
  130. if (null == dto || StringUtils.isEmpty(dto.getGraphCode()) || StringUtils.isEmpty(dto.getRelCode())
  131. || (StringUtils.isEmpty(dto.getObjFrom()) && StringUtils.isEmpty(dto.getObjTo()))) {
  132. return list;
  133. }
  134. Map<String, Object> requestMap = new HashMap<>();
  135. try {
  136. requestMap.put("projectId", dto.getProjectId());
  137. requestMap.put("groupCode", systemCode);
  138. Map<String, Object> criteria = new HashMap<>();
  139. Map<String, Object> relation = new HashMap<>();
  140. relation.put("graphCode", dto.getGraphCode());
  141. relation.put("relCode", dto.getRelCode());
  142. if (!StringUtils.isEmpty(dto.getObjFrom())) {
  143. relation.put("objFrom", dto.getObjFrom());
  144. criteria.put("relationTo", relation);
  145. } else {
  146. relation.put("objTo", dto.getObjTo());
  147. criteria.put("relationFrom", relation);
  148. }
  149. if (!StringUtils.isEmpty(dto.getClassCode())) {
  150. criteria.put("classCode", dto.getClassCode());
  151. }
  152. requestMap.put("criteria", criteria);
  153. String result = centerMiddlewareClient.queryObject(requestMap);
  154. JSONArray jsonArray = handleDataResult(result);
  155. List<JSONObject> jsonToList = JSON.parseArray(jsonArray.toJSONString(), JSONObject.class);
  156. if (!CollectionUtils.isEmpty(jsonToList)) {
  157. list.addAll(jsonToList);
  158. }
  159. } catch (Exception e) {
  160. log.error("获取关系对象数据错误,错误原因:" + e);
  161. log.error("查询数据中台queryObject参数:" + requestMap);
  162. throw e;
  163. }
  164. return list;
  165. }
  166. public JSONArray queryObjListByObjTypeAndName(String projectId, List<String> classCodeList, String keyWords)
  167. throws Exception {
  168. JSONArray resultArray = new JSONArray();
  169. Map<String, Object> requestMap = new HashMap<>();
  170. try {
  171. requestMap.put("projectId", projectId);
  172. requestMap.put("groupCode", systemCode);
  173. Map<String, Object> criteria = new HashMap<>();
  174. criteria.put("classCode", classCodeList);
  175. Map<String, Object> like = new HashMap<>();
  176. like.put("$like", "%" + keyWords + "%");
  177. criteria.put("localName", like);
  178. criteria.put("valid", 1);
  179. requestMap.put("criteria", criteria);
  180. String result = centerMiddlewareClient.queryObject(requestMap);
  181. resultArray = handleDataResult(result);
  182. } catch (Exception e) {
  183. log.error("获取类型对象列表数据错误,错误原因:" + e);
  184. log.error("查询数据中台queryObject参数:" + requestMap);
  185. throw e;
  186. }
  187. return resultArray;
  188. }
  189. }