From 8aa815dd9fca6dc914c0cba72ada9af5c795a44c Mon Sep 17 00:00:00 2001 From: ZhuDongming <773644075@qq.com> Date: Tue, 28 Apr 2020 15:19:42 +0800 Subject: [PATCH] update 1小时插入一次 --- src/main/java/com/moral/task/HistoryTableInsertTask.java | 111 +++++++++++++++++++++++++++++++++++++++++++------------ 1 files changed, 87 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/moral/task/HistoryTableInsertTask.java b/src/main/java/com/moral/task/HistoryTableInsertTask.java index 5e6d68b..a9f9c19 100644 --- a/src/main/java/com/moral/task/HistoryTableInsertTask.java +++ b/src/main/java/com/moral/task/HistoryTableInsertTask.java @@ -24,6 +24,7 @@ import com.moral.service.HistoryDailyService; import com.moral.service.HistoryHourlyService; import com.moral.service.HistoryMinutelyService; +import com.moral.service.HistoryService; import com.moral.service.SensorService; import com.xxl.job.core.biz.model.ReturnT; import com.xxl.job.core.handler.annotation.XxlJob; @@ -40,6 +41,9 @@ private DeviceService deviceService; @Resource + private HistoryService historyService; + + @Resource private HistoryMinutelyService historyMinutelyService; @Resource @@ -53,6 +57,15 @@ LocalDateTime time = LocalDateTime.now(); int year = time.getYear(); int month = time.getMonthValue(); + int day = time.getDayOfMonth(); + if (day == 1) { + if (month == 1) { + month = 12; + year = year - 1; + } else { + month = month - 1; + } + } String monthStr = month < 10 ? ("0" + month) : month + ""; String yearAndMonth = year + monthStr; LocalDateTime endTime = time.truncatedTo(ChronoUnit.MINUTES); @@ -492,6 +505,15 @@ LocalDateTime time = LocalDateTime.now(); int year = time.getYear(); int month = time.getMonthValue(); + int day = time.getDayOfMonth(); + if (day == 1) { + if (month == 1) { + month = 12; + year = year - 1; + } else { + month = month - 1; + } + } String monthStr = month < 10 ? ("0" + month) : month + ""; String yearAndMonth = year + monthStr; LocalDateTime endTime = time.truncatedTo(ChronoUnit.HOURS); @@ -519,8 +541,13 @@ if (!key.equals("mac") && !key.startsWith("M")) { List<Object> date = new ArrayList<>(); date.add(deviceData.get(key)); - date.add(new BigDecimal(deviceData.get("MIN" + key).toString())); - date.add(new BigDecimal(deviceData.get("MAX" + key).toString())); + if (deviceData.get("MIN" + key) instanceof String) { + date.add(new BigDecimal(deviceData.get("MIN" + key).toString())); + date.add(new BigDecimal(deviceData.get("MAX" + key).toString())); + } else if (deviceData.get("MIN" + key) instanceof byte[]) { + date.add(new BigDecimal(new String((byte[]) (deviceData.get("MIN" + key))))); + date.add(new BigDecimal(new String((byte[]) (deviceData.get("MAX" + key))))); + } jo.put(key, date); } } @@ -584,8 +611,13 @@ if (!key.equals("mac") && !key.startsWith("M")) { List<Object> date = new ArrayList<>(); date.add(deviceData.get(key)); - date.add(new BigDecimal(deviceData.get("MIN" + key).toString())); - date.add(new BigDecimal(deviceData.get("MAX" + key).toString())); + if (deviceData.get("MIN" + key) instanceof String) { + date.add(new BigDecimal(deviceData.get("MIN" + key).toString())); + date.add(new BigDecimal(deviceData.get("MAX" + key).toString())); + } else if (deviceData.get("MIN" + key) instanceof byte[]) { + date.add(new BigDecimal(new String((byte[]) (deviceData.get("MIN" + key))))); + date.add(new BigDecimal(new String((byte[]) (deviceData.get("MAX" + key))))); + } jo.put(key, date); } } @@ -618,18 +650,19 @@ LocalDateTime value = time.truncatedTo(ChronoUnit.DAYS); List<String> sensorKeys = sensorService.getSensorKeys(); List<String> macs = deviceService.getMacs(); - for (int i = 0; i <= 5; i++) { - LocalDateTime startTime = value.plusHours(i); - LocalDateTime endTime = value.plusHours(i + 1); - 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); - try { + int count = 0; + try { + for (int i = 0; i <= 5; i++) { + LocalDateTime startTime = value.plusHours(i); + LocalDateTime endTime = value.plusHours(i + 1); + 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>> hourlyData = historyMinutelyService.getMinutelySensorData(devices); - XxlJobLogger.log("historyHourlyData:" + hourlyData.size()); + count += hourlyData.size(); List<Map<String, Object>> hourlyDataList = new ArrayList<>(); for (Map<String, Object> deviceData : hourlyData) { if (!ObjectUtils.isEmpty(deviceData)) { @@ -642,8 +675,13 @@ if (!key.equals("mac") && !key.startsWith("M")) { List<Object> date = new ArrayList<>(); date.add(deviceData.get(key)); - date.add(new BigDecimal(deviceData.get("MIN" + key).toString())); - date.add(new BigDecimal(deviceData.get("MAX" + key).toString())); + if (deviceData.get("MIN" + key) instanceof String) { + date.add(new BigDecimal(deviceData.get("MIN" + key).toString())); + date.add(new BigDecimal(deviceData.get("MAX" + key).toString())); + } else if (deviceData.get("MIN" + key) instanceof byte[]) { + date.add(new BigDecimal(new String((byte[]) (deviceData.get("MIN" + key))))); + date.add(new BigDecimal(new String((byte[]) (deviceData.get("MAX" + key))))); + } jo.put(key, date); } } @@ -654,14 +692,15 @@ } if (!CollectionUtils.isEmpty(hourlyDataList)) { historyHourlyService.insertHistoryHourly(hourlyDataList); - ReturnT returnT = new ReturnT(200, "���������������������"); - return returnT; } - } catch (Exception e) { - XxlJobLogger.log("historyHourlyException:" + e.getMessage()); - logger.error(e.getMessage()); - e.printStackTrace(); } + XxlJobLogger.log("historyHourlyData:" + count); + ReturnT returnT = new ReturnT(200, "���������������������"); + return returnT; + } catch (Exception e) { + XxlJobLogger.log("historyHourlyException:" + e.getMessage()); + logger.error(e.getMessage()); + e.printStackTrace(); } ReturnT returnT = new ReturnT(500, "���������������������"); return returnT; @@ -808,7 +847,7 @@ devices.put("start", startTime); devices.put("end", endTime); try { - List<Map<String, Object>> minutelyData = deviceService.getSensorDataOnce(devices); + List<Map<String, Object>> minutelyData = deviceService.getSensorDataByMacOnce(devices); XxlJobLogger.log("historyMinutelyData:" + minutelyData.size()); List<Map<String, Object>> minutelyDataList = new ArrayList<>(); for (Map<String, Object> deviceData : minutelyData) { @@ -846,4 +885,28 @@ return returnT; } + @XxlJob("historySpecial") + public ReturnT insertHistorySpecialTable(String params) { + Map macMap = JSON.parseObject(params); + List<String> macList = (List<String>) macMap.get("mac"); + LocalDateTime value = LocalDateTime.now(); + Map<String, Object> devices = new HashMap<>(); + devices.put("macList", macList); + devices.put("time", value); + try { + int count = historyService.insertHistorySpecialTable(devices); + XxlJobLogger.log("insertHistorySpecialTable:" + count); + if (count > 0) { + ReturnT returnT = new ReturnT(200, "������historySpecial���������"); + return returnT; + } + } catch (Exception e) { + XxlJobLogger.log("insertHistorySpecialTableException:" + e.getMessage()); + logger.error(e.getMessage()); + e.printStackTrace(); + } + ReturnT returnT = new ReturnT(500, "������historySpecial���������"); + return returnT; + } + } -- Gitblit v1.8.0