GisSurveyThirdImporter.java 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. package com.shkpr.service.alambizplugin.components;
  2. import com.global.base.log.LogLevelFlag;
  3. import com.global.base.log.LogPrintMgr;
  4. import com.shkpr.service.alambizplugin.apiparam.GisSurveyThirdImportParams;
  5. import com.shkpr.service.alambizplugin.commtools.CRSUtil;
  6. import com.shkpr.service.alambizplugin.commtools.ExcelUtils;
  7. import com.shkpr.service.alambizplugin.commtools.PointCodeUtil;
  8. import com.shkpr.service.alambizplugin.commtools.ThirdImportTemplateUtils;
  9. import com.shkpr.service.alambizplugin.commtools.norule.JPSurveyApplyColumn;
  10. import com.shkpr.service.alambizplugin.commtools.norule.RuleFormatSetting;
  11. import com.shkpr.service.alambizplugin.commtools.norule.SurveyCommonAssistant;
  12. import com.shkpr.service.alambizplugin.components.checker.DuplicatePointsFinder;
  13. import com.shkpr.service.alambizplugin.components.checker.InvalidLinesFinder;
  14. import com.shkpr.service.alambizplugin.components.checker.InvalidPropertiesFinder;
  15. import com.shkpr.service.alambizplugin.constants.CommAsyncStatusEnum;
  16. import com.shkpr.service.alambizplugin.constants.CommCRSDefine;
  17. import com.shkpr.service.alambizplugin.constants.ExcelEnum;
  18. import com.shkpr.service.alambizplugin.constants.GisMetadataDefine;
  19. import com.shkpr.service.alambizplugin.constants.GisSurveyExcelDefine;
  20. import com.shkpr.service.alambizplugin.constants.GisSurveyImportStatusEnum;
  21. import com.shkpr.service.alambizplugin.constants.LogFlagBusiType;
  22. import com.shkpr.service.alambizplugin.dbdao.services.intef.GisMetadataLayerTemplateService;
  23. import com.shkpr.service.alambizplugin.dbdao.services.intef.GisSurveyJobInfoService;
  24. import com.shkpr.service.alambizplugin.dbdao.services.intef.GisSurveyLayerApplyThirdCopyService;
  25. import com.shkpr.service.alambizplugin.dbdao.services.intef.GisSurveyProjectInfoService;
  26. import com.shkpr.service.alambizplugin.dto.GisMetadataLayerTemplate;
  27. import com.shkpr.service.alambizplugin.dto.GisMetadataPropertyTemplate;
  28. import com.shkpr.service.alambizplugin.dto.GisSurveyCondition;
  29. import com.shkpr.service.alambizplugin.dto.GisSurveyLayerApplyThirdCopy;
  30. import com.shkpr.service.alambizplugin.dto.GisSurveyPropertyValueThirdCopy;
  31. import com.shkpr.service.alambizplugin.dto.GisSurveyThirdImportElement;
  32. import com.shkpr.service.alambizplugin.dto.GisSurveyThirdImportResult;
  33. import com.shkpr.service.alambizplugin.dto.GisSurveyThirdImportResultDetail;
  34. import com.shkpr.service.alambizplugin.dto.GisSurveyThirdImportSubtask;
  35. import org.apache.commons.collections4.CollectionUtils;
  36. import org.apache.commons.lang3.StringUtils;
  37. import org.apache.commons.lang3.math.NumberUtils;
  38. import org.locationtech.jts.geom.Coordinate;
  39. import org.locationtech.jts.geom.Geometry;
  40. import org.locationtech.jts.geom.GeometryFactory;
  41. import org.locationtech.jts.geom.PrecisionModel;
  42. import org.springframework.scheduling.annotation.Async;
  43. import org.springframework.scheduling.annotation.AsyncResult;
  44. import org.springframework.stereotype.Component;
  45. import org.springframework.util.concurrent.ListenableFuture;
  46. import java.io.IOException;
  47. import java.io.InputStream;
  48. import java.math.BigDecimal;
  49. import java.math.RoundingMode;
  50. import java.time.Duration;
  51. import java.time.LocalDateTime;
  52. import java.util.ArrayList;
  53. import java.util.HashMap;
  54. import java.util.List;
  55. import java.util.Map;
  56. import java.util.Objects;
  57. import java.util.concurrent.ConcurrentHashMap;
  58. import java.util.concurrent.ExecutionException;
  59. import java.util.function.Consumer;
  60. import java.util.stream.Collectors;
  61. /**
  62. * 第三方导入执行器
  63. *
  64. * @author 欧阳劲驰
  65. * @since 1.0.0
  66. */
  67. @Component
  68. public class GisSurveyThirdImporter {
  69. /**
  70. * log
  71. */
  72. private final String mStrClassName;
  73. private final String mBizType;
  74. private final GeometryFactory geometryFactory;
  75. private final GisSurveyProjectInfoService projectInfoService;
  76. private final GisSurveyJobInfoService jobInfoService;
  77. private final GisMetadataLayerTemplateService layerTemplateService;
  78. private final GisSurveyLayerApplyThirdCopyService layerApplyThirdCopyService;
  79. private final DuplicatePointsFinder duplicatePointsFinder;
  80. private final InvalidLinesFinder invalidLinesFinder;
  81. private final InvalidPropertiesFinder invalidPropertiesFinder;
  82. public GisSurveyThirdImporter(GisSurveyProjectInfoService projectInfoService
  83. , GisSurveyJobInfoService jobInfoService
  84. , GisMetadataLayerTemplateService layerTemplateService
  85. , GisSurveyLayerApplyThirdCopyService layerApplyThirdCopyService
  86. , DuplicatePointsFinder duplicatePointsFinder
  87. , InvalidLinesFinder invalidLinesFinder
  88. , InvalidPropertiesFinder invalidPropertiesFinder) {
  89. mStrClassName = "GisSurveyThirdImporter";
  90. mBizType = LogFlagBusiType.BUSI_GIS_SURVEY.toStrValue();
  91. geometryFactory = new GeometryFactory(new PrecisionModel(), 4490);
  92. this.projectInfoService = projectInfoService;
  93. this.jobInfoService = jobInfoService;
  94. this.layerTemplateService = layerTemplateService;
  95. this.layerApplyThirdCopyService = layerApplyThirdCopyService;
  96. this.duplicatePointsFinder = duplicatePointsFinder;
  97. this.invalidLinesFinder = invalidLinesFinder;
  98. this.invalidPropertiesFinder = invalidPropertiesFinder;
  99. }
  100. /**
  101. * 第三方导入任务
  102. *
  103. * @param params 系统检查参数
  104. * @param inputStreams 文件输入流
  105. * @param onStartSubtask 启动子任务
  106. * @param onDeprecatedSubtask 弃用子任务(完成数据收集/取消)
  107. * @return 系统检查返回
  108. */
  109. @Async
  110. public ListenableFuture<GisSurveyThirdImportResult> thirdImportTask(GisSurveyThirdImportParams params, List<InputStream> inputStreams
  111. , Consumer<GisSurveyThirdImportSubtask> onStartSubtask, Consumer<String> onDeprecatedSubtask) {
  112. //构建返回
  113. GisSurveyThirdImportResult result = GisSurveyThirdImportResult.fail(params);
  114. result.setOperator(params.getOperator());
  115. //无效属性任务
  116. ListenableFuture<GisSurveyThirdImportResultDetail<Map<String, List<GisSurveyThirdImportElement>>>> invalidPropertiesFuture = null;
  117. //重复点号任务
  118. ListenableFuture<GisSurveyThirdImportResultDetail<List<GisSurveyThirdImportElement>>> duplicatePointsFuture = null;
  119. //无效线任务
  120. ListenableFuture<GisSurveyThirdImportResultDetail<List<GisSurveyThirdImportElement>>> invalidLinesFuture = null;
  121. //点集合
  122. List<Map<String, String>> points = new ArrayList<>();
  123. //线集合
  124. List<Map<String, String>> lines = new ArrayList<>();
  125. try {
  126. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
  127. , String.format(
  128. "开始执行第三方导入;任务id: %s"
  129. , params.getJobId()
  130. )
  131. );
  132. //解析excel
  133. parseExcel(inputStreams, points, lines);
  134. //项目约束条件
  135. List<GisSurveyCondition> projectConstraints = projectInfoService.findConstraintsByJobId(params.getJobId());
  136. //提取图层名
  137. List<String> pointLayerNames = getLayerNames(points, GisSurveyExcelDefine.FILE.POINT_LAYER);
  138. List<String> lineLayerNames = getLayerNames(lines, GisSurveyExcelDefine.FILE.LINE_LAYER);
  139. //图层模版
  140. List<GisMetadataLayerTemplate> pointLayerTemplates = layerTemplateService.findByNatureAndNameIn(params.getNature(), pointLayerNames);
  141. List<GisMetadataLayerTemplate> lineLayerTemplates = layerTemplateService.findByNatureAndNameIn(params.getNature(), lineLayerNames);
  142. //无效属性检查
  143. invalidPropertiesFuture = invalidPropertiesFinder.findInvalidProperties(points, lines
  144. , pointLayerTemplates, lineLayerTemplates, params.getCheckConstraint(), projectConstraints);
  145. //重复点检查
  146. duplicatePointsFuture = duplicatePointsFinder.findDuplicatePoints(points, pointLayerTemplates);
  147. //无效线检查
  148. invalidLinesFuture = invalidLinesFinder.finderInvalidLines(points, lines, pointLayerTemplates, lineLayerTemplates);
  149. //返回子任务
  150. onStartSubtask.accept(
  151. new GisSurveyThirdImportSubtask(invalidPropertiesFuture, duplicatePointsFuture, invalidLinesFuture)
  152. );
  153. //等待结果
  154. GisSurveyThirdImportResultDetail<Map<String, List<GisSurveyThirdImportElement>>> invalidPropertiesResult = invalidPropertiesFuture.get();
  155. //存入无效属性结果
  156. result.setInvalidLayersResult(untarInvalidProperties(invalidPropertiesResult, GisSurveyExcelDefine.RESULT.INVALID_LAYERS));
  157. result.setMissingRequirementsResult(untarInvalidProperties(invalidPropertiesResult, GisSurveyExcelDefine.RESULT.MISSING_REQUIREMENTS));
  158. result.setInvalidTypesResult(untarInvalidProperties(invalidPropertiesResult, GisSurveyExcelDefine.RESULT.INVALID_TYPES));
  159. result.setOutRangesResult(untarInvalidProperties(invalidPropertiesResult, GisSurveyExcelDefine.RESULT.OUT_RANGES));
  160. result.setOutConstraintResult(untarInvalidProperties(invalidPropertiesResult, GisSurveyExcelDefine.RESULT.OUT_CONSTRAINT));
  161. //存入重复点结果
  162. result.setDuplicatePointsResult(duplicatePointsFuture.get());
  163. //存入无效线结果
  164. result.setInvalidLinesResult(invalidLinesFuture.get());
  165. //结果未通过
  166. if (!checkResult(result)) {
  167. result.setImportStatus(GisSurveyImportStatusEnum.DATA_ERROR.getCode());
  168. result.setCompleteTime(LocalDateTime.now());
  169. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
  170. , String.format(
  171. "第三方导入结果未通过;任务id: %s, 用时(毫秒):%d",
  172. params.getJobId(),
  173. Duration.between(result.getRequestTime(), result.getCompleteTime()).toMillis()
  174. )
  175. );
  176. //弃用子任务
  177. onDeprecatedSubtask.accept(params.getJobId());
  178. return new AsyncResult<>(result);
  179. }
  180. //解码点线数据
  181. List<GisSurveyLayerApplyThirdCopy> layerApplyList = decodeDataToLayerApply(points, lines, pointLayerTemplates, lineLayerTemplates, params);
  182. //重置点号
  183. if (params.getResetNo()) resetNo(params.getJobId(), layerApplyList);
  184. //批量写入
  185. Boolean saved = layerApplyThirdCopyService.saveAll(params.getJobId(), layerApplyList);
  186. if (!saved) {
  187. result.setCompleteTime(LocalDateTime.now());
  188. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
  189. , String.format(
  190. "第三方导入入库失败;任务id: %s, 用时(毫秒):%d",
  191. params.getJobId(),
  192. Duration.between(result.getRequestTime(), LocalDateTime.now()).toMillis()
  193. )
  194. );
  195. //弃用子任务
  196. onDeprecatedSubtask.accept(params.getJobId());
  197. return new AsyncResult<>(result);
  198. }
  199. //完成任务
  200. result.setImportStatus(GisSurveyImportStatusEnum.SUCCESS.getCode());
  201. result.setCompleteTime(LocalDateTime.now());
  202. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
  203. , String.format(
  204. "结束执行第三方导入;任务id: %s, 用时(毫秒):%d",
  205. params.getJobId(),
  206. Duration.between(result.getRequestTime(), result.getCompleteTime()).toMillis()
  207. )
  208. );
  209. //弃用子任务
  210. onDeprecatedSubtask.accept(params.getJobId());
  211. return new AsyncResult<>(result);
  212. } catch (InterruptedException | ExecutionException | IOException | RuntimeException e) {
  213. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, mBizType, mStrClassName
  214. , String.format(
  215. "监测到中断或执行异常,开始清除子任务 任务id:%s error:%s",
  216. params.getJobId(),
  217. e
  218. )
  219. );
  220. //清除子任务
  221. if (invalidPropertiesFuture != null) invalidPropertiesFuture.cancel(true);
  222. if (duplicatePointsFuture != null) duplicatePointsFuture.cancel(true);
  223. if (invalidLinesFuture != null) invalidLinesFuture.cancel(true);
  224. //失败信息
  225. result.setImportStatus(CommAsyncStatusEnum.FAIL.getCode());
  226. result.setCompleteTime(LocalDateTime.now());
  227. //弃用子任务
  228. onDeprecatedSubtask.accept(params.getJobId());
  229. return new AsyncResult<>(result);
  230. }
  231. }
  232. /**
  233. * 解析excel
  234. *
  235. * @param inputStreams 输入流
  236. * @param points 点集合
  237. * @param lines 线集合
  238. * @throws IOException io异常
  239. */
  240. private void parseExcel(List<InputStream> inputStreams, List<Map<String, String>> points, List<Map<String, String>> lines) throws IOException, InterruptedException {
  241. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName, "开始执行解析excel ======>");
  242. long begin = System.currentTimeMillis();
  243. //解析文件
  244. for (InputStream inputStream : inputStreams) {
  245. //检查线程中断,并响应
  246. if (Thread.interrupted()) throw new InterruptedException();
  247. //解析excel
  248. Map<String, List<Map<String, String>>> excelData = ExcelUtils.parseExcelFile(inputStream, ExcelEnum.XLSX
  249. , GisSurveyExcelDefine.FILE_HANDLE.HEADER_ROW_NUM, GisSurveyExcelDefine.FILE_HANDLE.DATA_ROW_NUM);
  250. //填入集合
  251. if (excelData != null && excelData.containsKey(GisMetadataDefine.TYPE_KINE.POINT))
  252. points.addAll(excelData.get(GisMetadataDefine.TYPE_KINE.POINT));
  253. if (excelData != null && excelData.containsKey(GisMetadataDefine.TYPE_KINE.LINE))
  254. lines.addAll(excelData.get(GisMetadataDefine.TYPE_KINE.LINE));
  255. //释放流
  256. inputStream.close();
  257. }
  258. long end = System.currentTimeMillis();
  259. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
  260. , String.format(
  261. "结束执行执行解析excel,用时(毫秒):%d"
  262. , (end - begin)
  263. )
  264. );
  265. }
  266. /**
  267. * 获取图层名称
  268. *
  269. * @param datas 数据
  270. * @param layerKey 文件图层的key
  271. * @return 图层名称集合
  272. */
  273. private List<String> getLayerNames(List<Map<String, String>> datas, String layerKey) {
  274. //去重图层
  275. return datas.parallelStream()
  276. .filter(data -> data.get(layerKey) != null)
  277. .map(data -> data.get(layerKey))
  278. .distinct()
  279. .collect(Collectors.toList());
  280. }
  281. /**
  282. * 解包无效属性
  283. *
  284. * @param invalidPropertiesResult 无效属性结果
  285. * @param resultKey 结果key
  286. * @return 具体结果
  287. */
  288. private GisSurveyThirdImportResultDetail<List<GisSurveyThirdImportElement>> untarInvalidProperties(
  289. GisSurveyThirdImportResultDetail<Map<String, List<GisSurveyThirdImportElement>>> invalidPropertiesResult
  290. , String resultKey) {
  291. //获取结果
  292. List<GisSurveyThirdImportElement> importElements = invalidPropertiesResult.getResults().get(resultKey);
  293. return new GisSurveyThirdImportResultDetail<>(true, importElements);
  294. }
  295. /**
  296. * 检查结果
  297. *
  298. * @param result 结果
  299. * @return 结果通过状态
  300. */
  301. private Boolean checkResult(GisSurveyThirdImportResult result) {
  302. List<GisSurveyThirdImportElement> invalidLayers = result.getInvalidLayersResult().getResults();
  303. List<GisSurveyThirdImportElement> missingRequirements = result.getMissingRequirementsResult().getResults();
  304. List<GisSurveyThirdImportElement> invalidTypesResult = result.getInvalidTypesResult().getResults();
  305. List<GisSurveyThirdImportElement> outRanges = result.getOutRangesResult().getResults();
  306. List<GisSurveyThirdImportElement> outConstraint = result.getOutConstraintResult().getResults();
  307. List<GisSurveyThirdImportElement> duplicatePoints = result.getDuplicatePointsResult().getResults();
  308. List<GisSurveyThirdImportElement> invalidLines = result.getInvalidLinesResult().getResults();
  309. //检查是否都为空
  310. return CollectionUtils.isEmpty(invalidLayers) && CollectionUtils.isEmpty(missingRequirements) &&
  311. CollectionUtils.isEmpty(invalidTypesResult) && CollectionUtils.isEmpty(outRanges) &&
  312. CollectionUtils.isEmpty(outConstraint) && CollectionUtils.isEmpty(duplicatePoints) &&
  313. CollectionUtils.isEmpty(invalidLines);
  314. }
  315. /**
  316. * 解码数据到采集元素对象
  317. * <p>从Map转为 {@code GisSurveyLayerApplyThirdCopy}</p>
  318. *
  319. * @param points 点数据
  320. * @param lines 线数据
  321. * @param pointLayerTemplates 点图层模版
  322. * @param lineLayerTemplates 线图层模版
  323. * @param params 导入参数
  324. * @return 格式化后数据
  325. */
  326. private List<GisSurveyLayerApplyThirdCopy> decodeDataToLayerApply(List<Map<String, String>> points, List<Map<String, String>> lines
  327. , List<GisMetadataLayerTemplate> pointLayerTemplates, List<GisMetadataLayerTemplate> lineLayerTemplates, GisSurveyThirdImportParams params) {
  328. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName, "导入结果通过,开始解码点线数据 ======>");
  329. long begin = System.currentTimeMillis();
  330. //按模版名分组,便于快速找到模版
  331. Map<String, GisMetadataLayerTemplate> poineTemplateMap = pointLayerTemplates.stream()
  332. .collect(Collectors.toMap(
  333. GisMetadataLayerTemplate::getName, it -> it,
  334. (it1, it2) -> it1
  335. ));
  336. Map<String, GisMetadataLayerTemplate> lineTemplateMap = lineLayerTemplates.stream()
  337. .collect(Collectors.toMap(
  338. GisMetadataLayerTemplate::getName, it -> it,
  339. (it1, it2) -> it1
  340. ));
  341. //采集元素象集合
  342. List<GisSurveyLayerApplyThirdCopy> result = new ArrayList<>();
  343. //点号映射,用于线写入上下游节点
  344. Map<String, String> pointNoMapping = new HashMap<>();
  345. //格式化点
  346. for (Map<String, String> point : points) {
  347. //获取模版
  348. GisMetadataLayerTemplate layerTemplate = poineTemplateMap.get(point.get(GisSurveyExcelDefine.FILE.POINT_LAYER));
  349. //解码点对象
  350. GisSurveyLayerApplyThirdCopy layerApply = decodePointToLayerApply(point, layerTemplate, params);
  351. //存入点号
  352. pointNoMapping.put(layerApply.getNo(), layerApply.getCode());
  353. result.add(layerApply);
  354. }
  355. //格式化线
  356. for (Map<String, String> line : lines) {
  357. //获取模版
  358. GisMetadataLayerTemplate layerTemplate = lineTemplateMap.get(line.get(GisSurveyExcelDefine.FILE.LINE_LAYER));
  359. //解码对象
  360. GisSurveyLayerApplyThirdCopy layerApply = decodeLineToLayerApply(line, layerTemplate, params, pointNoMapping);
  361. result.add(layerApply);
  362. }
  363. long end = System.currentTimeMillis();
  364. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
  365. , String.format(
  366. "结束执行解码点线数据,用时(毫秒):%d"
  367. , (end - begin)
  368. )
  369. );
  370. return result;
  371. }
  372. /**
  373. * 解码点数据到采集元素对象
  374. *
  375. * @param point 点数据
  376. * @param layerTemplate 图层模版
  377. * @param params 导入参数
  378. * @return 采集元素拷贝对象
  379. */
  380. private GisSurveyLayerApplyThirdCopy decodePointToLayerApply(Map<String, String> point, GisMetadataLayerTemplate layerTemplate, GisSurveyThirdImportParams params) {
  381. GisSurveyLayerApplyThirdCopy layerApply = new GisSurveyLayerApplyThirdCopy();
  382. //必填项
  383. layerApply.setJobId(params.getJobId());
  384. layerApply.setCode(PointCodeUtil.generateCode());
  385. layerApply.setLayer(layerTemplate.getKey());
  386. layerApply.setKind(GisMetadataDefine.TYPE_KINE.POINT);
  387. //默认值
  388. layerApply.setApply(GisSurveyExcelDefine.DEFAULT_VALUE.APPLY);
  389. layerApply.setSource(GisSurveyExcelDefine.DEFAULT_VALUE.SOURCE);
  390. //点号(长度64截断)
  391. String noStr = ThirdImportTemplateUtils.getValue(point, layerTemplate, GisSurveyExcelDefine.TEMPLATE.NO);
  392. if (StringUtils.isNotBlank(noStr))
  393. layerApply.setNo(StringUtils.substring(noStr, 0, 64));
  394. //经纬度
  395. Double lng = null;
  396. Double lat = null;
  397. //遍历属性模版
  398. List<GisSurveyPropertyValueThirdCopy> propertyValueList = new ArrayList<>();
  399. for (GisMetadataPropertyTemplate propertyTemplate : layerTemplate.getPropertyTemplates()) {
  400. //解析属性
  401. GisSurveyPropertyValueThirdCopy propertyValue = decodeDataToPropertyValue(point, layerApply
  402. , propertyTemplate, params);
  403. //不为空,填入数据
  404. if (propertyValue != null) {
  405. propertyValueList.add(propertyValue);
  406. //经纬度属性
  407. if (Objects.equals(propertyValue.getProperty(), GisSurveyExcelDefine.TEMPLATE.LNG)
  408. && NumberUtils.isParsable(propertyValue.getValue()))
  409. lng = Double.valueOf(propertyValue.getValue());
  410. if (Objects.equals(propertyValue.getProperty(), GisSurveyExcelDefine.TEMPLATE.LAT)
  411. && NumberUtils.isParsable(propertyValue.getValue()))
  412. lat = Double.valueOf(propertyValue.getValue());
  413. //高程和埋深
  414. if (Objects.equals(propertyValue.getProperty(), GisSurveyExcelDefine.TEMPLATE.ELEVATION)
  415. && NumberUtils.isParsable(propertyValue.getValue()))
  416. layerApply.setElevation(Double.valueOf(propertyValue.getValue()));
  417. if (Objects.equals(propertyValue.getProperty(), GisSurveyExcelDefine.TEMPLATE.DEPTH)
  418. && NumberUtils.isParsable(propertyValue.getValue()))
  419. layerApply.setDepth(Double.valueOf(propertyValue.getValue()));
  420. }
  421. }
  422. //处理经纬度
  423. if (lng != null && lat != null) {
  424. //构建geom
  425. Geometry geometry = geometryFactory.createPoint(new Coordinate(lng, lat));
  426. //如源不为大地2000
  427. if (!Objects.equals(params.getSourceCRSCode(), CommCRSDefine.CGCS2000)) {
  428. //转换坐标系
  429. Geometry transform = CRSUtil.transform(geometry, params.getSourceCRSCode(), CommCRSDefine.CGCS2000);
  430. if (transform != null) {
  431. //回填geom
  432. geometry = transform;
  433. //获取解析后的经纬度
  434. Coordinate coordinate = geometry.getCoordinate();
  435. String lngStr = BigDecimal.valueOf(coordinate.getX()).toPlainString();
  436. String latStr = BigDecimal.valueOf(coordinate.getY()).toPlainString();
  437. //回填经纬度属性
  438. propertyValueList.stream()
  439. .filter(it -> Objects.equals(it.getProperty(), GisSurveyExcelDefine.TEMPLATE.LAT))
  440. .forEach(it -> it.setValue(latStr));
  441. propertyValueList.stream()
  442. .filter(it -> Objects.equals(it.getProperty(), GisSurveyExcelDefine.TEMPLATE.LNG))
  443. .forEach(it -> it.setValue(lngStr));
  444. }
  445. }
  446. //设置geom
  447. layerApply.setGis(geometry);
  448. }
  449. //设置属性
  450. layerApply.setPropertyValues(propertyValueList);
  451. return layerApply;
  452. }
  453. /**
  454. * 解码线数据到采集元素对象
  455. *
  456. * @param line 线数据
  457. * @param layerTemplate 图层模版
  458. * @param params 导入参数
  459. * @param pointNoMapping 点号映射
  460. * @return 采集元素拷贝对象
  461. */
  462. private GisSurveyLayerApplyThirdCopy decodeLineToLayerApply(Map<String, String> line, GisMetadataLayerTemplate layerTemplate, GisSurveyThirdImportParams params, Map<String, String> pointNoMapping) {
  463. GisSurveyLayerApplyThirdCopy layerApply = new GisSurveyLayerApplyThirdCopy();
  464. //必填项
  465. layerApply.setJobId(params.getJobId());
  466. layerApply.setCode(PointCodeUtil.generateCode());
  467. layerApply.setLayer(layerTemplate.getKey());
  468. layerApply.setKind(GisMetadataDefine.TYPE_KINE.LINE);
  469. //默认值
  470. layerApply.setApply(GisSurveyExcelDefine.DEFAULT_VALUE.APPLY);
  471. layerApply.setSource(GisSurveyExcelDefine.DEFAULT_VALUE.SOURCE);
  472. //上下游节点
  473. String upNoStr = ThirdImportTemplateUtils.getValue(line, layerTemplate, GisSurveyExcelDefine.TEMPLATE.UP_NO);
  474. if (StringUtils.isNotBlank(upNoStr)) {
  475. layerApply.setUpNode(pointNoMapping.get(upNoStr));
  476. }
  477. String downNoStr = ThirdImportTemplateUtils.getValue(line, layerTemplate, GisSurveyExcelDefine.TEMPLATE.DOWN_NO);
  478. if (StringUtils.isNotBlank(downNoStr)) {
  479. layerApply.setDownNode(pointNoMapping.get(downNoStr));
  480. }
  481. //遍历属性模版
  482. List<GisSurveyPropertyValueThirdCopy> propertyValueList = new ArrayList<>();
  483. for (GisMetadataPropertyTemplate propertyTemplate : layerTemplate.getPropertyTemplates()) {
  484. //解析属性
  485. GisSurveyPropertyValueThirdCopy propertyValue = decodeDataToPropertyValue(line, layerApply, propertyTemplate, params);
  486. if (propertyValue != null) propertyValueList.add(propertyValue);
  487. }
  488. layerApply.setPropertyValues(propertyValueList);
  489. return layerApply;
  490. }
  491. /**
  492. * 解码数据到属性值对象
  493. *
  494. * @param data 数据
  495. * @param layerApply 采集元素
  496. * @param propertyTemplate 属性模版
  497. * @param params 导出参数
  498. * @return 属性值对象
  499. */
  500. private GisSurveyPropertyValueThirdCopy decodeDataToPropertyValue(Map<String, String> data, GisSurveyLayerApplyThirdCopy layerApply
  501. , GisMetadataPropertyTemplate propertyTemplate, GisSurveyThirdImportParams params) {
  502. //点号直接返回点号属性
  503. if (Objects.equals(GisSurveyExcelDefine.TEMPLATE.CODE, propertyTemplate.getKey()))
  504. return new GisSurveyPropertyValueThirdCopy(params.getJobId(), layerApply.getCode()
  505. , propertyTemplate.getKey(), layerApply.getCode());
  506. if (Objects.equals(GisSurveyExcelDefine.TEMPLATE.UP_NODE, propertyTemplate.getKey()))
  507. return new GisSurveyPropertyValueThirdCopy(params.getJobId(), layerApply.getCode()
  508. , propertyTemplate.getKey(), layerApply.getUpNode());
  509. if (Objects.equals(GisSurveyExcelDefine.TEMPLATE.DOWN_NODE, propertyTemplate.getKey()))
  510. return new GisSurveyPropertyValueThirdCopy(params.getJobId(), layerApply.getCode()
  511. , propertyTemplate.getKey(), layerApply.getDownNode());
  512. //获取表头
  513. String templateName = propertyTemplate.getName();
  514. //获取值
  515. String value = data.get(templateName);
  516. //构建dto
  517. if (StringUtils.isNotBlank(value)) {
  518. //double按数字处理,否则按字符串处理
  519. if (propertyTemplate.getType().equals(GisSurveyExcelDefine.DATA_TYPE.DOUBLE)
  520. && propertyTemplate.getPrecisions() != null) {
  521. //如能被解析,则处理精度,否则设置为0
  522. if (NumberUtils.isParsable(value)) {
  523. //值按精度设置小数位,并抹去0
  524. value = new BigDecimal(value)
  525. .setScale(propertyTemplate.getPrecisions(), RoundingMode.DOWN)
  526. .stripTrailingZeros()
  527. .toPlainString();
  528. } else value = "0";
  529. } else {
  530. //值长度64截断
  531. value = StringUtils.substring(value, 0, 64);
  532. }
  533. return new GisSurveyPropertyValueThirdCopy(params.getJobId()
  534. , layerApply.getCode(), propertyTemplate.getKey(), value);
  535. }
  536. return null;
  537. }
  538. /**
  539. * 重置点号
  540. *
  541. * @param jobId 任务id
  542. * @param layerApplyList 采集元素集合
  543. */
  544. private void resetNo(String jobId, List<GisSurveyLayerApplyThirdCopy> layerApplyList) {
  545. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName, "开始重置点号 ======>");
  546. long begin = System.currentTimeMillis();
  547. //点号规则
  548. final List<RuleFormatSetting> ruleFormat = jobInfoService.findRuleFormat(jobId);
  549. //点号映射
  550. Map<String, String> noMapping = new ConcurrentHashMap<>();
  551. //并行遍历元素,处理点的点号
  552. layerApplyList.parallelStream()
  553. .filter(la -> Objects.equals(la.getKind(), GisMetadataDefine.TYPE_KINE.POINT))
  554. .forEach(la -> {
  555. //生成点号
  556. String no = SurveyCommonAssistant.genNo(
  557. la.getCode(),
  558. la.getPropertyValues().stream()
  559. .map(pv -> new JPSurveyApplyColumn(pv.getProperty(), pv.getValue()))
  560. .collect(Collectors.toList()),
  561. ruleFormat
  562. );
  563. //设置元素点号
  564. la.setNo(no);
  565. //设置属性点号
  566. la.getPropertyValues().stream()
  567. .filter(it -> Objects.equals(it.getProperty(), GisSurveyExcelDefine.TEMPLATE.NO))
  568. .forEach(it -> it.setValue(no));
  569. //设置点号映射
  570. noMapping.put(la.getCode(), no);
  571. });
  572. //处理线的上下点号
  573. layerApplyList.parallelStream()
  574. .filter(la -> Objects.equals(la.getKind(), GisMetadataDefine.TYPE_KINE.LINE))
  575. .forEach(la -> {
  576. //处理上点号
  577. if (noMapping.containsKey(la.getUpNode())) {
  578. //设置属性点号
  579. la.getPropertyValues().stream()
  580. .filter(it -> Objects.equals(it.getProperty(), GisSurveyExcelDefine.TEMPLATE.UP_NO))
  581. .forEach(it -> it.setValue(noMapping.get(la.getUpNode())));
  582. }
  583. //处理下点号
  584. if (noMapping.containsKey(la.getDownNode())) {
  585. //设置属性点号
  586. la.getPropertyValues().stream()
  587. .filter(it -> Objects.equals(it.getProperty(), GisSurveyExcelDefine.TEMPLATE.DOWN_NO))
  588. .forEach(it -> it.setValue(noMapping.get(la.getDownNode())));
  589. }
  590. });
  591. long end = System.currentTimeMillis();
  592. LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName
  593. , String.format(
  594. "结束执行执行重置点号,用时(毫秒):%d"
  595. , (end - begin)
  596. )
  597. );
  598. }
  599. }