Coursach/Course/ImplementerApp/ImplementerData.cs

237 lines
7.2 KiB
C#
Raw Normal View History

2024-05-23 20:02:34 +04:00
using Contracts.BusinessLogicsContracts;
using Contracts.ViewModels;
using Contracts.BindingModels;
using Contracts.StoragesContracts;
using Contracts.SearchModels;
2024-05-28 19:21:01 +04:00
using BusinessLogic.OfficePackage;
using BusinessLogic.MailWorker;
using Org.BouncyCastle.Bcpg;
2024-05-23 20:02:34 +04:00
namespace ImplementerApp
{
public class ImplementerData
{
private readonly ILogger _logger;
private readonly IImplementerLogic _implementerLogic;
private readonly IDetailLogic _detailLogic;
private readonly IProductionLogic _productionLogic;
private readonly IProductLogic _productLogic;
2024-05-26 23:23:24 +04:00
private readonly IMachineLogic _machineLogic;
2024-05-27 22:06:37 +04:00
private readonly IWorkshopLogic _workshopLogic;
2024-05-28 19:21:01 +04:00
private readonly AbstractSaveToExcelImplementer _excel;
private readonly AbstractSaveToWordImplementer _word;
private readonly AbstractSaveToPdfImplementer _pdf;
private readonly AbstractMailWorker _mail;
2024-05-23 20:02:34 +04:00
2024-05-28 19:21:01 +04:00
public ImplementerData(ILogger<ImplementerData> logger,
IImplementerLogic implementerLogic,
IDetailLogic detailLogic,
IProductionLogic productionLogic,
IProductLogic productLogic,
IMachineLogic machineLogic,
IWorkshopLogic workshopLogic,
AbstractSaveToExcelImplementer excel,
AbstractSaveToWordImplementer word,
AbstractMailWorker mail,
AbstractSaveToPdfImplementer pdf)
2024-05-27 22:06:37 +04:00
{
_logger = logger;
_implementerLogic = implementerLogic;
_detailLogic = detailLogic;
_productionLogic = productionLogic;
_productLogic = productLogic;
_machineLogic = machineLogic;
_workshopLogic = workshopLogic;
2024-05-28 19:21:01 +04:00
_excel = excel;
_word = word;
_mail = mail;
_pdf = pdf;
2024-05-27 22:06:37 +04:00
}
2024-05-23 20:02:34 +04:00
2024-05-27 22:06:37 +04:00
public ImplementerViewModel? Login(string login, string password)
2024-05-23 20:02:34 +04:00
{
return _implementerLogic.ReadElement(new()
{
Login = login,
Password = password
});
}
public bool Register(ImplementerBindingModel model)
{
return _implementerLogic.Create(model);
}
public bool UpdateUser(ImplementerBindingModel model)
{
return _implementerLogic.Update(model);
}
2024-05-28 21:41:19 +04:00
public bool CheckLogin(string login)
{
return _implementerLogic.ReadElement(new() { Login = login }) == null;
}
2024-05-23 20:02:34 +04:00
public List<DetailViewModel>? GetDetails(int userId)
{
return _detailLogic.ReadList(new DetailSearchModel() { UserId = userId });
}
public bool DeleteDetail(int detailId)
{
return _detailLogic.Delete(new() { Id = detailId });
}
public bool CreateDetail(DetailBindingModel model)
{
return _detailLogic.Create(model);
}
public bool UpdateDetail(DetailBindingModel model)
{
return _detailLogic.Update(model);
}
public DetailViewModel? GetDetail(int id)
{
return _detailLogic.ReadElement(new() { Id= id });
}
2024-05-28 22:40:13 +04:00
public bool CheckDetailName(string name)
{
return _detailLogic.ReadElement(new() { Name = name }) == null;
}
2024-05-23 20:02:34 +04:00
public List<ProductViewModel>? GetProducts(int userId)
{
return _productLogic.ReadList(new ProductSearchModel() { UserId = userId });
}
2024-05-25 21:36:21 +04:00
public ProductViewModel? GetProduct(int id)
{
return _productLogic.ReadElement(new() { Id = id });
}
public bool UpdateProduct(ProductBindingModel model)
{
return _productLogic.Update(model);
}
public bool DeleteProduct(int productId)
{
return _productLogic.Delete(new() { Id = productId });
}
public bool CreateProduct(ProductBindingModel model)
{
return _productLogic.Create(model);
}
2024-05-28 22:40:13 +04:00
public bool CheckProductName(string name)
{
return _productLogic.ReadElement(new() { Name = name }) == null;
}
2024-05-25 21:36:21 +04:00
2024-05-28 22:40:13 +04:00
public List<ProductionViewModel>? GetProductions(int userId)
2024-05-23 20:02:34 +04:00
{
return _productionLogic.ReadList(new() { UserId = userId });
}
2024-05-25 21:36:21 +04:00
public ProductionViewModel? GetProduction(int id)
{
return _productionLogic.ReadElement(new() { Id = id });
}
public bool CreateProduction(ProductionBindingModel model)
{
return _productionLogic.Create(model);
}
public bool UpdateProduction(ProductionBindingModel model)
{
return _productionLogic.Update(model);
}
public bool DeleteProduction(int productionId)
{
return _productionLogic.Delete(new() { Id = productionId});
2024-05-28 22:40:13 +04:00
}
public bool CheckProductionName(string name)
{
return _productionLogic.ReadElement(new() { Name = name }) == null;
}
public List<MachineViewModel>? GetMachines()
2024-05-26 23:23:24 +04:00
{
return _machineLogic.ReadList(null);
}
public List<DetailTimeReport> GetTimeReport(DateTime? startDate, DateTime? endDate, int UserId)
{
var details = _detailLogic.ReadList(new() { DateFrom = startDate, DateTo = endDate, UserId = UserId });
if (details == null)
return new();
List<DetailTimeReport> detailTimeReports = new List<DetailTimeReport>();
foreach (var detail in details)
{
var report = new DetailTimeReport();
report.DetailName = detail.Name;
var products = _productLogic.ReadList(new() { DetailId = detail.Id, UserId = UserId });
if (products != null)
report.Products = products.Select(p => p.Name).ToList();
var productions = _productionLogic.ReadList(new() { DetailId = detail.Id, UserId = UserId });
if (productions != null)
report.Productions = productions.Select(p => p.Name).ToList();
detailTimeReports.Add(report);
}
return detailTimeReports;
2024-05-25 21:36:21 +04:00
}
2024-05-27 22:06:37 +04:00
public List<DetailWorkshopReportViewModel>? GetWorkshopReports(List<int> details)
{
List<DetailWorkshopReportViewModel> reports = new();
foreach (int i in details)
{
DetailWorkshopReportViewModel report = new();
var detail = _detailLogic.ReadElement(new() { Id = i });
report.DetailName = detail!.Name;
var workshops = _workshopLogic.ReadList(new() { DetailId = i });
if (workshops != null)
report.WorkShops = workshops.Select(w => w.Title).ToList();
reports.Add(report);
}
return reports;
}
2024-05-28 19:21:01 +04:00
public void SaveReportExcel(List<int> details, MemoryStream stream)
{
var reports = GetWorkshopReports(details);
if (reports == null)
return;
int maxsize = 0;
foreach (var report in reports) { maxsize = Math.Max(maxsize, report.WorkShops.Count); }
_excel.CreateReport(new()
{
detailWorkshops = reports,
Title = "Отчет Деталь - цех",
memoryStream = stream,
maxleng = maxsize
});
}
public void SaveReportWord(List<int> details, MemoryStream stream)
{
var reports = GetWorkshopReports(details);
if (reports == null)
return;
_word.CreateDoc(new()
{
memoryStream = stream,
Title = "Отчет Деталь - цех",
Workshops = reports
});
}
public void SendMailReport(DateTime? startDate, DateTime? endDate, int UserId, MemoryStream stream)
{
var reports = GetTimeReport(startDate, endDate, UserId);
if (reports == null)
return;
_pdf.CreateDoc(new()
{
DateFrom = startDate!.Value,
DateTo = endDate!.Value,
FileName = stream,
Reports = reports,
Title = "Отчет"
});
byte[] report = stream.GetBuffer();
_mail.MailSendAsync(new() { MailAddress = UserImplementer.user!.Email, Subject = "Отчет", FileName = "PdfReport.pdf", Pdf = report});
}
2024-05-23 20:02:34 +04:00
}
}