93 lines
3.7 KiB
C#
93 lines
3.7 KiB
C#
using Microsoft.Extensions.Logging;
|
||
using ProjectPolyclinic.Repositories;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Security.Policy;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace ProjectPolyclinic.Reports;
|
||
|
||
internal class TableReport
|
||
{
|
||
private readonly IDoctorRepository _doctorRepository;
|
||
private readonly IVisitingRepository _visitingRepository;
|
||
private readonly IMedicationRepository _medicationRepository;
|
||
|
||
private readonly ILogger<TableReport> _logger;
|
||
internal static readonly string[] item = ["Визит","Доктор", "Дата", "Количество визитов","Количество медикаментов"];
|
||
public TableReport(IDoctorRepository doctorRepository, IVisitingRepository visitingRepository, IMedicationRepository medicationRepository, ILogger<TableReport> logger)
|
||
{
|
||
_doctorRepository = doctorRepository ?? throw new ArgumentNullException(nameof(doctorRepository));
|
||
_visitingRepository = visitingRepository ?? throw new ArgumentNullException(nameof(visitingRepository));
|
||
_medicationRepository = medicationRepository ?? throw new ArgumentNullException(nameof(medicationRepository));
|
||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||
}
|
||
|
||
public bool CreateTable(string filePath, int doctorId, DateTime startDate, DateTime endDate)
|
||
{
|
||
try
|
||
{
|
||
var data = GetData(doctorId, startDate, endDate);
|
||
new ExcelBuilder(filePath)
|
||
.AddHeader("Сводка по движению визитов", 0, 2)
|
||
.AddParagraph("за период", 0)
|
||
.AddTable([10, 10,15, 15,15], data)
|
||
.Build();
|
||
return true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError(ex, "Ошибка при формировании документа");
|
||
return false;
|
||
}
|
||
}
|
||
private List<string[]> GetData(int doctorId, DateTime startDate, DateTime endDate)
|
||
{
|
||
/*var visits = _visitingRepository.ReadVisiting()
|
||
.Where(x => x.VisitingTime >= startDate && x.VisitingTime <= endDate && x.DoctorId == doctorId)
|
||
.Select(x => new
|
||
{
|
||
DoctorName = $"{_doctorRepository.ReadDoctorById(x.DoctorId)?.First_Name} {_doctorRepository.ReadDoctorById(x.DoctorId)?.Last_Name}",
|
||
Date = x.VisitingTime
|
||
})
|
||
.OrderBy(x => x.Date)
|
||
.ToList();
|
||
|
||
|
||
var result = new List<string[]> { item }
|
||
.Union(visits.Select(x => new string[]
|
||
{
|
||
x.DoctorName,
|
||
x.Date.ToString("g"),
|
||
"1"
|
||
}))
|
||
.Union([["Всего", " ", visits.Count().ToString()]])
|
||
.ToList();
|
||
|
||
|
||
return result;*/
|
||
var data = _visitingRepository
|
||
.ReadVisiting()
|
||
.Where(x => x.VisitingTime >= startDate && x.VisitingTime <= endDate && x.DoctorId == doctorId)
|
||
.Select(x => new
|
||
{
|
||
x.Id,
|
||
x.DoctorId,
|
||
Date = x.VisitingTime,
|
||
CountOfVisits = 1,
|
||
Medication = x.Medicines.Sum(m => m.Count)
|
||
})
|
||
.OrderBy(x => x.Date);
|
||
return
|
||
new List<string[]>() { item }
|
||
.Union(
|
||
data
|
||
.Select(x => new string[] { x.Id.ToString(), x.DoctorId.ToString(), x.Date.ToString(), x.CountOfVisits.ToString() ?? string.Empty, x.Medication.ToString() ?? string.Empty }))
|
||
.Union(
|
||
[["Всего ", "","", data.Sum(x => x.CountOfVisits).ToString(), data.Sum(x => x.Medication).ToString()]])
|
||
|
||
.ToList();
|
||
}
|
||
} |