From a1c73b15972401552452c0755558e0d323968004 Mon Sep 17 00:00:00 2001
From: xufenglei <xufenglei>
Date: Mon, 13 Nov 2017 14:26:30 +0800
Subject: [PATCH] 更新 AQI算法

---
 src/main/java/com/moral/monitor/service/impl/ScreenServiceImpl.java |  211 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 209 insertions(+), 2 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 080d5ba..eb35a0d 100644
--- a/src/main/java/com/moral/monitor/service/impl/ScreenServiceImpl.java
+++ b/src/main/java/com/moral/monitor/service/impl/ScreenServiceImpl.java
@@ -1,25 +1,232 @@
 package com.moral.monitor.service.impl;
 
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Collections;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.List;
+import java.util.Map;
+import java.util.Set;
 
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.time.DateUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.util.ObjectUtils;
 
+import com.alibaba.fastjson.JSON;
 import com.moral.monitor.dao.AccountEntityMapper;
+import com.moral.monitor.dao.HistoryEntityMapper;
 import com.moral.monitor.entity.AccountEntity;
 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
 public class ScreenServiceImpl implements ScreenService {
 
 	@Autowired
-	private AccountEntityMapper accountDao;
+	private AccountEntityMapper accountMapper;
+
+	@Autowired
+	private HistoryEntityMapper historyMapper;
+
+	@Autowired
+	private OrganizationService organizationService;
 
 	public List<AccountEntity> getAccountLists(String account, String password) {
 		AccountEntityExample example = new AccountEntityExample();
 		example.or().andAccountEqualTo(account).andPasswordEqualTo(password);
-		return accountDao.selectByExample(example);
+		return accountMapper.selectByExample(example);
+	}
+
+	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();
+		// ���������������������������������������
+		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));
+		}
+		// ������00:00:00
+		parameters.put("end", DateUtils.truncate(date, Calendar.DATE));
+
+		Map<String, Object> average = null;
+		average = historyMapper.getMonthAverageBySensor(parameters);
+
+		if (ObjectUtils.isEmpty(average)) {
+			// ������������������
+			//resultMap.put("average", 50.3467 + Calendar.getInstance().get(Calendar.DAY_OF_MONTH) / 5);
+			resultMap.put("average", "N/A");
+		} else {
+			//resultMap.putAll(average);
+			//resultMap.put("average", String.format("%.2f", resultMap.get("average")));
+			resultMap.put("average", String.format("%.2f", average.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, minMacKey = 0, maxMacKey = 0, minIAQI = 0, maxIAQI = 0;
+			parameters.put("macKey", macKey);
+			average = historyMapper.getMonthAverageBySensor(parameters);
+			if (ObjectUtils.isEmpty(average)) {
+				continue;
+			} else {
+				avg = (Double) average.get("average");
+			}
+			String[] macKeyValues = ResourceUtil.getArrValue(macKey);
+			int index = -1;
+			for (int i = 0; i < macKeyValues.length; i++) {
+				if (avg <= Double.valueOf(macKeyValues[i])) {
+					if (i == 0) {
+						index = i;
+					} else {
+						index = i - 1;
+					}
+					break;
+				}
+			}
+			if (index == -1) {
+				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));
+			}
+		}
+		
+		if (!resultMap.containsKey("AQI")) {
+			if (ObjectUtils.isEmpty(IAQIs)) {
+				resultMap.put("AQI", "N/A");
+			}else {
+				double AQI = Collections.max(IAQIs);
+				// ������������������
+				/*if (AQI == 0.0) {
+					AQI = 60.670;
+				}*/
+				resultMap.put("AQI", String.format("%.0f", AQI));
+			}
+		}
+		 
+		return resultMap;
+	}
+
+	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();
+		// ������������ -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 (ObjectUtils.isEmpty(averageByAll)) {
+			String macLog = historyMapper.getMacLogByLast();
+			if (StringUtils.isNotBlank(macLog)) {
+				Map<String, Object> map = JSON.parseObject(macLog);
+				for (String key : map.keySet()) {
+					if (key.startsWith("e")) {
+						result.put(key, map.get(key));
+					}
+				}
+			}
+		}*/
+
+		for (Map<String, Object> map : averageByAll) {
+			result.put((String) map.get("mac_key"), map.get("avg"));
+		}
+		return result;
+	}
+
+	@Override
+	public Map<String, Object> getEquipmentStates(Map<String, Object> parameters) {
+		Map<String, Object> result = new HashMap<String, Object>();
+		setOrgIdsByAccount(parameters);
+		List<Map<String, Object>> list = historyMapper.getEquipmentStates(parameters);
+		Long all = 0L, normal = 0L, abnormal = 0L, stop = 0L;
+		for (Map<String, Object> map : list) {
+			Long count = (Long) map.get("count");
+			all += count;
+			switch ((Integer) map.get("state")) {
+			case 0:
+				normal = count;
+				break;
+			case 4:
+				stop = count;
+				break;
+			default:
+				abnormal += count;
+			}
+		}
+		result.put("all", all);
+		result.put("normal", normal);
+		result.put("abnormal", abnormal);
+		result.put("stop", stop);
+		return result;
+	}
+
+	public void setOrgIdsByAccount(Map<String, Object> parameters) {
+		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)) {
+			throw new BusinessException(accountId + "���������������������");
+		}
+		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) {
+		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