package com.sooka.sponest.event.utils; import com.ruoyi.common.core.domain.R; import com.ruoyi.common.core.utils.DateUtils; import com.ruoyi.common.core.utils.SpringUtils; import com.ruoyi.common.core.utils.StringUtils; import com.ruoyi.common.core.utils.uuid.IdUtils; import com.ruoyi.common.redis.service.RedisService; import com.sooka.sponest.event.centereventteventcatalogue.domain.CentereventTDownloads; import com.sooka.sponest.event.centereventteventcatalogue.domain.CentereventTEventcatalogue; import com.sooka.sponest.event.centereventteventcatalogue.mapper.CentereventTEventcatalogueMapper; import com.sooka.sponest.event.centereventteventcatalogue.service.ICentereventTDownloadsService; import com.sooka.sponest.event.centereventtfirelog.domain.CentereventTFireLog; import com.sooka.sponest.event.centereventtfirelog.service.ICentereventTFireLogService; import com.sooka.sponest.message.api.RemoteMonitorService; import org.apache.commons.lang3.time.FastDateFormat; import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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 java.util.List; import java.util.concurrent.TimeUnit; /** * @Auther: mjq * @Date: 2023/9/12 - 09 - 12 - 10:08 * @Description: com.sooka.sponest.event.utils * @version: 1.0 */ @Configuration @EnableScheduling //@RestController //@RequestMapping("/testload") public class ScheduleTaskUtil { private final Logger logger = LoggerFactory.getLogger(this.getClass()); private static FastDateFormat simpleDateFormat = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss"); @Autowired private CentereventTEventcatalogueMapper centereventTEventcatalogueMapper; @Autowired private RemoteMonitorService remoteMonitorService; @Autowired private ICentereventTFireLogService centereventTFireLogService; @Autowired private ICentereventTDownloadsService centereventTDownloadsService; static final String DOWN_LOCK = "downlod:lock"; static final String VALUE_LOCK = "valueLock"; @Scheduled(cron = "0 10 0 * * ?") //@GetMapping("/down") public void runTask() throws Exception { logger.info("执行定时任务开始=>{}", DateUtils.dateTimeNow()); long startMill = System.currentTimeMillis(); // 读取缓存 RedisService redis = SpringUtils.getBean(RedisService.class); String lock = redis.getCacheObject(DOWN_LOCK); if (StringUtils.isBlank(lock)) { redis.setCacheObject(DOWN_LOCK, VALUE_LOCK); List eventcatalogues = centereventTEventcatalogueMapper.getbeforedateDownload(); for (CentereventTEventcatalogue eventcatalogue : eventcatalogues) { String busIndex = "bus_indx_other";// 其他 if ("1".equals(eventcatalogue.getEventType())) { busIndex = "bus_indx_forest";// 火情 } String uuid = IdUtils.simpleUUID(); R result = remoteMonitorService.getPlaybackByTime(uuid, busIndex, eventcatalogue.getCreateBy(), DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, DateUtils.subtractTime(eventcatalogue.getReportTime(), -2)), DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, DateUtils.subtractTime(eventcatalogue.getReportTime(), 1))); if (200 == result.getCode()) { //成功 CentereventTFireLog centereventTFireLog = new CentereventTFireLog(); centereventTFireLog.setId(uuid); centereventTFireLog.setEventCode(eventcatalogue.getEventCode()); centereventTFireLog.setLogContent("系统管理员下载该事件视频。"); centereventTFireLog.setOperation("bus_oper_type_1"); centereventTFireLog.setOperationType("log_oper_type_1"); centereventTFireLogService.insertCentereventTFireLog(centereventTFireLog); CentereventTDownloads centereventTDownloads = new CentereventTDownloads(); centereventTDownloads.setId(IdUtils.simpleUUID()); centereventTDownloads.setEventCode(eventcatalogue.getEventCode()); centereventTDownloads.setEventName(eventcatalogue.getEventName()); centereventTDownloads.setLogId(uuid); centereventTDownloadsService.insertCentereventTDownloads(centereventTDownloads); //TimeUnit.MINUTES.sleep(5);//延时5分钟在调取 Thread.sleep(300000); } else { //失败 CentereventTDownloads centereventTDownloads = new CentereventTDownloads(); centereventTDownloads.setId(IdUtils.simpleUUID()); centereventTDownloads.setEventCode(eventcatalogue.getEventCode()); centereventTDownloads.setEventName(eventcatalogue.getEventName()); centereventTDownloads.setFlag("0"); centereventTDownloads.setReason(result.getMsg());//失败原因 centereventTDownloadsService.insertCentereventTDownloads(centereventTDownloads); } } //视频下载日志表 视频下载成功 定时任务结束回调,检验是否真的成功 List downloadsList = centereventTDownloadsService.validDownloadIs(); for (CentereventTDownloads down : downloadsList) { if (StringUtils.isNotEmpty(down.getAttachPath())) { down.setFlag("1"); down.setPath(down.getAttachPath()); } else { down.setFlag("0"); down.setReason("视频在下载过程中失败!"); } centereventTDownloadsService.updateCentereventTDownloads(down); } } //redis.deleteObject(DOWN_LOCK); redis.setCacheObject(DOWN_LOCK, (System.currentTimeMillis() - startMill), (long) 3600 * 12, TimeUnit.SECONDS); logger.info("执行定时任务结束=====>{}", DateUtils.dateTimeNow()); } }