Light12306/TrainInfomationProviderService/TrainInfo/TrainInfoSearchProvider.cs
2014-12-01 02:17:47 +08:00

163 lines
4.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TrainInfomationProviderService.StationInfo;
using TrainInfomationProviderService.TrainInfo.Entities;
namespace TrainInfomationProviderService.TrainInfo
{
public class TrainInfoSearchProvider
{
public IndexStorage Storage { get; private set; }
public Dictionary<string, HashSet<string>> SameStationInfo { get; set; }
public TrainInfoSearchProvider()
{
Trace.TraceInformation("[TRAIN_INFO_SEARCH_PROVIDER] 正在创建车站-车次信息索引对象");
Storage = TrainInfoManager.Instance.IndexStorage;
foreach (var trainInfoStorage in Storage.TrainInfoStorages)
{
Trace.TraceInformation("[TRAIN_INFO_SEARCH_PROVIDER] 正在初始化 {0} 车站-车次信息索引对象", trainInfoStorage.Key);
trainInfoStorage.Value.InitStationTrainData();
}
Trace.TraceInformation("[TRAIN_INFO_SEARCH_PROVIDER] 完成创建车站-车次信息索引对象");
}
public IEnumerable<TrainLineSegment> FindDirectTrains(DateTime date, string from, string to)
{
var infoStorage = Storage.TrainInfoStorages.GetValue(date.ToString("yyyy-MM-dd"));
if (infoStorage == null)
yield break;
for (int i = 0; i < infoStorage.Trains.Count; i++)
{
var train = infoStorage.Trains[i];
var stf = FindStop(train, from);
if (stf == null)
continue;
var stt = FindStop(train, to);
if (stt == null)
continue;
if (stf.Index >= stt.Index)
continue;
yield return new TrainLineSegment(train, stf, stt);
}
}
public TrainTransitOnceResultCollection FindOnceTransitTrains(DateTime date, string from, string to, TrainTransitSearchOptions options)
{
var result = new TrainTransitOnceResultCollection(date, options, from, to);
var infoStorage = Storage.TrainInfoStorages.GetValue(date.ToString("yyyy-MM-dd"));
if (infoStorage == null)
return result;
//发车经过车站
var fromStations = GetSameStations(from);
var toStations = GetSameStations(to);
var fromTrains = fromStations.Select(s => infoStorage.StationLeftTrainData.GetValue(s)).ExceptNull().SelectMany(s => s).ToArray();
var toTrains = toStations.Select(s => infoStorage.StationLeftTrainData.GetValue(s)).ExceptNull().SelectMany(s => s).ToArray();
foreach (var fromTrain in fromTrains)
{
var startStop = FindStop(fromTrain, from);
foreach (var fstop in fromTrain.TrainStops.SkipWhile(x => startStop != x).Skip(1))
{
foreach (var toTrain in toTrains)
{
var toStop = FindStop(toTrain, to);
foreach (var tStop in toTrain.TrainStops.TakeWhile(x => toStop != x))
{
if (fstop.Code == tStop.Code)
{
//一个中转点
var line = new TrainTransitOnceResult(
new TrainLineSegment(fromTrain, startStop, fstop),
new TrainLineSegment(toTrain, tStop, toStop),
date
);
result.Add(line);
break;
}
if (IsStopInclude(fstop.Code, tStop.Code))
{
//一个中转点
var line = new TrainTransitOnceResult(
new TrainLineSegment(fromTrain, startStop, fstop),
new TrainLineSegment(toTrain, tStop, toStop),
date,
true
);
result.Add(line);
break;
}
}
}
}
}
result.SecondaryAnalyze();
return result;
}
HashSet<string> GetSameStations(string code)
{
var map = SameStationManager.SameStationMap;
if (map != null)
{
var hash = map.GetValue(code);
if (hash != null)
return hash;
}
return new HashSet<string>() { code };
}
bool IsStopInclude(string code1, string code2)
{
var map = SameStationManager.SameStationMap;
if (map == null)
return false;
var set = map.GetValue(code1);
return set != null && set.Contains(code2);
}
TrainStop FindStop(Train train, string code)
{
if (train == null || train.TrainStopMap == null)
return null;
var stop = train.TrainStopMap.GetValue(code);
if (stop != null)
return stop;
var map = SameStationManager.SameStationMap;
if (map == null)
return null;
var hash = map.GetValue(code);
if (hash == null)
return null;
foreach (var s in hash)
{
stop = train.TrainStopMap.GetValue(s);
if (stop != null)
return stop;
}
return null;
}
}
}