| | |
| | | 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 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); |
| | | public ReturnT insertAlarmDailyTable() { |
| | | List<Map<String, Object>> list = alarmService.getAlarmData(); |
| | | if (!CollectionUtils.isEmpty(list)) { |
| | | alarmService.insertAlarmDaily(list); |
| | | return new ReturnT(200, "插入天表成功"); |
| | | } |
| | | return new ReturnT(500, "插入天表失败"); |
| | | } |
| | | |
| | | |
| | | @XxlJob("createAlarmSubTable") |
| | | public void createAlarmSubTable(String params) { |
| | | LocalDateTime time = LocalDateTime.now().plusMonths(1);; |
| | | public ReturnT createAlarmSubTable() { |
| | | 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); |
| | | ReturnT returnT = new ReturnT(200, "创建alarm年月分表成功"); |
| | | return returnT; |
| | | } |
| | | } |