package com.szwl.controller; import com.szwl.mapper.TLocationCheckMapper; import com.szwl.model.entity.TLocationCheck; import com.szwl.service.*; import com.szwl.service.es.EsTCoinOrderService; import com.szwl.service.es.EsTOrderService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Calendar; import java.util.List; @Configuration //1.主要用于标记配置类,兼备Component的效果。 @Component @EnableScheduling // 2.开启定时任务 public class ScheduledService { @Autowired private TDepartmentService tDepartmentService; @Autowired EsTCoinOrderService esTCoinOrderService; @Autowired EsTOrderService esTOrderService; @Autowired THotUpdateService hotUpdateService; @Autowired TEquipmentService equipmentService; @Resource TLocationCheckMapper locationCheckMapper; @Resource TLocationCheckService locationCheckService; @Resource TWechatService wechatService; // 每周二上午8点10分去校验设备位置 @Scheduled(cron = "0 10 8 ? * TUE") public void equipmentLocCheck() throws IOException { if (isDo()) { // List locationChecks = locationCheckMapper.selectNotNullIp(); List locationChecks = locationCheckMapper.selectNotNullAddr(); // String locErrorEq = null; StringBuilder locErrorEq = new StringBuilder(); for (TLocationCheck locationChc : locationChecks) { String clientId = locationChc.getClientId(); String addr = locationChc.getAddr(); // 这里不需要更新ip,只需要校验有ip的设备位置对不对 // locationCheckService.locCheckMsg(locationChc, clientId, addr); String check = locationCheckService.schLocCheck(locationChc, clientId, addr); locErrorEq.append(check); } if (!locErrorEq.toString().isEmpty()) { locationCheckService.schSendMsg(locErrorEq.toString()); } // Optional.ofNullable(locErrorEq) // .filter(stringBuilder -> stringBuilder.length() > 0) // .ifPresent(stringBuilder -> { // try { // locationCheckService.schSendMsg(locErrorEq.toString()); // } catch (IOException e) { // throw new RuntimeException(e); // } // }); } } // 每天4:30去检查多少设备进行了热更新 // @Scheduled(cron = "0 30 4 * * ?") // public void haveUpdateNum() { // // 遍历所有设备的apk_version,看多少台设备的apk_version = patch_veriosn // // 获取热更新表中所有的 patch_version 信息 // List patchVersions = hotUpdateService.list().stream().map(THotUpdate::getPatchVersion).collect(Collectors.toList()); // for (String patchVersion : patchVersions) { // // 查询所有设备表中的的 apk_version 等于 patch_version的 // List equipmentList = equipmentService.lambdaQuery().eq(TEquipment::getApkVersion, patchVersion).list(); // // 计算当前的 patch_version = apk_version 的设备数量 // int count = equipmentList.size(); // // THotUpdate hotUpdate = hotUpdateService.lambdaQuery().eq(THotUpdate::getPatchVersion, patchVersion).one(); // if (hotUpdate != null) { // hotUpdate.setHaveUpdateNum(count); // hotUpdateService.updateById(hotUpdate); // } // } // } // 值日通知1 // 每天8:20执行一次 @Scheduled(cron = "0 20 8 * * ?") public void scheduled() { if (isDo()) { Calendar calendar = Calendar.getInstance(); int number = calendar.get(Calendar.DAY_OF_WEEK); // 如果不是周日 1 if (number != 1) { tDepartmentService.onTime(); } } } private Boolean isDo() { try { String hostAddress = InetAddress.getLocalHost().getHostAddress(); // 弹性112.74.63.148服务器的私网ip if (hostAddress.equals("10.0.0.153")) { return true; } else { return false; } } catch (UnknownHostException e) { e.printStackTrace(); } return false; } // 值日通知2 // 每天17:55执行一次 @Scheduled(cron = "0 55 17 * * ?") public void scheduled2() { if (isDo()) { Calendar calendar = Calendar.getInstance(); int number = calendar.get(Calendar.DAY_OF_WEEK); if (number != 1) { tDepartmentService.onTime2(); } } } // 设备状态检测 @Scheduled(cron = "0 30 * * * ?") public void checkEquipmentStatus() { if (isDo()) { equipmentService.checkEquipmentStatus(); } } /** * 定时更新access_token */ @Scheduled(cron = "0 20 * * * ?") public void updateAccessToken() { if (isDo()) { wechatService.getAccessToken("0"); wechatService.getAccessToken("1"); } } }