using CanteenBusinessLogic.OfficePackage; using CanteenBusinessLogic.OfficePackage.HelperModels; using CanteenContracts.BindingModels; using CanteenContracts.BusinessLogicsContracts; using CanteenContracts.SearchModel; using CanteenContracts.StoragesContracts; using CanteenContracts.View; using CanteenContracts.ViewModels; using DocumentFormat.OpenXml.Bibliography; using System.Linq; namespace CanteenBusinessLogic.BusinessLogics { public class ReportLogic : IReportLogic { private readonly ILunchStorage lunchStorage; private readonly IOrderStorage orderStorage; private readonly ICookStorage cookStorage; private readonly IProductStorage productStorage; private readonly IVisitorStorage workerStorage; private readonly AbstractSaveToPdf saveToPdf; private readonly AbstractSaveToWord saveToWord; private readonly AbstractSaveToExcel saveToExcel; public ReportLogic(ILunchStorage lunchStorage, IOrderStorage orderStorage, ICookStorage cookStorage, IProductStorage productStorage, IVisitorStorage workerStorage, AbstractSaveToPdf saveToPdf, AbstractSaveToWord saveToWord, AbstractSaveToExcel saveToExcel) { this.cookStorage = cookStorage; this.orderStorage = orderStorage; this.lunchStorage = lunchStorage; this.productStorage = productStorage; this.workerStorage = workerStorage; this.saveToPdf = saveToPdf; this.saveToWord = saveToWord; this.saveToExcel = saveToExcel; } public List GetLunchesPCView(ReportBindingModel model) { var list = new List(); var lunches = lunchStorage.GetFilteredList(new LunchSearchModel { DateFrom = (DateTime)model.DateAfter, DateTo = model.DateBefore, VisitorId = model.UserId }); foreach (var lunch in lunches) { var record = new ReportLunchesPCView { DateCreate = lunch.DateCreate, Sum = Convert.ToInt32(lunch.Sum), Orders = new List(), Cooks = new List() }; var orders = lunch.LunchOrders.Keys.ToList(); foreach (var orderId in orders) { var order = orderStorage.GetElement(new OrderSearchModel { Id = orderId }); record.Orders.Add(order); } var lunchProducts = lunch.LunchProducts.Keys.ToList(); foreach (var productId in lunchProducts) { var product = productStorage.GetElement(new ProductSearchModel { Id = productId }); var productCooks = product.ProductCooks.Keys.ToList(); foreach (var cookId in productCooks) { var cook = cookStorage.GetElement(new CookSearchModel { Id = cookId }); record.Cooks.Add(cook); } } list.Add(record); } return list; } public void saveLunchesToPdfFile(ReportBindingModel model) { saveToPdf.CreateDoc(new PdfInfo { FileName = model.FileName, Title = "Список заказов", DateAfter = model.DateAfter.Value, DateBefore = model.DateBefore.Value, Lunches = GetLunchesPCView(model) }); } public List GetCooksByLanches(ReportBindingModel model) { var list = new List(); var lunches = lunchStorage.GetFilteredList(new LunchSearchModel { DateFrom = (DateTime)model.DateAfter, DateTo = model.DateBefore, VisitorId = model.UserId }); foreach (var lunch in lunches) { var record = new ReportCookView { Lunch = lunch, Cooks = new List() }; var lunchProducts = lunch.LunchProducts.Keys.ToList(); foreach (var productId in lunchProducts) { var product = productStorage.GetElement(new ProductSearchModel { Id = productId }); var productCooks = product.ProductCooks.Keys.ToList(); foreach (var cookId in productCooks) { if (record.Cooks.Where(cook => cook.Id == cookId).ToList().Count == 0) { var cook = cookStorage.GetElement(new CookSearchModel { Id = cookId }); record.Cooks.Add(cook); } } } list.Add(record); } return list; } private List GetOrdersByProducts(ReportBindingModel model) { var list = new List(); var products = productStorage.GetFilteredList(new ProductSearchModel { ManagerId = model.UserId }); foreach (var product in products) { var record = new ReportOrderView { Product = product, Orders = new List() }; var productCook = product.ProductCooks.Keys.ToList(); foreach (var cookId in productCook) { var orders = orderStorage.GetOrderCooksList(new OrderSearchModel { CookId = cookId }); orders.ForEach(x => { if (record.Orders.Find(y => y.Id == x.Id) == null) record.Orders.Add(x); }); } list.Add(record); } return list; } public void saveCooksToExcel(ReportBindingModel model) { saveToExcel.CreateCooksReport(new ExcelInfo() { FileName = model.FileName, Title = "Список поваров:", Cooks = GetCooksByLanches(model) }); } public void saveCooksToWord(ReportBindingModel model) { saveToWord.CreateCooksDoc(new WordInfo() { FileName = model.FileName, Title = "Список поваров", Cooks = GetCooksByLanches(model) }); } public void saveOrdersToExcel(ReportBindingModel model) { saveToExcel.CreateOrdersReport(new ExcelInfo() { FileName = model.FileName, Title = "Список заказов:", Orders = GetOrdersByProducts(model) }); } public void saveOrdersToWord(ReportBindingModel model) { saveToWord.CreateOrdersDoc(new WordInfo() { FileName = model.FileName, Title = "Список заказов", Orders = GetOrdersByProducts(model) }); } } }