|  |  |  | 
|---|
|  |  |  | import com.moral.service.HistoryService; | 
|---|
|  |  |  | import com.moral.service.OrganizationService; | 
|---|
|  |  |  | import com.moral.service.SensorService; | 
|---|
|  |  |  | import com.moral.util.DateUtil; | 
|---|
|  |  |  | import com.moral.util.WeatherUtil; | 
|---|
|  |  |  | import com.xxl.job.core.biz.model.ReturnT; | 
|---|
|  |  |  | import com.xxl.job.core.handler.annotation.XxlJob; | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @XxlJob("historyHourly") | 
|---|
|  |  |  | public ReturnT insertHistoryHourlyTable(String params) { | 
|---|
|  |  |  | LocalDateTime time = LocalDateTime.now(); | 
|---|
|  |  |  | int year = time.getYear(); | 
|---|
|  |  |  | int month = time.getMonthValue(); | 
|---|
|  |  |  | int day = time.getDayOfMonth(); | 
|---|
|  |  |  | int hour = time.getHour(); | 
|---|
|  |  |  | if (day == 1) { | 
|---|
|  |  |  | if (hour == 0) { | 
|---|
|  |  |  | 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); | 
|---|
|  |  |  | LocalDateTime startTime = endTime.minusHours(1); | 
|---|
|  |  |  | //正式环境使用 | 
|---|
|  |  |  | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:00:00"); | 
|---|
|  |  |  | Date endTimeDate = new Date(); | 
|---|
|  |  |  | Date startTimeDate = DateUtil.rollHour(endTimeDate, -1); | 
|---|
|  |  |  | StringBuilder endTime = new StringBuilder(sdf.format(endTimeDate)); | 
|---|
|  |  |  | StringBuilder startTime = new StringBuilder(sdf.format(startTimeDate)); | 
|---|
|  |  |  | String yearAndMonth = DateUtil.getYear(startTimeDate) + DateUtil.getMonth(startTimeDate); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //测试环境使用,用于手动指定时间段 | 
|---|
|  |  |  | /* SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | 
|---|
|  |  |  | Date endTimeDate = sdf.parse("2020-11-01 00:00:22"); | 
|---|
|  |  |  | Date startTimeDate = DateUtil.rollHour(endTimeDate, -1); | 
|---|
|  |  |  | StringBuilder endTime = new StringBuilder(sdf.format(endTimeDate)); | 
|---|
|  |  |  | StringBuilder startTime = new StringBuilder(sdf.format(startTimeDate)); | 
|---|
|  |  |  | endTime.replace(14, 19, "00:00"); | 
|---|
|  |  |  | startTime.replace(14, 19, "00:00"); | 
|---|
|  |  |  | String yearAndMonth = DateUtil.getYear(startTimeDate) + DateUtil.getMonth(startTimeDate);*/ | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<String> sensorKeys = sensorService.getSensorKeys(); | 
|---|
|  |  |  | List<String> macs = deviceService.getMacs(); | 
|---|
|  |  |  | Map<String, Object> devices = new HashMap<>(); | 
|---|
|  |  |  | devices.put("sensorKeys", sensorKeys); | 
|---|
|  |  |  | devices.put("start", startTime); | 
|---|
|  |  |  | devices.put("end", endTime); | 
|---|
|  |  |  | devices.put("start", startTime.toString()); | 
|---|
|  |  |  | devices.put("end", endTime.toString()); | 
|---|
|  |  |  | devices.put("macs", macs); | 
|---|
|  |  |  | devices.put("yearAndMonth", yearAndMonth); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | List<Map<String, Object>> hourlyData = historyMinutelyService.getMinutelySensorData(devices); | 
|---|
|  |  |  | XxlJobLogger.log("historyHourlyData:" + hourlyData.size()); | 
|---|
|  |  |  | 
|---|
|  |  |  | Map<String, Object> hourlyDataMap = new LinkedHashMap<>(); | 
|---|
|  |  |  | JSONObject jo = new JSONObject(true); | 
|---|
|  |  |  | hourlyDataMap.put("mac", deviceData.get("mac")); | 
|---|
|  |  |  | hourlyDataMap.put("time", startTime); | 
|---|
|  |  |  | hourlyDataMap.put("time", endTime.toString()); | 
|---|
|  |  |  | JSONArray jsonArray = new JSONArray(); | 
|---|
|  |  |  | for (String key : deviceData.keySet()) { | 
|---|
|  |  |  | if (!key.equals("mac") && !key.startsWith("M")) { | 
|---|