diff --git a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/ReportLogic.cs b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/ReportLogic.cs new file mode 100644 index 0000000..3087dbb --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/ReportLogic.cs @@ -0,0 +1,126 @@ +using BlacksmithWorkshopBusinessLogic.OfficePackage; +using BlacksmithWorkshopBusinessLogic.OfficePackage.HelperModels; +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.BusinessLogicsContracts; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopContracts.StoragesContracts; +using BlacksmithWorkshopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopBusinessLogic.BusinessLogic +{ + //Реализация бизнес-логики отчётов + public class ReportLogic : IReportLogic + { + private readonly IWorkPieceStorage _workPieceStorage; + + private readonly IManufactureStorage _manufactureStorage; + + private readonly IOrderStorage _orderStorage; + + private readonly AbstractSaveToExcel _saveToExcel; + + private readonly AbstractSaveToWord _saveToWord; + + private readonly AbstractSaveToPdf _saveToPdf; + + //инициализируем поля класса через контейнер + public ReportLogic(IManufactureStorage manufactureStorage, IWorkPieceStorage + workPieceStorage, IOrderStorage orderStorage, AbstractSaveToExcel saveToExcel, + AbstractSaveToWord saveToWord, AbstractSaveToPdf saveToPdf) + { + _manufactureStorage = manufactureStorage; + _workPieceStorage = workPieceStorage; + _orderStorage = orderStorage; + _saveToExcel = saveToExcel; + _saveToWord = saveToWord; + _saveToPdf = saveToPdf; + } + + //Получение списка компонент с указанием, в каких изделиях используются + public List GetManufactureWorkPiece() + { + var workPieces = _workPieceStorage.GetFullList(); + var manufactures = _manufactureStorage.GetFullList(); + + var list = new List(); + + foreach (var workPiece in workPieces) + { + var record = new ReportManufactureWorkPieceViewModel + { + WorkPieceName = workPiece.WorkPieceName, + Manufactures = new List<(string, int)>(), + TotalCount = 0 + }; + + foreach (var manufacture in manufactures) + { + if (manufacture.ManufactureWorkPieces.ContainsKey(workPiece.Id)) + { + record.Manufactures.Add(new (manufacture.ManufactureName, manufacture.ManufactureWorkPieces[workPiece.Id].Item2)); + record.TotalCount += manufacture.ManufactureWorkPieces[workPiece.Id].Item2; + } + } + + list.Add(record); + } + + return list; + } + + //Получение списка заказов за определенный период + public List GetOrders(ReportBindingModel model) + { + return _orderStorage.GetFilteredList(new OrderSearchModel { DateFrom = model.DateFrom, DateTo = model.DateTo }) + .Select(x => new ReportOrdersViewModel + { + Id = x.Id, + DateCreate = x.DateCreate, + ManufactureName = x.ManufactureName, + Sum = x.Sum + }) + .ToList(); + } + + //Сохранение заготовок в файл-Word + public void SaveWorkPiecesToWordFile(ReportBindingModel model) + { + _saveToWord.CreateDoc(new WordInfo + { + FileName = model.FileName, + Title = "Список заготовок", + WorkPieces = _workPieceStorage.GetFullList() + }); + + } + + //Сохранение заготовок с указаеним продуктов в файл-Excel + public void SaveManufactureWorkPieceToExcelFile(ReportBindingModel model) + { + _saveToExcel.CreateReport(new ExcelInfo + { + FileName = model.FileName, + Title = "Список заготовок", + ManufactureWorkPieces = GetManufactureWorkPiece() + }); + } + + //Сохранение заказов в файл-Pdf + public void SaveOrdersToPdfFile(ReportBindingModel model) + { + _saveToPdf.CreateDoc(new PdfInfo + { + FileName = model.FileName, + Title = "Список заказов", + DateFrom = model.DateFrom!.Value, + DateTo = model.DateTo!.Value, + Orders = GetOrders(model) + }); + } + } +}