diff --git a/RegistrationPatientsPolyclinic/RegistrationPatientsPolyclinic/Entities/MedicalHistory.cs b/RegistrationPatientsPolyclinic/RegistrationPatientsPolyclinic/Entities/MedicalHistory.cs index a6d3ebe..b84239d 100644 --- a/RegistrationPatientsPolyclinic/RegistrationPatientsPolyclinic/Entities/MedicalHistory.cs +++ b/RegistrationPatientsPolyclinic/RegistrationPatientsPolyclinic/Entities/MedicalHistory.cs @@ -27,12 +27,12 @@ public class MedicalHistory // сущность пополнения, напо [DisplayName("Дата приема")] public DateTime VisitDate { get; private set; } - [DisplayName("Таблетки")] + + public string Drug => DrugMedicalHistory != null ? string.Join(", ", DrugMedicalHistory.Select(x => $"{x.Id} {x.Description}")) : string.Empty; - [Browsable(false)] public IEnumerable DrugMedicalHistory { get; set; } = []; diff --git a/RegistrationPatientsPolyclinic/RegistrationPatientsPolyclinic/Reports/TableReport.cs b/RegistrationPatientsPolyclinic/RegistrationPatientsPolyclinic/Reports/TableReport.cs index 779aea8..2a3438a 100644 --- a/RegistrationPatientsPolyclinic/RegistrationPatientsPolyclinic/Reports/TableReport.cs +++ b/RegistrationPatientsPolyclinic/RegistrationPatientsPolyclinic/Reports/TableReport.cs @@ -20,20 +20,24 @@ internal class TableReport private readonly IDoctorPaymentsRepository _doctorPaymentsRepository; + private readonly IDrugRepository _drugRepository; + private readonly ILogger _logger; - internal static readonly string[] item = ["Дата", "Описание", "Количество лекарства"]; + internal static readonly string[] item = ["Доктор", "Описание", "Количество пациентов"]; - public TableReport(IMedicalHistoryRepository medicalHistoryRepository, IDoctorPaymentsRepository doctorPaymentsRepository, ILogger logger) + public TableReport(IMedicalHistoryRepository medicalHistoryRepository, IDoctorPaymentsRepository doctorPaymentsRepository, IDrugRepository drugRepository, ILogger logger) { _medicalHistoryRepository = medicalHistoryRepository ?? throw new ArgumentNullException(nameof(medicalHistoryRepository)); ; _doctorPaymentsRepository = doctorPaymentsRepository ?? throw new ArgumentNullException(nameof(doctorPaymentsRepository)); + _drugRepository = drugRepository ?? + throw new ArgumentNullException(nameof(drugRepository)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } - + public bool CreateTable(string filePath, int drugId, DateTime startDate, DateTime endDate) { try @@ -62,12 +66,11 @@ internal class TableReport /// ВНИЗУ БЫЛ ПРАВИЛЬНЫЙ - + /* private List GetData(int drugId, DateTime startDate, DateTime endDate) { var data = _medicalHistoryRepository - .ReadMedicalHistory() - .Where(x => x.VisitDate >= startDate && x.VisitDate <= endDate && x.DrugMedicalHistory.Any(y => y.DrugId == drugId) ) + .ReadMedicalHistory(dateForm: startDate,dateTo : endDate, null, null) .Select(x => new { Date = x.VisitDate, @@ -85,18 +88,69 @@ internal class TableReport return new List() { item } .Union( data - .Select(x => new string[] {x.Date.ToString(), x.CountIn ?? string.Empty, x.CountOut?.ToString() ?? string.Empty })) + .Select(x => new string[] {x.Date.ToString("dd.MM.yyyy"), x.CountIn ?? string.Empty, x.CountOut?.ToString() ?? string.Empty })) .Union( new List() { new string[] { "Всего", data.Sum(x => x.CountOut ?? 0).ToString(), string.Empty } }) .ToList(); } + */ - + /* + private List GetData(int drugId, DateTime startDate, DateTime endDate) + { + var data = _medicalHistoryRepository + .ReadMedicalHistory() + .Where(x => x.VisitDate >= startDate && x.VisitDate <= endDate && x.DrugMedicalHistory.Any(y => y.DrugId == drugId)) + .Select(x => new + { + Date = x.VisitDate, + CountIn = x.DrugMedicalHistory.FirstOrDefault(y => y.DrugId == drugId)?.Description, + CountOut = (int?)null + }) + .Union( + _doctorPaymentsRepository + .ReadDoctorPayments() + .Where(x => x.DoctorPaymentData >= startDate && x.DoctorPaymentData <= endDate) + .Select(x => new { Date = x.DoctorPaymentData, CountIn = (string?)null, CountOut = (int?)x.Count_Patient })) + .OrderBy(x => x.Date); - - + return new List() { item } + .Union( + data + .Select(x => new string[] { x.Date.ToString(), x.CountIn ?? string.Empty, x.CountOut?.ToString() ?? string.Empty })) + .Union( + new List() { new string[] { "Всего", data.Sum(x => x.CountOut ?? 0).ToString(), string.Empty } }) + .ToList(); + } + */ - + private List GetData(int drugId, DateTime startDate, DateTime endDate) + { + var data = _medicalHistoryRepository + .ReadMedicalHistory() + .Where(x => x.VisitDate >= startDate && x.VisitDate <= endDate && x.DrugMedicalHistory.Any(y => y.DrugId == drugId)) + .Select(x => new + { + Date = x.DoctorId, + CountIn = x.DrugMedicalHistory.FirstOrDefault(y => y.DrugId == drugId)?.Description, + CountOut = (int?)null + }) + .Union( + _doctorPaymentsRepository + .ReadDoctorPayments() + .Where(x => x.Id != 0) + .Select(x => new { Date = x.IdDoctor, CountIn = (string?)null, CountOut = (int?)x.Count_Patient })) + .OrderBy(x => x.Date); + + + return new List() { item } + .Union( + data + .Select(x => new string[] { x.Date.ToString(), x.CountIn ?? string.Empty, x.CountOut?.ToString() ?? string.Empty })) + .Union( + new List() { new string[] { "Всего", string.Empty, data.Sum(x => x.CountOut ?? 0).ToString() } }) + .ToList(); + } } diff --git a/RegistrationPatientsPolyclinic/RegistrationPatientsPolyclinic/Repositories/IDoctorPaymentsRepository.cs b/RegistrationPatientsPolyclinic/RegistrationPatientsPolyclinic/Repositories/IDoctorPaymentsRepository.cs index bb151d8..4b65f1b 100644 --- a/RegistrationPatientsPolyclinic/RegistrationPatientsPolyclinic/Repositories/IDoctorPaymentsRepository.cs +++ b/RegistrationPatientsPolyclinic/RegistrationPatientsPolyclinic/Repositories/IDoctorPaymentsRepository.cs @@ -9,8 +9,8 @@ namespace RegistrationPatientsPolyclinic.Repositories; public interface IDoctorPaymentsRepository { - IEnumerable ReadDoctorPayments(); - + //IEnumerable ReadDoctorPayments(); + IEnumerable ReadDoctorPayments(int? doctorId = null, string month = null); void CreateDoctorPayments(DoctorPayments doctorPayments); diff --git a/RegistrationPatientsPolyclinic/RegistrationPatientsPolyclinic/Repositories/IMedicalHistoryRepository.cs b/RegistrationPatientsPolyclinic/RegistrationPatientsPolyclinic/Repositories/IMedicalHistoryRepository.cs index 7979ac9..c32625a 100644 --- a/RegistrationPatientsPolyclinic/RegistrationPatientsPolyclinic/Repositories/IMedicalHistoryRepository.cs +++ b/RegistrationPatientsPolyclinic/RegistrationPatientsPolyclinic/Repositories/IMedicalHistoryRepository.cs @@ -13,12 +13,6 @@ public interface IMedicalHistoryRepository IEnumerable ReadMedicalHistory(DateTime? dateForm = null, DateTime? dateTo = null, int? PatientId = null, int? DoctorId = null); // по этим параметрам можно не весь список читать, а только какую то часть - - /* - IEnumerable ReadMedicalHistory(DateTime? dateForm = null, DateTime? dateTo = null, int? PatientId = null, - int? DoctorId = null); // по этим параметрам можно не весь список читать, а только какую то часть - - */ void CreateMedicalHistory(MedicalHistory medicalHistory); // объекь будет заносится в список diff --git a/RegistrationPatientsPolyclinic/RegistrationPatientsPolyclinic/Repositories/Implementations/DoctorPaymentsRepository.cs b/RegistrationPatientsPolyclinic/RegistrationPatientsPolyclinic/Repositories/Implementations/DoctorPaymentsRepository.cs index 5394d98..b2bac8a 100644 --- a/RegistrationPatientsPolyclinic/RegistrationPatientsPolyclinic/Repositories/Implementations/DoctorPaymentsRepository.cs +++ b/RegistrationPatientsPolyclinic/RegistrationPatientsPolyclinic/Repositories/Implementations/DoctorPaymentsRepository.cs @@ -1,4 +1,6 @@ using Dapper; +using DocumentFormat.OpenXml.Bibliography; +using DocumentFormat.OpenXml.Wordprocessing; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Npgsql; @@ -50,19 +52,31 @@ VALUES (@IdDoctor, @Month, @Count_Patient, @Payment)"; - public IEnumerable ReadDoctorPayments() + public IEnumerable ReadDoctorPayments(int? doctorId = null, string month = null) { _logger.LogInformation("Получение всех объектов"); try { + var builder = new QueryBuilder(); + + if (doctorId.HasValue) + { + builder.AddCondition("fa.IdDoctor = @doctorId"); + } + if (!string.IsNullOrEmpty(month)) + { + builder.AddCondition("fa.Month = @month"); + } + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - var querySelect = @"SELECT fa.*, + var querySelect = $@"SELECT fa.*, CONCAT(e.Last_Name, ' ', e.First_Name) AS DoctorName FROM DoctorPayments fa - LEFT JOIN Doctor e ON e.Id = fa.IdDoctor"; + LEFT JOIN Doctor e ON e.Id = fa.IdDoctor + {builder.Build()}"; var doctorPayments = - connection.Query(querySelect); + connection.Query(querySelect, new { doctorId , month }); _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(doctorPayments)); return doctorPayments; diff --git a/RegistrationPatientsPolyclinic/RegistrationPatientsPolyclinic/Repositories/Implementations/MedicalHistoryRepository.cs b/RegistrationPatientsPolyclinic/RegistrationPatientsPolyclinic/Repositories/Implementations/MedicalHistoryRepository.cs index 366b606..0695d4b 100644 --- a/RegistrationPatientsPolyclinic/RegistrationPatientsPolyclinic/Repositories/Implementations/MedicalHistoryRepository.cs +++ b/RegistrationPatientsPolyclinic/RegistrationPatientsPolyclinic/Repositories/Implementations/MedicalHistoryRepository.cs @@ -1,4 +1,5 @@ using Dapper; +using DocumentFormat.OpenXml.Wordprocessing; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Npgsql; @@ -24,7 +25,7 @@ public class MedicalHistoryRepository : IMedicalHistoryRepository _logger = logger; } - + public void CreateMedicalHistory(MedicalHistory medicalHistory) { _logger.LogInformation("Добавление объекта"); @@ -63,7 +64,7 @@ VALUES (@DrugId,@MedicalHistoryId, @Description)"; } } - + public void DeletemedicalHistory(int id) { @@ -113,27 +114,40 @@ WHERE Id=@id"; } */ + + // ВНИЗУ САМЫЙ ПОСЛЕДНИЙ ПРАВИЛЬНЫЙ!!!! + + public IEnumerable ReadMedicalHistory(DateTime? dateForm = null, DateTime? dateTo = null, int? PatientId = null, int? DoctorId = null) { _logger.LogInformation("Получение всех объектов"); try { + var builder = new QueryBuilder(); + + if (dateForm.HasValue) + { + builder.AddCondition("mh.VisitDate >= @dateForm"); + } + if (dateTo.HasValue) + { + builder.AddCondition("mh.VisitDate <= @dateTo"); + } + if (DoctorId.HasValue) + { + builder.AddCondition("mh.DoctorId = @DoctorId"); + } + if (PatientId.HasValue) + { + builder.AddCondition("mh.PatientId = @PatientId"); + } + + + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); - //var querySelect = @"SELECT * FROM MedicalHistory"; - /* var querySelect = @"SELECT mh.*, - CONCAT(p.LastName, ' ', p.FirstName) as PatientName, - CONCAT(d.LastName, ' ', d.FirstName) as DoctorName, - dmh.DrugId, - dmh.Description - dr.DrugName AS drugName - FROM MedicalHistory mh - LEFT JOIN Patient p on p.Id = mh.PatientId - LEFT JOIN Doctor d on d.Id = mh.DoctorId - INNER JOIN DrugMedicalHistory dmh ON dmh.MedicalHistoryId = mh.Id - LEFT JOIN Drug dr on dr.Id = dmh.DrugId";*/ - var querySelect = @"SELECT mh.*, + var querySelect = $@"SELECT mh.*, CONCAT(p.Last_Name, ' ', p.First_Name) as PatientName, CONCAT(d.Last_Name, ' ', d.First_Name) as DoctorName, dmh.Id AS Id, dmh.DrugId AS drugId, dmh.Description AS Description @@ -141,7 +155,7 @@ WHERE Id=@id"; LEFT JOIN DrugMedicalHistory dmh ON dmh.MedicalHistoryId = mh.Id LEFT JOIN Patient p ON p.Id = mh.PatientId LEFT JOIN doctor d ON d.Id = mh.DoctorId - "; + {builder.Build()}"; var historyDict = new Dictionary>(); var medicalHistory = @@ -158,7 +172,7 @@ WHERE Id=@id"; drugs.Add(DrugMedicalHistory.CreateEntity(drugMedicalHistory.Id, drugMedicalHistory.DrugId, drugMedicalHistory.Description)); } return history; - }, splitOn: "Id");//, param: new { dateForm, dateTo, DoctorId, PatientId}); + }, splitOn: "Id", param: new { dateForm, dateTo, DoctorId, PatientId }); _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(medicalHistory)); @@ -175,8 +189,9 @@ WHERE Id=@id"; throw; } } + + } - diff --git a/RegistrationPatientsPolyclinic/RegistrationPatientsPolyclinic/Repositories/Implementations/QueryBuilder.cs b/RegistrationPatientsPolyclinic/RegistrationPatientsPolyclinic/Repositories/Implementations/QueryBuilder.cs new file mode 100644 index 0000000..3fb2627 --- /dev/null +++ b/RegistrationPatientsPolyclinic/RegistrationPatientsPolyclinic/Repositories/Implementations/QueryBuilder.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RegistrationPatientsPolyclinic.Repositories.Implementations; + +internal class QueryBuilder +{ + private readonly StringBuilder _builder; + public QueryBuilder() + { + _builder = new(); + } + public QueryBuilder AddCondition(string condition) + { + if (_builder.Length > 0) + { + _builder.Append(" AND "); + } + _builder.Append(condition); + return this; + } + public string Build() + { + if (_builder.Length == 0) + { + return string.Empty; + } + return $"WHERE {_builder}"; + } +}