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

248 lines
9.1 KiB
C#
Raw Permalink 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;
2023-06-19 09:12:18 +04:00
using DocumentFormat.OpenXml.Drawing.Charts;
using DocumentFormat.OpenXml.Presentation;
using DocumentFormat.OpenXml.Wordprocessing;
2023-06-14 22:47:30 +04:00
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;
}
2023-06-19 09:12:18 +04:00
public List<ReportCooksPCView> GetCooksPCView(ReportBindingModel model)
{
var list = new List<ReportCooksPCView>();
2023-05-20 12:11:48 +04:00
2023-06-19 09:12:18 +04:00
var cooks = cookStorage.GetFilteredList(new CookSearchModel
{
ManagerId = model.UserId
});
2023-05-20 12:11:48 +04:00
2023-06-19 09:12:18 +04:00
var lunches = lunchStorage.GetFilteredList(new LunchSearchModel
2023-05-20 12:11:48 +04:00
{
2023-06-21 14:52:08 +04:00
DateFrom = model.DateAfter,
DateTo = model.DateBefore,
2023-05-20 12:11:48 +04:00
});
2023-06-19 09:12:18 +04:00
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;
}
}
}
2023-06-21 14:52:08 +04:00
if (record.Lunches.Count > 0) list.Add(record);
2023-06-19 09:12:18 +04:00
}
return list;
2023-05-20 12:11:48 +04:00
}
2023-06-19 09:12:18 +04:00
public List<ReportCookView> GetCooksByLunches(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
{
VisitorId = model.UserId
2023-06-19 09:12:18 +04:00
}).Where(x => model.LunchId.Contains(x.Id)).ToList();
2023-06-14 22:47:30 +04:00
foreach (var lunch in lunches)
{
var record = new ReportCookView
{
Lunch = lunch,
Cooks = new List<CookViewModel>()
};
2023-06-21 19:25:26 +04:00
var lunchOrders = lunch.LunchOrders.Keys.ToList();
foreach (var orderId in lunchOrders)
2023-06-14 22:47:30 +04:00
{
2023-06-21 19:25:26 +04:00
var order = orderStorage.GetElement(new OrderSearchModel { Id = orderId });
var orderCooks = order.OrderCooks.Keys.ToList();
2023-06-14 22:47:30 +04:00
2023-06-21 19:25:26 +04:00
foreach (var cookId in orderCooks)
2023-06-14 22:47:30 +04:00
{
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
2023-06-19 09:12:18 +04:00
}).Where(x => model.ProductId.Contains(x.Id));
2023-06-14 22:47:30 +04:00
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;
}
2023-06-19 09:12:18 +04:00
public void saveLunchesToPdfFile(ReportBindingModel model)
2023-05-20 12:11:48 +04:00
{
2023-06-19 09:12:18 +04:00
saveToPdf.CreateLunchDoc(new PdfInfo
2023-05-20 12:11:48 +04:00
{
2023-06-19 09:12:18 +04:00
Title = "Список обедов",
DateAfter = model.DateAfter.Value,
DateBefore = model.DateBefore.Value,
Lunches = GetLunchesPCView(model)
});
2023-05-20 12:11:48 +04:00
}
2023-06-19 09:12:18 +04:00
public void saveCooksToPdfFile(ReportBindingModel model)
2023-05-20 12:11:48 +04:00
{
2023-06-19 09:12:18 +04:00
saveToPdf.CreateCookDoc(new PdfInfo
2023-05-20 12:11:48 +04:00
{
Title = "Список поваров",
2023-06-19 09:12:18 +04:00
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 void saveCooksToExcel(ReportBindingModel model)
{
saveToExcel.CreateCooksReport(new ExcelInfo()
{
Title = "Список поваров:",
Cooks = GetCooksByLunches(model)
2023-05-20 12:11:48 +04:00
});
}
2023-06-14 22:47:30 +04:00
public void saveOrdersToExcel(ReportBindingModel model)
{
saveToExcel.CreateOrdersReport(new ExcelInfo()
{
Title = "Список заказов:",
Orders = GetOrdersByProducts(model)
});
}
2023-06-19 09:12:18 +04:00
public void saveCooksToWord(ReportBindingModel model)
{
saveToWord.CreateCooksDoc(new WordInfo()
{
Title = "Список поваров",
Cooks = GetCooksByLunches(model)
});
}
2023-06-14 22:47:30 +04:00
public void saveOrdersToWord(ReportBindingModel model)
{
saveToWord.CreateOrdersDoc(new WordInfo()
{
Title = "Список заказов",
Orders = GetOrdersByProducts(model)
});
}
2023-05-20 12:11:48 +04:00
}
}