ScheduleTaskUtil.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package com.sooka.sponest.event.utils;
  2. import com.ruoyi.common.core.domain.R;
  3. import com.ruoyi.common.core.utils.DateUtils;
  4. import com.ruoyi.common.core.utils.SpringUtils;
  5. import com.ruoyi.common.core.utils.StringUtils;
  6. import com.ruoyi.common.core.utils.uuid.IdUtils;
  7. import com.ruoyi.common.redis.service.RedisService;
  8. import com.sooka.sponest.event.centereventteventcatalogue.domain.CentereventTDownloads;
  9. import com.sooka.sponest.event.centereventteventcatalogue.domain.CentereventTEventcatalogue;
  10. import com.sooka.sponest.event.centereventteventcatalogue.mapper.CentereventTEventcatalogueMapper;
  11. import com.sooka.sponest.event.centereventteventcatalogue.service.ICentereventTDownloadsService;
  12. import com.sooka.sponest.event.centereventtfirelog.domain.CentereventTFireLog;
  13. import com.sooka.sponest.event.centereventtfirelog.service.ICentereventTFireLogService;
  14. import com.sooka.sponest.message.api.RemoteMonitorService;
  15. import org.apache.commons.lang3.time.FastDateFormat;
  16. import org.slf4j.Logger;
  17. import org.slf4j.LoggerFactory;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.context.annotation.Configuration;
  20. import org.springframework.scheduling.annotation.EnableScheduling;
  21. import org.springframework.scheduling.annotation.Scheduled;
  22. import java.util.List;
  23. import java.util.concurrent.TimeUnit;
  24. /**
  25. * @Auther: mjq
  26. * @Date: 2023/9/12 - 09 - 12 - 10:08
  27. * @Description: com.sooka.sponest.event.utils
  28. * @version: 1.0
  29. */
  30. @Configuration
  31. @EnableScheduling
  32. //@RestController
  33. //@RequestMapping("/testload")
  34. public class ScheduleTaskUtil {
  35. private final Logger logger = LoggerFactory.getLogger(this.getClass());
  36. private static FastDateFormat simpleDateFormat = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
  37. @Autowired
  38. private CentereventTEventcatalogueMapper centereventTEventcatalogueMapper;
  39. @Autowired
  40. private RemoteMonitorService remoteMonitorService;
  41. @Autowired
  42. private ICentereventTFireLogService centereventTFireLogService;
  43. @Autowired
  44. private ICentereventTDownloadsService centereventTDownloadsService;
  45. static final String DOWN_LOCK = "downlod:lock";
  46. static final String VALUE_LOCK = "valueLock";
  47. @Scheduled(cron = "0 10 0 * * ?")
  48. //@GetMapping("/down")
  49. public void runTask() throws Exception {
  50. logger.info("执行定时任务开始=>{}", DateUtils.dateTimeNow());
  51. long startMill = System.currentTimeMillis();
  52. // 读取缓存
  53. RedisService redis = SpringUtils.getBean(RedisService.class);
  54. String lock = redis.getCacheObject(DOWN_LOCK);
  55. if (StringUtils.isBlank(lock)) {
  56. redis.setCacheObject(DOWN_LOCK, VALUE_LOCK);
  57. List<CentereventTEventcatalogue> eventcatalogues = centereventTEventcatalogueMapper.getbeforedateDownload();
  58. for (CentereventTEventcatalogue eventcatalogue : eventcatalogues) {
  59. String busIndex = "bus_indx_other";// 其他
  60. if ("1".equals(eventcatalogue.getEventType())) {
  61. busIndex = "bus_indx_forest";// 火情
  62. }
  63. String uuid = IdUtils.simpleUUID();
  64. R result = remoteMonitorService.getPlaybackByTime(uuid, busIndex, eventcatalogue.getCreateBy(),
  65. DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, DateUtils.subtractTime(eventcatalogue.getReportTime(), -2)),
  66. DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, DateUtils.subtractTime(eventcatalogue.getReportTime(), 1)));
  67. if (200 == result.getCode()) {
  68. //成功
  69. CentereventTFireLog centereventTFireLog = new CentereventTFireLog();
  70. centereventTFireLog.setId(uuid);
  71. centereventTFireLog.setEventCode(eventcatalogue.getEventCode());
  72. centereventTFireLog.setLogContent("系统管理员下载该事件视频。");
  73. centereventTFireLog.setOperation("bus_oper_type_1");
  74. centereventTFireLog.setOperationType("log_oper_type_1");
  75. centereventTFireLogService.insertCentereventTFireLog(centereventTFireLog);
  76. CentereventTDownloads centereventTDownloads = new CentereventTDownloads();
  77. centereventTDownloads.setId(IdUtils.simpleUUID());
  78. centereventTDownloads.setEventCode(eventcatalogue.getEventCode());
  79. centereventTDownloads.setEventName(eventcatalogue.getEventName());
  80. centereventTDownloads.setLogId(uuid);
  81. centereventTDownloadsService.insertCentereventTDownloads(centereventTDownloads);
  82. //TimeUnit.MINUTES.sleep(5);//延时5分钟在调取
  83. Thread.sleep(300000);
  84. } else {
  85. //失败
  86. CentereventTDownloads centereventTDownloads = new CentereventTDownloads();
  87. centereventTDownloads.setId(IdUtils.simpleUUID());
  88. centereventTDownloads.setEventCode(eventcatalogue.getEventCode());
  89. centereventTDownloads.setEventName(eventcatalogue.getEventName());
  90. centereventTDownloads.setFlag("0");
  91. centereventTDownloads.setReason(result.getMsg());//失败原因
  92. centereventTDownloadsService.insertCentereventTDownloads(centereventTDownloads);
  93. }
  94. }
  95. //视频下载日志表 视频下载成功 定时任务结束回调,检验是否真的成功
  96. List<CentereventTDownloads> downloadsList = centereventTDownloadsService.validDownloadIs();
  97. for (CentereventTDownloads down : downloadsList) {
  98. if (StringUtils.isNotEmpty(down.getAttachPath())) {
  99. down.setFlag("1");
  100. down.setPath(down.getAttachPath());
  101. } else {
  102. down.setFlag("0");
  103. down.setReason("视频在下载过程中失败!");
  104. }
  105. centereventTDownloadsService.updateCentereventTDownloads(down);
  106. }
  107. }
  108. //redis.deleteObject(DOWN_LOCK);
  109. redis.setCacheObject(DOWN_LOCK, (System.currentTimeMillis() - startMill), (long) 3600 * 12, TimeUnit.SECONDS);
  110. logger.info("执行定时任务结束=====>{}", DateUtils.dateTimeNow());
  111. }
  112. }