/// <summary> /// 分析航班 /// </summary> /// <param name="voyage">整个航程信息</param> /// <param name="scheduledFlight">航班号如:*MU8966 或者 8C8290</param> /// <param name="seat">航程MODEL</param> protected void AnalyseScheduledFlight(string voyage, string scheduledFlight, ref MPnrSeat seat) { ////将承运人和航班拆分 Regex reg = new Regex(@"\*?(?<CYR>\w{2})(?<HBH>[A-Z0-9]{2,6})"); Match mat = reg.Match(voyage); if (!mat.Success) { return; } ////航班号 seat.FlightNO = mat.Groups["CYR"].Value + mat.Groups["HBH"].Value; ////如果有 "*"则认为是共享航班 if (Regex.IsMatch(scheduledFlight, @"\*")) { seat.IsShareFlight = true; ////匹配共享航班 reg = new Regex(@"(?<CYR>\w{2})(?<HBH>[A-Z0-9]{2,6})$"); mat = reg.Match(voyage); if (mat.Success) { seat.ShareFlightNO = mat.Groups["CYR"].Value + mat.Groups["HBH"].Value; } } }
/// <summary> /// 把解析到的时间组合成DATETIME,出发及到达时间 /// </summary> /// <param name="month">月SEP</param> /// <param name="day">日</param> /// <param name="departureTime">出发时间</param> /// <param name="arriveTime">到达时间</param> /// <param name="seat">航程对象</param> protected void ComposeTime(string month, string day, string departureTime, string arriveTime, ref MPnrSeat seat) { ////以数字表示的月份 string monthNum = "01"; ////翻译月份 switch (month) { case "JAN": monthNum = "01"; break; case "FEB": monthNum = "02"; break; case "MAR": monthNum = "03"; break; case "APR": monthNum = "04"; break; case "MAY": monthNum = "05"; break; case "JUN": monthNum = "06"; break; case "JUL": monthNum = "07"; break; case "AUG": monthNum = "08"; break; case "SEP": monthNum = "09"; break; case "OCT": monthNum = "10"; break; case "NOV": monthNum = "11"; break; case "DEC": monthNum = "12"; break; } ////当前年 DateTime now = DateTime.Now; int year = now.Year; ////如果预订的月份小于当前月份则为跨年 if (now.Month > int.Parse(monthNum)) { year++; } ////组合时间字符串 StringBuilder dateTime = new StringBuilder(); dateTime.Append(year.ToString()); dateTime.Append("-"); dateTime.Append(monthNum); dateTime.Append("-"); dateTime.Append(day); dateTime.Append(" "); string date = dateTime.ToString(); dateTime.Append(departureTime.Insert(2, ":")); ////组合后的时间 DateTime composeTime; ////出发时间 if (!DateTime.TryParse(dateTime.ToString(), out composeTime)) { composeTime = now; } seat.DepartureTime = composeTime; dateTime = new StringBuilder(date); dateTime.Append(arriveTime.Insert(2, ":")); ////到达时间 if (!DateTime.TryParse(dateTime.ToString(), out composeTime)) { composeTime = now; } seat.ArrivalTime = composeTime; ////处理跨天 if (seat.ArrivalTime < seat.DepartureTime) { seat.ArrivalTime = seat.ArrivalTime.AddDays(1); } }
/// <summary> /// 解析航程舱位 /// </summary> /// <returns>航程集合</returns> public List <MPnrSeat> GetSeats(string pnrContentStr) { ////航程: 2. 3U8881 G TH25SEP CTUPEK HK1 0720 0940 E --T3 List <MPnrSeat> seats = new List <MPnrSeat>(); ////解决联程换行匹配不到的问题 Regex reg = new Regex(@"\d{1,2}\. (\r\n| |)+\*?\w{2}\d{2,5}(?:\n|.)+?(?=\d{1,2}\.)"); MatchCollection mats = reg.Matches(pnrContentStr); if (mats.Count == 0) { //// 解决航班字母与数字之间有空格的问题 reg = new Regex(@"\d{1,2}\. (\r\n| |)+\*?\w{2}\s*\d{2,5}(?:\n|.)+?(?=\d{1,2}\.)"); mats = reg.Matches(pnrContentStr); } MPnrSeat seat; ////匹配航程信息 string voyagePattern = @"(?<HB>\*?\w{2}\s*[A-Z0-9]{2,6}).*(?<CW>[A-Z])\d?\W+\w{2}(?<DAY>\d{2})(?<MONTH>[A-Z]{3})(?:\W*|\d{2})(?<FCITY>[A-Z]{3})(?<TCITY>[A-Z]{3})\W*(?<STATUS>[A-Z]{2})\W*\d*\W*(?<FTIME>\d{4})\W*(?<TTIME>\d{4})"; voyagePattern += @"\s*[\+|\-]?\s*\d?\W*E?\s*(?<BUILD>[A-Z0-9\-\s*]*)"; // 航站楼解决 if (mats.Count == 0) { System.Exception ex = new System.Exception("从PNR内容中获取航程信息失败。"); throw ex; } foreach (Match mat in mats) { seat = new MPnrSeat(); reg = new Regex(voyagePattern); string voyageInfo = Regex.Replace(mat.Value, @"\n|\r", string.Empty).Trim(); Match matVoyage = reg.Match(voyageInfo); if (!matVoyage.Success) { System.Exception ex = new System.Exception("分析航程信息出错。"); throw ex; } ////航班 删除航班中间空格 this.AnalyseScheduledFlight(voyageInfo, matVoyage.Groups["HB"].Value.Replace(" ", string.Empty), ref seat); ////舱位 seat.SeatCode = matVoyage.Groups["CW"].Value; // 处理子舱位 string childSeatClass = string.Empty; Match matChild = Regex.Match(voyageInfo, string.Format(@" (?<ChildSeat>{0}\d)(?: |$)", seat.SeatCode)); if (matChild.Success) { seat.SeatCode = matChild.Groups["ChildSeat"].Value; childSeatClass = seat.SeatCode; // 子舱位 } ////出发时间及到达时间 this.ComposeTime(matVoyage.Groups["MONTH"].Value, matVoyage.Groups["DAY"].Value, matVoyage.Groups["FTIME"].Value, matVoyage.Groups["TTIME"].Value, ref seat); ////状态 try { seat.Status = (SeatStatus)Enum.Parse(typeof(SeatStatus), matVoyage.Groups["STATUS"].Value, true); } catch { seat.Status = SeatStatus.OTHER; } ////出发城市 seat.FromCity = matVoyage.Groups["FCITY"].Value; ////到达城市 seat.ToCity = matVoyage.Groups["TCITY"].Value; try { // 航站楼起始到达 string strBuilding = matVoyage.Groups["BUILD"].Value; List <string> buildList = FormatTerminalBuilding.GetTerminalBuiding(seat.FromCity, seat.ToCity, strBuilding, childSeatClass); if (buildList.Count > 0) { seat.DepartureTerm = buildList[0]; // 出发航站楼 seat.ArrivalTerm = buildList[1]; // 到达航站楼 } } catch { // ignored } seats.Add(seat); } ////行程类型 ////SetTravel(); return(seats); }