почти готово
This commit is contained in:
parent
5dbc07fdc5
commit
2a5fb215e3
@ -25,6 +25,13 @@ public class Order
|
||||
[Browsable(false)]
|
||||
public IEnumerable<OrderService> OrderService { get; private set; } = [];
|
||||
|
||||
public void SetServices(IEnumerable<OrderService> orderServices)
|
||||
{
|
||||
if (orderServices != null && orderServices.Any())
|
||||
{
|
||||
OrderService = orderServices;
|
||||
}
|
||||
}
|
||||
public static Order CreateOperation(int id, int companyId, decimal orderPrice, IEnumerable<OrderService> orderService)
|
||||
{
|
||||
return new Order
|
||||
|
@ -35,43 +35,24 @@ public class ChartReport
|
||||
}
|
||||
}
|
||||
|
||||
//private List<(string Caption, double Value)> GetData(DateTime dateTime)
|
||||
//{
|
||||
// return _orderRepository
|
||||
// .ReadOrders() // Получаем все заказы
|
||||
// .Where(order => order.Date.Date == dateTime.Date) // Фильтруем по дате заказа
|
||||
// .SelectMany(order => order.OrderService) // Разворачиваем список услуг в заказах
|
||||
// .GroupBy(service => service.ServiceId, (key, group) => new { ServiceId = key, Count = group.Sum(service => service.Quantity) }) // Группируем по ID услуги и суммируем количество
|
||||
// .Select(group =>
|
||||
// {
|
||||
// var service = _serviceRepository.ReadServiceById(group.ServiceId); // Получаем информацию о услуге по ID
|
||||
// return (service.ServiceType.ToString(), (double)group.Count); // Возвращаем название услуги и ее количество
|
||||
// })
|
||||
// .ToList();
|
||||
//}
|
||||
|
||||
private List<(string Caption, double Value)> GetData(DateTime dateTime)
|
||||
{
|
||||
// Получаем заказы за указанную дату
|
||||
var orders = _orderRepository
|
||||
.ReadOrders()
|
||||
.Where(order => order.Date.Date == dateTime.Date) // Фильтруем заказы по дате
|
||||
.ReadOrders(dateForm: dateTime.Date, dateTo:dateTime.Date.AddDays(1))
|
||||
.ToList();
|
||||
|
||||
// Группируем заказы по CompanyId и вычисляем общую сумму затрат
|
||||
var groupedOrders = orders
|
||||
.GroupBy(order => order.CompanyId)
|
||||
.Select(group => new
|
||||
{
|
||||
CompanyId = group.Key,
|
||||
TotalSpent = group.Sum(order => (double)order.Price) // Суммируем стоимость заказов
|
||||
TotalSpent = group.Sum(order => (double)order.Price)
|
||||
});
|
||||
|
||||
// Получаем названия компаний и формируем данные для диаграммы
|
||||
return groupedOrders
|
||||
.Select(group =>
|
||||
{
|
||||
var company = _companyRepository.ReadCompanyById(group.CompanyId); // Получаем информацию о компании
|
||||
var company = _companyRepository.ReadCompanyById(group.CompanyId);
|
||||
return (Caption: company.Name, Value: group.TotalSpent);
|
||||
})
|
||||
.ToList();
|
||||
|
@ -27,8 +27,9 @@ public class TableReport
|
||||
{
|
||||
new ExcelBuilder(filePath)
|
||||
.AddHeader("Сводка по обслуживанию услуг", 0, 3)
|
||||
.AddParagraph("за период", 0)
|
||||
.AddTable([10, 30, 30], GetData(employeeId, startDate, endDate))
|
||||
.AddParagraph($"за период c {startDate:dd.MM.yyyy} по {endDate: dd.MM.yyyy}", 0)
|
||||
|
||||
.AddTable([10, 10, 15, 15], GetData(employeeId, startDate, endDate))
|
||||
.Build();
|
||||
return true;
|
||||
}
|
||||
@ -43,13 +44,8 @@ public class TableReport
|
||||
{
|
||||
|
||||
var data = _orderRepository
|
||||
.ReadOrders()
|
||||
.Where(order =>
|
||||
order.Date >= startDate &&
|
||||
order.Date <= endDate &&
|
||||
order.OrderService.Any(service => service.ServiceId == serviceId))
|
||||
.ReadOrders(dateForm: startDate, dateTo: endDate, serviceId: serviceId)
|
||||
.SelectMany(order => order.OrderService
|
||||
.Where(service => service.ServiceId == serviceId)
|
||||
.Select(service => new
|
||||
{
|
||||
EmployeeId = _appointmentRepository
|
||||
@ -62,7 +58,7 @@ public class TableReport
|
||||
return new List<string[]>() { item }
|
||||
.Union(
|
||||
data
|
||||
.Select(x => new string[] { x.EmployeeId?.ToString() ?? "Не назначен", x.Date.ToString(), x.Quantity.ToString()}))
|
||||
.Select(x => new string[] { x.EmployeeId?.ToString() ?? "Не назначен", x.Date.ToString("dd.MM.yyyy"), x.Quantity.ToString()}))
|
||||
.Union(
|
||||
[["Всего", string.Empty, data.Sum(x => x.Quantity).ToString()]])
|
||||
.ToList();
|
||||
|
@ -4,7 +4,7 @@ namespace ITServiceManager.Repositories;
|
||||
|
||||
public interface IAppointmentRepository
|
||||
{
|
||||
IEnumerable<Appointment> ReadAppointments();
|
||||
IEnumerable<Appointment> ReadAppointments(int? employeeId = null, DateTime? dateFrom = null, DateTime? dateTo = null);
|
||||
Appointment ReadAppointmentById(int id);
|
||||
void CreateAppointment(Appointment appointment);
|
||||
void UpdateAppointment(Appointment appointment);
|
||||
|
@ -4,7 +4,7 @@ namespace ITServiceManager.Repositories;
|
||||
|
||||
public interface IOrderRepository
|
||||
{
|
||||
IEnumerable<Order> ReadOrders(DateTime? dateForm = null, DateTime? dateTo = null, int? orderId = null, int? companyId = null);
|
||||
IEnumerable<Order> ReadOrders(DateTime? dateForm = null, DateTime? dateTo = null, int? orderId = null, int? serviceId = null, int? companyId = null);
|
||||
void CreateOrder(Order order);
|
||||
void DeleteOrder(int id);
|
||||
}
|
||||
|
@ -15,14 +15,36 @@ public class AppointmentRepository : IAppointmentRepository
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
public IEnumerable<Appointment> ReadAppointments()
|
||||
public IEnumerable<Appointment> ReadAppointments(int? employeeId = null, DateTime? dateFrom = null, DateTime? dateTo = null)
|
||||
{
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
if (dateFrom.HasValue && dateTo.HasValue)
|
||||
{
|
||||
if (dateFrom.Value >= dateTo.Value)
|
||||
{
|
||||
throw new Exception("Дата начала должна быть раньше даты окончания");
|
||||
}
|
||||
}
|
||||
var builder = new QueryBuilder();
|
||||
if (employeeId.HasValue)
|
||||
{
|
||||
builder.AddCondition("ap.EmployeeId = @employeeId");
|
||||
}
|
||||
if (dateFrom.HasValue)
|
||||
{
|
||||
builder.AddCondition("ap.dateStart >= @dateFrom");
|
||||
}
|
||||
if (dateTo.HasValue)
|
||||
{
|
||||
builder.AddCondition("ap.dateEnd >= @dateTo");
|
||||
}
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = @"SELECT * FROM Appointments";
|
||||
var appointment = connection.Query<Appointment>(querySelect);
|
||||
var querySelect = @$"SELECT ap.*, e.Name as 'EmployeeName'
|
||||
FROM Employees ap JOIN Employees e ON e.id = ap.EmployeeId
|
||||
{builder.Build()}";
|
||||
var appointment = connection.Query<Appointment>(querySelect, new { employeeId, dateFrom, dateTo});
|
||||
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(appointment));
|
||||
return appointment;
|
||||
}
|
||||
|
@ -16,46 +16,54 @@ public class OrderRepository : IOrderRepository
|
||||
_connectionString = connectionString;
|
||||
_logger = logger;
|
||||
}
|
||||
public IEnumerable<Order> ReadOrders(DateTime? dateForm = null, DateTime? dateTo = null, int? serviceId = null, int? companyId = null)
|
||||
public IEnumerable<Order> ReadOrders(DateTime? dateFrom = null, DateTime? dateTo = null, int? serviceId = null, int? companyId = null)
|
||||
{
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var builder = new QueryBuilder();
|
||||
if (dateForm.HasValue)
|
||||
if (dateFrom.HasValue)
|
||||
{
|
||||
builder.AddCondition("fa.FeedingDate >= @dateForm");
|
||||
builder.AddCondition("Orders.Date >= @dateForm");
|
||||
}
|
||||
if (dateTo.HasValue)
|
||||
{
|
||||
builder.AddCondition("fa.FeedingDate <= @dateTo");
|
||||
builder.AddCondition("Orders.Date <= @dateTo");
|
||||
}
|
||||
if (feedId.HasValue)
|
||||
if (companyId.HasValue)
|
||||
{
|
||||
builder.AddCondition("fa.FeedId = @feedId");
|
||||
builder.AddCondition("Orders.CompanyId = @companyId");
|
||||
}
|
||||
if (employeeId.HasValue)
|
||||
{
|
||||
builder.AddCondition("fa.EmployeeId = @employeeId");
|
||||
}
|
||||
if (animalId.HasValue)
|
||||
{
|
||||
builder.AddCondition("fa.AnimalId = @animalId");
|
||||
}
|
||||
|
||||
var querySelect = @"
|
||||
SELECT o.*, os.serviceId, os.quantity
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = @$"
|
||||
SELECT o.*, c.Name as CompanyName, os.serviceId, os.quantity, s.ServiceType
|
||||
FROM Orders AS o
|
||||
LEFT JOIN Companies c on c.Id = o.CompanyId
|
||||
INNER JOIN Order_Service AS os ON o.id = os.orderId
|
||||
Where os.serviceId = @serviceId";
|
||||
var orders = connection.Query<TempOrder>(querySelect);
|
||||
LEFT JOIN Services s on s.Id = os.ServiceId
|
||||
Where os.serviceId = @serviceId
|
||||
{builder.Build}";
|
||||
var orderDict = new Dictionary<int, List<OrderService>>();
|
||||
|
||||
var orders = connection.Query<Order, OrderService, Order>(querySelect,
|
||||
(order, orderServices) =>
|
||||
{
|
||||
if (!orderDict.TryGetValue(order.Id, out var frr))
|
||||
{
|
||||
frr = [];
|
||||
orderDict.Add(order.Id, frr);
|
||||
}
|
||||
frr.Add(orderServices);
|
||||
return order;
|
||||
}, splitOn: "ServiceId", param: new { dateFrom, dateTo, serviceId, companyId });
|
||||
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(orders));
|
||||
return orders.GroupBy(x => x.Id, y => y,
|
||||
(key, value) =>
|
||||
Order.CreateOperation(value.First(),
|
||||
value.Select(z =>
|
||||
OrderService.CreateOperation(0, z.ServiceId, z.Quantity, z.ExecutionTime)))).ToList();
|
||||
|
||||
return orderDict.Select(x =>
|
||||
{
|
||||
var or = orders.First(y => y.Id == x.Key);
|
||||
or.SetServices(x.Value);
|
||||
return or;
|
||||
}).ToArray();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user