PIbd-21_BatylkinaAO_MusoevD.../Canteen/CanteenBusinessLogic/BusinessLogics/ReportLogic.cs
2023-06-22 02:38:03 +04:00

252 lines
9.3 KiB
C#

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 DocumentFormat.OpenXml.Drawing.Charts;
using DocumentFormat.OpenXml.Presentation;
using DocumentFormat.OpenXml.Wordprocessing;
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<ReportLunchesPCView> GetLunchesPCView(ReportBindingModel model)
{
var list = new List<ReportLunchesPCView>();
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<OrderViewModel>(),
Cooks = new List<CookViewModel>()
};
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 List<ReportCooksPCView> GetCooksPCView(ReportBindingModel model)
{
var list = new List<ReportCooksPCView>();
var cooks = cookStorage.GetFilteredList(new CookSearchModel
{
ManagerId = model.UserId
});
var lunches = lunchStorage.GetFilteredList(new LunchSearchModel
{
DateFrom = model.DateAfter,
DateTo = model.DateBefore,
});
foreach (var cook in cooks)
{
var record = new ReportCooksPCView
{
CookId = cook.Id,
FIO = cook.FIO,
Lunches = new List<LunchViewModel>()
};
foreach (var lunch in lunches)
{
foreach(var order in lunch.LunchOrders)
{
var findOrder = orderStorage.GetElement(new OrderSearchModel { Id = order.Key });
if (findOrder.OrderCooks.ContainsKey(cook.Id))
{
record.Lunches.Add(lunch);
break;
}
}
}
if (record.Lunches.Count > 0) list.Add(record);
}
return list;
}
public List<ReportCookView> GetCooksByLunches(ReportBindingModel model)
{
var list = new List<ReportCookView>();
var lunches = lunchStorage.GetFilteredList(new LunchSearchModel
{
VisitorId = model.UserId
}).Where(x => model.LunchId.Contains(x.Id)).ToList();
foreach (var lunch in lunches)
{
var record = new ReportCookView
{
Lunch = lunch,
Cooks = new List<CookViewModel>()
};
var lunchOrders = lunch.LunchOrders.Keys.ToList();
foreach (var orderId in lunchOrders)
{
var order = orderStorage.GetElement(new OrderSearchModel { Id = orderId });
var orderCooks = order.OrderCooks.Keys.ToList();
foreach (var cookId in orderCooks)
{
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<ReportOrderView> GetOrdersByProducts(ReportBindingModel model)
{
var list = new List<ReportOrderView>();
var products = productStorage.GetFilteredList(new ProductSearchModel
{
ManagerId = model.UserId
}).Where(x => model.ProductId.Contains(x.Id));
foreach (var product in products)
{
var record = new ReportOrderView
{
Product = product,
Orders = new List<OrderViewModel>()
};
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 saveLunchesToPdfFile(ReportBindingModel model)
{
saveToPdf.CreateLunchDoc(new PdfInfo
{
Title = "Список обедов",
DateAfter = model.DateAfter.Value,
DateBefore = model.DateBefore.Value,
Lunches = GetLunchesPCView(model)
});
}
public void saveCooksToPdfFile(ReportBindingModel model)
{
saveToPdf.CreateCookDoc(new PdfInfo
{
Title = "Список поваров",
DateAfter = model.DateAfter.Value,
DateBefore = model.DateBefore.Value,
FileName = (model.FileName != null) ? model.FileName : $"C:\\PdfReports\\{DateTime.Now.ToString("HH.mm.ss_dd.MM.yyyy")}.pdf",
Cooks = GetCooksPCView(model)
});
}
public byte[] saveCooksToExcel(ReportBindingModel model)
{
byte[] report = saveToExcel.CreateCooksReport(new ExcelInfo()
{
Title = "Список поваров:",
Cooks = GetCooksByLunches(model)
});
return report;
}
public byte[] saveOrdersToExcel(ReportBindingModel model)
{
byte[] report = saveToExcel.CreateOrdersReport(new ExcelInfo()
{
Title = "Список заказов:",
Orders = GetOrdersByProducts(model)
});
return report;
}
public byte[] saveCooksToWord(ReportBindingModel model)
{
byte[] report = saveToWord.CreateCooksDoc(new WordInfo()
{
Title = "Список поваров",
Cooks = GetCooksByLunches(model)
});
return report;
}
public byte[] saveOrdersToWord(ReportBindingModel model)
{
byte[] report = saveToWord.CreateOrdersDoc(new WordInfo()
{
Title = "Список заказов",
Orders = GetOrdersByProducts(model)
});
return report;
}
}
}