public async Task <IActionResult> Search(ShopSearchModel model) { // Default price and max price. var defaultMinPrice = double.Parse(_configuration["Default:MinPrice"]); var defaultMaxPrice = double.Parse(_configuration["Default:MaxPrice"]); // Get all the default colors to show on the page. var defaultColors = await _mediator.Send(new SearchProductAllColorsCommand()); // Create command for executing searhcing for products. var command = new SearchProductCommand( model.MinPrice, model.MaxPrice, model.Colors); // Get the result of Search. var result = await _mediator.Send(command); // Apply sorting out from the selected sorting. if (model.SortBy == ShopSearchModel.Sorting.Highest) { result = result.OrderByDescending(x => x.Inventory.Price).ToList(); } else if (model.SortBy == ShopSearchModel.Sorting.Lowest) { result = result.OrderBy(x => x.Inventory.Price).ToList(); } // Prepare view model. var viewModel = new ShopSearchViewModel() { DefaultColors = defaultColors.ToList(), DefaultMinPrice = defaultMinPrice, DefaultMaxPrice = defaultMaxPrice, Colors = model.Colors ?? defaultColors.Select(x => x.Id).ToList(), MaxPrice = model.MaxPrice, MinPrice = model.MinPrice, Products = result.Select(x => new ShopSearchProductViewModel() { Name = x.Name, Price = x.Inventory.Price, Picture = x.Picture, Id = x.Id, Color = x.Color }), SortBy = model.SortBy }; return(View("Index", viewModel)); }
public async Task <IActionResult> Index() { // Default price and max price. var defaultMinPrice = double.Parse(_configuration["Default:MinPrice"]); var defaultMaxPrice = double.Parse(_configuration["Default:MaxPrice"]); // Get all the default colors to show on the page. var defaultColors = await _mediator.Send(new SearchProductAllColorsCommand()); // Create command for getting all products. var command = new SearchProductCommand( defaultMinPrice, defaultMaxPrice, defaultColors.Select(x => x.Id).ToList()); // Execute and search for produts. var result = await _mediator.Send(command); // Order the result by lowest price. result = result.OrderBy(x => x.Inventory.Price).ToList(); // Prepare view model for display. var viewModel = new ShopSearchViewModel() { DefaultColors = defaultColors.ToList(), DefaultMinPrice = defaultMinPrice, DefaultMaxPrice = defaultMaxPrice, Colors = defaultColors.Select(x => x.Id).ToList(), MaxPrice = defaultMaxPrice, MinPrice = defaultMinPrice, Products = result.Select(x => new ShopSearchProductViewModel() { Name = x.Name, Price = x.Inventory.Price, Picture = x.Picture, Id = x.Id, Color = x.Color }) }; return(View("Index", viewModel)); }
public ActionResult Index(string keywords = "", long categoryId = 0, long brandId = 0, int orderBy = 0, int pageNo = 1, int pageSize = 40) { var query = new ShopQuery { ShopName = keywords, CategoryId = categoryId, BrandId = brandId, PageNo = pageNo, PageSize = pageSize, Status = Entities.ShopInfo.ShopAuditStatus.Open }; if (orderBy == 1) { query.Sort = "OrderCount"; } var result = new ShopSearchViewModel { Keywords = keywords, CategoryId = categoryId, BrandId = brandId, Shops = new List <Shop>(), OrderBy = orderBy, IsSaleCountOnOff = SiteSettingApplication.SiteSettings.ProductSaleCountOnOff == 1 }; var shopData = _iShopService.GetShops(query); var shops = shopData.Models; if (shopData.Total == 0) { return(View(result)); } result.Pager = new PagingInfo { CurrentPage = pageNo, ItemsPerPage = pageSize, TotalItems = shopData.Total }; result.Total = shopData.Total; var statisticList = ShopApplication.GetStatisticOrderComment(shops.Select(p => p.Id).ToList()); var productQuery = new ProductQuery { PageNo = 1, PageSize = 4, OnSale = true, IsAsc = false, Sort = "displaysale", }; foreach (var item in shops) { var shop = new Shop(); shop.Id = item.Id; shop.ShopName = item.ShopName; //销量 shop.SaleCount = (int)ShopApplication.GetShopDisplaySales(item.Id); var statistic = statisticList.FirstOrDefault(p => p.ShopId == item.Id); //宝贝与描述 shop.ProductAndDescription = string.Format("{00:F}", statistic.ProductAndDescription); //卖家服务态度 shop.SellerServiceAttitude = string.Format("{00:F}", statistic.SellerServiceAttitude); //卖家发货速度 shop.SellerDeliverySpeed = string.Format("{00:F}", statistic.SellerDeliverySpeed); //查询商品 productQuery.ShopId = item.Id; var products = ProductManagerApplication.GetProducts(productQuery); shop.ProductCount = products.Total; shop.Products = products.Models.Select(p => new Product { Id = p.Id, Image = MallIO.GetProductSizeImage(p.ImagePath, 1, (int)ImageSize.Size_150), Price = p.MinSalePrice, CategoryId = p.CategoryId, }).ToList(); shop.Logo = item.Logo; result.Shops.Add(shop); } //经营品牌 var shopBrands = ShopApplication.GetShopBrands(shops.Select(p => p.Id).ToList()); result.Brands = shopBrands.GroupBy(p => p.BrandId).ToDictionary(k => k.Key, v => v.FirstOrDefault().BrandName); //经营类型 var CategoryIds = result.Shops.SelectMany(s => s.Products.Select(p => p.CategoryId)); result.Categorys = CategoryApplication.GetCategories() .Where(p => CategoryIds.Contains(p.Id)) .ToDictionary(k => k.Id, v => v.Name); return(View(result)); }