using DocumentFormat.OpenXml.Office.CustomUI; using ElectronicsShopBusinessLogic.OfficePackage; using ElectronicsShopBusinessLogic.OfficePackage.HelperModels; using ElectronicsShopContracts.BindingModels; using ElectronicsShopContracts.BusinessLogicContracts; using ElectronicsShopContracts.SearchModels; using ElectronicsShopContracts.StorageContracts; using ElectronicsShopContracts.ViewModels; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ElectronicsShopBusinessLogic.BusinessLogic { public class ReportEmployeeLogic : IReportEmployeeLogic { private readonly IProductStorage _productStorage; private readonly IPaymeantStorage _paymeantStorage; private readonly IOrderStorage _orderStorage; private readonly AbstractSaveToWordEmployee _saveToWord; private readonly AbstractSaveToExcelEmployee _saveToExcel; public ReportEmployeeLogic(AbstractSaveToExcelEmployee abstractSaveToExcelEmployee, AbstractSaveToWordEmployee abstractSaveToWordEmployee, IProductStorage productStorage, IPaymeantStorage paymeantStorage, IOrderStorage orderStorage) { _productStorage = productStorage; _paymeantStorage = paymeantStorage; _orderStorage = orderStorage; _saveToExcel = abstractSaveToExcelEmployee; _saveToWord = abstractSaveToWordEmployee; } public List GetProducts(ReportProductBindingModel model) { var paymeants = _paymeantStorage.GetFillteredList(new PaymeantSearchModel { DateFrom = model.DateFrom, DateTo = model.DateTo, }); List paymeantProductList = new(); foreach (var paymeant in paymeants) { var order = _orderStorage.GetElement(new OrderSearchModel { ID = paymeant.OrderID }) ?? throw new Exception("Ошибка получения данных"); foreach (var product in order.ProductList) { paymeantProductList.Add(new(paymeant.ID, product.Key, product.Value.Item2)); } } paymeantProductList.OrderBy(x => x.ProductID); List ansProductsList = new(); List productNames = new(); foreach (var pp in paymeantProductList) { var product = _productStorage.GetElement(new ProductSearchModel { ID = pp.ProductID}) ?? throw new Exception("Ошибка получения данных"); if (productNames.Contains(product.ProductName) == false) { productNames.Add(product.ProductName); // создаем запись var record = new ReportProductInPaymeantsViewModel { ProductName = product.ProductName, Values = new(), Total = 0 }; ansProductsList.Add(record); } // Ищем запись и вносим изменения int index = ansProductsList.IndexOf(ansProductsList.First(x => x.ProductName == product.ProductName)); var paymeant = _paymeantStorage.GetElement(new PaymeantSearchModel { ID = pp.PaymeantID }) ?? throw new Exception("Ошибка получения данных"); ansProductsList[index].Values.Add(new(pp.PaymeantID, pp.Count, paymeant.PayOption.ToString(), product.Price * pp.Count)); ansProductsList[index].Total++; } return ansProductsList; } public byte[]? SaveProductsToExcelFile(ReportProductBindingModel model) { var document = _saveToExcel.CreateReport(new ExcelInfoEmployee { Title = "Список продуктов", ListProduct = GetProducts(model), }); return document; } public byte[]? SaveProductsToWordFile(ReportProductBindingModel model) { var document = _saveToWord.CreateDoc(new WordInfoEmployee { Title = "Список оплат электротоваров", ListProduct = GetProducts(model) }); return document; } } }