This commit is contained in:
2023-06-21 15:23:53 +08:00
parent 1f98916d36
commit ad47b0a4b5

View File

@@ -32,6 +32,7 @@ import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.stream.Collectors;
/**
* 高德地图Service
@@ -74,7 +75,7 @@ public class AMapServiceImpl implements AMapService {
@Override
public List<AMapStationInfo> getStationInfos(GetStationInfoDTO dto) {
StopWatch sw = new StopWatch();
List<AMapStationInfo> resultList = new ArrayList<>();
// List<AMapStationInfo> resultList = new ArrayList<>();
if (StringUtils.equals("page", dto.getType())) {
int pageNo = dto.getCurrentPage() == null ? 1 : dto.getCurrentPage();
@@ -91,22 +92,23 @@ public class AMapServiceImpl implements AMapService {
}
// 拼装高德格式数据
sw.start("拼装高德格式数据");
for (PileStationInfo stationInfo : stationInfos) {
CompletableFuture<AMapStationInfo> completableFuture = CompletableFuture.supplyAsync(() -> assembleAMapData(stationInfo), executor);
// for (PileStationInfo stationInfo : stationInfos) {
//
// // AMapStationInfo aMapStationInfo = assembleAMapData(stationInfo);
// // resultList.add(aMapStationInfo);
// }
List<AMapStationInfo> collect = stationInfos.parallelStream().map(x -> {
try {
AMapStationInfo aMapStationInfo = completableFuture.get();
resultList.add(aMapStationInfo);
} catch (InterruptedException e) {
throw new RuntimeException(e);
return assembleAMapData(x);
} catch (ExecutionException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
// AMapStationInfo aMapStationInfo = assembleAMapData(stationInfo);
// resultList.add(aMapStationInfo);
}
}).collect(Collectors.toList());
sw.stop();
log.info("接口耗时:{}, 详情:{}", sw.getTotalTimeMillis(), sw.prettyPrint());
return resultList;
return collect;
}
/**
@@ -114,11 +116,10 @@ public class AMapServiceImpl implements AMapService {
* @param stationInfo 站点信息
* @return 高度需要的数据格式
*/
private AMapStationInfo assembleAMapData(PileStationInfo stationInfo) {
StopWatch sw = new StopWatch();
sw.start("set数据");
private AMapStationInfo assembleAMapData(PileStationInfo stationInfo) throws ExecutionException, InterruptedException {
AMapStationInfo aMapInfo = new AMapStationInfo();
aMapInfo.setStationID(String.valueOf(stationInfo.getId()));
Long stationInfoId = stationInfo.getId();
aMapInfo.setStationID(String.valueOf(stationInfoId));
aMapInfo.setOperatorID("");
aMapInfo.setEquipmentOwnerID("");
aMapInfo.setOperatorName("");
@@ -141,24 +142,30 @@ public class AMapServiceImpl implements AMapService {
}
aMapInfo.setConstruction(Integer.parseInt(construction));
aMapInfo.setBusineHours(stationInfo.getBusinessHours());
sw.stop();
// 根据站点id查询计费模板
sw.start("根据站点id查询计费模板");
List<AMapPriceChargingInfo> priceList = getPriceInfoByStationId(String.valueOf(stationInfo.getId()));
aMapInfo.setPriceChargingInfo(priceList);
sw.stop();
// List<AMapPriceChargingInfo> priceList = getPriceInfoByStationId(String.valueOf(stationInfoId));
CompletableFuture<List<AMapPriceChargingInfo>> future1 = CompletableFuture.supplyAsync(() -> getPriceInfoByStationId(String.valueOf(stationInfoId)));
// 根据站点id查询快、慢充设备数量
sw.start("根据站点id查询快、慢充设备数量");
Map<String, Integer> pileNumMap = pileConnectorInfoService.getPileTypeNum(stationInfo.getId());
// Map<String, Integer> pileNumMap = pileConnectorInfoService.getPileTypeNum(stationInfoId);
CompletableFuture<Map<String, Integer>> future2 = CompletableFuture.supplyAsync(() -> pileConnectorInfoService.getPileTypeNum(stationInfoId));
// 根据站点查询站点下所有桩
// List<AMapEquipmentInfo> aMapEquipmentInfos = getPileListByStationId(String.valueOf(stationInfoId));
CompletableFuture<List<AMapEquipmentInfo>> future3 = CompletableFuture.supplyAsync(() -> getPileListByStationId(String.valueOf(stationInfoId)));
CompletableFuture<Void> all = CompletableFuture.allOf(future1, future2, future3);
// .join()和.get()都会阻塞并获取线程的执行情况
// .join()会抛出未经检查的异常,不会强制开发者处理异常 .get()会抛出检查异常,需要开发者处理
all.join();
all.get();
aMapInfo.setPriceChargingInfo(future1.get());
Map<String, Integer> pileNumMap = future2.get();
aMapInfo.setFastEquipmentNum(pileNumMap.get("fastTotal"));
aMapInfo.setSlowEquipmentNum(pileNumMap.get("slowTotal"));
sw.stop();
// 根据站点查询站点下所有桩
sw.start("根据站点查询站点下所有桩");
List<AMapEquipmentInfo> aMapEquipmentInfos = getPileListByStationId(String.valueOf(stationInfo.getId()));
aMapInfo.setEquipmentInfos(aMapEquipmentInfos);
sw.stop();
log.info("组装高德数据格式耗时详细:{}, 线程名:{}", prettyPrintBySecond(sw), Thread.currentThread().getName());
aMapInfo.setEquipmentInfos(future3.get());
return aMapInfo;
}