| | |
| | | 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.math.BigDecimal;
|
| | | import java.math.RoundingMode;
|
| | | import java.time.LocalDateTime;
|
| | | import java.util.ArrayList;
|
| | | import java.util.Collection;
|
| | | import java.util.HashMap;
|
| | | import java.util.LinkedHashMap;
|
| | | import java.util.List;
|
| | | import java.util.Map;
|
| | | import java.util.concurrent.Callable;
|
| | | import java.util.concurrent.CompletionService;
|
| | | import java.util.concurrent.ExecutorCompletionService;
|
| | | import java.util.concurrent.ExecutorService;
|
| | | import java.util.concurrent.Executors;
|
| | |
|
| | | 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.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, Number[]> result = new HashMap<String, Number[]>();
|
| | | Map<String,Object> resultMap = new HashMap<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));
|
| | | parameters.put("end", time.minusMinutes(5));
|
| | | parameters.put("macKey", "all");
|
| | | List<Integer> deviceVersionIds = deviceMapper.getDeviceVersionIdByAreaCode(parameters);
|
| | | ExecutorService threadPool = Executors.newCachedThreadPool();
|
| | | CompletionService<Map<String, Object>> cs = new ExecutorCompletionService<Map<String, Object>>(threadPool);
|
| | | for (Integer deviceVersionId : deviceVersionIds) {
|
| | | Map<String, Object> parameter = new HashMap<String, Object>(parameters);
|
| | | String queryColumns = "";
|
| | | List<Map<String, Object>> sensors = sensorMapper.getSensorsByDeviceVersionId(deviceVersionId);
|
| | | for (int i = 0; i < sensors.size(); i++) {
|
| | | String sensorKey = (String) sensors.get(i).get("sensor_key");
|
| | | queryColumns += "AVG(value -> '$." + sensorKey + "') " + sensorKey;
|
| | | if (i != sensors.size() - 1) {
|
| | | queryColumns += " ,";
|
| | | } |
| | | }
|
| | | parameter.put("queryColumns", queryColumns);
|
| | | parameter.put("deviceVersionId", deviceVersionId);
|
| | | cs.submit(new Callable<Map<String, Object>>() {
|
| | | @Override
|
| | | public Map<String, Object> call() throws Exception {
|
| | | Map<String, Object> map = new HashMap<String, Object>();
|
| | | List<Map<String, Object>> list = historyMapper.getAreaAllDataByAccount(parameter);
|
| | | map.put(deviceVersionId.toString(), list.get(0));
|
| | | return map;
|
| | | }
|
| | | });
|
| | | }
|
| | | List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
|
| | | for (Integer deviceVersionId : deviceVersionIds) {
|
| | | list.add(cs.take().get());
|
| | | }
|
| | | |
| | | for (Map<String, Object> map : list) {
|
| | | Collection<Object> values = map.values();
|
| | | for (Object object : values) {
|
| | | Map<String, Object> result1 = (Map<String, Object>)object;
|
| | | for (String key : result1.keySet()) {
|
| | | Number[] numbers = new Number[2];
|
| | | Double sum = (Double) result1.get(key);
|
| | | Integer count = 1;
|
| | | if (result.containsKey(key)) {
|
| | | numbers = result.get(key);
|
| | | sum += (Double)numbers[0];
|
| | | count += (Integer)numbers[1];
|
| | | }
|
| | | numbers[0] = sum;
|
| | | numbers[1] = count;
|
| | | result.put(key, numbers);
|
| | | resultMap.put(key, new BigDecimal(sum / count).setScale(3, RoundingMode.HALF_UP).doubleValue());
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | | return resultMap;
|
| | | /*Date date = new Date();
|
| | | // 当前时间 -10分钟
|
| | | parameters.put("start", DateUtils.addMinutes(date, -10));
|
| | | // 当前时间 -5分钟
|
| | | parameters.put("end", DateUtils.addMinutes(date, -5));
|
| | | String queryColumns = "";
|
| | | for (int i = 1; i < 20; i++) {
|
| | | if (i == 1) {
|
| | |
| | | parameters.put("macKey", "all");
|
| | | List<Map<String, Object>> list = historyMapper.getAreaAllDataByAccount(parameters);
|
| | |
|
| | | return list.get(0);
|
| | | return list.get(0);*/
|
| | |
|
| | | }
|
| | |
|
| | | @Override
|
| | |
| | |
|
| | | 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("start", time.minusHours(1));
|
| | | parameters.put("end", time);
|
| | | parameters.put("macKey", "'$." + parameters.get("macKey") + "'");
|
| | | List<Map<String, Object>> list = historyMapper.getAreaAllDataByAccount(parameters);
|
| | |
|
| | |
| | | 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;
|
| | | }
|
| | |
|
| | | |
| | |
|
| | | }
|