2024-12-18 23:57:16 +04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using ProjectOptika.Scripts.Repositories;
|
|
|
|
|
|
|
|
|
|
namespace ProjectOptika.Scripts.Reports
|
|
|
|
|
{
|
|
|
|
|
internal class TableReport
|
|
|
|
|
{
|
|
|
|
|
private readonly IOrderRepository _orderRepository;
|
|
|
|
|
private readonly IEmployeeRepository _employeeRepository;
|
|
|
|
|
private readonly IClientRepositiory _clientRepositiory;
|
|
|
|
|
|
|
|
|
|
private readonly ILogger<TableReport> _logger;
|
|
|
|
|
|
|
|
|
|
internal static readonly string[] item = ["Дата", "Сотрудник", "Общее количество клиентов"];
|
|
|
|
|
|
|
|
|
|
public TableReport(IOrderRepository orderRepository, IClientRepositiory clientRepositiory, IEmployeeRepository employeeRepository, ILogger<TableReport> logger)
|
|
|
|
|
{
|
|
|
|
|
_orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository));
|
|
|
|
|
_clientRepositiory = clientRepositiory ?? throw new ArgumentNullException(nameof(clientRepositiory));
|
|
|
|
|
_employeeRepository = employeeRepository ?? throw new ArgumentNullException(nameof(employeeRepository));
|
|
|
|
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool CreateTable(string filePath, int emplyeeID, DateTime startDate, DateTime endDate)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
new ExcelBuilder(filePath)
|
|
|
|
|
.AddHeader("Сводка по заказам", 0, 4)
|
2024-12-18 23:59:13 +04:00
|
|
|
|
.AddParagraph($"за период c {startDate: dd.MM.yyyy} по {endDate:dd.MM.yyyy}", 0)
|
2024-12-18 23:57:16 +04:00
|
|
|
|
.AddTable([15, 20, 25], GetData(emplyeeID, startDate, endDate))
|
|
|
|
|
.Build();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка при формировании документа");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-18 23:59:13 +04:00
|
|
|
|
private List<string[]> GetData(int employeeID, DateTime startDate, DateTime endDate)
|
2024-12-18 23:57:16 +04:00
|
|
|
|
{
|
2024-12-18 23:59:13 +04:00
|
|
|
|
var orders = _orderRepository.GetOrders(startDate: startDate, endDate: endDate, employeeID: employeeID);
|
2024-12-18 23:57:16 +04:00
|
|
|
|
var employees = _employeeRepository.GetEmployees();
|
2024-12-18 23:59:13 +04:00
|
|
|
|
var employeesNames = _employeeRepository.GetEmployees().ToDictionary(d => d.ID, d => d.FullName);
|
2024-12-18 23:57:16 +04:00
|
|
|
|
var clientsNames = _clientRepositiory.GetClients().ToList();
|
|
|
|
|
|
|
|
|
|
var data = orders
|
|
|
|
|
.Join(employees, r => r.EmployeeID, p => p.ID, (r, p) => new
|
|
|
|
|
{
|
|
|
|
|
Date = r.OrderDate,
|
|
|
|
|
EmployeeID = p.ID,
|
|
|
|
|
ClientID = r.ClientID,
|
|
|
|
|
TotalCost = r.TotalCost
|
|
|
|
|
})
|
|
|
|
|
.OrderBy(x => x.Date)
|
|
|
|
|
.GroupBy(x => x.Date)
|
|
|
|
|
.Select(g => new
|
|
|
|
|
{
|
|
|
|
|
Date = g.Key,
|
|
|
|
|
Totals = g.Count()
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var result = new List<string[]> { item };
|
|
|
|
|
Random r = new Random();
|
|
|
|
|
foreach (var entry in data)
|
|
|
|
|
{
|
|
|
|
|
result.Add(new string[]
|
|
|
|
|
{
|
|
|
|
|
entry.Date.ToString("dd.MM.yyyy"),
|
2024-12-18 23:59:13 +04:00
|
|
|
|
employeesNames.ContainsKey(employeeID) ? employeesNames[employeeID] : string.Empty,
|
2024-12-18 23:57:16 +04:00
|
|
|
|
entry.Totals.ToString(),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var totalPatients = data.Sum(x => x.Totals);
|
|
|
|
|
result.Add(new string[]
|
|
|
|
|
{
|
|
|
|
|
"Всего",
|
|
|
|
|
"",
|
|
|
|
|
totalPatients.ToString()
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|