87 lines
3.1 KiB
C#
87 lines
3.1 KiB
C#
using Microsoft.Extensions.Logging;
|
||
using Publication.Repositories;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace Publication.Reports;
|
||
|
||
public class DocReport
|
||
{
|
||
private readonly ICustomerRepository _customerRepository;
|
||
private readonly IMaterialRepository _materialRepository;
|
||
private readonly IPublisingHouseRepository _publisingHouseRepository;
|
||
|
||
private readonly ILogger<DocReport> _logger;
|
||
|
||
public DocReport(ICustomerRepository customerRepository, IMaterialRepository materialRepository,
|
||
IPublisingHouseRepository publisingHouseRepository, ILogger<DocReport> logger)
|
||
{
|
||
_customerRepository = customerRepository ?? throw new ArgumentNullException(nameof(customerRepository));
|
||
_materialRepository = materialRepository ?? throw new ArgumentNullException(nameof(materialRepository));
|
||
_publisingHouseRepository = publisingHouseRepository ?? throw new ArgumentNullException(nameof(publisingHouseRepository));
|
||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||
}
|
||
|
||
public bool CreateDoc(string filePath, bool includeClients, bool includeManufacturers, bool includeProducts)
|
||
{
|
||
try
|
||
{
|
||
var builder = new WordBuilder(filePath).AddHeader("Документ со справочниками");
|
||
if (includeClients)
|
||
{
|
||
builder.AddParagraph("Заказчики").AddTable([2400, 1200, 2400, 2400, 2400], GetCustomers());
|
||
}
|
||
if (includeManufacturers)
|
||
{
|
||
builder.AddParagraph("Материалы").AddTable([1200, 2400, 2400], GetMaterials());
|
||
}
|
||
if (includeProducts)
|
||
{
|
||
builder.AddParagraph("Издательства").AddTable([2400, 2400, 2400], GetPublishingHouse());
|
||
}
|
||
builder.Build();
|
||
return true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError(ex, "Ошибка при формировании документа");
|
||
return false;
|
||
}
|
||
}
|
||
|
||
private List<string[]> GetCustomers()
|
||
{
|
||
return [
|
||
["ФИО заказчика", "Возраст", "Тип заказчика", "Телефон", "Email"],
|
||
.. _customerRepository
|
||
.ReadCustomers()
|
||
.Select(x => new string[] { x.FullName, x.Age.ToString(), x.TypeCustomer.ToString(), x.Phone.ToString(), x.Email}),
|
||
];
|
||
}
|
||
|
||
|
||
private List<string[]> GetMaterials()
|
||
{
|
||
return [
|
||
[ "Материал", "Количество", "Дата"],
|
||
.. _materialRepository
|
||
.ReadMaterials()
|
||
.Select(x => new string[] {x.Material.ToString(), x.Count.ToString(), x.DateMaterials.ToString()}),
|
||
];
|
||
}
|
||
|
||
|
||
private List<string[]> GetPublishingHouse()
|
||
{
|
||
return [
|
||
["Название", "Адрес", "Рабочик телефон"],
|
||
.._publisingHouseRepository
|
||
.ReadPublishingHouses()
|
||
.Select(x => new string[] {x.Title, x.Address, x.WorkPhone.ToString()}),
|
||
];
|
||
}
|
||
}
|