| | |
| | | import static org.springframework.util.ObjectUtils.isEmpty;
|
| | |
|
| | | import java.math.BigDecimal;
|
| | | import java.math.RoundingMode;
|
| | | import java.text.ParseException;
|
| | | import java.time.LocalDate;
|
| | | import java.time.temporal.TemporalAdjusters;
|
| | | import java.util.ArrayList;
|
| | | import java.util.Arrays;
|
| | | import java.util.Collection;
|
| | | import java.util.Calendar;
|
| | | import java.util.Collections;
|
| | | import java.util.Comparator;
|
| | | import java.util.Date;
|
| | | import java.util.HashMap;
|
| | | import java.util.HashSet;
|
| | | import java.util.Iterator;
|
| | | import java.util.LinkedHashMap;
|
| | | import java.util.List;
|
| | | import java.util.Map;
|
| | |
| | | import java.util.concurrent.ExecutorCompletionService;
|
| | | import java.util.concurrent.ExecutorService;
|
| | | import java.util.concurrent.Executors;
|
| | | import java.util.stream.Collector;
|
| | | 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;
|
| | |
| | | import com.alibaba.fastjson.JSON;
|
| | | import com.alibaba.fastjson.TypeReference;
|
| | | import com.moral.common.util.CalculateUtils;
|
| | | import com.moral.common.util.ReportTimeFormat;
|
| | | 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.entity.charts.DataCondition;
|
| | | import com.moral.entity.charts.LineChartCriteria;
|
| | | import com.moral.entity.charts.TimePeriod;
|
| | | import com.moral.mapper.AlarmDailyMapper;
|
| | | import com.moral.mapper.AlarmMapper;
|
| | | import com.moral.mapper.DeviceMapper;
|
| | | import com.moral.mapper.HistoryMinutelyMapper;
|
| | | import com.moral.mapper.SensorMapper;
|
| | | import com.moral.service.HistoryMinutelyService;
|
| | |
|
| | | @Service
|
| | | @SuppressWarnings({ "unchecked", "rawtypes", "unused" })
|
| | | @SuppressWarnings({ "unchecked", "unused", "rawtypes" })
|
| | | public class HistoryMinutelyServiceImpl implements HistoryMinutelyService {
|
| | |
|
| | | @Resource
|
| | |
| | | @Resource
|
| | | private SensorMapper sensorMapper;
|
| | |
|
| | | // volatile
|
| | | @Resource
|
| | | private AlarmDailyMapper alarmDailyMapper;
|
| | |
|
| | | @Override
|
| | | public Map<String, Object> getDayAQIByDevice(Map<String, Object> parameters) {
|
| | |
| | |
|
| | | // 今日00:00:00
|
| | | parameters.put("end", localDate);
|
| | | String[] macKeys = { "e1", "e2", "e10", "e11", "e15", "e16" };
|
| | | String sensorKeyColumn = "";
|
| | | for (int i = 0; i < macKeys.length; i++) {
|
| | | sensorKeyColumn += "AVG(json -> '$." + macKeys[i] + "[0]') " + macKeys[i];
|
| | | if (i != macKeys.length -1) {
|
| | | sensorKeyColumn += " ,";
|
| | | } |
| | | }
|
| | | parameters.put("sensorKeyColumn", sensorKeyColumn);
|
| | | parameters.put("sensorKeys", Arrays.asList("e1", "e2", "e10", "e11", "e15", "e16"));
|
| | | Map<String, Double> average = historyMinutelyMapper.getSersionAvgByDevice(parameters);
|
| | | return getAQIByDataMap(average);
|
| | | }
|
| | |
| | | }
|
| | |
|
| | | @Override
|
| | | public Map<String, List<Object>> getCompareReport(Map<String, Object> parameters) throws Exception {
|
| | | 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("timeType"), new TypeReference<Map<String, Object>>() {});
|
| | | parameters.remove("timeType");
|
| | | parameters.putAll(timeType);
|
| | | public Map<String, List> getCompareReport(Map<String, Object> parameters) throws Exception {
|
| | | Map<String, List> resultMap = new HashMap<String, List>();
|
| | | List<Map<String, Object>> list = JSON.parseObject((String)parameters.remove("items"), new TypeReference<List<Map<String, Object>>>() {});
|
| | |
|
| | | ExecutorService threadPool = Executors.newCachedThreadPool();
|
| | | CompletionService<Map<String, List<Object>>> cs = new ExecutorCompletionService<Map<String, List<Object>>>(threadPool);
|
| | |
|
| | | String type = (String) parameters.get("type");
|
| | | parameters.putAll(getElementByType(type));
|
| | | Integer timeLength = Integer.valueOf(parameters.remove("timeLength").toString());
|
| | | if ("month".equals(type)) {
|
| | | for (Map<String, Object> map : list) {
|
| | | int timeLength = Integer.valueOf(parameters.get("timeLength").toString());
|
| | | String[] formatTime = map.get("formatTime").toString().split("-");
|
| | | LocalDate localDate = LocalDate.of(Integer.valueOf(formatTime[0]), Integer.valueOf(formatTime[1]), 1);
|
| | | int lengthOfMonth = localDate.lengthOfMonth();
|
| | | if (lengthOfMonth > timeLength) {
|
| | | parameters.put("timeLength", lengthOfMonth);
|
| | | timeLength = lengthOfMonth;
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | | List<Object> timeList = new ArrayList<Object>();
|
| | | for (int i = 0; i <Integer.valueOf(parameters.get("timeLength").toString()); i++) {
|
| | | timeList.add(i, String.format("%02d", "day".equals(type) || "hour".equals(type) ? i : i + 1));
|
| | | for (int i = 0; i < timeLength; i++) {
|
| | | timeList.add(String.format("%02d", "day".equals(type) || "hour".equals(type) ? i : i + 1));
|
| | | }
|
| | | parameters.put("timeList", timeList);
|
| | |
|
| | | ExecutorService threadPool = Executors.newCachedThreadPool();
|
| | | CompletionService<Map<String, Object>> cs = new ExecutorCompletionService<Map<String, Object>>(threadPool);
|
| | | for (int i = 0; i < list.size(); i++) {
|
| | | Map<String, Object> map = list.get(i);
|
| | | map.put("part", i);
|
| | | if (ObjectUtils.isEmpty(map.get("mac"))) {
|
| | | map.remove("mac");
|
| | | }
|
| | | map.put("time", map.get("formatTime"));
|
| | | map.remove("formatTime");
|
| | | map.put("time", map.remove("formatTime"));
|
| | | map.putAll(parameters);
|
| | | cs.submit(new Callable<Map<String, List<Object>>>() {
|
| | | cs.submit(new Callable<Map<String, Object>>() {
|
| | | @Override
|
| | | public Map<String, List<Object>> call() throws Exception {
|
| | | public Map<String, Object> call() throws Exception {
|
| | | return getMonitorPointOrDeviceAvgData4Compare(map);
|
| | | }
|
| | | });
|
| | |
| | | for (Map<String, Object> map : list) {
|
| | | dataList.add(cs.take().get());
|
| | | }
|
| | | Map[] maps = new HashMap[list.size()];
|
| | | Object[] datas = new Object[list.size()];
|
| | | Object[] deviceCounts = new Object[list.size()];
|
| | | Object[] alarmDatas = new Object[list.size()];
|
| | | 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", "")));
|
| | | }
|
| | | });
|
| | | Map<String, Double> sortMap = new HashMap<String, Double>();
|
| | | for (Object object : dataList) {
|
| | | Map<String, Object> map = (Map<String, Object>)object;
|
| | | for (String key : map.keySet()) {
|
| | | if (key.startsWith("data")) {
|
| | | int index = Integer.valueOf(key.replace("data", ""));
|
| | | maps[index] = map;
|
| | | }
|
| | | if (key.startsWith("sensors")) {
|
| | | sensors.addAll((List<String>) map.get(key));
|
| | | int index = Integer.valueOf(key.substring(key.length() - 1));
|
| | | String actual = key.substring(0, key.length() - 1);
|
| | | Object obj = map.get(key);
|
| | | switch (actual) {
|
| | | case "data":
|
| | | datas[index] = obj;
|
| | | break;
|
| | | case "deviceCount":
|
| | | deviceCounts[index] = obj;
|
| | | break;
|
| | | case "alarmData":
|
| | | alarmDatas[index] = obj;
|
| | | if (!ObjectUtils.isEmpty(obj)) {
|
| | | Map<String, BigDecimal> mapData = (Map<String, BigDecimal>) obj;
|
| | | BigDecimal sum = mapData.remove("sum");
|
| | | for (Entry<String, BigDecimal> entry : mapData.entrySet()) {
|
| | | sortMap.put(entry.getKey() + "-" + index, new BigDecimal(100).multiply(entry.getValue())
|
| | | .divide(sum, 2, BigDecimal.ROUND_HALF_UP).doubleValue());
|
| | | }
|
| | | }
|
| | | break;
|
| | | case "sensors":
|
| | | sensors.addAll((List<String>) obj);
|
| | | break;
|
| | | }
|
| | | }
|
| | | }
|
| | | List<Map.Entry<String, Double>> sortList = new ArrayList<Map.Entry<String,Double>>(sortMap.entrySet());
|
| | | Collections.sort(sortList,new Comparator<Map.Entry<String,Double>>(){
|
| | | @Override
|
| | | public int compare(Entry<String, Double> o1, Entry<String, Double> o2) {
|
| | | if (o2.getValue().compareTo(o1.getValue()) == 0) {
|
| | | String[] key1 = o1.getKey().split("-");
|
| | | String[] key2 = o2.getKey().split("-");
|
| | | String sensor1 = key1[0].replace("e", "");
|
| | | String sensor2 = key2[0].replace("e", "");
|
| | | if (Integer.valueOf(sensor1).compareTo(Integer.valueOf(sensor2)) == 0) {
|
| | | return Integer.compare(Integer.valueOf(key1[1]), Integer.valueOf(key2[1]));
|
| | | }
|
| | | return Integer.valueOf(sensor1).compareTo(Integer.valueOf(sensor2));
|
| | | }
|
| | | return o2.getValue().compareTo(o1.getValue());
|
| | | }
|
| | | |
| | | });
|
| | | resultMap.put("times", timeList);
|
| | | resultMap.put("datas", Arrays.asList(maps));
|
| | | resultMap.put("datas", Arrays.asList(datas));
|
| | | resultMap.put("deviceCounts", Arrays.asList(deviceCounts));
|
| | | resultMap.put("alarmDatas", Arrays.asList(alarmDatas));
|
| | | resultMap.put("sensors", new ArrayList<Object>(sensors));
|
| | | resultMap.put("sortList", sortList);
|
| | | return resultMap;
|
| | | }
|
| | |
|
| | |
|
| | | public Map<String, List<Object>> getMonitorPointOrDeviceAvgData4Compare(Map<String, Object> parameters) throws Exception {
|
| | | Map<String, List<Object>> resultMap = new HashMap<String, List<Object>>();
|
| | |
|
| | | public Map<String, Object> getMonitorPointOrDeviceAvgData4Compare(Map<String, Object> parameters) throws Exception {
|
| | | Map<String, Object> resultMap = new HashMap<String, Object>();
|
| | | List<Map<String, Object>> resultList = getMonitorPointOrDeviceAvgData(parameters);
|
| | | |
| | | 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"));
|
| | | }
|
| | | List<String> sensors = (List<String>) parameters.get("sensors");
|
| | | String part = parameters.get("part").toString();
|
| | | Map<String, Double[]> doubleMap = new LinkedHashMap<String, Double[]>();
|
| | | for (Map<String, Object> map : resultList) {
|
| | | String time = map.get("time").toString();
|
| | |
| | | }
|
| | | doubles[index] = (Double) map.get(sensorKey);
|
| | | doubleMap.put(sensor, doubles);
|
| | | |
| | | }
|
| | | }
|
| | | }
|
| | | dataList.add(doubleMap);
|
| | | String part = parameters.get("part").toString();
|
| | | resultMap.put("data" + part, dataList);
|
| | | resultMap.put("sensors" + part, new ArrayList<Object>(sensors));
|
| | |
|
| | | Object deviceCount;
|
| | | if (parameters.containsKey("deviceCount")) {
|
| | | deviceCount = parameters.remove("deviceCount");
|
| | | } else {
|
| | | deviceCount = deviceMapper.getDeviceCountByRegion(parameters);
|
| | | }
|
| | |
|
| | | resultMap.put("deviceCount" + part, deviceCount);
|
| | | resultMap.put("data" + part, doubleMap);
|
| | | resultMap.put("sensors" + part, sensors);
|
| | | Object type = parameters.get("type");
|
| | | if ("year".equals(type) || "month".equals(type)) {
|
| | | parameters.put("sensorKeys", Arrays.asList("e1", "e2", "e10", "e11", "e15", "e16"));
|
| | | resultMap.put("alarmData" + part, alarmDailyMapper.getAlarmData(parameters).get(0));
|
| | | }
|
| | | return resultMap;
|
| | | }
|
| | |
|
| | | @Override
|
| | | public List<Map<String, Object>> getMonitorPointOrDeviceAvgData(Map<String, Object> parameters) throws Exception {
|
| | | List<Map<String, Object>> resultList ;
|
| | | convertQueryParam(parameters);
|
| | |
|
| | | return historyMinutelyMapper.getMonitorPointOrDeviceAvgData(parameters);
|
| | | }
|
| | |
|
| | | @Override
|
| | | public void convertQueryParam(Map<String, Object> parameters) throws ParseException {
|
| | | String type = (String) parameters.get("type");
|
| | | if (!parameters.containsKey("field")) {
|
| | | parameters.putAll(getElementByType(type));
|
| | | }
|
| | | String time = (String) parameters.get("time");
|
| | | String format = (String) parameters.get("format");
|
| | | Integer field = Integer.valueOf(parameters.get("field").toString());
|
| | | Date start = DateUtils.parseDate(time, format), end = null ;
|
| | | if (parameters.containsKey("timeb")) {
|
| | | end = DateUtils.parseDate((String)parameters.get("timeb"), format);
|
| | | } else {
|
| | | switch (type) {
|
| | | case "year":
|
| | | end = DateUtils.addYears(start, 1);
|
| | | break;
|
| | | case "month":
|
| | | end = DateUtils.addMonths(start, 1);
|
| | | break;
|
| | | case "day":
|
| | | end = DateUtils.addDays(start, 1);
|
| | | break;
|
| | | case "hour":
|
| | | end = DateUtils.addHours(start, 1);
|
| | | break;
|
| | | }
|
| | | |
| | | Calendar instance = Calendar.getInstance();
|
| | | instance.setTime(start);
|
| | | instance.add(field, 1);
|
| | | end = instance.getTime();
|
| | | }
|
| | | parameters.put("start", start);
|
| | | parameters.put("end", end);
|
| | |
|
| | | if (parameters.containsKey("sensorKey")) {
|
| | | String sensorKey = (String) parameters.get("sensorKey");
|
| | | String sensorKeyColumn = "AVG(json -> '$." + sensorKey + "[0]') " + sensorKey;
|
| | | parameters.put("sensorKeyColumn", sensorKeyColumn);
|
| | | }
|
| | |
|
| | | //sensorKeys.clear();
|
| | | // 监控点平均值
|
| | | if (!parameters.containsKey("mac")) {
|
| | | resultList = new ArrayList<Map<String,Object>>();
|
| | | Integer monitorPointId = Integer.valueOf(parameters.get("monitorPoint").toString());
|
| | | List<Map<String, Object>> deviceVersions = deviceMapper.getDeviceVersionIdByMonitorPoint(monitorPointId);
|
| | | ExecutorService threadPool = Executors.newCachedThreadPool();
|
| | | CompletionService<List<Map<String, Object>>> cs = new ExecutorCompletionService<List<Map<String, Object>>>(threadPool);
|
| | | List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
|
| | | for (Map<String, Object> map : deviceVersions) {
|
| | | cs.submit(new Callable<List<Map<String, Object>>>() {
|
| | | @Override
|
| | | public List<Map<String, Object>> call() throws Exception {
|
| | | Map<String, Object> parameter = new HashMap<String, Object>(parameters);
|
| | | Integer deviceVersionId = (Integer) map.get("deviceVersionId");
|
| | | if (!parameter.containsKey("sensorKeyColumn")) {
|
| | | List<Map<String, Object>> sensors = sensorMapper.getSensorsByDeviceVersionId(deviceVersionId);
|
| | | String sensorKeyColumn = getSensorKeyColumnBySensors(sensors, parameters);
|
| | | parameter.put("sensorKeyColumn", sensorKeyColumn);
|
| | | }
|
| | | List<String> macs = deviceMapper.getDeviceMacByMonitorPointAndDeviceVersion(monitorPointId, deviceVersionId);
|
| | | if (ObjectUtils.isEmpty(macs) || macs.contains("null") ) {
|
| | | return new ArrayList<Map<String,Object>>();
|
| | | }else {
|
| | | parameter.put("macs", macs);
|
| | | return historyMinutelyMapper.getMonitorPointOrDeviceAvgData(parameter);
|
| | | }
|
| | | }
|
| | | });
|
| | | }
|
| | | for (Map<String, Object> map : deviceVersions) {
|
| | | list.addAll(cs.take().get());
|
| | | }
|
| | | Map<String, Map<String, Number[]>> result = new LinkedHashMap<String, Map<String, Number[]>>();
|
| | | for (Map<String, Object> map : list) {
|
| | | time = (String) map.get("time");
|
| | | map.remove("time");
|
| | | Map<String, Number[]> times = new HashMap<String, Number[]>();
|
| | | if (result.containsKey(time)) {
|
| | | times = (Map<String, Number[]>) result.get(time);
|
| | | } |
| | | for (Map.Entry<String,Object> entry : map.entrySet()) {
|
| | | String sensorKey = entry.getKey();
|
| | | Number[] doubles = new Number[3];
|
| | | Double sum = (Double) entry.getValue();
|
| | | Integer count = 1;
|
| | | if (times.containsKey(sensorKey)) {
|
| | | doubles = times.get(sensorKey);
|
| | | sum = (Double) doubles[0] + sum;
|
| | | count = (Integer) doubles[1] + 1;
|
| | | } |
| | | doubles[0] = sum;
|
| | | doubles[1] = count;
|
| | | doubles[2] = new BigDecimal(sum / count).setScale(3, RoundingMode.HALF_UP).doubleValue();
|
| | | times.put(sensorKey, doubles);
|
| | | }
|
| | | result.put(time, times);
|
| | | }
|
| | | Iterator<Entry<String, Map<String, Number[]>>> iterator = result.entrySet().iterator();
|
| | | while(iterator.hasNext()){ |
| | | Entry<String, Map<String, Number[]>> entry = iterator.next();
|
| | | String resultTime = entry.getKey();
|
| | | Map<String, Number[]> value = entry.getValue();
|
| | | Map<String, Object> resultMap = new HashMap<String, Object>();
|
| | | resultMap.put("time", resultTime);
|
| | | for (String sensorKey : value.keySet()) {
|
| | | resultMap.put(sensorKey, value.get(sensorKey)[2]);
|
| | | }
|
| | | resultList.add(resultMap);
|
| | | }
|
| | | // 设备 平均值 |
| | | } else {
|
| | | // 所有传感器
|
| | | if (!parameters.containsKey("sensorKeyColumn")) {
|
| | | String mac = (String) parameters.get("mac");
|
| | | Device device = new Device();
|
| | | device.setMac(mac);
|
| | | device = deviceMapper.selectOne(device);
|
| | | List<Map<String, Object>> sensors = sensorMapper.getSensorsByDeviceVersionId(device.getDeviceVersionId());
|
| | | String sensorKeyColumn = getSensorKeyColumnBySensors(sensors,parameters);
|
| | | parameters.put("sensorKeyColumn", sensorKeyColumn);
|
| | | |
| | | }
|
| | | resultList = historyMinutelyMapper.getMonitorPointOrDeviceAvgData(parameters);
|
| | | }
|
| | | return resultList;
|
| | | }
|
| | |
|
| | | private String getSensorKeyColumnBySensors(List<Map<String, Object>> sensors,Map<String, Object> parameters) {
|
| | | String sensorKeyColumn = "";
|
| | | Set<String> sensorKeys = new HashSet<String>();
|
| | | List<String> sensorKeys = new ArrayList<String>();
|
| | | List<String> sensors = new ArrayList<String>();
|
| | | if (parameters.containsKey("sensors")) {
|
| | | sensorKeys = (Set<String>) parameters.get("sensors");
|
| | | try {
|
| | | sensors = JSON.parseObject((String)parameters.get("sensors"), new TypeReference<List<String>>() {});
|
| | | for (String sensor : sensors) {
|
| | | sensorKeys.add(sensor.split("-")[0]);
|
| | | }
|
| | | } catch (Exception e) {
|
| | | sensorKeys = sensors = (List<String>)parameters.remove("sensors");
|
| | | }
|
| | | } else {
|
| | | List<Sensor> sensorList = sensorMapper.getSensorsByCriteria(parameters);
|
| | | for (Sensor sensor : sensorList) {
|
| | | sensorKeys.add(sensor.getSensorKey());
|
| | | String string = sensor.getSensorKey() + "-" + sensor.getName() + "-" + sensor.getUnit();
|
| | | if (parameters.containsKey("description")) {
|
| | | string += "-" + sensor.getDescription();
|
| | | }
|
| | | sensors.add(string);
|
| | | }
|
| | | }
|
| | | 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 + "-" + unit );
|
| | | parameters.put("sensors", sensorKeys);
|
| | | }
|
| | | return sensorKeyColumn;
|
| | | parameters.put("sensorKeys", sensorKeys);
|
| | | parameters.put("sensors", sensors);
|
| | | }
|
| | |
|
| | | @Override
|
| | |
| | | }
|
| | | parameters.put("start", start);
|
| | | parameters.put("end", end);
|
| | | String sensorKey = (String) parameters.get("macKey");
|
| | | String sensorKeyColumn = "AVG(json -> '$." + sensorKey + "[0]') " + sensorKey;
|
| | | parameters.put("sensorKeyColumn", sensorKeyColumn);
|
| | | String sensorKey = (String) parameters.remove("macKey");
|
| | | List<String> sensorKeys = new ArrayList<String>();
|
| | | sensorKeys.add(sensorKey);
|
| | | parameters.put("sensorKeys", sensorKeys);
|
| | |
|
| | | Map<String, Double> average = historyMinutelyMapper.getSersionAvgByDevice(parameters);
|
| | | if (isEmpty(average)) {
|
| | |
| | | * @return
|
| | | */
|
| | | @Override
|
| | | public List<List<PairData>> queryLineChartDateByCrieria(LineChartCriteria lineChartCriteria){
|
| | | String sensorKey = lineChartCriteria.getSensorKey();
|
| | | TimePeriod timePeriod = lineChartCriteria.getTimePeriod();
|
| | | 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();
|
| | | List<List<PairData>> list = new ArrayList<>();
|
| | | dataConditionList.forEach(item -> {
|
| | | List<PairData> pairDataList = queryOneLineChartDateByCrieria(sensorKey,timePeriod,item);
|
| | | list.add(pairDataList);
|
| | | TimePeriod timePeriod = lineChartCriteria.getTimePeriod();
|
| | | sensorKeys.forEach(sensorKey -> {
|
| | | listMap.put(sensorKey,new ArrayList<List<Double>>(dataConditionList.size()));
|
| | | });
|
| | | return list;
|
| | | 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 sensorKey
|
| | | * 根据线性表单的条件规则,获取一条线性表单数据,包含 所有检测项目
|
| | | * @param sensorKeys
|
| | | * @param timePeriod
|
| | | * @param dataCondition
|
| | | * @return
|
| | | */
|
| | | public List<PairData> queryOneLineChartDateByCrieria(String sensorKey, TimePeriod timePeriod, DataCondition dataCondition){
|
| | | public Map<String,List<Double>> queryOneLineChartDateByCrieria(List<String> sensorKeys, TimePeriod timePeriod, DataCondition dataCondition){
|
| | | List<String> timeList = ReportTimeFormat.makeTimeList(timePeriod);
|
| | | List<PairData> lineChartDatas = historyMinutelyMapper.selectLineChartDateByCrieria(sensorKey,timePeriod,dataCondition);
|
| | | List<PairData> lineChartDatasWithEmpty = new ArrayList<>();
|
| | | 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){
|
| | | if(m >-1){
|
| | | for(int n =0;n<timeList.size();n++){
|
| | | String time = timeList.get(n);
|
| | | if(m>-1){
|
| | | PairData pairData = lineChartDatas.get(m);
|
| | | String keyTime = pairData.getCategory();
|
| | | if(time.equals(keyTime)){
|
| | | lineChartDatasWithEmpty.add(pairData);
|
| | | m = m<dataLength ? m+1 : -1;
|
| | | }else{
|
| | | lineChartDatasWithEmpty.add(generateEmptyData(time));
|
| | | }
|
| | | }else {
|
| | | lineChartDatasWithEmpty.add(generateEmptyData(time));
|
| | | }
|
| | |
|
| | | 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 -> {
|
| | | Object value = rowData.get(sensorKey);
|
| | | List<Double> lineChartDatasWithEmptyTemp = lineChartDatasWithEmpty.get(sensorKey);
|
| | | if(finalN < lineChartDatasWithEmptyTemp.size()){
|
| | | Double sensorValue = value!= null?new Double(value.toString()):null;
|
| | | lineChartDatasWithEmptyTemp.set(finalN,sensorValue);
|
| | | }
|
| | | });
|
| | | // 置为 -1,防止越界
|
| | | m = m<dataLength ? m+1 : -1;
|
| | | }
|
| | | }
|
| | | }
|
| | | }else{
|
| | | fillEmptyDataToList(timeList,lineChartDatasWithEmpty);
|
| | | }
|
| | | return lineChartDatasWithEmpty;
|
| | | }
|
| | | private PairData generateEmptyData(String time) {
|
| | | PairData pairData = new PairData();
|
| | | pairData.setCategory(time);
|
| | | pairData.setValue(null);
|
| | | return pairData;
|
| | | }
|
| | | private void fillEmptyDataToList(List<String> timeList,List<PairData> lineChartDatasWithEmpty){
|
| | | timeList.forEach( time ->{
|
| | | PairData pairData = generateEmptyData(time);
|
| | | lineChartDatasWithEmpty.add(pairData);
|
| | | });
|
| | | |
| | | private Map<String, Object> getElementByType(Object type){
|
| | | Map<String, Object> resultMap = new HashMap<String, Object>();
|
| | | switch (type.toString()) {
|
| | | case "year":
|
| | | resultMap.put("format", "yyyy");
|
| | | resultMap.put("typeFormat", "%Y-%m");
|
| | | resultMap.put("timeLength", 12);
|
| | | resultMap.put("field", Calendar.YEAR);
|
| | | break;
|
| | | case "month":
|
| | | resultMap.put("format", "yyyy-MM");
|
| | | resultMap.put("typeFormat", "%Y-%m-%d");
|
| | | resultMap.put("timeLength", 28);
|
| | | resultMap.put("field", Calendar.MONTH);
|
| | | break;
|
| | | case "day":
|
| | | resultMap.put("format", "yyyy-MM-dd");
|
| | | resultMap.put("typeFormat", "%Y-%m-%d %H");
|
| | | resultMap.put("timeLength", 24);
|
| | | resultMap.put("field", Calendar.DATE);
|
| | | break;
|
| | | case "hour":
|
| | | resultMap.put("format", "yyyy-MM-dd HH");
|
| | | resultMap.put("typeFormat", "%Y-%m-%d %H:%i");
|
| | | resultMap.put("timeLength", 60);
|
| | | resultMap.put("field", Calendar.HOUR);
|
| | | break;
|
| | | }
|
| | | return resultMap;
|
| | | }
|
| | | }
|