bool TryConvertToResultItem(string data, QueryResult result, out QueryResultItem item)
 {
     item = null;
     try
     {
         item = ConvertToResultItem(data, result);
         return(true);
     }
     catch (Exception e)
     {
         return(false);
     }
 }
        /// <summary>
        /// 根据上车站时间推算全部时间
        /// </summary>
        /// <param name="currentDate"></param>
        /// <param name="startTrainDate"></param>
        public void ApplyTime(QueryResultItem train, DateTime?startTrainDate, DateTime currentDate)
        {
            if (Count == 0)
            {
                return;
            }

            var classL = Regex.IsMatch(train.Code, @"^(K[456]\d{3})$", RegexOptions.IgnoreCase);


            if (startTrainDate != null)
            {
                var start    = this[0];
                var baseDate = startTrainDate.Value;
                Debug.Assert(start.DepartureTime != null, "start.DepartureTime != null");
                start.DepartureFullTime = baseDate + start.DepartureTime.Value;

                for (var i = 1; i < Count; i++)
                {
                    var t = this[i];
                    t.ArriveFullTime = baseDate.Add(t.ArriveTime.Value);
                    if (t.ArriveFullTime.Value < this[i - 1].DepartureFullTime.Value)
                    {
                        baseDate         = baseDate.AddDays(1);
                        t.ArriveFullTime = baseDate.Add(t.ArriveTime.Value);
                    }

                    if (classL && (t.ArriveFullTime.Value - start.DepartureFullTime.Value).TotalMinutes <= 60)
                    {
                        baseDate         = baseDate.AddDays(1);
                        t.ArriveFullTime = t.ArriveFullTime.Value.AddHours(24);
                    }

                    if (t.DepartureTime != null)
                    {
                        if (t.StopHoverTime == null)
                        {
                            t.StopHoverTime = 0;
                        }

                        t.DepartureFullTime = baseDate.Add(t.DepartureTime.Value);
                        if (t.DepartureFullTime.Value < t.ArriveFullTime.Value)
                        {
                            baseDate         = t.ArriveFullTime.Value.Date.AddDays(1);
                            t.ArriveFullTime = baseDate.Add(t.DepartureTime.Value);
                        }
                    }
                }
            }
            else
            {
                var currentStation = this.First(s => s.IsEnabled);
                //var endStation = this.Last(s => s.IsEnabled);

                currentStation.DepartureFullTime = currentDate.Date + currentStation.DepartureTime.Value;
                currentStation.ArriveFullTime    = currentDate.Date + currentStation.ArriveTime;
                if (currentStation.ArriveFullTime.HasValue && currentStation.ArriveFullTime.Value < currentStation.ArriveFullTime.Value)
                {
                    currentStation.ArriveFullTime = currentStation.ArriveFullTime.Value.AddDays(1);
                }

                //往前推测时间
                TrainStopInfo current = null;
                foreach (var source in this.TakeWhile(s => s != currentStation).Reverse())
                {
                    current = current ?? currentStation;
                    source.DepartureFullTime = current.ArriveFullTime.Value.Date + source.DepartureTime.Value;
                    if (source.DepartureFullTime.Value >= current.DepartureFullTime.Value)
                    {
                        source.DepartureFullTime = source.DepartureFullTime.Value.AddDays(-1);
                    }
                    else if (classL && (current.ArriveFullTime.HasValue && (current.ArriveFullTime.Value - source.DepartureFullTime.Value).TotalMinutes <= 60))
                    {
                        source.DepartureFullTime = source.DepartureFullTime.Value.AddDays(-1);
                    }

                    if (source.ArriveTime != null)
                    {
                        source.ArriveFullTime = source.DepartureFullTime.Value.AddMinutes(-source.StopHoverTime.Value);
                    }

                    current = source;
                }

                //往后推测时间
                current = null;
                foreach (var source in this.SkipWhile(s => s != currentStation).Skip(1))
                {
                    current = current ?? currentStation;
                    source.ArriveFullTime = current.DepartureFullTime.Value.Date + source.ArriveTime.Value;
                    if (source.ArriveFullTime.Value <= current.DepartureFullTime.Value || (classL && (source.ArriveFullTime.Value - current.DepartureFullTime.Value).TotalMinutes <= 60))
                    {
                        source.ArriveFullTime = source.ArriveFullTime.Value.AddDays(1);
                    }

                    if (source.DepartureTime != null && source.StopHoverTime != null)
                    {
                        source.DepartureFullTime = source.ArriveFullTime.Value.AddMinutes(source.StopHoverTime.Value);
                    }

                    current = source;
                }
            }
        }
