87 lines
3.1 KiB
C#
Raw Normal View History

2024-12-16 20:18:06 +04:00
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)
{
2024-12-18 19:11:56 +04:00
builder.AddParagraph("Заказчики").AddTable([2400, 1200, 2400, 2400, 2400], GetCustomers());
2024-12-16 20:18:06 +04:00
}
if (includeManufacturers)
{
2024-12-18 19:11:56 +04:00
builder.AddParagraph("Материалы").AddTable([1200, 2400, 2400], GetMaterials());
2024-12-16 20:18:06 +04:00
}
if (includeProducts)
{
2024-12-18 19:11:56 +04:00
builder.AddParagraph("Издательства").AddTable([2400, 2400, 2400], GetPublishingHouse());
2024-12-16 20:18:06 +04:00
}
builder.Build();
return true;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при формировании документа");
return false;
}
}
2024-12-18 19:11:56 +04:00
private List<string[]> GetCustomers()
2024-12-16 20:18:06 +04:00
{
return [
2024-12-18 19:11:56 +04:00
["ФИО заказчика", "Возраст", "Тип заказчика", "Телефон", "Email"],
2024-12-16 20:18:06 +04:00
.. _customerRepository
.ReadCustomers()
.Select(x => new string[] { x.FullName, x.Age.ToString(), x.TypeCustomer.ToString(), x.Phone.ToString(), x.Email}),
];
}
2024-12-18 19:11:56 +04:00
private List<string[]> GetMaterials()
2024-12-16 20:18:06 +04:00
{
return [
2024-12-18 19:11:56 +04:00
[ "Материал", "Количество", "Дата"],
2024-12-16 20:18:06 +04:00
.. _materialRepository
.ReadMaterials()
2024-12-18 19:11:56 +04:00
.Select(x => new string[] {x.Material.ToString(), x.Count.ToString(), x.DateMaterials.ToString()}),
2024-12-16 20:18:06 +04:00
];
}
2024-12-18 19:11:56 +04:00
private List<string[]> GetPublishingHouse()
2024-12-16 20:18:06 +04:00
{
return [
2024-12-18 19:11:56 +04:00
["Название", "Адрес", "Рабочик телефон"],
2024-12-16 20:18:06 +04:00
.._publisingHouseRepository
.ReadPublishingHouses()
.Select(x => new string[] {x.Title, x.Address, x.WorkPhone.ToString()}),
];
}
}