diff --git a/ProjectPolyclinic/ProjectPolyclinic/Entities/Doctor.cs b/ProjectPolyclinic/ProjectPolyclinic/Entities/Doctor.cs index f3e16b7..25d3c7b 100644 --- a/ProjectPolyclinic/ProjectPolyclinic/Entities/Doctor.cs +++ b/ProjectPolyclinic/ProjectPolyclinic/Entities/Doctor.cs @@ -1,6 +1,7 @@ using ProjectPolyclinic.Entities.Enums; using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -9,17 +10,19 @@ namespace ProjectPolyclinic.Entities; public class Doctor { - public int Id { get; private set; } - + [DisplayName("Имя")] public string First_Name { get; private set; } = string.Empty; - + [DisplayName("Фамилия")] public string Last_Name { get; private set; } = string.Empty; + public string FullName => $"{First_Name} {Last_Name}"; + [DisplayName("Кабинет")] public int Room { get; private set; } - - public Specialization Specialization { get; private set; } - + [DisplayName("Специализация")] + public Specialization Specialization { get; private set; } + + [DisplayName("Уровень специализации")] public SpecializationLevel SpecializationLevel { get; private set; } public static Doctor CreateEntity(int id, string first_Name, string last_Name, int room, Specialization specialization, SpecializationLevel specializationLevel) diff --git a/ProjectPolyclinic/ProjectPolyclinic/Entities/DoctorPay.cs b/ProjectPolyclinic/ProjectPolyclinic/Entities/DoctorPay.cs index 8363c94..0d86d90 100644 --- a/ProjectPolyclinic/ProjectPolyclinic/Entities/DoctorPay.cs +++ b/ProjectPolyclinic/ProjectPolyclinic/Entities/DoctorPay.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -8,15 +9,18 @@ namespace ProjectPolyclinic.Entities; public class DoctorPay { - public int Id { get; private set; } - + + [Browsable(false)] public int IdDoctor { get; private set; } + [DisplayName("Имя врача")] + public string DoctorName { get; private set; } = string.Empty; + [DisplayName("Месяц")] public string Month { get; private set; } = string.Empty; - + [DisplayName("Количество пациентов")] public int Count_Patient { get; private set; } - + [DisplayName("Оплата")] public int Payment { get; private set; } public static DoctorPay CreateElement(int id, int idDoctor, string month, int count_patient, int payment) diff --git a/ProjectPolyclinic/ProjectPolyclinic/Entities/Drug.cs b/ProjectPolyclinic/ProjectPolyclinic/Entities/Drug.cs index 0696774..9db5fb2 100644 --- a/ProjectPolyclinic/ProjectPolyclinic/Entities/Drug.cs +++ b/ProjectPolyclinic/ProjectPolyclinic/Entities/Drug.cs @@ -1,6 +1,7 @@ - using ProjectPolyclinic.Entities.Enums; +using ProjectPolyclinic.Entities.Enums; using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -11,10 +12,12 @@ public class Drug { public int Id { get; private set; } + [DisplayName("Названия лекарств")] public DrugsNames drugsNames { get; private set; } + [DisplayName("Грамовка")] public int Grams { get; private set; } - + [DisplayName("Рецепт")] public string Description { get; private set; } = string.Empty; public static Drug CreateElement(int id, DrugsNames name, int grams, string description) diff --git a/ProjectPolyclinic/ProjectPolyclinic/Entities/DrugMedHistory.cs b/ProjectPolyclinic/ProjectPolyclinic/Entities/DrugMedHistory.cs index 498184f..7b3eb91 100644 --- a/ProjectPolyclinic/ProjectPolyclinic/Entities/DrugMedHistory.cs +++ b/ProjectPolyclinic/ProjectPolyclinic/Entities/DrugMedHistory.cs @@ -12,10 +12,15 @@ public class DrugMedHistory public int DrugId { get; private set; } + public string DrugName { get; private set; } = string.Empty; + public int MedicalHistoryId { get; private set; } public int Count { get; private set; } + private IEnumerable _drugMedHistory = new List(); + + public static DrugMedHistory CreateEntity(int id, int drugId, int medicalHistoryId, int count) { return new DrugMedHistory @@ -26,4 +31,6 @@ public class DrugMedHistory Count = count }; } + + } diff --git a/ProjectPolyclinic/ProjectPolyclinic/Entities/MedicalHistory.cs b/ProjectPolyclinic/ProjectPolyclinic/Entities/MedicalHistory.cs index 6371e1c..d4ab578 100644 --- a/ProjectPolyclinic/ProjectPolyclinic/Entities/MedicalHistory.cs +++ b/ProjectPolyclinic/ProjectPolyclinic/Entities/MedicalHistory.cs @@ -1,6 +1,7 @@ using ProjectPolyclinic.Entities.Enums; using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -10,17 +11,25 @@ namespace ProjectPolyclinic.Entities; public class MedicalHistory { public int Id { get; private set; } - + [Browsable(false)] public int PatientId { get; private set; } - + [Browsable(false)] public int DoctorId { get; private set; } - + [DisplayName("Имя пациента")] + public string PatientName { get; private set; } = string.Empty; + [DisplayName("Имя врача")] + public string DoctorName { get; private set; } = string.Empty; + [DisplayName("Время визита")] public DateTime VisitDate { get; private set; } - + [DisplayName("Состояние пациента")] public Status Status { get; private set; } - + [Browsable(false)] public IEnumerable DrugMedHistory { get; private set; } = []; + [DisplayName("Назначенные лекарства")] + public string DrugsSummary => string.Join(", ", DrugMedHistory + .Select(d => $"Id: {d.DrugId}, Кол-во: {d.Count}")); + public static MedicalHistory CreateEntity(int id, int patientId, Status status, int doctorId, IEnumerable drugMedHistory) { @@ -35,20 +44,12 @@ public class MedicalHistory }; } - - public static MedicalHistory CreateEntity(TempMedicalHistory tempMedicalHistory, - IEnumerable drugMedHistory) + public void SetDrugMedHistory(IEnumerable drugMedHistory) { - return new MedicalHistory + if (drugMedHistory != null) { - Id = tempMedicalHistory.Id, - PatientId = tempMedicalHistory.PatientId, - DoctorId = tempMedicalHistory.DoctorId, - VisitDate = tempMedicalHistory.VisitDate, - Status = (Status)tempMedicalHistory.Status, - DrugMedHistory = drugMedHistory - }; + DrugMedHistory = drugMedHistory; + } } - } \ No newline at end of file diff --git a/ProjectPolyclinic/ProjectPolyclinic/Entities/Patient.cs b/ProjectPolyclinic/ProjectPolyclinic/Entities/Patient.cs index 9205ade..b3b0dc0 100644 --- a/ProjectPolyclinic/ProjectPolyclinic/Entities/Patient.cs +++ b/ProjectPolyclinic/ProjectPolyclinic/Entities/Patient.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -8,15 +9,17 @@ namespace ProjectPolyclinic.Entities; public class Patient { - public int Id { get; private set; } + [DisplayName("Имя")] public string First_Name { get; private set; } = string.Empty; - + [DisplayName("Фамилия")] public string Last_Name { get; private set; } = string.Empty; + public string FullName => $"{First_Name} {Last_Name}"; + [DisplayName("Адрес")] public string Address { get; private set; } = string.Empty; - + [DisplayName("Номер телефона")] public string ContactNumber { get; private set; } = string.Empty; diff --git a/ProjectPolyclinic/ProjectPolyclinic/Entities/TempMedicalHistory.cs b/ProjectPolyclinic/ProjectPolyclinic/Entities/TempMedicalHistory.cs deleted file mode 100644 index 7d86820..0000000 --- a/ProjectPolyclinic/ProjectPolyclinic/Entities/TempMedicalHistory.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace ProjectPolyclinic.Entities; - -//public class TempMedicalHistory -//{ -// public int Id { get; set; } -// public int PatientId { get; set; } -// public int DoctorId { get; set; } -// public DateTime VisitDate { get; set; } -// public int Status { get; set; } -// public int DrugId { get; set; } -// public int Count { get; set; } - -//} -public class TempMedicalHistory -{ - public int Id { get; set; } - public int PatientId { get; set; } - public int DoctorId { get; set; } - public DateTime VisitDate { get; set; } - public int Status { get; set; } - - // Поля для лекарств - public int DrugId { get; set; } - public int Count { get; set; } -} - diff --git a/ProjectPolyclinic/ProjectPolyclinic/Forms/FormDoctorPayment.cs b/ProjectPolyclinic/ProjectPolyclinic/Forms/FormDoctorPayment.cs index 47440f8..b0ba825 100644 --- a/ProjectPolyclinic/ProjectPolyclinic/Forms/FormDoctorPayment.cs +++ b/ProjectPolyclinic/ProjectPolyclinic/Forms/FormDoctorPayment.cs @@ -26,7 +26,7 @@ public partial class FormDoctorPayment : Form throw new ArgumentNullException(nameof(doctorPayRepository)); comboBoxDoctor.DataSource = doctorRepository.ReadDoctors(); - comboBoxDoctor.DisplayMember = "Last_Name"; + comboBoxDoctor.DisplayMember = "FullName"; comboBoxDoctor.ValueMember = "Id"; } diff --git a/ProjectPolyclinic/ProjectPolyclinic/Forms/FormDoctorPayments.cs b/ProjectPolyclinic/ProjectPolyclinic/Forms/FormDoctorPayments.cs index 45beeda..1e7529d 100644 --- a/ProjectPolyclinic/ProjectPolyclinic/Forms/FormDoctorPayments.cs +++ b/ProjectPolyclinic/ProjectPolyclinic/Forms/FormDoctorPayments.cs @@ -47,10 +47,12 @@ public partial class FormDoctorPayments : Form } - - - private void LoadList() => dataGridView.DataSource = _doctorPayRepository.ReadDoctorPayments(); - + private void LoadList() + { + dataGridView.DataSource = _doctorPayRepository.ReadDoctorPayments(); + dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["Month"].DefaultCellStyle.Format = "yyyy-dd"; + } private void buttonAdd_Click(object sender, EventArgs e) diff --git a/ProjectPolyclinic/ProjectPolyclinic/Forms/FormDoctors.cs b/ProjectPolyclinic/ProjectPolyclinic/Forms/FormDoctors.cs index d24a4bc..d9f2a61 100644 --- a/ProjectPolyclinic/ProjectPolyclinic/Forms/FormDoctors.cs +++ b/ProjectPolyclinic/ProjectPolyclinic/Forms/FormDoctors.cs @@ -48,7 +48,12 @@ public partial class FormDoctors : Form } } - private void LoadList() => dataGridView.DataSource = _doctorRepository.ReadDoctors(); + private void LoadList() + { + dataGridView.DataSource = _doctorRepository.ReadDoctors(); + dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["FullName"].Visible = false; + } private bool TryGetIdentifierFromSelectedRow(out int id) diff --git a/ProjectPolyclinic/ProjectPolyclinic/Forms/FormDrugs.cs b/ProjectPolyclinic/ProjectPolyclinic/Forms/FormDrugs.cs index de5fc98..19547c6 100644 --- a/ProjectPolyclinic/ProjectPolyclinic/Forms/FormDrugs.cs +++ b/ProjectPolyclinic/ProjectPolyclinic/Forms/FormDrugs.cs @@ -47,8 +47,11 @@ public partial class FormDrugs : Form } - - private void LoadList() => dataGridView.DataSource = _dragRepository.ReadDrug(); + private void LoadList() + { + dataGridView.DataSource = _dragRepository.ReadDrug(); + dataGridView.Columns["Id"].Visible = false; + } diff --git a/ProjectPolyclinic/ProjectPolyclinic/Forms/FormMedicalHistories.cs b/ProjectPolyclinic/ProjectPolyclinic/Forms/FormMedicalHistories.cs index c1ad3e5..916a48c 100644 --- a/ProjectPolyclinic/ProjectPolyclinic/Forms/FormMedicalHistories.cs +++ b/ProjectPolyclinic/ProjectPolyclinic/Forms/FormMedicalHistories.cs @@ -48,10 +48,13 @@ public partial class FormMedicalHistories : Form } } - - private void LoadList() => dataGridView.DataSource = _medicalHistoryRepository.ReadMedicalHistory(); - - + private void LoadList() + { + dataGridView.DataSource = _medicalHistoryRepository.ReadMedicalHistory(); + dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["VisitDate"].DefaultCellStyle.Format = "dd MMMM yyyy hh:mm"; + } + private void buttonAdd_Click(object sender, EventArgs e) { try diff --git a/ProjectPolyclinic/ProjectPolyclinic/Forms/FormMedicalHistory.cs b/ProjectPolyclinic/ProjectPolyclinic/Forms/FormMedicalHistory.cs index 43b2cef..57fca17 100644 --- a/ProjectPolyclinic/ProjectPolyclinic/Forms/FormMedicalHistory.cs +++ b/ProjectPolyclinic/ProjectPolyclinic/Forms/FormMedicalHistory.cs @@ -33,11 +33,11 @@ public partial class FormMedicalHistory : Form throw new ArgumentNullException(nameof(medicalHistoryRepository)); comboBoxPacient.DataSource = patientRepository.ReadPatient(); - comboBoxPacient.DisplayMember = "First_Name"; + comboBoxPacient.DisplayMember = "FullName"; comboBoxPacient.ValueMember = "Id"; comboBoxDoctor.DataSource = doctorRepository.ReadDoctors(); - comboBoxDoctor.DisplayMember = "Last_Name"; + comboBoxDoctor.DisplayMember = "FullName"; comboBoxDoctor.ValueMember = "Id"; ColumnDrug.DataSource = drugRepository.ReadDrug(); diff --git a/ProjectPolyclinic/ProjectPolyclinic/Forms/FormPatients.cs b/ProjectPolyclinic/ProjectPolyclinic/Forms/FormPatients.cs index 4e8e9e6..0874e59 100644 --- a/ProjectPolyclinic/ProjectPolyclinic/Forms/FormPatients.cs +++ b/ProjectPolyclinic/ProjectPolyclinic/Forms/FormPatients.cs @@ -49,7 +49,12 @@ public partial class FormPatients : Form - private void LoadList() => dataGridView.DataSource = _pacientRepository.ReadPatient(); + private void LoadList() + { + dataGridView.DataSource = _pacientRepository.ReadPatient(); + dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["FullName"].Visible = false; + } diff --git a/ProjectPolyclinic/ProjectPolyclinic/Forms/FormPayReport.cs b/ProjectPolyclinic/ProjectPolyclinic/Forms/FormPayReport.cs index 828a9dc..5749e65 100644 --- a/ProjectPolyclinic/ProjectPolyclinic/Forms/FormPayReport.cs +++ b/ProjectPolyclinic/ProjectPolyclinic/Forms/FormPayReport.cs @@ -25,7 +25,7 @@ namespace ProjectPolyclinic.Forms throw new ArgumentNullException(nameof(container)); comboBoxDoctor.DataSource = doctorRepository.ReadDoctors(); - comboBoxDoctor.DisplayMember = "Last_Name"; + comboBoxDoctor.DisplayMember = "FullName"; comboBoxDoctor.ValueMember = "Id"; } private void buttonBuild_Click(object sender, EventArgs e) diff --git a/ProjectPolyclinic/ProjectPolyclinic/Reports/ChartReport.cs b/ProjectPolyclinic/ProjectPolyclinic/Reports/ChartReport.cs index 6dc399f..04cfe12 100644 --- a/ProjectPolyclinic/ProjectPolyclinic/Reports/ChartReport.cs +++ b/ProjectPolyclinic/ProjectPolyclinic/Reports/ChartReport.cs @@ -14,13 +14,14 @@ public class ChartReport _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } + public bool CreateChart(string filePath, DateTime dateTime) { try { new PdfBuilder(filePath) - .AddHeader("Выписанные лекарства") - .AddPieChart("Выписанные лекарства (Соотношение врачей и количества выписанных лекарств)", GetData(dateTime)) + .AddHeader($"Выписанные лекарства на {dateTime:dd.MM.yyyy}") + .AddPieChart("Соотношение выписанных лекарств по врачам", GetData(dateTime)) .Build(); return true; } @@ -31,6 +32,7 @@ public class ChartReport } } + private List<(string Caption, double Value)> GetData(DateTime dateTime) { var medicalHistories = _historyRepository.ReadMedicalHistory(null, null, null, null); @@ -39,20 +41,22 @@ public class ChartReport .Where(mh => mh.VisitDate.Date == dateTime.Date) .Where(mh => mh.DrugMedHistory != null && mh.DrugMedHistory.Any()) .ToList(); + var groupedData = filteredHistories .GroupBy( - mh => mh.DoctorId, - (key, group) => new + mh => mh.DoctorName, + (doctorName, group) => new { - DoctorId = key, + DoctorName = doctorName, TotalDrugs = group .SelectMany(mh => mh.DrugMedHistory) .Sum(dmh => dmh.Count) } ) .ToList(); + var result = groupedData - .Select(x => (x.DoctorId.ToString(), (double)x.TotalDrugs)) + .Select(x => (Caption: x.DoctorName, Value: (double)x.TotalDrugs)) .ToList(); return result; diff --git a/ProjectPolyclinic/ProjectPolyclinic/Reports/TableReport.cs b/ProjectPolyclinic/ProjectPolyclinic/Reports/TableReport.cs index f19ef1b..43ef390 100644 --- a/ProjectPolyclinic/ProjectPolyclinic/Reports/TableReport.cs +++ b/ProjectPolyclinic/ProjectPolyclinic/Reports/TableReport.cs @@ -1,6 +1,7 @@ using Microsoft.Extensions.Logging; using Newtonsoft.Json; using ProjectPolyclinic.Repositories; +using ProjectPolyclinic.Repositories.Implementations; namespace ProjectPolyclinic.Reports; @@ -11,12 +12,15 @@ public class TableReport private readonly IDoctorPayRepository _doctorPayRepository; + private readonly IDoctorRepository _doctorRepository; + private readonly ILogger _logger; internal static readonly string[] item = ["Id врача", "Дата", "Количество пациентов", "Выплаты"]; public TableReport(IDoctorPayRepository doctorPayRepository, - IMedicalHistoryRepository medicalHistoryRepository, ILogger logger) + IMedicalHistoryRepository medicalHistoryRepository, + IDoctorRepository doctorRepository, ILogger logger) { _doctorPayRepository = doctorPayRepository ?? throw new ArgumentNullException(nameof(IDoctorPayRepository)); @@ -24,26 +28,30 @@ public class TableReport _medicalHistoryRepository = medicalHistoryRepository ?? throw new ArgumentNullException(nameof(IMedicalHistoryRepository)); + _doctorRepository = doctorRepository ?? + throw new ArgumentNullException(nameof(IMedicalHistoryRepository)); + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } - public bool CreateTable(string filePath, int doctorId, DateTime startDate, DateTime endDate) { try { - // Приведение дат к началу и концу месяцев var startOfMonth = new DateTime(startDate.Year, startDate.Month, 1); var endOfMonth = new DateTime(endDate.Year, endDate.Month, 1).AddMonths(1).AddDays(-1); + // Получаем данные для таблицы и имя врача + var (tableData, doctorFullName) = GetData(doctorId, startOfMonth, endOfMonth); + var excelBuilder = new ExcelBuilder(filePath) .AddHeader("Сводка по оплате", 0, 3) - .AddParagraph($"За период с {startOfMonth:MM.yyyy} по {endOfMonth:MM.yyyy}", 0) - .AddParagraph($"Id врача: {doctorId}", 0) - .AddTable(new[] { 25, 25, 25 }, GetData(doctorId, startOfMonth, endOfMonth)); + .AddParagraph($"За период с {startOfMonth:MMMM yyyy} по {endOfMonth:MMMM yyyy}", 0) + .AddParagraph($"Врач: {doctorFullName}", 0) + .AddTable(new[] { 25, 25, 25 }, tableData); excelBuilder.AddParagraph("", 0); @@ -61,8 +69,7 @@ public class TableReport } } - - private List GetData(int doctorId, DateTime startOfMonth, DateTime endOfMonth) + private (List, string) GetData(int doctorId, DateTime startOfMonth, DateTime endOfMonth) { var data = _doctorPayRepository .ReadDoctorPayments() @@ -73,50 +80,54 @@ public class TableReport }) .Select(x => new { + DoctorName = x.DoctorName, // Предполагается, что есть поле DoctorName Date = DateTime.ParseExact(x.Month, "yyyy-MM", System.Globalization.CultureInfo.InvariantCulture), CountOfPatients = x.Count_Patient, Payments = x.Payment }) - .OrderBy(x => x.Date); + .OrderBy(x => x.Date) + .ToList(); - var result = new List + var doctorName = data.FirstOrDefault()?.DoctorName ?? "Неизвестный врач"; + + var result = new List() { new[] { "Дата", "Количество пациентов", "Выплаты" } } - .Union(data.Select(x => new string[] - { - x.Date.ToString("MMMM yyyy"), - x.CountOfPatients.ToString(), - x.Payments.ToString() - })) - .Union(new[] - { - new string[] - { - "Всего", - data.Sum(x => x.CountOfPatients).ToString(), - data.Sum(x => x.Payments).ToString() - } - }) + .Union( + data.Select(x => new string[] + { + x.Date.ToString("MMMM yyyy"), + x.CountOfPatients.ToString(), + x.Payments.ToString() + }) + ) + .Union( + new[] + { + new string[] + { + "Всего", + data.Sum(x => x.CountOfPatients).ToString(), + data.Sum(x => x.Payments).ToString() + } + } + ) .ToList(); - return result; + return (result, doctorName); } private List GetDrugData(int doctorId, DateTime startOfMonth, DateTime endOfMonth) { var medicalHistories = _medicalHistoryRepository - .ReadMedicalHistory(startOfMonth, endOfMonth, null, doctorId) - .Where(mh => mh.DoctorId == doctorId && mh.VisitDate >= startOfMonth && mh.VisitDate <= endOfMonth) + .ReadMedicalHistory(startOfMonth, endOfMonth, doctorId, null) // Исправлено + .Where(mh => mh.DrugMedHistory != null && mh.DrugMedHistory.Any()) .ToList(); - var result = new List - { - new[] { "Дата", "Назначенные лекарства" } - }; + _logger.LogDebug("Полученные медицинские истории с лекарствами: {data}", JsonConvert.SerializeObject(medicalHistories)); - var sortedDrugData = medicalHistories - .Where(mh => mh.DrugMedHistory != null && mh.DrugMedHistory.Any()) + var drugData = medicalHistories .SelectMany(mh => mh.DrugMedHistory.Select(drug => new { Date = mh.VisitDate, @@ -125,17 +136,20 @@ public class TableReport .OrderBy(x => x.Date) .ToList(); - result.AddRange(sortedDrugData - .Select(item => new string[] - { - item.Date.ToString("dd.MM.yyyy"), - item.Count.ToString() - })); + var result = new List { new[] { "Дата", "Назначенные лекарства" } }; - var totalDrugs = sortedDrugData.Sum(x => x.Count); + result.AddRange(drugData.Select(item => new string[] + { + item.Date.ToString("dd MMMM yyyy"), + item.Count.ToString() + })); + var totalDrugs = drugData.Sum(x => x.Count); result.Add(new[] { "Всего", totalDrugs.ToString() }); return result; } + + + } \ No newline at end of file diff --git a/ProjectPolyclinic/ProjectPolyclinic/Repositories/IDoctorPayRepository.cs b/ProjectPolyclinic/ProjectPolyclinic/Repositories/IDoctorPayRepository.cs index a92bbfd..11ee5fe 100644 --- a/ProjectPolyclinic/ProjectPolyclinic/Repositories/IDoctorPayRepository.cs +++ b/ProjectPolyclinic/ProjectPolyclinic/Repositories/IDoctorPayRepository.cs @@ -9,7 +9,9 @@ namespace ProjectPolyclinic.Repositories; public interface IDoctorPayRepository { - IEnumerable ReadDoctorPayments(); + //IEnumerable ReadDoctorPayments(); + IEnumerable ReadDoctorPayments(int? doctorId = null, string month = null); + void CreateDoctorPayments(DoctorPay doctorPayments); } diff --git a/ProjectPolyclinic/ProjectPolyclinic/Repositories/IMedicalHistoryRepository.cs b/ProjectPolyclinic/ProjectPolyclinic/Repositories/IMedicalHistoryRepository.cs index e936dd8..33ab8e9 100644 --- a/ProjectPolyclinic/ProjectPolyclinic/Repositories/IMedicalHistoryRepository.cs +++ b/ProjectPolyclinic/ProjectPolyclinic/Repositories/IMedicalHistoryRepository.cs @@ -9,7 +9,8 @@ namespace ProjectPolyclinic.Repositories; public interface IMedicalHistoryRepository { - IEnumerable ReadMedicalHistory(DateTime? dateForm = null, DateTime? dateTo = null, int? DoctorId = null, int? PatientId = null); + IEnumerable ReadMedicalHistory(DateTime? dateForm = null, DateTime? dateTo = null, int? doctorId = null, int? patientId = null); + void CreateMedicalHistory(MedicalHistory medicalHistory); diff --git a/ProjectPolyclinic/ProjectPolyclinic/Repositories/Implementations/DoctorPayRepository.cs b/ProjectPolyclinic/ProjectPolyclinic/Repositories/Implementations/DoctorPayRepository.cs index de94e97..66ef436 100644 --- a/ProjectPolyclinic/ProjectPolyclinic/Repositories/Implementations/DoctorPayRepository.cs +++ b/ProjectPolyclinic/ProjectPolyclinic/Repositories/Implementations/DoctorPayRepository.cs @@ -52,26 +52,42 @@ public class DoctorPayRepository : IDoctorPayRepository } } - public IEnumerable ReadDoctorPayments() + public IEnumerable ReadDoctorPayments(int? doctorId = null, string month = null) { - - _logger.LogInformation("Получение всех объектов"); + _logger.LogInformation("Получение данных об оплатах врачам"); try { - using var connection = new - NpgsqlConnection(_connectionString.ConnectionString); - var querySelect = "SELECT * FROM doctor_pay"; - var professionalDevelopments = - connection.Query(querySelect); - _logger.LogDebug("Полученные объекты: {json}", - JsonConvert.SerializeObject(professionalDevelopments)); - return professionalDevelopments; + var builder = new QueryBuilder(); + + if (doctorId.HasValue) + { + builder.AddCondition("dp.IdDoctor = @doctorId"); + } + if (!string.IsNullOrEmpty(month)) + { + builder.AddCondition("dp.Month = @month"); + } + + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = $@" + SELECT dp.*, + CONCAT(d.First_Name, ' ', d.Last_Name) as DoctorName + FROM doctor_pay dp + LEFT JOIN Doctors d ON dp.IdDoctor = d.Id + {builder.Build()}"; + + var doctorPayments = connection.Query(querySelect, new { doctorId, month }); + + _logger.LogDebug("Полученные записи об оплатах: {json}", JsonConvert.SerializeObject(doctorPayments)); + + return doctorPayments; } catch (Exception ex) { - _logger.LogError(ex, "Ошибка при чтении объектов"); + _logger.LogError(ex, "Ошибка при чтении данных об оплатах врачей"); throw; } - } + + } \ No newline at end of file diff --git a/ProjectPolyclinic/ProjectPolyclinic/Repositories/Implementations/MedicalHistoryRepository.cs b/ProjectPolyclinic/ProjectPolyclinic/Repositories/Implementations/MedicalHistoryRepository.cs index cb838bf..83a4dc9 100644 --- a/ProjectPolyclinic/ProjectPolyclinic/Repositories/Implementations/MedicalHistoryRepository.cs +++ b/ProjectPolyclinic/ProjectPolyclinic/Repositories/Implementations/MedicalHistoryRepository.cs @@ -92,32 +92,88 @@ public class MedicalHistoryRepository : IMedicalHistoryRepository throw; } } - public IEnumerable ReadMedicalHistory(DateTime? dateForm = null, DateTime? dateTo = null, int? patientId = null, int? doctorId = null) + + public IEnumerable ReadMedicalHistory(DateTime? dateForm = null, DateTime? dateTo = null, int? doctorId = null, int? patientId = 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 mh.*, dmh.drug_id AS DrugId, dmh.count AS Count + + var querySelect = $@" + SELECT mh.*, + CONCAT(p.First_Name, ' ', p.Last_Name) AS PatientName, + CONCAT(d.First_Name, ' ', d.Last_Name) AS DoctorName, -- Исправлено + dmh.Id AS Id, dmh.drug_id AS DrugId, dmh.medical_history_id AS MedicalHistoryId, dmh.Count FROM medical_history mh - LEFT JOIN drug_med_history dmh ON mh.id = dmh.medical_history_id"; + LEFT JOIN drug_med_history dmh ON dmh.medical_history_id = mh.Id + LEFT JOIN patients p ON p.Id = mh.PatientId + LEFT JOIN doctors d ON d.Id = mh.DoctorId + {builder.Build()}"; - var tempHistories = connection.Query(querySelect); + _logger.LogDebug("SQL запрос: {query}", querySelect); + _logger.LogDebug("Параметры: dateForm = {dateForm}, dateTo = {dateTo}, doctorId = {doctorId}, patientId = {patientId}", + dateForm, dateTo, doctorId, patientId); - _logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(tempHistories)); + var medicalHistoryDict = new Dictionary>(); - return tempHistories.GroupBy(x => x.Id) - .Select(group => MedicalHistory.CreateEntity( - group.First(), - group.Select(d => DrugMedHistory.CreateEntity(0, d.DrugId, d.Id, d.Count)) - )).ToList(); + var medicalHistories = connection.Query( + querySelect, + (history, drug) => + { + if (!medicalHistoryDict.TryGetValue(history.Id, out var drugs)) + { + drugs = new List(); + medicalHistoryDict.Add(history.Id, drugs); + } + + if (drug != null && drug.Id > 0) + { + drugs.Add(DrugMedHistory.CreateEntity(drug.Id, drug.DrugId, drug.MedicalHistoryId, drug.Count)); + } + + return history; + }, + splitOn: "Id", + param: new { dateForm, dateTo, doctorId, patientId }); + + return medicalHistoryDict.Select(x => + { + var mh = medicalHistories.First(y => y.Id == x.Key); + mh.SetDrugMedHistory(x.Value); + return mh; + }).ToArray(); } catch (Exception ex) { - _logger.LogError(ex, "Ошибка при чтении объектов"); + _logger.LogError(ex, "Ошибка при чтении медицинских историй"); throw; } } + + + + + + } diff --git a/ProjectPolyclinic/ProjectPolyclinic/Repositories/Implementations/QueryBuilder.cs b/ProjectPolyclinic/ProjectPolyclinic/Repositories/Implementations/QueryBuilder.cs new file mode 100644 index 0000000..7882451 --- /dev/null +++ b/ProjectPolyclinic/ProjectPolyclinic/Repositories/Implementations/QueryBuilder.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ProjectPolyclinic.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}"; + } + +}