Exemple #3
0
        public QueryResultItem ToQueryResultItem(QueryResult result)
        {
            var queryDate = result.Date;
            var tinfo     = queryLeftNewDTO;

            var      startTrainDateMatch  = Regex.Match(queryLeftNewDTO.start_train_date, @"(\d{4})(\d{2})(\d{2})");
            var      trainControlDayMatch = Regex.Match(tinfo.control_train_day ?? "", @"(\d{4})(0(\d)|([^0]\d))(0(\d)|([^0]\d))");
            DateTime?trainControlDay      = trainControlDayMatch.Success ? new DateTime(
                trainControlDayMatch.GetGroupValue(1).ToInt32(),
                trainControlDayMatch.GetGroupValue(3).ToInt32(),
                trainControlDayMatch.GetGroupValue(5).ToInt32()
                ) : (DateTime?)null;
            var train = new QueryResultItem(result)
            {
                RawResult    = this,
                Id           = tinfo.train_no,
                Code         = tinfo.station_train_code,
                ElapsedTime  = new TimeSpan(0, tinfo.lishiValue, 0),
                StartStation = new TicketStationInfo()
                {
                    StationName   = tinfo.start_station_name,
                    Code          = tinfo.start_station_telecode,
                    DepartureTime = startTrainDateMatch.Success ? (DateTime?)new DateTime(startTrainDateMatch.GetGroupValue(1).ToInt32(), startTrainDateMatch.GetGroupValue(2).ToInt32(), startTrainDateMatch.GetGroupValue(3).ToInt32()) : null
                },
                FromStation = new TicketStationInfo()
                {
                    StationName = tinfo.from_station_name,
                    Code        = tinfo.from_station_telecode,
                    IsFirst     = tinfo.from_station_telecode == tinfo.start_station_telecode
                },
                ToStation = new TicketStationInfo()
                {
                    StationName = tinfo.to_station_name,
                    Code        = tinfo.to_station_telecode,
                    IsEnd       = tinfo.to_station_telecode == tinfo.end_station_telecode
                },
                EndStation = new TicketStationInfo()
                {
                    StationName = tinfo.end_station_name,
                    Code        = tinfo.end_station_telecode
                },
                ControlFlag         = tinfo.controlled_train_flag ?? 0,
                ControlMessage      = tinfo.controlled_train_message,
                AlmostIllegalResult = !ApiConfiguration.Instance.DisableIllegalDetect && tinfo.controlled_train_flag == null,
                AllowBackup         = tinfo.AllowBackup
            };

            train.FromStation.DepartureTime = queryDate.Add(ParseTimeSpan(tinfo.start_time));
            train.ToStation.ArriveTime      = train.FromStation.DepartureTime.Value.Add(train.ElapsedTime);
            train.SubmitOrderInfo           = secretStr;
            train.IsAvailable         = tinfo.canWebBuy == "Y";
            train.ButtonTextInfo      = Regex.Replace(buttonTextInfo ?? "", @"(\s|<[^>]*>)", "");
            train.IsLimitSell         = train.ControlFlag > 0 || train.ButtonTextInfo.IndexOf("暂售", StringComparison.Ordinal) != -1;
            train.CanExchangeByCredit = tinfo.exchange_train_flag == "1";
            if (!train.IsAvailable && trainControlDay < queryDate && trainControlDay.HasValue)
            {
                train.ControlFlag    = 1;
                train.IsLimitSell    = true;
                train.ControlMessage = $"限售至{trainControlDay.Value.ToShortDateString()}";
            }

            TOBA.Entity.ParseUtility.GetTicketCountFromOrderInfo(tinfo, train.TicketCount);
            //折扣
            if (!string.IsNullOrEmpty(tinfo.yp_ex))
            {
                for (var i = 0; i < (tinfo.yp_ex.Length - 1); i += 2)
                {
                    if (tinfo.yp_ex[i + 1] == '1')
                    {
                        train.TicketCount[tinfo.yp_ex[i]].HasDiscount = true;
                    }
                }
            }
            if (buttonTextInfo?.IndexOf("起售", StringComparison.Ordinal) >= 0)
            {
                var now = DateTime.Now.Date;
                TOBA.Entity.ParseUtility.Match(@"(0*(\d+)月0*(\d+)日)?(\d+)\s*点\s*((\d+)分)?\s*起售", Regex.Replace(buttonTextInfo, "<[^>]+>", ""), _ =>
                {
                    if (_[0].IsNullOrEmpty())
                    {
                        train.BeginSellTime = now.Date.AddHours(_[4].ToInt32());
                    }
                    else
                    {
                        var mm     = _[2].ToInt32(now.Month);
                        var dd     = _[3].ToInt32(now.Day);
                        var minute = _[6].ToInt32(0);

                        var year = now.Year;
                        if (mm < now.Month)
                        {
                            year++;
                        }
                        train.BeginSellTime = new DateTime(year, mm, dd, _[4].ToInt32(), minute, 0);
                    }
                }, "起售时间");
            }
            //if (tinfo.controlled_train_flag == 0 && !tinfo.control_train_day.IsNullOrEmpty() && !train.IsAvailable)
            //{
            //	var tm = Regex.Match(tinfo.control_train_day, @"(\d2)(\d2)");

            //	if (dm.Success && tm.Success)
            //		train.BeginSellTime = new DateTime(
            //										 dm.GetGroupValue(1).ToInt32(),
            //										 dm.GetGroupValue(2).ToInt32(),
            //										 dm.GetGroupValue(3).ToInt32(),
            //										 tm.GetGroupValue(1).ToInt32(),
            //										 tm.GetGroupValue(2).ToInt32(),
            //										 0
            //										);
            //}

            //有可疑的结果?


            return(train);
        }