Example #1
0
        /// <summary>
        /// Данные по квотам на перелеты
        /// </summary>
        /// <param name="dc">Контекст БД</param>
        /// <param name="mainDc">Контекст БД</param>
        /// <param name="query">Параметры запроса</param>
        /// <param name="hash">Строка кэша</param>
        /// <returns></returns>
        public static QuotaPriceResult GetFlightQuotaPrices(this MtSearchDbDataContext dc, MtMainDbDataContext mainDc, QuotaPriceQuery query, out string hash)
        {
            QuotaPriceResult result;

            hash = String.Format("{0}_{1}", MethodBase.GetCurrentMethod().Name, query);
            if ((result = CacheHelper.GetCacheItem <QuotaPriceResult>(hash)) != null)
            {
                return(result);
            }

            // определяем агента по логину / паролю пользователя
            int?agentKey = null;

            if (!string.IsNullOrEmpty(query.UserName))
            {
                var user = mainDc.GetDupUser(query.UserName, query.UserPassword);
                if (user != null)
                {
                    agentKey = user.US_PRKEY;
                }
            }

            var cacheDependencies = new List <string>();

            result = new QuotaPriceResult();

            foreach (var flightPars in query.FlightParams)
            {
                string hashOut;
                var    cost = dc.GetFlightPriceByParams(flightPars.FlightParamKeys.CharterKey,
                                                        flightPars.FlightParamKeys.CharterClassKey, flightPars.FlightParamKeys.PartnerKey,
                                                        flightPars.FlightParamKeys.PacketKey, flightPars.DepartTime.Date,
                                                        flightPars.Duration != null
                                        ? flightPars.Duration.Value
                                        : 1,
                                                        out hashOut);
                if (!cacheDependencies.Contains(hashOut))
                {
                    cacheDependencies.Add(hashOut);
                }

                if (cost != null && cost.Price != null)
                {
                    var quotaState = dc.CheckServiceQuotaByDay(ServiceClass.Flight,
                                                               flightPars.FlightParamKeys.CharterKey,
                                                               flightPars.FlightParamKeys.CharterClassKey,
                                                               null,
                                                               flightPars.FlightParamKeys.PartnerKey,
                                                               agentKey,
                                                               flightPars.DepartTime.Date,
                                                               flightPars.Duration != null
                            ? flightPars.Duration.Value
                            : 1,
                                                               true,
                                                               out hashOut);

                    if (!cacheDependencies.Contains(hashOut))
                    {
                        cacheDependencies.Add(hashOut);
                    }

                    result.FlightData.Add(new QuotaPriceData
                    {
                        QuotaState  = quotaState.QuotaState,
                        QuotaPlaces = quotaState.Places,
                        PriceValue  = cost
                    });
                }
            }

            CacheHelper.AddCacheData(hash, result, cacheDependencies.ToList(), Globals.Settings.Cache.LongCacheTimeout);
            return(result);
        }
Example #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);
        }