| package com.moral.task; | 
|   | 
| import java.time.LocalDateTime; | 
| import java.time.temporal.ChronoUnit; | 
| import java.util.ArrayList; | 
| import java.util.HashMap; | 
| import java.util.List; | 
| import java.util.Map; | 
|   | 
| import javax.annotation.Resource; | 
|   | 
| import org.slf4j.Logger; | 
| import org.slf4j.LoggerFactory; | 
| import org.springframework.stereotype.Component; | 
| import org.springframework.util.CollectionUtils; | 
|   | 
| import com.alibaba.fastjson.JSONObject; | 
| import com.moral.service.AlarmService; | 
| import com.moral.service.DeviceService; | 
| import com.moral.service.SensorService; | 
| import com.xxl.job.core.biz.model.ReturnT; | 
| import com.xxl.job.core.handler.annotation.XxlJob; | 
|   | 
| @Component | 
| public class AlarmTableInsertTask { | 
|     private static transient Logger logger = LoggerFactory.getLogger(AlarmTableInsertTask.class); | 
|     @Resource | 
|     private AlarmService alarmService; | 
|   | 
|     @Resource | 
|     private SensorService sensorService; | 
|   | 
|     @Resource | 
|     private DeviceService deviceService; | 
|   | 
|     @XxlJob("alarmDaily") | 
|     public ReturnT insertAlarmDailyTable(String params) { | 
|         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(); | 
|         if (monthValue < 10) { | 
|             month = "0" + monthValue; | 
|         } else { | 
|             month = monthValue + ""; | 
|         } | 
|         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>> dailyData = alarmService.getAvgAlarmData(devices); | 
|         List<Map<String, Object>> dailyDataList = new ArrayList<>(); | 
|         for (Map<String, Object> deviceData : dailyData) { | 
|             Map<String, Object> map = new HashMap<>(); | 
|             for (String sensorKey : sensorKeys) { | 
|                 if (deviceData.get(sensorKey) != null) { | 
|                     map.put(sensorKey, deviceData.get(sensorKey).toString()); | 
|                 } | 
|             } | 
|             JSONObject json = new JSONObject(map); | 
|             deviceData.put("json", json); | 
|         } | 
|   | 
|         for (Map<String, Object> dailyMap : dailyData) { | 
|             Map<String, Object> map = new HashMap<>(); | 
|             String mac = dailyMap.get("mac").toString(); | 
|             String json = dailyMap.get("json").toString(); | 
|             String state = dailyMap.get("state").toString(); | 
|             map.put("mac", mac); | 
|             map.put("json", json); | 
|             map.put("state", state); | 
|             map.put("time", startTime); | 
|             dailyDataList.add(map); | 
|         } | 
|         if (!CollectionUtils.isEmpty(dailyDataList)) { | 
|             alarmService.insertAlarmDaily(dailyDataList); | 
|             return new ReturnT(200, "插入天表成功"); | 
|         } | 
|         return new ReturnT(500, "插入天表失败"); | 
|     } | 
|   | 
|   | 
|     @XxlJob("createAlarmSubTable") | 
|     public void createAlarmSubTable(String params) { | 
|         LocalDateTime time = LocalDateTime.now().plusMonths(1);; | 
|         String year = time.getYear() + ""; | 
|         String month = time.getMonthValue() + ""; | 
|         if (time.getMonthValue() < 10) { | 
|             month = "0" + time.getMonthValue(); | 
|         } | 
|         alarmService.createTable(year + month); | 
|     } | 
| } |