//		public ServiceOrderDetailModel Get(int id)
//		{
//			var entity =_serviceOrderDetailService.GetServiceOrderDetailById(id);
//			var model = new ServiceOrderDetailModel
//			{
//				Id = entity.Id,
////                ServiceOrder = entity.ServiceOrder,
////                Product = entity.Product,
//                Count = entity.Count,
//                Price = entity.Price,
//            };
//			return model;
//		}

        public HttpResponseMessage Get([FromUri] ServiceOrderDetailSearchCondition condition)
        {
            var model = _serviceOrderDetailService.GetServiceOrderDetailsByCondition(condition).Select(c => new
            {
                Id = c.Id,
//				ServiceOrder = c.ServiceOrder,
                //Product = c.Product,
                ProductName  = c.Product.Name,
                MainImg      = c.Product.MainImg,
                Count        = c.Count,
                Price        = c.Price,
                OrderNo      = c.ServiceOrder.OrderNo,
                proparameter = c.Product.Parameters.Select(o => o.Parameter.Name).FirstOrDefault(),
                propValue    = c.Product.Parameters.Select(o => o.ParameterValue.Value).FirstOrDefault(),
            }).ToList();
            var count = _serviceOrderDetailService.GetServiceOrderDetailCount(condition);

            return(PageHelper.toJson(new { List = model, TotalCount = count }));
        }
        public int GetServiceOrderDetailCount(ServiceOrderDetailSearchCondition condition)
        {
            var query = _serviceorderdetailRepository.Table;

            try
            {
                if (condition.CountBegin.HasValue)
                {
                    query = query.Where(q => q.Count >= condition.CountBegin.Value);
                }
                if (condition.CountEnd.HasValue)
                {
                    query = query.Where(q => q.Count < condition.CountEnd.Value);
                }
                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.Ids != null && condition.Ids.Any())
                {
                    query = query.Where(q => condition.Ids.Contains(q.Id));
                }
                if (condition.ServiceOrders != null && condition.ServiceOrders.Any())
                {
                    query = query.Where(q => condition.ServiceOrders.Contains(q.ServiceOrder));
                }
                if (condition.Products != null && condition.Products.Any())
                {
                    query = query.Where(q => condition.Products.Contains(q.Product));
                }
                return(query.Count());
            }
            catch (Exception e)
            {
                _log.Error(e, "数据库操作出错");
                return(-1);
            }
        }
        public IQueryable <ServiceOrderDetailEntity> GetServiceOrderDetailsByCondition(ServiceOrderDetailSearchCondition condition)
        {
            var query = _serviceorderdetailRepository.Table;

            try
            {
                if (condition.CountBegin.HasValue)
                {
                    query = query.Where(q => q.Count >= condition.CountBegin.Value);
                }
                if (condition.CountEnd.HasValue)
                {
                    query = query.Where(q => q.Count < condition.CountEnd.Value);
                }
                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.Status.HasValue)
                {
                    query = query.Where(q => q.ServiceOrder.Status == condition.Status.Value);
                }
                if (condition.Ids != null && condition.Ids.Any())
                {
                    query = query.Where(q => condition.Ids.Contains(q.Id));
                }
                if (condition.ServiceOrders != null && condition.ServiceOrders.Any())
                {
                    query = query.Where(q => condition.ServiceOrders.Contains(q.ServiceOrder));
                }
                if (condition.Products != null && condition.Products.Any())
                {
                    query = query.Where(q => condition.Products.Contains(q.Product));
                }
                if (condition.OrderBy.HasValue)
                {
                    switch (condition.OrderBy.Value)
                    {
                    case EnumServiceOrderDetailSearchOrderBy.OrderById:
                        query = condition.IsDescending?query.OrderByDescending(q => q.Id):query.OrderBy(q => q.Id);
                        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);
            }
        }