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;
private readonly IOrderStorage _orderStorage;
private readonly IShopStorage _shopStorage;
private readonly IComponentStorage _componentStorage;
private readonly AbstractSaveToExcel _saveToExcel;
private readonly AbstractSaveToWord _saveToWord;
private readonly AbstractSaveToPdf _saveToPdf;
public ReportLogic(IPastryStorage PastryStorage, IOrderStorage orderStorage, IShopStorage shopStorage, IComponentStorage componentStorage,
AbstractSaveToExcel saveToExcel, AbstractSaveToWord saveToWord, AbstractSaveToPdf saveToPdf)
{
_componentStorage = componentStorage;
_pastryStorage = PastryStorage;
_orderStorage = orderStorage;
_shopStorage = shopStorage;
_saveToExcel = saveToExcel;
_saveToWord = saveToWord;
_saveToPdf = saveToPdf;
}
///
/// Получение списка магазинов с изделиями
///
///
public List GetShopPastries()
{
var shops = _shopStorage.GetFullList();
var pastries = _pastryStorage.GetFullList();
var list = new List();
foreach (var shop in shops)
{
var record = new ReportShopPastrytViewModel
{
ShopName = shop.Name,
Pastries = new List>(),
TotalCount = 0
};
foreach (var pastry in pastries)
{
if (shop.Pastries.ContainsKey(pastry.Id))
{
record.Pastries.Add(new(pastry.PastryName, shop.Pastries[pastry.Id].Item2));
record.TotalCount += shop.Pastries[pastry.Id].Item2;
}
}
record.Workload = record.TotalCount / (double)shop.MaxCountPastries;
list.Add(record);
}
return list;
}
///
/// Получение списка компонент с указанием, в каких изделиях используются
///
///
public List GetPastryComponent()
{
var components = _componentStorage.GetFullList();
var pastries = _pastryStorage.GetFullList();
var list = new List();
foreach (var pastry in pastries)
{
var record = new ReportPastryComponentViewModel
{
PastryName = pastry.PastryName,
Components = new List>(),
TotalCount = 0
};
foreach (var component in components)
{
if (pastry.PastryComponents.ContainsKey(component.Id))
{
record.Components.Add(new(component.ComponentName, pastry.PastryComponents[component.Id].Item2));
record.TotalCount += pastry.PastryComponents[component.Id].Item2;
}
}
list.Add(record);
}
return list;
}
///
/// Получение списка заказов за определенный период
///
///
///
public List GetOrders(ReportBindingModel model)
{
return _orderStorage.GetFilteredList(new OrderSearchModel { DateFrom = model.DateFrom, DateTo = model.DateTo })
.Select(x => new ReportOrdersViewModel
{
Id = x.Id,
DateCreate = x.DateCreate,
PastryName = x.PastryName,
OrderStatus = Convert.ToString(x.Status) ?? string.Empty,
Sum = x.Sum
})
.ToList();
}
///
/// Получение списка заказов за определенный период
///
///
///
public List GetGroupOrders(ReportBindingModel model)
{
return _orderStorage.GetFullList()
.GroupBy(x => x.DateCreate.Date)
.Select(x => new ReportGroupOrdersViewModel
{
Date = x.Key,
Count = x.Count(),
Sum = x.Sum(y => y.Sum)
}).ToList();
}
///
/// Сохранение магазинов в файл-Word
///
///
public void SaveShopsTableToWordFile(ReportBindingModel model)
{
_saveToWord.CreateDocTable(new WordInfoTable
{
FileName = model.FileName,
Title = "Список магазинов",
Shops = _shopStorage.GetFullList()
});
}
public void SavePastriesToWordFile(ReportBindingModel model)
{
_saveToWord.CreateDoc(new WordInfo
{
FileName = model.FileName,
Title = "Список изделий",
Pastries = _pastryStorage.GetFullList()
});
}
///
/// Сохранение магазинов с указаеним продуктов в файл-Excel
///
///
public void SaveShopPastryToExcelFile(ReportBindingModel model)
{
_saveToExcel.CreateReportShop(new ExcelInfo
{
FileName = model.FileName,
Title = "Список магазинов",
ShopPastries = GetShopPastries()
});
}
public void SavePastryComponentToExcelFile(ReportBindingModel model)
{
_saveToExcel.CreateReportPastry(new ExcelInfoPastry
{
FileName = model.FileName,
Title = "Список изделий по компонентам",
PastryComponents = GetPastryComponent(),
});
}
///
/// Сохранение заказов в файл-Pdf
///
///
public void SaveGroupOrdersToPdfFile(ReportBindingModel model)
{
_saveToPdf.CreateDoc(new PdfInfo
{
FileName = model.FileName,
Title = "Список сгруппированных заказов по датам",
DateFrom = model.DateFrom!.Value,
DateTo = model.DateTo!.Value,
Orders = GetGroupOrders(model)
});
}
///
/// Сохранение заказов в файл-Pdf
///
///
public void SaveOrdersToPdfFile(ReportBindingModel model)
{
_saveToPdf.CreateDocOrders(new PdfInfoOrders
{
FileName = model.FileName,
Title = "Список заказов",
DateFrom = model.DateFrom!.Value,
DateTo = model.DateTo!.Value,
Orders = GetOrders(model),
});
}
}
}