IsolatedPointsFinder.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package com.shkpr.service.alambizplugin.components.checker;
  2. import com.global.base.log.LogLevelFlag;
  3. import com.global.base.log.LogPrintMgr;
  4. import com.shkpr.service.alambizplugin.commtools.BeanUtil;
  5. import com.shkpr.service.alambizplugin.components.GisSurveySystemCheckResultManager;
  6. import com.shkpr.service.alambizplugin.constants.GisSurveySystemCheckResultPath;
  7. import com.shkpr.service.alambizplugin.constants.LogFlagBusiType;
  8. import com.shkpr.service.alambizplugin.dto.GisSurveyLayerApplyLine;
  9. import com.shkpr.service.alambizplugin.dto.GisSurveyLayerApplyPoint;
  10. import com.shkpr.service.alambizplugin.dto.GisSurveySystemCheckElement;
  11. import com.shkpr.service.alambizplugin.dto.GisSurveySystemCheckId;
  12. import com.shkpr.service.alambizplugin.dto.GisSurveySystemCheckResultDetail;
  13. import org.apache.commons.lang3.StringUtils;
  14. import org.springframework.scheduling.annotation.Async;
  15. import org.springframework.scheduling.annotation.AsyncResult;
  16. import org.springframework.stereotype.Component;
  17. import org.springframework.util.concurrent.ListenableFuture;
  18. import java.util.Collections;
  19. import java.util.HashSet;
  20. import java.util.List;
  21. import java.util.Objects;
  22. import java.util.Set;
  23. import java.util.stream.Collectors;
  24. /**
  25. * 寻找孤立点
  26. */
  27. @Component
  28. public class IsolatedPointsFinder {
  29. /**
  30. * log
  31. */
  32. private final String mStrClassName;
  33. private final String mBizType;
  34. private final GisSurveySystemCheckResultManager systemCheckResultManager;
  35. public IsolatedPointsFinder(GisSurveySystemCheckResultManager systemCheckResultManager) {
  36. mStrClassName = "IsolatedPointsFinder";
  37. mBizType = LogFlagBusiType.BUSI_GIS_SURVEY.toStrValue();
  38. this.systemCheckResultManager = systemCheckResultManager;
  39. }
  40. /**
  41. * 寻找孤立点
  42. * <p>根据 {@code code} 上下游匹配,不涉及拓扑点</p><p>标识: {@code code} 唯一</p>
  43. *
  44. * @param points 点集合
  45. * @param lines 线集合
  46. * @param systemCheckId 系统检查id
  47. * @return 孤立点集合
  48. */
  49. @Async
  50. public ListenableFuture<GisSurveySystemCheckResultDetail> findIsolatedPoints(List<GisSurveyLayerApplyPoint> points
  51. , List<GisSurveyLayerApplyLine> lines, GisSurveySystemCheckId systemCheckId) throws InterruptedException {
  52. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName, "开始执行寻找孤立点========>");
  53. long begin = System.currentTimeMillis();
  54. //线为空,点不为空,则全孤立点
  55. if (lines.isEmpty() && !points.isEmpty()) {
  56. List<GisSurveySystemCheckElement> elements = points.parallelStream()
  57. //转为返回元素
  58. .map(point -> BeanUtil.copy(point, GisSurveySystemCheckElement.class))
  59. .collect(Collectors.toList());
  60. return new AsyncResult<>(createResult(elements, systemCheckId, begin));
  61. }
  62. //线为空,点也为空,则无孤立点
  63. if (lines.isEmpty()) return new AsyncResult<>(createResult(Collections.emptyList(), systemCheckId, begin));
  64. //点为空,则无孤立点
  65. if (points.isEmpty()) return new AsyncResult<>(createResult(Collections.emptyList(), systemCheckId, begin));
  66. //计算预期最大容量,避免频繁扩容
  67. int expectedMaxSize = (int) (((long) lines.size() << 1) / .75f + 1);
  68. //联通点code
  69. Set<String> connectedPoints = new HashSet<>(expectedMaxSize);
  70. //遍历所有线,标记参与的点
  71. for (GisSurveyLayerApplyLine line : lines) {
  72. //响应中断
  73. if (Thread.interrupted()) throw new InterruptedException();
  74. //上下游节点判空
  75. if (StringUtils.isBlank(line.getUpNode()) || StringUtils.isBlank(line.getDownNode())) continue;
  76. connectedPoints.add(line.getUpNode());
  77. connectedPoints.add(line.getDownNode());
  78. }
  79. //使用并行流找出孤立点
  80. List<GisSurveySystemCheckElement> elements = points.parallelStream()
  81. //转为返回元素
  82. .map(point -> BeanUtil.copy(point, GisSurveySystemCheckElement.class))
  83. //过滤连通点
  84. .filter(element -> Objects.nonNull(element) && !connectedPoints.contains(element.getCode()))
  85. .collect(Collectors.toList());
  86. return new AsyncResult<>(createResult(elements, systemCheckId, begin));
  87. }
  88. /**
  89. * 创建结果
  90. *
  91. * @param data 数据
  92. * @param systemCheckId 系统检查id
  93. * @param begin 开始时间
  94. * @return 结果
  95. */
  96. private GisSurveySystemCheckResultDetail createResult(List<GisSurveySystemCheckElement> data
  97. , GisSurveySystemCheckId systemCheckId, long begin) {
  98. //数据大小
  99. final int size = data.size();
  100. //写入路径
  101. String path = GisSurveySystemCheckResultPath.ISOLATED_POINTS;
  102. //写入文件
  103. systemCheckResultManager.writeResult(data, systemCheckId, path);
  104. long end = System.currentTimeMillis();
  105. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
  106. , String.format(
  107. "结束执行寻找孤立点,用时(毫秒):%d"
  108. , (end - begin)
  109. )
  110. );
  111. //构建结果
  112. return new GisSurveySystemCheckResultDetail(true, GisSurveySystemCheckResultPath.relativePath(systemCheckId, path), size);
  113. }
  114. }