From 918572b2e18e5a42d61dbf8d61d746e2da070610 Mon Sep 17 00:00:00 2001
From: jinpengyong <jpy123456>
Date: Thu, 30 Dec 2021 11:13:04 +0800
Subject: [PATCH] 行业贡献率修改接口数据格式

---
 screen-api/src/main/java/com/moral/api/service/impl/ProfessionServiceImpl.java |  272 +++++++++++++++++++++++++++++++++---------------------
 1 files changed, 165 insertions(+), 107 deletions(-)

diff --git a/screen-api/src/main/java/com/moral/api/service/impl/ProfessionServiceImpl.java b/screen-api/src/main/java/com/moral/api/service/impl/ProfessionServiceImpl.java
index 0a7dcfa..54daf3e 100644
--- a/screen-api/src/main/java/com/moral/api/service/impl/ProfessionServiceImpl.java
+++ b/screen-api/src/main/java/com/moral/api/service/impl/ProfessionServiceImpl.java
@@ -149,13 +149,13 @@
     }
 
     @Override
-    public List<Map<String, Object>> professionContribution(Map<String, Object> params) {
+    public Map<String, Object> professionContribution(Map<String, Object> params) {
         int orgId = Integer.parseInt(params.get("organizationId").toString());
         List<String> professions = Arrays.asList(params.get("professions").toString().split(","));
         String type = params.get("type").toString();
         String time = params.get("time").toString();
         String sensorCode = params.get("sensorCode").toString();
-        List<Map<String, Object>> result = new ArrayList<>();
+        Map<String, Object> result = new HashMap<>();
 
         switch (type) {
             case "year":
@@ -174,8 +174,13 @@
     }
 
     //���������������
-    private List<Map<String, Object>> professionContributionOfYear(Integer orgId, List<String> professions, String time, String sensorCode) {
-        List<Map<String, Object>> result = new ArrayList<>();
+    private Map<String, Object> professionContributionOfYear(Integer orgId, List<String> professions, String time, String sensorCode) {
+        Map<String, Object> result = new HashMap<>();
+
+        //1.������������������
+        List<String> timeLag = DateUtils.getTimeLag(time);
+        result.put("time", timeLag);
+
 
         //������������������������������
         QueryWrapper<Organization> organizationQueryWrapper = new QueryWrapper<>();
@@ -183,10 +188,6 @@
                 .eq("id", orgId);
         Integer locationLevelCode = organizationService.getOne(organizationQueryWrapper).getLocationLevelCode();
 
-        //���������������������������
-        List<String> allMacs = deviceService.getMacsByOrganizationId(orgId);
-
-        List<String> timeLag = DateUtils.getTimeLag(time);
 
         //���������������������������
         QueryWrapper<CityAqiMonthly> cityAqiMonthlyQueryWrapper = new QueryWrapper<>();
@@ -202,25 +203,26 @@
         }
 
 
-        //���������������������������������
+        //���������������������������������������������������
+        List<String> allMacs = deviceService.getMacsByOrganizationId(orgId);
         List<HistoryMonthly> allDeviceData = historyMonthlyService.getValueByMacs(allMacs, time);
-        //���time������
         Map<String, List<HistoryMonthly>> allDeviceDataMap = allDeviceData.stream()
                 .collect(Collectors.groupingBy(o -> DateUtils.dateToDateString(o.getTime()).substring(0, 7)));
 
 
+        //���������������������
+        Map<String, Object> resultCityDataMap = new HashMap<>();
+        List<Map<String, Object>> cityDataList = new ArrayList<>();
+        //������������������������������������
+        Map<String, Object> allDeviceSumMap = new HashMap<>();
         for (String yearMonth : timeLag) {
-            Map<String, Object> resultMap = new HashMap<>();
-            resultMap.put("time", yearMonth);
-
-
             //������������������������������
             Double allDeviceSum = null;
             List<HistoryMonthly> historyMonthlyList = allDeviceDataMap.get(yearMonth);
             if (!ObjectUtils.isEmpty(historyMonthlyList)) {
                 allDeviceSum = historyMonthlyService.calculatedValue(historyMonthlyList, sensorCode, "sum");
             }
-            resultMap.put("allDeviceSum", allDeviceSum);
+            allDeviceSumMap.put(yearMonth, allDeviceSum);
 
 
             //���������
@@ -233,11 +235,23 @@
                     cityValue = Double.parseDouble(dataValue.get(sensorName).toString());
                 }
             }
-            resultMap.put("cityValue", cityValue);
-            result.add(resultMap);
+            Map<String, Object> cityDataMap = new HashMap<>();
+            //������������
+            cityDataMap.put("name", cityValue);
+            cityDataList.add(cityDataMap);
         }
 
+        //2.������������������
+        resultCityDataMap.put("data", cityDataList);
+        result.put("������", resultCityDataMap);
+
+
         for (String profession : professions) {
+            //���������������������������
+            HashMap<String, Object> professionMap = new HashMap<>();
+            List<Map<String, Object>> dataList = new ArrayList<>();
+
+
             //������������������
             String professionName = null;
             Map<String, Object> dictData = (Map<String, Object>) redisTemplate.opsForValue().get(RedisConstants.DICT_DATA_KEY);
@@ -250,62 +264,66 @@
             }
 
 
-            //���������������������
+            //������������������������������������������������������
             List<Device> professionDevices = getDevicesOfProfessions(orgId, Collections.singletonList(profession));
             List<String> professionMacs = professionDevices.stream().map(Device::getMac).collect(Collectors.toList());
-
-
-            //������������������������������������
             List<HistoryMonthly> professionDeviceData = historyMonthlyService.getValueByMacs(professionMacs, time);
-            //���time������
             Map<String, List<HistoryMonthly>> professionDataMap = professionDeviceData.stream()
                     .collect(Collectors.groupingBy(o -> DateUtils.dateToDateString(o.getTime()).substring(0, 7)));
 
 
-            for (Map<String, Object> map : result) {
-                Object allDeviceSum = map.get("allDeviceSum");
-                String resultTime = map.get("time").toString();
-                List<HistoryMonthly> historyMonthlyList = professionDataMap.get(resultTime);
+            for (String resultTime : timeLag) {
+                //���������������������
+                Map<String, Object> timeDataMap = new HashMap<>();
+                Object o = allDeviceSumMap.get(resultTime);
+                Double allDeviceSum = null;
+                if (o != null) {
+                    allDeviceSum = Double.parseDouble(o.toString());
+                }
+
+                List<HistoryMonthly> preofessionDeviceHistoryMonthlyList = professionDataMap.get(resultTime);
                 //���������
                 String contributionRate = null;
                 Double professionAvg = null;
-                if (!ObjectUtils.isEmpty(historyMonthlyList)) {
+                if (!ObjectUtils.isEmpty(preofessionDeviceHistoryMonthlyList)) {
                     //���������������������
-                    professionAvg = historyMonthlyService.calculatedValue(historyMonthlyList, sensorCode, "avg");
+                    professionAvg = historyMonthlyService.calculatedValue(preofessionDeviceHistoryMonthlyList, sensorCode, "avg");
                     //������������������������
-                    Double professionSum = historyMonthlyService.calculatedValue(historyMonthlyList, sensorCode, "sum");
+                    Double professionSum = historyMonthlyService.calculatedValue(preofessionDeviceHistoryMonthlyList, sensorCode, "sum");
                     //���������������������
                     NumberFormat numberFormat = NumberFormat.getInstance();
                     numberFormat.setMaximumFractionDigits(2);
-                    if (allDeviceSum != null && (Double) allDeviceSum != 0d) {
-                        contributionRate = numberFormat.format(professionSum / ((Double) allDeviceSum) * 100) + "%";
+                    if (allDeviceSum != null && allDeviceSum != 0d) {
+                        contributionRate = numberFormat.format(professionSum / (allDeviceSum) * 100) + "%";
                     }
                 }
-                Map<String, Object> professionMap = new HashMap<>();
-                professionMap.put("contributionRate", contributionRate);
-                professionMap.put("value", professionAvg == null ? null : AmendUtils.sciCal(professionAvg, 0));
-                map.put(professionName, professionMap);
+
+                //������������
+                timeDataMap.put("name", professionAvg == null ? null : AmendUtils.sciCal(professionAvg, 0));
+                //���������
+                timeDataMap.put("value", contributionRate);
+                dataList.add(timeDataMap);
             }
+            professionMap.put("data", dataList);
+            result.put(professionName, professionMap);
         }
-        result.forEach(map -> map.remove("allDeviceSum"));
         return result;
     }
 
     //���������������
-    private List<Map<String, Object>> professionContributionOfMonth(Integer orgId, List<String> professions, String time, String sensorCode) {
-        List<Map<String, Object>> result = new ArrayList<>();
+    private Map<String, Object> professionContributionOfMonth(Integer orgId, List<String> professions, String time, String sensorCode) {
+        Map<String, Object> result = new HashMap<>();
+
+        //1.������������������
+        List<String> timeLag = DateUtils.getTimeLag(time);
+        result.put("time", timeLag);
+
 
         //������������������������������
         QueryWrapper<Organization> organizationQueryWrapper = new QueryWrapper<>();
         organizationQueryWrapper.select("location_level_code")
                 .eq("id", orgId);
         Integer locationLevelCode = organizationService.getOne(organizationQueryWrapper).getLocationLevelCode();
-
-        //���������������������������
-        List<String> allMacs = deviceService.getMacsByOrganizationId(orgId);
-
-        //������������
-        List<String> timeLag = DateUtils.getTimeLag(time);
 
 
         //���������������������������
@@ -322,24 +340,26 @@
         }
 
 
-        //���������������������������������
+        //���������������������������������������������������
+        List<String> allMacs = deviceService.getMacsByOrganizationId(orgId);
         List<HistoryDaily> allDeviceData = historyDailyService.getValueByMacs(allMacs, time);
-        //���time������
         Map<String, List<HistoryDaily>> allDeviceDataMap = allDeviceData.stream()
                 .collect(Collectors.groupingBy(o -> DateUtils.dateToDateString(o.getTime()).substring(0, 10)));
 
 
+        //���������������������
+        Map<String, Object> resultCityDataMap = new HashMap<>();
+        List<Map<String, Object>> cityDataList = new ArrayList<>();
+        //������������������������������������
+        Map<String, Object> allDeviceSumMap = new HashMap<>();
         for (String yearMonthDay : timeLag) {
-            Map<String, Object> resultMap = new HashMap<>();
-            resultMap.put("time", yearMonthDay);
-
             //������������������������������
             Double allDeviceSum = null;
             List<HistoryDaily> historyDailyList = allDeviceDataMap.get(yearMonthDay);
             if (!ObjectUtils.isEmpty(historyDailyList)) {
                 allDeviceSum = historyDailyService.calculatedValue(historyDailyList, sensorCode, "sum");
             }
-            resultMap.put("allDeviceSum", allDeviceSum);
+            allDeviceSumMap.put(yearMonthDay, allDeviceSum);
 
 
             //���������
@@ -352,11 +372,24 @@
                     cityValue = Double.parseDouble(dataValue.get(sensorName).toString());
                 }
             }
-            resultMap.put("cityValue", cityValue);
-            result.add(resultMap);
+            Map<String, Object> cityDataMap = new HashMap<>();
+            //������������
+            cityDataMap.put("name", cityValue);
+            cityDataList.add(cityDataMap);
         }
 
+        //2.������������������
+        resultCityDataMap.put("data", cityDataList);
+        result.put("������", resultCityDataMap);
+
+
         for (String profession : professions) {
+            //���������������������������
+            HashMap<String, Object> professionMap = new HashMap<>();
+            List<Map<String, Object>> dataList = new ArrayList<>();
+
+
+            //������������������
             String professionName = null;
             Map<String, Object> dictData = (Map<String, Object>) redisTemplate.opsForValue().get(RedisConstants.DICT_DATA_KEY);
             List<SysDictData> professionInfo = (List<SysDictData>) dictData.get("profession");
@@ -367,49 +400,60 @@
                 }
             }
 
