|  |  |  | 
|---|
|  |  |  | package com.moral.service.impl; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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.stereotype.Service; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | 
|---|
|  |  |  | 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>(); | 
|---|
|  |  |  | Map<String,Object> resultMap = new LinkedHashMap<String, Object>(); | 
|---|
|  |  |  | accountService.setOrgIdsByAccount(parameters); | 
|---|
|  |  |  | LocalDateTime time = LocalDateTime.now(); | 
|---|
|  |  |  | // 当前时间 -10分钟 | 
|---|
|  |  |  | parameters.put("start", time.minusMinutes(10)); | 
|---|
|  |  |  | // 当前时间 -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<Sensor> Sensors = sensorMapper.getSensorsByCriteria(parameters); | 
|---|
|  |  |  | List<String> sensorKeys = new ArrayList<String>(); | 
|---|
|  |  |  | for (Sensor sensor : Sensors) { | 
|---|
|  |  |  | sensorKeys.add(sensor.getSensorKey()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 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) { | 
|---|
|  |  |  | queryColumns += "AVG(value -> '$.e" + i + "') e" + i; | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | queryColumns += " , AVG(value -> '$.e" + i + "') e" + i; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 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); | 
|---|
|  |  |  | 
|---|
|  |  |  | // 当前时间 -1小时 | 
|---|
|  |  |  | parameters.put("start", time.minusHours(1)); | 
|---|
|  |  |  | parameters.put("end", time); | 
|---|
|  |  |  | parameters.put("macKey", "'$." + parameters.get("macKey") + "'"); | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | } | 
|---|