EnergyPlatformServiceImpl.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. package com.ebei.screen.service.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.map.MapUtil;
  4. import cn.hutool.http.HttpRequest;
  5. import com.alibaba.fastjson.JSON;
  6. import com.alibaba.fastjson.JSONObject;
  7. import com.ebei.screen.common.constants.DmonDeviceCode;
  8. import com.ebei.screen.common.constants.EnergyPlatformConstants;
  9. import com.ebei.screen.common.response.ResponseBean;
  10. import com.ebei.screen.common.response.ResponseBuilder;
  11. import com.ebei.screen.common.util.EnergyUtils;
  12. import com.ebei.screen.common.util.Levi;
  13. import com.ebei.screen.common.util.LeviUtils;
  14. import com.ebei.screen.service.EnergyPlatformService;
  15. import lombok.extern.slf4j.Slf4j;
  16. import org.apache.commons.lang3.StringUtils;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.beans.factory.annotation.Value;
  19. import org.springframework.data.mongodb.core.MongoTemplate;
  20. import org.springframework.data.mongodb.core.query.Criteria;
  21. import org.springframework.data.mongodb.core.query.Query;
  22. import org.springframework.data.mongodb.core.query.Update;
  23. import org.springframework.stereotype.Service;
  24. import org.springframework.transaction.annotation.Transactional;
  25. import java.time.LocalDateTime;
  26. import java.time.temporal.ChronoUnit;
  27. import java.time.temporal.TemporalAdjusters;
  28. import java.util.*;
  29. import java.util.stream.Collectors;
  30. /**
  31. * 能耗平台大屏
  32. *
  33. * @author JCM
  34. * @description
  35. * @date 2021/1/15 15:36
  36. */
  37. @Slf4j
  38. @Service("energyPlatformService")
  39. @Transactional(rollbackFor = {Exception.class})
  40. public class EnergyPlatformServiceImpl implements EnergyPlatformService {
  41. @Autowired
  42. private MongoTemplate mongoTemplate;
  43. @Value("${energy.grpId}")
  44. private String grpIds;
  45. /**
  46. * 初始化数据
  47. */
  48. @Override
  49. public void initData() {
  50. initToken();
  51. initBoxList();
  52. initDmonGroups();
  53. initDmonByDmonGroups();
  54. initDmonValues();
  55. log.info("能耗数据全部初始化完毕");
  56. }
  57. /**
  58. * 初始化当前用户名token到mongo
  59. */
  60. @Override
  61. public void initToken() {
  62. // 如果刷新token失效 则重新走登录接口获取
  63. if (StringUtils.isBlank(EnergyUtils.doToken(EnergyUtils.refreshToken()))) {
  64. EnergyUtils.doToken(EnergyUtils.loginToken());
  65. EnergyUtils.doToken(EnergyUtils.refreshToken());
  66. }
  67. }
  68. /**
  69. * 初始化当前用户名盒子列表到mongo
  70. */
  71. @Override
  72. public void initBoxList() {
  73. String result = HttpRequest.get(EnergyPlatformConstants.apiClientBoxGrouped)
  74. .header("Authorization", EnergyUtils.token)
  75. .execute().body();
  76. if (StringUtils.isNotBlank(result)) {
  77. List<Map> mapList = LeviUtils.getJsonFieldMany(result, null, Map.class);
  78. Query query = new Query();
  79. query.addCriteria(Criteria.where("_id").is("boxList"));
  80. Update update = new Update();
  81. update.set("data", mapList);
  82. mongoTemplate.upsert(query, update, "energyAuth");
  83. List<String> boxIds = new ArrayList<>();
  84. if (CollUtil.isNotEmpty(mapList)) {
  85. mapList.forEach(x -> {
  86. List<Map> boxRegs = (List<Map>) x.get("boxRegs");
  87. if (CollUtil.isNotEmpty(boxRegs)) {
  88. boxRegs.forEach(y -> boxIds.add(y.get("boxUid").toString()));
  89. }
  90. });
  91. }
  92. EnergyUtils.boxIds = boxIds;
  93. }
  94. }
  95. /**
  96. * 初始化监控点分组
  97. */
  98. @Override
  99. public void initDmonGroups() {
  100. List<String> boxIds = EnergyUtils.boxIds;
  101. Map<String, List<String>> groupIds = new HashMap<>();
  102. boxIds.forEach(boxId -> {
  103. String result = HttpRequest.get(EnergyUtils.getUrl(EnergyPlatformConstants.v2BoxDmonGroups, boxId))
  104. .header("Authorization", EnergyUtils.token)
  105. .execute().body();
  106. List<Map> mapList = LeviUtils.getJsonFieldMany(result, null, Map.class);
  107. Query query = new Query();
  108. query.addCriteria(Criteria.where("_id").is("dmonGroups"));
  109. Update update = new Update();
  110. update.set(boxId, mapList);
  111. mongoTemplate.upsert(query, update, "energyAuth");
  112. if (StringUtils.isBlank(grpIds)) {
  113. List<String> ids = mapList.stream().map(x -> x.get("id").toString()).collect(Collectors.toList());
  114. groupIds.put(boxId, ids);
  115. } else {
  116. groupIds.put(boxId, Arrays.asList(grpIds.split(",")));
  117. }
  118. });
  119. EnergyUtils.groupIds = groupIds;
  120. }
  121. /**
  122. * 初始化配置的监控点
  123. */
  124. @Override
  125. public void initDmonByDmonGroups() {
  126. List<String> boxIds = EnergyUtils.boxIds;
  127. Map<String, List<String>> dmonIds = new HashMap<>(16);
  128. if (CollUtil.isNotEmpty(boxIds)) {
  129. boxIds.forEach(boxId -> {
  130. String result = HttpRequest.get(EnergyUtils.getUrl(EnergyPlatformConstants.v2BoxDmonGrouped, boxId))
  131. .header("Authorization", EnergyUtils.token)
  132. .execute().body();
  133. List<Map> mapList = LeviUtils.getJsonFieldMany(result, null, Map.class);
  134. if (CollUtil.isNotEmpty(mapList)) {
  135. JSONObject obj = new JSONObject();
  136. mapList.forEach(x -> obj.put(x.get("id").toString(), x));
  137. Query query = new Query();
  138. query.addCriteria(Criteria.where("_id").is("dmonList"));
  139. Update update = new Update();
  140. update.set(boxId, obj);
  141. mongoTemplate.upsert(query, update, "energyAuth");
  142. List<String> ids = new ArrayList<>();
  143. mapList.forEach(x -> {
  144. List<Map> list = (List<Map>) x.get("items");
  145. if (CollUtil.isNotEmpty(list)) {
  146. ids.addAll(list.stream().map(y -> y.get("id").toString()).collect(Collectors.toList()));
  147. }
  148. });
  149. dmonIds.put(boxId, ids);
  150. }
  151. });
  152. EnergyUtils.dmonIds = dmonIds;
  153. }
  154. }
  155. /**
  156. * 初始化配置监控点值
  157. */
  158. @Override
  159. public void initDmonValues() {
  160. List<String> boxIds = EnergyUtils.boxIds;
  161. Map<String, List<String>> dmonIds = EnergyUtils.dmonIds;
  162. if (CollUtil.isNotEmpty(boxIds)) {
  163. boxIds.forEach(boxId -> {
  164. String result = HttpRequest.post(EnergyUtils.getUrl(EnergyPlatformConstants.v2BoxDmonValueGet, boxId))
  165. .header("Authorization", EnergyUtils.token)
  166. .body(JSON.toJSONString(Levi.by("ids", dmonIds.get(boxId))))
  167. .execute().body();
  168. List<Map> mapList = LeviUtils.getJsonFieldMany(result, null, Map.class);
  169. // 存入原始数据用于统计
  170. Query query = new Query();
  171. query.addCriteria(Criteria.where("_id").is(boxId + "_" + LocalDateTime.now().getYear() + "_" + LocalDateTime.now().getMonthValue()));
  172. Update update = new Update();
  173. Map map = EnergyUtils.setDataField(mapList, boxId, "boxDmonLists");
  174. update.set("data", map.get("data"));
  175. for (int i = 1; i <= 6; i++) {
  176. update.set("zj" + i, map.get("zj" + i));
  177. }
  178. update.set("all", map.get("all"));
  179. mongoTemplate.upsert(query, update, "boxDmonLists");
  180. });
  181. }
  182. }
  183. /**
  184. * 获取能耗大屏除预警外全部数据
  185. *
  186. * @return
  187. */
  188. @Override
  189. public ResponseBean getAllInOne(String boxId) {
  190. LocalDateTime now = LocalDateTime.now();
  191. int year = now.getYear();
  192. int month = now.getMonthValue();
  193. int day = now.getDayOfMonth();
  194. int hour = now.getHour();
  195. LocalDateTime lastYearNow = now.minus(1, ChronoUnit.YEARS);
  196. int lastYear = lastYearNow.getYear();
  197. int lastMonth = lastYearNow.getMonthValue();
  198. int lastDay = lastYearNow.getDayOfMonth();
  199. int lastHour = lastYearNow.getHour();
  200. LocalDateTime lastMonthNow = now.minus(1, ChronoUnit.MONTHS);
  201. int dayOfMonth = lastMonthNow.with(TemporalAdjusters.lastDayOfMonth()).getDayOfMonth();
  202. // 封装本月用电构成
  203. Map thisBoxDmonMap = mongoTemplate.findById(boxId + "_" + year + "_" + month, Map.class, "boxDmonLists");
  204. Map thisBoxDmonMapL = mongoTemplate.findById(boxId + "_" + ((month) == 1 ? lastYear : year) + "_" + ((month == 1) ? 12 : month - 1), Map.class, "boxDmonLists");
  205. boolean flag = MapUtil.isNotEmpty(thisBoxDmonMap);
  206. boolean flagL = MapUtil.isNotEmpty(thisBoxDmonMapL);
  207. Double zj1 = flag ? Double.parseDouble(thisBoxDmonMap.get("zj1").toString()) : 0.0;
  208. Double zj2 = flag ? Double.parseDouble(thisBoxDmonMap.get("zj2").toString()) : 0.0;
  209. Double zj3 = flag ? Double.parseDouble(thisBoxDmonMap.get("zj3").toString()) : 0.0;
  210. Double zj4 = flag ? Double.parseDouble(thisBoxDmonMap.get("zj4").toString()) : 0.0;
  211. Double zj5 = flag ? Double.parseDouble(thisBoxDmonMap.get("zj5").toString()) : 0.0;
  212. Double zj6 = flag ? Double.parseDouble(thisBoxDmonMap.get("zj6").toString()) : 0.0;
  213. Double all = flag ? Double.parseDouble(thisBoxDmonMap.get("all").toString()) : 0.0;
  214. List<Map> byydgc = Arrays.asList(Levi.by("name", DmonDeviceCode.ONE.getName()).set("value", zj1).set("percent", EnergyUtils.getPercent(zj1, all)),
  215. Levi.by("name", DmonDeviceCode.TWO.getName()).set("value", zj2).set("percent", EnergyUtils.getPercent(zj2, all)),
  216. Levi.by("name", DmonDeviceCode.THREE.getName()).set("value", zj3).set("percent", EnergyUtils.getPercent(zj3, all)),
  217. Levi.by("name", DmonDeviceCode.FOUR.getName()).set("value", zj4).set("percent", EnergyUtils.getPercent(zj4, all)),
  218. Levi.by("name", DmonDeviceCode.FIVE.getName()).set("value", zj5).set("percent", EnergyUtils.getPercent(zj5, all)),
  219. Levi.by("name", DmonDeviceCode.SIX.getName()).set("value", zj6).set("percent", EnergyUtils.getPercent(zj6, all)),
  220. Levi.by("name", DmonDeviceCode.ALL.getName()).set("value", all));
  221. double ndlj = 0.0, by = 0.0, jrss = 0.0, first = 0.0, second = 0.0, third = 0.0;
  222. double ndljL = 0.0, byL = 0.0, jrssL = 0.0, firstL = 0.0, secondL = 0.0, thirdL = 0.0;
  223. String ndljP = "0%", byP = "0%", jrssP = "0%", firstP = "0%", secondP = "0%", thirdP = "0%";
  224. String firstName = "", secondName = "", thirdName = "";
  225. // 封装年度用量环比
  226. List<Map> monthList = new ArrayList<>();
  227. for (int i = 1; i <= 12; i++) {
  228. // 遍历今年和去年每个月的数据
  229. Map map = mongoTemplate.findById(boxId + "_" + year + "_" + i, Map.class, "boxDmonLists");
  230. Map lastMap = mongoTemplate.findById(boxId + "_" + lastYear + "_" + i, Map.class, "boxDmonLists");
  231. Double aDouble = MapUtil.isNotEmpty(map) ? Double.parseDouble(map.get("all").toString()) : 0.0;
  232. Double aDoubleL = MapUtil.isNotEmpty(lastMap) ? Double.parseDouble(lastMap.get("all").toString()) : 0.0;
  233. monthList.add(Levi.by("year", year).set("month", i).set("value", aDouble).set("yearL", lastYear).set("valueL", aDoubleL));
  234. // 累计今年和去年总数
  235. ndlj = Double.sum(ndlj, aDouble);
  236. ndljL = Double.sum(ndljL, aDoubleL);
  237. }
  238. // 计算年度累计总用量百分比
  239. ndljP = EnergyUtils.getPercent(ndlj, ndljL);
  240. // 计算本月总用量
  241. by = Double.valueOf(monthList.get(month - 1).get("value").toString());
  242. byL = Double.valueOf(monthList.get(month - 1).get("valueL").toString());
  243. byP = EnergyUtils.getPercent(by, byL);
  244. // 计算今日实时用量
  245. jrss = EnergyUtils.getDayInfo("all", thisBoxDmonMap, day);
  246. jrssL = EnergyUtils.getDayInfo("all", thisBoxDmonMapL, day);
  247. jrssP = EnergyUtils.getPercent(jrss, jrssL);
  248. // 用量第一、第二、第三
  249. Double d1 = EnergyUtils.getDayInfo("zj1", thisBoxDmonMap, day);
  250. Double d2 = EnergyUtils.getDayInfo("zj2", thisBoxDmonMap, day);
  251. Double d3 = EnergyUtils.getDayInfo("zj3", thisBoxDmonMap, day);
  252. Double d4 = EnergyUtils.getDayInfo("zj4", thisBoxDmonMap, day);
  253. Double d5 = EnergyUtils.getDayInfo("zj5", thisBoxDmonMap, day);
  254. Double d6 = EnergyUtils.getDayInfo("zj6", thisBoxDmonMap, day);
  255. Map<Double, List<String>> dmonMaps = new HashMap(16);
  256. dmonMaps.put(d1, EnergyUtils.addSameList(dmonMaps.get(d1), DmonDeviceCode.ONE.getName()));
  257. dmonMaps.put(d2, EnergyUtils.addSameList(dmonMaps.get(d2), DmonDeviceCode.TWO.getName()));
  258. dmonMaps.put(d3, EnergyUtils.addSameList(dmonMaps.get(d3), DmonDeviceCode.THREE.getName()));
  259. dmonMaps.put(d4, EnergyUtils.addSameList(dmonMaps.get(d4), DmonDeviceCode.FOUR.getName()));
  260. dmonMaps.put(d5, EnergyUtils.addSameList(dmonMaps.get(d5), DmonDeviceCode.FIVE.getName()));
  261. dmonMaps.put(d6, EnergyUtils.addSameList(dmonMaps.get(d6), DmonDeviceCode.SIX.getName()));
  262. List<Double> doubles = Arrays.asList(d1, d2, d3, d4, d5, d6);
  263. Collections.sort(doubles);
  264. first = doubles.get(doubles.size() - 1);
  265. firstName = String.join(",", dmonMaps.get(first));
  266. firstL = EnergyUtils.getDayInfo(firstName.replace("主机", "zj"), (day == 1 ? thisBoxDmonMapL : thisBoxDmonMap), (day == 1 ? dayOfMonth : day - 1));
  267. firstP = EnergyUtils.getPercent(first, firstL);
  268. second = doubles.get(doubles.size() - 2);
  269. secondName = String.join(",", dmonMaps.get(second));
  270. secondL = EnergyUtils.getDayInfo(secondName.replace("主机", "zj"), (day == 1 ? thisBoxDmonMapL : thisBoxDmonMap), (day == 1 ? dayOfMonth : day - 1));
  271. secondP = EnergyUtils.getPercent(second, secondL);
  272. third = doubles.get(doubles.size() - 3);
  273. thirdName = String.join(",", dmonMaps.get(third));
  274. thirdL = EnergyUtils.getDayInfo(thirdName.replace("主机", "zj"), (day == 1 ? thisBoxDmonMapL : thisBoxDmonMap), (day == 1 ? dayOfMonth : day - 1));
  275. thirdP = EnergyUtils.getPercent(third, thirdL);
  276. List<Map> byhxzb = Arrays.asList(Levi.by("name", "年度累计总用量").set("value1", ndlj).set("value2", ndljP),
  277. Levi.by("name", "本月总用量").set("value1", by).set("value2", byP),
  278. Levi.by("name", "今日实时用量").set("value1", jrss).set("value2", jrssP),
  279. Levi.by("name", "业态用量第一:" + firstName).set("value1", first).set("value2", firstP),
  280. Levi.by("name", "业态用量第二:" + secondName).set("value1", second).set("value2", secondP),
  281. Levi.by("name", "业态用量第三:" + thirdName).set("value1", third).set("value2", thirdP)
  282. );
  283. // 计算本日用量环比
  284. List<Map> dayHbList = new ArrayList<>();
  285. List<Map> dataHb = new ArrayList<>();
  286. List<Map> dataHbL = new ArrayList<>();
  287. if (thisBoxDmonMap.get("data") != null) {
  288. List<Map> x1 = (List<Map>) thisBoxDmonMap.get("data");
  289. if (x1.get(day - 1) != null) {
  290. dataHb = flag ? (List<Map>) x1.get(day - 1).get("data") : null;
  291. }
  292. }
  293. if ((day == 1 ? (flagL ? thisBoxDmonMapL.get("data") : null) : (flag ? thisBoxDmonMap.get("data") : null)) != null) {
  294. List<Map> x2 = (List<Map>) (day == 1 ? thisBoxDmonMapL : thisBoxDmonMap).get("data");
  295. if (x2.get(day == 1 ? dayOfMonth - 1 : day - 2) != null) {
  296. dataHbL = flag ? (List<Map>) x2.get(day == 1 ? dayOfMonth - 1 : day - 2).get("data") : null;
  297. }
  298. }
  299. if (CollUtil.isNotEmpty(dataHb) && CollUtil.isNotEmpty(dataHbL)) {
  300. for (int i = 0; i < 8; i++) {
  301. dayHbList.add(Levi.by("time", String.format("%02d", i * 3) + ":00").set("today", dataHb.get(i * 3).get("all") == null ? 0.0 : dataHb.get(i * 3).get("all")).set("lastDay", dataHbL.get(i * 3).get("all") == null ? 0.0 : dataHbL.get(i * 3).get("all")));
  302. }
  303. dayHbList.add(Levi.by("time", "23:59").set("today", dataHb.get(23).get("all") == null ? 0.0 : dataHb.get(23).get("all")).set("lastDay", dataHbL.get(23).get("all") == null ? 0.0 : dataHbL.get(23).get("all")));
  304. } else {
  305. for (int i = 0; i < 8; i++) {
  306. dayHbList.add(Levi.by("time", String.format("%02d", i * 3) + ":00").set("today", 0.0).set("lastDay", 0.0));
  307. }
  308. dayHbList.add(Levi.by("time", "23:59").set("today", 0.0).set("lastDay", 0.0));
  309. }
  310. // 计算月度用量环比
  311. List<Map> monthHbList = new ArrayList<>();
  312. for (int i = 1; i <= 31; i++) {
  313. double x1 = 0.0;
  314. double x2 = 0.0;
  315. if (flag) {
  316. List<Map> l1 = (List<Map>) thisBoxDmonMap.get("data");
  317. if (CollUtil.isNotEmpty(l1)) {
  318. x1 = l1.get(i - 1).get("all") == null ? 0.0 : Double.valueOf((l1.get(i - 1) == null ? "0" : l1.get(i - 1).get("all").toString()));
  319. }
  320. }
  321. if (flagL) {
  322. List<Map> l2 = (List<Map>) thisBoxDmonMapL.get("data");
  323. if (CollUtil.isNotEmpty(l2)) {
  324. x2 = l2.get(i - 1).get("all") == null ? 0.0 : Double.valueOf((l2.get(i - 1) == null ? "0" : l2.get(i - 1).get("all").toString()));
  325. }
  326. }
  327. monthHbList.add(Levi.by("day", i + "号").set("this", x1).set("last", x2));
  328. }
  329. // 封装所有数据
  330. return ResponseBuilder.ok(Levi.by("boxId", boxId).set("byydgc", byydgc).set("byhxzb", byhxzb).set("ndylhb", monthList).set("brylhb", dayHbList).set("ydylhb", monthHbList));
  331. }
  332. }