diff --git a/ProjectGSM/Documents/ChartReport.cs b/ProjectGSM/Documents/ChartReport.cs index 394ad4e..95a2c2c 100644 --- a/ProjectGSM/Documents/ChartReport.cs +++ b/ProjectGSM/Documents/ChartReport.cs @@ -47,22 +47,20 @@ public class ChartReport { try { - var advocateEarnings = _caseRepository - .ReadCases() - .Where(x => x.CreatedAt.Date <= dateTime.Date) - .Join(_caseAdvocateRepository.ReadCaseAdvocates().Where(x => x.CreatedAt.Date <= dateTime.Date), - caseItem => caseItem.Id, - caseAdvocate => caseAdvocate.CaseId, - (caseItem, caseAdvocate) => new { caseItem, caseAdvocate }) - .GroupBy(x => x.caseAdvocate.AdvocateId, (key, group) => new - { - AdvocateName = _advocateRepository.ReadAdvocateById(key)?.Name ?? "Unknown", - Earnings = group.Sum(x => x.caseItem.Payment ? (x.caseItem.Verdict ? x.caseItem.VictoryPrice : x.caseItem.Price) : 0) - }) - .Select(x => (x.AdvocateName, (double)x.Earnings)) + return _caseAdvocateRepository + .ReadCaseAdvocates(dateTo: dateTime.Date.AddDays(1)) + .Join(_caseRepository.ReadCases(dateTime.Date.AddDays(1)), + caseItem => caseItem.CaseId, + caseCaseId => caseCaseId.Id, + (caseItem, caseCase) => new + { + caseItem.AdvocateName, caseCase.Payment, caseCase.Verdict, caseCase.Price, caseCase.VictoryPrice + }) + .GroupBy(x => x.AdvocateName) + .Select(g => (g.Key, + Sum: g.Sum(x => + x.Payment ? (x.Verdict ? (double)(x.Price + x.VictoryPrice) : (double)x.Price) : 0))) .ToList(); - - return advocateEarnings; } catch (Exception ex) { diff --git a/ProjectGSM/Documents/TableReport.cs b/ProjectGSM/Documents/TableReport.cs index e55c5f6..74a64f1 100644 --- a/ProjectGSM/Documents/TableReport.cs +++ b/ProjectGSM/Documents/TableReport.cs @@ -33,7 +33,7 @@ internal class TableReport { new ExcelBuilder(filePath) .AddHeader($"Сводка по делу \"{_caseRepository.ReadCaseById(caseId).Description}\"", 0, 4) - .AddParagraph("за период", 0) + .AddParagraph($"за период c {startDate:dd.MM.yyyy} по{endDate: dd.MM.yyyy}", 0) .AddTable([10, 10, 15], GetData(caseId, startDate, endDate)) .Build(); @@ -50,6 +50,7 @@ internal class TableReport { var statusData = _statusHistoryRepository .ReadStatusHistories() + // .ReadStatusHistories(startDate, endDate, caseId) .Where(x => x.CreatedAt >= startDate && x.CreatedAt <= endDate && x.CaseId == caseId) .GroupBy(x => (x.Status, x.CreatedAt.Date)) .Select(x => new @@ -65,7 +66,7 @@ internal class TableReport statusData .Select(x => new string[] { - x.Status.ToString(), x.Date.ToString(), x.Count.ToString() + x.Status.ToString(), x.Date.ToString("dd.MM.yyyy"), x.Count.ToString() })) .Union( [ diff --git a/ProjectGSM/Entities/Advocate.cs b/ProjectGSM/Entities/Advocate.cs index ba02b2d..bfa8611 100644 --- a/ProjectGSM/Entities/Advocate.cs +++ b/ProjectGSM/Entities/Advocate.cs @@ -5,7 +5,7 @@ namespace ProjectGSM.Entities; public class Advocate { - [Browsable(false)] public int Id { get; private set; } + public int Id { get; private set; } [DisplayName("Имя")] public string Name { get; private set; } = string.Empty; diff --git a/ProjectGSM/Entities/Case.cs b/ProjectGSM/Entities/Case.cs index 5c97c9d..9d06729 100644 --- a/ProjectGSM/Entities/Case.cs +++ b/ProjectGSM/Entities/Case.cs @@ -6,7 +6,7 @@ namespace ProjectGSM.Entities; public class Case { - [Browsable(false)] public int Id { get; private set; } + public int Id { get; private set; } [DisplayName("Тип обращения")] public TypeAppeal TypeAppeal { get; private set; } @@ -29,11 +29,19 @@ public class Case [DisplayName("Дата создания")] public DateTime CreatedAt { get; private set; } = DateTime.UtcNow; - [JsonIgnore] [Browsable(false)] public List Advocates { get; set; } = new(); + [JsonIgnore] [Browsable(false)] public IEnumerable Advocates { get; private set; } = []; [DisplayName("Адвокаты")] public string AdvocatesNames => string.Join(", ", Advocates.Select(x => $"{x.AdvocateName} ({x.Post})")); + public void SetAdvocates(IEnumerable advocates) + { + if (advocates != null && advocates.Any()) + { + Advocates = advocates; + } + } + // Конструктор для создания сущности public static Case CreateEntity( int id, diff --git a/ProjectGSM/Entities/CaseAdvocate.cs b/ProjectGSM/Entities/CaseAdvocate.cs index 1df5ff9..1a47841 100644 --- a/ProjectGSM/Entities/CaseAdvocate.cs +++ b/ProjectGSM/Entities/CaseAdvocate.cs @@ -4,9 +4,9 @@ namespace ProjectGSM.Entities; public class CaseAdvocate { - [Browsable(false)] public int CaseId { get; private set; } + public int CaseId { get; private set; } - [Browsable(false)] public int AdvocateId { get; private set; } + public int AdvocateId { get; private set; } [DisplayName("Дело")] public string CaseDescription { get; private set; } = string.Empty; diff --git a/ProjectGSM/Entities/Client.cs b/ProjectGSM/Entities/Client.cs index f3380c6..c35a614 100644 --- a/ProjectGSM/Entities/Client.cs +++ b/ProjectGSM/Entities/Client.cs @@ -4,7 +4,7 @@ namespace ProjectGSM.Entities; public class Client { - [Browsable(false)] public int Id { get; private set; } + public int Id { get; private set; } [DisplayName("Имя")] public string Name { get; private set; } = string.Empty; diff --git a/ProjectGSM/Entities/Court.cs b/ProjectGSM/Entities/Court.cs index 29f585e..6748f85 100644 --- a/ProjectGSM/Entities/Court.cs +++ b/ProjectGSM/Entities/Court.cs @@ -4,7 +4,7 @@ namespace ProjectGSM.Entities; public class Court { - [Browsable(false)] public int Id { get; private set; } + public int Id { get; private set; } [DisplayName("Название")] public string Name { get; private set; } = string.Empty; diff --git a/ProjectGSM/Entities/StatusHistory.cs b/ProjectGSM/Entities/StatusHistory.cs index e84cd6e..1753d74 100644 --- a/ProjectGSM/Entities/StatusHistory.cs +++ b/ProjectGSM/Entities/StatusHistory.cs @@ -5,7 +5,7 @@ namespace ProjectGSM.Entities; public class StatusHistory { - [Browsable(false)] public int CaseId { get; private set; } + public int CaseId { get; private set; } [DisplayName("Дело")] public string CaseDescription { get; private set; } = string.Empty; diff --git a/ProjectGSM/Forms/FormAdvocates.cs b/ProjectGSM/Forms/FormAdvocates.cs index 8631fbd..65e0e89 100644 --- a/ProjectGSM/Forms/FormAdvocates.cs +++ b/ProjectGSM/Forms/FormAdvocates.cs @@ -105,6 +105,7 @@ namespace ProjectGSM.Forms private void LoadList() { dataGridViewAdvocats.DataSource = _advocateRepository.ReadAdvocates(); + dataGridViewAdvocats.Columns["Id"].Visible = false; dataGridViewAdvocats.Columns["CreatedAt"].DefaultCellStyle.Format = "dd.MM.yyyy"; } diff --git a/ProjectGSM/Forms/FormCase.cs b/ProjectGSM/Forms/FormCase.cs index 924f6db..ae7ff4e 100644 --- a/ProjectGSM/Forms/FormCase.cs +++ b/ProjectGSM/Forms/FormCase.cs @@ -43,7 +43,7 @@ namespace ProjectGSM.Forms } Case caseE = CreateCase(0); - caseE.Advocates = CreateListCaseAdvocateFromDataGrid(); + caseE.SetAdvocates(CreateListCaseAdvocateFromDataGrid()); _caseRepository.CreateCase(caseE); Close(); } diff --git a/ProjectGSM/Forms/FormCases.cs b/ProjectGSM/Forms/FormCases.cs index 04f9654..1ca7a8a 100644 --- a/ProjectGSM/Forms/FormCases.cs +++ b/ProjectGSM/Forms/FormCases.cs @@ -85,6 +85,7 @@ namespace ProjectGSM.Forms { dataGridView.DataSource = _caseRepository.ReadCases(); + dataGridView.Columns["Id"].Visible = false; dataGridView.Columns["CreatedAt"].DefaultCellStyle.Format = "dd.MM.yyyy"; } diff --git a/ProjectGSM/Forms/FormClients.cs b/ProjectGSM/Forms/FormClients.cs index 3eb8123..83379dd 100644 --- a/ProjectGSM/Forms/FormClients.cs +++ b/ProjectGSM/Forms/FormClients.cs @@ -105,6 +105,7 @@ namespace ProjectGSM.Forms private void LoadList() { dataGridView.DataSource = _clientRepository.ReadClients(); + dataGridView.Columns["Id"].Visible = false; dataGridView.Columns["CreatedAt"].DefaultCellStyle.Format = "dd.MM.yyyy"; } diff --git a/ProjectGSM/Forms/FormCourts.cs b/ProjectGSM/Forms/FormCourts.cs index d594c2f..92a2a98 100644 --- a/ProjectGSM/Forms/FormCourts.cs +++ b/ProjectGSM/Forms/FormCourts.cs @@ -106,6 +106,7 @@ namespace ProjectGSM.Forms { dataGridView.DataSource = _courtRepository.ReadCourts(); + dataGridView.Columns["Id"].Visible = false; dataGridView.Columns["CreatedAt"].DefaultCellStyle.Format = "dd.MM.yyyy"; } diff --git a/ProjectGSM/Forms/FormStatusesCasesReport.cs b/ProjectGSM/Forms/FormStatusesCasesReport.cs index 83d228a..aabe5dd 100644 --- a/ProjectGSM/Forms/FormStatusesCasesReport.cs +++ b/ProjectGSM/Forms/FormStatusesCasesReport.cs @@ -20,6 +20,7 @@ namespace ProjectGSM.Forms public FormStatusesCasesReport(IUnityContainer container) { InitializeComponent(); + _container = container ?? throw new ArgumentNullException(nameof(container)); } diff --git a/ProjectGSM/Forms/FormStatusesHistory.cs b/ProjectGSM/Forms/FormStatusesHistory.cs index 0622a16..ecbd532 100644 --- a/ProjectGSM/Forms/FormStatusesHistory.cs +++ b/ProjectGSM/Forms/FormStatusesHistory.cs @@ -50,6 +50,7 @@ namespace ProjectGSM.Forms { dataGridView.DataSource = _statusHistoryRepository.ReadStatusHistories(); + dataGridView.Columns["CaseId"].Visible = false; dataGridView.Columns["CreatedAt"].DefaultCellStyle.Format = "dd.MM.yyyy"; } diff --git a/ProjectGSM/Repositories/ICaseRepository.cs b/ProjectGSM/Repositories/ICaseRepository.cs index ff0f6cf..7db8947 100644 --- a/ProjectGSM/Repositories/ICaseRepository.cs +++ b/ProjectGSM/Repositories/ICaseRepository.cs @@ -4,7 +4,7 @@ namespace ProjectGSM.Repositories; public interface ICaseRepository { - IEnumerable ReadCases(); + IEnumerable ReadCases(DateTime? dateTo = null); Case ReadCaseById(int id); void CreateCase(Case caseEntity); void DeleteCase(int id); diff --git a/ProjectGSM/Repositories/Implementations/CaseAdvocatesRepository.cs b/ProjectGSM/Repositories/Implementations/CaseAdvocatesRepository.cs index 408b99e..d71cc69 100644 --- a/ProjectGSM/Repositories/Implementations/CaseAdvocatesRepository.cs +++ b/ProjectGSM/Repositories/Implementations/CaseAdvocatesRepository.cs @@ -4,6 +4,7 @@ using Dapper; using Microsoft.Extensions.Logging; using Npgsql; using ProjectGSM.Entities; +using ProjectGSM.Query; namespace ProjectGSM.Repositories.Implementations; @@ -25,14 +26,20 @@ public class CaseAdvocatesRepository : ICaseAdvocateRepository _logger.LogInformation("Получение всех объектов"); try { + var builder = new QueryBuilder(); + if (dateTo.HasValue) + { + builder.AddCondition("ca.CreatedAt <= @dateTo"); + } + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); var querySelect = "SELECT ca.*, a.name as AdvocateName, cs.description as CaseDescription " + "FROM case_advocates as ca LEFT JOIN cases as cs ON cs.id = ca.caseid " + - "LEFT JOIN advocates as a ON a.id = ca.advocateid"; + $"LEFT JOIN advocates as a ON a.id = ca.advocateid {builder.Build()}"; var caseAdvocates = - connection.Query(querySelect); + connection.Query(querySelect, param: new { dateTo }); _logger.LogDebug("Полученные объекты: {json}", JsonSerializer.Serialize(caseAdvocates)); return caseAdvocates; diff --git a/ProjectGSM/Repositories/Implementations/CaseRepository.cs b/ProjectGSM/Repositories/Implementations/CaseRepository.cs index bc6ac48..e3fa022 100644 --- a/ProjectGSM/Repositories/Implementations/CaseRepository.cs +++ b/ProjectGSM/Repositories/Implementations/CaseRepository.cs @@ -1,9 +1,11 @@ using System.Data.SqlClient; -using System.Text.Json; using Dapper; using Microsoft.Extensions.Logging; +using Newtonsoft.Json; using Npgsql; using ProjectGSM.Entities; +using ProjectGSM.Query; +using JsonSerializer = System.Text.Json.JsonSerializer; namespace ProjectGSM.Repositories.Implementations; @@ -19,22 +21,51 @@ public class CaseRepository : ICaseRepository _logger = logger; } - public IEnumerable ReadCases() + public IEnumerable ReadCases(DateTime? dateTo = null) { _logger.LogInformation("Получение всех объектов"); try { + var builder = new QueryBuilder(); + if (dateTo.HasValue) + { + builder.AddCondition("cases.CreatedAt <= @dateTo"); + } + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - var querySelect = - "SELECT cases.*, courts.name as CourtName, clients.name as ClientName " + - "FROM cases LEFT JOIN courts on courts.Id = cases.courtid " + - "LEFT JOIN clients on clients.Id = cases.clientid"; - var cases = connection.Query(querySelect); + var querySelect = + "SELECT cases.*, courts.name as CourtName, clients.name as ClientName, ca.caseid as CaseId, a.name as AdvocateName " + + "FROM cases " + + "LEFT JOIN courts on courts.Id = cases.courtid " + + "LEFT JOIN clients on clients.Id = cases.clientid " + + "INNER JOIN case_advocates ca ON ca.caseid = cases.Id LEFT JOIN advocates a on a.Id = ca.advocateid " + + $"{builder.Build()}"; + // $"{builder.Build()}"; + var casesDict = new Dictionary>(); + ; + var cases = connection.Query(querySelect, + (caseEntity, caseAdvocate) => + { + if (!casesDict.TryGetValue(caseEntity.Id, out var ca)) + { + ca = new List(); + casesDict.Add(caseEntity.Id, ca); + } + + ca.Add(caseAdvocate); + return caseEntity; + }, splitOn: "CaseId", param: new { dateTo, }); + _logger.LogDebug("Полученные объекты: {json}", - JsonSerializer.Serialize(cases)); - return cases; + JsonConvert.SerializeObject(cases)); + return casesDict.Select(x => + { + var caseEntity = cases.First(y => y.Id == x.Key); + caseEntity.SetAdvocates(x.Value); + return caseEntity; + }).ToArray(); } catch (Exception ex) { diff --git a/ProjectGSM/Repositories/Implementations/StatusHistoryRepository.cs b/ProjectGSM/Repositories/Implementations/StatusHistoryRepository.cs index 303f6c4..f4f324a 100644 --- a/ProjectGSM/Repositories/Implementations/StatusHistoryRepository.cs +++ b/ProjectGSM/Repositories/Implementations/StatusHistoryRepository.cs @@ -43,7 +43,7 @@ public class StatusHistoryRepository : IStatusHistoryRepository using var connection = new NpgsqlConnection(_connectionString.ConnectionString); var querySelect = - @$"SELECT sh.*, c.description as CaseDescription FROM status_histories as sh LEFT JOIN cases as c ON c.id = sh.caseid{builder.Build()}"; + @$"SELECT sh.*, c.description as CaseDescription FROM status_histories as sh LEFT JOIN cases as c ON c.id = sh.caseid {builder.Build()}"; var statusHistories = connection.Query(querySelect, new { dateForm, dateTo, caseId }); _logger.LogDebug("Полученные объекты: {json}",