| | |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springblade.core.tool.utils.StringUtil; |
| | | import org.sxkj.gd.utils.GdGeoAddressUtil; |
| | | import org.sxkj.gd.workorder.entity.GdPatrolTaskEntity; |
| | | import org.sxkj.gd.workorder.entity.GdTaskResultEntity; |
| | | |
| | |
| | | CompletableFuture<Map<String, byte[]>> qrCodeFuture = CompletableFuture.supplyAsync( |
| | | () -> preloadQrCodes(results), RESOURCE_EXECUTOR |
| | | ); |
| | | CompletableFuture.allOf(imageFuture, qrCodeFuture).join(); |
| | | CompletableFuture<Map<String, String>> addressFuture = CompletableFuture.supplyAsync( |
| | | () -> preloadAddresses(results), RESOURCE_EXECUTOR |
| | | ); |
| | | CompletableFuture.allOf(imageFuture, qrCodeFuture, addressFuture).join(); |
| | | Map<String, byte[]> imageCache = imageFuture.get(); |
| | | Map<String, byte[]> qrCodeCache = qrCodeFuture.get(); |
| | | Map<String, String> addressCache = addressFuture.get(); |
| | | |
| | | createTitle(document, title); |
| | | createSectionHeader(document, "一、任务详情"); |
| | |
| | | |
| | | int resultCount = results.size(); |
| | | createSectionHeader(document, "二、巡查结果(" + resultCount + "件)"); |
| | | addResultDetails(document, results, imageCache, qrCodeCache); |
| | | addResultDetails(document, results, imageCache, qrCodeCache, addressCache); |
| | | |
| | | document.write(fos); |
| | | return reportFile; |
| | |
| | | private static void addResultDetails(XWPFDocument document, |
| | | List<GdTaskResultEntity> results, |
| | | Map<String, byte[]> imageCache, |
| | | Map<String, byte[]> qrCodeCache) |
| | | Map<String, byte[]> qrCodeCache, |
| | | Map<String, String> addressCache) |
| | | throws IOException, InvalidFormatException { |
| | | if (results == null || results.isEmpty()) { |
| | | addDetailItem(document, "/"); |
| | |
| | | currentRow++; |
| | | |
| | | setTableCellWithBoldAndCenter(resultTable, currentRow, 1, "成果地址"); |
| | | setTableCellWithSpacing(resultTable, currentRow, 2, "/"); |
| | | String address = getAddressFromCache(result, addressCache); |
| | | setTableCellWithSpacing(resultTable, currentRow, 2, formatString(address)); |
| | | currentRow++; |
| | | |
| | | setTableCellWithBoldAndCenter(resultTable, currentRow, 1, "成果定位"); |
| | |
| | | return qrCodeCache; |
| | | } |
| | | |
| | | private static Map<String, String> preloadAddresses(List<GdTaskResultEntity> results) { |
| | | if (results == null || results.isEmpty()) { |
| | | return Collections.emptyMap(); |
| | | } |
| | | List<GdTaskResultEntity> needAddressResults = results.stream() |
| | | .filter(result -> result.getLongitude() != null && result.getLatitude() != null) |
| | | .collect(Collectors.toList()); |
| | | if (needAddressResults.isEmpty()) { |
| | | return Collections.emptyMap(); |
| | | } |
| | | |
| | | Map<String, String> addressCache = new ConcurrentHashMap<>(); |
| | | List<CompletableFuture<Void>> futures = needAddressResults.stream() |
| | | .map(result -> CompletableFuture.runAsync(() -> { |
| | | String key = buildCoordinateKey(result.getLongitude(), result.getLatitude()); |
| | | if (StringUtil.isBlank(key) || addressCache.containsKey(key)) { |
| | | return; |
| | | } |
| | | String address = GdGeoAddressUtil.getFormattedAddress(result.getLongitude(), result.getLatitude()); |
| | | if (!StringUtil.isBlank(address)) { |
| | | addressCache.put(key, address); |
| | | } |
| | | }, RESOURCE_EXECUTOR)) |
| | | .collect(Collectors.toList()); |
| | | |
| | | try { |
| | | CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join(); |
| | | } catch (Exception e) { |
| | | logger.error("等待成果地址获取完成时发生异常", e); |
| | | } |
| | | return addressCache; |
| | | } |
| | | |
| | | private static byte[] downloadImage(String urlString) throws IOException { |
| | | URL url = new URL(urlString); |
| | | HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| | |
| | | return String.format(Locale.CHINA, "%.6f", coordinate); |
| | | } |
| | | |
| | | private static String getAddressFromCache(GdTaskResultEntity result, Map<String, String> addressCache) { |
| | | if (result == null || addressCache == null || addressCache.isEmpty()) { |
| | | return null; |
| | | } |
| | | String key = buildCoordinateKey(result.getLongitude(), result.getLatitude()); |
| | | if (StringUtil.isBlank(key)) { |
| | | return null; |
| | | } |
| | | return addressCache.get(key); |
| | | } |
| | | |
| | | private static String buildCoordinateKey(Double longitude, Double latitude) { |
| | | if (longitude == null || latitude == null) { |
| | | return ""; |
| | | } |
| | | return formatCoordinateValue(longitude) + "," + formatCoordinateValue(latitude); |
| | | } |
| | | |
| | | private static int findImageFormat(String url) { |
| | | String cleanUrl = url; |
| | | int queryIndex = url.indexOf("?"); |