xufenglei
2018-03-05 fbec1c270bfbeac936035cbc9db61d8a1162ccf9
src/main/java/com/moral/service/impl/HistoryMinutelyServiceImpl.java
@@ -19,6 +19,7 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutorCompletionService;
@@ -35,6 +36,7 @@
import com.moral.common.util.ResourceUtil;
import com.moral.common.util.ValidateUtil;
import com.moral.entity.Device;
import com.moral.entity.Sensor;
import com.moral.mapper.DeviceMapper;
import com.moral.mapper.HistoryMinutelyMapper;
import com.moral.mapper.SensorMapper;
@@ -52,6 +54,7 @@
   @Resource
   private SensorMapper sensorMapper;
   // volatile
   private Set<String> sensorKeys = new HashSet<String>();
   @Override
@@ -128,29 +131,6 @@
      return resultMap;
   }
   public Map<String, List<Object>> getMonitorPointOrDeviceAvgData4Compare(Map<String, Object> parameters) throws Exception {
      Map<String, List<Object>> resultMap = new HashMap<String, List<Object>>();
      if (ObjectUtils.isEmpty(parameters)) {
         resultMap.put("c", null);
         return resultMap;
      }
      List<Map<String, Object>> resultList = getMonitorPointOrDeviceAvgData(parameters);
      List<Object> timeList = new ArrayList<Object>();
      List<Object> dataList = new ArrayList<Object>();
      String sensorKey = (String) parameters.get("sensorKey");
      for (Map<String, Object> map : resultList) {
         String time = map.get("time").toString();
         time =time.substring(time.length() - 2);
         timeList.add(time);
         dataList.add(map.get(sensorKey));
      }
      String part = (String) parameters.get("part");
      resultMap.put("time" + part, timeList);
      resultMap.put("data" + part, dataList);
      return resultMap;
   }
   @Override
   public Map<String, List<Object>> getCompareReport(Map<String, Object> parameters) throws Exception {
      Map<String, List<Object>> resultMap = new HashMap<String, List<Object>>();
@@ -231,6 +211,73 @@
      return resultMap;
   }
   public Map<String, List<Object>> getMonitorPointOrDeviceAvgData4Compare(Map<String, Object> parameters) throws Exception {
      Map<String, List<Object>> resultMap = new HashMap<String, List<Object>>();
      if (ObjectUtils.isEmpty(parameters)) {
         resultMap.put("c", null);
         return resultMap;
      }
      List<Map<String, Object>> resultList = getMonitorPointOrDeviceAvgData(parameters);
      List<Object> timeList = new ArrayList<Object>();
      List<Object> dataList = new ArrayList<Object>();
      Set<String> sensors ;
      if (parameters.containsKey("sensorKey")) {
         String sensorKey = (String) parameters.get("sensorKey");
//         Map<String, List<Double>> doubleMap = new HashMap<String, List<Double>>();
//         List<Double> doubles = new ArrayList<Double>();
//         for (Map<String, Object> map : resultList) {
//            String time = map.get("time").toString();
//            time =time.substring(time.length() - 2);
//            timeList.add(time);
//            // dataList.add(map.get(sensorKey));
//            doubles.add((Double) map.get(sensorKey));
//         }
//         doubleMap.put(sensorKey, doubles);
//         dataList.add(doubleMap);
         Sensor sensor = new Sensor();
         sensor.setSensorKey(sensorKey);
         sensor = sensorMapper.selectOne(sensor );
         sensors = new HashSet<String>();
         sensors.add(sensorKey + "-" + sensor.getName() + "-" + sensor.getUnit());
      } else {
         sensors = new TreeSet<String>(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
               return Integer.compare(Integer.valueOf(o1.split("-")[0].replace("e", "")), Integer.valueOf(o2.split("-")[0].replace("e", "")));
            }
         });
         sensors.addAll((Set<String>)parameters.get("sensors"));
      }
      Map<String, List<Double>> doubleMap = new LinkedHashMap<String, List<Double>>();
      for (Map<String, Object> map : resultList) {
         String time = map.get("time").toString();
         time =time.substring(time.length() - 2);
         timeList.add(time);
         for (String sensor : sensors) {
            String[] split = sensor.split("-");
            String sensorKey = split[0];
            if (map.containsKey(sensorKey)) {
               List<Double> doubles;
               if (doubleMap.containsKey(sensor)) {
                  doubles = doubleMap.get(sensor);
               } else {
                  doubles = new ArrayList<Double>();
               }
               doubles.add((Double) map.get(sensorKey));
               doubleMap.put(sensor, doubles);
            }
            //dataList.add(map.get(sensorKey));
         }
      }
      dataList.add(doubleMap);
      String part = (String) parameters.get("part");
      resultMap.put("time" + part, timeList);
      resultMap.put("data" + part, dataList);
      return resultMap;
   }
   
   @Override
   public List<Map<String, Object>> getMonitorPointOrDeviceAvgData(Map<String, Object> parameters) throws Exception {
@@ -364,11 +411,12 @@
      for (int i = 0; i < sensors.size(); i++) {
         String sensorKey = (String) sensors.get(i).get("sensor_key");
         String name = (String) sensors.get(i).get("name");
         String unit = (String) sensors.get(i).get("unit");
         sensorKeyColumn += "AVG(json -> '$." + sensorKey + "[0]') " + sensorKey;
         if (i != sensors.size() - 1) {
            sensorKeyColumn += " ,";
         } 
         sensorKeys.add(sensorKey + "-" + name);
         sensorKeys.add(sensorKey + "-" + name + "-" + unit );
      }
      return sensorKeyColumn;
   }