|
@@ -1,12 +1,15 @@
|
|
|
package top.haijunit.train.service;
|
|
package top.haijunit.train.service;
|
|
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.cache.annotation.Cacheable;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
import top.haijunit.common.utils.StreamUtils;
|
|
import top.haijunit.common.utils.StreamUtils;
|
|
|
|
|
+import top.haijunit.train.domain.constant.CacheNames;
|
|
|
|
|
+import top.haijunit.train.domain.constant.DirectionEnum;
|
|
|
import top.haijunit.train.domain.constant.SectionStatusEnum;
|
|
import top.haijunit.train.domain.constant.SectionStatusEnum;
|
|
|
-import top.haijunit.train.domain.constant.TrainConstant;
|
|
|
|
|
import top.haijunit.train.domain.entity.RouteEntity;
|
|
import top.haijunit.train.domain.entity.RouteEntity;
|
|
|
import top.haijunit.train.domain.entity.SelectionEntity;
|
|
import top.haijunit.train.domain.entity.SelectionEntity;
|
|
|
import top.haijunit.train.domain.entity.SignStopEntity;
|
|
import top.haijunit.train.domain.entity.SignStopEntity;
|
|
@@ -14,9 +17,7 @@ import top.haijunit.train.domain.repository.RouteRepository;
|
|
|
import top.haijunit.train.domain.repository.SelectionRepository;
|
|
import top.haijunit.train.domain.repository.SelectionRepository;
|
|
|
import top.haijunit.train.domain.repository.SignStopRepository;
|
|
import top.haijunit.train.domain.repository.SignStopRepository;
|
|
|
import top.haijunit.train.simulation.domain.SectionItem;
|
|
import top.haijunit.train.simulation.domain.SectionItem;
|
|
|
-import top.haijunit.train.utils.NumberUtil;
|
|
|
|
|
|
|
|
|
|
-import java.math.BigDecimal;
|
|
|
|
|
import java.util.*;
|
|
import java.util.*;
|
|
|
import java.util.function.Function;
|
|
import java.util.function.Function;
|
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
@@ -44,35 +45,26 @@ public class SectionService {
|
|
|
return list.stream().collect(HashMap::new, (map, entity) -> map.put(entity.getSelectionId(), entity), HashMap::putAll);
|
|
return list.stream().collect(HashMap::new, (map, entity) -> map.put(entity.getSelectionId(), entity), HashMap::putAll);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * 根据sectionId获取section
|
|
|
|
|
- *
|
|
|
|
|
- * @param sectionId 当前区段
|
|
|
|
|
- * @param offset 偏移量
|
|
|
|
|
- * @param distance 距离 可能是负数,计算车尾的位置
|
|
|
|
|
- * @return 前进指定距离后的所在区段
|
|
|
|
|
- */
|
|
|
|
|
- public SectionItem getSection(Long sectionId, BigDecimal offset, BigDecimal distance) {
|
|
|
|
|
- SectionItem sectionItem = this.getSection(sectionId);
|
|
|
|
|
- BigDecimal decimal = NumberUtil.add(offset, NumberUtil.mul(distance, sectionItem.getDirection().getFactor()));
|
|
|
|
|
- if (NumberUtil.isGreater(decimal, sectionItem.getSectionLength())) {
|
|
|
|
|
- Long sId = switch (sectionItem.getDirection()) {
|
|
|
|
|
- case UP -> sectionItem.getSectionPreId();
|
|
|
|
|
- case DOWN -> sectionItem.getSectionNextId();
|
|
|
|
|
- };
|
|
|
|
|
- return getSection(sId, new BigDecimal("0"), NumberUtil.sub(decimal, sectionItem.getSectionLength()));
|
|
|
|
|
- }
|
|
|
|
|
- return sectionItem;
|
|
|
|
|
|
|
+ @Cacheable(cacheNames = CacheNames.SECTION, key = "'id-'+#id")
|
|
|
|
|
+ public SectionItem getSectionItem(String id) {
|
|
|
|
|
+ return StreamUtils.filter(getAllSection(), item -> {
|
|
|
|
|
+ return item.getId().equals(id);
|
|
|
|
|
+ }).stream().findFirst().orElseThrow(() -> new IllegalArgumentException(String.format("不存在该区段,uid: %s", id)));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public SectionItem getSection(Long sectionId) {
|
|
|
|
|
- return StreamUtils.filter(getAllSection(), item -> item.getSectionId().equals(sectionId)).stream().findFirst().orElseThrow(() -> new IllegalArgumentException(String.format("不存在该区段,id: %s", sectionId)));
|
|
|
|
|
|
|
+ @Cacheable(cacheNames = CacheNames.SECTION, key = "'sectionId-'+#sectionId+'-direction-'+#direction.name()")
|
|
|
|
|
+ public SectionItem getSectionItem(Long sectionId, DirectionEnum direction) {
|
|
|
|
|
+ return StreamUtils.filter(getAllSection(), item -> {
|
|
|
|
|
+ return item.getSectionId().equals(sectionId) && item.getDirection().equals(direction);
|
|
|
|
|
+ }).stream().findFirst().orElseThrow(() -> new IllegalArgumentException(String.format("不存在该区段,sectionId: %s", sectionId)));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ @Cacheable(cacheNames = CacheNames.SECTION, key = "'routeId-'+#routeId")
|
|
|
public List<SectionItem> getSectionAll(Long routeId) {
|
|
public List<SectionItem> getSectionAll(Long routeId) {
|
|
|
return StreamUtils.filter(getAllSection(), item -> item.getRouteId().equals(routeId));
|
|
return StreamUtils.filter(getAllSection(), item -> item.getRouteId().equals(routeId));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ @Cacheable(cacheNames = CacheNames.SECTION, key = "'routeIds-' + T(java.util.Objects).hash(#routeIds)")
|
|
|
public List<SectionItem> getSectionAll(Collection<Long> routeIds) {
|
|
public List<SectionItem> getSectionAll(Collection<Long> routeIds) {
|
|
|
if (CollUtil.isEmpty(routeIds)) {
|
|
if (CollUtil.isEmpty(routeIds)) {
|
|
|
return new ArrayList<>();
|
|
return new ArrayList<>();
|
|
@@ -80,32 +72,23 @@ public class SectionService {
|
|
|
return StreamUtils.filter(getAllSection(), item -> CollUtil.contains(routeIds, item.getRouteId()));
|
|
return StreamUtils.filter(getAllSection(), item -> CollUtil.contains(routeIds, item.getRouteId()));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public Long getRouteNextId(Long sectionId) {
|
|
|
|
|
- SectionItem sectionItem = this.getSection(sectionId);
|
|
|
|
|
- RouteEntity route = routeRepository.findById(sectionItem.getRouteId()).orElseThrow(() -> new IllegalArgumentException(String.format("未找到进路数据,进路Id: %s", sectionItem.getRouteId())));
|
|
|
|
|
- RouteEntity entity = routeRepository.findById(route.getNextId()).orElseThrow(() -> new IllegalArgumentException(String.format("未找到进路数据,进路Id: %s", route.getNextId())));
|
|
|
|
|
|
|
+ @Cacheable(cacheNames = CacheNames.SECTION + "-RouteNextId", key = "'sectionId-'+#sectionId+'-direction-'+#direction.name()")
|
|
|
|
|
+ public Long getRouteNextId(Long sectionId, DirectionEnum direction) {
|
|
|
|
|
+ SectionItem sectionItem = this.getSectionItem(sectionId, direction);
|
|
|
|
|
+ RouteEntity route = routeRepository.findById(sectionItem.getRouteId()).orElseThrow(() -> new IllegalArgumentException(String.format("未找到进路数据,routeId: %s", sectionItem.getRouteId())));
|
|
|
|
|
+ RouteEntity entity = routeRepository.findById(route.getNextId()).orElseThrow(() -> new IllegalArgumentException(String.format("未找到进路数据, routeId: %s", route.getNextId())));
|
|
|
if (null == entity) {
|
|
if (null == entity) {
|
|
|
throw new IllegalArgumentException(String.format("没有找到下一个进路,区段Id:%s", sectionId));
|
|
throw new IllegalArgumentException(String.format("没有找到下一个进路,区段Id:%s", sectionId));
|
|
|
}
|
|
}
|
|
|
return entity.getId();
|
|
return entity.getId();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public boolean isTrigger(Long sectionId, BigDecimal offset) {
|
|
|
|
|
- SectionItem section = this.getSection(sectionId);
|
|
|
|
|
- RouteEntity route = routeRepository.findById(section.getRouteId()).orElseThrow(() -> new IllegalArgumentException(String.format("不存在该进路,id: %s", section.getRouteId())));
|
|
|
|
|
- if (sectionId.equals(CollUtil.getLast(route.getSelectionList()))) {
|
|
|
|
|
- // 进入最后一个区段才触发下一个进路
|
|
|
|
|
- BigDecimal distance = NumberUtil.add(offset, TrainConstant.ROUTE_TRIGGER_OFFSET);
|
|
|
|
|
- return NumberUtil.isLessOrEqual(section.getSectionLength(), distance);
|
|
|
|
|
- }
|
|
|
|
|
- return false;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
/**
|
|
/**
|
|
|
* 获取所有进路的区段
|
|
* 获取所有进路的区段
|
|
|
*
|
|
*
|
|
|
* @return 全部进路的区段
|
|
* @return 全部进路的区段
|
|
|
*/
|
|
*/
|
|
|
|
|
+ @Cacheable(cacheNames = CacheNames.SECTION, key = "'section_all'")
|
|
|
public List<SectionItem> getAllSection() {
|
|
public List<SectionItem> getAllSection() {
|
|
|
List<SectionItem> sectionList = new ArrayList<>();
|
|
List<SectionItem> sectionList = new ArrayList<>();
|
|
|
List<RouteEntity> routeList = routeRepository.findAll();
|
|
List<RouteEntity> routeList = routeRepository.findAll();
|
|
@@ -125,23 +108,25 @@ public class SectionService {
|
|
|
sectionItem.setSectionLength(selection.getSelectionLength());
|
|
sectionItem.setSectionLength(selection.getSelectionLength());
|
|
|
sectionItem.setStartKilometer(selection.getStartKilometer());
|
|
sectionItem.setStartKilometer(selection.getStartKilometer());
|
|
|
sectionItem.setSwitch(selection.isSwitch());
|
|
sectionItem.setSwitch(selection.isSwitch());
|
|
|
- sectionItem.setDirection(selection.getDirection());
|
|
|
|
|
|
|
+ sectionItem.setTurnBack(selection.getZfState());
|
|
|
// 进路数据
|
|
// 进路数据
|
|
|
|
|
+ sectionItem.setId(getSectionUId(route.getId(), selection.getId()));
|
|
|
sectionItem.setRouteId(route.getId());
|
|
sectionItem.setRouteId(route.getId());
|
|
|
sectionItem.setRouteName(route.getName());
|
|
sectionItem.setRouteName(route.getName());
|
|
|
|
|
+ sectionItem.setDirection(route.getDirection());
|
|
|
sectionItem.setSectionStatus(SectionStatusEnum.IDLE);
|
|
sectionItem.setSectionStatus(SectionStatusEnum.IDLE);
|
|
|
// 区段连接数据
|
|
// 区段连接数据
|
|
|
if (i == 0) {
|
|
if (i == 0) {
|
|
|
// 进路的第一个区段
|
|
// 进路的第一个区段
|
|
|
- sectionItem.setSectionPreId(this.getRouteSectionLast(route.getId()));
|
|
|
|
|
- sectionItem.setSectionNextId(selectionIds.get(i + 1));
|
|
|
|
|
|
|
+ sectionItem.setPreId(this.getRouteSectionLast(route.getId()));
|
|
|
|
|
+ sectionItem.setNextId(getSectionUId(route.getId(), selectionIds.get(i + 1)));
|
|
|
} else if (i + 1 >= selectionIds.size()) {
|
|
} else if (i + 1 >= selectionIds.size()) {
|
|
|
// 进路的最后一个区段
|
|
// 进路的最后一个区段
|
|
|
- sectionItem.setSectionPreId(selectionIds.get(i - 1));
|
|
|
|
|
- sectionItem.setSectionNextId(this.getRouteSectionFirst(route.getNextId()));
|
|
|
|
|
|
|
+ sectionItem.setPreId(getSectionUId(route.getId(), selectionIds.get(i - 1)));
|
|
|
|
|
+ sectionItem.setNextId(this.getRouteSectionFirst(route.getNextId()));
|
|
|
} else {
|
|
} else {
|
|
|
- sectionItem.setSectionPreId(selectionIds.get(i - 1));
|
|
|
|
|
- sectionItem.setSectionNextId(selectionIds.get(i + 1));
|
|
|
|
|
|
|
+ sectionItem.setPreId(getSectionUId(route.getId(), selectionIds.get(i - 1)));
|
|
|
|
|
+ sectionItem.setNextId(getSectionUId(route.getId(), selectionIds.get(i + 1)));
|
|
|
}
|
|
}
|
|
|
sectionList.add(sectionItem);
|
|
sectionList.add(sectionItem);
|
|
|
}
|
|
}
|
|
@@ -149,13 +134,24 @@ public class SectionService {
|
|
|
return sectionList;
|
|
return sectionList;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private Long getRouteSectionFirst(Long routeId) {
|
|
|
|
|
- Optional<RouteEntity> optional = routeRepository.findById(routeId);
|
|
|
|
|
- return optional.map(item -> CollUtil.getFirst(item.getSelectionList())).orElse(-1L);
|
|
|
|
|
|
|
+ private String getRouteSectionFirst(Long routeId) {
|
|
|
|
|
+ RouteEntity route = routeRepository.findById(routeId).orElseThrow(() -> new IllegalArgumentException(String.format("未找到进路数据,进路Id: %s", routeId)));
|
|
|
|
|
+ return getSectionUId(routeId, CollUtil.getFirst(route.getSelectionList()));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private Long getRouteSectionLast(Long routeNextId) {
|
|
|
|
|
|
|
+ private String getRouteSectionLast(Long routeNextId) {
|
|
|
RouteEntity entity = routeRepository.findByNextId(routeNextId);
|
|
RouteEntity entity = routeRepository.findByNextId(routeNextId);
|
|
|
- return Optional.of(entity).map(item -> CollUtil.getLast(item.getSelectionList())).orElse(-1L);
|
|
|
|
|
|
|
+ RouteEntity route = Optional.of(entity).orElseThrow(() -> new IllegalArgumentException(String.format("未找到进路数据,进路Id: %s", routeNextId)));
|
|
|
|
|
+ return getSectionUId(route.getId(), CollUtil.getLast(route.getSelectionList()));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 进路区段的唯一标识 用于查询唯一的下一个、下一个
|
|
|
|
|
+ * @param routeI 所在的进路Id
|
|
|
|
|
+ * @param selectionId 区段Id
|
|
|
|
|
+ * @return 进路区段Id
|
|
|
|
|
+ */
|
|
|
|
|
+ private String getSectionUId(long routeI, long selectionId) {
|
|
|
|
|
+ return StrUtil.join("-", routeI, selectionId);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|