SectionService.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. List<SectionItem> filter = StreamUtils.filter(getAllSection(), item -> item.getSectionId().equals(sectionId));
  52. if (filter.size() == 1) {
  53. return filter.get(0);
  54. }
  55. return StreamUtils.filter(getAllSection(), item -> {
  56. return item.getSectionId().equals(sectionId) && item.getDirection().equals(direction);
  57. }).stream().findFirst().orElseThrow(() -> new IllegalArgumentException(String.format("不存在该区段,sectionId: %s", sectionId)));
  58. }
  59. @Cacheable(cacheNames = CacheNames.SECTION, key = "'routeId-'+#routeId")
  60. public List<SectionItem> getSectionAll(Long routeId) {
  61. return StreamUtils.filter(getAllSection(), item -> item.getRouteId().equals(routeId));
  62. }
  63. @Cacheable(cacheNames = CacheNames.SECTION, key = "'routeIds-' + T(java.util.Objects).hash(#routeIds)")
  64. public List<SectionItem> getSectionAll(Collection<Long> routeIds) {
  65. if (CollUtil.isEmpty(routeIds)) {
  66. return new ArrayList<>();
  67. }
  68. List<SectionItem> allSection = getAllSection();
  69. Long firstRouteId = CollUtil.getFirst(allSection).getRouteId();
  70. Long lastRouteId = CollUtil.getLast(allSection).getRouteId();
  71. if (CollUtil.containsAll(routeIds, CollUtil.newArrayList(firstRouteId, lastRouteId))) {
  72. // 保证首尾相连的顺序
  73. ArrayList<SectionItem> result = new ArrayList<>();
  74. result.addAll(StreamUtils.filter(allSection, item -> item.getRouteId().equals(lastRouteId)));
  75. result.addAll(StreamUtils.filter(allSection, item -> item.getRouteId().equals(firstRouteId)));
  76. return result;
  77. }
  78. return StreamUtils.filter(getAllSection(), item -> CollUtil.contains(routeIds, item.getRouteId()));
  79. }
  80. @Cacheable(cacheNames = CacheNames.SECTION + "-RouteNextId", key = "'sectionId-'+#sectionId+'-direction-'+#direction.name()")
  81. public Long getRouteNextId(Long sectionId, DirectionEnum direction) {
  82. SectionItem sectionItem = this.getSectionItem(sectionId, direction);
  83. RouteEntity route = routeRepository.findById(sectionItem.getRouteId()).orElseThrow(() -> new IllegalArgumentException(String.format("未找到进路数据,routeId: %s", sectionItem.getRouteId())));
  84. RouteEntity entity = routeRepository.findById(route.getNextId()).orElseThrow(() -> new IllegalArgumentException(String.format("未找到进路数据, routeId: %s", route.getNextId())));
  85. if (null == entity) {
  86. throw new IllegalArgumentException(String.format("没有找到下一个进路,区段Id:%s", sectionId));
  87. }
  88. return entity.getId();
  89. }
  90. /**
  91. * 获取所有进路的区段
  92. *
  93. * @return 全部进路的区段
  94. */
  95. @Cacheable(cacheNames = CacheNames.SECTION, key = "'section_all'")
  96. public List<SectionItem> getAllSection() {
  97. List<SectionItem> sectionList = new ArrayList<>();
  98. List<RouteEntity> routeList = routeRepository.findAll();
  99. Map<Long, SelectionEntity> selectionMap = selectionRepository.findAll().stream().collect(Collectors.toMap(SelectionEntity::getId, Function.identity(), (v1, v2) -> v1));
  100. for (RouteEntity route : routeList) {
  101. ArrayList<Long> selectionIds = route.getSelectionList();
  102. for (int i = 0; i < selectionIds.size(); i++) {
  103. Long sectionId = selectionIds.get(i);
  104. SelectionEntity selection = selectionMap.get(sectionId);
  105. if (null == selection) {
  106. throw new IllegalArgumentException(String.format("未找到区段数据,区段Id: %s", sectionId));
  107. }
  108. // 区段数据
  109. SectionItem sectionItem = new SectionItem();
  110. sectionItem.setSectionId(selection.getId());
  111. sectionItem.setSectionName(selection.getName());
  112. sectionItem.setSectionLength(selection.getSelectionLength());
  113. sectionItem.setStartKilometer(selection.getStartKilometer());
  114. sectionItem.setSwitch(selection.isSwitch());
  115. sectionItem.setTurnBack(selection.getZfState());
  116. sectionItem.setStationId(selection.getStationId());
  117. // 进路数据
  118. sectionItem.setId(getSectionUId(route.getId(), selection.getId()));
  119. sectionItem.setRouteId(route.getId());
  120. sectionItem.setRouteName(route.getName());
  121. sectionItem.setDirection(route.getDirection());
  122. sectionItem.setSectionStatus(SectionStatusEnum.IDLE);
  123. // 区段连接数据
  124. if (i == 0) {
  125. // 进路的第一个区段
  126. sectionItem.setPreId(this.getRouteSectionLast(route.getId()));
  127. sectionItem.setNextId(getSectionUId(route.getId(), selectionIds.get(i + 1)));
  128. } else if (i + 1 >= selectionIds.size()) {
  129. // 进路的最后一个区段
  130. sectionItem.setPreId(getSectionUId(route.getId(), selectionIds.get(i - 1)));
  131. sectionItem.setNextId(this.getRouteSectionFirst(route.getNextId()));
  132. } else {
  133. sectionItem.setPreId(getSectionUId(route.getId(), selectionIds.get(i - 1)));
  134. sectionItem.setNextId(getSectionUId(route.getId(), selectionIds.get(i + 1)));
  135. }
  136. sectionList.add(sectionItem);
  137. }
  138. }
  139. return sectionList;
  140. }
  141. private String getRouteSectionFirst(Long routeId) {
  142. RouteEntity route = routeRepository.findById(routeId).orElseThrow(() -> new IllegalArgumentException(String.format("未找到进路数据,进路Id: %s", routeId)));
  143. return getSectionUId(routeId, CollUtil.getFirst(route.getSelectionList()));
  144. }
  145. private String getRouteSectionLast(Long routeNextId) {
  146. RouteEntity entity = routeRepository.findByNextId(routeNextId);
  147. RouteEntity route = Optional.of(entity).orElseThrow(() -> new IllegalArgumentException(String.format("未找到进路数据,进路Id: %s", routeNextId)));
  148. return getSectionUId(route.getId(), CollUtil.getLast(route.getSelectionList()));
  149. }
  150. /**
  151. * 进路区段的唯一标识 用于查询唯一的下一个、下一个
  152. * @param routeI 所在的进路Id
  153. * @param selectionId 区段Id
  154. * @return 进路区段Id
  155. */
  156. private String getSectionUId(long routeI, long selectionId) {
  157. return StrUtil.join("-", routeI, selectionId);
  158. }
  159. }