123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- 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;
- /**
- * 能耗平台大屏
- *
- * @author JCM
- * @description
- * @date 2021/1/15 15:36
- */
- @Slf4j
- @Service("energyPlatformService")
- @Transactional(rollbackFor = {Exception.class})
- public class EnergyPlatformServiceImpl implements EnergyPlatformService {
- @Autowired
- private MongoTemplate mongoTemplate;
- @Value("${energy.grpId}")
- private String grpIds;
- /**
- * 初始化数据
- */
- @Override
- public void initData() {
- initToken();
- initBoxList();
- initDmonGroups();
- initDmonByDmonGroups();
- initDmonValues();
- log.info("能耗数据全部初始化完毕");
- }
- /**
- * 初始化当前用户名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 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(String boxId) {
- 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();
- // 封装本月用电构成
- 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;
- List<Map> byydgc = Arrays.asList(Levi.by("name", DmonDeviceCode.ONE.getName()).set("value", zj1).set("percent", EnergyUtils.getPercent(zj1, all)),
- Levi.by("name", DmonDeviceCode.TWO.getName()).set("value", zj2).set("percent", EnergyUtils.getPercent(zj2, all)),
- Levi.by("name", DmonDeviceCode.THREE.getName()).set("value", zj3).set("percent", EnergyUtils.getPercent(zj3, all)),
- Levi.by("name", DmonDeviceCode.FOUR.getName()).set("value", zj4).set("percent", EnergyUtils.getPercent(zj4, all)),
- Levi.by("name", DmonDeviceCode.FIVE.getName()).set("value", zj5).set("percent", EnergyUtils.getPercent(zj5, all)),
- Levi.by("name", DmonDeviceCode.SIX.getName()).set("value", zj6).set("percent", EnergyUtils.getPercent(zj6, all)),
- Levi.by("name", DmonDeviceCode.ALL.getName()).set("value", 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);
- List<Map> byhxzb = Arrays.asList(Levi.by("name", "年度累计总用量").set("value1", ndlj).set("value2", ndljP),
- Levi.by("name", "本月总用量").set("value1", by).set("value2", byP),
- Levi.by("name", "今日实时用量").set("value1", jrss).set("value2", jrssP),
- Levi.by("name", "业态用量第一:" + firstName).set("value1", first).set("value2", firstP),
- Levi.by("name", "业态用量第二:" + secondName).set("value1", second).set("value2", secondP),
- Levi.by("name", "业态用量第三:" + thirdName).set("value1", third).set("value2", thirdP)
- );
- // 计算本日用量环比
- List<Map> dayHbList = new ArrayList<>();
- List<Map> dataHb = new ArrayList<>();
- List<Map> dataHbL = new ArrayList<>();
- if (thisBoxDmonMap.get("data") != null) {
- List<Map> x1 = (List<Map>) thisBoxDmonMap.get("data");
- if (x1.get(day - 1) != null) {
- dataHb = flag ? (List<Map>) x1.get(day - 1).get("data") : null;
- }
- }
- if ((day == 1 ? (flagL ? thisBoxDmonMapL.get("data") : null) : (flag ? thisBoxDmonMap.get("data") : null)) != null) {
- List<Map> x2 = (List<Map>) (day == 1 ? thisBoxDmonMapL : thisBoxDmonMap).get("data");
- if (x2.get(day == 1 ? dayOfMonth - 1 : day - 2) != null) {
- dataHbL = flag ? (List<Map>) x2.get(day == 1 ? dayOfMonth - 1 : day - 2).get("data") : null;
- }
- }
- if (CollUtil.isNotEmpty(dataHb) && CollUtil.isNotEmpty(dataHbL)) {
- 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")));
- } else {
- for (int i = 0; i < 8; i++) {
- dayHbList.add(Levi.by("time", String.format("%02d", i * 3) + ":00").set("today", 0.0).set("lastDay", 0.0));
- }
- dayHbList.add(Levi.by("time", "23:59").set("today", 0.0).set("lastDay", 0.0));
- }
- // 计算月度用量环比
- 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");
- if (CollUtil.isNotEmpty(l1)) {
- 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");
- if (CollUtil.isNotEmpty(l2)) {
- 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));
- }
- // 封装所有数据
- return ResponseBuilder.ok(Levi.by("boxId", boxId).set("byydgc", byydgc).set("byhxzb", byhxzb).set("ndylhb", monthList).set("brylhb", dayHbList).set("ydylhb", monthHbList));
- }
- }
|