|
3 | 3 | using AppBoot.DependencyInjection; |
4 | 4 | using Contracts.Sales; |
5 | 5 | using DataAccess; |
| 6 | +using Microsoft.Extensions.Logging; |
6 | 7 | using Sales.DataModel.SalesLT; |
| 8 | +using Sales.DataModel.Values; |
7 | 9 |
|
8 | 10 | namespace Sales.Services; |
9 | 11 |
|
10 | 12 | [Service(typeof(ICustomerService))] |
11 | | -class CustomerService(IRepository repository) : ICustomerService |
| 13 | +class CustomerService(IRepository repository, ILogger<CustomerService> logger) : ICustomerService |
12 | 14 | { |
13 | 15 | public CustomerData[] GetCustomersWithOrders() |
14 | 16 | { |
@@ -52,4 +54,32 @@ public CustomerData[] GetCustomersWithOrdersContaining(string fragment) |
52 | 54 |
|
53 | 55 | return GetCustomersWithOrdersFilteredBy(filter); |
54 | 56 | } |
| 57 | + |
| 58 | + public CustomerOverdueOrdersData[] GetCustomersWithOverdueOrders() |
| 59 | + { |
| 60 | + logger.LogInformation("Retrieving customers with overdue orders"); |
| 61 | + |
| 62 | + var today = DateTime.Today; |
| 63 | + var closedStatuses = new[] { SalesOrderHeaderStatusValues.Shipped, SalesOrderHeaderStatusValues.Cancelled }; |
| 64 | + |
| 65 | + var results = repository.GetEntities<SalesOrderHeader>() |
| 66 | + .Where(o => o.DueDate < today && !closedStatuses.Contains(o.Status)) |
| 67 | + .GroupBy(o => o.Customer) |
| 68 | + .Select(g => new CustomerOverdueOrdersData |
| 69 | + { |
| 70 | + CustomerName = !string.IsNullOrWhiteSpace(g.Key.CompanyName) |
| 71 | + ? g.Key.CompanyName |
| 72 | + : !string.IsNullOrWhiteSpace(g.Key.FirstName) || !string.IsNullOrWhiteSpace(g.Key.LastName) |
| 73 | + ? $"{g.Key.FirstName} {g.Key.LastName}".Trim() |
| 74 | + : $"Customer {g.Key.CustomerID}", |
| 75 | + OverdueOrderCount = g.Count(), |
| 76 | + OldestOverdueOrderDate = g.Min(o => o.DueDate) |
| 77 | + }) |
| 78 | + .OrderBy(c => c.OldestOverdueOrderDate) |
| 79 | + .ToArray(); |
| 80 | + |
| 81 | + logger.LogDebug("Found {Count} customers with overdue orders", results.Length); |
| 82 | + |
| 83 | + return results; |
| 84 | + } |
55 | 85 | } |
0 commit comments