ISEbd-21_Agliullov.D.A._Con.../ConfectionaryBusinessLogic/ReportLogic.cs

135 lines
4.6 KiB
C#
Raw Normal View History

2023-03-01 16:42:58 +04:00
using ConfectioneryBusinessLogic.OfficePackage.HelperModels;
using ConfectioneryBusinessLogic.OfficePackage;
using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.BusinessLogicsContracts;
using ConfectioneryContracts.SearchModels;
using ConfectioneryContracts.StoragesContract;
using ConfectioneryContracts.ViewModels;
namespace ConfectioneryBusinessLogic
{
public class ReportLogic : IReportLogic
{
private readonly IPastryStorage _pastryStorage;
2023-03-01 16:42:58 +04:00
private readonly IOrderStorage _orderStorage;
private readonly IShopStorage _shopStorage;
2023-03-01 16:42:58 +04:00
private readonly AbstractSaveToExcel _saveToExcel;
private readonly AbstractSaveToWord _saveToWord;
private readonly AbstractSaveToPdf _saveToPdf;
public ReportLogic(IPastryStorage PastryStorage, IOrderStorage orderStorage, IShopStorage shopStorage,
2023-03-01 16:42:58 +04:00
AbstractSaveToExcel saveToExcel, AbstractSaveToWord saveToWord, AbstractSaveToPdf saveToPdf)
{
_pastryStorage = PastryStorage;
2023-03-01 16:42:58 +04:00
_orderStorage = orderStorage;
_shopStorage = shopStorage;
2023-03-01 16:42:58 +04:00
_saveToExcel = saveToExcel;
_saveToWord = saveToWord;
_saveToPdf = saveToPdf;
}
/// <summary>
/// Получение списка магазинов с изделиями
2023-03-01 16:42:58 +04:00
/// </summary>
/// <returns></returns>
public List<ReportShopPastrytViewModel> GetShopPastries()
2023-03-01 16:42:58 +04:00
{
var shops = _shopStorage.GetFullList();
2023-03-01 16:42:58 +04:00
var pastries = _pastryStorage.GetFullList();
2023-03-01 16:42:58 +04:00
var list = new List<ReportShopPastrytViewModel>();
2023-03-01 16:42:58 +04:00
foreach (var shop in shops)
2023-03-01 16:42:58 +04:00
{
var record = new ReportShopPastrytViewModel
2023-03-01 16:42:58 +04:00
{
ShopName = shop.Name,
Pastries = new List<Tuple<string, int>>(),
2023-03-01 16:42:58 +04:00
TotalCount = 0
};
foreach (var pastry in pastries)
2023-03-01 16:42:58 +04:00
{
if (shop.Pastries.ContainsKey(pastry.Id))
2023-03-01 16:42:58 +04:00
{
record.Pastries.Add(new(pastry.PastryName, shop.Pastries[pastry.Id].Item2));
record.TotalCount += shop.Pastries[pastry.Id].Item2;
2023-03-01 16:42:58 +04:00
}
}
record.Workload = record.TotalCount / (double)shop.MaxCountPastries;
2023-03-01 16:42:58 +04:00
list.Add(record);
}
return list;
}
/// <summary>
/// Получение списка заказов за определенный период
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public List<ReportOrdersViewModel> GetOrders(ReportBindingModel model)
{
return _orderStorage.GetFullList()
.GroupBy(x => x.DateCreate.Date)
.Select(x => new ReportOrdersViewModel
{
Date = x.Key,
Count = x.Count(),
Sum = x.Sum(y => y.Sum)
}).ToList();
2023-03-01 16:42:58 +04:00
}
/// <summary>
/// Сохранение магазинов в файл-Word
2023-03-01 16:42:58 +04:00
/// </summary>
/// <param name="model"></param>
public void SaveShopsToWordFile(ReportBindingModel model)
2023-03-01 16:42:58 +04:00
{
_saveToWord.CreateDoc(new WordInfo
{
FileName = model.FileName,
Title = "Список магазинов",
Shops = _shopStorage.GetFullList()
2023-03-01 16:42:58 +04:00
});
}
/// <summary>
/// Сохранение магазинов с указаеним продуктов в файл-Excel
2023-03-01 16:42:58 +04:00
/// </summary>
/// <param name="model"></param>
public void SaveShopPastryToExcelFile(ReportBindingModel model)
2023-03-01 16:42:58 +04:00
{
_saveToExcel.CreateReport(new ExcelInfo
{
FileName = model.FileName,
Title = "Список магазинов",
ShopPastries = GetShopPastries()
2023-03-01 16:42:58 +04:00
});
}
/// <summary>
/// Сохранение заказов в файл-Pdf
/// </summary>
/// <param name="model"></param>
public void SaveOrdersToPdfFile(ReportBindingModel model)
{
_saveToPdf.CreateDoc(new PdfInfo
{
FileName = model.FileName,
Title = "Список заказов",
DateFrom = model.DateFrom!.Value,
DateTo = model.DateTo!.Value,
Orders = GetOrders(model)
});
}
}
}