diff --git a/PIbd-23_Gutorov_I.A._IT-Company/Entities/Contract.cs b/PIbd-23_Gutorov_I.A._IT-Company/Entities/Contract.cs index 4d6d45f..60c26ae 100644 --- a/PIbd-23_Gutorov_I.A._IT-Company/Entities/Contract.cs +++ b/PIbd-23_Gutorov_I.A._IT-Company/Entities/Contract.cs @@ -14,7 +14,7 @@ public class Contract public int ExecutorID { get; private set; } [DisplayName("Заказчик")] - public int CustomerName { get; private set; } + public string CustomerName { get; private set; } = string.Empty; [Browsable(false)] public ExecutorPost ExecutorPost { get; private set; } @@ -60,21 +60,6 @@ public class Contract }; } - public static Contract CreateEntity(TempServiceContract tempServiceContract, IEnumerable services) - { - return new Contract - { - Id = tempServiceContract.Id, - CustomerID = tempServiceContract.CustomerID, - ExecutorID = tempServiceContract.ExecutorID, - Category = tempServiceContract.Category, - ConclusionDate = tempServiceContract.ConclusionDate, - Deadline = tempServiceContract.Deadline, - PaymentAmount = tempServiceContract.PaymentAmount, - Services = services - }; - } - public void SetServices(IEnumerable services) { if (services != null && services.Any()) diff --git a/PIbd-23_Gutorov_I.A._IT-Company/Entities/ServiceContract.cs b/PIbd-23_Gutorov_I.A._IT-Company/Entities/ServiceContract.cs index 4a4c36d..83d9a1f 100644 --- a/PIbd-23_Gutorov_I.A._IT-Company/Entities/ServiceContract.cs +++ b/PIbd-23_Gutorov_I.A._IT-Company/Entities/ServiceContract.cs @@ -12,11 +12,8 @@ public class ServiceContract [Browsable(false)] public int ContractId { get; private set; } - [DisplayName("Заказчик")] - public int CustomerName { get; private set; } - - [DisplayName("Исполнитель")] - public int ExecutorName { get; private set; } + [DisplayName("Описание услуги")] + public string ServiceDescription { get; private set; } = string.Empty; public static ServiceContract CreateElement(int id, int serviceId, int contractId) { diff --git a/PIbd-23_Gutorov_I.A._IT-Company/Reports/ChartReport.cs b/PIbd-23_Gutorov_I.A._IT-Company/Reports/ChartReport.cs index c3f808b..d350597 100644 --- a/PIbd-23_Gutorov_I.A._IT-Company/Reports/ChartReport.cs +++ b/PIbd-23_Gutorov_I.A._IT-Company/Reports/ChartReport.cs @@ -39,7 +39,7 @@ internal class ChartReport { return _contractRepository .ReadContracts(dateFrom: dateTime.Date, dateTo: dateTime.Date.AddDays(1)) - .GroupBy(x => x.CustomerID, (key, group) => new + .GroupBy(x => x.CustomerName, (key, group) => new { Id = key, Count = group.Sum(x => x.Services.Count()) diff --git a/PIbd-23_Gutorov_I.A._IT-Company/Reports/TableReport.cs b/PIbd-23_Gutorov_I.A._IT-Company/Reports/TableReport.cs index 8c288da..2217019 100644 --- a/PIbd-23_Gutorov_I.A._IT-Company/Reports/TableReport.cs +++ b/PIbd-23_Gutorov_I.A._IT-Company/Reports/TableReport.cs @@ -46,21 +46,18 @@ internal class TableReport .GroupBy(x => x.ConclusionDate) .Select(x => new { - x.First().ExecutorID, + x.First().ExecutorFullName, Date = x.First().ConclusionDate, CountIn = (int?)x.Count(), CountOut = (int?)null }) .Union( _contractRepository - .ReadContracts() - .Where(x => x.Deadline >= startDate - && x.Deadline <= endDate - && x.CustomerID == customerId) + .ReadContracts(customerId: customerId, dateFrom: startDate, dateTo: endDate) .GroupBy(x => x.Deadline) .Select(x => new { - x.First().ExecutorID, + x.First().ExecutorFullName, Date = x.First().Deadline, CountIn = (int?)null, CountOut = (int?)x.Count() @@ -71,7 +68,7 @@ internal class TableReport return new List() { item } .Union(data.Select(x => new string[] { - x.ExecutorID.ToString(), + x.ExecutorFullName, x.Date.ToString(), x.CountIn?.ToString() ?? string.Empty, x.CountOut?.ToString() ?? string.Empty diff --git a/PIbd-23_Gutorov_I.A._IT-Company/Repositories/Implementations/ContractRepository.cs b/PIbd-23_Gutorov_I.A._IT-Company/Repositories/Implementations/ContractRepository.cs index 354bd73..dc34316 100644 --- a/PIbd-23_Gutorov_I.A._IT-Company/Repositories/Implementations/ContractRepository.cs +++ b/PIbd-23_Gutorov_I.A._IT-Company/Repositories/Implementations/ContractRepository.cs @@ -101,10 +101,12 @@ public class ContractRepository : IContractRepository using var connection = new NpgsqlConnection(_connectionString.ConnectionString); var querySelect = @$" - SELECT c.*, sc.ServiceId, s.Description, + SELECT c.*, CONCAT(cu.Name, ' ', cu.Contact) as CustomerName, e.Post as ExecutorPost, - e.Name as ExecutorName + e.Name as ExecutorName, + sc.ServiceId, + s.Description as ServiceDescription FROM Contracts c INNER JOIN ServiceContract sc ON sc.ContractId = c.Id LEFT JOIN Services s ON sc.ServiceId = s.Id @@ -112,9 +114,35 @@ public class ContractRepository : IContractRepository LEFT JOIN Executors e ON e.Id = c.ExecutorId {builder.Build()}; "; - var serviceDict = new Dictionary>(); - var contracts = connection.Query(querySelect, new { dateFrom, dateTo, customerId, executorId }); + + var contractsDict = new Dictionary>(); + + var contracts = connection.Query(querySelect, (contract, serviceContract) => + { + if (!contractsDict.TryGetValue(contract.Id, out var sc)) + { + sc = []; + contractsDict.Add(contract.Id, sc); + } + + sc.Add(serviceContract); + return contract; + }, splitOn: "ServiceId", param: new + { + dateFrom, + dateTo, + customerId, + executorId + }); _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(contracts)); + + return contractsDict.Select(x => + { + var c = contracts.First(y => y.Id == x.Key); + c.SetServices(x.Value.Select(x => Service.CreateEntity(x.ServiceId, x.ServiceDescription))); + return c; + }).ToArray(); + } catch (Exception ex) { _logger.LogError(ex, "Ошибка при чтении объектов");