|
@@ -1,12 +1,35 @@
|
|
|
package com.ebei.screen.service.impl;
|
|
|
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.map.MapUtil;
|
|
|
+import cn.hutool.http.HttpRequest;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.ebei.screen.common.constants.DmonDeviceCode;
|
|
|
+import com.ebei.screen.common.constants.EnergyPlatformConstants;
|
|
|
+import com.ebei.screen.common.response.ResponseBean;
|
|
|
+import com.ebei.screen.common.response.ResponseBuilder;
|
|
|
+import com.ebei.screen.common.util.EnergyUtils;
|
|
|
+import com.ebei.screen.common.util.Levi;
|
|
|
+import com.ebei.screen.common.util.LeviUtils;
|
|
|
import com.ebei.screen.service.EnergyPlatformService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.data.mongodb.core.MongoTemplate;
|
|
|
+import org.springframework.data.mongodb.core.query.Criteria;
|
|
|
+import org.springframework.data.mongodb.core.query.Query;
|
|
|
+import org.springframework.data.mongodb.core.query.Update;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.temporal.ChronoUnit;
|
|
|
+import java.time.temporal.TemporalAdjusters;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
/**
|
|
|
* 能耗平台大屏
|
|
|
*
|
|
@@ -22,8 +45,278 @@ public class EnergyPlatformServiceImpl implements EnergyPlatformService {
|
|
|
@Autowired
|
|
|
private MongoTemplate mongoTemplate;
|
|
|
|
|
|
+ @Value("${energy.grpId}")
|
|
|
+ private String grpIds;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 初始化数据
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void initData() {
|
|
|
+ initToken();
|
|
|
+ initBoxList();
|
|
|
+ initDmonGroups();
|
|
|
+ initDmonByDmonGroups();
|
|
|
+ initDmonValues();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 初始化当前用户名token到mongo
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void initToken() {
|
|
|
+ // 如果刷新token失效 则重新走登录接口获取
|
|
|
+ if (StringUtils.isBlank(EnergyUtils.doToken(EnergyUtils.refreshToken()))) {
|
|
|
+ EnergyUtils.doToken(EnergyUtils.loginToken());
|
|
|
+ EnergyUtils.doToken(EnergyUtils.refreshToken());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 初始化当前用户名盒子列表到mongo
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void initBoxList() {
|
|
|
+ String result = HttpRequest.get(EnergyPlatformConstants.apiClientBoxGrouped)
|
|
|
+ .header("Authorization", EnergyUtils.token)
|
|
|
+ .execute().body();
|
|
|
+ if (StringUtils.isNotBlank(result)) {
|
|
|
+ List<Map> mapList = LeviUtils.getJsonFieldMany(result, null, Map.class);
|
|
|
+ Query query = new Query();
|
|
|
+ query.addCriteria(Criteria.where("_id").is("boxList"));
|
|
|
+ Update update = new Update();
|
|
|
+ update.set("data", mapList);
|
|
|
+ mongoTemplate.upsert(query, update, "energyAuth");
|
|
|
+ List<String> boxIds = new ArrayList<>();
|
|
|
+ if (CollUtil.isNotEmpty(mapList)) {
|
|
|
+ mapList.forEach(x -> {
|
|
|
+ List<Map> boxRegs = (List<Map>) x.get("boxRegs");
|
|
|
+ if (CollUtil.isNotEmpty(boxRegs)) {
|
|
|
+ boxRegs.forEach(y -> boxIds.add(y.get("boxUid").toString()));
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ EnergyUtils.boxIds = boxIds;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 初始化监控点分组
|
|
|
+ */
|
|
|
@Override
|
|
|
- public void apiClientBoxGrouped() {
|
|
|
+ public void initDmonGroups() {
|
|
|
+ List<String> boxIds = EnergyUtils.boxIds;
|
|
|
+ Map<String, List<String>> groupIds = new HashMap<>();
|
|
|
+ boxIds.forEach(boxId -> {
|
|
|
+ String result = HttpRequest.get(EnergyUtils.getUrl(EnergyPlatformConstants.v2BoxDmonGroups, boxId))
|
|
|
+ .header("Authorization", EnergyUtils.token)
|
|
|
+ .execute().body();
|
|
|
+ List<Map> mapList = LeviUtils.getJsonFieldMany(result, null, Map.class);
|
|
|
+ Query query = new Query();
|
|
|
+ query.addCriteria(Criteria.where("_id").is("dmonGroups"));
|
|
|
+ Update update = new Update();
|
|
|
+ update.set(boxId, mapList);
|
|
|
+ mongoTemplate.upsert(query, update, "energyAuth");
|
|
|
+ if (StringUtils.isBlank(grpIds)) {
|
|
|
+ List<String> ids = mapList.stream().map(x -> x.get("id").toString()).collect(Collectors.toList());
|
|
|
+ groupIds.put(boxId, ids);
|
|
|
+ } else {
|
|
|
+ groupIds.put(boxId, Arrays.asList(grpIds.split(",")));
|
|
|
+ }
|
|
|
+ });
|
|
|
+ EnergyUtils.groupIds = groupIds;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 初始化配置的监控点
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void initDmonByDmonGroups() {
|
|
|
+ List<String> boxIds = EnergyUtils.boxIds;
|
|
|
+ Map<String, List<String>> dmonIds = new HashMap<>(16);
|
|
|
+ if (CollUtil.isNotEmpty(boxIds)) {
|
|
|
+ boxIds.forEach(boxId -> {
|
|
|
+ String result = HttpRequest.get(EnergyUtils.getUrl(EnergyPlatformConstants.v2BoxDmonGrouped, boxId))
|
|
|
+ .header("Authorization", EnergyUtils.token)
|
|
|
+ .execute().body();
|
|
|
+ List<Map> mapList = LeviUtils.getJsonFieldMany(result, null, Map.class);
|
|
|
+ if (CollUtil.isNotEmpty(mapList)) {
|
|
|
+ JSONObject obj = new JSONObject();
|
|
|
+ mapList.forEach(x -> obj.put(x.get("id").toString(), x));
|
|
|
+ Query query = new Query();
|
|
|
+ query.addCriteria(Criteria.where("_id").is("dmonList"));
|
|
|
+ Update update = new Update();
|
|
|
+ update.set(boxId, obj);
|
|
|
+ mongoTemplate.upsert(query, update, "energyAuth");
|
|
|
+ List<String> ids = new ArrayList<>();
|
|
|
+ mapList.forEach(x -> {
|
|
|
+ List<Map> list = (List<Map>) x.get("items");
|
|
|
+ if (CollUtil.isNotEmpty(list)) {
|
|
|
+ ids.addAll(list.stream().map(y -> y.get("id").toString()).collect(Collectors.toList()));
|
|
|
+ }
|
|
|
+ });
|
|
|
+ dmonIds.put(boxId, ids);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ EnergyUtils.dmonIds = dmonIds;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 初始化配置监控点值
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void initDmonValues() {
|
|
|
+ List<String> boxIds = EnergyUtils.boxIds;
|
|
|
+ Map<String, List<String>> dmonIds = EnergyUtils.dmonIds;
|
|
|
+ if (CollUtil.isNotEmpty(boxIds)) {
|
|
|
+ boxIds.forEach(boxId -> {
|
|
|
+ String result = HttpRequest.post(EnergyUtils.getUrl(EnergyPlatformConstants.v2BoxDmonValueGet, boxId))
|
|
|
+ .header("Authorization", EnergyUtils.token)
|
|
|
+ .body(JSON.toJSONString(Levi.by("ids", dmonIds.get(boxId))))
|
|
|
+ .execute().body();
|
|
|
+ List<Map> mapList = LeviUtils.getJsonFieldMany(result, null, Map.class);
|
|
|
+ // 存入原始数据用于统计
|
|
|
+ Query query = new Query();
|
|
|
+ query.addCriteria(Criteria.where("_id").is(boxId + "_" + LocalDateTime.now().getYear() + "_" + LocalDateTime.now().getMonthValue()));
|
|
|
+ Update update = new Update();
|
|
|
+ Map map = EnergyUtils.setDataField(mapList, boxId, "boxDmonLists");
|
|
|
+ update.set("data", map.get("data"));
|
|
|
+ for (int i = 1; i <= 6; i++) {
|
|
|
+ update.set("zj" + i, map.get("zj" + i));
|
|
|
+ }
|
|
|
+ update.set("all", map.get("all"));
|
|
|
+ mongoTemplate.upsert(query, update, "boxDmonLists");
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取能耗大屏除预警外全部数据
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public ResponseBean getAllInOne() {
|
|
|
+ List<Map> results = new ArrayList<>();
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
+ int year = now.getYear();
|
|
|
+ int month = now.getMonthValue();
|
|
|
+ int day = now.getDayOfMonth();
|
|
|
+ int hour = now.getHour();
|
|
|
+ LocalDateTime lastYearNow = now.minus(1, ChronoUnit.YEARS);
|
|
|
+ int lastYear = lastYearNow.getYear();
|
|
|
+ int lastMonth = lastYearNow.getMonthValue();
|
|
|
+ int lastDay = lastYearNow.getDayOfMonth();
|
|
|
+ int lastHour = lastYearNow.getHour();
|
|
|
+ LocalDateTime lastMonthNow = now.minus(1, ChronoUnit.MONTHS);
|
|
|
+ int dayOfMonth = lastMonthNow.with(TemporalAdjusters.lastDayOfMonth()).getDayOfMonth();
|
|
|
+ // 获取所有盒子id
|
|
|
+ List<String> boxIds = EnergyUtils.boxIds;
|
|
|
+ // 封装本月用电构成
|
|
|
+
|
|
|
+ boxIds.forEach(boxId -> {
|
|
|
+ Map thisBoxDmonMap = mongoTemplate.findById(boxId + "_" + year + "_" + month, Map.class, "boxDmonLists");
|
|
|
+ Map thisBoxDmonMapL = mongoTemplate.findById(boxId + "_" + ((month) == 1 ? lastYear : year) + "_" + ((month == 1) ? 12 : month - 1), Map.class, "boxDmonLists");
|
|
|
+ boolean flag = MapUtil.isNotEmpty(thisBoxDmonMap);
|
|
|
+ boolean flagL = MapUtil.isNotEmpty(thisBoxDmonMapL);
|
|
|
+ Double zj1 = flag ? Double.parseDouble(thisBoxDmonMap.get("zj1").toString()) : 0.0;
|
|
|
+ Double zj2 = flag ? Double.parseDouble(thisBoxDmonMap.get("zj2").toString()) : 0.0;
|
|
|
+ Double zj3 = flag ? Double.parseDouble(thisBoxDmonMap.get("zj3").toString()) : 0.0;
|
|
|
+ Double zj4 = flag ? Double.parseDouble(thisBoxDmonMap.get("zj4").toString()) : 0.0;
|
|
|
+ Double zj5 = flag ? Double.parseDouble(thisBoxDmonMap.get("zj5").toString()) : 0.0;
|
|
|
+ Double zj6 = flag ? Double.parseDouble(thisBoxDmonMap.get("zj6").toString()) : 0.0;
|
|
|
+ Double all = flag ? Double.parseDouble(thisBoxDmonMap.get("all").toString()) : 0.0;
|
|
|
+ Levi byydgc = Levi.by("zj1Name", DmonDeviceCode.ONE.getName()).set("zj1Value", EnergyUtils.getPercent(zj1, all))
|
|
|
+ .set("zj2Name", DmonDeviceCode.TWO.getName()).set("zj2Value", EnergyUtils.getPercent(zj2, all))
|
|
|
+ .set("zj3Name", DmonDeviceCode.THREE.getName()).set("zj3Value", EnergyUtils.getPercent(zj3, all))
|
|
|
+ .set("zj4Name", DmonDeviceCode.FOUR.getName()).set("zj4Value", EnergyUtils.getPercent(zj4, all))
|
|
|
+ .set("zj5Name", DmonDeviceCode.FIVE.getName()).set("zj5Value", EnergyUtils.getPercent(zj5, all))
|
|
|
+ .set("zj6Name", DmonDeviceCode.SIX.getName()).set("zj6Value", EnergyUtils.getPercent(zj6, all))
|
|
|
+ .set("allName", DmonDeviceCode.ALL.getName()).set("allValue", thisBoxDmonMap.get("all"));
|
|
|
|
|
|
+ double ndlj = 0.0, by = 0.0, jrss = 0.0, first = 0.0, second = 0.0, third = 0.0;
|
|
|
+ double ndljL = 0.0, byL = 0.0, jrssL = 0.0, firstL = 0.0, secondL = 0.0, thirdL = 0.0;
|
|
|
+ String ndljP = "0%", byP = "0%", jrssP = "0%", firstP = "0%", secondP = "0%", thirdP = "0%";
|
|
|
+ String firstName = "", secondName = "", thirdName = "";
|
|
|
+ // 封装年度用量环比
|
|
|
+ List<Map> monthList = new ArrayList<>();
|
|
|
+ for (int i = 1; i <= 12; i++) {
|
|
|
+ // 遍历今年和去年每个月的数据
|
|
|
+ Map map = mongoTemplate.findById(boxId + "_" + year + "_" + i, Map.class, "boxDmonLists");
|
|
|
+ Map lastMap = mongoTemplate.findById(boxId + "_" + lastYear + "_" + i, Map.class, "boxDmonLists");
|
|
|
+ Double aDouble = MapUtil.isNotEmpty(map) ? Double.parseDouble(map.get("all").toString()) : 0.0;
|
|
|
+ Double aDoubleL = MapUtil.isNotEmpty(lastMap) ? Double.parseDouble(lastMap.get("all").toString()) : 0.0;
|
|
|
+ monthList.add(Levi.by("year", year).set("month", i).set("value", aDouble).set("yearL", lastYear).set("valueL", aDoubleL));
|
|
|
+ // 累计今年和去年总数
|
|
|
+ ndlj = Double.sum(ndlj, aDouble);
|
|
|
+ ndljL = Double.sum(ndljL, aDoubleL);
|
|
|
+ }
|
|
|
+ // 计算年度累计总用量百分比
|
|
|
+ ndljP = EnergyUtils.getPercent(ndlj, ndljL);
|
|
|
+ // 计算本月总用量
|
|
|
+ by = Double.valueOf(monthList.get(month - 1).get("value").toString());
|
|
|
+ byL = Double.valueOf(monthList.get(month - 1).get("valueL").toString());
|
|
|
+ byP = EnergyUtils.getPercent(by, byL);
|
|
|
+ // 计算今日实时用量
|
|
|
+ jrss = EnergyUtils.getDayInfo("all", thisBoxDmonMap, day);
|
|
|
+ jrssL = EnergyUtils.getDayInfo("all", thisBoxDmonMapL, day);
|
|
|
+ jrssP = EnergyUtils.getPercent(jrss, jrssL);
|
|
|
+ // 用量第一、第二、第三
|
|
|
+ Double d1 = EnergyUtils.getDayInfo("zj1", thisBoxDmonMap, day);
|
|
|
+ Double d2 = EnergyUtils.getDayInfo("zj2", thisBoxDmonMap, day);
|
|
|
+ Double d3 = EnergyUtils.getDayInfo("zj3", thisBoxDmonMap, day);
|
|
|
+ Double d4 = EnergyUtils.getDayInfo("zj4", thisBoxDmonMap, day);
|
|
|
+ Double d5 = EnergyUtils.getDayInfo("zj5", thisBoxDmonMap, day);
|
|
|
+ Double d6 = EnergyUtils.getDayInfo("zj6", thisBoxDmonMap, day);
|
|
|
+ Map<Double, List<String>> dmonMaps = new HashMap(16);
|
|
|
+ dmonMaps.put(d1, EnergyUtils.addSameList(dmonMaps.get(d1), DmonDeviceCode.ONE.getName()));
|
|
|
+ dmonMaps.put(d2, EnergyUtils.addSameList(dmonMaps.get(d2), DmonDeviceCode.TWO.getName()));
|
|
|
+ dmonMaps.put(d3, EnergyUtils.addSameList(dmonMaps.get(d3), DmonDeviceCode.THREE.getName()));
|
|
|
+ dmonMaps.put(d4, EnergyUtils.addSameList(dmonMaps.get(d4), DmonDeviceCode.FOUR.getName()));
|
|
|
+ dmonMaps.put(d5, EnergyUtils.addSameList(dmonMaps.get(d5), DmonDeviceCode.FIVE.getName()));
|
|
|
+ dmonMaps.put(d6, EnergyUtils.addSameList(dmonMaps.get(d6), DmonDeviceCode.SIX.getName()));
|
|
|
+ List<Double> doubles = Arrays.asList(d1, d2, d3, d4, d5, d6);
|
|
|
+ Collections.sort(doubles);
|
|
|
+ first = doubles.get(doubles.size() - 1);
|
|
|
+ firstName = String.join(",", dmonMaps.get(first));
|
|
|
+ firstL = EnergyUtils.getDayInfo(firstName.replace("主机", "zj"), (day == 1 ? thisBoxDmonMapL : thisBoxDmonMap), (day == 1 ? dayOfMonth : day - 1));
|
|
|
+ firstP = EnergyUtils.getPercent(first, firstL);
|
|
|
+ second = doubles.get(doubles.size() - 2);
|
|
|
+ secondName = String.join(",", dmonMaps.get(second));
|
|
|
+ secondL = EnergyUtils.getDayInfo(secondName.replace("主机", "zj"), (day == 1 ? thisBoxDmonMapL : thisBoxDmonMap), (day == 1 ? dayOfMonth : day - 1));
|
|
|
+ secondP = EnergyUtils.getPercent(second, secondL);
|
|
|
+ third = doubles.get(doubles.size() - 3);
|
|
|
+ thirdName = String.join(",", dmonMaps.get(third));
|
|
|
+ thirdL = EnergyUtils.getDayInfo(thirdName.replace("主机", "zj"), (day == 1 ? thisBoxDmonMapL : thisBoxDmonMap), (day == 1 ? dayOfMonth : day - 1));
|
|
|
+ thirdP = EnergyUtils.getPercent(third, thirdL);
|
|
|
+ Levi byhxzb = Levi.by("ndlj", ndlj).set("ndljP", ndljP).set("by", by).set("byP", byP).set("jrss", jrss).set("jrssP", jrssP).set("first", first).set("firstP", firstP).set("firstName", firstName).set("second", second).set("secondP", secondP).set("secondName", secondName).set("third", third).set("thirdP", thirdP).set("thirdName", thirdName);
|
|
|
+ // 计算本日用量环比
|
|
|
+ List<Map> dayHbList = new ArrayList<>();
|
|
|
+ List<Map> dataHb = flag ? (List<Map>) ((List<Map>) thisBoxDmonMap.get("data")).get(day - 1).get("data") : null;
|
|
|
+ List<Map> dataHbL = flag ? (List<Map>) ((List<Map>) (day == 1 ? thisBoxDmonMapL : thisBoxDmonMap).get("data")).get(day == 1 ? dayOfMonth - 1 : day - 2).get("data") : null;
|
|
|
+ for (int i = 0; i < 8; i++) {
|
|
|
+ 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")));
|
|
|
+ }
|
|
|
+ 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")));
|
|
|
+ // 计算月度用量环比
|
|
|
+ List<Map> monthHbList = new ArrayList<>();
|
|
|
+ for (int i = 1; i <= 31; i++) {
|
|
|
+ double x1 = 0.0;
|
|
|
+ double x2 = 0.0;
|
|
|
+ if (flag) {
|
|
|
+ List<Map> l1 = (List<Map>) thisBoxDmonMap.get("data");
|
|
|
+ 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()));
|
|
|
+ }
|
|
|
+ if (flagL) {
|
|
|
+ List<Map> l2 = (List<Map>) thisBoxDmonMapL.get("data");
|
|
|
+ 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()));
|
|
|
+ }
|
|
|
+ monthHbList.add(Levi.by("day", i + "号").set("this", x1).set("last", x2));
|
|
|
+ }
|
|
|
+ // 封装所有数据
|
|
|
+ results.add(Levi.by("boxId", boxId).set("byydgc", byydgc).set("byhxzb", byhxzb).set("ndylhb", monthList).set("brylhb", dayHbList).set("ydylhb", monthHbList));
|
|
|
+ });
|
|
|
+ return ResponseBuilder.ok(results);
|
|
|
}
|
|
|
}
|