135 lines
5.9 KiB
C#
Raw Normal View History

using UniversityContracts.BindingModels;
using UniversityContracts.BusinessLogicContracts;
using UniversityContracts.ViewModels;
using UniversityContracts.SearchModels;
using UniversityContracts.StoragesContracts;
using CarDealershipBusinessLogic.BusinessLogic.OfficePackage;
2023-04-08 20:16:42 +04:00
using UniversityBusinessLogic.OfficePackage;
using System.Reflection.PortableExecutable;
using DocumentFormat.OpenXml.InkML;
using DocumentFormat.OpenXml.Wordprocessing;
namespace UniversityBusinessLogic.BusinessLogics
{
public class ReportProviderLogic : IReportProviderLogic
{
private readonly IDocumentStorage _documentStorage;
2023-04-08 20:16:42 +04:00
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;
2023-04-08 20:16:42 +04:00
public ReportProviderLogic(IDocumentStorage documentStorage,
IStudentStorage studentStorage,
IEducationStatusStorage educationStatusStorage,
IEducationGroupStorage educationGroupStorage,
IDisciplineStorage disciplineStorage,
IStreamStorage streamStorage,
WordBuilderProvider wordBuilder,
ExcelBuilderProvider excelBuilder,
PdfBuilderProvider pdfBuilder)
2023-04-08 20:16:42 +04:00
{
_documentStorage = documentStorage;
_studentStorage = studentStorage;
_educationStatusStorage = educationStatusStorage;
_educationGroupStorage = educationGroupStorage;
_disciplineStorage = disciplineStorage;
_streamStorage = streamStorage;
this.wordBuilder = wordBuilder;
this.excelBuilder = excelBuilder;
this.pdfBuilder = pdfBuilder;
2023-04-08 20:16:42 +04:00
}
2023-04-08 23:59:00 +04:00
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()
2023-04-08 20:16:42 +04:00
{
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.GetFilteredList(new StreamSearchModel()
{
UserId = model.UserId,
});
foreach (var stream in streams)
{
ReportStreamStudentEdStatPeriodViewModel reportData = new ReportStreamStudentEdStatPeriodViewModel();
reportData.StreamName = stream.Name;
var students = _streamStorage.GetStreamStudents(new() { Id = stream.Id })
.Select(s => new StudentStatusViewModel()
{
StudentName = s.Name + " " + s.Surname,
EducationStatus = s.EducationStatusName
})
.ToList();
reportData.StudentStatus = students;
reportRecords.Add(reportData);
}
return reportRecords;
}
public byte[] SaveListFile(StudentDisciplineListBindingModel model)
{
byte[] file = Array.Empty<byte>();
if (model.FileType == "docx")
{
wordBuilder.CreateDocument();
wordBuilder.CreateTitle("Список комплектаций по выбранным покупкам");
wordBuilder.CreateStudentsDisciplineTable(GetStudentsDiscipline(model.Students));
file = wordBuilder.GetFile();
}
else if (model.FileType == "xlsx")
{
excelBuilder.CreateDocument();
excelBuilder.CreateTitle("Список комплектаций по выбранным покупкам");
excelBuilder.CreateStudentsDisciplineTable(GetStudentsDiscipline(model.Students));
file = excelBuilder.GetFile();
}
return file;
}
public void SendByMailEducationStatusReport(ReportBindingModel reportModel)
{
byte[] file = pdfBuilder.GetEquipmentReportFile(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
}); */
}
}
}