| | |
| | | package com.moral.service.impl;
|
| | |
|
| | | import static com.moral.common.bean.Constants.NULL_VALUE;
|
| | | 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.truncate;
|
| | | import static org.springframework.util.ObjectUtils.isEmpty;
|
| | |
|
| | | import java.util.Calendar;
|
| | | import java.util.Date;
|
| | | import java.util.HashMap;
|
| | | import java.util.LinkedHashMap;
|
| | | import java.util.List;
|
| | | import java.util.Map;
|
| | | import java.time.LocalDateTime;
|
| | | import java.util.*;
|
| | |
|
| | | import javax.annotation.Resource;
|
| | |
|
| | | 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 com.mongodb.BasicDBObject;
|
| | | import com.moral.common.util.ValidateUtil;
|
| | | import com.moral.entity.Sensor;
|
| | | import com.moral.mapper.DeviceMapper;
|
| | | import com.moral.mapper.HistoryMapper;
|
| | | import com.moral.mapper.SensorMapper;
|
| | | import com.moral.service.AccountService;
|
| | | import com.moral.service.HistoryService;
|
| | |
|
| | |
| | | private HistoryMapper historyMapper;
|
| | |
|
| | | @Resource
|
| | | private MongoTemplate mongoTemplate;
|
| | | private DeviceMapper deviceMapper;
|
| | |
|
| | | @Resource
|
| | | private SensorMapper sensorMapper;
|
| | |
|
| | | @Override
|
| | | public Map<String, Object> getAllSensorAverageByDevice(Map<String, Object> parameters) {
|
| | | public Map<String, Object> getAllSensorAverageByDevice(Map<String, Object> parameters) throws Exception {
|
| | | ValidateUtil.notNull(parameters.get("areaCode"), "param.is.null");
|
| | | ValidateUtil.notNull(parameters.get("accountId"), "param.is.null");
|
| | | Map<String,Object> resultMap = new LinkedHashMap<String, Object>();
|
| | | accountService.setOrgIdsByAccount(parameters);
|
| | | Date date = new Date();
|
| | | LocalDateTime time = LocalDateTime.now();
|
| | | // 当前时间 -10分钟
|
| | | parameters.put("start", addMinutes(date, -10));
|
| | | parameters.put("start", time.minusMinutes(10));
|
| | | // 当前时间 -5分钟
|
| | | 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;
|
| | | }
|
| | | parameters.put("end", time.minusMinutes(5));
|
| | | List<Sensor> Sensors = sensorMapper.getSensorsByCriteria(parameters);
|
| | | List<String> sensorKeys = new ArrayList<String>();
|
| | | for (Sensor sensor : Sensors) {
|
| | | sensorKeys.add(sensor.getSensorKey());
|
| | | }
|
| | | parameters.put("queryColumns", queryColumns);
|
| | | parameters.put("macKey", "all");
|
| | | parameters.put("sensorKeys", sensorKeys);
|
| | | List<Map<String, Object>> list = historyMapper.getAreaAllDataByAccount(parameters);
|
| | |
|
| | | return list.get(0);
|
| | | resultMap.putAll(list.get(0));
|
| | | return resultMap;
|
| | | }
|
| | |
|
| | | @Override
|
| | | public Map<String, Object> getDeviceRankingBySensorAverage(Map<String, Object> parameters) {
|
| | | ValidateUtil.notNull(parameters.get("areaCode"), "param.is.null");
|
| | | ValidateUtil.notNull(parameters.get("accountId"), "param.is.null");
|
| | | ValidateUtil.notNull(parameters.get("macKey"), "param.is.null");
|
| | | Object macKey = parameters.get("macKey");
|
| | | ValidateUtil.notNull(macKey, "param.is.null");
|
| | |
|
| | | Map<String, Object> result = new LinkedHashMap<String, Object>();
|
| | | accountService.setOrgIdsByAccount(parameters);
|
| | | Date date = new Date();
|
| | | LocalDateTime time = LocalDateTime.now();
|
| | | // 当前时间 -1小时
|
| | | parameters.put("start", addHours(date, -1));
|
| | | parameters.put("end", date);
|
| | | parameters.put("macKey", "'$." + parameters.get("macKey") + "'");
|
| | | parameters.put("start", time.minusHours(1));
|
| | | parameters.put("end", time);
|
| | | List<String> sensorKeys = new ArrayList<String>();
|
| | | sensorKeys.add(macKey.toString());
|
| | | parameters.put("sensorKeys", sensorKeys);
|
| | | List<Map<String, Object>> list = historyMapper.getAreaAllDataByAccount(parameters);
|
| | |
|
| | | for (Map<String, Object> map : list) {
|
| | | result.put((String) map.get("name"), map.get("avg"));
|
| | | result.put((String) map.get("name"), map.get(macKey.toString()));
|
| | | }
|
| | | return result;
|
| | | }
|
| | |
|
| | | @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();
|
| | | Long end = truncate(date, Calendar.DATE).getTime(), start;
|
| | | // 每月一日的数据取上月的数据
|
| | | if (1 == Calendar.getInstance().get(Calendar.DAY_OF_MONTH)) {
|
| | | // 上个月1日00:00:00
|
| | | start = truncate(addMonths(date, -1), Calendar.MONTH).getTime();
|
| | | } else {
|
| | | // 这个月1日00:00:00
|
| | | start = truncate(date, Calendar.MONTH).getTime();
|
| | | }
|
| | | Aggregation aggregation = Aggregation.newAggregation(
|
| | | Aggregation.match(Criteria.where("mac").is(parameters.get("mac"))),
|
| | | Aggregation.match(Criteria.where("time").gte(start)), Aggregation.match(Criteria.where("time").lt(end)),
|
| | | Aggregation.group("mac").avg((String) parameters.get("macKey")).as("average"));
|
| | | AggregationResults<BasicDBObject> results = mongoTemplate.aggregate(aggregation, "data", BasicDBObject.class);
|
| | | List<BasicDBObject> list = results.getMappedResults();
|
| | | if (isEmpty(list)) {
|
| | | result.put("average", NULL_VALUE);
|
| | | } else {
|
| | | result = list.get(0);
|
| | | result.put("average", String.format("%.2f", result.get("average")));
|
| | | }
|
| | | return result;
|
| | | public String queryValueByMacAndTime(String mac, Date time){
|
| | | return historyMapper.selectValueByMacAndTime(mac, time);
|
| | | }
|
| | |
|
| | | |
| | |
|
| | | }
|