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