| | |
| | | import java.time.LocalDateTime; |
| | | import java.time.temporal.ChronoUnit; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.HashMap; |
| | | import java.util.Iterator; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.stream.Collectors; |
| | |
| | | import com.alibaba.druid.support.json.JSONUtils; |
| | | import com.moral.mapper.AlarmMapper; |
| | | import com.moral.service.AlarmService; |
| | | import com.moral.service.DeviceService; |
| | | import com.moral.service.SensorService; |
| | | |
| | | |
| | |
| | | private SensorService sensorService; |
| | | |
| | | @Override |
| | | public List<Map<String, Object>> getAlarmData() { |
| | | public void insertAlarmDaily() { |
| | | LocalDateTime time = LocalDateTime.now(); |
| | | LocalDateTime endTime = time.truncatedTo(ChronoUnit.DAYS); |
| | | LocalDateTime startTime = endTime.minusDays(1); |
| | |
| | | devices.put("start", startTime); |
| | | devices.put("end", endTime); |
| | | devices.put("yearAndMonth", yearAndMonth); |
| | | List<String> macs = alarmMapper.getMacs(devices); |
| | | devices.put("macs", macs); |
| | | for (String mac : macs) { |
| | | List<Map<String, Object>> resultList = new ArrayList<>(); |
| | | devices.put("mac", mac); |
| | | List<Map<String, Object>> list = alarmMapper.getAlarmData(devices); |
| | | List<Map<String, Object>> newList1 = list.stream().distinct().collect(Collectors.toList()); |
| | | for (Map<String, Object> map : newList1) { |
| | | for (String key : map.keySet()) { |
| | | if (!"mac".equals(key)) { |
| | | String[] strings = map.get(key).toString().replace("$.", "") |
| | | .replace("[", "").replace("]", "") |
| | | .replace("\"", "").replace(" ", "").split(","); |
| | | |
| | | List<Map<String, Object>> list = alarmMapper.getAlarmData(devices); |
| | | for (Map<String, Object> map : list) { |
| | | ArrayList<String> list1 = new ArrayList<>(); |
| | | ArrayList<String> list2 = new ArrayList<>(); |
| | | ArrayList<String> list3 = new ArrayList<>(); |
| | | for (String key : map.keySet()) { |
| | | if (!key.equals("mac")) { |
| | | String value = map.get(key).toString().substring(1, 2); |
| | | if (value.equals("1")) { |
| | | list1.add(key); |
| | | } else if (value.equals("2")) { |
| | | list2.add(key); |
| | | } else if (value.equals("3")) { |
| | | list3.add(key); |
| | | List<String> sensors = new ArrayList<String>(Arrays.asList(strings)); |
| | | map.put(key, sensors); |
| | | } |
| | | } |
| | | } |
| | | map.put("1", list1); |
| | | map.put("2", list2); |
| | | map.put("3", list3); |
| | | } |
| | | |
| | | for (Map<String, Object> map : list) { |
| | | Iterator<String> iter = map.keySet().iterator(); |
| | | while (iter.hasNext()) { |
| | | String next = iter.next(); |
| | | if (!(next.equals("mac")) && !(next.equals("1")) && !(next.equals("2")) && !(next.equals("3"))) { |
| | | iter.remove(); |
| | | Map<String, Object> hashMap = new HashMap<>(); |
| | | hashMap.put("mac", mac); |
| | | ArrayList<String> stateList1 = new ArrayList<>(); |
| | | ArrayList<String> stateList2 = new ArrayList<>(); |
| | | ArrayList<String> stateList3 = new ArrayList<>(); |
| | | for (Map<String, Object> map : newList1) { |
| | | if (map.get("1") != null) { |
| | | stateList1.addAll((ArrayList<String>) map.get("1")); |
| | | hashMap.put("1", stateList1); |
| | | } |
| | | } |
| | | } |
| | | |
| | | for (int i = list.size() - 1; i >= 0; i--) { |
| | | for (int j = i - 1; j >= 0; j--) { |
| | | Map<String, Object> map1 = list.get(i); |
| | | Map<String, Object> map2 = list.get(j); |
| | | if (map1.get("mac").equals(map2.get("mac"))) { |
| | | List<String> frontList1 = (ArrayList<String>) map1.get("1"); |
| | | List<String> frontList2 = (ArrayList<String>) map1.get("2"); |
| | | List<String> frontList3 = (ArrayList<String>) map1.get("3"); |
| | | List<String> backList1 = (ArrayList<String>) map2.get("1"); |
| | | List<String> backList2 = (ArrayList<String>) map2.get("2"); |
| | | List<String> backList3 = (ArrayList<String>) map2.get("3"); |
| | | backList1.addAll(frontList1); |
| | | backList2.addAll(frontList2); |
| | | backList3.addAll(frontList3); |
| | | list.remove(map1); |
| | | List<String> list1 = backList1.stream().distinct().collect(Collectors.toList()); |
| | | List<String> list2 = backList2.stream().distinct().collect(Collectors.toList()); |
| | | List<String> list3 = backList3.stream().distinct().collect(Collectors.toList()); |
| | | map2.put("1", list1); |
| | | map2.put("2", list2); |
| | | map2.put("3", list3); |
| | | break; |
| | | if (map.get("2") != null) { |
| | | stateList2.addAll((ArrayList<String>) map.get("2")); |
| | | hashMap.put("2", stateList2); |
| | | } |
| | | } |
| | | } |
| | | if (map.get("3") != null) { |
| | | stateList3.addAll((ArrayList<String>) map.get("3")); |
| | | hashMap.put("3", stateList3); |
| | | } |
| | | |
| | | for (Map<String, Object> map : list) { |
| | | List<String> list1 = (ArrayList<String>) map.get("1"); |
| | | List<String> list2 = (ArrayList<String>) map.get("2"); |
| | | List<String> list3 = (ArrayList<String>) map.get("3"); |
| | | if (list1.size() == 0) { |
| | | map.remove("1"); |
| | | } |
| | | if (list2.size() == 0) { |
| | | map.remove("2"); |
| | | } |
| | | if (list3.size() == 0) { |
| | | map.remove("3"); |
| | | } |
| | | } |
| | | |
| | | List<Map<String, Object>> resultList = new ArrayList<>(); |
| | | for (Map<String, Object> map : list) { |
| | | for (String key : map.keySet()) { |
| | | Map<String, Object> jsonMap = new HashMap<>(); |
| | | for (String key : hashMap.keySet()) { |
| | | if (!"mac".equals(key)) { |
| | | Map<String, Object> hashMap = new HashMap<>(); |
| | | hashMap.put("mac",map.get("mac")); |
| | | hashMap.put("time",startTime); |
| | | hashMap.put("state",key); |
| | | ArrayList<String> stateList = (ArrayList<String>) map.get(key); |
| | | ArrayList<String> arrayList = (ArrayList<String>) hashMap.get(key); |
| | | List<String> sensorsList = arrayList.stream().distinct().collect(Collectors.toList()); |
| | | hashMap.put(key, sensorsList); |
| | | } |
| | | } |
| | | |
| | | for (String key : hashMap.keySet()) { |
| | | if(!"mac".equals(key)){ |
| | | Map<String, Object> jsonMap = new HashMap<>(); |
| | | Map<String, Object> map = new HashMap<>(); |
| | | map.put("mac",mac); |
| | | map.put("time",startTime); |
| | | map.put("state", key); |
| | | ArrayList<String> stateList = (ArrayList<String>) hashMap.get(key); |
| | | for (String state : stateList) { |
| | | jsonMap.put(state,1); |
| | | } |
| | | hashMap.put("json", JSONUtils.toJSONString(jsonMap)); |
| | | resultList.add(hashMap); |
| | | map.put("json",JSONUtils.toJSONString(jsonMap)); |
| | | resultList.add(map); |
| | | } |
| | | } |
| | | alarmMapper.insertAlarmDaily(resultList); |
| | | } |
| | | return resultList; |
| | | } |
| | | |
| | | @Override |
| | | public int insertAlarmDaily(List<Map<String, Object>> list) { |
| | | return alarmMapper.insertAlarmDaily(list); |
| | | } |
| | | |
| | | @Override |