/// <summary> /// 类别与商品集合 /// </summary> /// <returns></returns> public CategoryAndCommodityModel GetCategoryAndCommodity(double?discount = null) { if (discount.HasValue) { hasDiscount = true; memberDiscount = discount.Value; } var categoryTree = GetCategoryTree(); var nodes = categoryTree.Tree; List <TreeNode> resultNodes = new List <TreeNode>(); List <GetCommoditysModel> commodities = new List <GetCommoditysModel>(); foreach (TreeNode node in nodes) { GetCommoditysModel commodity = GetCommodity(node); if (commodity.Items.Count > 0) { commodities.Add(commodity); resultNodes.Add(node); } } commodities.Reverse(); resultNodes.Reverse(); return(new CategoryAndCommodityModel() { Commodities = commodities, Nodes = resultNodes }); }
public GetCommoditysModel GetCommodity(TreeNode node) { string categoryFlag = node.Flag; bool isFb = categoryFlag == "fb"; //火爆分类 var query = db.Query <ShopCommodityStock>() .Where(m => !m.IsDel) .Where(m => m.Shop.Flag == mShop.Flag); if (isFb) //火爆分类 { query = query.Where(m => m.Sku.Commodity.CategoryId != 0); } else if (!string.IsNullOrEmpty(categoryFlag)) { query = query.Where(m => m.Sku.Commodity.Category.Flag == categoryFlag); } //这里获得指定店铺的库存及价格及关联的Sku List <StockRowItem> stockItems; if (hasDiscount) { stockItems = query .Select(m => new StockRowItem { SkuId = m.Sku.Id, SkuFlag = m.Sku.Flag, HasVipPrice = m.Sku.Commodity.UseMemberPrice, CommodityId = m.Sku.CommodityId, PrimePrice = m.SalePrice, SalePrice = m.Sku.Commodity.UseMemberPrice ? GetMemberPrice(m.SalePrice) : m.SalePrice, Stock = m.Stock }) .ToList(); } else { stockItems = query .Select(m => new StockRowItem { SkuId = m.Sku.Id, SkuFlag = m.Sku.Flag, CommodityId = m.Sku.CommodityId, SalePrice = m.SalePrice, HasVipPrice = false, PrimePrice = m.SalePrice, Stock = m.Stock }) .ToList(); } var viewModel = new GetCommoditysModel(); //获取库存关联的商铺信息,主要是一些描述 var commodityIds = stockItems.Select(m => m.CommodityId).Distinct().ToList(); var commodities = db.Query <ShopBrandCommodity>().Where(m => !m.IsDel) .Where(m => commodityIds.Contains(m.Id)); switch (diningWay) { case DiningWay.自助: commodities = commodities.Where(m => m.IsSelfOrder); break; case DiningWay.堂食: commodities = commodities.Where(m => m.IsScanCode); break; case DiningWay.外卖: commodities = commodities.Where(m => m.IsTakeout); break; default: break; } if (isFb) { commodities = commodities.OrderByDescending(m => m.SalesForMonth).Take(10); } viewModel.Items = commodities.Select(m => new CommodityRowItem() { Id = m.Id, Cover = m.Cover, Detail = m.Detail, Name = m.Name, SalesForMonth = m.SalesForMonth, Upvote = m.Upvote }).ToList(); //获取库存关联的规格的规格项 var skuIds = stockItems.Select(m => m.SkuId).Distinct().ToList(); var skuItems = db.Query <ShopBrandCommoditySkuItem>() .Where(m => !m.IsDel) .Where(m => skuIds.Contains(m.SkuId)) .OrderByDescending(m => m.Id) .Select(m => new SkuItem() { Id = m.Id, SkuId = m.SkuId, Value = m.ParameterValue.Value, ParameterValueId = m.ParameterValueId, ParameterId = m.ParameterId, ParameterName = m.Parameter.Name }) .ToList(); //循环每一个商品 foreach (var item in viewModel.Items) { //获得商品的sku id列表,用于下面获取商品的skuItems var commoditySkuIds = stockItems.Where(m => m.CommodityId == item.Id).Select(m => m.SkuId).Distinct().ToList(); // var stock = stockItems.Where(m => m.CommodityId == item.Id).First(); item.Skus = skuItems.Where(m => commoditySkuIds.Contains(m.SkuId)) .GroupBy(m => m.SkuId) .Select(m => new CommoditySku(m.Key, m.ToList(), stockItems.Where(x => x.CommodityId == item.Id && x.SkuId == m.Key).FirstOrDefault())) .OrderBy(m => m.SalePrice) .ToList(); item.Parameters = skuItems.Where(m => commoditySkuIds.Contains(m.SkuId)).GroupBy(m => m.ParameterId).Select(m => new CommodityParameter(m.ToList())).ToList(); } return(viewModel); }