поменяю

This commit is contained in:
Bulat 2024-12-19 14:42:11 +04:00
parent 2fc3852943
commit 0046fdf9b7
7 changed files with 153 additions and 43 deletions

View File

@ -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> DrugMedicalHistory { get; set; } = [];

View File

@ -20,20 +20,24 @@ internal class TableReport
private readonly IDoctorPaymentsRepository _doctorPaymentsRepository;
private readonly IDrugRepository _drugRepository;
private readonly ILogger<TableReport> _logger;
internal static readonly string[] item = [ата", "Описание", "Количество лекарства"];
internal static readonly string[] item = [октор", "Описание", "Количество пациентов"];
public TableReport(IMedicalHistoryRepository medicalHistoryRepository, IDoctorPaymentsRepository doctorPaymentsRepository, ILogger<TableReport> logger)
public TableReport(IMedicalHistoryRepository medicalHistoryRepository, IDoctorPaymentsRepository doctorPaymentsRepository, IDrugRepository drugRepository, ILogger<TableReport> 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<string[]> 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<string[]>() { 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<string[]>() { new string[] { "Всего", data.Sum(x => x.CountOut ?? 0).ToString(), string.Empty } })
.ToList();
}
*/
/*
private List<string[]> 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<string[]>() { item }
.Union(
data
.Select(x => new string[] { x.Date.ToString(), x.CountIn ?? string.Empty, x.CountOut?.ToString() ?? string.Empty }))
.Union(
new List<string[]>() { new string[] { "Всего", data.Sum(x => x.CountOut ?? 0).ToString(), string.Empty } })
.ToList();
}
*/
private List<string[]> 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<string[]>() { item }
.Union(
data
.Select(x => new string[] { x.Date.ToString(), x.CountIn ?? string.Empty, x.CountOut?.ToString() ?? string.Empty }))
.Union(
new List<string[]>() { new string[] { "Всего", string.Empty, data.Sum(x => x.CountOut ?? 0).ToString() } })
.ToList();
}
}

View File

@ -9,8 +9,8 @@ namespace RegistrationPatientsPolyclinic.Repositories;
public interface IDoctorPaymentsRepository
{
IEnumerable<DoctorPayments> ReadDoctorPayments();
//IEnumerable<DoctorPayments> ReadDoctorPayments();
IEnumerable<DoctorPayments> ReadDoctorPayments(int? doctorId = null, string month = null);
void CreateDoctorPayments(DoctorPayments doctorPayments);

View File

@ -13,12 +13,6 @@ public interface IMedicalHistoryRepository
IEnumerable<MedicalHistory> ReadMedicalHistory(DateTime? dateForm = null, DateTime? dateTo = null, int? PatientId = null,
int? DoctorId = null); // по этим параметрам можно не весь список читать, а только какую то часть
/*
IEnumerable<TempDrugMedicalHistory> ReadMedicalHistory(DateTime? dateForm = null, DateTime? dateTo = null, int? PatientId = null,
int? DoctorId = null); // по этим параметрам можно не весь список читать, а только какую то часть
*/
void CreateMedicalHistory(MedicalHistory medicalHistory); // объекь будет заносится в список

View File

@ -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<DoctorPayments> ReadDoctorPayments()
public IEnumerable<DoctorPayments> 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<DoctorPayments>(querySelect);
connection.Query<DoctorPayments>(querySelect, new { doctorId , month });
_logger.LogDebug("Полученные объекты: {json}",
JsonConvert.SerializeObject(doctorPayments));
return doctorPayments;

View File

@ -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<MedicalHistory> 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<int, List<DrugMedicalHistory>>();
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;
}
}
}

View File

@ -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}";
}
}