在NopCommerce中添加自定义任务,返回每日畅销书列表

时间:2014-01-20 14:50:12

标签: c# .net asp.net-mvc nopcommerce

我正在努力在NopCommerce中创建一个自定义任务,列出当天销售的大部分产品,并在管理面板中显示此数据。 E g列出已售出的所有产品,并按销售数量订购。

我发现一些运行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”方法返回此列表。我最好的猜测是让一个方法创建畅销书列表,并拥有“执行第一个并执行第一个的Execute方法。”

谢谢

1 个答案:

答案 0 :(得分:3)

我相信你误解了ITask接口的目的。 ITask用于运行一些后台非UI任务,因此您永远无法让它在管理面板中“返回”该列表。

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