PIbd-21_BatylkinaAO_MusoevD.../Canteen/CanteenBusinessLogic/BusinessLogics/ReportLogic.cs
2023-05-20 12:11:48 +04:00

133 lines
5.6 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;
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>();
// Получаем список обедов (сущность 1) за указанный период и для указанного посетителя
var lunches = lunchStorage.GetFilteredList(new LunchSearchModel
{
DateFrom = (DateTime)model.DateAfter,
DateTo = model.DateBefore,
VisitorId = model.VisitorId
});
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>()
};
// Получаем связанные заказы (сущность 2) для текущего обеда
var orders = lunch.LunchOrders.Keys.ToList();
foreach (var orderId in orders)
{
// Получаем заказы (сущность 2) и добавляем их в список Orders
var order = orderStorage.GetElement(new OrderSearchModel { Id = orderId });
record.Orders.Add(order);
}
// Получаем связанных поваров (сущность 4) для текущих продуктов обеда
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)
{
// Получаем поваров (сущность 4) и добавляем их в список Cooks
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<CookViewModel> GetCooksByLanches(ReportBindingModel model)
{
var list = new List<CookViewModel>();
var listCookIds = new List<int>();
foreach (var lunch in model.lunches)
{
var lunchProducts = lunch.LunchProducts.Keys.ToList().Select(rec => productStorage.GetElement(new ProductSearchModel { Id = rec }));
foreach (var elem in lunchProducts)
{
listCookIds.AddRange(elem.ProductCooks.Keys.ToList());
}
}
list = listCookIds.Distinct().ToList().Select(rec => cookStorage.GetElement(new CookSearchModel { Id = rec })).ToList();
return list;
}
public void saveCooksToExcel(ReportBindingModel model)
{
saveToExcel.CreateReport(new ExcelInfo()
{
FileName = model.FileName,
Title = "Список поваров:",
Cooks = GetCooksByLanches(model)
});
}
public void saveCooksToWord(ReportBindingModel model)
{
saveToWord.CreateDoc(new WordInfo()
{
FileName = model.FileName,
Title = "Список поваров",
Cooks = GetCooksByLanches(model)
});
}
}
}