public IQueryable<ProductEntity> GetProductsByCondition(ProductSearchCondition condition) { var query = _productRepository.Table; try { if (condition.PriceBegin.HasValue) { query = query.Where(q => q.Price >= condition.PriceBegin.Value); } if (condition.PriceEnd.HasValue) { query = query.Where(q => q.Price < condition.PriceEnd.Value); } if (condition.StockBegin.HasValue) { query = query.Where(q => q.Stock >= condition.StockBegin.Value); } if (condition.StockEnd.HasValue) { query = query.Where(q => q.Stock < condition.StockEnd.Value); } if (condition.AddtimeBegin.HasValue) { query = query.Where(q => q.AddTime >= condition.AddtimeBegin.Value); } if (condition.AddtimeEnd.HasValue) { query = query.Where(q => q.AddTime < condition.AddtimeEnd.Value); } if (condition.UpdTimeBegin.HasValue) { query = query.Where(q => q.UpdTime >= condition.UpdTimeBegin.Value); } if (condition.UpdTimeEnd.HasValue) { query = query.Where(q => q.UpdTime < condition.UpdTimeEnd.Value); } if (condition.Status.HasValue) { query = query.Where(q => q.Status == condition.Status.Value); } if (condition.Adduser.HasValue) { query = query.Where(q => q.AddUser == condition.Adduser.Value); } if (condition.UpdUser.HasValue) { query = query.Where(q => q.UpdUser == condition.UpdUser.Value); } if (condition.Type.HasValue) { query = query.Where(q => q.Type == condition.Type.Value); } if (!string.IsNullOrEmpty(condition.Name)) { query = query.Where(q => q.Name.Contains(condition.Name)); } if (condition.IsRecommend.HasValue) { query = query.Where(q => q.IsRecommend == condition.IsRecommend); } if (!string.IsNullOrEmpty(condition.Subtitte)) { query = query.Where(q => q.Subtitte.Contains(condition.Subtitte)); } if (condition.Ids != null && condition.Ids.Any()) { query = query.Where(q => condition.Ids.Contains(q.Id)); } if (condition.Categorys != null )//传进来的是二级 查他下面的三级所有商品 { query = query.Where(q =>q.Category.Father.Id==condition.Categorys.Id); } if (condition.CategoryId.HasValue && condition.CategoryId!=0)// 传进来的是3级 { query = query.Where(q => q.Category.Id == condition.CategoryId); } if (!string.IsNullOrEmpty(condition.CategoryName))// 传进来的是分类名称 { query = query.Where(q => q.Category.Name==condition.CategoryName); } if (condition.OrderBy.HasValue) { switch (condition.OrderBy.Value) { case EnumProductSearchOrderBy.OrderById: query = condition.IsDescending ? query.OrderByDescending(q => q.Id) : query.OrderBy(q => q.Id); break; case EnumProductSearchOrderBy.OrderByPrice: query = condition.IsDescending ? query.OrderByDescending(q => q.Price) : query.OrderBy(q => q.Price); break; case EnumProductSearchOrderBy.OrderBySort: query = condition.IsDescending ? query.OrderByDescending(q => q.Sort) : query.OrderBy(q => q.Sort); break; case EnumProductSearchOrderBy.OrderByAddtime: query = condition.IsDescending ? query.OrderByDescending(q => q.AddTime) : query.OrderBy(q => q.AddTime); break; case EnumProductSearchOrderBy.OrderByOwner: query = condition.IsDescending ? query.OrderByDescending(q => q.Owner) : query.OrderBy(q => q.Owner); break; } } else { query = query.OrderBy(q => q.Id); } if (condition.Page.HasValue && condition.PageCount.HasValue) { query = query.Skip((condition.Page.Value - 1) * condition.PageCount.Value).Take(condition.PageCount.Value); } return query; } catch (Exception e) { _log.Error(e, "数据库操作出错"); return null; } }
/// <summary> /// 获取6个商品 /// </summary> /// <param name="id">分类id</param> /// <returns>商品列表</returns> public List <Product> GetSixPro(int id) { var con = new ProductSearchCondition { Page =1, PageCount =6, // Name = condition.Name, IsDescending = true, // OrderBy = condition.OrderBy, // PriceBegin = condition.PriceBegin, //PriceEnd = condition.PriceEnd, // CategoryName = condition.CategoryName, CategoryId=id }; List<Product> data = new List<Product>(); var model = _productService.GetProductsByCondition(con).Select(c => new ProductModel { Id = c.Id, Name = c.Name, MainImg = c.MainImg, }).ToList(); foreach (var pro in model) { Product produce = new Product { Id = pro.Id, Name =pro.Name , MainImg =pro.MainImg }; data.Add(produce); } return data; }
public int GetProductCount(ProductSearchCondition condition) { var query = _productRepository.Table; try { if (condition.PriceBegin.HasValue) { query = query.Where(q => q.Price >= condition.PriceBegin.Value); } if (condition.PriceEnd.HasValue) { query = query.Where(q => q.Price < condition.PriceEnd.Value); } if (condition.StockBegin.HasValue) { query = query.Where(q => q.Stock >= condition.StockBegin.Value); } if (condition.StockEnd.HasValue) { query = query.Where(q => q.Stock < condition.StockEnd.Value); } if (condition.AddtimeBegin.HasValue) { query = query.Where(q => q.AddTime >= condition.AddtimeBegin.Value); } if (condition.AddtimeEnd.HasValue) { query = query.Where(q => q.AddTime < condition.AddtimeEnd.Value); } if (condition.UpdTimeBegin.HasValue) { query = query.Where(q => q.UpdTime >= condition.UpdTimeBegin.Value); } if (condition.UpdTimeEnd.HasValue) { query = query.Where(q => q.UpdTime < condition.UpdTimeEnd.Value); } if (condition.Status.HasValue) { query = query.Where(q => q.Status == condition.Status.Value); } if (condition.Adduser.HasValue) { query = query.Where(q => q.AddUser == condition.Adduser.Value); } if (condition.UpdUser.HasValue) { query = query.Where(q => q.UpdUser == condition.UpdUser.Value); } if (condition.Type.HasValue) { query = query.Where(q => q.Type == condition.Type.Value); } if (!string.IsNullOrEmpty(condition.Name)) { query = query.Where(q => q.Name.Contains(condition.Name)); } if (condition.IsRecommend.HasValue) { query = query.Where(q => q.IsRecommend == condition.IsRecommend.Value); } if (!string.IsNullOrEmpty(condition.Subtitte)) { query = query.Where(q => q.Subtitte.Contains(condition.Subtitte)); } if (condition.Ids != null && condition.Ids.Any()) { query = query.Where(q => condition.Ids.Contains(q.Id)); } if (condition.Categorys != null)//传进来的是二级 查他下面的三级所有商品 { query = query.Where(q => q.Category.Father.Id == condition.Categorys.Id); } if (condition.CategoryId.HasValue && condition.CategoryId != 0)// 传进来的是3级 { query = query.Where(q => q.Category.Id == condition.CategoryId); } if (!string.IsNullOrEmpty(condition.CategoryName))// 传进来的是分类名称 { query = query.Where(q=>q.Category.Name ==condition.CategoryName); } return query.Count(); } catch (Exception e) { _log.Error(e, "数据库操作出错"); return -1; } }
/// <summary> /// 获取商品列表 /// </summary> /// <param name="condition">查询条件</param> /// <returns>商品列表</returns> public HttpResponseMessage Get([FromUri]ProductSearchCondition condition) { var con = new ProductSearchCondition { Page = condition.Page, PageCount = condition.PageCount, Name = condition.Name, IsDescending = condition.IsDescending, OrderBy = condition.OrderBy, PriceBegin = condition.PriceBegin, PriceEnd = condition.PriceEnd, CategoryName=condition.CategoryName }; if (condition.CategoryId!=0 && condition.CategoryId!=null) { var category = _categoryService.GetCategoryById(Convert.ToInt32(condition.CategoryId)); if (category.Father.Father == null)//判断是否是二级 { //var firstOrDefault = _categoryService.GetCategorysBySuperFather(category.Id).FirstOrDefault(); //if (firstOrDefault != null) // con.CategoryId = firstOrDefault.Id; con.Categorys = category;//_categoryService.GetCategorysBySuperFather(category.Id).ToArray(); } else//3级 { con.CategoryId = condition.CategoryId; } } var model = _productService.GetProductsByCondition(con).Select(c => new ProductModel { Id = c.Id, CategoryId = c.Category.Id, BussnessId = c.BussnessId, BussnessName = c.BussnessName, Price = c.Price, Name = c.Name, Status = c.Status, MainImg = c.MainImg, IsRecommend = c.IsRecommend, Sort = c.Sort, Stock = c.Stock, Subtitte = c.Subtitte, Contactphone = c.Contactphone, Type = c.Type, OldPrice = c.OldPrice, Owner =c.Owner, Addtime = c.AddTime, Detail = c.Detail.Detail, Ad1 = c.Detail.Ad1 // Comments = c.Comments, // Parameters = c.Parameters, //ParameterValue =c.Parameters.Select(p => new ProductParameterValueModel //{ // ParameterId = p.Parameter.Id, // ParameterString = p.Parameter.Name, // ValueId = p.ParameterValue.Id, // Value = p.ParameterValue.Value //}).ToList() }).ToList(); var totalCount = _productService.GetProductCount(con); return PageHelper.toJson(new { List = model, Condition = con, TotalCount = totalCount }); }