public ActionResult _SaveMenuAjax(List <CategoryMenuModel> model)
        {
            if (model == null)
            {
                model = new List <CategoryMenuModel>();
            }
            var lookup = model.ToLookup(x => x.Order);

            var sid = LS.CurrentShop.ID;
            var currentShopModel = _db.ShopCategoryMenus.Where(x => x.ShopID == sid).ToList();

            _db.ShopCategoryMenus.RemoveRange(currentShopModel);
            if (model != null)
            {
                _db.Configuration.ValidateOnSaveEnabled = false;
                foreach (var group in lookup)
                {
                    int displayOrder = 0;
                    foreach (var item in group)
                    {
                        var shopMenu = new ShopCategoryMenu()
                        {
                            CategoryID   = item.CategoryID,
                            DisplayOrder = displayOrder,
                            GroupNumber  = group.Key,
                            Published    = item.Published,
                            HeadOfGroup  = item.HeadOfGroup,
                            ShopID       = sid,
                            Level        = 0
                        };
                        if (displayOrder > 0)
                        {
                            shopMenu.Level = 1;
                        }
                        displayOrder++;
                        _db.ShopCategoryMenus.Add(shopMenu);
                    }
                }
                _db.SaveChanges();
            }
            return(Json(lookup));
        }
        public ActionResult MakeCategoryMenuItems()
        {
            var category1 = LS.Get <Category>()
                            //.Where(x => x.ParentCategoryID == 0)
                            .ToList();
            var shopCatMap = LS.Get <ShopCategory>().Where(x => x.Published);

            foreach (var shop in LS.Get <Shop>())
            {
                var shopID   = shop.ID;
                var category = (from c in category1
                                join sc in shopCatMap
                                on c.ID equals sc.CategoryID
                                where sc.ShopID == shopID &&
                                c.ParentCategoryID == 0
                                orderby sc.DisplayOrder
                                select c
                                ).Distinct().Take(11).ToList();

                var IDs     = category.Select(x => x.ID).ToList();
                var subcats =
                    (from c in category1
                     join sc in shopCatMap
                     on c.ID equals sc.CategoryID
                     where sc.ShopID == shopID &&
                     IDs.Contains(c.ParentCategoryID)
                     orderby sc.DisplayOrder
                     select c
                    ).ToList();
                // LS.Get<Category>().Where(x => IDs.Contains(x.ParentCategoryID)).OrderBy(x => x.DisplayOrder).ToList();
                category.AddRange(subcats);
                //process list
                var currentShopModel = _db.ShopCategoryMenus.Where(x => x.ShopID == shopID).ToList();
                _db.ShopCategoryMenus.RemoveRange(currentShopModel);
                _db.SaveChanges();
                int groupNum = 0;
                foreach (var c in category.Where(x => x.ParentCategoryID == 0))
                {
                    int displayOrder = 0;
                    var shopMenu     = new ShopCategoryMenu()
                    {
                        CategoryID   = c.ID,
                        DisplayOrder = displayOrder,
                        GroupNumber  = groupNum,
                        Published    = c.Published,
                        ShopID       = shopID,
                        Level        = 0
                    };
                    _db.ShopCategoryMenus.Add(shopMenu);
                    displayOrder++;
                    foreach (var sub in category.Where(x => x.ParentCategoryID == c.ID))
                    {
                        var shopsubMenu = new ShopCategoryMenu()
                        {
                            CategoryID   = sub.ID,
                            DisplayOrder = displayOrder,
                            GroupNumber  = groupNum,
                            Published    = sub.Published,
                            ShopID       = shopID,
                            Level        = 1
                        };
                        displayOrder++;
                        _db.ShopCategoryMenus.Add(shopsubMenu);
                    }

                    groupNum++;
                }
                _db.SaveChanges();
            }
            return(Content("Coppy categories"));
        }
        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) }));
        }