using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using TrainInfomationProviderService.StationInfo; using TrainInfomationProviderService.StationInfo.Entities; namespace TrainInfomationProviderService.TrainInfo.Entities { using System.Text.RegularExpressions; public class TrainLineSegment { StationDetailInfo _toStation; StationDetailInfo _fromStation; TimeSpan? _elapsedTime; public Train Train { get; private set; } public TrainStop From { get; private set; } public TrainStop To { get; private set; } public bool IsBegin { get { return From.Arrive == null; } } public bool IsEnd { get { return To.Left == null; } } public StationDetailInfo FromStation { get { if (_fromStation == null) _fromStation = StationManager.Instance.Storage.Stations.GetValue(From.Code); return _fromStation; } } public StationDetailInfo ToStation { get { if (_toStation == null) _toStation = StationManager.Instance.Storage.Stations.GetValue(To.Code); return _toStation; } } /// /// 历时 /// public TimeSpan ElapsedTime => _elapsedTime ?? TimeSpan.Zero; int? _calculatedMinutesBase; /// /// 进行时间对比的基础分钟数 /// public int CalculatedMinutesBase { get { if (_calculatedMinutesBase == null) { var minutes = ElapsedTime.TotalMinutes; _calculatedMinutesBase = Utility.CalculateTimeForCompare(Train.TrainClass, minutes); } return _calculatedMinutesBase ?? 0; } } /// /// 创建 的新实例(TrainLineSegment) /// /// /// /// public TrainLineSegment(Train train, TrainStop from, TrainStop to, DateTime baseDate) { Train = train; From = from; To = to; _left = baseDate.Date.Add(from.Left.Value.AddDays(-from.Left.Value.Days)); if (_left < baseDate) _left = _left.AddDays(1); //_arrive = _left.Add(to.Arrive.Value - from.Left.Value); //计算历时 var stopList = train.TrainStops.SkipWhile(s => s != From).TakeWhile(s => s != to).Concat(new[] { to }).ToArray(); var ts = TimeSpan.Zero; var classL = Regex.IsMatch(train.Code, @"^(K[456]\d{3})$", RegexOptions.IgnoreCase); for (int i = 1; i < stopList.Length; i++) { var elpTime = (stopList[i].Arrive ?? TimeSpan.Zero) - (stopList[i - 1].Left ?? TimeSpan.Zero); if (elpTime.TotalMinutes < 60 && classL) elpTime = elpTime.AddHours(24); ts += elpTime; if (i < stopList.Length - 1) ts += stopList[i].StopTime; } _elapsedTime = ts; _arrive = (_left + ts).Date.Add(to.Arrive.Value); } DateTime _left, _arrive; public DateTime LeftTime { get { return _left; } } public DateTime ArriveTime { get { return _arrive; } } } }