Exemplo n.º 1
0
        public static StringBuilder GetHotelQuotasHtml(StringBuilder sb, QuotaStatePlaces hotelQuota, int mergedRows)
        {
            if (sb == null)
            {
                throw new ArgumentNullException("sb");
            }

            var hotelQuotaState = hotelQuota.QuotaState;
            var imgHtml         = String.Format(@"<img alt='{0}' src='{1}' />", hotelQuotaState,
                                                String.Format(@"./styles/images/{0}-hotel.png", hotelQuotaState).ToLower());

            return(sb.AppendFormat(@"<td class='TFS_HotelQuota TFS_Hq_{0}' {1}>{2}</td>", hotelQuotaState,
                                   mergedRows > 1 ? String.Format(@"rowspan='{0}'", mergedRows) : String.Empty,
                                   hotelQuotaState != QuotesStates.None
                    ? (hotelQuotaState == QuotesStates.Small
                        ? String.Format("{0}<div class='TFS_QuotaPlaces'>{1}</div>", imgHtml, hotelQuota.Places)
                        : imgHtml)
                    : @"&nbsp;"));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Проверяет квоту на всю услугу по параметрам
        /// </summary>
        /// <param name="dc">Контекст базы данных</param>
        /// <param name="serviceClass">Класс услуги</param>
        /// <param name="code">Code услуги (для авиаперелетов - Charter, для проживания - ключ отеля)</param>
        /// <param name="subCode1">SubCode1 услуги, для перелета - класс, для проживания - тип номера (Room)</param>
        /// <param name="subCode2">SubCode1 услуги, для перелета - не используется, для проживания - категория номера (RoomCategory)</param>
        /// <param name="partnerKey">Ключ партнера по услуге</param>
        /// <param name="serviceDateFrom">Дата начала услуги</param>
        /// <param name="serviceDateTo">Дата окончания услуги</param>
        /// <param name="agentKey">Ключ агентства</param>
        /// <param name="hash">Хэш кэша</param>
        /// <returns></returns>
        public static QuotaStatePlaces CheckServiceQuota(this MtSearchDbDataContext dc, ServiceClass serviceClass, Int32 code,
                                                         Int32 subCode1, Int32?subCode2, Int32 partnerKey, int?agentKey, DateTime serviceDateFrom, DateTime serviceDateTo,
                                                         out string hash)
        {
            hash = String.Format("{0}_{1}_{2}_{3}", MethodBase.GetCurrentMethod().Name, (int)serviceClass, code, CacheHelper.GetCacheKeyHashed(new[]
            {
                subCode1.ToString(),
                subCode2.ToString(),
                partnerKey.ToString(),
                agentKey.ToString(),
                serviceDateFrom.ToString("yyyy-MM-dd"),
                serviceDateTo.ToString("yyyy-MM-dd")
            }));

            var cacheDependecies = new List <string>();
            QuotaStatePlaces result;

            if ((result = CacheHelper.GetCacheItem <QuotaStatePlaces>(hash)) != null)
            {
                return(result);
            }

            int duration;

            result = new QuotaStatePlaces()
            {
                IsCheckInQuota = false, Places = 0, QuotaState = QuotesStates.Request
            };
            if (serviceClass == ServiceClass.Flight)
            {
                DateTime linkedDate1, linkedDate2;
                if (serviceDateFrom <= serviceDateTo)
                {
                    linkedDate1 = serviceDateFrom;
                    linkedDate2 = serviceDateTo;
                }
                else
                {
                    linkedDate1 = serviceDateTo;
                    linkedDate2 = serviceDateFrom;
                }
                duration = linkedDate2.Subtract(linkedDate1).Days + 1;
            }
            else
            {
                duration = serviceDateTo.Subtract(serviceDateFrom).Days + 1;
            }

            if (serviceClass == ServiceClass.Flight)
            {
                serviceDateTo = serviceDateFrom;
            }

            var quotasStatusesByDays = new Dictionary <DateTime, QuotaStatePlaces>(duration);

            for (var date = serviceDateFrom; date <= serviceDateTo; date = date.AddDays(1))
            {
                string hashOut;
                quotasStatusesByDays.Add(date, dc.CheckServiceQuotaByDay(serviceClass, code, subCode1, subCode2, partnerKey, agentKey, date, duration, date == serviceDateFrom, out hashOut));
                cacheDependecies.Add(hashOut);
            }

            if (quotasStatusesByDays.Values.Any(s => s.QuotaState == QuotesStates.No))
            {
                result.QuotaState = QuotesStates.No;
            }
            else if (quotasStatusesByDays.Values.Any(s => s.QuotaState == QuotesStates.Request))
            {
                result.QuotaState = QuotesStates.Request;
            }
            else if (quotasStatusesByDays.Values.Any(s => s.QuotaState == QuotesStates.Small))
            {
                result.QuotaState = QuotesStates.Small;
            }
            else if (quotasStatusesByDays.Values.Any(s => s.QuotaState == QuotesStates.Availiable))
            {
                result.QuotaState = QuotesStates.Availiable;
            }

            if (result.QuotaState == QuotesStates.Availiable || result.QuotaState == QuotesStates.Small)
            {
                result.Places = quotasStatusesByDays.Values.Min(s => s.Places);
            }

            if (quotasStatusesByDays.Values.Any(s => s.IsCheckInQuota))
            {
                var checkInState = quotasStatusesByDays.Values.Where(s => s.IsCheckInQuota).Select(s => s).SingleOrDefault();
                if (checkInState != null && QuotasExtension.OrderderQuotaStates[checkInState.QuotaState] > QuotasExtension.OrderderQuotaStates[result.QuotaState])
                {
                    result = checkInState;
                }
            }

            CacheHelper.AddCacheData(hash, result, cacheDependecies.ToList(), Globals.Settings.Cache.LongCacheTimeout);
            return(result);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Проверяет квоту на конкретный день по параметрам. Для услуг с продолжительностью этот метод нужно вызвать на каждый день
        /// </summary>
        /// <param name="dc">Контекст базы данных</param>
        /// <param name="serviceClass">Класс услуги</param>
        /// <param name="code">Code услуги (для авиаперелетов - Charter, для проживания - ключ отеля)</param>
        /// <param name="subCode1">SubCode1 услуги, для перелета - класс, для проживания - тип номера (Room)</param>
        /// <param name="subCode2">SubCode1 услуги, для перелета - не используется, для проживания - категория номера (RoomCategory)</param>
        /// <param name="partnerKey">Ключ партнера по услуге</param>
        /// <param name="date">Дата проверки квоты</param>
        /// <param name="duration">Продолжительность услуги (нужна для подбора квот на продолжительность)</param>
        /// <param name="isTheFirstDay">Является ли проверяемая дата первым днем или нет (нужно для подбора квот на заезд)</param>
        /// <param name="agentKey">КЛюч агентства</param>
        /// <param name="hash">Хэш кэша</param>
        /// <returns></returns>
        public static QuotaStatePlaces CheckServiceQuotaByDay(this MtSearchDbDataContext dc, ServiceClass serviceClass, Int32 code,
                                                              Int32 subCode1, Int32?subCode2, Int32 partnerKey, int?agentKey, DateTime date, int duration, bool isTheFirstDay, out string hash)
        {
            QuotaStatePlaces result;

            hash = String.Format("{0}_{1}_{2}_{3}", MethodBase.GetCurrentMethod().Name, (int)serviceClass, code,
                                 CacheHelper.GetCacheKeyHashed(new[]
            {
                subCode1.ToString(),
                subCode2.ToString(),
                partnerKey.ToString(),
                agentKey.ToString(),
                date.ToString("yyyy-MM-dd"),
                duration.ToString(),
                isTheFirstDay.ToString()
            }));

            if ((result = CacheHelper.GetCacheItem <QuotaStatePlaces>(hash)) != null)
            {
                return(result);
            }

            var cacheDependecies = new List <string>();

            result = new QuotaStatePlaces();

            string hashOut;
            var    plainQuotasByDate = dc.GetPlainQuotasObjects(serviceClass, code, subCode1, subCode2, partnerKey, agentKey, date, out hashOut);

            cacheDependecies.Add(hashOut);

            plainQuotasByDate =
                plainQuotasByDate.Where(q => q.Duration.Split(',').Contains(duration.ToString()) || q.Duration == String.Empty)
                .ToList();

            if (!plainQuotasByDate.Any())
            {
                result.QuotaState = QuotesStates.Request;
            }

            var ssQoIds = new List <bool>();

            foreach (var plainQuota in plainQuotasByDate.OrderByDescending(p => p.SsId))
            {
                ////стоит какой-то стоп
                if (plainQuota.SsId != null)
                {
                    //стоит общий стоп
                    if (plainQuota.SsQdId == null)
                    {
                        // стоп только на Allotment, или еще и на Commitment
                        ssQoIds.Add(plainQuota.IsAllotmentAndCommitment);
                    }

                    if (result.QuotaState == QuotesStates.None)
                    {
                        result.QuotaState = QuotesStates.No;
                    }

                    continue;
                }

                //стопа на текущей строке нет, но есть общий стоп, который подходит под эту строку
                //в переменной result.QuotaState у нас и так уже QuotesStates.No, но проставлю еще раз для наглядности
                if (ssQoIds.Any(s => s || plainQuota.Type == QuotaType.Allotment))
                {
                    result.QuotaState = QuotesStates.No;
                    continue;
                }

                //наступил релиз-период
                //todo: сделать настройку в web.config
                if (plainQuota.Release != null && (plainQuota.Date.Subtract(DateTime.Now).Days < plainQuota.Release))
                {
                    if (result.QuotaState == QuotesStates.None || result.QuotaState == QuotesStates.No)
                    {
                        result.QuotaState = QuotesStates.Request;
                    }

                    continue;
                }

                // проверяем квоту на заезд или на период
                if (serviceClass == ServiceClass.Flight ||
                    (serviceClass == ServiceClass.Hotel &&
                     ((plainQuota.CheckInPlaces ?? 0) <= 0 || plainQuota.CheckInPlacesBusy == null)))
                {
                    var state = dc.GetQuotesStateByInt(serviceClass, plainQuota.Places - plainQuota.Busy, plainQuota.Places, out hashOut);
                    cacheDependecies.Add(hashOut);

                    // или на эту дату не было статуса квотирования, либо мы нашли статус на текущую дату и он лучше
                    if (QuotasExtension.OrderderQuotaStates[result.QuotaState] < QuotasExtension.OrderderQuotaStates[state])
                    {
                        result.QuotaState = state;
                        result.Places    += (uint)(plainQuota.Places - plainQuota.Busy);
                    }
                }
                else if (isTheFirstDay)
                {
                    // нашли квоту на заезд
                    var checkInState = dc.GetQuotesStateByInt(serviceClass, plainQuota.Places - plainQuota.Busy, plainQuota.Places, out hashOut);
                    cacheDependecies.Add(hashOut);

                    result.Places         = (uint)(plainQuota.Places - plainQuota.Busy);
                    result.QuotaState     = checkInState;
                    result.IsCheckInQuota = true;
                }
            }

            CacheHelper.AddCacheData(hash, result, cacheDependecies.ToList(), Globals.Settings.Cache.LongCacheTimeout);
            return(result);
        }
Exemplo n.º 4
0
        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);
        }