package com.example.underground.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.underground.common.QueryParam;
import com.example.underground.common.Result;
import com.example.underground.entity.Line;
import com.example.underground.entity.PreandNextstation;
import com.example.underground.entity.Station;
import com.example.underground.entity.Subway;
import com.example.underground.service.LineService;
import com.example.underground.service.StationService;
import org.apache.ibatis.session.SqlSessionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.*;
/**
* <p>
* 前端控制器
* </p>
*
* @author xu
* @since 2024-04-04
*/
@RestController
@RequestMapping("/station")
public class StationController {
@Autowired
private StationService stationService;
@Autowired
private LineService lineService;
@PostMapping("getstation")
public Result getStation(@RequestBody QueryParam query){
HashMap param = query.getParam();
String startStation = (String) param.get("startStation");
String endStation = (String) param.get("endStation");
System.out.println(startStation);
System.out.println(endStation);
List<List<String>> allSubwayStations = getAllSubwayStations();
Subway subway = new Subway();
List<String> stationsBetween = subway.getStationsBetween(startStation,endStation,allSubwayStations);
List<Station> stations = new ArrayList<>();
for(String s : stationsBetween){
LambdaQueryWrapper<Station> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(Station::getStationName,s);
List<Station> station = stationService.list(lambdaQueryWrapper);
station.get(0).setIsExchange(0);
stations.add(station.get(0));
}
for(int i = 1;i<stations.size();i++){
if(stations.get(i).getLineNumber()!= stations.get(i-1).getLineNumber()){
stations.get(i).setIsExchange(1);
}
}
System.out.println("Stations between A and G: " + stationsBetween);
return Result.suc(stations);
}
// 数据库加一个字段来判断是不是中转站
//修改 应该发回站点的id名称
@PostMapping("getline")
public Result getLine(@RequestBody String keyword){
keyword = keyword.replaceAll("\"", "");
System.out.println(keyword);
QueryWrapper<Line> queryWrapper = new QueryWrapper<>();
queryWrapper.like("LineName",keyword);
List<Line> lists = lineService.list(queryWrapper);
List<Station> stations = getLineStation(lists.get(0).getLineNumber());
return Result.suc(stations);
}
@PostMapping("/getsinglestation")
public Result getSingleStation(@RequestBody String stationName) {
stationName = stationName.replaceAll("\"", "");
QueryWrapper<Station> queryWrapper = new QueryWrapper<>();
queryWrapper.like("StationName", stationName);
List<Station> stations = stationService.list(queryWrapper);
List<PreandNextstation> preandNextstations = new ArrayList<>();
for (Station s : stations) {
System.out.println(s.getStationName());
PreandNextstation preandNextstation = new PreandNextstation();
String preStation = "无前置";
String nextStation = "无后置";
try {
Station pre = stationService.getById(s.getStationID() - 1);
Station next = stationService.getById(s.getStationID() + 1);
preStation = pre == null ? "无前置" : pre.getStationName();
nextStation = next == null ? "无后置" : next.getStationName();
} catch (SqlSessionException e) {
e.printStackTrace();
}
preandNextstation.setLineName(lineService.getById(s.getLineNumber()).getLineName());
preandNextstation.setThisStation(s.getStationName());
preandNextstation.setPreStation(preStation);
preandNextstation.setNextStation(nextStation);
preandNextstations.add(preandNextstation);
}
return Result.suc(preandNextstations);
}
//将站点的所有信息返回
public List<Station> getLineStation(Integer lineNumber){
QueryWrapper<Station> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("LineNumber", lineNumber); // 假设 line_number 是 Station 实体类中的一个属性
List<Station> stationList = stationService.list(queryWrapper);
// // 将 Station 对象转换为字符串列表
// List<String> stationNames = new ArrayList<>();
// for (Station station : stationList) {
// stationNames.add(station.getStationName()); // 假设 Station 类有一个名为 getName 的方法来获取站点名称
// }
return stationList;
}
// 一次性代码,把新加的字段按是不是中转站赋成不同的值
// @PostMapping("isexchange")
// public Result changeAll(){
// HashMap<String, Integer> hashMap = new HashMap<>();
// LambdaQueryWrapper<Station> lambdaQueryWrapper = new LambdaQueryWrapper<>();
// List<Station> stations = stationService.list(lambdaQueryWrapper);
//
// // 统计站点名称出现的次数
// for (Station s : stations) {
// String stationName = s.getStationName();
// hashMap.put(stationName, hashMap.getOrDefault(stationName, 0) + 1);
// }
//
// // 遍历 hashMap,进行进一步操作
// for (Map.Entry<String, Integer> entry : hashMap.entrySet()) {
// String stationName = entry.getKey();
// int count = entry.getValue();
// if (count >= 2) {
// for(Station s : stations){
// if(s.getStationName().equals(stationName)){
// s.setIsExchange(1);
// stationService.updateById(s);
// }
// }
// }
// }
//
// return Result.suc();
// }
public List<List<String>> getAllSubwayStations() {
// 创建一个用于存储站点的 Map
Map<Integer, List<String>> subwayStationsMap = new HashMap<>();
// 查询数据库中所有的站点,并按照站点 ID 从小到大排序
List<Station> allStations = stationService.list();
Collections.sort(allStations, Comparator.comparing(Station::getStationID));
// 遍历所有的站点,按照地铁线路号将站点添加到相应的列表中
for (Station station : allStations) {
Integer lineNumber = station.getLineNumber();
String stationName = station.getStationName();
List<String> stationList = subwayStationsMap.getOrDefault(lineNumber, new ArrayList<>());
stationList.add(stationName);
subwayStationsMap.put(lineNumber, stationList);
}
// 将所有线路的站点列表放入一个 List<List<String>> 中,并按照地铁线路号进行排序
List<List<String>> allSubwayStations = new ArrayList<>();
for (Map.Entry<Integer, List<String>> entry : subwayStationsMap.entrySet()) {
allSubwayStations.add(entry.getValue());
}
return allSubwayStations;
}
}