using PdfSharp.Pdf; using ServiceStationBusinessLogic.OfficePackage; using ServiceStationBusinessLogic.OfficePackage.HelperModels; using ServiceStationContracts.BindingModels; using ServiceStationContracts.SearchModels; using ServiceStationContracts.ViewModels; using ServiceStationContracts.StorageContracts; using ServiceStationContracts.BusinessLogic; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ServiceStationBusinessLogic.BusinessLogic { public class ReportLogic : IReportLogic { private readonly IWorkStorage _workStorage; private readonly ITaskStorage _taskStorage; private readonly IClientStorage _clientStorage; private readonly AbstractSaveToExcel _saveToExcel; private readonly AbstractSaveToPdf _saveToPdf; private readonly AbstractSaveToWord _saveToWord; public ReportLogic(IWorkStorage workStorage, ITaskStorage taskStorage, IClientStorage clientStorage, AbstractSaveToExcel saveToExcel, AbstractSaveToPdf saveToPdf, AbstractSaveToWord saveToWord) { _workStorage = workStorage; _taskStorage = taskStorage; _clientStorage = clientStorage; _saveToExcel = saveToExcel; _saveToPdf = saveToPdf; _saveToWord = saveToWord; } public List GetClientListInWorks(ReportBindeingModel model) { List client_list_in_works = new(); foreach (int id in model.ids) { var work = _workStorage.GetElement(new WorkSearchModel { Id = id }) ?? throw new Exception("Ошибка получения данных"); var record = new ReportClientsInWorksViewModel { WorkId = work.Id, Date = work.Date.ToShortDateString(), Price = work.Price, TotalCount = 0, _Clients = new() }; foreach (var client in work.ClientList) { record._Clients.Add(new(client.Key, client.Value.FIO)); record.TotalCount++; } client_list_in_works.Add(record); } return client_list_in_works; } public byte[]? SaveToExcelFile(ReportBindeingModel model) { var document = _saveToExcel.CreateReport(new ExcelInfo { Title = "Список клиентов по выбранным работам", WorksClients = GetClientListInWorks(model) }); return document; } public PdfDocument SaveToPdfFile(ReportBindeingModel model) { var works = _workStorage.GetFilteredList(new WorkSearchModel { DateFrom = model.DateFrom, DateTo = model.DateTo }); var record = new PdfInfo { Title = "Список работ и назначенных заданий", Date_From = model.DateFrom.ToShortDateString(), Date_To = model.DateTo.ToShortDateString(), WorkTask = new(), TotalCount = 0, }; foreach (var work in works) { var task = _taskStorage.GetElement(new TaskSearchModel { Id = work.TaskId }) ?? throw new Exception("Ошибка получения данных"); record.WorkTask.Add(new(work.Id, work.Date.ToShortDateString(), work.Price, task.Id, task.Name)); record.TotalCount++; } var document = _saveToPdf.CreateDoc(record); return document; } public byte[]? SaveToWordFile(ReportBindeingModel model) { var document = _saveToWord.CreateDoc(new WordInfo { Title = "Список клиентов по выбранным работам", WorksClients = GetClientListInWorks(model) }); return document; } } }