package com.moral.task; import com.alibaba.fastjson.JSON; import com.moral.service.HistoryFiveMinutelyService; import com.moral.service.SensorService; import com.moral.util.DateUtil; import com.xxl.job.core.biz.model.ReturnT; import com.xxl.job.core.handler.annotation.XxlJob; import com.xxl.job.core.log.XxlJobLogger; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; import org.springframework.util.ObjectUtils; import javax.annotation.Resource; import java.text.SimpleDateFormat; import java.util.*; @Component public class HistoryFiveMinutelyTask { @Resource HistoryFiveMinutelyService historyFiveMinutelyService; @Resource SensorService sensorService; @Resource RedisTemplate redisTemplate; @XxlJob("createHistoryFiveMinutelyTb") public ReturnT createHistoryMinutelyTb(String param) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(); date = DateUtil.rollMon(date, 1); String year = DateUtil.getYear(date); String month = DateUtil.getMonth(date).length() == 1 ? "0".concat(DateUtil.getMonth(date)) : DateUtil.getMonth(date); try { historyFiveMinutelyService.createHistoryFiveMinutelyTable(year + month); } catch (Exception e) { return new ReturnT(500, "建立五分钟表失败"); } return new ReturnT(200, "建立五分钟表成功"); } /*分钟表插入时间为每分钟的第二十秒 * 分钟数据读取时间,每五分钟的第三十秒开始读取 * EG: 10:15:30 插入10:10--10:15的数据 * 10:20:30 插入10:15--10:20的数据*/ @XxlJob("insertHistoryFiveMinutelyTb") public ReturnT insertHistoryFiveMinutely(String param) { //获取当前时间 Date errorDate = new Date(); try { /*根据时间从分钟表数据钟查询五分钟数据的平均值*/ Map params = getStartAndEndTime(); List sensorKeys = sensorService.getSensorKeys(); params.put("sensorKeys", sensorKeys); List> fiveMinutesSensorDatas; fiveMinutesSensorDatas = historyFiveMinutelyService.getFiveMinutesSensorData(params); /*将得到的数据进行转换*/ List> insertDatas; insertDatas = new ArrayList<>(); for (Map data : fiveMinutesSensorDatas) { String mac = (String) data.get("mac"); data.remove("mac"); Map keyAndValueMap = new LinkedHashMap<>(); Map insertDataMap = new LinkedHashMap<>(); data.forEach((key, value) -> { key = key.substring(3); List list = null; if (ObjectUtils.isEmpty(keyAndValueMap.get(key))) { list = new ArrayList<>(); } else { list = (List) keyAndValueMap.get(key); } if (value instanceof Double) { value = String.valueOf(value); value = value.equals("0.0") ? 0 : Double.valueOf((String) value); } list.add(value); keyAndValueMap.put(key, list); }); String keyAndValueJson = JSON.toJSONString(keyAndValueMap); insertDataMap.put("mac", mac); insertDataMap.put("time", params.get("end")); insertDataMap.put("json", keyAndValueJson); insertDatas.add(insertDataMap); } /*将数据插入数据库*/ if (!ObjectUtils.isEmpty(insertDatas)) { historyFiveMinutelyService.insertHistoryFiveMinutely(insertDatas, (String) params.get("yearAndMonth")); return new ReturnT(200, "插入五分钟数据成功"); } } catch (Exception e) { XxlJobLogger.log("historyFiveMinutelyException:" + e.getMessage()); e.printStackTrace(); } List record = new ArrayList(); record.add("repairFiveMinutelyData_" + errorDate.getTime() / 1000); redisTemplate.opsForList().leftPushAll("unrepair_data", record); return new ReturnT(500, "插入五分钟数据失败"); } /** * @Description: 根据当前时间获取到查询的开始时间和结束时间以及年月字符串 * @Param: * @return: * @Author: 下雨听风 * @Date: 2020/10/15 */ private Map getStartAndEndTime() { Map map = new HashMap<>(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(); String minute = DateUtil.getMinute(date); String year = ""; String month = ""; String yearAndMonth = ""; String startTime = ""; String endTime = ""; Integer endMinute = Integer.parseInt(String.valueOf(minute.charAt(minute.length() - 1))); if (endMinute >= 5) { StringBuilder time = new StringBuilder(sdf.format(date)); startTime = time.replace(15, 19, "0:00").toString(); endTime = time.replace(15, 19, "5:00").toString(); } else { StringBuilder endTimesb = new StringBuilder(sdf.format(date)); endTime = endTimesb.replace(15, 19, "0:00").toString(); date = DateUtil.rollMinute(date, -5); StringBuilder startTimesb = new StringBuilder(sdf.format(date)); startTime = startTimesb.replace(15, 19, "5:00").toString(); } year = DateUtil.getYear(date); month = DateUtil.getMonth(date); yearAndMonth = year + month; map.put("start", startTime); map.put("end", endTime); map.put("yearAndMonth", yearAndMonth); return map; } }