2014-01-20 26 views
0

我正在努力在NopCommerce中创建一个自定义任务,列出当天销售量最高的产品并在管理面板中显示此数据。 E g列出了所有已售出的产品并按销售数量排列。在NopCommerce中添加自定义任务,返回每日畅销排行榜

Iv'e找到了一些运行BestSeller报告的代码,这个代码非常棒,所有这些方法都很大,我不确定哪些部分实际上需要显示畅销书列表。也为畅销书的方法是一个“虚拟的IList和执行方法是无效的

这是畅销书代码:

class MostSoldProductsTask : ITask 
    { 


     private readonly IRepository<Order> _orderRepository; 
     private readonly IRepository<OrderProductVariant> _opvRepository; 
     private readonly IRepository<Product> _productRepository; 
     private readonly IRepository<ProductVariant> _productVariantRepository; 

     private readonly IDateTimeHelper _dateTimeHelper; 
     private readonly IProductService _productService; 





     public virtual IList<BestsellersReportLine> BestSellersReport(DateTime? startTime, 
      DateTime? endTime, OrderStatus? os, PaymentStatus? ps, ShippingStatus? ss, 
      int billingCountryId = 0, 
      int recordsToReturn = 5, int orderBy = 1, int groupBy = 1, bool showHidden = false) 
     { 
      int? orderStatusId = null; 
      if (os.HasValue) 
       orderStatusId = (int)os.Value; 

      int? paymentStatusId = null; 
      if (ps.HasValue) 
       paymentStatusId = (int)ps.Value; 

      int? shippingStatusId = null; 
      if (ss.HasValue) 
       shippingStatusId = (int)ss.Value; 


      var query1 = from opv in _opvRepository.Table 
         join o in _orderRepository.Table on opv.OrderId equals o.Id 
         join pv in _productVariantRepository.Table on opv.ProductVariantId equals pv.Id 
         join p in _productRepository.Table on pv.ProductId equals p.Id 
         where (!startTime.HasValue || startTime.Value <= o.CreatedOnUtc) && 
         (!endTime.HasValue || endTime.Value >= o.CreatedOnUtc) && 
         (!orderStatusId.HasValue || orderStatusId == o.OrderStatusId) && 
         (!paymentStatusId.HasValue || paymentStatusId == o.PaymentStatusId) && 
         (!shippingStatusId.HasValue || shippingStatusId == o.ShippingStatusId) && 
         (!o.Deleted) && 
         (!p.Deleted) && 
         (!pv.Deleted) && 
         (billingCountryId == 0 || o.BillingAddress.CountryId == billingCountryId) && 
         (showHidden || p.Published) && 
         (showHidden || pv.Published) 
         select opv; 

      var query2 = groupBy == 1 ? 
       //group by product variants 
       from opv in query1 
       group opv by opv.ProductVariantId into g 
       select new 
       { 
        EntityId = g.Key, 
        TotalAmount = g.Sum(x => x.PriceExclTax), 
        TotalQuantity = g.Sum(x => x.Quantity), 
       } 
       : 
       //group by products 
       from opv in query1 
       group opv by opv.ProductVariant.ProductId into g 
       select new 
       { 
        EntityId = g.Key, 
        TotalAmount = g.Sum(x => x.PriceExclTax), 
        TotalQuantity = g.Sum(x => x.Quantity), 
       } 
       ; 

      switch (orderBy) 
      { 
       case 1: 
        { 
         query2 = query2.OrderByDescending(x => x.TotalQuantity); 
        } 
        break; 
       case 2: 
        { 
         query2 = query2.OrderByDescending(x => x.TotalAmount); 
        } 
        break; 
       default: 
        throw new ArgumentException("Wrong orderBy parameter", "orderBy"); 
      } 

      if (recordsToReturn != 0 && recordsToReturn != int.MaxValue) 
       query2 = query2.Take(recordsToReturn); 

      var result = query2.ToList().Select(x => 
      { 
       var reportLine = new BestsellersReportLine() 
       { 
        EntityId = x.EntityId, 
        TotalAmount = x.TotalAmount, 
        TotalQuantity = x.TotalQuantity 
       }; 
       return reportLine; 
      }).ToList(); 

      return result; 
     } 



     public void Execute() 
     { 


      throw new NotImplementedException("Return something"); 
     } 

我也有通过返回此列表‘执行’ - 方法是正在实现ITask接口,我最好的猜测是有一种方法创建畅销书列表,并拥有“Execute方法实现第一个并返回它。

谢谢

回答

3

我相信,如果ITask界面,你误解的目的。 ITask用于运行一些背景,非UI任务,所以你永远不能让它在管理面板中“返回”列表。

你想要做的是定期运行它并将数据保存在某个自定义表中。然后使用它

+0

谢谢。不完全确定我将如何去实现这个目标 – koffe14