| | |
| | | package com.moral.api.service.impl; |
| | | |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.moral.api.entity.Device; |
| | | import com.moral.api.entity.Sensor; |
| | | import com.moral.api.mapper.DeviceMapper; |
| | | import com.moral.api.service.DeviceService; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.moral.api.service.HistoryHourlyService; |
| | | import com.moral.constant.Constants; |
| | | import com.moral.constant.RedisConstants; |
| | | import com.moral.util.DateUtils; |
| | |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.Collections; |
| | | import java.util.Comparator; |
| | | import java.util.HashMap; |
| | | import java.util.HashSet; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Optional; |
| | | import java.util.Set; |
| | | import java.util.TreeMap; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | QueryWrapper<Device> wrapper = new QueryWrapper(); |
| | | wrapper.eq("monitor_point_id",monitorPointId); |
| | | wrapper.eq("is_delete", Constants.NOT_DELETE); |
| | | List<Device> devices = deviceMapper.selectList(wrapper); |
| | | return devices; |
| | | return deviceMapper.selectList(wrapper); |
| | | } |
| | | |
| | | @Override |
| | | public Map<String, Object> getSensorsByMac(String mac) { |
| | | public Map<String, Object> getSensorsByMac(Map<String, Object> params) { |
| | | String[] macs = params.get("macs").toString().split(","); |
| | | List<Map<String, Object>> elementLists = new ArrayList<>(); |
| | | |
| | | for (String mac : macs) { |
| | | //从redis中获取设备因子信息 |
| | | Device device = (Device) redisTemplate.opsForHash().get(RedisConstants.DEVICE_INFO, mac); |
| | | List<Sensor> sensors = device.getVersion().getSensors(); |
| | | Map<String, Object> result = new HashMap<>(); |
| | | Map<String, Object> map = new HashMap<>(); |
| | | for (Sensor sensor : sensors) { |
| | | String sensorCode = sensor.getCode(); |
| | | String name = sensor.getName(); |
| | | result.put(sensorCode, name); |
| | | map.put(sensorCode, name); |
| | | } |
| | | return result; |
| | | elementLists.add(map); |
| | | } |
| | | |
| | | Map<String, Object> map = elementLists.parallelStream() |
| | | .filter(elementList -> elementList.size() != 0) |
| | | .reduce((a, b) -> { |
| | | a.keySet().retainAll(b.keySet()); |
| | | return a; |
| | | }).orElse(new HashMap<>()); |
| | | return map; |
| | | } |
| | | |
| | | @Override |
| | | public List<Map<String, Object>> getTrendChartData(Map<String, Object> params) { |
| | | Object type = params.get("type"); |
| | | String start = params.remove("time").toString(); |
| | | //设备mac |
| | | String[] macs = params.remove("macs").toString().split(","); |
| | | //所选时间 |
| | | String[] times = params.remove("times").toString().split(","); |
| | | //因子code |
| | | String sensorCode = params.get("sensorCode").toString(); |
| | | String end; |
| | | String timeUnits; |
| | | String dateFormat; |
| | | |
| | | //返回结果集,time=data |
| | | List<Map<String, Object>> result = new ArrayList<>(); |
| | | |
| | | for (String start : times) { |
| | | Map<String, Object> resultMap = new HashMap<>(); |
| | | |
| | | if ("day".equals(type)) { |
| | | end = DateUtils.getDateAddDay(start, 1); |
| | | timeUnits = "hourly"; |
| | | dateFormat = "%H"; |
| | | dateFormat = "%k"; |
| | | } else if ("month".equals(type)) { |
| | | end = DateUtils.getDateAddMonth(start, 1); |
| | | timeUnits = "daily"; |
| | | dateFormat = "%d"; |
| | | dateFormat = "%e"; |
| | | } else { |
| | | end = DateUtils.getDateAddYear(start, 1); |
| | | timeUnits = "monthly"; |
| | | dateFormat = "%m"; |
| | | dateFormat = "%c"; |
| | | } |
| | | params.put("dateFormat", dateFormat); |
| | | params.put("timeUnits", timeUnits); |
| | | params.put("dateFormat", dateFormat); |
| | | params.put("start", start); |
| | | params.put("end", end); |
| | | return deviceMapper.getTrendChartData(params); |
| | | } |
| | | params.put("macs", macs); |
| | | //获取多设备指定因子数据 |
| | | List<Map<String, Object>> list = deviceMapper.getTrendChartData(params); |
| | | |
| | | //按time分组 |
| | | Map<String, List<Map<String, Object>>> data = list.parallelStream() |
| | | .collect(Collectors.groupingBy(o -> o.get("time").toString())); |
| | | |
| | | //TreeMap<String, List<Map<String, Object>>> data = new TreeMap<>(listMap); |
| | | |
| | | for (Map.Entry<String, List<Map<String, Object>>> entry : data.entrySet()) { |
| | | List<Object> values = new ArrayList<>(); |
| | | String time = entry.getKey(); |
| | | List<Map<String, Object>> value = entry.getValue(); |
| | | if (value.isEmpty()) { |
| | | continue; |
| | | } |
| | | for (String mac : macs) { |
| | | boolean flag = false; |
| | | for (Map<String, Object> map : value) { |
| | | if (mac.equals(map.get("mac"))) { |
| | | Object o = map.get(sensorCode); |
| | | values.add(o); |
| | | flag = true; |
| | | break; |
| | | } |
| | | } |
| | | if (!flag) { |
| | | values.add(""); |
| | | } |
| | | } |
| | | resultMap.put(time, values); |
| | | } |
| | | result.add(resultMap); |
| | | } |
| | | return result; |
| | | } |
| | | } |