|  |  |  | 
|---|
|  |  |  | 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.HashSet; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.Map; | 
|---|
|  |  |  | import java.util.Set; | 
|---|
|  |  |  | import java.util.stream.Collectors; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import javax.annotation.Resource; | 
|---|
|  |  |  | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Service | 
|---|
|  |  |  | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | private AlarmMapper alarmMapper; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | private SensorService sensorService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | private DeviceService deviceService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @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); | 
|---|
|  |  |  | List<String> sensorKeys = sensorService.getSensorKeys(); | 
|---|
|  |  |  | List<String> macs = deviceService.getMacs(); | 
|---|
|  |  |  | String year = startTime.getYear() + ""; | 
|---|
|  |  |  | String month = null; | 
|---|
|  |  |  | int monthValue = startTime.getMonthValue(); | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | String yearAndMonth = year + month; | 
|---|
|  |  |  | Map<String, Object> devices = new HashMap<>(); | 
|---|
|  |  |  | devices.put("sensorKeys", sensorKeys); | 
|---|
|  |  |  | devices.put("start", startTime); | 
|---|
|  |  |  | devices.put("end", endTime); | 
|---|
|  |  |  | devices.put("macs", macs); | 
|---|
|  |  |  | devices.put("yearAndMonth", yearAndMonth); | 
|---|
|  |  |  | 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> macs = alarmMapper.getMacs(devices); | 
|---|
|  |  |  | 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>> newList = list.stream().distinct().collect(Collectors.toList()); | 
|---|
|  |  |  | for (Map<String, Object> map : newList) { | 
|---|
|  |  |  | for (String key : map.keySet()) { | 
|---|
|  |  |  | String[] strings = map.get(key).toString().replace("$.", "") | 
|---|
|  |  |  | .replace("[", "").replace("]", "") | 
|---|
|  |  |  | .replace("\"", "").replace(" ", "").split(","); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<String> sensors = new ArrayList<String>(Arrays.asList(strings)); | 
|---|
|  |  |  | map.put(key, sensors); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Map<String, Object> hashMap = new HashMap<>(); | 
|---|
|  |  |  | Set<String> stateSet1 = new HashSet<>(); | 
|---|
|  |  |  | Set<String> stateSet2 = new HashSet<>(); | 
|---|
|  |  |  | Set<String> stateSet3 = new HashSet<>(); | 
|---|
|  |  |  | for (Map<String, Object> map : newList) { | 
|---|
|  |  |  | if (map.get("1") != null) { | 
|---|
|  |  |  | stateSet1.addAll((ArrayList<String>) map.get("1")); | 
|---|
|  |  |  | hashMap.put("1", stateSet1); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (map.get("2") != null) { | 
|---|
|  |  |  | stateSet2.addAll((ArrayList<String>) map.get("2")); | 
|---|
|  |  |  | hashMap.put("2", stateSet2); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (map.get("3") != null) { | 
|---|
|  |  |  | stateSet3.addAll((ArrayList<String>) map.get("3")); | 
|---|
|  |  |  | hashMap.put("3", stateSet3); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | for (String key : hashMap.keySet()) { | 
|---|
|  |  |  | Map<String, Object> jsonMap = new HashMap<>(); | 
|---|
|  |  |  | Map<String, Object> map = new HashMap<>(); | 
|---|
|  |  |  | map.put("mac",mac); | 
|---|
|  |  |  | map.put("time",startTime); | 
|---|
|  |  |  | map.put("state", key); | 
|---|
|  |  |  | Set<String> sensors = (Set<String>) hashMap.get(key); | 
|---|
|  |  |  | for (String sensor : sensors) { | 
|---|
|  |  |  | jsonMap.put(sensor,1); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | map.put("json",JSONUtils.toJSONString(jsonMap)); | 
|---|
|  |  |  | resultList.add(map); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | map.put("1", list1); | 
|---|
|  |  |  | map.put("2", list2); | 
|---|
|  |  |  | map.put("3", list3); | 
|---|
|  |  |  | alarmMapper.insertAlarmDaily(resultList); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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 (int i = 0; i < map.keySet().size() - 1; i++) { | 
|---|
|  |  |  | Map<String, Object> hashMap = new HashMap<>(); | 
|---|
|  |  |  | Map<String, Object> jsonMap = new HashMap<>(); | 
|---|
|  |  |  | hashMap.put("mac", map.get("mac").toString()); | 
|---|
|  |  |  | hashMap.put("state", i + 1); | 
|---|
|  |  |  | hashMap.put("time", startTime); | 
|---|
|  |  |  | ArrayList<String> arrayList = (ArrayList<String>) map.get(i + 1 + ""); | 
|---|
|  |  |  | for (String sensorKey : arrayList) { | 
|---|
|  |  |  | jsonMap.put(sensorKey, 1); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | hashMap.put("json", JSONUtils.toJSONString(jsonMap)); | 
|---|
|  |  |  | resultList.add(hashMap); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return resultList; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public int insertAlarmDaily(List<Map<String, Object>> list) { | 
|---|
|  |  |  | return alarmMapper.insertAlarmDaily(list); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|