From 369454bcdbdd0c6fb0fa7ac6ec19112b9b6a179e Mon Sep 17 00:00:00 2001
From: fengxiang <110431245@qq.com>
Date: Mon, 26 Mar 2018 14:03:12 +0800
Subject: [PATCH] 数据查询 sql查询优化,一次带出 多项数据
---
src/main/java/com/moral/service/impl/HistoryMinutelyServiceImpl.java | 124 ++++++++++++++++++++++++++++++++++------
1 files changed, 104 insertions(+), 20 deletions(-)
diff --git a/src/main/java/com/moral/service/impl/HistoryMinutelyServiceImpl.java b/src/main/java/com/moral/service/impl/HistoryMinutelyServiceImpl.java
index bf84e4a..a35f50d 100644
--- a/src/main/java/com/moral/service/impl/HistoryMinutelyServiceImpl.java
+++ b/src/main/java/com/moral/service/impl/HistoryMinutelyServiceImpl.java
@@ -9,7 +9,6 @@
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Arrays;
-import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
@@ -27,9 +26,15 @@
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
+import java.util.stream.Collectors;
import javax.annotation.Resource;
+import com.moral.common.util.ReportTimeFormat;
+import com.moral.entity.charts.DataCondition;
+import com.moral.entity.charts.LineChartCriteria;
+import com.moral.entity.charts.PairData;
+import com.moral.entity.charts.TimePeriod;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
@@ -140,6 +145,12 @@
Map<String, List<Object>> resultMap = new HashMap<String, List<Object>>();
List<Map<String, Object>> list = JSON.parseObject((String)parameters.get("items"), new TypeReference<List<Map<String, Object>>>() {});
parameters.remove("items");
+ Map<String, Object> timeType = JSON.parseObject((String)parameters.get("timeTypes"), new TypeReference<Map<String, Object>>() {});
+ parameters.remove("timeTypes");
+ parameters.putAll(timeType);
+ parameters.put("type", parameters.get("value"));
+ parameters.remove("value");
+
ExecutorService threadPool = Executors.newCachedThreadPool();
CompletionService<Map<String, List<Object>>> cs = new ExecutorCompletionService<Map<String, List<Object>>>(threadPool);
@@ -217,23 +228,13 @@
List<Object> timeList = (List<Object>) parameters.get("timeList");
List<Object> dataList = new ArrayList<Object>();
- Set<String> sensors ;
- if (parameters.containsKey("sensorKey")) {
- String sensorKey = (String) parameters.get("sensorKey");
- 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"));
- }
+ Set<String> 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, Double[]> doubleMap = new LinkedHashMap<String, Double[]>();
for (Map<String, Object> map : resultList) {
String time = map.get("time").toString();
@@ -293,8 +294,23 @@
parameters.put("end", end);
if (parameters.containsKey("sensorKey")) {
- String sensorKey = (String) parameters.get("sensorKey");
- String sensorKeyColumn = "AVG(json -> '$." + sensorKey + "[0]') " + sensorKey;
+ List<String> sensors;
+ try {
+ sensors = JSON.parseObject((String)parameters.get("sensors"), new TypeReference<List<String>>() {});
+ parameters.put("sensors", new HashSet<String>(sensors));
+ } catch (Exception e) {
+ sensors = new ArrayList<String>();
+ sensors.add((String)parameters.get("sensorKey"));
+ }
+ String sensorKeyColumn = "";
+ for (int i = 0; i < sensors.size(); i++) {
+ String sensorKey = sensors.get(i).split("-")[0];
+ sensorKeyColumn += "AVG(json -> '$." + sensorKey + "[0]') " + sensorKey;
+ if (i != sensors.size() - 1) {
+ sensorKeyColumn += " ,";
+ }
+
+ }
parameters.put("sensorKeyColumn", sensorKeyColumn);
}
@@ -435,4 +451,72 @@
return result;
}
+ /**
+ * ������������������������������������������������������������������
+ * @param lineChartCriteria
+ * @return
+ */
+ @Override
+ public Map<String, List<List<Double>>> queryLineChartDateByCrieria(LineChartCriteria lineChartCriteria){
+ Map<String,List<List<Double>>> listMap = new HashMap<>();
+ List<String> sensorKeys = lineChartCriteria.getSensorKeys();
+ List<DataCondition> dataConditionList = lineChartCriteria.getDataConditions();
+ TimePeriod timePeriod = lineChartCriteria.getTimePeriod();
+ sensorKeys.forEach(sensorKey -> {
+ listMap.put(sensorKey,new ArrayList<List<Double>>(dataConditionList.size()));
+ });
+ dataConditionList.forEach(dataCondition -> {
+ Map<String,List<Double>> dataMap = queryOneLineChartDateByCrieria(sensorKeys,timePeriod,dataCondition);
+ // ������������
+ listMap.forEach( (sensorKey,list) -> {
+ List<Double> rowData = dataMap.get(sensorKey);
+ list.add(rowData);
+ });
+ });
+ return listMap;
+ }
+
+ /**
+ * ������������������������������������������������������������������
+ * @param sensorKeys
+ * @param timePeriod
+ * @param dataCondition
+ * @return
+ */
+ public Map<String,List<Double>> queryOneLineChartDateByCrieria(List<String> sensorKeys, TimePeriod timePeriod, DataCondition dataCondition){
+ List<String> timeList = ReportTimeFormat.makeTimeList(timePeriod);
+ List<Map<String,Object>> lineChartDatas = historyMinutelyMapper.selectLineChartDateByCrieria(sensorKeys,timePeriod,dataCondition);
+ Map<String,List<Double>> lineChartDatasWithEmpty = new HashMap<>();
+ // lineChartDatasWithEmpty ���������
+ sensorKeys.forEach(sensorKey -> {
+ lineChartDatasWithEmpty.put(sensorKey,timeList.stream().map(time -> {
+ Double data = null;
+ return data;
+ }).collect(Collectors.toList()));
+ });
+ // m ���������data���index������������������m������
+ int m = 0;
+ int dataLength = lineChartDatas.size()-1;
+ m = dataLength>-1?0:-1;
+ if(m >-1){
+ for(int n =0;n<timeList.size();n++){
+ if(m>-1) {
+ String time = timeList.get(n);
+ Map<String,Object> rowData = lineChartDatas.get(m);
+ String keyTime = rowData.get("format_time").toString();
+ if(time.equals(keyTime)){
+ // list to map
+ int finalN = n;
+ sensorKeys.forEach(sensorKey -> {
+ Double sensorValue = rowData.get(sensorKey)!= null?new Double(rowData.get(sensorKey).toString()):null;
+ lineChartDatasWithEmpty.get(sensorKey).set(finalN,sensorValue);
+ });
+ // ������ -1���������������
+ m = m<dataLength ? m+1 : -1;
+ }
+ }
+ }
+ }
+ return lineChartDatasWithEmpty;
+ }
}
--
Gitblit v1.8.0