PIbd-21_BatylkinaAO_MusoevD.../Canteen/CanteenBusinessLogic/BusinessLogics/ReportLogic.cs

201 lines
7.3 KiB
C#
Raw Normal View History

2023-05-20 12:11:48 +04:00
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;
2023-06-14 22:47:30 +04:00
using DocumentFormat.OpenXml.Bibliography;
using System.Linq;
2023-05-20 12:11:48 +04:00
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,
2023-06-14 22:47:30 +04:00
VisitorId = model.UserId
2023-05-20 12:11:48 +04:00
});
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 void saveLunchesToPdfFile(ReportBindingModel model)
{
saveToPdf.CreateDoc(new PdfInfo
{
FileName = model.FileName,
Title = "Список заказов",
DateAfter = model.DateAfter.Value,
DateBefore = model.DateBefore.Value,
Lunches = GetLunchesPCView(model)
});
}
2023-06-14 22:47:30 +04:00
public List<ReportCookView> GetCooksByLanches(ReportBindingModel model)
2023-05-20 12:11:48 +04:00
{
2023-06-14 22:47:30 +04:00
var list = new List<ReportCookView>();
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<CookViewModel>()
};
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<ReportOrderView> GetOrdersByProducts(ReportBindingModel model)
{
var list = new List<ReportOrderView>();
var products = productStorage.GetFilteredList(new ProductSearchModel
2023-05-20 12:11:48 +04:00
{
2023-06-14 22:47:30 +04:00
ManagerId = model.UserId
});
foreach (var product in products)
{
var record = new ReportOrderView
2023-05-20 12:11:48 +04:00
{
2023-06-14 22:47:30 +04:00
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);
});
2023-05-20 12:11:48 +04:00
}
2023-06-14 22:47:30 +04:00
list.Add(record);
2023-05-20 12:11:48 +04:00
}
return list;
}
public void saveCooksToExcel(ReportBindingModel model)
{
2023-06-14 22:47:30 +04:00
saveToExcel.CreateCooksReport(new ExcelInfo()
2023-05-20 12:11:48 +04:00
{
FileName = model.FileName,
Title = "Список поваров:",
Cooks = GetCooksByLanches(model)
});
}
public void saveCooksToWord(ReportBindingModel model)
{
2023-06-14 22:47:30 +04:00
saveToWord.CreateCooksDoc(new WordInfo()
2023-05-20 12:11:48 +04:00
{
FileName = model.FileName,
Title = "Список поваров",
Cooks = GetCooksByLanches(model)
});
}
2023-06-14 22:47:30 +04:00
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)
});
}
2023-05-20 12:11:48 +04:00
}
}