-            //���������������������
+
+            //������������������������������������������������������
             List<Device> professionDevices = getDevicesOfProfessions(orgId, Collections.singletonList(profession));
             List<String> professionMacs = professionDevices.stream().map(Device::getMac).collect(Collectors.toList());
-
-
-            //������������������������������������
             List<HistoryDaily> professionDeviceData = historyDailyService.getValueByMacs(professionMacs, time);
-            //���time������
             Map<String, List<HistoryDaily>> professionDataMap = professionDeviceData.stream()
                     .collect(Collectors.groupingBy(o -> DateUtils.dateToDateString(o.getTime()).substring(0, 10)));
 
 
-            for (Map<String, Object> map : result) {
-                Object allDeviceSum = map.get("allDeviceSum");
-                String resultTime = map.get("time").toString();
-                List<HistoryDaily> historyDailyList = professionDataMap.get(resultTime);
+            for (String resultTime : timeLag) {
+                //���������������������
+                Map<String, Object> timeDataMap = new HashMap<>();
+                Object o = allDeviceSumMap.get(resultTime);
+                Double allDeviceSum = null;
+                if (o != null) {
+                    allDeviceSum = Double.parseDouble(o.toString());
+                }
+
+                List<HistoryDaily> preofessionDeviceHistoryDailyList = professionDataMap.get(resultTime);
                 //���������
                 String contributionRate = null;
                 Double professionAvg = null;
-                if (!ObjectUtils.isEmpty(historyDailyList)) {
+                if (!ObjectUtils.isEmpty(preofessionDeviceHistoryDailyList)) {
                     //���������������������
-                    professionAvg = historyDailyService.calculatedValue(historyDailyList, sensorCode, "avg");
+                    professionAvg = historyDailyService.calculatedValue(preofessionDeviceHistoryDailyList, sensorCode, "avg");
                     //������������������������
-                    Double professionSum = historyDailyService.calculatedValue(historyDailyList, sensorCode, "sum");
+                    Double professionSum = historyDailyService.calculatedValue(preofessionDeviceHistoryDailyList, sensorCode, "sum");
                     //���������������������
                     NumberFormat numberFormat = NumberFormat.getInstance();
                     numberFormat.setMaximumFractionDigits(2);
-                    if (allDeviceSum != null && (Double) allDeviceSum != 0d) {
-                        contributionRate = numberFormat.format(professionSum / ((Double) allDeviceSum) * 100) + "%";
+                    if (allDeviceSum != null && allDeviceSum != 0d) {
+                        contributionRate = numberFormat.format(professionSum / (allDeviceSum) * 100) + "%";
                     }
                 }
-                Map<String, Object> professionMap = new HashMap<>();
-                professionMap.put("contributionRate", contributionRate);
-                professionMap.put("value", professionAvg == null ? null : AmendUtils.sciCal(professionAvg, 0));
-                map.put(professionName, professionMap);
+
+                //������������
+                timeDataMap.put("name", professionAvg == null ? null : AmendUtils.sciCal(professionAvg, 0));
+                //���������
+                timeDataMap.put("value", contributionRate);
+                dataList.add(timeDataMap);
             }
+            professionMap.put("data", dataList);
+            result.put(professionName, professionMap);
         }
-        result.forEach(map -> map.remove("allDeviceSum"));
         return result;
     }
 
-    private List<Map<String, Object>> professionContributionOfDay(Integer orgId, List<String> professions, String time, String sensorCode) {
-        List<Map<String, Object>> result = new ArrayList<>();
+    private Map<String, Object> professionContributionOfDay(Integer orgId, List<String> professions, String time, String sensorCode) {
+        Map<String, Object> result = new HashMap<>();
+
+        //1.������������������
+        List<String> timeLag = DateUtils.getTimeLag(time);
+        result.put("time", timeLag);
+
 
         //������������������������������
         QueryWrapper<Organization> organizationQueryWrapper = new QueryWrapper<>();
@@ -428,12 +472,6 @@
             sensorUpper = sensor.getUpper();
         }
 
-        //���������������������������
-        List<String> allMacs = deviceService.getMacsByOrganizationId(orgId);
-
-        //���������������
-        List<String> timeLag = DateUtils.getTimeLag(time);
-
 
         //������������������������������
         QueryWrapper<CityAqi> cityAqiQueryWrapper = new QueryWrapper<>();
@@ -449,25 +487,26 @@
         }
 
 
-        //������������������������������������
+        //������������������������������������������������������
+        List<String> allMacs = deviceService.getMacsByOrganizationId(orgId);
         List<HistoryHourly> allDeviceData = historyHourlyService.getValueByMacs(allMacs, time);
-        //���time������
         Map<String, List<HistoryHourly>> allDeviceDataMap = allDeviceData.stream()
                 .collect(Collectors.groupingBy(o -> DateUtils.dateToDateString(o.getTime()).substring(0, 13)));
 
 
+        //���������������������
+        Map<String, Object> resultCityDataMap = new HashMap<>();
+        List<Map<String, Object>> cityDataList = new ArrayList<>();
+        //������������������������������������
+        Map<String, Object> allDeviceSumMap = new HashMap<>();
         for (String yearMonthDayHour : timeLag) {
-            Map<String, Object> resultMap = new HashMap<>();
-            resultMap.put("time", yearMonthDayHour);
-
-
             //������������������������������
             Double allDeviceSum = null;
             List<HistoryHourly> historyHourlyList = allDeviceDataMap.get(yearMonthDayHour);
             if (!ObjectUtils.isEmpty(historyHourlyList)) {
                 allDeviceSum = historyHourlyService.calculatedValue(historyHourlyList, sensorCode, "sum", sensorLower, sensorUpper);
             }
-            resultMap.put("allDeviceSum", allDeviceSum);
+            allDeviceSumMap.put(yearMonthDayHour, allDeviceSum);
 
 
             //���������
@@ -480,11 +519,24 @@
                     cityValue = Double.parseDouble(dataValue.get(sensorName).toString());
                 }
             }
