Example #1
0
        public ShowPackageProductPrices LoadAllPackageAndProduct(int categoryId, string countryCode, bool useCache)
        {
            ShowPackageProductPrices showPackageProductPrices = null;
            var cache = DataCache.Cache;
            string cacheKey = "SGPP:Cat:" + categoryId.ToString() + ";C:" + countryCode;

            if (useCache)
            {
                //var cacheItem = DataCache.Cache.GetCacheItem(cacheKey);
                showPackageProductPrices = (ShowPackageProductPrices)cache[cacheKey];
            }

            if (showPackageProductPrices == null)
            {
                //create helper class for displaying content to the View
                List<ShowAlacarteProduct> showAlarcarteProducts = new List<ShowAlacarteProduct>();

                //create helper class for displaying content to the View
                List<ShowPackageGroupProduct> showPackageGroupProductList = new List<ShowPackageGroupProduct>();

                showPackageProductPrices = new ShowPackageProductPrices();
                var context = new IPTV2Entities();
                var offering = context.Offerings.Find(GlobalConfig.offeringId);
                string currencyCode = MyUtility.GetCurrencyOrDefault(countryCode);
                var show = (Show)context.CategoryClasses.Find(categoryId);

                var packageProductIds = show.GetPackageProductIds(offering, countryCode, RightsType.Online);
                var showProductIds = show.GetShowProductIds(offering, countryCode, RightsType.Online);



                var subscriptionProductC = SubscriptionProductC.LoadAll(context, offering.OfferingId)
                                               .Where(p => p.IsAllowed(countryCode));


                if (showProductIds != null)
                {
                    var showProducts = from product in subscriptionProductC
                                       join id in showProductIds
                                       on product.ProductId equals id
                                       where product.IsForSale
                                       select product;


                    //show product with alacarte subscription
                    var aLaCarteProducts = from showproducts in showProducts
                                           join subcription in show.Products
                                           on showproducts.ProductId equals subcription.ProductId
                                           select new
                                           {
                                               ALaCarteSubscriptionTypeId = subcription.Product.ALaCarteSubscriptionTypeId,
                                               ProductPrices = showproducts.ProductPrices,
                                               Duration = showproducts.Duration,
                                               DurationInDays = showproducts.DurationInDays,
                                               DurationType = showproducts.DurationType,
                                               ProductId = showproducts.ProductId,
                                               CategoryId = subcription.CategoryId
                                           };


                    foreach (var item in aLaCarteProducts)
                    {
                        showAlarcarteProducts.Add(new ShowAlacarteProduct
                        {
                            ALaCarteSubscriptionTypeId = item.ALaCarteSubscriptionTypeId,
                            Duration = item.Duration,
                            DurationInDays = item.DurationInDays,
                            DurationType = item.DurationType,
                            CurrencyCode = currencyCode,
                            ProductId = item.ProductId,
                            CategoryId = item.CategoryId,
                            ProductPrices = item.ProductPrices.Where(p => p.CurrencyCode == currencyCode).ToList()
                        });
                    }

                }




                if (packageProductIds != null)
                {
                    //join the packageproduct to get the packageID 
                    var packageProducts = from product in subscriptionProductC
                                          join id in packageProductIds
                                          on product.ProductId equals id
                                          where product.IsForSale
                                          select product;


                    var packageGroupProducts = from product in packageProducts
                                               group product by product.ProductGroupId into ProductGroup
                                               orderby ProductGroup.Key
                                               select new
                                               {
                                                   GroupId = ProductGroup.Key,
                                                   Products =
                                                   (
                                                       from product2 in ProductGroup
                                                       orderby product2.DurationInDays descending
                                                       select new Product2
                                                       {
                                                           ProductId = product2.ProductId,
                                                           Description = product2.Description,
                                                           Duration = product2.Duration,
                                                           DurationInDays = product2.DurationInDays,
                                                           DurationType = product2.DurationType,
                                                           ProductPrice = product2.ProductPrices.FirstOrDefault(p => p.CurrencyCode == currencyCode),
                                                           ProductGroupId = product2.ProductGroupId
                                                       }
                                                   )
                                               };

                    foreach (var item in packageGroupProducts)
                    {
                        showPackageGroupProductList.Add(new ShowPackageGroupProduct
                        {
                            ProductGroupId = item.GroupId,
                            Product2 = item.Products.ToList()
                        });

                    }

                }

                //Upgradable Products

                showPackageProductPrices.ShowAlacarteProductList = showAlarcarteProducts;
                showPackageProductPrices.ShowPackageGroupProductList = showPackageGroupProductList;

                if (useCache)
                {
                    //cache.Put(cacheKey, showPackageProductPrices, DataCache.CacheDuration);
                    var cacheDuration = new TimeSpan(0, GlobalConfig.PackageAndProductCacheDuration, 0);
                    cache.Put(cacheKey, showPackageProductPrices, cacheDuration);
                }
            }

            return showPackageProductPrices;
        }
        public void FillCacheOfAllPackageAndProduct(IPTV2Entities context, int offeringId, string DefaultCurrency, RightsType rightsType, TimeSpan cacheDuration)
        {
            var categories = context.CategoryClasses.Where(c => c.StatusId == 1 && c is Show).ToList();
            var countries = context.Countries.ToList();
            var offering = context.Offerings.Find(offeringId);

            foreach (var category in categories)
            {
                var categoryId = category.CategoryId;
                foreach (var country in countries)
                {
                    try
                    {
                        var countryCode = country.Code;
                        string cacheKey = "SGPP:Cat:" + categoryId.ToString() + ";C:" + countryCode;
                        //create helper class for displaying content to the View
                        List<ShowAlacarteProduct> showAlarcarteProducts = new List<ShowAlacarteProduct>();

                        //create helper class for displaying content to the View
                        List<ShowPackageGroupProduct> showPackageGroupProductList = new List<ShowPackageGroupProduct>();

                        ShowPackageProductPrices showPackageProductPrices = new ShowPackageProductPrices();

                        string currencyCode = PackageCacheRefresher.GetCurrencyOrDefault(countryCode, DefaultCurrency);
                        var show = (Show)category;

                        var packageProductIds = show.GetPackageProductIds(offering, countryCode, RightsType.Online);
                        var showProductIds = show.GetShowProductIds(offering, countryCode, RightsType.Online);

                        var subscriptionProductC = SubscriptionProductC.LoadAll(context, offering.OfferingId)
                                                       .Where(p => p.IsAllowed(countryCode));


                        string AlaCarteCacheKey = "SALACARTEPRD:Cat:" + categoryId.ToString() + ";C:" + countryCode;
                        var cacheItem = DataCache.Cache.GetCacheItem(AlaCarteCacheKey);
                        if (cacheItem != null)
                        {
                            showAlarcarteProducts = (List<ShowAlacarteProduct>)DataCache.Cache[AlaCarteCacheKey];
                        }
                        else
                        {
                            if (showProductIds != null)
                            {
                                var showProducts = from product in subscriptionProductC
                                                   join id in showProductIds
                                                   on product.ProductId equals id
                                                   where product.IsForSale
                                                   select product;


                                //show product with alacarte subscription
                                var aLaCarteProducts = from showproducts in showProducts
                                                       join subcription in show.Products
                                                       on showproducts.ProductId equals subcription.ProductId
                                                       select new
                                                       {
                                                           ALaCarteSubscriptionTypeId = subcription.Product.ALaCarteSubscriptionTypeId,
                                                           ProductPrices = showproducts.ProductPrices,
                                                           Duration = showproducts.Duration,
                                                           DurationInDays = showproducts.DurationInDays,
                                                           DurationType = showproducts.DurationType,
                                                           ProductId = showproducts.ProductId,
                                                           CategoryId = subcription.CategoryId
                                                       };


                                foreach (var item in aLaCarteProducts)
                                {
                                    showAlarcarteProducts.Add(new ShowAlacarteProduct
                                    {
                                        ALaCarteSubscriptionTypeId = item.ALaCarteSubscriptionTypeId,
                                        Duration = item.Duration,
                                        DurationInDays = item.DurationInDays,
                                        DurationType = item.DurationType,
                                        CurrencyCode = currencyCode,
                                        ProductId = item.ProductId,
                                        CategoryId = item.CategoryId,
                                        ProductPrices = item.ProductPrices.Where(p => p.CurrencyCode == currencyCode).ToList()
                                    });
                                }
                            }
                        }

                        string PackageCacheKey = "SPKGPRD:Cat:" + categoryId.ToString() + ";C:" + countryCode;
                        var PackageCacheItem = DataCache.Cache.GetCacheItem(PackageCacheKey);
                        if (PackageCacheItem != null)
                        {
                            showPackageGroupProductList = (List<ShowPackageGroupProduct>)DataCache.Cache[PackageCacheKey];
                        }
                        else
                        {
                            if (packageProductIds != null)
                            {
                                //join the packageproduct to get the packageID 
                                var packageProducts = from product in subscriptionProductC
                                                      join id in packageProductIds
                                                      on product.ProductId equals id
                                                      where product.IsForSale
                                                      select product;

                                var packageGroupProducts = from product in packageProducts
                                                           group product by product.ProductGroupId into ProductGroup
                                                           orderby ProductGroup.Key
                                                           select new
                                                           {
                                                               GroupId = ProductGroup.Key,
                                                               Products =
                                                               (
                                                                   from product2 in ProductGroup
                                                                   orderby product2.DurationInDays descending
                                                                   select new Product2
                                                                   {
                                                                       ProductId = product2.ProductId,
                                                                       Description = product2.Description,
                                                                       Duration = product2.Duration,
                                                                       DurationInDays = product2.DurationInDays,
                                                                       DurationType = product2.DurationType,
                                                                       ProductPrice = product2.ProductPrices.FirstOrDefault(p => p.CurrencyCode == currencyCode)
                                                                   }
                                                               )
                                                           };

                                foreach (var item in packageGroupProducts)
                                {
                                    showPackageGroupProductList.Add(new ShowPackageGroupProduct
                                    {
                                        ProductGroupId = item.GroupId,
                                        Product2 = item.Products.ToList()
                                    });
                                }
                            }
                        }
                        //Upgradable Products
                        showPackageProductPrices.ShowAlacarteProductList = showAlarcarteProducts;
                        showPackageProductPrices.ShowPackageGroupProductList = showPackageGroupProductList;
                        DataCache.Cache.Put(cacheKey, showPackageProductPrices, cacheDuration);
                    }
                    catch (Exception e) { Trace.TraceInformation(e.Message); }
                }
            }
        }