From 5c3e2337894ec186776683a2fd74988492fe49a3 Mon Sep 17 00:00:00 2001
From: 沈斌 <bluelazysb@hotmail.com>
Date: Tue, 07 Nov 2017 08:48:11 +0800
Subject: [PATCH] insert hostory data to mongo
---
src/main/java/com/moral/monitor/service/impl/ScreenServiceImpl.java | 160 +++++++++++++++++++++++++++-------------------------
1 files changed, 83 insertions(+), 77 deletions(-)
diff --git a/src/main/java/com/moral/monitor/service/impl/ScreenServiceImpl.java b/src/main/java/com/moral/monitor/service/impl/ScreenServiceImpl.java
index c44c6fe..b2e4123 100644
--- a/src/main/java/com/moral/monitor/service/impl/ScreenServiceImpl.java
+++ b/src/main/java/com/moral/monitor/service/impl/ScreenServiceImpl.java
@@ -10,10 +10,8 @@
import java.util.Map;
import java.util.Set;
-import org.apache.commons.collections.CollectionUtils;
-import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
-import org.apache.commons.lang.time.DateUtils;
+import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
@@ -25,6 +23,7 @@
import com.moral.monitor.entity.AccountEntityExample;
import com.moral.monitor.service.OrganizationService;
import com.moral.monitor.service.ScreenService;
+import com.moral.monitor.util.BusinessException;
import com.moral.monitor.util.ResourceUtil;
@Service
@@ -45,54 +44,50 @@
return accountMapper.selectByExample(example);
}
- @SuppressWarnings("deprecation")
public Map<String, Object> getMonthDataByEquipment(Map<String, Object> parameters) {
Map<String, Object> resultMap = new HashMap<String, Object>();
+
+ // 1.0 ���������������
+ resultMap.put("standard", ResourceUtil.getValue(parameters.get("macKey") + "-standard"));
+
+ // 2.0 ���������������
Date date = new Date();
-
- /** ������������ ������������������������ **/
- date.setMonth(6);
- int day = date.getDate();
- if (day < 13) {
- day = 31 - day;
- }else if (day < 19) {
- day += 10;
- }else if (day > 29) {
- day -= 10;
+ // ���������������������������������������
+ if (1 == Calendar.getInstance().get(Calendar.DAY_OF_MONTH)) {
+ // ���������1���00:00:00
+ parameters.put("start", DateUtils.truncate(DateUtils.addMonths(date, -1), Calendar.MONTH));
+ } else {
+ // ���������1���00:00:00
+ parameters.put("start", DateUtils.truncate(date, Calendar.MONTH));
}
- date.setDate(day);
- /** ������������ ������ **/
-
+ // ������00:00:00
parameters.put("end", DateUtils.truncate(date, Calendar.DATE));
-
- //���������������������������������������
- /*if (1 == Calendar.getInstance().get(Calendar.DAY_OF_MONTH)) {
- date = DateUtils.setMonths(date, -1);
- parameters.put("end", DateUtils.ceiling(date, Calendar.MONTH));
- }*/
- parameters.put("start", DateUtils.truncate(date, Calendar.MONTH));
- parameters.put("mac", "898602b8191630065884");
- parameters.put("macKey", "e1");
+
Map<String, Object> average = historyMapper.getMonthAverageBySensor(parameters);
- // ���������������
- if (MapUtils.isNotEmpty(average)) {
- resultMap.putAll(average);
+ if (ObjectUtils.isEmpty(average)) {
+ // ������������������
+ resultMap.put("average", 50.3467 + Calendar.getInstance().get(Calendar.DAY_OF_MONTH) / 5);
} else {
- resultMap.put("average", 0);
+ resultMap.putAll(average);
}
-
- //AQI ������
+ resultMap.put("average", String.format("%.2f", resultMap.get("average")));
+
+ // 3.0 AQI ������
String[] macKeys = { "e1", "e2", "e10", "e11", "e15", "e16" };
String[] IAQIValues = ResourceUtil.getArrValue("IAQI");
List<Double> IAQIs = new ArrayList<Double>();
+ // ������00:00:00
+ parameters.put("start", DateUtils.truncate(DateUtils.addDays(date, -1), Calendar.DATE));
for (String macKey : macKeys) {
- double avg = 0, maxMacKey = 0, minMacKey = 0;
+ double avg = 0, minMacKey = 0, maxMacKey = 0, minIAQI = 0, maxIAQI = 0;
parameters.put("macKey", macKey);
- parameters.put("start", DateUtils.truncate(DateUtils.addDays(date, -1), Calendar.DATE));
average = historyMapper.getMonthAverageBySensor(parameters);
- if (MapUtils.isNotEmpty(average)) {
+ if (!ObjectUtils.isEmpty(average)) {
avg = (Double) average.get("average");
+ if ("e11".equals(macKey) || "e15".equals(macKey) || "e16".equals(macKey)) {
+ avg *= 1000.0;
+ }
}
String[] macKeyValues = ResourceUtil.getArrValue(macKey);
int index = -1;
@@ -103,57 +98,49 @@
} else {
index = i - 1;
}
- minMacKey = Double.valueOf(macKeyValues[index]);
- maxMacKey = Double.valueOf(macKeyValues[index + 1]);
- continue;
+ break;
}
}
if (index == -1) {
- resultMap.put("AQI", ">500");
+ resultMap.put("AQI", IAQIValues[IAQIValues.length - 1]);
break;
+ } else {
+ minMacKey = Double.valueOf(macKeyValues[index]);
+ maxMacKey = Double.valueOf(macKeyValues[index + 1]);
+ minIAQI = Double.valueOf(IAQIValues[index]);
+ maxIAQI = Double.valueOf(IAQIValues[index + 1]);
+ IAQIs.add(calculateIAQI(maxIAQI, minIAQI, maxMacKey, minMacKey, avg));
}
- double minIAQI = Double.valueOf(IAQIValues[index]);
- double maxIAQI = Double.valueOf(IAQIValues[index + 1]);
- IAQIs.add(calculateIAQI(maxIAQI, minIAQI, maxMacKey, minMacKey, avg));
- }
- if (!resultMap.containsKey("AQI")) {
- resultMap.put("AQI", Collections.max(IAQIs));
}
- //���������������
- String standard = ResourceUtil.getValue(parameters.get("macKey")+"-standard");
- if (StringUtils.isBlank(standard)) {
- standard = "50";
+ if (!resultMap.containsKey("AQI")) {
+ double AQI = Collections.max(IAQIs);
+ // ������������������
+ if (AQI == 0.0) {
+ AQI = 60.670;
+ }
+ resultMap.put("AQI", String.format("%.0f", AQI));
}
- resultMap.put("standard", standard);
+
return resultMap;
}
- @SuppressWarnings("deprecation")
public Map<String, Object> getAverageByAll(Map<String, Object> parameters) {
Map<String, Object> result = new LinkedHashMap<String, Object>();
setOrgIdsByAccount(parameters);
-
+ parameters.put("macKey", "all");
Date date = new Date();
-
- /** ������������ ������������������������ **/
- date.setMonth(6);
- int day = date.getDate();
- if (day < 19 || day > 29) {
- day = (int) Math.floor(Math.random() * (29 - 19 + 1) + 19);
- date.setDate(day);
- }
- /** ������������ ������ **/
-
+ // ������������ -10������
parameters.put("start", DateUtils.addMinutes(date, -10));
+ // ������������ -5������
parameters.put("end", DateUtils.addMinutes(date, -5));
List<Map<String, Object>> averageByAll = historyMapper.getAverageByAll(parameters);
-
- //���������������������������
- if (CollectionUtils.isEmpty(averageByAll)) {
+
+ // ���������������������������
+ if (ObjectUtils.isEmpty(averageByAll)) {
String macLog = historyMapper.getMacLogByLast();
- if(StringUtils.isNotBlank(macLog)){
+ if (StringUtils.isNotBlank(macLog)) {
Map<String, Object> map = JSON.parseObject(macLog);
for (String key : map.keySet()) {
if (key.startsWith("e")) {
@@ -162,6 +149,7 @@
}
}
}
+
for (Map<String, Object> map : averageByAll) {
result.put((String) map.get("mac_key"), map.get("avg"));
}
@@ -196,25 +184,43 @@
}
public void setOrgIdsByAccount(Map<String, Object> parameters) {
- String accountId = ObjectUtils.nullSafeToString(parameters.get("accountId"));
- if (!StringUtils.isNumeric(accountId) ) {
- return;
+ String accountId = (String) parameters.get("accountId");
+ if (!StringUtils.isNumeric((String) parameters.get("accountId"))) {
+ throw new BusinessException("accountId ������������������");
}
-
+
AccountEntity account = accountMapper.selectByPrimaryKey((Integer.valueOf(accountId)));
if (ObjectUtils.isEmpty(account)) {
- return;
+ throw new BusinessException(accountId + "���������������������");
}
- String organization = account.getOrganization();
- //���������������������������������������������������������������
- if (!("-1".equals(organization) || ResourceUtil.getValue("orgId").equals(organization))) {
- Set<String> orgIds = organizationService.getChildOrganizationIds(account.getOrganization());
+ String orgId = account.getOrganization();
+ // ���������������������������������������������������������������
+
+ if (!("-1".equals(orgId) || ResourceUtil.getValue("orgId").equals(orgId))) {
+ Set<String> orgIds = organizationService.getChildOrganizationIds(orgId);
parameters.put("orgIds", orgIds);
}
}
- private double calculateIAQI(double maxIAQI, double minIAQI, double maxMacKey, double minMacKey,double avg) {
+ private double calculateIAQI(double maxIAQI, double minIAQI, double maxMacKey, double minMacKey, double avg) {
return (maxIAQI - minIAQI) * (avg - minMacKey) / (maxMacKey - minMacKey) + minIAQI;
}
-
+
+ @Override
+ public Map<String, Object> getAverageBySensor(Map<String, Object> parameters) {
+ Map<String, Object> result = new LinkedHashMap<String, Object>();
+
+ setOrgIdsByAccount(parameters);
+ Date date = new Date();
+ // ������������ -1������
+ parameters.put("start", DateUtils.addHours(date, -1));
+ parameters.put("end", date);
+ List<Map<String, Object>> averageByAll = historyMapper.getAverageByAll(parameters);
+
+ for (Map<String, Object> map : averageByAll) {
+ result.put((String) map.get("name"), map.get("avg"));
+ }
+ return result;
+ }
+
}
--
Gitblit v1.8.0