| | |
| | | package com.moral.service.impl;
|
| | |
|
| | | import java.util.Calendar;
|
| | | import java.util.Collections;
|
| | | import java.util.Date;
|
| | | import java.util.HashMap;
|
| | | import java.util.HashSet;
|
| | | import java.util.LinkedHashMap;
|
| | | import java.util.List;
|
| | | import java.util.Map;
|
| | | import java.util.Set;
|
| | | import java.time.LocalDateTime;
|
| | | import java.util.*;
|
| | |
|
| | | import org.apache.commons.lang3.time.DateUtils;
|
| | | import org.springframework.beans.factory.annotation.Autowired;
|
| | | 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 javax.annotation.Resource;
|
| | |
|
| | | import org.springframework.stereotype.Service;
|
| | | import org.springframework.util.ObjectUtils;
|
| | |
|
| | | import com.mongodb.BasicDBObject;
|
| | | import com.moral.common.util.ValidateUtil;
|
| | | import com.moral.entity.Profession;
|
| | | import com.moral.entity.Sensor;
|
| | | import com.moral.mapper.DeviceMapper;
|
| | | import com.moral.mapper.HistoryMapper;
|
| | | import com.moral.mapper.ProfessionMapper;
|
| | | import com.moral.mapper.SensorMapper;
|
| | | import com.moral.service.AccountService;
|
| | | import com.moral.service.HistoryService;
|
| | | import com.moral.util.CalculateUtils;
|
| | | import com.moral.util.Constants;
|
| | | import com.moral.util.ResourceUtil;
|
| | |
|
| | | @Service
|
| | | public class HistoryServiceImpl implements HistoryService {
|
| | |
|
| | | @Autowired
|
| | | @Resource
|
| | | private AccountService accountService;
|
| | |
|
| | | @Autowired
|
| | | @Resource
|
| | | private HistoryMapper historyMapper;
|
| | |
|
| | | @Autowired
|
| | | private MongoTemplate mongoTemplate;
|
| | | @Resource
|
| | | private DeviceMapper deviceMapper;
|
| | |
|
| | | @Resource
|
| | | private SensorMapper sensorMapper;
|
| | |
|
| | | @Resource
|
| | | private ProfessionMapper professionMapper;
|
| | |
|
| | | @Override
|
| | | public Map<String, Object> getAverageByAll(Map<String, Object> parameters) {
|
| | | Map<String, Object> result = new LinkedHashMap<String, Object>();
|
| | |
|
| | | 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);
|
| | | parameters.put("macKey", "all");
|
| | | Date date = new Date();
|
| | | LocalDateTime time = LocalDateTime.now();
|
| | | // 当前时间 -10分钟
|
| | | parameters.put("start", DateUtils.addMinutes(date, -10));
|
| | | parameters.put("start", time.minusMinutes(10));
|
| | | // 当前时间 -5分钟
|
| | | parameters.put("end", DateUtils.addMinutes(date, -5));
|
| | | List<Map<String, Object>> averageByAll = historyMapper.getAverageByAll(parameters);
|
| | |
|
| | | for (Map<String, Object> map : averageByAll) {
|
| | | result.put((String) map.get("mac_key"), map.get("avg"));
|
| | | 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());
|
| | | }
|
| | | return result;
|
| | | }
|
| | |
|
| | | @Override
|
| | | public Map<String, Object> getAverageBySensor(Map<String, Object> parameters) {
|
| | | Map<String, Object> result = new LinkedHashMap<String, Object>();
|
| | |
|
| | | accountService.setOrgIdsByAccount(parameters);
|
| | | Date date = new Date();
|
| | | // 当前时间 -1小时
|
| | | parameters.put("start", DateUtils.addHours(date, -1));
|
| | | parameters.put("end", date);
|
| | | List<Map<String, Object>> averageByAll = historyMapper.getAverageByAll(parameters);
|
| | |
|
| | | for (Map<String, Object> map : averageByAll) {
|
| | | result.put((String) map.get("name"), map.get("avg"));
|
| | | }
|
| | | return result;
|
| | | }
|
| | |
|
| | | @Override
|
| | | public Map<String, Object> getDayAQIByDevice(Map<String, Object> parameters) {
|
| | | Map<String, Object> resultMap = new HashMap<String, Object>();
|
| | | Date date = new Date();
|
| | | // 昨日00:00:00
|
| | | parameters.put("start", DateUtils.truncate(DateUtils.addDays(date, -1), Calendar.DATE));
|
| | | // 今日00:00:00
|
| | | parameters.put("end", DateUtils.truncate(date, Calendar.DATE));
|
| | | String[] IAQIValues = ResourceUtil.getArrValue("IAQI");
|
| | | Map<String, Double> average = historyMapper.getDayAQIByDevice(parameters);
|
| | | if (ObjectUtils.isEmpty(average)) {
|
| | | resultMap.put("AQI", Constants.NULL_VALUE);
|
| | | } else {
|
| | | Set<Double> IAQIs = new HashSet<Double>();
|
| | | for (Map.Entry<String, Double> entry : average.entrySet()) {
|
| | | double minMacKey = 0, maxMacKey = 0, minIAQI = 0, maxIAQI = 0;
|
| | | String[] macKeyValues = ResourceUtil.getArrValue(entry.getKey());
|
| | | Double avg = entry.getValue();
|
| | | if (ObjectUtils.isEmpty(avg)) {
|
| | | IAQIs.add(null);
|
| | | } else {
|
| | | int index = -1;
|
| | | for (int i = 0; i < macKeyValues.length; i++) {
|
| | | if (avg <= Double.valueOf(macKeyValues[i])) {
|
| | | if (i == 0) {
|
| | | index = i;
|
| | | } else {
|
| | | index = i - 1;
|
| | | }
|
| | | break;
|
| | | }
|
| | | }
|
| | | if (index == -1) {
|
| | | IAQIs.add(Double.MAX_VALUE);
|
| | | } else {
|
| | | minMacKey = Double.valueOf(macKeyValues[index]);
|
| | | maxMacKey = Double.valueOf(macKeyValues[index + 1]);
|
| | | minIAQI = Double.valueOf(IAQIValues[index]);
|
| | | maxIAQI = Double.valueOf(IAQIValues[index + 1]);
|
| | | Double result = CalculateUtils.calculateIAQI(maxIAQI, minIAQI, maxMacKey, minMacKey, avg);
|
| | | IAQIs.add(result);
|
| | | }
|
| | | }
|
| | | }
|
| | | IAQIs.remove(null);
|
| | | if (ObjectUtils.isEmpty(IAQIs)) {
|
| | | resultMap.put("AQI", Constants.NULL_VALUE);
|
| | | } else {
|
| | | Double AQI = Collections.max(IAQIs);
|
| | | if (AQI == Double.MAX_VALUE) {
|
| | | resultMap.put("AQI", IAQIValues[IAQIValues.length - 1]);
|
| | | } else {
|
| | | resultMap.put("AQI", String.format("%.0f", AQI));
|
| | | }
|
| | | }
|
| | | }
|
| | | parameters.put("sensorKeys", sensorKeys);
|
| | | List<Map<String, Object>> list = historyMapper.getAreaAllDataByAccount(parameters);
|
| | | resultMap.putAll(list.get(0));
|
| | | return resultMap;
|
| | | }
|
| | |
|
| | | @Override
|
| | | public Map<String, Object> getMonthAverageBySensor(Map<String, Object> parameters) {
|
| | | Map<String, Object> result = new HashMap<String, Object>();
|
| | | Date date = new Date();
|
| | | Long end = DateUtils.truncate(date, Calendar.DATE).getTime(),start;
|
| | | // 每月一日的数据取上月的数据
|
| | | if (1 == Calendar.getInstance().get(Calendar.DAY_OF_MONTH)) {
|
| | | // 上个月1日00:00:00
|
| | | start = DateUtils.truncate(DateUtils.addMonths(date, -1), Calendar.MONTH).getTime();
|
| | | } else {
|
| | | // 这个月1日00:00:00
|
| | | start = DateUtils.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 (ObjectUtils.isEmpty(list)) {
|
| | | result.put("average", Constants.NULL_VALUE);
|
| | | } else {
|
| | | result = list.get(0);
|
| | | result.put("average", String.format("%.2f", result.get("average")));
|
| | | 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");
|
| | | Object macKey = parameters.get("macKey");
|
| | | ValidateUtil.notNull(macKey, "param.is.null");
|
| | |
|
| | | Map<String, Object> result = new LinkedHashMap<String, Object>();
|
| | | accountService.setOrgIdsByAccount(parameters);
|
| | | LocalDateTime time = LocalDateTime.now();
|
| | | // 当前时间 -1小时
|
| | | 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(macKey.toString()));
|
| | | }
|
| | | return result;
|
| | | }
|
| | | @Override
|
| | | public String queryValueByMacAndTime(String mac, Date time){
|
| | | return historyMapper.selectValueByMacAndTime(mac, time);
|
| | | }
|
| | |
|
| | | @Override
|
| | | public List<Map<String, Object>> getRegionRankingData(Map<String, Object> parameters) {
|
| | | ValidateUtil.notNull(parameters.get("regionCode"), "param.is.null");
|
| | | ValidateUtil.notNull(parameters.get("dimension"), "param.is.null");
|
| | | ValidateUtil.notNull(parameters.get("sensorKey"), "param.is.null");
|
| | | ValidateUtil.notNull(parameters.get("accountId"), "param.is.null");
|
| | |
|
| | | String regionCode = parameters.get("regionCode").toString();
|
| | | String regionType = "village_code";
|
| | | String nextLevel = "";
|
| | | if (regionCode.length() == 6) {
|
| | | if (regionCode.endsWith("0000")) {
|
| | | regionType = "province_code";
|
| | | nextLevel = "city";
|
| | | } else if (regionCode.endsWith("00")) {
|
| | | regionType = "city_code";
|
| | | nextLevel = "area";
|
| | | } else {
|
| | | regionType = "area_code";
|
| | | nextLevel = "town";
|
| | | }
|
| | | } else {
|
| | | if (regionCode.endsWith("000")) {
|
| | | regionType = "town_code";
|
| | | nextLevel = "village";
|
| | | }
|
| | | }
|
| | | parameters.put("regionType", regionType);
|
| | | parameters.put("nextLevel", nextLevel);
|
| | | parameters.put("start", LocalDateTime.now().minusMinutes(5));
|
| | | |
| | | accountService.setOrgIdsByAccount(parameters);
|
| | | List<Map<String, Object>> result = historyMapper.getRegionRankingData(parameters);
|
| | | if ("profession".equals(parameters.get("dimension"))) {
|
| | | List<Profession> professions = professionMapper.selectAll();
|
| | | for (Map<String, Object> map : result) {
|
| | | for (Profession profession : professions) {
|
| | | if (map.get("name").equals(profession.getName())) {
|
| | | professions.remove(profession);
|
| | | break;
|
| | | }
|
| | | }
|
| | | }
|
| | | for (Profession profession : professions) {
|
| | | Map<String, Object> map = new HashMap<String, Object>();
|
| | | map.put("name", profession.getName());
|
| | | map.put("avg", 0);
|
| | | result.add(map);
|
| | | }
|
| | | } else if ("region".equals(parameters.get("dimension"))) {
|
| | | List<Map<String, Object>> regions = historyMapper.selectNextLevelRegion(parameters);
|
| | | for (int i = result.size() - 1; i >= 0; i--) {
|
| | | Map<String, Object> map = result.get(i);
|
| | | for (Map<String, Object> region : regions) {
|
| | | if ((map.get("code") == null)) {
|
| | | result.remove(map);
|
| | | break;
|
| | | } |
| | | if (map.get("code").equals(region.get("code"))) {
|
| | | map.put("name", region.get("name"));
|
| | | regions.remove(region);
|
| | | break;
|
| | | }
|
| | | }
|
| | | }
|
| | | for (Map<String, Object> region : regions) {
|
| | | Map<String, Object> map = new HashMap<String, Object>();
|
| | | map.put("name", region.get("name"));
|
| | | map.put("avg", 0);
|
| | | result.add(map);
|
| | | }
|
| | | }
|
| | | return result;
|
| | | }
|
| | | }
|