PIbd-21_Pyatakov_KM_Markov_.../UniversityBusinessLogic/BusinessLogics/ReportProviderLogic.cs

139 lines
6.0 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UniversityContracts.BindingModels;
using UniversityContracts.BusinessLogicContracts;
using UniversityContracts.ViewModels;
using UniversityContracts.SearchModels;
using UniversityContracts.StoragesContracts;
using UniversityBusinessLogic.OfficePackage;
using System.Reflection.PortableExecutable;
using DocumentFormat.OpenXml.InkML;
using DocumentFormat.OpenXml.Wordprocessing;
using UniversityBusinessLogic.BusinessLogic.OfficePackage;
namespace UniversityBusinessLogic.BusinessLogics
{
public class ReportProviderLogic : IReportProviderLogic
{
private readonly IDocumentStorage _documentStorage;
private readonly IStudentStorage _studentStorage;
private readonly IEducationStatusStorage _educationStatusStorage;
private readonly IEducationGroupStorage _educationGroupStorage;
private readonly IDisciplineStorage _disciplineStorage;
private readonly IStreamStorage _streamStorage;
private readonly WordBuilderProvider _wordBuilder;
private readonly ExcelBuilderProvider _excelBuilder;
private readonly PdfBuilderProvider _pdfBuilder;
private readonly MailSender _mailSender;
public ReportProviderLogic(IDocumentStorage documentStorage,
IStudentStorage studentStorage,
IEducationStatusStorage educationStatusStorage,
IEducationGroupStorage educationGroupStorage,
IDisciplineStorage disciplineStorage,
IStreamStorage streamStorage,
WordBuilderProvider wordBuilder,
ExcelBuilderProvider excelBuilder,
PdfBuilderProvider pdfBuilder,
MailSender mailSender)
{
_documentStorage = documentStorage;
_studentStorage = studentStorage;
_educationStatusStorage = educationStatusStorage;
_educationGroupStorage = educationGroupStorage;
_disciplineStorage = disciplineStorage;
_streamStorage = streamStorage;
_wordBuilder = wordBuilder;
_excelBuilder = excelBuilder;
_pdfBuilder = pdfBuilder;
_mailSender = mailSender;
}
public List<ReportStudentsDisciplineViewModel> GetStudentsDiscipline(List<StudentViewModel> students)
{
var reportRecords = new List<ReportStudentsDisciplineViewModel>();
foreach (var student in students)
{
var disciplines = _studentStorage.GetStudentStreams(new() { Id = student.Id })
.SelectMany(stream => _streamStorage.GetStreamDisciplines(new() { Id = stream.Id }))
.Select(discipline => discipline.Name)
.ToList();
ReportStudentsDisciplineViewModel reportRecord = new()
{
Student = student.Name + " " + student.Surname + ", " + student.StudentCard,
Disciplines = disciplines
};
reportRecords.Add(reportRecord);
}
return reportRecords;
}
public List<ReportStreamStudentEdStatPeriodViewModel> GetStreamStudentEdStatPeriod(ReportBindingModel model)
{
List<ReportStreamStudentEdStatPeriodViewModel> reportRecords = new List<ReportStreamStudentEdStatPeriodViewModel>();
var streams = _streamStorage.GetFullList();
foreach (var stream in streams)
{
ReportStreamStudentEdStatPeriodViewModel reportData = new ReportStreamStudentEdStatPeriodViewModel();
reportData.StreamName = stream.Name;
var students = _streamStorage.GetStreamStudents(new() { Id = stream.Id, DateFrom = model.DateFrom, DateTo = model.DateTo })
.Select(s => new StudentStatusViewModel()
{
StudentName = s.Name + " " + s.Surname,
DateOfAddmission = s.DateOfAddmission,
EducationStatus = s.EducationStatusName
})
.ToList();
reportData.StudentStatus = students;
reportRecords.Add(reportData);
}
return reportRecords;
}
public byte[] SaveListFile(StudentDisciplineListBindingModel model)
{
byte[] file = Array.Empty<byte>();
string title = "Список дисциплин по выбранным студентам";
if (model.FileType == "docx")
{
_wordBuilder.CreateDocument();
_wordBuilder.CreateTitle(title);
_wordBuilder.CreateStudentsDisciplineTable(GetStudentsDiscipline(model.Students));
file = _wordBuilder.GetFile();
}
else if (model.FileType == "xlsx")
{
_excelBuilder.CreateDocument();
_excelBuilder.CreateTitle(title);
_excelBuilder.CreateStudentsDisciplineTable(GetStudentsDiscipline(model.Students));
file = _excelBuilder.GetFile();
}
return file;
}
public void SendByMailStatusReport(ReportBindingModel reportModel)
{
byte[] file = _pdfBuilder.GetEducationStatusReportFile(new()
{
Title = "Отчет по статусам обучения",
DateFrom = reportModel.DateFrom,
DateTo = reportModel.DateTo,
Records = GetStreamStudentEdStatPeriod(reportModel)
});
_mailSender.SendMailAsync(new ()
{
MailAddress = reportModel.UserEmail,
Subject = "Отчет по статусам обучения",
Text = $"За период с {reportModel.DateFrom.ToShortDateString()} " +
$"по {reportModel.DateTo.ToShortDateString()}.",
File = file
});
}
}
}