public ActionResult _AjaxIndex([DataSourceRequest] DataSourceRequest request) { IQueryable <Category> items = _db.Categories.AsQueryable(); DataSourceResult result = items.ToDataSourceResult(request); var allCategories = LS.Get <Category>(); foreach (var c in (IEnumerable <Category>)result.Data) { if (c.ParentCategoryID > 0) { var pc = allCategories.FirstOrDefault(x => x.ID == c.ParentCategoryID); if (pc != null) { c.ParentCategory = pc; } } //cached product count c.CachedProductCount = LS.GetCachedFunc(() => { var cid = c.ID; return(_db.Products.Where(x => x.Deleted == false && x.CategoryID == cid).Count()); }, string.Format("productsInCategory-{0}.", c.ID), 60); } return(Json(result)); }
public int GetCurrentLimitPosition(Discount discount, User user, bool cache = true , string userName = null, string Phone = null, string Address = null, string Email = null) { Func <int> getHistoryFunc = () => { if (discount.LimitType == DiscountLimitType.PerAll) { int sum = _db.DiscountUsages .Where(x => x.DiscountID == discount.ID) .Select(x => x.UsedTimes).DefaultIfEmpty(0).Sum(); return(sum); } else if (discount.LimitType == DiscountLimitType.PerCustomer) { var query = _db.DiscountUsages .Where(x => x.DiscountID == discount.ID && x.UserID == user.ID ); if (!string.IsNullOrEmpty(userName) || !string.IsNullOrEmpty(Phone) || !string.IsNullOrEmpty(Address) || !string.IsNullOrEmpty(Email) ) { query = _db.DiscountUsages .Where(x => x.DiscountID == discount.ID && (x.UserID == user.ID || (x.UsageData != null && userName != null && x.UsageData.Contains(":\"" + userName)) || (x.UsageData != null && Phone != null && x.UsageData.Contains(":\"" + Phone)) || (x.UsageData != null && Address != null && x.UsageData.Contains(":\"" + Address)) || (x.UsageData != null && Email != null && x.UsageData.Contains(":\"" + Email)) ) ); } int sum = query .Select(x => x.UsedTimes).DefaultIfEmpty(0).Sum(); return(sum); } return(0); }; if (cache) { string key = "Discount_History_" + discount.ID + "_" + user.ID; return(LS.GetCachedFunc <int>(getHistoryFunc, key, 30)); } return(getHistoryFunc()); }
public static IEnumerable <Shop> GetNearestShop(int shopType, decimal latitude, decimal longitude , string address, bool ignoreDistance = false) { var allShops = LS.Get <Shop>(); var cacheSource = allShops.Where(x => x.ShopTypeIDs != null && (x.IsShipEnabled || x.InStorePickUpEnabled) && x.Active ); var allAdditionalZones = LS.Get <ShopDeliveryZone>(); if (shopType > 0) { cacheSource = cacheSource.Where(x => x.ShopTypeIDs.Contains("," + shopType.ToString() + ",") || x.ShopTypeIDs == shopType.ToString() || x.ShopTypeIDs.StartsWith(shopType.ToString() + ",") || x.ShopTypeIDs.EndsWith("," + shopType.ToString()) ); } var source = cacheSource.Select(x => new DistanceSearchItem() { ID = x.ID, RadiusLatitude = x.RadiusLatitude , RadiusLongitude = x.RadiusLongitude , ShipRadius = x.ShipRadius , ShipCost = x.ShipCost, Rate = x.Rate, DisplayOrder = x.DisplayOrder }).ToList(); source.AddRange( (from az in allAdditionalZones join s in allShops on az.ShopID equals s.ID where az.Active && az.ShopID > 0 select new DistanceSearchItem() { ID = s.ID, RadiusLatitude = az.RadiusLatitude , RadiusLongitude = az.RadiusLongitude , ShipRadius = az.ShipRadius , ShipCost = az.ShipCost, Rate = s.Rate, DisplayOrder = s.DisplayOrder, IsWholeCity = az.DeliveryFroAllCity, City = az.City } ) ); List <IntDoublePair> distances = new List <IntDoublePair>(); double maxRate = 0.1; double maxShip = 0.1; int maxOrders = 0; // 1) Display Order var lowerCaseAddress = address != null?address.ToLower() : ""; foreach (var s in source) { if (s.IsWholeCity) { //check the city in address if (!string.IsNullOrEmpty(s.City) && ( lowerCaseAddress.Contains(", " + s.City.ToLower() + ", ") || lowerCaseAddress.StartsWith(s.City.ToLower() + ", ") // || lowerCaseAddress.EndsWith(", " + s.City.ToLower()) // || lowerCaseAddress.StartsWith(s.City.ToLower() + " ") || lowerCaseAddress.EndsWith(", " + s.City.ToLower()) ) ) { //city founded distances.Add(new IntDoublePair() { Int = s.ID, Double = 1 }); } else if (ignoreDistance) { //not this city distances.Add(new IntDoublePair() { Int = s.ID, Double = 10000 }); } } else if (!ignoreDistance) { var clientradius = distance((double)s.RadiusLatitude, (double)s.RadiusLongitude, (double)latitude, (double)longitude, 'K'); if (clientradius <= (double)s.ShipRadius) { distances.Add(new IntDoublePair() { Int = s.ID, Double = clientradius }); } } else { distances.Add(new IntDoublePair() { Int = s.ID, Double = 10000 }); } if ((double)s.Rate > maxRate) { maxRate = (double)s.Rate; } if ((double)s.ShipCost > maxShip) { maxShip = (double)s.ShipCost; } int ShopID = s.ID; int ordersToday = LS.GetCachedFunc <int>(() => { var curDateOnly = DateTime.Now.Date; var count = LS.CurrentEntityContext.Orders.Count(x => x.ShopID == ShopID && x.CreateOn >= curDateOnly); return(count); }, "shopOrdersToday_" + ShopID.ToString(), 10); //cache 10 min if (ordersToday > maxOrders) { maxOrders = ordersToday; } } double max = 1; if (distances.Count > 0) { max = distances.Select(x => x.Double).Max(); } if (max == 0) { max = 1;//prevent zero division } var curdate = DateTime.Now.Date; var curdateTime = DateTime.Now; var lastdate = curdate.AddDays(7); foreach (var s in source) { var groupDistances = distances.Where(x => x.Int == s.ID).ToList(); if (groupDistances.Count > 0) { groupDistances.ForEach((e) => { e.Double = s.DisplayOrder // 70 % + (max - e.Double) * 10 / max // 10 % + (maxShip - (double)s.ShipCost) * 5 / maxShip // + (double)s.Rate * 10 / maxRate ; int ShopID = s.ID; if (maxOrders > 0) { int ordersToday = LS.GetCachedFunc <int>(() => { var curDateOnly = DateTime.Now.Date; var count = LS.CurrentEntityContext.Orders.Count(x => x.ShopID == ShopID && x.CreateOn >= curDateOnly); return(count); }, "shopOrdersToday_" + ShopID.ToString(), 10); //cache 10 min e.Double += ordersToday * 5 / maxOrders; } //check shop closed var WorkTimes = LS.GetCachedFunc <List <ShopWorkTime> >(() => { return(LS.CurrentEntityContext.ShopWorkTimes.Where(x => x.ShopID == ShopID && x.Active && (!x.IsSpecial || (x.Date >= curdate && x.Date <= lastdate)) ).OrderBy(x => x.IsSpecial).ThenBy(x => x.Day).ThenBy(x => x.Date) .ToList()); }, "shopWorkTime_" + ShopID.ToString(), 60); var time = WorkTimes.FirstOrDefault( x => (x.IsSpecial && x.Date.Date == curdate) || (!x.IsSpecial && x.Day == curdate.DayOfWeek) ); bool validTime = false; if (time != null) { int curIntTime = curdateTime.Hour * 60 + curdateTime.Minute; if (time.TimeFrom <= curIntTime && curIntTime <= time.TimeTo) { validTime = true; } } if (!validTime) { e.Double -= 30; } //end shop closed check }); } } var shopIds = (from s in allShops join d in distances on s.ID equals d.Int orderby d.Double descending select s ); return(shopIds); }
public ActionResult _GetMappedCategoriesAjax() { var allCategories = LS.Get <Category>();//.Select(x => new { x.ID, x.Name, x.ParentCategoryID }) var sid = LS.CurrentShop.ID; var allmapped = (from ps in _db.ProductShopMap join p in _db.Products on ps.ProductID equals p.ID join c in _db.Categories on p.CategoryID equals c.ID where p.Deleted == false && ps.ShopID == sid select c.ID ).ToList(); var preparedMenu = new Dictionary <int, ShopCategoryMenu>(); foreach (var cid in allmapped) { var category = allCategories.FirstOrDefault(x => x.ID == cid); if (category != null) { if (category.ParentCategoryID == 0) { if (!preparedMenu.ContainsKey(cid)) { var prodCount = LS.GetCachedFunc(() => { return(( from ps in _db.ProductShopMap join p in _db.Products on ps.ProductID equals p.ID where ps.ShopID == sid && p.CategoryID == cid && p.Deleted == false select ps.ID).Count()); }, string.Format("productsInCategory-{0}-{1}.", cid, sid), 60); preparedMenu[cid] = new ShopCategoryMenu() { CategoryID = cid, ShopID = sid , CacheProdCount = prodCount , Published = true, Level = cid, GroupNumber = category.DisplayOrder - 100000 }; } } else { var pid = category.ParentCategoryID; if (!preparedMenu.ContainsKey(pid)) { var categoryParent = allCategories.FirstOrDefault(x => x.ID == pid); if (categoryParent == null) { categoryParent = category; } var prodCount = LS.GetCachedFunc(() => { return(( from ps in _db.ProductShopMap join p in _db.Products on ps.ProductID equals p.ID where ps.ShopID == sid && p.CategoryID == pid && p.Deleted == false select ps.ID).Count()); }, string.Format("productsInCategory-{0}-{1}.", pid, sid), 60); preparedMenu[pid] = new ShopCategoryMenu() { CategoryID = pid, ShopID = sid , CacheProdCount = prodCount , Published = true, Level = pid, GroupNumber = categoryParent.DisplayOrder - 100000 }; } if (!preparedMenu.ContainsKey(cid)) { //cached product count var prodCount = LS.GetCachedFunc(() => { return(( from ps in _db.ProductShopMap join p in _db.Products on ps.ProductID equals p.ID where ps.ShopID == sid && p.CategoryID == cid && p.Deleted == false select ps.ID).Count()); }, string.Format("productsInCategory-{0}-{1}.", cid, sid), 60); preparedMenu[cid] = new ShopCategoryMenu() { CategoryID = cid, ShopID = sid , Published = true, Level = pid, CacheProdCount = prodCount, GroupNumber = category.DisplayOrder + 1000000 }; preparedMenu[pid].CacheProdCount += prodCount; } } } } // var currentShopModel = _db.ShopCategoryMenus.Where(x => x.ShopID == sid).ToList(); return(Json(new { preparedMenu = preparedMenu.Select(x => x.Value).OrderBy(x => x.GroupNumber).ThenBy(x => x.Level) })); }