/// <summary> /// Метод по проверке квот на перелет /// </summary> /// <param name="dc">Контекст БД</param> /// <param name="mainDc">Контекст основной БД</param> /// <param name="charterKey">Ключ перелета</param> /// <param name="cityKeyFrom">Ключ города вылета</param> /// <param name="cityKeyTo">Ключ города прилета</param> /// <param name="charterDay">День вылета в туре</param> /// <param name="partnerKey">Ключ партнера по перелету</param> /// <param name="agentKey">Ключ агентства</param> /// <param name="packetKey">Ключ пакета</param> /// <param name="tourDate">Дата тура</param> /// <param name="linkedDay">День вылета парного перелета</param> /// <param name="findFlight">Признак "подбирать или не подбирать" перелет</param> /// <param name="placesToCheck">Количество мест, которые должны быть в квоте, чтобы она считалась подходящей</param> /// <param name="flightGroups">Группы перелетов (эконом, бизнес, премиум)</param> /// <param name="hash">Хэш кэша</param> /// <param name="aviaQuotaMask">Маска квот на перелет</param> /// <returns></returns> public static Dictionary <int, FlightPlainInfo> CheckCharterQuota(this MtSearchDbDataContext dc, MtMainDbDataContext mainDc, Int32 charterKey, Int32 cityKeyFrom, Int32 cityKeyTo, Int32 charterDay, int partnerKey, int?agentKey, int packetKey, DateTime tourDate, Int32?linkedDay, bool findFlight, QuotesStates aviaQuotaMask, Int32 placesToCheck, IDictionary <int, IEnumerable <int> > flightGroups, out string hash) { Dictionary <int, FlightPlainInfo> result; hash = String.Format("{0}_{1}_{2}", MethodBase.GetCurrentMethod().Name, charterKey, CacheHelper.GetCacheKeyHashed(new[] { cityKeyFrom.ToString(), cityKeyTo.ToString(), partnerKey.ToString(), agentKey.ToString(), charterDay.ToString(), packetKey.ToString(), tourDate.ToString("yyyy-MM-dd"), linkedDay.ToString(), findFlight.ToString(), aviaQuotaMask.ToString(), placesToCheck.ToString(), String.Join("_", flightGroups.Keys), String.Join("_", flightGroups.Values) })); if ((result = CacheHelper.GetCacheItem <Dictionary <int, FlightPlainInfo> >(hash)) != null) { return(result); } var cacheDependencies = new List <string>(); string hashOut; var flightsInfo = dc.GetCharterAllQuota(mainDc, charterKey, cityKeyFrom, cityKeyTo, charterDay, partnerKey, agentKey, packetKey, tourDate, linkedDay, findFlight, flightGroups, out hashOut); result = new Dictionary <int, FlightPlainInfo>(); foreach (var key in flightsInfo.Keys) { var quotaIsOk = false; var tempState = new FlightPlainInfo(); foreach (var flightInfo in flightsInfo[key]) { var quotaState = flightInfo.QuotaState; var maskIsOk = ((quotaState.QuotaState == QuotesStates.Small && (aviaQuotaMask & QuotesStates.Availiable) == QuotesStates.Availiable) || (quotaState.QuotaState & aviaQuotaMask) == quotaState.QuotaState); if (!maskIsOk) { continue; } if ((quotaState.QuotaState == QuotesStates.Availiable || quotaState.QuotaState == QuotesStates.Small) && quotaState.Places < placesToCheck && (aviaQuotaMask & QuotesStates.Request) != QuotesStates.Request) { continue; } if (QuotasExtension.OrderderQuotaStates[flightInfo.QuotaState.QuotaState] > QuotasExtension.OrderderQuotaStates[tempState.QuotaState.QuotaState] || (tempState.QuotaState.QuotaState == flightInfo.QuotaState.QuotaState && flightInfo.QuotaState.Places > tempState.QuotaState.Places)) { tempState = flightInfo; quotaIsOk = true; } if (tempState.QuotaState.QuotaState == QuotesStates.Availiable) { break; } } if (quotaIsOk) { result.Add(key, new FlightPlainInfo(tempState)); } } CacheHelper.AddCacheData(hash, result, cacheDependencies.ToList(), Globals.Settings.Cache.LongCacheTimeout); return(result); }
public static ActualizedTour ActualizeTour(this MtSearchDbDataContext searchDc, MtMainDbDataContext mainDc, SftWebDbDataContext sftDc, long offerId, int currencyId, out string hash) { ActualizedTour result; hash = String.Format("{0}_{1}_{2}", MethodBase.GetCurrentMethod().Name, offerId, currencyId); if ((result = CacheHelper.GetCacheItem <ActualizedTour>(hash)) != null) { return(result); } string hashOut; var searchResult = searchDc.GetSearchResult(mainDc, sftDc, offerId, currencyId, out hashOut); var tour = searchDc.ConvertSearchItemsToTours(mainDc, searchResult.SearchItems, currencyId); if (tour.Count == 0) { throw new ApplicationException(MethodBase.GetCurrentMethod().Name + ". Данных по цене в БД не найдено. Параметры: " + String.Format("{0}_{1}", offerId, currencyId)); } string rateCode = searchDc.GetRateCodeByKey(currencyId); var cacheDependencies = new List <string> { hashOut }; result = new ActualizedTour { FewBusinessTicketsDpt = tour[0].FewTicketsDptB, FewBusinessTicketsRtn = tour[0].FewTicketsRtnB, FewEconomTicketsDpt = tour[0].FewTicketsDptY, FewEconomTicketsRtn = tour[0].FewTicketsRtnY, FewPlacesInHotel = tour[0].FewPlacesInHotel, TicketsIsIncluded = tour[0].TicketsIncluded, HasBusinessTicketsDpt = tour[0].HasBusinessTicketsDpt, HasBusinessTicketsRtn = tour[0].HasBusinessTicketsRtn, HasEconomTicketsDpt = tour[0].HasEconomTicketsDpt, HasEconomTicketsRtn = tour[0].HasEconomTicketsRtn, TourUrl = tour[0].TourUrl, Price = tour[0].Price }; var parameters = new List <MtServiceParams>(); var commandBuilder = new StringBuilder(); commandBuilder.AppendLine("select distinct ts_key, ts_svkey, ts_name, ts_day, ts_days, ts_attribute, ts_code, ts_subcode1, ts_subcode2, ts_ctkey, ts_men, ts_oppacketkey, ts_oppartnerkey "); commandBuilder.AppendLine("from tp_services "); commandBuilder.AppendLine(String.Format("where ts_Key in (select tl_tskey from tp_servicelists where tl_tikey in (select tp_tikey from tp_prices where tp_key = {0})) ", tour[0].OfferId)); commandBuilder.AppendLine("order by ts_day asc "); using (var command = mainDc.Connection.CreateCommand()) { command.CommandText = commandBuilder.ToString(); mainDc.Connection.Open(); using (var reader = command.ExecuteReader()) { while (reader.Read()) { var pars = new MtServiceParams() { Code = reader.GetInt32("ts_code"), CtKey = reader.GetInt32("ts_ctkey"), Day = reader.GetInt16("ts_day"), Days = reader.GetInt16("ts_days"), Men = reader.GetInt16("ts_men"), PkKey = reader.GetInt32("ts_oppacketkey"), PrKey = reader.GetInt32("ts_oppartnerkey"), SubCode1 = reader.GetInt32("ts_subcode1"), SubCode2 = reader.GetInt32("ts_subcode2"), SvKey = reader.GetInt32("ts_svkey"), Key = reader.GetInt32("ts_key"), Name = reader.GetString("ts_name"), Attribute = reader.GetInt32("ts_attribute") }; parameters.Add(pars); } } mainDc.Connection.Close(); } foreach (var pars in parameters) { var type = Converters.ServiceClassToServiceType(pars.SvKey, pars.Day); if (type == ServiceType.Undefined) { continue; } var service = new Service { Id = pars.Key, Description = String.Empty, Name = pars.Name, IsIncluded = (pars.Attribute & (int)ServiceAttribute.NotCalculate) != (int)ServiceAttribute.NotCalculate, Type = type }; if (!service.IsIncluded) { var brutto = mainDc.GetServiceCost(pars.SvKey, pars.Code, pars.SubCode1, pars.SubCode2, pars.PrKey, pars.PkKey, searchResult.SearchItems[0].Date.AddDays(pars.Day - 1), pars.Days, rateCode, pars.Men, out hashOut); if (brutto.HasValue) { service.Surcharge = (int)brutto; } else { service.Surcharge = 0; } cacheDependencies.Add(hashOut); } FlightPlainInfo flightInfo = null; QuotaStatePlaces quotaStatePlaces = null; // услуга - перелет туда и в поиске у нас есть о нем информация if (type == ServiceType.DptTransport && pars.SubCode2 == Globals.Settings.HomeCityKey && searchResult.SearchItems[0].FlightCalcInfo.CharterKey.HasValue) { service.FlightClass = Converters.CharterClassToFlightClass(pars.SubCode1); switch (service.FlightClass) { case FlightClass.Econom: quotaStatePlaces = searchResult.SearchItems[0].DirectFlightsInfo[0].QuotaState; break; case FlightClass.Business: quotaStatePlaces = searchResult.SearchItems[0].DirectFlightsInfo[1].QuotaState; break; } if (quotaStatePlaces != null) { service.FlightAvailability = Converters.QuotaStateToQuotaAvailability(quotaStatePlaces.QuotaState); if (quotaStatePlaces.QuotaState == QuotesStates.Small) { service.FlightPlacesCount = (int)quotaStatePlaces.Places; } } if (searchResult.SearchItems[0].DirectFlightsInfo.Count > 0) { service.FlightStartDateTime = searchResult.SearchItems[0].DirectFlightsInfo[0].FlightDateTimeFrom; service.FlightEndDateTime = searchResult.SearchItems[0].DirectFlightsInfo[0].FlightDateTimeTo; } flightInfo = searchResult.SearchItems[0].DirectFlightsInfo[0]; } else if (type == ServiceType.RtnTransport && pars.CtKey == Globals.Settings.HomeCityKey && searchResult.SearchItems[0].FlightCalcInfo.BackCharterKey.HasValue) { service.FlightClass = Converters.CharterClassToFlightClass(pars.SubCode1); switch (service.FlightClass) { case FlightClass.Econom: quotaStatePlaces = searchResult.SearchItems[0].BackFlightsInfo[0].QuotaState; break; case FlightClass.Business: quotaStatePlaces = searchResult.SearchItems[0].BackFlightsInfo[1].QuotaState; break; } if (searchResult.SearchItems[0].BackFlightsInfo.Count > 0) { service.FlightStartDateTime = searchResult.SearchItems[0].BackFlightsInfo[0].FlightDateTimeFrom; service.FlightEndDateTime = searchResult.SearchItems[0].BackFlightsInfo[0].FlightDateTimeTo; } flightInfo = searchResult.SearchItems[0].BackFlightsInfo[0]; } if (quotaStatePlaces != null) { service.FlightAvailability = Converters.QuotaStateToQuotaAvailability(quotaStatePlaces.QuotaState); if (quotaStatePlaces.QuotaState == QuotesStates.Small) { service.FlightPlacesCount = (int)quotaStatePlaces.Places; } } if (flightInfo != null) { service.FlightAirportFrom = flightInfo.AirportFrom; service.FlightAirportTo = flightInfo.AirportTo; service.FlightNum = flightInfo.AirlineCode + " " + flightInfo.FlightNumber; service.FlightAirline = flightInfo.AirlineCode; service.FlightAircraft = flightInfo.AircraftCode; } result.Services.Add(service); } foreach (var service in result.Services) { int compId = 0; switch (service.Type) { case ServiceType.DptTransport: { var service1 = service; compId = result.Services.Where(s => s.Type == ServiceType.RtnTransport && s.FlightAirportFrom == service1.FlightAirportTo && s.FlightAirportTo == service1.FlightAirportFrom) .Select(s => s.Id) .SingleOrDefault(); } break; case ServiceType.RtnTransport: { var service1 = service; compId = result.Services.Where(s => s.Type == ServiceType.DptTransport && s.FlightAirportFrom == service1.FlightAirportTo && s.FlightAirportTo == service1.FlightAirportFrom) .Select(s => s.Id) .SingleOrDefault(); } break; } service.FlightCompatibleIds = compId != 0 ? compId.ToString() : String.Empty; } CacheHelper.AddCacheData(hash, result, cacheDependencies.ToList(), Globals.Settings.Cache.LongCacheTimeout); return(result); }