|
|
@@ -1,8 +1,11 @@
|
|
|
package top.haijunit.train.service;
|
|
|
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import top.haijunit.common.utils.StreamUtils;
|
|
|
+import top.haijunit.train.domain.constant.RouteStatusEnum;
|
|
|
import top.haijunit.train.domain.entity.RouteEntity;
|
|
|
import top.haijunit.train.domain.entity.SelectionEntity;
|
|
|
import top.haijunit.train.domain.entity.SignalEntity;
|
|
|
@@ -12,8 +15,8 @@ import top.haijunit.train.domain.repository.SignalRepository;
|
|
|
import top.haijunit.train.simulation.domain.RouteItem;
|
|
|
import top.haijunit.train.simulation.domain.TrainItem;
|
|
|
|
|
|
-import java.util.LinkedList;
|
|
|
-import java.util.List;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* @author zhanghaijun
|
|
|
@@ -28,6 +31,7 @@ public class RouteService {
|
|
|
private final RouteRepository routeRepository;
|
|
|
private final SelectionRepository selectionRepository;
|
|
|
private final SignalRepository signalRepository;
|
|
|
+ private final SelectionService selectionService;
|
|
|
|
|
|
public RouteItem toBuildRoute(RouteEntity route) {
|
|
|
if (route == null) {
|
|
|
@@ -39,21 +43,56 @@ public class RouteService {
|
|
|
}
|
|
|
|
|
|
public RouteItem getNextRoute(RouteItem route) {
|
|
|
- return routeRepository.findById(route.getNextId())
|
|
|
- // 类型转换
|
|
|
- .map(this::toBuildRoute).orElse(null);
|
|
|
- }
|
|
|
-
|
|
|
- public RouteItem getCurrentRoute(Long selectionId) {
|
|
|
- return routeRepository.findAll().stream()
|
|
|
- // 过滤出当前区段所在的进路
|
|
|
- .filter(item -> item.isContain(selectionId)).findFirst()
|
|
|
- // 转换类型
|
|
|
- .map(this::toBuildRoute).orElse(null);
|
|
|
+ Optional<RouteEntity> optional = routeRepository.findById(route.getNextId());
|
|
|
+ if (optional.isEmpty()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ RouteEntity entity = optional.get();
|
|
|
+ LinkedList<SelectionEntity> selectionList = selectionService.getSelectionList(entity.getId());
|
|
|
+ // TODO 信号灯
|
|
|
+ RouteItem routeItem = new RouteItem(entity, selectionList, new LinkedList<>());
|
|
|
+ routeItem.setStatus(RouteStatusEnum.LOCK);
|
|
|
+ return routeItem;
|
|
|
}
|
|
|
|
|
|
- public RouteItem getCurrentRoute(TrainItem train) {
|
|
|
- return getCurrentRoute(train.getSelectionId());
|
|
|
+ public LinkedList<RouteItem> getCurrentRouteList(TrainItem train) {
|
|
|
+ LinkedList<RouteEntity> list = new LinkedList<>();
|
|
|
+ for (RouteEntity route : routeRepository.findAll()) {
|
|
|
+ List<SelectionEntity> selectionList = selectionRepository.findAllById(route.getSelectionList());
|
|
|
+ for (SelectionEntity selection : selectionList) {
|
|
|
+ if (!StreamUtils.filter(list, item -> item.getId().equals(route.getId())).isEmpty()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (selection.isContain(train.getHeadKilometer(), train.getDirection())) {
|
|
|
+ switch (selection.getDirection()) {
|
|
|
+ case UP:
|
|
|
+ list.addFirst(route);
|
|
|
+ break;
|
|
|
+ case DOWN:
|
|
|
+ list.addLast(route);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!StreamUtils.filter(list, item -> item.getId().equals(route.getId())).isEmpty()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (selection.isContain(train.getTailKilometer(), train.getDirection())) {
|
|
|
+ switch (selection.getDirection()) {
|
|
|
+ case UP:
|
|
|
+ list.addLast(route);
|
|
|
+ break;
|
|
|
+ case DOWN:
|
|
|
+ list.addFirst(route);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return list.stream().map(entity -> {
|
|
|
+ LinkedList<SelectionEntity> selectionList = selectionService.getSelectionList(entity.getId(), train.getDirection(), train.getHeadKilometer(), train.getTailKilometer());
|
|
|
+ // TODO 型号灯
|
|
|
+ RouteItem routeItem = new RouteItem(entity, selectionList, new LinkedList<>());
|
|
|
+ routeItem.setStatus(RouteStatusEnum.OCCUPY);
|
|
|
+ return routeItem;
|
|
|
+ }).collect(Collectors.toCollection(LinkedList::new));
|
|
|
}
|
|
|
|
|
|
}
|