ProjectLib/ProjectLibrary/Reports/DocReport.cs

89 lines
3.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.Extensions.Logging;
using ProjectLibrary.Repositores;
using ProjectLibrary.Repositories;
using ProjectLibrary.Repositories.Implementations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectLibrary.Reports;
public class DocReport
{
private readonly IBookRepository _bookRepository;
private readonly IReaderRepository _readerRepository;
private readonly ITicketExtensionsRepository _ticketExtensionsRepository;
private readonly ILogger<DocReport> _logger;
public DocReport(IBookRepository bookRepository, IReaderRepository readerRepository,
ITicketExtensionsRepository ticketExtensionsRepository, ILogger<DocReport> logger)
{
_bookRepository = bookRepository ?? throw new ArgumentNullException(nameof(bookRepository));
_readerRepository = readerRepository ?? throw new ArgumentNullException(nameof(readerRepository));
_ticketExtensionsRepository = ticketExtensionsRepository ?? throw new ArgumentNullException(nameof(ticketExtensionsRepository));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public bool CreateDoc(string filePath, bool includeBooks, bool includeReaders, bool includeTicketExtensions)
{
try
{
var builder = new WordBuilder(filePath).AddHeader("Документ со справочниками");
if (includeBooks)
{
builder.AddParagraph("Книги").AddTable([2400, 2400, 1200], GetBooks());
}
if (includeReaders)
{
builder.AddParagraph("Читатели").AddTable([2400, 1200, 1200], GetReaders());
}
if (includeTicketExtensions)
{
builder.AddParagraph("Библиотеки").AddTable([2400, 2400, 2400], GetTicketExtensions());
}
builder.Build();
return true;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при формировании документа");
return false;
}
}
private List<string[]> GetBooks()
{
return [
["Автор", "Название", "Тип книги"],
.. _bookRepository
.ReadBooks()
.Select(x => new string[] { x.Author, x.Name, x.TypeBookID.ToString()}),
];
}
private List<string[]> GetReaders()
{
return [
["ФИО читателя", "Билет читателя", "Дата регистрации"],
.. _readerRepository
.ReadReaders()
.Select(x => new string[] { x.Name, x.ReaderTicket.ToString(), x.RegistrationDateRT.ToString()}),
];
}
private List<string[]> GetTicketExtensions()
{
return [
["ID Читателя", "Последняя дата обновления", "Следующая дата обновления"],
.. _ticketExtensionsRepository
.ReadTicketExtensions()
.Select(x => new string[] {x.ReaderID.ToString(), x.LastUpdateDate.ToString(), x.NextUpdateDate.ToString()}),
];
}
}