2024.4.16
第三十天
所花时间:2小时
代码量:200+
博客量:1
了解到的知识点:services代码
1 package com.example.metroinfo.service; 2 import com.example.metroinfo.model.MetroLine; 3 import com.example.metroinfo.model.MetroStation; 4 import com.example.metroinfo.model.MetroSystem; 5 import com.example.metroinfo.repository.MetroStationRepository; 6 import com.example.metroinfo.repository.MetroSystemRepository; 7 import org.jgrapht.Graph; 8 import org.jgrapht.GraphPath; 9 import org.jgrapht.alg.shortestpath.DijkstraShortestPath; 10 import org.jgrapht.graph.DefaultEdge; 11 import org.jgrapht.graph.DefaultUndirectedGraph; 12 import org.springframework.beans.factory.annotation.Autowired; 13 import org.springframework.stereotype.Service; 14 import java.util.*; 15 import java.util.stream.Collectors; 16 17 @Service 18 public class MetroInfoService { 19 @Autowired 20 MetroSystemRepository metroSystemRepository; 21 @Autowired 22 MetroStationRepository metroStationRepository; 23 static HashMap<String, Graph<String, DefaultEdge>> allMetroStationGraphs = new HashMap<>(); 24 static HashMap<String, DijkstraShortestPath<String, DefaultEdge>> allDijkstraAlgMetroSearch = new HashMap<>(); 25 static HashMap<String, HashMap<String,MetroStation>> allMetroStationHashMaps = new HashMap<>(); 26 27 28 ////////////////////////// 29 30 //根据地铁系统编号和线路编号查询所有站点 31 public List<MetroStation> getAllStationsBySystemCodeAndLineNumber(String systemName, String lineNumber) { 32 MetroSystem metroSystem = metroSystemRepository.findBySystemName(systemName); 33 if (metroSystem == null) { 34 return null; 35 } 36 MetroLine targetLine = metroSystem.getLines().stream() 37 .filter(line -> line.getLineNumber().equals(lineNumber)) 38 .findFirst() 39 .orElse(null); 40 41 if (targetLine == null) { 42 return null; 43 } 44 return targetLine.getStations(); 45 } 46 47 ////////////////////////// 48 49 public String addMetroSystem(MetroSystem metroSystem) { 50 metroSystemRepository.save(metroSystem); 51 System.out.println(loadMetroSystem(metroSystem) + " " + metroSystem.getSystemCode()); 52 return "Success"; 53 } 54 55 public String loadAllMetroSystem() { 56 List<MetroSystem> metroSystemList = metroSystemRepository.findAll(); 57 for (MetroSystem metroSystem:metroSystemList) { 58 System.out.println(loadMetroSystem(metroSystem) + " " + metroSystem.getSystemCode()); 59 } 60 return "Success loadAllMetroSystem"; 61 } 62 63 public String loadMetroSystem(MetroSystem metroSystem) { 64 if (!allMetroStationHashMaps.containsKey(metroSystem.getSystemCode())) { 65 Graph<String, DefaultEdge> metroStationGraph = new DefaultUndirectedGraph<>(DefaultEdge.class); 66 DijkstraShortestPath<String, DefaultEdge> dijkstraAlgMetroSearch = new DijkstraShortestPath<>(metroStationGraph); 67 HashMap<String,MetroStation> metroStationHashMap = new HashMap<>(); 68 for (MetroLine metroLine:metroSystem.getLines()) { 69 MetroStation previousMetroStation = null; 70 for (MetroStation currentMetroStation:metroLine.getStations()) { 71 metroStationGraph.addVertex(currentMetroStation.getStationCode()); 72 if (previousMetroStation != null) { 73 metroStationGraph.addEdge(previousMetroStation.getStationCode(),currentMetroStation.getStationCode()); 74 } 75 previousMetroStation = currentMetroStation; 76 } 77 } 78 for (MetroLine metroLine:metroSystem.getLines()) { 79 for (MetroStation currentMetroStation:metroLine.getStations()) { 80 currentMetroStation.getLineNumbers().add(metroLine.getLineNumber()); 81 if (currentMetroStation.getIsTransferStation()) { 82 if (metroStationHashMap.containsKey(currentMetroStation.getStationCode())) { 83 MetroStation existingMetroStation = metroStationHashMap.get(currentMetroStation.getStationCode()); 84 existingMetroStation.getLineNumbers().add(metroLine.getLineNumber()); 85 metroStationHashMap.put(currentMetroStation.getStationCode(),existingMetroStation); 86 continue; 87 } 88 } 89 metroStationHashMap.put(currentMetroStation.getStationCode(),currentMetroStation); 90 } 91 } 92 allMetroStationGraphs.put(metroSystem.getSystemCode(),metroStationGraph); 93 allDijkstraAlgMetroSearch.put(metroSystem.getSystemCode(),dijkstraAlgMetroSearch); 94 allMetroStationHashMaps.put(metroSystem.getSystemCode(),metroStationHashMap); 95 return "Success"; 96 } 97 return "Already Exists"; 98 } 99 public List<MetroStation> searchShortestPath(String systemCode, String start, String destination) { 100 if (allDijkstraAlgMetroSearch.containsKey(systemCode)) { 101 DijkstraShortestPath<String, DefaultEdge> dijkstraAlgMetroSearch = allDijkstraAlgMetroSearch.get(systemCode); 102 Map<String, MetroStation> metroStationHashMap = allMetroStationHashMaps.get(systemCode); 103 104 // 获取起始站点的站点代码 105 String startStationCode = getStationCodeFromName(metroStationHashMap, start); 106 // 获取目标站点的站点代码 107 String destinationStationCode = getStationCodeFromName(metroStationHashMap, destination); 108 109 if (startStationCode != null && destinationStationCode != null) { 110 // 计算最短路径 111 GraphPath<String, DefaultEdge> shortestPath = dijkstraAlgMetroSearch.getPath(startStationCode, destinationStationCode); 112 if (shortestPath != null) { 113 // 将站点代码转换为 MetroStation 对象列表 114 List<MetroStation> pathStationList = shortestPath.getVertexList() 115 .stream() 116 .map(metroStationHashMap::get) 117 .collect(Collectors.toList()); 118 return pathStationList; 119 } 120 } 121 } 122 return null; 123 } 124 125 // 辅助方法:根据站点名称从 metroStationHashMap 中获取对应的站点代码 126 private String getStationCodeFromName(Map<String, MetroStation> metroStationHashMap, String stationName) { 127 for (MetroStation station : metroStationHashMap.values()) { 128 if (station.getStationName().equalsIgnoreCase(stationName)) { 129 return station.getStationCode(); 130 } 131 } 132 return null; // 如果找不到对应的站点名称,返回 null 133 } 134 135 136 public List<MetroStation> searchByLine(String systemCode,String lineNumber) { 137 if (allMetroStationHashMaps.containsKey(systemCode)) { 138 MetroSystem metroSystem = metroSystemRepository.findBySystemCode(systemCode); 139 for (MetroLine metroLine:metroSystem.getLines()) { 140 if (metroLine.getLineNumber().equals(lineNumber)) { 141 return metroLine.getStations(); 142 } 143 } 144 } 145 146 return null; 147 } 148 public MetroSystem getMetroSystem(String systemCode) { 149 MetroSystem metroSystem = metroSystemRepository.findBySystemCode(systemCode); 150 return metroSystem; 151 } 152 153 public List<MetroSystem> findAllMetroSystem() { 154 return metroSystemRepository.findAll(); 155 } 156 157 public List<MetroSystem> findSystemBySystemName(String systemName) { 158 return metroSystemRepository.findAllBySystemNameContaining(systemName); 159 } 160 161 public List<MetroStation> findStationByStationNameContaining(String systemCode,String stationName) { 162 if (allMetroStationHashMaps.containsKey(systemCode)) { 163 Map<String, MetroStation> metroStationHashMap = allMetroStationHashMaps.get(systemCode); 164 return metroStationHashMap.values().stream() 165 .filter(metroStation -> metroStation.getStationName().contains(stationName)) 166 .collect(Collectors.toList()); 167 } 168 return null; 169 } 170 171 public List<MetroLine> getLinesBySystemName(String systemName) { 172 MetroSystem metroSystem = metroSystemRepository.findBySystemName(systemName); 173 if (metroSystem == null) { 174 return Collections.emptyList(); // 如果未找到地铁系统,返回空列表 175 } 176 return metroSystem.getLines(); // 返回地铁系统的线路列表 177 } 178 179 // 根据 systemName 查询对应的 MetroSystem 对象 180 public MetroSystem findMetroSystemBySystemName(String systemName) { 181 try { 182 // 调用 Repository 层方法,根据 systemName 查询对应的 MetroSystem 对象 183 return metroSystemRepository.findBySystemName(systemName); 184 } catch (Exception e) { 185 // 处理异常情况,比如数据库访问异常等 186 e.printStackTrace(); 187 return null; // 或者抛出自定义异常,视情况而定 188 } 189 } 190 191 192 public MetroStation findStationByStationName(String stationName) { 193 try { 194 // 调用 Repository 层方法,根据 stationName 查询对应的 MetroStation 对象 195 return metroStationRepository.findByStationName(stationName); 196 } catch (Exception e) { 197 // 处理异常情况,比如数据库访问异常等 198 e.printStackTrace(); 199 return null; // 或者抛出自定义异常,视情况而定 200 } 201 } 202 203 204 }