RainDataCountProcessor.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /**
  2. * Copyright 2019 DH
  3. * All right reserved.
  4. * 项目名称: 山洪灾害运维系统
  5. * 创建日期:2023/8/4
  6. */
  7. package org.springblade.jobtask;
  8. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  9. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  10. import lombok.extern.slf4j.Slf4j;
  11. import org.glassfish.jersey.internal.guava.ThreadFactoryBuilder;
  12. import org.springblade.core.tool.utils.Func;
  13. import org.springblade.modules.business.data.dto.RtuDataRainDTO;
  14. import org.springblade.modules.business.data.entity.*;
  15. import org.springblade.modules.business.data.service.*;
  16. import org.springframework.beans.factory.annotation.Value;
  17. import org.springframework.scheduling.annotation.EnableScheduling;
  18. import org.springframework.scheduling.annotation.Scheduled;
  19. import org.springframework.stereotype.Component;
  20. import javax.annotation.Resource;
  21. import java.util.Calendar;
  22. import java.util.Date;
  23. import java.util.LinkedList;
  24. import java.util.List;
  25. import java.util.concurrent.*;
  26. /***
  27. * Date:2023/8/4
  28. * Title: 雨量统计
  29. * Description:统计雨量5分钟数据,生成1小时、3小时、6小时、12小时及24小时成果表
  30. * @author dylan
  31. * @version 1.0
  32. * Remark:认为有必要的其他信息
  33. */
  34. @Slf4j
  35. @Component
  36. @EnableScheduling
  37. public class RainDataCountProcessor {
  38. private static ThreadFactory rainDataCountThreadFactory = new ThreadFactoryBuilder().setNameFormat("rain-data-count-thread-pool-%d").build();
  39. private static ExecutorService rainDataCountThreadPool = new ThreadPoolExecutor(1, 1,
  40. 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(1024), rainDataCountThreadFactory, new ThreadPoolExecutor.AbortPolicy());
  41. @Resource
  42. private IRtuDataRainH1Service h1Service;
  43. @Resource
  44. private IRtuDataRainH3Service h3Service;
  45. @Resource
  46. private IRtuDataRainH6Service h6Service;
  47. @Resource
  48. private IRtuDataRainH12Service h12Service;
  49. @Resource
  50. private IRtuDataRainH24Service h24Service;
  51. @Resource
  52. private IRtuDataRainStoreService rainStoreService;
  53. @Resource
  54. private IRtuDataRainService rainService;
  55. @Value("${spring.task-config.rain-count-task}")
  56. private boolean rainCountTaskRun;
  57. // @Scheduled(cron = "0 0/5 * * * * ")
  58. public void rainDataCountTask() {
  59. try {
  60. if (rainCountTaskRun) {
  61. Date dt = new Date();
  62. log.info("雨量统计任务创建 {}", Func.formatDateTime(dt));
  63. RainDataCountTask task = new RainDataCountTask(dt);
  64. FutureTask<Integer> futureTask = new FutureTask<>(task);
  65. rainDataCountThreadPool.execute(futureTask);
  66. }
  67. } catch (Exception e) {
  68. log.error("{}", e.getMessage());
  69. }
  70. }
  71. private RtuDataRainH1Entity updateH1RainData(Date countTime, String rtuCode, Double drp) {
  72. try {
  73. LambdaQueryWrapper<RtuDataRainH1Entity> rainh1Wrapper = Wrappers.<RtuDataRainH1Entity>query().lambda();
  74. rainh1Wrapper.eq(RtuDataRainH1Entity::getIsDeleted, 0);
  75. rainh1Wrapper.eq(RtuDataRainH1Entity::getRtuCode, rtuCode);
  76. RtuDataRainH1Entity rainH1Entity = h1Service.getOne(rainh1Wrapper);
  77. if (null == rainH1Entity) {
  78. rainH1Entity = new RtuDataRainH1Entity();
  79. rainH1Entity.setRtuCode(rtuCode);
  80. }
  81. rainH1Entity.setDrp(drp);
  82. rainH1Entity.setTm(countTime);
  83. rainH1Entity.setFromTime(countTime);
  84. return rainH1Entity;
  85. } catch (Exception e) {
  86. log.error(e.getMessage());
  87. return null;
  88. }
  89. }
  90. private RtuDataRainH3Entity updateH3RainData(Date countTime, String rtuCode, Double drp) {
  91. try {
  92. LambdaQueryWrapper<RtuDataRainH3Entity> rainh3Wrapper = Wrappers.<RtuDataRainH3Entity>query().lambda();
  93. rainh3Wrapper.eq(RtuDataRainH3Entity::getIsDeleted, 0);
  94. rainh3Wrapper.eq(RtuDataRainH3Entity::getRtuCode, rtuCode);
  95. RtuDataRainH3Entity rainH3Entity = h3Service.getOne(rainh3Wrapper);
  96. if (null == rainH3Entity) {
  97. rainH3Entity = new RtuDataRainH3Entity();
  98. rainH3Entity.setRtuCode(rtuCode);
  99. }
  100. rainH3Entity.setDrp(drp);
  101. rainH3Entity.setTm(countTime);
  102. rainH3Entity.setFromTime(countTime);
  103. return rainH3Entity;
  104. } catch (Exception e) {
  105. log.error(e.getMessage());
  106. return null;
  107. }
  108. }
  109. private RtuDataRainH6Entity updateH6RainData(Date countTime, String rtuCode, Double drp) {
  110. try {
  111. LambdaQueryWrapper<RtuDataRainH6Entity> rainh6Wrapper = Wrappers.<RtuDataRainH6Entity>query().lambda();
  112. rainh6Wrapper.eq(RtuDataRainH6Entity::getIsDeleted, 0);
  113. rainh6Wrapper.eq(RtuDataRainH6Entity::getRtuCode, rtuCode);
  114. RtuDataRainH6Entity rainH6Entity = h6Service.getOne(rainh6Wrapper);
  115. if (null == rainH6Entity) {
  116. rainH6Entity = new RtuDataRainH6Entity();
  117. rainH6Entity.setRtuCode(rtuCode);
  118. }
  119. rainH6Entity.setDrp(drp);
  120. rainH6Entity.setTm(countTime);
  121. rainH6Entity.setFromTime(countTime);
  122. return rainH6Entity;
  123. } catch (Exception e) {
  124. log.error(e.getMessage());
  125. return null;
  126. }
  127. }
  128. private RtuDataRainH12Entity updateH12RainData(Date countTime, String rtuCode, Double drp) {
  129. try {
  130. LambdaQueryWrapper<RtuDataRainH12Entity> rainh12Wrapper = Wrappers.<RtuDataRainH12Entity>query().lambda();
  131. rainh12Wrapper.eq(RtuDataRainH12Entity::getIsDeleted, 0);
  132. rainh12Wrapper.eq(RtuDataRainH12Entity::getRtuCode, rtuCode);
  133. RtuDataRainH12Entity rainH12Entity = h12Service.getOne(rainh12Wrapper);
  134. if (null == rainH12Entity) {
  135. rainH12Entity = new RtuDataRainH12Entity();
  136. rainH12Entity.setRtuCode(rtuCode);
  137. }
  138. rainH12Entity.setDrp(drp);
  139. rainH12Entity.setTm(countTime);
  140. rainH12Entity.setFromTime(countTime);
  141. return rainH12Entity;
  142. } catch (Exception e) {
  143. log.error(e.getMessage());
  144. return null;
  145. }
  146. }
  147. private RtuDataRainH24Entity updateH24RainData(Date countTime, String rtuCode, Double drp) {
  148. try {
  149. LambdaQueryWrapper<RtuDataRainH24Entity> rainh24Wrapper = Wrappers.<RtuDataRainH24Entity>query().lambda();
  150. rainh24Wrapper.eq(RtuDataRainH24Entity::getIsDeleted, 0);
  151. rainh24Wrapper.eq(RtuDataRainH24Entity::getRtuCode, rtuCode);
  152. RtuDataRainH24Entity rainH24Entity = h24Service.getOne(rainh24Wrapper);
  153. if (null == rainH24Entity) {
  154. rainH24Entity = new RtuDataRainH24Entity();
  155. rainH24Entity.setRtuCode(rtuCode);
  156. }
  157. rainH24Entity.setDrp(drp);
  158. rainH24Entity.setTm(countTime);
  159. rainH24Entity.setFromTime(countTime);
  160. return rainH24Entity;
  161. } catch (Exception e) {
  162. log.error(e.getMessage());
  163. return null;
  164. }
  165. }
  166. private class RainDataCountTask implements Callable<Integer> {
  167. private Date countTime;
  168. public RainDataCountTask(Date countTime) {
  169. this.countTime = countTime;
  170. }
  171. @Override
  172. public Integer call() throws Exception {
  173. try {
  174. log.info("雨量统计任务开始执行 {}", Func.formatDateTime(countTime));
  175. LambdaQueryWrapper<RtuDataRainEntity> dataWrapper = Wrappers.<RtuDataRainEntity>query().lambda();
  176. dataWrapper.eq(RtuDataRainEntity::getIsDeleted, 0);
  177. List<RtuDataRainEntity> list = rainService.list(dataWrapper);
  178. if (null != list && list.size() > 0) {
  179. List<RtuDataRainH1Entity> h1EntityList = new LinkedList<>();
  180. List<RtuDataRainH3Entity> h3EntityList = new LinkedList<>();
  181. List<RtuDataRainH6Entity> h6EntityList = new LinkedList<>();
  182. List<RtuDataRainH12Entity> h12EntityList = new LinkedList<>();
  183. List<RtuDataRainH24Entity> h24EntityList = new LinkedList<>();
  184. Calendar calendar = Calendar.getInstance();
  185. //近1小时
  186. calendar.setTime(countTime);
  187. calendar.set(Calendar.SECOND, 0);
  188. calendar.add(Calendar.HOUR_OF_DAY, -1);
  189. log.info("近1小时统计时间 {}", Func.formatDateTime(calendar.getTime()));
  190. Date h1Time = calendar.getTime();
  191. //近3小时
  192. calendar.setTime(countTime);
  193. calendar.set(Calendar.SECOND, 0);
  194. calendar.add(Calendar.HOUR_OF_DAY, -3);
  195. log.info("近3小时统计时间 {}", Func.formatDateTime(calendar.getTime()));
  196. Date h3Time = calendar.getTime();
  197. //近6小时
  198. calendar.setTime(countTime);
  199. calendar.set(Calendar.SECOND, 0);
  200. calendar.add(Calendar.HOUR_OF_DAY, -6);
  201. log.info("近6小时统计时间 {}", Func.formatDateTime(calendar.getTime()));
  202. Date h6Time = calendar.getTime();
  203. //近12小时
  204. calendar.setTime(countTime);
  205. calendar.set(Calendar.SECOND, 0);
  206. calendar.add(Calendar.HOUR_OF_DAY, -12);
  207. log.info("近12小时统计时间 {}", Func.formatDateTime(calendar.getTime()));
  208. Date h12Time = calendar.getTime();
  209. //近24小时
  210. calendar.setTime(countTime);
  211. calendar.set(Calendar.SECOND, 0);
  212. calendar.add(Calendar.HOUR_OF_DAY, -24);
  213. log.info("近24小时统计时间 {}", Func.formatDateTime(countTime));
  214. Date h24Time = calendar.getTime();
  215. for (RtuDataRainEntity entity : list) {
  216. //log.info("upcount code {}", entity.getRtuCode());
  217. Double h1Drp = 0.0;
  218. Double h3Drp = 0.0;
  219. Double h6Drp = 0.0;
  220. Double h12Drp = 0.0;
  221. Double h24Drp = 0.0;
  222. // RtuDataRainDTO dataRainDTO = new RtuDataRainDTO();
  223. // dataRainDTO.setRtuCode(entity.getRtuCode());
  224. // dataRainDTO.setBeginTime(h1Time);
  225. // Double drp = rainStoreService.rainDrpCount(dataRainDTO);
  226. // if (null != drp) {
  227. // h1Drp = drp;
  228. // }
  229. // dataRainDTO.setBeginTime(h3Time);
  230. // drp = rainStoreService.rainDrpCount(dataRainDTO);
  231. // if (null != drp) {
  232. // h3Drp = drp;
  233. // }
  234. // dataRainDTO.setBeginTime(h6Time);
  235. // drp = rainStoreService.rainDrpCount(dataRainDTO);
  236. // if (null != drp) {
  237. // h6Drp = drp;
  238. // }
  239. // dataRainDTO.setBeginTime(h12Time);
  240. // drp = rainStoreService.rainDrpCount(dataRainDTO);
  241. // if (null != drp) {
  242. // h12Drp = drp;
  243. // }
  244. // dataRainDTO.setBeginTime(h24Time);
  245. // drp = rainStoreService.rainDrpCount(dataRainDTO);
  246. // if (null != drp) {
  247. // h24Drp = drp;
  248. // }
  249. RtuDataRainDTO dataRainDTO = new RtuDataRainDTO();
  250. dataRainDTO.setRtuCode(entity.getRtuCode());
  251. dataRainDTO.setBeginTime(h24Time);
  252. List<RtuDataRainStoreEntity> rainStoreEntityList = rainStoreService.selectList(dataRainDTO);
  253. if (rainStoreEntityList != null && rainStoreEntityList.size() > 0) {
  254. for (RtuDataRainStoreEntity rainStoreEntity : rainStoreEntityList) {
  255. //近1小时
  256. if (rainStoreEntity.getTm().after(h1Time)) {
  257. h1Drp += rainStoreEntity.getDrp();
  258. }
  259. //近3小时
  260. if (rainStoreEntity.getTm().after(h3Time)) {
  261. h3Drp += rainStoreEntity.getDrp();
  262. }
  263. //近6小时
  264. if (rainStoreEntity.getTm().after(h6Time)) {
  265. h6Drp += rainStoreEntity.getDrp();
  266. }
  267. //近12小时
  268. if (rainStoreEntity.getTm().after(h12Time)) {
  269. h12Drp += rainStoreEntity.getDrp();
  270. }
  271. //近24小时
  272. h24Drp += rainStoreEntity.getDrp();
  273. }
  274. }
  275. RtuDataRainH1Entity h1Entity = updateH1RainData(countTime, entity.getRtuCode(), h1Drp);
  276. if (h1Entity != null) {
  277. h1EntityList.add(h1Entity);
  278. }
  279. RtuDataRainH3Entity h3Entity = updateH3RainData(countTime, entity.getRtuCode(), h3Drp);
  280. if (h3Entity != null) {
  281. h3EntityList.add(h3Entity);
  282. }
  283. RtuDataRainH6Entity h6Entity = updateH6RainData(countTime, entity.getRtuCode(), h6Drp);
  284. if (h6Entity != null) {
  285. h6EntityList.add(h6Entity);
  286. }
  287. RtuDataRainH12Entity h12Entity = updateH12RainData(countTime, entity.getRtuCode(), h12Drp);
  288. if (h12Entity != null) {
  289. h12EntityList.add(h12Entity);
  290. }
  291. RtuDataRainH24Entity h24Entity = updateH24RainData(countTime, entity.getRtuCode(), h24Drp);
  292. if (h24Entity != null) {
  293. h24EntityList.add(h24Entity);
  294. }
  295. }
  296. h1Service.saveOrUpdateBatch(h1EntityList);
  297. h3Service.saveOrUpdateBatch(h3EntityList);
  298. h6Service.saveOrUpdateBatch(h6EntityList);
  299. h12Service.saveOrUpdateBatch(h12EntityList);
  300. h24Service.saveOrUpdateBatch(h24EntityList);
  301. }
  302. } catch (Exception e) {
  303. e.printStackTrace();
  304. log.error("雨量数据统计任务异常 {}", e.getMessage());
  305. } finally {
  306. log.error("雨量数据统计任务完成 {} 毫秒", System.currentTimeMillis() - countTime.getTime());
  307. }
  308. return 0;
  309. }
  310. }
  311. }