Case_accounting/CaseAccounting/CaseAccountingBusinessLogics/BusinessLogics/ReportProviderLogic.cs
2023-05-20 00:24:04 +04:00

145 lines
6.3 KiB
C#
Raw 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 CaseAccountingBusinessLogic.OfficePackage;
using CaseAccountingContracts.BindingModels;
using CaseAccountingContracts.BusinessLogicContracts;
using CaseAccountingContracts.SearchModels;
using CaseAccountingContracts.StoragesContracts;
using CaseAccountingContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CaseAccountingBusinessLogic.BusinessLogics
{
public class ReportProviderLogic : IReportProviderLogic
{
private readonly ICaseStorage _caseStorage;
private readonly ISpecializationStorage _specializationStorage;
private readonly IHearingStorage _hearingStorage;
private readonly ILawyerStorage _lawyerStorage;
private readonly WordBuilderProvider _wordBuilder;
private readonly ExcelBuilderProvider _excelBuilder;
/*private readonly PdfBuilderProvider _pdfBuilder;
private readonly MailSender _mailSender;*/
public ReportProviderLogic(ICaseStorage caseStorage, ISpecializationStorage specializationStorage, IHearingStorage hearingStorage, ILawyerStorage lawyerStorage, WordBuilderProvider wordBuilder, ExcelBuilderProvider excelBuilder)
{
_caseStorage = caseStorage ?? throw new ArgumentNullException(nameof(caseStorage));
_specializationStorage = specializationStorage ?? throw new ArgumentNullException(nameof(specializationStorage));
_hearingStorage = hearingStorage ?? throw new ArgumentNullException(nameof(hearingStorage));
_lawyerStorage = lawyerStorage ?? throw new ArgumentNullException(nameof(lawyerStorage));
_wordBuilder = wordBuilder ?? throw new ArgumentNullException(nameof(wordBuilder));
_excelBuilder = excelBuilder ?? throw new ArgumentNullException(nameof(excelBuilder));
}
public List<ReportCaseSpecializationViewModel> GetCaseSpecialization(List<CaseViewModel> models)
{
List<ReportCaseSpecializationViewModel> list = new();
foreach (var model in models)
{
var specialization = _specializationStorage.GetElement(new SpecializationSearchModel { Id = model.SpecializationId });
var caseModel = _caseStorage.GetElement(new CaseSearchModel { Id = model.Id });
if (specialization == null)
{
throw new Exception("Некоректные данные по специализации");
}
if (caseModel == null)
{
throw new Exception("Некоректные данные по делу");
}
bool hasSpec = false;
if (list.Count > 0)
{
foreach (var report in list)
{
if (hasSpec = report.Specialization.Equals(specialization.Name))
{
report.Cases.Add("Дело #" + caseModel.Id.ToString());
break;
}
}
}
if (!hasSpec)
{
var newElement = new ReportCaseSpecializationViewModel
{
Specialization = specialization.Name,
Cases = new()
};
newElement.Cases.Add("Дело #" + caseModel.Id.ToString());
list.Add(newElement);
}
}
return list;
}
public List<ReportHearingLawyerViewModel> GetHearingLawyer(ReportBindingModel model)
{
/*var hearings = _hearingStorage
.GetFilteredList(new HearingSearchModel { UserId = model.UserId})
.Where(x => model.DateFrom <= x.Date && model.DateTo >= x.Date);
var list = new List<ReportHearingLawyerViewModel>();
foreach (var hearing in hearings)
{
var record = new ReportHearingLawyerViewModel
{
Information = hearing.Information,
Date = hearing.Date,
Lawyers = new List<(string Surname, string Name, string Patronymic)>()
};
foreach (var lawyer in _caseStorage.GetElement(new CaseSearchModel { Id = hearing.CaseId }).Lawyers.Values)
{
record.Lawyers.Add((lawyer.Surname, lawyer.Name, lawyer.Patronymic));
}
list.Add(record);
}
return list;*/
return new();
}
public byte[] SaveListFile(CaseSpecializationListBindingModel model)
{
byte[] file = Array.Empty<byte>();
string title = "Список дисциплин по выбранным студентам";
if (model.FileType == "docx")
{
_wordBuilder.CreateDocument();
_wordBuilder.CreateTitle(title);
_wordBuilder.CreateCaseSpecializationTable(GetCaseSpecialization(model.Cases));
file = _wordBuilder.GetFile();
}
else if (model.FileType == "xlsx")
{
_excelBuilder.CreateDocument();
_excelBuilder.CreateTitle(title);
_excelBuilder.CreateCaseSpecializationTable(GetCaseSpecialization(model.Cases));
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
});
}*/
}
}