| | |
| | | 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.addMonths;
|
| | | import static org.apache.commons.lang3.time.DateUtils.truncate;
|
| | | import static org.springframework.util.ObjectUtils.isEmpty;
|
| | |
|
| | |
| | | import javax.annotation.Resource;
|
| | |
|
| | | import org.apache.commons.lang3.time.DateUtils;
|
| | | 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.util.CalculateUtils;
|
| | | import com.moral.common.util.ResourceUtil;
|
| | | import com.moral.common.util.ValidateUtil;
|
| | |
| | | parameters.put("start", truncate(addDays(date, -1), Calendar.DATE));
|
| | | // 今日00:00:00
|
| | | parameters.put("end", truncate(date, Calendar.DATE));
|
| | | Map<String, Double> average = historyMinutelyMapper.getDayAQIByDevice(parameters);
|
| | | 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);
|
| | | Map<String, Double> average = historyMinutelyMapper.getSersionAvgByDevice(parameters);
|
| | | return getAQIByDataMap(average);
|
| | | }
|
| | |
|
| | |
| | | parameters.put("sensorKeyColumn", sensorKeyColumn);
|
| | | }
|
| | |
|
| | | sensorKeys.clear();
|
| | | // 监控点平均值
|
| | | if (!parameters.containsKey("mac")) {
|
| | | resul = new ArrayList<Map<String,Object>>();
|
| | | Integer monitorPointId = Integer.valueOf((String) parameters.get("monitorPoint"));
|
| | |
| | | }
|
| | | resul.add(resultMap);
|
| | | }
|
| | | |
| | | // 设备 平均值 |
| | | } else {
|
| | | // 所有传感器
|
| | | if (!parameters.containsKey("sensorKeyColumn")) {
|
| | | String mac = (String) parameters.get("mac");
|
| | | Device device = new Device();
|
| | |
| | |
|
| | | private String getSensorKeyColumnBySensors(List<Map<String, Object>> sensors) {
|
| | | String sensorKeyColumn = "";
|
| | | |
| | | 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");
|
| | | if (i == sensors.size() - 1) {
|
| | | sensorKeyColumn += "AVG(json -> '$." + sensorKey + "[0]') " + sensorKey;
|
| | | } else {
|
| | | sensorKeyColumn += "AVG(json -> '$." + sensorKey + "[0]') " + sensorKey + ",";
|
| | | if (i != sensors.size() - 1) {
|
| | | sensorKeyColumn += " ,";
|
| | | }
|
| | | sensorKeys.add(sensorKey + "-" + name);
|
| | | }
|
| | | return sensorKeyColumn;
|
| | | }
|
| | |
|
| | | @Override
|
| | | public Map<String, Object> getMonthAverageBySensor(Map<String, Object> parameters) {
|
| | | ValidateUtil.notNull(parameters.get("mac"), "param.is.null");
|
| | | ValidateUtil.notNull(parameters.get("macKey"), "param.is.null");
|
| | | Map<String, Object> result = new HashMap<String, Object>();
|
| | | Date date = new Date();
|
| | | Date end = truncate(date, Calendar.DATE), start;
|
| | | // 每月一日的数据取上月的数据
|
| | | if (1 == Calendar.getInstance().get(Calendar.DAY_OF_MONTH)) {
|
| | | // 上个月1日00:00:00
|
| | | start = truncate(addMonths(date, -1), Calendar.MONTH);
|
| | | } else {
|
| | | // 这个月1日00:00:00
|
| | | start = truncate(date, Calendar.MONTH);
|
| | | }
|
| | | 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);
|
| | |
|
| | | Map<String, Double> average = historyMinutelyMapper.getSersionAvgByDevice(parameters);
|
| | | if (isEmpty(average)) {
|
| | | result.put("average", NULL_VALUE);
|
| | | } else {
|
| | | result.put("average", String.format("%.2f", average.get(sensorKey)));
|
| | | }
|
| | | return result;
|
| | | }
|
| | |
|
| | | }
|