package com.boying.job; import cn.hutool.core.lang.UUID; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.boying.entity.SnmpInfo; import com.boying.entity.SystemInfo; import com.boying.entity.WarnLog; import com.boying.service.SnmpInfoService; import com.boying.service.SystemInfoService; import com.boying.service.WarnService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.time.LocalDate; import java.time.LocalDateTime; import java.util.List; /** * @author kdq * @version 1.0.0 * @ClassName YunWeiSchheduled.java * @Description TODO * @createTime 2023年07月01日 18:09:00 */ @Slf4j @Component public class YunWeiSchheduled { @Autowired private SystemInfoService systemInfoService; @Autowired private SnmpInfoService snmpInfoService; @Autowired private WarnService warnService; private Boolean taskFlag = false; @Scheduled(cron = "0 0/3 * * * ?") public void execute() throws Exception { if (taskFlag == true) { System.out.println("正在运行,强制退出-------》"); return; }else{ QueryWrapper wrapper = new QueryWrapper<>(); wrapper.select(" id,host_name,state"); wrapper.lambda().eq(SystemInfo::getState,"2"); List list = systemInfoService.list(wrapper); for (SystemInfo systemInfo : list) { WarnLog warnLog = new WarnLog(); warnLog.setId(UUID.fastUUID().toString(true)); warnLog.setWarnDate(LocalDateTime.now()); warnLog.setName(systemInfo.getHostName()); warnLog.setContent("主机掉线"); warnLog.setType(1); warnService.save(warnLog); } QueryWrapper wrapper1 = new QueryWrapper<>(); wrapper1.select(" id,host_name,state"); wrapper1.lambda().eq(SnmpInfo::getState,"2"); List list1 = snmpInfoService.list(wrapper1); for (SnmpInfo snmpInfo : list1) { WarnLog warnLog = new WarnLog(); warnLog.setId(UUID.fastUUID().toString(true)); warnLog.setWarnDate(LocalDateTime.now()); warnLog.setName(snmpInfo.getHostName()); warnLog.setContent("设备掉线"); warnLog.setType(2); warnService.save(warnLog); } } } }