|  |  |  | 
|---|
|  |  |  | package com.moral.task; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.moral.service.DeviceAdjustValueService; | 
|---|
|  |  |  | import com.moral.service.DeviceAdjustValueTimingService; | 
|---|
|  |  |  | import com.moral.util.AlarmUtils_2; | 
|---|
|  |  |  | import com.moral.util.LengthOfTimeUtils; | 
|---|
|  |  |  | 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.slf4j.Logger; | 
|---|
|  |  |  | import org.slf4j.LoggerFactory; | 
|---|
|  |  |  | import org.springframework.stereotype.Component; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import javax.annotation.Resource; | 
|---|
|  |  |  | import java.time.LocalDateTime; | 
|---|
|  |  |  | import java.time.format.DateTimeFormatter; | 
|---|
|  |  |  | import java.util.ArrayList; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.Map; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Component | 
|---|
|  |  |  | public class DeviceAdjustValueTimingTask { | 
|---|
|  |  |  | private static transient Logger logger = LoggerFactory.getLogger(DeviceAdjustValueTimingTask.class); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | private DeviceAdjustValueTimingService deviceAdjustValueTimingService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Resource | 
|---|
|  |  |  | private DeviceAdjustValueService deviceAdjustValueService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @XxlJob("DeviceAdjustValueTiming") | 
|---|
|  |  |  | public ReturnT DeviceAdjustValueTiming(String params) { | 
|---|
|  |  |  | AlarmUtils_2.sendMail("357328213@qq.com","任务调度测试!","任务调度成功!"); | 
|---|
|  |  |  | ReturnT returnT = new ReturnT(500, "dayinxinxi"); | 
|---|
|  |  |  | //获取当前时间和五分钟前的时间 | 
|---|
|  |  |  | DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); | 
|---|
|  |  |  | LocalDateTime time = LocalDateTime.now(); | 
|---|
|  |  |  | String endTime = df.format(time); | 
|---|
|  |  |  | // 当前时间 -5分钟 | 
|---|
|  |  |  | String startTime = df.format(time.minusMinutes(5)); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | //从device_adjust_value表中查询时间段内需要变更的设备列表 | 
|---|
|  |  |  | int startTimeSeconds = LengthOfTimeUtils.Seconds(startTime); | 
|---|
|  |  |  | int endTimeSeconds = LengthOfTimeUtils.Seconds(endTime); | 
|---|
|  |  |  | //创建list,接收要变更校准值公式的设备列表 | 
|---|
|  |  |  | List<Map<String, Object>> deviceAdjusTimingList = new ArrayList(); | 
|---|
|  |  |  | //判断开始时间和结束时间是否处于同一天 | 
|---|
|  |  |  | if (startTimeSeconds>86100&&endTimeSeconds<300){ | 
|---|
|  |  |  | List<Map<String, Object>> dayBeforeList = deviceAdjustValueTimingService.selectDataBySecondSolt(startTimeSeconds,86400); | 
|---|
|  |  |  | List<Map<String, Object>> dayNextList = deviceAdjustValueTimingService.selectDataBySecondSolt(0,endTimeSeconds); | 
|---|
|  |  |  | deviceAdjusTimingList.addAll(dayBeforeList); | 
|---|
|  |  |  | deviceAdjusTimingList.addAll(dayNextList); | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | List<Map<String, Object>> list = deviceAdjustValueTimingService.selectDataBySecondSolt(startTimeSeconds,endTimeSeconds); | 
|---|
|  |  |  | deviceAdjusTimingList.addAll(list); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //判断deviceAdjustimingList集合是否为空 | 
|---|
|  |  |  | if (deviceAdjusTimingList.isEmpty()){ | 
|---|
|  |  |  | ReturnT returnT = new ReturnT(500, "没有需要更新的设备!"); | 
|---|
|  |  |  | return returnT; | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | //遍历deviceAdjustimingList集合,根据集合中的设备对device_adjust_value表中的数据进行替换 | 
|---|
|  |  |  | for (Map<String, Object> deviceAdjustimingMap:deviceAdjusTimingList) { | 
|---|
|  |  |  | int device_id = Integer.parseInt(deviceAdjustimingMap.get("device_id").toString()); | 
|---|
|  |  |  | String value = deviceAdjustimingMap.get("value").toString(); | 
|---|
|  |  |  | //通过device_id查询device_adjust_value是否存在这台设备 | 
|---|
|  |  |  | List<Map<String, Object>> deviceAdjustValueList = deviceAdjustValueService.getDataByDevice_id(device_id); | 
|---|
|  |  |  | //判断表里原来是否有这台设备的信息,没有的话需要插入 | 
|---|
|  |  |  | if (deviceAdjustValueList.isEmpty()){ | 
|---|
|  |  |  | deviceAdjustValueService.insertData(device_id,value,endTime); | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | deviceAdjustValueService.updateValueByDeviceID(device_id,value); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | ReturnT returnT = new ReturnT(500, "校准值变更成功!"); | 
|---|
|  |  |  | return returnT; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }catch (Exception e){ | 
|---|
|  |  |  | XxlJobLogger.log("DeviceAdjustValueTimingException:" + e.getMessage()); | 
|---|
|  |  |  | logger.error(e.getMessage()); | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | ReturnT returnT = new ReturnT(500, "校准值变更失败!"); | 
|---|
|  |  |  | return returnT; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|