PIbd-23-Salin-O.A.-IceCream.../IceCreamShop/IceCreamShopBusinessLogic/BusinessLogic/ReportLogic.cs

171 lines
6.3 KiB
C#
Raw Normal View History

2024-03-24 13:36:55 +04:00
using IceCreamShopBusinessLogic.OfficePackage.HelperModels;
using IceCreamShopBusinessLogic.OfficePackage;
using IceCreamShopContracts.BindingModels;
using IceCreamShopContracts.BusinessLogicsContracts;
using IceCreamShopContracts.SearchModels;
using IceCreamShopContracts.StoragesContracts;
using IceCreamShopContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IceCreamShopBusinessLogic.BusinessLogic
{
public class ReportLogic : IReportLogic
{
private readonly IComponentStorage _componentStorage;
private readonly IIceCreamStorage _iceCreamStorage;
private readonly IOrderStorage _orderStorage;
2024-03-24 18:46:06 +04:00
private readonly IShopStorage _shopStorage;
2024-03-24 13:36:55 +04:00
private readonly AbstractSaveToExcel _saveToExcel;
private readonly AbstractSaveToWord _saveToWord;
private readonly AbstractSaveToPdf _saveToPdf;
public ReportLogic(IIceCreamStorage iceCreamStorage, IComponentStorage
2024-03-24 18:46:06 +04:00
componentStorage, IOrderStorage orderStorage, IShopStorage shopStorage,
2024-03-24 13:36:55 +04:00
AbstractSaveToExcel saveToExcel, AbstractSaveToWord saveToWord,
AbstractSaveToPdf saveToPdf)
{
_iceCreamStorage = iceCreamStorage;
_componentStorage = componentStorage;
_orderStorage = orderStorage;
2024-03-24 18:46:06 +04:00
_shopStorage = shopStorage;
2024-03-24 13:36:55 +04:00
_saveToExcel = saveToExcel;
_saveToWord = saveToWord;
_saveToPdf = saveToPdf;
}
public List<ReportIceCreamComponentViewModel> GetIceCreamComponent()
{
var iceCreams = _iceCreamStorage.GetFullList();
var list = new List<ReportIceCreamComponentViewModel>();
foreach (var iceCream in iceCreams)
{
var record = new ReportIceCreamComponentViewModel
{
IceCreamName = iceCream.IceCreamName,
Components = new List<Tuple<string, int>>(),
TotalCount = 0
};
foreach (var component in iceCream.IceCreamComponents)
{
record.Components.Add(new Tuple<string,
int>(component.Value.Item1.ComponentName, component.Value.Item2));
record.TotalCount +=
component.Value.Item2;
}
list.Add(record);
}
return list;
}
2024-03-24 18:46:06 +04:00
public List<ReportShopIceCreamViewModel> GetShopsIceCreams()
{
var shops = _shopStorage.GetFullList();
var list = new List<ReportShopIceCreamViewModel>();
foreach (var shop in shops)
{
var record = new ReportShopIceCreamViewModel
{
ShopName = shop.ShopName,
IceCreams = new List<Tuple<string, int>>(),
TotalCount = 0
};
foreach (var iceCream in shop.ShopIceCreams)
{
record.IceCreams.Add(new Tuple<string,
int>(iceCream.Value.Item1.IceCreamName, iceCream.Value.Item2));
record.TotalCount +=
iceCream.Value.Item2;
}
list.Add(record);
}
return list;
}
public List<ReportDateOrdersViewModel> GetDatesOrders()
{
return _orderStorage.GetFullList().GroupBy(x => x.DateCreate.Date).Select(x => new ReportDateOrdersViewModel
{
DateOfOrders = x.Key,
Count = x.Count(),
Sum = x.Sum(y => y.Sum)
}).ToList();
}
public void SaveDatesOrdersToPdfFile(ReportBindingModel model)
{
_saveToPdf.CreateReportDateDoc(new PdfInfo
{
FileName = model.FileName,
Title = "Заказы по датам",
DateOrders = GetDatesOrders()
});
}
public void SaveShopsToWordFile(ReportBindingModel model)
{
var tmp = _shopStorage.GetFullList();
_saveToWord.CreateTableDoc(new WordInfo
{
FileName = model.FileName,
Title = "Список магазинов",
Shops = _shopStorage.GetFullList()
});
}
public void SaveShopsIceCreamsToExcelFile(ReportBindingModel model)
{
_saveToExcel.CreateShopReport(new ExcelInfo
{
FileName = model.FileName,
Title = "Загруженность магазинов",
ShopIceCreams = GetShopsIceCreams()
});
}
2024-03-24 13:36:55 +04:00
public List<ReportOrdersViewModel> GetOrders(ReportBindingModel model)
{
return _orderStorage.GetFilteredList(new OrderSearchModel
{
DateFrom
= model.DateFrom,
DateTo = model.DateTo
})
.Select(x => new ReportOrdersViewModel
{
Id = x.Id,
DateCreate = x.DateCreate,
IceCreamName = x.IceCreamName,
OrderStatus = x.Status.ToString(),
Sum = x.Sum
})
.ToList();
}
2024-03-24 15:12:19 +04:00
public void SaveIceCreamToWordFile(ReportBindingModel model)
2024-03-24 13:36:55 +04:00
{
_saveToWord.CreateDoc(new WordInfo
{
FileName = model.FileName,
Title = "Список мороженного",
IceCreams = _iceCreamStorage.GetFullList()
});
}
public void SaveIceCreamComponentToExcelFile(ReportBindingModel model)
{
_saveToExcel.CreateReport(new ExcelInfo
{
FileName = model.FileName,
Title = "Список мороженного",
IceCreamComponents = GetIceCreamComponent()
});
}
public void SaveOrdersToPdfFile(ReportBindingModel model)
{
_saveToPdf.CreateDoc(new PdfInfo
{
FileName = model.FileName,
Title = "Список заказов",
DateFrom = model.DateFrom!.Value,
DateTo = model.DateTo!.Value,
Orders = GetOrders(model)
});
}
}
}