-            resultMap.put("cityValue", cityValue);
-            result.add(resultMap);
+            Map<String, Object> cityDataMap = new HashMap<>();
+            //������������
+            cityDataMap.put("name", cityValue);
+            cityDataList.add(cityDataMap);
         }
 
+        //2.������������������
+        resultCityDataMap.put("data", cityDataList);
+        result.put("������", resultCityDataMap);
+
+
         for (String profession : professions) {
+            //���������������������������
+            HashMap<String, Object> professionMap = new HashMap<>();
+            List<Map<String, Object>> dataList = new ArrayList<>();
+
+
+            //������������������
             String professionName = null;
             Map<String, Object> dictData = (Map<String, Object>) redisTemplate.opsForValue().get(RedisConstants.DICT_DATA_KEY);
             List<SysDictData> professionInfo = (List<SysDictData>) dictData.get("profession");
@@ -495,44 +547,50 @@
                 }
             }
 
-            //���������������������
+
+            //������������������������������������������������������
             List<Device> professionDevices = getDevicesOfProfessions(orgId, Collections.singletonList(profession));
             List<String> professionMacs = professionDevices.stream().map(Device::getMac).collect(Collectors.toList());
-
-
-            //���������������������������������������
             List<HistoryHourly> professionDeviceData = historyHourlyService.getValueByMacs(professionMacs, time);
