using BusinessLogic.BusinessLogic; using BusinessLogic.MailWorker; using BusinessLogic.OfficePackage; using Contracts.BindingModels; using Contracts.BusinessLogicsContracts; using Contracts.SearchModels; using Contracts.ViewModels; using DocumentFormat.OpenXml.Bibliography; namespace GuarantorAPP { public class GuarantorData { private readonly ILogger _logger; private readonly IGuarantorLogic _guarantorLogic; private readonly IWorkerLogic _workerLogic; private readonly IMachineLogic _machineLogic; private readonly IWorkshopLogic _workshopLogic; private readonly IProductionLogic _productionLogic; private readonly IProductLogic _productLogic; private readonly AbstractSaveToExcelGuarantor _excel; private readonly AbstractSaveToWordGuarantor _word; private readonly AbstractSaveToPdfGuarantor _pdf; private readonly AbstractMailWorker _mail; public GuarantorData(ILogger logger, IGuarantorLogic guarantorLogic, IWorkerLogic workerLogic, IMachineLogic machineLogic, IWorkshopLogic workshopLogic, IProductionLogic productionLogic, IProductLogic productLogic, AbstractSaveToExcelGuarantor excel, AbstractSaveToPdfGuarantor pdf, AbstractSaveToWordGuarantor word, AbstractMailWorker mail) { _logger = logger; _guarantorLogic = guarantorLogic; _workerLogic = workerLogic; _machineLogic = machineLogic; _workshopLogic = workshopLogic; _productionLogic = productionLogic; _productLogic = productLogic; _excel = excel; _pdf = pdf; _mail = mail; _word = word; } public GuarantorViewModel? Login(string login, string password) { return _guarantorLogic.ReadElement(new() { Login = login, Password = password }); } public bool Register(GuarantorBindingModel model) { return _guarantorLogic.Create(model); } public bool UpdateUser(GuarantorBindingModel model) { return _guarantorLogic.Update(model); } public bool CheckLogin(string login) { return _guarantorLogic.ReadElement(new() { Login = login }) == null; } public List? GetWorkers(int userId) { return _workerLogic.ReadList(new WorkerSearchModel() { UserId = userId }); } public bool DeleteWorker(int workerId) { return _workerLogic.Delete(new() { Id = workerId }); } public bool CreateWorker(WorkerBindingModel model) { return _workerLogic.Create(model); } public bool UpdateWorker(WorkerBindingModel model) { return _workerLogic.Update(model); } public WorkerViewModel? GetWorker(int id) { return _workerLogic.ReadElement(new() { Id = id }); } public bool CheckWorkerName(string name) { return _workerLogic.ReadElement(new() { Name = name }) == null; } public List? GetWorkshops(int userId) { return _workshopLogic.ReadList(new WorkshopSearchModel() { UserId = userId }); } public WorkshopViewModel? GetWorkshop(int id) { return _workshopLogic.ReadElement(new() { Id = id }); } public bool UpdateWorkshop(WorkshopBindingModel model) { return _workshopLogic.Update(model); } public bool DeleteWorkshop(int workshopId) { return _workshopLogic.Delete(new() { Id = workshopId }); } public bool CreateWorkshop(WorkshopBindingModel model) { return _workshopLogic.Create(model); } public bool CheckWorkshopTitle(string title) { return _workshopLogic.ReadElement(new() { Title = title }) == null; } public List? GetMachines(int userId) { return _machineLogic.ReadList(new() { UserId = userId }); } public MachineViewModel? GetMachine(int id) { return _machineLogic.ReadElement(new() { Id = id }); } public bool CreateMachine(MachineBindingModel model) { return _machineLogic.Create(model); } public bool UpdateMachine(MachineBindingModel model) { return _machineLogic.Update(model); } public bool DeleteMachine(int machineId) { return _machineLogic.Delete(new() { Id = machineId }); } public bool CheckMachineTitle(string title) { return _machineLogic.ReadElement(new() { Title = title }) == null; } public List? GetProductions() { return _productionLogic.ReadList(null); } public List GetTimeReport(DateTime? startDate, DateTime? endDate, int UserId) { var workshops = _workshopLogic.ReadList(new() { DateFrom = startDate, DateTo = endDate, UserId = UserId }); if (workshops == null) return new(); List machineWorkshopTimeReports = new List(); foreach (var workshop in workshops) { var report = new MachineWorkshopTimeReport(); report.WorkshopName = workshop.Title; var machines = _machineLogic.ReadList(new() { WorkshopId = workshop.Id, UserId = UserId }); if (machines != null) report.Machines = machines.Select(p => p.Title).ToList(); machineWorkshopTimeReports.Add(report); } return machineWorkshopTimeReports; } public List? GetProductReports(List workers) { List reports = new(); foreach (int i in workers) { WorkerProductReportViewModel report = new(); var worker = _workerLogic.ReadElement(new() { Id = i }); report.WorkerName = worker!.Name; var products = _productLogic.ReadList(new() { WorkerId = i }); if (products != null) report.Products = products.Select(x => x.Name).ToList(); reports.Add(report); } return reports; } public void SaveReportExcel(List workers, MemoryStream stream) { var reports = GetProductReports(workers); if (reports == null) { return; } int maxsize = 0; foreach (var report in reports) { maxsize = Math.Max(maxsize, report.Products.Count); } _excel.CreateReport(new() { workerProducts = reports, Title = "Отчет Работник - Изделие", memoryStream = stream, maxleng = maxsize }); } public void SaveReportWord(List workers, MemoryStream stream) { var reports = GetProductReports(workers); if (reports == null) { return; } _word.CreateDoc(new() { memoryStream = stream, Title = "Отчет Работник - изделие", Products = reports, }); } public void SendMailReport(DateTime? startDate, DateTime? endDate, int UserId, MemoryStream stream) { var reports = GetTimeReport(startDate, endDate, UserId); if (reports == null) { return; } _pdf.CreateDoc(new() { DateFrom = startDate!.Value, DateTo = endDate!.Value, FileName = stream, Reports = reports, Title = "Отчет" }); byte[] report = stream.GetBuffer(); _mail.MailSendAsync(new() { MailAddress = UserGuarantor.user!.Email, Subject = "Отчет", FileName = "PdfReport.pdf", Pdf = report }); } } }