Files
ISEbd-21_Savelyev.P.Y._Proj…/ProjectSellPC/ProjectSellPC/Reports/DocReport.cs
2024-12-22 19:20:18 +04:00

94 lines
3.5 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 ProjectSellPC.Repos;
using ProjectSellPC.DocBuilder;
using ProjectSellPC.Repos.Impements;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectSellPC.DocumentsBuilder
{
internal class DocReport
{
private readonly IClientRepository _clientRepository;
private readonly IProductRepository _productRepository;
private readonly IWarehouseRepository _warehouseRepository;
private readonly ILogger<DocReport> _logger;
public DocReport(IClientRepository clientRepository, IProductRepository productRepository, IWarehouseRepository warehouseRepository, ILoggerFactory loggerFactory)
{
_clientRepository = clientRepository ??
throw new ArgumentNullException(nameof(clientRepository));
_productRepository = productRepository ??
throw new ArgumentNullException(nameof(productRepository));
_warehouseRepository = warehouseRepository ??
throw new ArgumentNullException(nameof(warehouseRepository));
_logger = loggerFactory.CreateLogger<DocReport>();
}
public bool CreateDoc(string filePath, bool includeClients, bool includeProducts, bool includeStorage)
{
try
{
var builder = new WordBuilder(filePath)
.AddHeader("Документ со справочниками");
if (includeClients)
{
builder.AddParagraph("Клиенты")
.AddTable(new int[] { 2400, 2400, 3200, 1200 }, GetClients());
}
if (includeProducts)
{
builder.AddParagraph("Товары")
.AddTable(new int[] { 2400, 2400, 2400, 2400, 2400 }, GetProducts());
}
if (includeStorage)
{
builder.AddParagraph("Склады")
.AddTable(new int[] { 2400, 2400, 1200 }, GetStorage());
}
builder.Build();
return true;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при формировании документа");
return false;
}
}
private List<string[]> GetClients()
{
List<string[]> result = _clientRepository.ReadAll().Select(x => new string[] { x.Id.ToString(), x.Name, x.PhoneNumber, x.ClientType.ToString() }).ToList();
result.Insert(0, new string[] { "ID", "Имя", "Номер телефона", "Тип клиента" });
return result;
}
private List<string[]> GetProducts()
{
List<string[]> result = _productRepository.ReadAll().Select(x => new string[] { x.ID.ToString(), x.Name, x.Description, x.ProductType.ToString(), x.Price.ToString() + " р." }).ToList();
result.Insert(0, new string[] { "ID", "Имя", "Описание", "Тип товара", "Цена" });
return result;
}
private List<string[]> GetStorage()
{
List<string[]> result = _warehouseRepository.ReadAll().Select(x => new string[] { x.Id.ToString(), x.Adress, x.Size.ToString()}).ToList();
result.Insert(0, new string[] { "ID", "Адрес", "Вместимость"});
return result;
}
}
}