package com.moral.service.impl; import java.time.LocalDateTime; import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.alibaba.druid.support.json.JSONUtils; import com.moral.mapper.AlarmMapper; import com.moral.service.AlarmService; import com.moral.service.SensorService; @Service public class AlarmServiceImpl implements AlarmService { @Resource private AlarmMapper alarmMapper; @Resource private SensorService sensorService; @Override public void insertAlarmDaily() { LocalDateTime time = LocalDateTime.now(); LocalDateTime endTime = time.truncatedTo(ChronoUnit.DAYS); LocalDateTime startTime = endTime.minusDays(1); List sensorKeys = sensorService.getSensorKeys(); String year = startTime.getYear() + ""; String month = null; int monthValue = startTime.getMonthValue(); if (monthValue < 10) { month = "0" + monthValue; } else { month = monthValue + ""; } String yearAndMonth = year + month; Map devices = new HashMap<>(); devices.put("sensorKeys", sensorKeys); devices.put("start", startTime); devices.put("end", endTime); devices.put("yearAndMonth", yearAndMonth); List macs = alarmMapper.getMacs(devices); devices.put("macs", macs); for (String mac : macs) { List> resultList = new ArrayList<>(); devices.put("mac", mac); List> list = alarmMapper.getAlarmData(devices); List> newList1 = list.stream().distinct().collect(Collectors.toList()); for (Map map : newList1) { for (String key : map.keySet()) { if (!"mac".equals(key)) { String[] strings = map.get(key).toString().replace("$.", "") .replace("[", "").replace("]", "") .replace("\"", "").replace(" ", "").split(","); List sensors = new ArrayList(Arrays.asList(strings)); map.put(key, sensors); } } } Map hashMap = new HashMap<>(); hashMap.put("mac", mac); ArrayList stateList1 = new ArrayList<>(); ArrayList stateList2 = new ArrayList<>(); ArrayList stateList3 = new ArrayList<>(); for (Map map : newList1) { if (map.get("1") != null) { stateList1.addAll((ArrayList) map.get("1")); hashMap.put("1", stateList1); } if (map.get("2") != null) { stateList2.addAll((ArrayList) map.get("2")); hashMap.put("2", stateList2); } if (map.get("3") != null) { stateList3.addAll((ArrayList) map.get("3")); hashMap.put("3", stateList3); } } for (String key : hashMap.keySet()) { if (!"mac".equals(key)) { ArrayList arrayList = (ArrayList) hashMap.get(key); List sensorsList = arrayList.stream().distinct().collect(Collectors.toList()); hashMap.put(key, sensorsList); } } for (String key : hashMap.keySet()) { if(!"mac".equals(key)){ Map jsonMap = new HashMap<>(); Map map = new HashMap<>(); map.put("mac",mac); map.put("time",startTime); map.put("state", key); ArrayList stateList = (ArrayList) hashMap.get(key); for (String state : stateList) { jsonMap.put(state,1); } map.put("json",JSONUtils.toJSONString(jsonMap)); resultList.add(map); } } alarmMapper.insertAlarmDaily(resultList); } } @Override public void createTable(String yearAndMonth) { alarmMapper.createTable(yearAndMonth); } }