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)
{
builder.AddParagraph("Заказчики").AddTable([2400, 1200, 2400, 2400, 2400], GetClients());
}
if (includeManufacturers)
{
builder.AddParagraph("Материалы").AddTable([2400], GetManufacturers());
}
if (includeProducts)
{
builder.AddParagraph("Издательства").AddTable([2400, 2400, 2400, 2400], GetProducts());
}
builder.Build();
return true;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при формировании документа");
return false;
}
}
private List<string[]> GetClients()
{
return [
["ФИО клиента", "Тип клиента", "Оптовик"],
.. _customerRepository
.ReadCustomers()
.Select(x => new string[] { x.FullName, x.Age.ToString(), x.TypeCustomer.ToString(), x.Phone.ToString(), x.Email}),
];
}
private List<string[]> GetManufacturers()
{
return [
["Название"],
.. _materialRepository
.ReadMaterials()
.Select(x => new string[] { x.DateMaterials.ToString(), x.Count.ToString(), x.Material.ToString()}),
];
}
private List<string[]> GetProducts()
{
return [
["Название", "Производитель", "Категория", "Начальная цена"],
.._publisingHouseRepository
.ReadPublishingHouses()
.Select(x => new string[] {x.Title, x.Address, x.WorkPhone.ToString()}),
];
}
}