xufenglei
2017-12-01 b41f303340d8c21dad9e1b2fd798a0957e7fd7d1
src/main/java/com/moral/service/impl/HistoryServiceImpl.java
@@ -1,5 +1,15 @@
package com.moral.service.impl;
import static com.moral.common.bean.Constants.NULL_VALUE;
import static org.apache.commons.lang3.time.DateUtils.addDays;
import static org.apache.commons.lang3.time.DateUtils.addHours;
import static org.apache.commons.lang3.time.DateUtils.addMinutes;
import static org.apache.commons.lang3.time.DateUtils.addMonths;
import static org.apache.commons.lang3.time.DateUtils.parseDate;
import static org.apache.commons.lang3.time.DateUtils.truncate;
import static org.springframework.util.ObjectUtils.isEmpty;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
@@ -10,22 +20,20 @@
import java.util.Map;
import java.util.Set;
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.common.exception.BusinessException;
import com.moral.common.util.CalculateUtils;
import com.moral.common.util.ResourceUtil;
import com.moral.mapper.HistoryMapper;
import com.moral.service.AccountService;
import com.moral.service.HistoryService;
import com.moral.util.CalculateUtils;
import com.moral.util.Constants;
import com.moral.util.ResourceUtil;
@Service
public class HistoryServiceImpl implements HistoryService {
@@ -40,36 +48,41 @@
    private MongoTemplate mongoTemplate;
   @Override
   public Map<String, Object> getAverageByAll(Map<String, Object> parameters) {
      Map<String, Object> result = new LinkedHashMap<String, Object>();
   public Map<String, Object> getAllSensorAverageByDevice(Map<String, Object> parameters) {
      accountService.setOrgIdsByAccount(parameters);
      parameters.put("macKey", "all");
      Date date = new Date();
      // 当前时间 -10分钟
      parameters.put("start", DateUtils.addMinutes(date, -10));
      parameters.put("start", 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"));
      parameters.put("end", addMinutes(date, -5));
      String queryColumns = "";
      for (int i = 1; i < 20; i++) {
         if (i == 1) {
            queryColumns += "AVG(value -> '$.e" + i + "') e" + i;
         } else {
            queryColumns += " , AVG(value -> '$.e" + i + "') e" + i;
         }
      }
      return result;
      parameters.put("queryColumns", queryColumns);
      parameters.put("macKey", "all");
      List<Map<String, Object>> list = historyMapper.getAreaAllDataByAccount(parameters);
      return list.get(0);
   }
   @Override
   public Map<String, Object> getAverageBySensor(Map<String, Object> parameters) {
   public Map<String, Object> getDeviceRankingBySensorAverage(Map<String, Object> parameters) {
      Map<String, Object> result = new LinkedHashMap<String, Object>();
      accountService.setOrgIdsByAccount(parameters);
      Date date = new Date();
      // 当前时间 -1小时
      parameters.put("start", DateUtils.addHours(date, -1));
      parameters.put("start", addHours(date, -1));
      parameters.put("end", date);
      List<Map<String, Object>> averageByAll = historyMapper.getAverageByAll(parameters);
      parameters.put("macKey", "'$."+ parameters.get("macKey")+"'");
      List<Map<String, Object>> list = historyMapper.getAreaAllDataByAccount(parameters);
      for (Map<String, Object> map : averageByAll) {
      for (Map<String, Object> map : list) {
         result.put((String) map.get("name"), map.get("avg"));
      }
      return result;
@@ -80,20 +93,20 @@
      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));
      parameters.put("start", truncate(addDays(date, -1), Calendar.DATE));
      // 今日00:00:00
      parameters.put("end", DateUtils.truncate(date, Calendar.DATE));
      parameters.put("end", truncate(date, Calendar.DATE));
      String[] IAQIValues = ResourceUtil.getArrValue("IAQI");
      Map<String, Double> average = historyMapper.getDayAQIByDevice(parameters);
      if (ObjectUtils.isEmpty(average)) {
         resultMap.put("AQI", Constants.NULL_VALUE);
      if (isEmpty(average)) {
         resultMap.put("AQI", NULL_VALUE);
      } 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 (ObjectUtils.isEmpty(avg)) {
            if (isEmpty(avg)) {
               IAQIs.add(null);
            } else {
               int index = -1;
@@ -120,8 +133,8 @@
            }
         }
         IAQIs.remove(null);
         if (ObjectUtils.isEmpty(IAQIs)) {
            resultMap.put("AQI", Constants.NULL_VALUE);
         if (isEmpty(IAQIs)) {
            resultMap.put("AQI", NULL_VALUE);
         } else {
            Double AQI = Collections.max(IAQIs);
            if (AQI == Double.MAX_VALUE) {
@@ -138,14 +151,14 @@
   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;
      Long end = truncate(date, Calendar.DATE).getTime(), 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 = truncate(addMonths(date, -1), Calendar.MONTH).getTime();
      } else {
         // 这个月1日00:00:00
         start =  DateUtils.truncate(date, Calendar.MONTH).getTime();
         start =  truncate(date, Calendar.MONTH).getTime();
      }
      Aggregation aggregation = Aggregation.newAggregation(
            Aggregation.match(Criteria.where("mac").is(parameters.get("mac"))),
@@ -155,8 +168,8 @@
      );
      AggregationResults<BasicDBObject> results = mongoTemplate.aggregate(aggregation, "data", BasicDBObject.class);
      List<BasicDBObject> list = results.getMappedResults();
      if (ObjectUtils.isEmpty(list)) {
         result.put("average", Constants.NULL_VALUE);
      if (isEmpty(list)) {
         result.put("average", NULL_VALUE);
      } else {
         result = list.get(0);
         result.put("average", String.format("%.2f", result.get("average")));
@@ -164,4 +177,37 @@
      return result;
   }
   @Override
   public List<Map<String, Object>> getSensorsAverageByDevice4Report(Map<String, Object> parameters,List<Map<String, Object>> sensors) {
      Object type = parameters.get("type");
      if ("hour".equals(type)) {
         parameters.put("type", "%Y-%m-%d %H:00");
      } else if ("minute".equals(type)) {
         parameters.put("type", "%Y-%m-%d %H:%i:00");
      } else {
         throw new BusinessException("type参数输入错误!");
      }
      try {
         Date start = parseDate((String)parameters.get("time"), "yyyy-MM-dd");
         parameters.put("start", start);
         parameters.put("end", addDays(start, 1));
      } catch (ParseException e) {
         e.printStackTrace();
         throw new BusinessException("time参数输入错误!");
      }
      String queryColumns = "";
      for (int i = 0; i < sensors.size(); i++) {
         String sensorKey = (String) sensors.get(i).get("key");
         if (i == sensors.size() - 1) {
            queryColumns += "AVG(value -> '$." + sensorKey + "') " + sensorKey;
         } else {
            queryColumns += "AVG(value -> '$." + sensorKey + "') " + sensorKey +",";
         }
      }
      parameters.put("queryColumns", queryColumns);
      return historyMapper.getSensorsAverageByDevice4Report(parameters);
   }
}