-            //���time������
             Map<String, List<HistoryHourly>> professionDataMap = professionDeviceData.stream()
                     .collect(Collectors.groupingBy(o -> DateUtils.dateToDateString(o.getTime()).substring(0, 13)));
 
 
-            for (Map<String, Object> map : result) {
-                Object allDeviceSum = map.get("allDeviceSum");
-                String resultTime = map.get("time").toString();
-                List<HistoryHourly> historyHourlyList = professionDataMap.get(resultTime);
+            for (String resultTime : timeLag) {
+                //���������������������
+                Map<String, Object> timeDataMap = new HashMap<>();
+                Object o = allDeviceSumMap.get(resultTime);
+                Double allDeviceSum = null;
+                if (o != null) {
+                    allDeviceSum = Double.parseDouble(o.toString());
+                }
+
+                List<HistoryHourly> preofessionDeviceHistoryHourlyList = professionDataMap.get(resultTime);
                 //���������
                 String contributionRate = null;
                 Double professionAvg = null;
-                if (!ObjectUtils.isEmpty(historyHourlyList)) {
+                if (!ObjectUtils.isEmpty(preofessionDeviceHistoryHourlyList)) {
                     //���������������������
-                    professionAvg = historyHourlyService.calculatedValue(historyHourlyList, sensorCode, "avg", sensorLower, sensorUpper);
+                    professionAvg = historyHourlyService.calculatedValue(preofessionDeviceHistoryHourlyList, sensorCode, "avg", sensorLower, sensorUpper);
                     //������������������������
-                    Double professionSum = historyHourlyService.calculatedValue(historyHourlyList, sensorCode, "sum", sensorLower, sensorUpper);
+                    Double professionSum = historyHourlyService.calculatedValue(preofessionDeviceHistoryHourlyList, sensorCode, "sum", sensorLower, sensorUpper);
                     //���������������������
                     NumberFormat numberFormat = NumberFormat.getInstance();
                     numberFormat.setMaximumFractionDigits(2);
-                    if (allDeviceSum != null && (Double) allDeviceSum != 0d) {
-                        contributionRate = numberFormat.format(professionSum / ((Double) allDeviceSum) * 100) + "%";
+                    if (allDeviceSum != null && allDeviceSum != 0d) {
+                        contributionRate = numberFormat.format(professionSum / (allDeviceSum) * 100) + "%";
                     }
                 }
-                Map<String, Object> professionMap = new HashMap<>();
-                professionMap.put("contributionRate", contributionRate);
-                professionMap.put("value", professionAvg == null ? null : AmendUtils.sciCal(professionAvg, 0));
-                map.put(professionName, professionMap);
+
+                //������������
+                timeDataMap.put("name", professionAvg == null ? null : AmendUtils.sciCal(professionAvg, 0));
+                //���������
+                timeDataMap.put("value", contributionRate);
+                dataList.add(timeDataMap);
             }
+            professionMap.put("data", dataList);
+            result.put(professionName, professionMap);
         }
-        result.forEach(map -> map.remove("allDeviceSum"));
         return result;
     }
 }

--
Gitblit v1.8.0