GisSurveySystemChecker.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. package com.shkpr.service.alambizplugin.components;
  2. import com.fasterxml.jackson.core.type.TypeReference;
  3. import com.global.base.log.LogLevelFlag;
  4. import com.global.base.log.LogPrintMgr;
  5. import com.shkpr.service.alambizplugin.apiparam.GisSurveyCheckParams;
  6. import com.shkpr.service.alambizplugin.components.checker.*;
  7. import com.shkpr.service.alambizplugin.constants.CommAsyncStatusEnum;
  8. import com.shkpr.service.alambizplugin.constants.GisSurveyCheckTypeEnum;
  9. import com.shkpr.service.alambizplugin.constants.GisSurveySystemCheckKeys;
  10. import com.shkpr.service.alambizplugin.constants.LogFlagBusiType;
  11. import com.shkpr.service.alambizplugin.dbdao.services.intef.GisSurveyLayerApplyService;
  12. import com.shkpr.service.alambizplugin.dbdao.services.intef.TypeDefineService;
  13. import com.shkpr.service.alambizplugin.dto.*;
  14. import lombok.extern.slf4j.Slf4j;
  15. import org.springframework.scheduling.annotation.Async;
  16. import org.springframework.scheduling.annotation.AsyncResult;
  17. import org.springframework.stereotype.Component;
  18. import org.springframework.util.concurrent.ListenableFuture;
  19. import java.nio.file.Files;
  20. import java.nio.file.Path;
  21. import java.time.Duration;
  22. import java.time.LocalDateTime;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.Objects;
  27. import java.util.concurrent.ExecutionException;
  28. import java.util.function.Consumer;
  29. /**
  30. * 系统检查执行器
  31. *
  32. * @author 欧阳劲驰
  33. * @since 1.0.0
  34. */
  35. @Component
  36. @Slf4j
  37. public class GisSurveySystemChecker {
  38. /**
  39. * log
  40. */
  41. private final String mStrClassName;
  42. private final String mBizType;
  43. private final AsyncResultManager asyncResultManager;
  44. private final GisSurveyLayerApplyService gisSurveyLayerApplyService;
  45. private final TypeDefineService typeDefineService;
  46. private final InvalidPointsFinder invalidPointsFinder;
  47. private final IsolatedPointsFinder isolatedPointsFinder;
  48. private final IsolatedLinesFinder isolatedLinesFinder;
  49. private final DuplicatePointsFinder duplicatePointsFinder;
  50. private final InvalidLinesFinder invalidLinesFinder;
  51. private final OverlapLinesFinder overlapLinesFinder;
  52. private final CrossLinesFinder crossLinesFinder;
  53. private final ElevationDiffFinder elevationDiffFinder;
  54. public GisSurveySystemChecker(AsyncResultManager asyncResultManager
  55. , GisSurveyLayerApplyService gisSurveyLayerApplyService, TypeDefineService typeDefineService
  56. , InvalidPointsFinder invalidPointsFinder, IsolatedPointsFinder isolatedPointsFinder
  57. , IsolatedLinesFinder isolatedLinesFinder, DuplicatePointsFinder duplicatePointsFinder
  58. , InvalidLinesFinder invalidLinesFinder, OverlapLinesFinder overlapLinesFinder
  59. , CrossLinesFinder crossLinesFinder, ElevationDiffFinder elevationDiffFinder) {
  60. mStrClassName = "GisSurveySystemChecker";
  61. mBizType = LogFlagBusiType.BUSI_GIS_SURVEY.toStrValue();
  62. this.asyncResultManager = asyncResultManager;
  63. this.gisSurveyLayerApplyService = gisSurveyLayerApplyService;
  64. this.typeDefineService = typeDefineService;
  65. this.invalidPointsFinder = invalidPointsFinder;
  66. this.isolatedPointsFinder = isolatedPointsFinder;
  67. this.isolatedLinesFinder = isolatedLinesFinder;
  68. this.duplicatePointsFinder = duplicatePointsFinder;
  69. this.invalidLinesFinder = invalidLinesFinder;
  70. this.overlapLinesFinder = overlapLinesFinder;
  71. this.crossLinesFinder = crossLinesFinder;
  72. this.elevationDiffFinder = elevationDiffFinder;
  73. }
  74. /**
  75. * 系统检查任务
  76. *
  77. * @param params 系统检查参数
  78. * @param systemCheckDefines 系统检查定义
  79. * @param onStartSubtask 启动子任务
  80. * @param onDeprecatedSubtask 弃用子任务(完成数据收集/取消)
  81. * @return 系统检查返回
  82. */
  83. @Async
  84. public ListenableFuture<CommAsyncResult<Map<String, GisSurveySystemCheckResultDetail>>> systemCheckTask(
  85. GisSurveyCheckParams params, List<GisSurveySystemCheckDefine> systemCheckDefines
  86. , Consumer<Map<String, ListenableFuture<GisSurveySystemCheckResultDetail>>> onStartSubtask
  87. , Consumer<GisSurveySystemCheckId> onDeprecatedSubtask) {
  88. //系统检查id
  89. GisSurveySystemCheckId systemCheckId = GisSurveySystemCheckId.generateId(params);
  90. if (systemCheckId == null) return new AsyncResult<>(CommAsyncResult.fail(null));
  91. //构建返回
  92. CommAsyncResult<Map<String, GisSurveySystemCheckResultDetail>> result = CommAsyncResult.fail(systemCheckId.toString());
  93. result.setOperator(params.getOperator());
  94. //无效点任务
  95. ListenableFuture<GisSurveySystemCheckResultDetail> invalidPointsFuture = null;
  96. //孤立点任务
  97. ListenableFuture<GisSurveySystemCheckResultDetail> isolatedPointsFuture = null;
  98. //孤立线任务
  99. ListenableFuture<GisSurveySystemCheckResultDetail> isolatedLinesFuture = null;
  100. //重复点任务
  101. ListenableFuture<GisSurveySystemCheckResultDetail> duplicatePointsFuture = null;
  102. //无效线任务
  103. ListenableFuture<GisSurveySystemCheckResultDetail> invalidLinesFuture = null;
  104. //重叠线任务
  105. ListenableFuture<GisSurveySystemCheckResultDetail> overlapLinesFuture = null;
  106. //交叉线任务
  107. ListenableFuture<GisSurveySystemCheckResultDetail> crossLinesFuture = null;
  108. //高程差任务
  109. ListenableFuture<GisSurveySystemCheckResultDetail> elevationDiffFuture = null;
  110. //点集合
  111. List<GisSurveyLayerApplyPoint> points = null;
  112. //线集合
  113. List<GisSurveyLayerApplyLine> lines = null;
  114. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
  115. , String.format(
  116. "开始执行系统检查;检查类型 检查类型:%s code:%s"
  117. , systemCheckId.getCheckType().getFlag()
  118. , systemCheckId.getCode()
  119. )
  120. );
  121. try {
  122. //根据项目查询点线信息
  123. if (GisSurveyCheckTypeEnum.PROJECT == systemCheckId.getCheckType()) {
  124. points = gisSurveyLayerApplyService.findAddPointByProjId(systemCheckId.getCode());
  125. lines = gisSurveyLayerApplyService.findAddLineByProjId(systemCheckId.getCode());
  126. }
  127. //根据任务查询点线信息
  128. else if (GisSurveyCheckTypeEnum.JOB == systemCheckId.getCheckType()) {
  129. points = gisSurveyLayerApplyService.findAddPointByJobId(systemCheckId.getCode());
  130. lines = gisSurveyLayerApplyService.findAddLineByJobId(systemCheckId.getCode());
  131. }
  132. //查询经纬度类型定义
  133. List<TypeDefine> typeDefines = typeDefineService.findLatLng();
  134. //孤立点定义
  135. GisSurveySystemCheckDefine isolatedPointsDefine = systemCheckDefines.stream().
  136. filter(it -> Objects.equals(GisSurveySystemCheckKeys.ISOLATED_POINTS, it.getKey()))
  137. .findFirst().orElse(null);
  138. //无效线定义
  139. GisSurveySystemCheckDefine invalidLinesDefine = systemCheckDefines.stream().
  140. filter(it -> Objects.equals(GisSurveySystemCheckKeys.INVALID_LINES, it.getKey()))
  141. .findFirst().orElse(null);
  142. //重复点定义
  143. GisSurveySystemCheckDefine duplicatePointsDefine = systemCheckDefines.stream().
  144. filter(it -> Objects.equals(GisSurveySystemCheckKeys.DUPLICATE_POINTS, it.getKey()))
  145. .findFirst().orElse(null);
  146. //结果flag
  147. final String FLAG = systemCheckId.getFlag();
  148. //创建临时文件夹
  149. if (!asyncResultManager.createTempDirectory(FLAG))
  150. return new AsyncResult<>(result);
  151. //子任务
  152. HashMap<String, ListenableFuture<GisSurveySystemCheckResultDetail>> subtask = new HashMap<>();
  153. //无效点检查
  154. if (points != null) {
  155. if (params.getStartSubitemKeys().contains(GisSurveySystemCheckKeys.INVALID_POINTS)) {
  156. invalidPointsFuture = invalidPointsFinder.finderInvalidPoints(points, systemCheckId);
  157. subtask.put(GisSurveySystemCheckKeys.INVALID_POINTS, invalidPointsFuture);
  158. }
  159. }
  160. //孤立点检查
  161. if (points != null && lines != null) {
  162. if (params.getStartSubitemKeys().contains(GisSurveySystemCheckKeys.ISOLATED_POINTS)) {
  163. isolatedPointsFuture = isolatedPointsFinder.findIsolatedPoints(points, lines, isolatedPointsDefine, systemCheckId);
  164. subtask.put(GisSurveySystemCheckKeys.ISOLATED_POINTS, isolatedPointsFuture);
  165. }
  166. }
  167. //无效线检查
  168. if (points != null && lines != null) {
  169. if (params.getStartSubitemKeys().contains(GisSurveySystemCheckKeys.INVALID_LINES)) {
  170. invalidLinesFuture = invalidLinesFinder.finderInvalidLines(points, lines, invalidLinesDefine, systemCheckId);
  171. subtask.put(GisSurveySystemCheckKeys.INVALID_LINES, invalidLinesFuture);
  172. }
  173. }
  174. //重复点检查
  175. if (points != null) {
  176. if (params.getStartSubitemKeys().contains(GisSurveySystemCheckKeys.DUPLICATE_POINTS)) {
  177. duplicatePointsFuture = duplicatePointsFinder.findDuplicatePoints(points, typeDefines, duplicatePointsDefine, systemCheckId);
  178. subtask.put(GisSurveySystemCheckKeys.DUPLICATE_POINTS, duplicatePointsFuture);
  179. }
  180. }
  181. if (lines != null) {
  182. //孤立线检查
  183. if (params.getStartSubitemKeys().contains(GisSurveySystemCheckKeys.ISOLATED_LINES)) {
  184. isolatedLinesFuture = isolatedLinesFinder.findIsolatedLines(lines, systemCheckId);
  185. subtask.put(GisSurveySystemCheckKeys.ISOLATED_LINES, isolatedLinesFuture);
  186. }
  187. //重叠线检查
  188. if (params.getStartSubitemKeys().contains(GisSurveySystemCheckKeys.OVERLAP_LINES)) {
  189. overlapLinesFuture = overlapLinesFinder.findOverlapLines(lines, systemCheckId);
  190. subtask.put(GisSurveySystemCheckKeys.OVERLAP_LINES, overlapLinesFuture);
  191. }
  192. //交叉线检查
  193. if (params.getStartSubitemKeys().contains(GisSurveySystemCheckKeys.CROSS_LINES)) {
  194. crossLinesFuture = crossLinesFinder.findCrossLines(lines, systemCheckId);
  195. subtask.put(GisSurveySystemCheckKeys.CROSS_LINES, crossLinesFuture);
  196. }
  197. //高程差异常检查
  198. if (params.getStartSubitemKeys().contains(GisSurveySystemCheckKeys.ELEVATION_DIFF)) {
  199. elevationDiffFuture = elevationDiffFinder.findElevationDiff(lines, systemCheckId);
  200. subtask.put(GisSurveySystemCheckKeys.ELEVATION_DIFF, elevationDiffFuture);
  201. }
  202. }
  203. //返回子任务
  204. onStartSubtask.accept(subtask);
  205. //获取上次结果
  206. CommAsyncResult<Map<String, GisSurveySystemCheckResultDetail>> lastResult = asyncResultManager.getResult(FLAG, new TypeReference<CommAsyncResult<Map<String, GisSurveySystemCheckResultDetail>>>() {
  207. });
  208. //设置数据
  209. Map<String, GisSurveySystemCheckResultDetail> data = new HashMap<>();
  210. if (lastResult != null && lastResult.getData() != null) data = lastResult.getData();
  211. //设置数据时间
  212. Map<String, LocalDateTime> refreshTimes = new HashMap<>();
  213. if (lastResult != null && lastResult.getRefreshTimes() != null) refreshTimes = lastResult.getRefreshTimes();
  214. //等待结果,并存入返回,优先监听点相关(执行速度快且只需要点集合,优先释放点集合内存)
  215. if (invalidPointsFuture != null) {
  216. //监听无效点
  217. final GisSurveySystemCheckResultDetail invalidPointsResult = invalidPointsFuture.get();
  218. data.put(GisSurveySystemCheckKeys.INVALID_POINTS, invalidPointsResult);
  219. refreshTimes.put(GisSurveySystemCheckKeys.INVALID_POINTS, params.getRefreshTime());
  220. //释放点缓存
  221. clearPoints(points, invalidPointsFuture, isolatedPointsFuture, duplicatePointsFuture, invalidLinesFuture);
  222. }
  223. if (isolatedPointsFuture != null) {
  224. //监听孤立点
  225. final GisSurveySystemCheckResultDetail isolatedPointsResult = isolatedPointsFuture.get();
  226. data.put(GisSurveySystemCheckKeys.ISOLATED_POINTS, isolatedPointsResult);
  227. refreshTimes.put(GisSurveySystemCheckKeys.ISOLATED_POINTS, params.getRefreshTime());
  228. //释放点缓存
  229. clearPoints(points, invalidPointsFuture, isolatedPointsFuture, duplicatePointsFuture, invalidLinesFuture);
  230. }
  231. if (duplicatePointsFuture != null) {
  232. //监听重复点
  233. final GisSurveySystemCheckResultDetail duplicatePointsResult = duplicatePointsFuture.get();
  234. data.put(GisSurveySystemCheckKeys.DUPLICATE_POINTS, duplicatePointsResult);
  235. refreshTimes.put(GisSurveySystemCheckKeys.DUPLICATE_POINTS, params.getRefreshTime());
  236. //释放点缓存
  237. clearPoints(points, invalidPointsFuture, isolatedPointsFuture, duplicatePointsFuture, invalidLinesFuture);
  238. }
  239. if (isolatedLinesFuture != null) {
  240. //监听孤立线
  241. final GisSurveySystemCheckResultDetail isolatedLinesResult = isolatedLinesFuture.get();
  242. data.put(GisSurveySystemCheckKeys.ISOLATED_LINES, isolatedLinesResult);
  243. refreshTimes.put(GisSurveySystemCheckKeys.ISOLATED_LINES, params.getRefreshTime());
  244. }
  245. if (invalidLinesFuture != null) {
  246. //监听无效线
  247. final GisSurveySystemCheckResultDetail invalidLinesResult = invalidLinesFuture.get();
  248. data.put(GisSurveySystemCheckKeys.INVALID_LINES, invalidLinesResult);
  249. refreshTimes.put(GisSurveySystemCheckKeys.INVALID_LINES, params.getRefreshTime());
  250. //释放点缓存
  251. clearPoints(points, invalidPointsFuture, isolatedPointsFuture, duplicatePointsFuture, invalidLinesFuture);
  252. }
  253. if (overlapLinesFuture != null) {
  254. //监听重叠线
  255. final GisSurveySystemCheckResultDetail overlapLinesResult = overlapLinesFuture.get();
  256. data.put(GisSurveySystemCheckKeys.OVERLAP_LINES, overlapLinesResult);
  257. refreshTimes.put(GisSurveySystemCheckKeys.OVERLAP_LINES, params.getRefreshTime());
  258. }
  259. if (crossLinesFuture != null) {
  260. //监听交叉线
  261. final GisSurveySystemCheckResultDetail crossLinesResult = crossLinesFuture.get();
  262. data.put(GisSurveySystemCheckKeys.CROSS_LINES, crossLinesResult);
  263. refreshTimes.put(GisSurveySystemCheckKeys.CROSS_LINES, params.getRefreshTime());
  264. }
  265. if (elevationDiffFuture != null) {
  266. //监听高程差异常
  267. final GisSurveySystemCheckResultDetail elevationDiffResult = elevationDiffFuture.get();
  268. data.put(GisSurveySystemCheckKeys.ELEVATION_DIFF, elevationDiffResult);
  269. refreshTimes.put(GisSurveySystemCheckKeys.ELEVATION_DIFF, params.getRefreshTime());
  270. }
  271. //完成检查
  272. result.setStatus(CommAsyncStatusEnum.SUCCESS.getCode());
  273. result.setCompleteTime(LocalDateTime.now());
  274. result.setData(data);
  275. result.setRefreshTimes(refreshTimes);
  276. result.setSubitemKeys(params.getSubitemKeys());
  277. //写入结果
  278. Path resultPath = asyncResultManager.writeJson(result, FLAG, AsyncResultManager.RESULT_FILE_NAME);
  279. if (resultPath == null || !Files.exists(resultPath)) {
  280. //弃用子任务
  281. onDeprecatedSubtask.accept(systemCheckId);
  282. //打印报错信息
  283. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, mBizType, mStrClassName
  284. , String.format("第三方导出文件写入失败, 任务id:%s", systemCheckId)
  285. );
  286. return new AsyncResult<>(CommAsyncResult.fail(systemCheckId.toString()));
  287. }
  288. //替换结果
  289. if (!asyncResultManager.replaceResult(FLAG)) {
  290. //弃用子任务
  291. onDeprecatedSubtask.accept(systemCheckId);
  292. return new AsyncResult<>(CommAsyncResult.fail(systemCheckId.toString()));
  293. }
  294. //弃用子任务
  295. onDeprecatedSubtask.accept(systemCheckId);
  296. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
  297. , String.format(
  298. "结束执行系统检查;检查类型: %s, code:%s , 用时(毫秒):%d",
  299. systemCheckId.getCheckType().getFlag(),
  300. systemCheckId.getCode(),
  301. Duration.between(result.getRequestTime(), result.getCompleteTime()).toMillis()
  302. )
  303. );
  304. return new AsyncResult<>(result);
  305. } catch (InterruptedException | ExecutionException e) {
  306. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, mBizType, mStrClassName
  307. , String.format(
  308. "监测到中断或执行异常,开始清除子任务 检查类型:%s, code:%s ,error:%s",
  309. systemCheckId.getCheckType().getFlag(),
  310. systemCheckId.getCode(),
  311. e
  312. )
  313. );
  314. log.error("StackTrac:", e);
  315. //清除子任务
  316. if (invalidPointsFuture != null) invalidPointsFuture.cancel(true);
  317. if (isolatedPointsFuture != null) isolatedPointsFuture.cancel(true);
  318. if (isolatedLinesFuture != null) isolatedLinesFuture.cancel(true);
  319. if (duplicatePointsFuture != null) duplicatePointsFuture.cancel(true);
  320. if (invalidLinesFuture != null) invalidLinesFuture.cancel(true);
  321. if (overlapLinesFuture != null) overlapLinesFuture.cancel(true);
  322. if (crossLinesFuture != null) crossLinesFuture.cancel(true);
  323. if (elevationDiffFuture != null) elevationDiffFuture.cancel(true);
  324. //失败信息
  325. result.setStatus(CommAsyncStatusEnum.FAIL.getCode());
  326. result.setCompleteTime(LocalDateTime.now());
  327. //弃用子任务
  328. onDeprecatedSubtask.accept(systemCheckId);
  329. return new AsyncResult<>(result);
  330. }
  331. }
  332. /**
  333. * 清除点缓存
  334. *
  335. * @param points 点集合
  336. * @param invalidPointsFuture 无效点任务
  337. * @param isolatedPointsFuture 孤立点任务
  338. * @param duplicatePointsFuture 重复点任务
  339. * @param invalidLinesFuture 无效线任务
  340. */
  341. public void clearPoints(List<GisSurveyLayerApplyPoint> points
  342. , ListenableFuture<GisSurveySystemCheckResultDetail> invalidPointsFuture, ListenableFuture<GisSurveySystemCheckResultDetail> isolatedPointsFuture
  343. , ListenableFuture<GisSurveySystemCheckResultDetail> duplicatePointsFuture
  344. , ListenableFuture<GisSurveySystemCheckResultDetail> invalidLinesFuture) {
  345. //点相关任务都完成,则清除缓存
  346. if (invalidPointsFuture != null && invalidPointsFuture.isDone() &&
  347. isolatedPointsFuture != null && isolatedPointsFuture.isDone() &&
  348. duplicatePointsFuture != null && duplicatePointsFuture.isDone() &&
  349. invalidLinesFuture != null && invalidLinesFuture.isDone()) points.clear();
  350. }
  351. }