|  |  | 
 |  |  | package com.moral.monitor.service.impl;
 | 
 |  |  | 
 | 
 |  |  | import java.util.ArrayList;
 | 
 |  |  | import static org.apache.commons.lang3.time.DateUtils.addDays;
 | 
 |  |  | import static org.apache.commons.lang3.time.DateUtils.truncate;
 | 
 |  |  | import static org.springframework.util.ObjectUtils.isEmpty;
 | 
 |  |  | 
 | 
 |  |  | import java.util.Calendar;
 | 
 |  |  | import java.util.Collections;
 | 
 |  |  | import java.util.Date;
 | 
 |  |  | import java.util.HashMap;
 | 
 |  |  | import java.util.HashSet;
 | 
 |  |  | import java.util.LinkedHashMap;
 | 
 |  |  | import java.util.List;
 | 
 |  |  | import java.util.Map;
 | 
 |  |  | import java.util.Set;
 | 
 |  |  | import java.util.concurrent.Callable;
 | 
 |  |  | import java.util.concurrent.CompletionService;
 | 
 |  |  | import java.util.concurrent.ExecutorCompletionService;
 | 
 |  |  | import java.util.concurrent.ExecutorService;
 | 
 |  |  | import java.util.concurrent.Executors;
 | 
 |  |  | 
 | 
 |  |  | import org.apache.commons.collections.MapUtils;
 | 
 |  |  | import org.apache.commons.lang.time.DateUtils;
 | 
 |  |  | import org.apache.commons.lang3.StringUtils;
 | 
 |  |  | import org.apache.commons.lang3.time.DateUtils;
 | 
 |  |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
 |  |  | import org.springframework.data.mongodb.core.MongoTemplate;
 | 
 |  |  | import org.springframework.data.mongodb.core.aggregation.Aggregation;
 | 
 |  |  | import org.springframework.data.mongodb.core.aggregation.AggregationResults;
 | 
 |  |  | import org.springframework.data.mongodb.core.query.Criteria;
 | 
 |  |  | import org.springframework.stereotype.Service;
 | 
 |  |  | import org.springframework.util.ObjectUtils;
 | 
 |  |  | 
 | 
 |  |  | import com.mongodb.BasicDBObject;
 | 
 |  |  | 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
 | 
 |  |  | @SuppressWarnings("unused")
 | 
 |  |  | public class ScreenServiceImpl implements ScreenService {
 | 
 |  |  | 
 | 
 |  |  |    @Autowired
 | 
 |  |  | 
 |  |  |    @Autowired
 | 
 |  |  |    private OrganizationService organizationService;
 | 
 |  |  | 
 | 
 |  |  |    @Autowired
 | 
 |  |  |    private MongoTemplate mongoTemplate;
 | 
 |  |  | 	 | 
 |  |  |    public List<AccountEntity> getAccountLists(String account, String password) {
 | 
 |  |  |       AccountEntityExample example = new AccountEntityExample();
 | 
 |  |  |       example.or().andAccountEqualTo(account).andPasswordEqualTo(password);
 | 
 |  |  |       return accountMapper.selectByExample(example);
 | 
 |  |  |    }
 | 
 |  |  | 
 | 
 |  |  |    @SuppressWarnings("deprecation")
 | 
 |  |  |    public Map<String, Object> getMonthDataByEquipment(Map<String, Object> parameters) {
 | 
 |  |  |    public Map<String, Object> getMonthDataByEquipment(final Map<String, Object> parameters) throws Exception {
 | 
 |  |  |       Map<String, Object> resultMap = new HashMap<String, Object>();
 | 
 |  |  |       // 1.0 实际标准值
 | 
 |  |  |       resultMap.put("standard", ResourceUtil.getValue(parameters.get("macKey") + "-standard"));
 | 
 |  |  | 
 | 
 |  |  |       // 2.0 AQI 指数
 | 
 |  |  |       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;
 | 
 |  |  |       }
 | 
 |  |  |       date.setDate(day);
 | 
 |  |  |       /** 临时代码 结束 **/
 | 
 |  |  | 
 | 
 |  |  |       // 昨日00:00:00
 | 
 |  |  |       parameters.put("start", DateUtils.truncate(DateUtils.addDays(date, -1), Calendar.DATE));
 | 
 |  |  |       // 今日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);
 | 
 |  |  |       } else {
 | 
 |  |  |          resultMap.put("average", 0);
 | 
 |  |  |       }
 | 
 |  |  |       //
 | 
 |  |  |       String[] macKeys = { "e1", "e2", "e10", "e11", "e15", "e16" };
 | 
 |  |  |       List<Double> IAQIs = new ArrayList<Double>();
 | 
 |  |  |       for (String macKey : macKeys) {
 | 
 |  |  |          double avg = 0, maxMacKey = 0, minMacKey = 0;
 | 
 |  |  |          parameters.put("macKey", macKey);
 | 
 |  |  |          parameters.put("start", DateUtils.truncate(DateUtils.addDays(date, -1), Calendar.DATE));
 | 
 |  |  |          average = historyMapper.getMonthAverageBySensor(parameters);
 | 
 |  |  |          if (MapUtils.isNotEmpty(average)) {
 | 
 |  |  |             avg = (Double) average.get("average");
 | 
 |  |  |          }
 | 
 |  |  |          String[] macKeyValues = ResourceUtil.getArrValue(macKey);
 | 
 |  |  |          int index = -1;
 | 
 |  |  |          for (int i = 0; i < macKeyValues.length; i++) {
 | 
 |  |  |             Double macKeyValue = Double.valueOf(macKeyValues[i]);
 | 
 |  |  |             if (avg <= macKeyValue) {
 | 
 |  |  |                if (i == 0) {
 | 
 |  |  |                   index = 0;
 | 
 |  |  |                   minMacKey = macKeyValue;
 | 
 |  |  |                   maxMacKey = Double.valueOf(macKeyValues[i + 1]);
 | 
 |  |  |                } else {
 | 
 |  |  |                   index = i - 1;
 | 
 |  |  |                   minMacKey = Double.valueOf(macKeyValues[i - 1]);
 | 
 |  |  |                   maxMacKey = macKeyValue;
 | 
 |  |  |       final String[] IAQIValues = ResourceUtil.getArrValue("IAQI");
 | 
 |  |  |       ExecutorService threadPool = Executors.newCachedThreadPool();
 | 
 |  |  |       CompletionService<Double> cs = new ExecutorCompletionService<Double>(threadPool);
 | 
 |  |  |       for (final String macKey : macKeys) {
 | 
 |  |  |          cs.submit(new Callable<Double>() {
 | 
 |  |  |             @Override
 | 
 |  |  |             public Double call() throws Exception {
 | 
 |  |  |                Double result = null;
 | 
 |  |  |                double avg = 0, minMacKey = 0, maxMacKey = 0, minIAQI = 0, maxIAQI = 0;
 | 
 |  |  |                Map<String, Object> map = new HashMap<String, Object>(parameters);
 | 
 |  |  |                map.put("macKey", macKey);
 | 
 |  |  |                Map<String, Object> average = historyMapper.getMonthAverageBySensor(map);
 | 
 |  |  |                if (!ObjectUtils.isEmpty(average)) {
 | 
 |  |  |                   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) {
 | 
 |  |  |                      result = Double.MAX_VALUE;
 | 
 |  |  |                   } else {
 | 
 |  |  |                      minMacKey = Double.valueOf(macKeyValues[index]);
 | 
 |  |  |                      maxMacKey = Double.valueOf(macKeyValues[index + 1]);
 | 
 |  |  |                      minIAQI = Double.valueOf(IAQIValues[index]);
 | 
 |  |  |                      maxIAQI = Double.valueOf(IAQIValues[index + 1]);
 | 
 |  |  |                      result = calculateIAQI(maxIAQI, minIAQI, maxMacKey, minMacKey, avg);
 | 
 |  |  |                   }
 | 
 |  |  |                }
 | 
 |  |  |                continue;
 | 
 |  |  |                return result;
 | 
 |  |  |             }
 | 
 |  |  |          });
 | 
 |  |  |       }
 | 
 |  |  | 
 | 
 |  |  |          }
 | 
 |  |  |          if (index == -1) {
 | 
 |  |  |             resultMap.put("AQI", ">500");
 | 
 |  |  |             break;
 | 
 |  |  |          }
 | 
 |  |  |          String[] IAQIValues = ResourceUtil.getArrValue("IAQI");
 | 
 |  |  |          double minIAQI = Double.valueOf(IAQIValues[index]);
 | 
 |  |  |          double maxIAQI = Double.valueOf(IAQIValues[index + 1]);
 | 
 |  |  |          IAQIs.add(calculateIAQI(maxIAQI, minIAQI, maxMacKey, minMacKey, avg));
 | 
 |  |  |       // 3.0 本月平均值
 | 
 |  |  |       Map<String, Object> average = null;
 | 
 |  |  |       Map<String, Object> map = new HashMap<String, Object>(parameters);
 | 
 |  |  |       // 每月一日的数据取上月的数据
 | 
 |  |  |       if (1 == Calendar.getInstance().get(Calendar.DAY_OF_MONTH)) {
 | 
 |  |  |          // 上个月1日00:00:00
 | 
 |  |  |          map.put("start", DateUtils.truncate(DateUtils.addMonths(date, -1), Calendar.MONTH));
 | 
 |  |  |       } else {
 | 
 |  |  |          // 这个月1日00:00:00
 | 
 |  |  |          map.put("start", DateUtils.truncate(date, Calendar.MONTH));
 | 
 |  |  |       }
 | 
 |  |  |       resultMap.put("standard", 35);
 | 
 |  |  |       if (!resultMap.containsKey("AQI")) {
 | 
 |  |  |          resultMap.put("AQI", Collections.max(IAQIs));
 | 
 |  |  |       average = historyMapper.getMonthAverageBySensor(map);
 | 
 |  |  |       if (ObjectUtils.isEmpty(average)) {
 | 
 |  |  |          resultMap.put("average", "N/A");
 | 
 |  |  |       } else {
 | 
 |  |  |          resultMap.put("average", String.format("%.2f", average.get("average")));
 | 
 |  |  |       }
 | 
 |  |  | 		 | 
 |  |  |       Set<Double> IAQIs = new HashSet<Double>();
 | 
 |  |  |       for (String macKey : macKeys) {
 | 
 |  |  |          IAQIs.add(cs.take().get());
 | 
 |  |  |       }
 | 
 |  |  |       IAQIs.remove(null);
 | 
 |  |  |       if (ObjectUtils.isEmpty(IAQIs)) {
 | 
 |  |  |          resultMap.put("AQI", "N/A");
 | 
 |  |  |       } else {
 | 
 |  |  |          Double AQI = Collections.max(IAQIs);
 | 
 |  |  |          if (AQI == Double.MAX_VALUE) {
 | 
 |  |  |             resultMap.put("AQI", IAQIValues[IAQIValues.length - 1]);
 | 
 |  |  |          } else {
 | 
 |  |  |             resultMap.put("AQI", String.format("%.0f", AQI));
 | 
 |  |  |          }
 | 
 |  |  |       }
 | 
 |  |  | 
 | 
 |  |  |       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);
 | 
 |  |  | 
 | 
 |  |  |       for (Map<String, Object> map : averageByAll) {
 | 
 |  |  |          result.put((String) map.get("mac_key"), map.get("avg"));
 | 
 |  |  |       }
 | 
 |  |  |       return result;
 | 
 |  |  |    }
 | 
 |  |  | 
 | 
 |  |  |    public void setOrgIdsByAccount(Map<String, Object> parameters) {
 | 
 |  |  |       AccountEntity account = accountMapper.selectByPrimaryKey((Integer.valueOf((String) parameters.get("accountId"))));
 | 
 |  |  |       String organization = account.getOrganization();
 | 
 |  |  |       //不是摩瑞尔账号的需要根据组织来获取数据权限
 | 
 |  |  |       if (!("-1".equals(organization) || "5212b9dfb55448e699889e01fa0fa6a2".equals(organization))) {
 | 
 |  |  |          Set<String> orgIds = organizationService.getChildOrganizationIds(account.getOrganization());
 | 
 |  |  |          parameters.put("orgIds", orgIds);
 | 
 |  |  |       }
 | 
 |  |  |    }
 | 
 |  |  | 
 | 
 |  |  |    @Override
 | 
 |  |  | 
 |  |  |       return result;
 | 
 |  |  |    }
 | 
 |  |  | 
 | 
 |  |  |    private double calculateIAQI(double maxIAQI, double minIAQI, double maxMacKey, double minMacKey,double avg) {
 | 
 |  |  |    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;
 | 
 |  |  |    }
 | 
 |  |  | 
 | 
 |  |  |    @Override
 | 
 |  |  |    public Map<String, Object> getMonthAverageBySensor(Map<String, Object> parameters) {
 | 
 |  |  |       Map<String, Object> result = new HashMap<String, Object>();
 | 
 |  |  |       Date date = new Date();
 | 
 |  |  |       //Long end = DateUtils.truncate(date, Calendar.DATE).getTime(),start;
 | 
 |  |  |       Date end = DateUtils.truncate(date, Calendar.DATE),start;
 | 
 |  |  | 		 | 
 |  |  |       // 每月一日的数据取上月的数据
 | 
 |  |  |       if (1 == Calendar.getInstance().get(Calendar.DAY_OF_MONTH)) {
 | 
 |  |  |          // 上个月1日00:00:00
 | 
 |  |  |          //start = DateUtils.truncate(DateUtils.addMonths(date, -1), Calendar.MONTH).getTime();
 | 
 |  |  |          start = DateUtils.truncate(DateUtils.addMonths(date, -1), Calendar.MONTH);
 | 
 |  |  |       } else {
 | 
 |  |  |          // 这个月1日00:00:00
 | 
 |  |  |          //start =  DateUtils.truncate(date, Calendar.MONTH).getTime();
 | 
 |  |  |          start =  DateUtils.truncate(date, Calendar.MONTH);
 | 
 |  |  |       }
 | 
 |  |  |       start = DateUtils.addHours(start, 8);
 | 
 |  |  |       end = DateUtils.addHours(end, 8);
 | 
 |  |  |       Aggregation aggregation = Aggregation.newAggregation(
 | 
 |  |  |             Aggregation.match(Criteria.where("mac").is(parameters.get("mac"))),
 | 
 |  |  |             Aggregation.match(Criteria.where("time").gte(start)),
 | 
 |  |  |             Aggregation.match(Criteria.where("time").lt(end)),
 | 
 |  |  |             Aggregation.group("mac").avg((String) parameters.get("macKey")).as("avg")
 | 
 |  |  |          );
 | 
 |  |  |       //AggregationResults<BasicDBObject> results = mongoTemplate.aggregate(aggregation, "data",BasicDBObject.class);
 | 
 |  |  |       AggregationResults<BasicDBObject> results = mongoTemplate.aggregate(aggregation, "data_hourly",BasicDBObject.class);
 | 
 |  |  |       List<BasicDBObject> list = results.getMappedResults();
 | 
 |  |  |       if (!ObjectUtils.isEmpty(list)) {
 | 
 |  |  |          result = list.get(0);
 | 
 |  |  |       }
 | 
 |  |  |       return result;
 | 
 |  |  |    }
 | 
 |  |  | 
 | 
 |  |  |    @Override
 | 
 |  |  |    public Map<String, Object> getDayAQIBySensor(final Map<String, Object> parameters) throws Exception {
 | 
 |  |  |       Map<String, Object> resultMap = new HashMap<String, Object>();
 | 
 |  |  |       Date date = new Date();
 | 
 |  |  |       // 昨日00:00:00
 | 
 |  |  |       parameters.put("start", DateUtils.truncate(DateUtils.addDays(date, -1), Calendar.DATE));
 | 
 |  |  |       // 今日00:00:00
 | 
 |  |  |       parameters.put("end", DateUtils.truncate(date, Calendar.DATE));
 | 
 |  |  |       String[] macKeys = { "e1", "e2", "e10", "e11", "e15", "e16" };
 | 
 |  |  |       final String[] IAQIValues = ResourceUtil.getArrValue("IAQI");
 | 
 |  |  |       ExecutorService threadPool = Executors.newCachedThreadPool();
 | 
 |  |  |       CompletionService<Double> cs = new ExecutorCompletionService<Double>(threadPool);
 | 
 |  |  |       for (final String macKey : macKeys) {
 | 
 |  |  |          cs.submit(new Callable<Double>() {
 | 
 |  |  |             @Override
 | 
 |  |  |             public Double call() throws Exception {
 | 
 |  |  |                Double result = null;
 | 
 |  |  |                double avg = 0, minMacKey = 0, maxMacKey = 0, minIAQI = 0, maxIAQI = 0;
 | 
 |  |  |                Map<String, Object> map = new HashMap<String, Object>(parameters);
 | 
 |  |  |                map.put("macKey", macKey);
 | 
 |  |  |                Map<String, Object> average = historyMapper.getMonthAverageBySensor(map);
 | 
 |  |  |                if (!ObjectUtils.isEmpty(average)) {
 | 
 |  |  |                   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) {
 | 
 |  |  |                      result = Double.MAX_VALUE;
 | 
 |  |  |                   } else {
 | 
 |  |  |                      minMacKey = Double.valueOf(macKeyValues[index]);
 | 
 |  |  |                      maxMacKey = Double.valueOf(macKeyValues[index + 1]);
 | 
 |  |  |                      minIAQI = Double.valueOf(IAQIValues[index]);
 | 
 |  |  |                      maxIAQI = Double.valueOf(IAQIValues[index + 1]);
 | 
 |  |  |                      result = calculateIAQI(maxIAQI, minIAQI, maxMacKey, minMacKey, avg);
 | 
 |  |  |                   }
 | 
 |  |  |                }
 | 
 |  |  |                return result;
 | 
 |  |  |             }
 | 
 |  |  |          });
 | 
 |  |  |       }
 | 
 |  |  |       Set<Double> IAQIs = new HashSet<Double>();
 | 
 |  |  |       for (String macKey : macKeys) {
 | 
 |  |  |          IAQIs.add(cs.take().get());
 | 
 |  |  |       }
 | 
 |  |  |       IAQIs.remove(null);
 | 
 |  |  |       if (ObjectUtils.isEmpty(IAQIs)) {
 | 
 |  |  |          resultMap.put("AQI", "N/A");
 | 
 |  |  |       } else {
 | 
 |  |  |          Double AQI = Collections.max(IAQIs);
 | 
 |  |  |          if (AQI == Double.MAX_VALUE) {
 | 
 |  |  |             resultMap.put("AQI", IAQIValues[IAQIValues.length - 1]);
 | 
 |  |  |          } else {
 | 
 |  |  |             resultMap.put("AQI", String.format("%.0f", AQI));
 | 
 |  |  |          }
 | 
 |  |  |       }
 | 
 |  |  |       return resultMap;
 | 
 |  |  |    }
 | 
 |  |  | 
 | 
 |  |  |    @Override
 | 
 |  |  |    public Map<String, Object> getDayAQIByDevice(Map<String, Object> parameters) {
 | 
 |  |  |       Map<String, Object> resultMap = new HashMap<String, Object>();
 | 
 |  |  |       Date date = new Date();
 | 
 |  |  |       // 昨日00:00:00
 | 
 |  |  |       parameters.put("start", truncate(addDays(date, -1), Calendar.DATE));
 | 
 |  |  |       // 今日00:00:00
 | 
 |  |  |       parameters.put("end", truncate(date, Calendar.DATE));
 | 
 |  |  |       String[] IAQIValues = ResourceUtil.getArrValue("IAQI");
 | 
 |  |  |       Map<String, Double> average = historyMapper.getDayAQIByDevice(parameters);
 | 
 |  |  |       if (isEmpty(average)) {
 | 
 |  |  |          resultMap.put("AQI", "N/V");
 | 
 |  |  |       } else {
 | 
 |  |  |          Set<Double> IAQIs = new HashSet<Double>();
 | 
 |  |  |          for (Map.Entry<String, Double> entry : average.entrySet()) {
 | 
 |  |  |             double minMacKey = 0, maxMacKey = 0, minIAQI = 0, maxIAQI = 0;
 | 
 |  |  |             String[] macKeyValues = ResourceUtil.getArrValue(entry.getKey());
 | 
 |  |  |             Double avg = entry.getValue();
 | 
 |  |  |             if (isEmpty(avg)) {
 | 
 |  |  |                IAQIs.add(null);
 | 
 |  |  |             } else {
 | 
 |  |  |                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) {
 | 
 |  |  |                   IAQIs.add(Double.MAX_VALUE);
 | 
 |  |  |                } else {
 | 
 |  |  |                   minMacKey = Double.valueOf(macKeyValues[index]);
 | 
 |  |  |                   maxMacKey = Double.valueOf(macKeyValues[index + 1]);
 | 
 |  |  |                   minIAQI = Double.valueOf(IAQIValues[index]);
 | 
 |  |  |                   maxIAQI = Double.valueOf(IAQIValues[index + 1]);
 | 
 |  |  |                   Double result = calculateIAQI(maxIAQI, minIAQI, maxMacKey, minMacKey, avg);
 | 
 |  |  |                   IAQIs.add(result);
 | 
 |  |  |                }
 | 
 |  |  |             }
 | 
 |  |  |          }
 | 
 |  |  |          IAQIs.remove(null);
 | 
 |  |  |          if (isEmpty(IAQIs)) {
 | 
 |  |  |             resultMap.put("AQI", "N/V");
 | 
 |  |  |          } else {
 | 
 |  |  |             Double AQI = Collections.max(IAQIs);
 | 
 |  |  |             if (AQI == Double.MAX_VALUE) {
 | 
 |  |  |                resultMap.put("AQI", IAQIValues[IAQIValues.length - 1]);
 | 
 |  |  |             } else {
 | 
 |  |  |                resultMap.put("AQI", String.format("%.0f", AQI));
 | 
 |  |  |             }
 | 
 |  |  |          }
 | 
 |  |  |       }
 | 
 |  |  |       return resultMap;
 | 
 |  |  |    }
 | 
 |  |  | }
 |