diff --git a/services/ordering/src/EShopOnAbp.OrderingService.Application/Orders/OrderAppService.cs b/services/ordering/src/EShopOnAbp.OrderingService.Application/Orders/OrderAppService.cs index 63b84cb2..93bcc00c 100644 --- a/services/ordering/src/EShopOnAbp.OrderingService.Application/Orders/OrderAppService.cs +++ b/services/ordering/src/EShopOnAbp.OrderingService.Application/Orders/OrderAppService.cs @@ -76,21 +76,21 @@ public class OrderAppService : ApplicationService, IOrderAppService private async Task> GetTopSellingAsync(string filter) { ISpecification specification = SpecificationFactory.Create(filter); - var orderItems = await _orderRepository.GetTopSelling(specification, true); - return ObjectMapper.Map, List>(orderItems); + var orderItems = await _orderRepository.GetDashboardAsync(specification); + return CreateTopSellingDtoMapping(orderItems); } private async Task> GetPercentOfTotalPaymentAsync(string filter) { ISpecification specification = SpecificationFactory.Create(filter); - var orders = await _orderRepository.GetPercentOfTotalPayment(specification); + var orders = await _orderRepository.GetDashboardAsync(specification, false); return CreatePaymentDtoMapping(orders); } private async Task> GetCountOfTotalOrderStatusAsync(string filter) { ISpecification specification = SpecificationFactory.Create(filter); - var orders = await _orderRepository.GetCountOfTotalOrderStatus(specification, true); + var orders = await _orderRepository.GetDashboardAsync(specification); return CreateOrderStatusDtoMapping(orders); } @@ -200,11 +200,24 @@ public class OrderAppService : ApplicationService, IOrderAppService return payments; } + private List CreateTopSellingDtoMapping(List orders) + { + var orderItems = orders.Select(p => p.OrderItems).SelectMany(p => p).ToList(); + + var topSetlling = orderItems + .GroupBy(p => p.ProductId) + .Select(p => new TopSellingDto { Units = p.Count(), ProductName = p.First().ProductName , PictureUrl = p.First().PictureUrl }) + .OrderByDescending(p => p.Units) + .Take(OrderConstants.Top10).ToList(); + + return topSetlling; + } + private List CreateOrderStatusDtoMapping(List orders) { var orderStatus = orders .GroupBy(p => p.OrderStatus) - .Select(p => new OrderStatusDto { CountOfStatusOrder = p.Count(), OrderStatus = p.Key.ToString(), }) + .Select(p => new OrderStatusDto { CountOfStatusOrder = p.Count(), OrderStatus = p.Key.ToString() }) .OrderBy(p => p.CountOfStatusOrder) .ToList(); diff --git a/services/ordering/src/EShopOnAbp.OrderingService.Domain/Orders/IOrderRepository.cs b/services/ordering/src/EShopOnAbp.OrderingService.Domain/Orders/IOrderRepository.cs index 0e953790..97e3fd77 100644 --- a/services/ordering/src/EShopOnAbp.OrderingService.Domain/Orders/IOrderRepository.cs +++ b/services/ordering/src/EShopOnAbp.OrderingService.Domain/Orders/IOrderRepository.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Threading; using System.Threading.Tasks; using Volo.Abp.Domain.Repositories; @@ -20,16 +21,7 @@ public interface IOrderRepository : IRepository bool includeDetails = true, CancellationToken cancellationToken = default); - Task> GetTopSelling( - ISpecification spec, - bool includeDetails = true, - CancellationToken cancellationToken = default); - - Task> GetPercentOfTotalPayment( - ISpecification spec, - CancellationToken cancellationToken = default); - - Task> GetCountOfTotalOrderStatus( + Task> GetDashboardAsync( ISpecification spec, bool includeDetails = true, CancellationToken cancellationToken = default); diff --git a/services/ordering/src/EShopOnAbp.OrderingService.EntityFrameworkCore/Orders/EfCoreOrderRepository.cs b/services/ordering/src/EShopOnAbp.OrderingService.EntityFrameworkCore/Orders/EfCoreOrderRepository.cs index b717a372..f84e01d2 100644 --- a/services/ordering/src/EShopOnAbp.OrderingService.EntityFrameworkCore/Orders/EfCoreOrderRepository.cs +++ b/services/ordering/src/EShopOnAbp.OrderingService.EntityFrameworkCore/Orders/EfCoreOrderRepository.cs @@ -45,35 +45,8 @@ public class EfCoreOrderRepository : EfCoreRepository o.OrderDate) .ToListAsync(GetCancellationToken(cancellationToken)); } - - public async Task> GetTopSelling( - ISpecification spec, - bool includeDetails = true, - CancellationToken cancellationToken = default) - { - return (await (await GetDbSetAsync()) - .IncludeDetails(includeDetails) - .Where(spec.ToExpression()) - .SelectMany(oi => oi.OrderItems) - .GroupBy(p => p.ProductCode) - .OrderByDescending(o => o.Sum(p => p.Units)) - .Select(o => o.ToList()) - .ToListAsync()) - .SelectMany(t => t) - .Take(OrderConstants.Top10) - .ToList(); - } - - public async Task> GetPercentOfTotalPayment( - ISpecification spec, - CancellationToken cancellationToken = default) - { - return await (await GetDbSetAsync()) - .Where(spec.ToExpression()) - .ToListAsync(GetCancellationToken(cancellationToken)); - } - - public async Task> GetCountOfTotalOrderStatus( + + public async Task> GetDashboardAsync( ISpecification spec, bool includeDetails = true, CancellationToken cancellationToken = default)