SectionService.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package top.haijunit.train.service;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.util.StrUtil;
  4. import lombok.RequiredArgsConstructor;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.springframework.cache.annotation.Cacheable;
  7. import org.springframework.stereotype.Service;
  8. import top.haijunit.common.utils.StreamUtils;
  9. import top.haijunit.train.domain.constant.CacheNames;
  10. import top.haijunit.train.domain.constant.DirectionEnum;
  11. import top.haijunit.train.domain.constant.SectionStatusEnum;
  12. import top.haijunit.train.domain.entity.RouteEntity;
  13. import top.haijunit.train.domain.entity.SelectionEntity;
  14. import top.haijunit.train.domain.entity.SignStopEntity;
  15. import top.haijunit.train.domain.repository.RouteRepository;
  16. import top.haijunit.train.domain.repository.SelectionRepository;
  17. import top.haijunit.train.domain.repository.SignStopRepository;
  18. import top.haijunit.train.simulation.domain.SectionItem;
  19. import java.util.*;
  20. import java.util.function.Function;
  21. import java.util.stream.Collectors;
  22. /**
  23. * @author zhanghaijun
  24. * @date 2023/12/9 11:14
  25. * @description 进路区段计算
  26. */
  27. @Slf4j
  28. @Service
  29. @RequiredArgsConstructor
  30. public class SectionService {
  31. private final RouteRepository routeRepository;
  32. private final SelectionRepository selectionRepository;
  33. private final SignStopRepository signStopRepository;
  34. /**
  35. * 获取所有停车标信息
  36. * @return 停车标集合
  37. */
  38. @Cacheable(cacheNames = CacheNames.SECTION, key = "'StopSignalMap'")
  39. public HashMap<Long, SignStopEntity> getStopSignalMap() {
  40. List<SignStopEntity> list = signStopRepository.findAll();
  41. return list.stream().collect(HashMap::new, (map, entity) -> map.put(entity.getSelectionId(), entity), HashMap::putAll);
  42. }
  43. @Cacheable(cacheNames = CacheNames.SECTION, key = "'id-'+#id")
  44. public SectionItem getSectionItem(String id) {
  45. return StreamUtils.filter(getAllSection(), item -> {
  46. return item.getId().equals(id);
  47. }).stream().findFirst().orElseThrow(() -> new IllegalArgumentException(String.format("不存在该区段,uid: %s", id)));
  48. }
  49. @Cacheable(cacheNames = CacheNames.SECTION, key = "'sectionId-'+#sectionId+'-direction-'+#direction.name()")
  50. public SectionItem getSectionItem(Long sectionId, DirectionEnum direction) {
  51. return StreamUtils.filter(getAllSection(), item -> {
  52. return item.getSectionId().equals(sectionId) && item.getDirection().equals(direction);
  53. }).stream().findFirst().orElseThrow(() -> new IllegalArgumentException(String.format("不存在该区段,sectionId: %s", sectionId)));
  54. }
  55. @Cacheable(cacheNames = CacheNames.SECTION, key = "'routeId-'+#routeId")
  56. public List<SectionItem> getSectionAll(Long routeId) {
  57. return StreamUtils.filter(getAllSection(), item -> item.getRouteId().equals(routeId));
  58. }
  59. @Cacheable(cacheNames = CacheNames.SECTION, key = "'routeIds-' + T(java.util.Objects).hash(#routeIds)")
  60. public List<SectionItem> getSectionAll(Collection<Long> routeIds) {
  61. if (CollUtil.isEmpty(routeIds)) {
  62. return new ArrayList<>();
  63. }
  64. return StreamUtils.filter(getAllSection(), item -> CollUtil.contains(routeIds, item.getRouteId()));
  65. }
  66. @Cacheable(cacheNames = CacheNames.SECTION + "-RouteNextId", key = "'sectionId-'+#sectionId+'-direction-'+#direction.name()")
  67. public Long getRouteNextId(Long sectionId, DirectionEnum direction) {
  68. SectionItem sectionItem = this.getSectionItem(sectionId, direction);
  69. RouteEntity route = routeRepository.findById(sectionItem.getRouteId()).orElseThrow(() -> new IllegalArgumentException(String.format("未找到进路数据,routeId: %s", sectionItem.getRouteId())));
  70. RouteEntity entity = routeRepository.findById(route.getNextId()).orElseThrow(() -> new IllegalArgumentException(String.format("未找到进路数据, routeId: %s", route.getNextId())));
  71. if (null == entity) {
  72. throw new IllegalArgumentException(String.format("没有找到下一个进路,区段Id:%s", sectionId));
  73. }
  74. return entity.getId();
  75. }
  76. /**
  77. * 获取所有进路的区段
  78. *
  79. * @return 全部进路的区段
  80. */
  81. @Cacheable(cacheNames = CacheNames.SECTION, key = "'section_all'")
  82. public List<SectionItem> getAllSection() {
  83. List<SectionItem> sectionList = new ArrayList<>();
  84. List<RouteEntity> routeList = routeRepository.findAll();
  85. Map<Long, SelectionEntity> selectionMap = selectionRepository.findAll().stream().collect(Collectors.toMap(SelectionEntity::getId, Function.identity(), (v1, v2) -> v1));
  86. for (RouteEntity route : routeList) {
  87. ArrayList<Long> selectionIds = route.getSelectionList();
  88. for (int i = 0; i < selectionIds.size(); i++) {
  89. Long sectionId = selectionIds.get(i);
  90. SelectionEntity selection = selectionMap.get(sectionId);
  91. if (null == selection) {
  92. throw new IllegalArgumentException(String.format("未找到区段数据,区段Id: %s", sectionId));
  93. }
  94. // 区段数据
  95. SectionItem sectionItem = new SectionItem();
  96. sectionItem.setSectionId(selection.getId());
  97. sectionItem.setSectionName(selection.getName());
  98. sectionItem.setSectionLength(selection.getSelectionLength());
  99. sectionItem.setStartKilometer(selection.getStartKilometer());
  100. sectionItem.setSwitch(selection.isSwitch());
  101. sectionItem.setTurnBack(selection.getZfState());
  102. sectionItem.setStationId(selection.getStationId());
  103. // 进路数据
  104. sectionItem.setId(getSectionUId(route.getId(), selection.getId()));
  105. sectionItem.setRouteId(route.getId());
  106. sectionItem.setRouteName(route.getName());
  107. sectionItem.setDirection(route.getDirection());
  108. sectionItem.setSectionStatus(SectionStatusEnum.IDLE);
  109. // 区段连接数据
  110. if (i == 0) {
  111. // 进路的第一个区段
  112. sectionItem.setPreId(this.getRouteSectionLast(route.getId()));
  113. sectionItem.setNextId(getSectionUId(route.getId(), selectionIds.get(i + 1)));
  114. } else if (i + 1 >= selectionIds.size()) {
  115. // 进路的最后一个区段
  116. sectionItem.setPreId(getSectionUId(route.getId(), selectionIds.get(i - 1)));
  117. sectionItem.setNextId(this.getRouteSectionFirst(route.getNextId()));
  118. } else {
  119. sectionItem.setPreId(getSectionUId(route.getId(), selectionIds.get(i - 1)));
  120. sectionItem.setNextId(getSectionUId(route.getId(), selectionIds.get(i + 1)));
  121. }
  122. sectionList.add(sectionItem);
  123. }
  124. }
  125. return sectionList;
  126. }
  127. private String getRouteSectionFirst(Long routeId) {
  128. RouteEntity route = routeRepository.findById(routeId).orElseThrow(() -> new IllegalArgumentException(String.format("未找到进路数据,进路Id: %s", routeId)));
  129. return getSectionUId(routeId, CollUtil.getFirst(route.getSelectionList()));
  130. }
  131. private String getRouteSectionLast(Long routeNextId) {
  132. RouteEntity entity = routeRepository.findByNextId(routeNextId);
  133. RouteEntity route = Optional.of(entity).orElseThrow(() -> new IllegalArgumentException(String.format("未找到进路数据,进路Id: %s", routeNextId)));
  134. return getSectionUId(route.getId(), CollUtil.getLast(route.getSelectionList()));
  135. }
  136. /**
  137. * 进路区段的唯一标识 用于查询唯一的下一个、下一个
  138. * @param routeI 所在的进路Id
  139. * @param selectionId 区段Id
  140. * @return 进路区段Id
  141. */
  142. private String getSectionUId(long routeI, long selectionId) {
  143. return StrUtil.join("-", routeI, selectionId);
  144. }
  145. }