提交 967c03fa authored 作者: hzh's avatar hzh

代码优化

上级 4e2216c1
......@@ -39,10 +39,6 @@ import org.springframework.web.bind.annotation.*;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.stream.Collectors;
/**
......@@ -118,11 +114,6 @@ public class TrainController extends BaseController {
return stationList;
}
/**
* 线程数
*/
private static final int NUM_THREADS = 100;
/**
* 火车票查询列表
*
......@@ -132,7 +123,6 @@ public class TrainController extends BaseController {
* @param fromCity 出发城市
* @param toCity 到达城市
* @return 查询列表
* @throws InterruptedException
*/
@GlobalTransactional(timeoutMills = 600000)
@GetMapping("/list")
......@@ -142,56 +132,45 @@ public class TrainController extends BaseController {
@RequestParam(value = "toStationCodeList", required = false) List<String> toStationCodeList,
@RequestParam(value = "fromCity", required = false) String fromCity,
@RequestParam(value = "toCity", required = false) String toCity
) throws InterruptedException {
) {
//获取出发站点
List<String> fromStationCodes = getStationCodeList(fromStationCodeList, fromCity);
String fromStationCode = getStationCodeList(fromStationCodeList, fromCity);
//获取目的站点
List<String> toStationCodes = getStationCodeList(toStationCodeList, toCity);
String toStationCode = getStationCodeList(toStationCodeList, toCity);
String userId = getUserId();
List<Callable<List<org.dromara.common.ys.model.res.train.QueryModel>>> taskList = fromStationCodes.stream().flatMap(fromCode ->
toStationCodes.stream().map(toCode -> (Callable<List<org.dromara.common.ys.model.res.train.QueryModel>>) () -> {
QueryModel query = new QueryModel()
.setFromDate(fromDate)
.setFromStationCode(fromCode)
.setToStationCode(toCode)
.setTripType(TripType.PUBLIC.getCode());
return trainService.queryModel(userId, query);
})).collect(Collectors.toList());
// 创建一个线程池
ExecutorService executorService = Executors.newFixedThreadPool(NUM_THREADS);
// 提交任务并等待完成
List<Future<List<org.dromara.common.ys.model.res.train.QueryModel>>> futures = executorService.invokeAll(taskList);
// 关闭线程池
executorService.shutdown();
// 检查任务是否完成
List<org.dromara.common.ys.model.res.train.QueryModel> list = futures.stream().flatMap(f -> {
try {
return f.get().stream();
} catch (Exception e) {
return new ArrayList<org.dromara.common.ys.model.res.train.QueryModel>().stream();
}
}).collect(Collectors.groupingBy(org.dromara.common.ys.model.res.train.QueryModel::getTrainCode))
.entrySet().stream().map(entry -> entry.getValue().get(0)).collect(Collectors.toList());
QueryModel query = new QueryModel()
.setFromDate(fromDate)
.setFromStationCode(fromStationCode)
.setToStationCode(toStationCode)
.setTripType(TripType.PUBLIC.getCode());
List<org.dromara.common.ys.model.res.train.QueryModel> list = trainService.queryModel(userId, query);
System.out.println("list:" + list.size());
return R.ok(list);
}
private List<String> getStationCodeList(List<String> codeList, String city) {
private String getStationCodeList(List<String> codeList, String city) {
List<String> stationCodeList = new ArrayList<>();
if (CollectionUtil.isNotEmpty(codeList)) {
stationCodeList.addAll(codeList);
} else {
stationCodeList = getStationList().stream().filter(item -> StringUtils.equals(item.getCityName(), city)).map(AllStationModel::getZddm)
stationCodeList = getStationList().stream().filter(item -> StringUtils.equals(item.getCityName(), city))
.sorted((o1, o2) -> {
double v = StrUtil.similar(o1.getZdmc(), city) - StrUtil.similar(o2.getZdmc(), city);
if (v > 0) {
return -1;
} else if (v == 0) {
return 0;
} else {
return 1;
}
})
.map(AllStationModel::getZddm)
.collect(Collectors.toList());
}
return stationCodeList;
return stationCodeList.get(0);
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论