final version, reports with names, loading many-to-many entity

This commit is contained in:
Ivan Gutorov 2024-12-24 15:43:04 +04:00
parent c62dd35f93
commit bce2e9612a
5 changed files with 40 additions and 33 deletions

View File

@ -14,7 +14,7 @@ public class Contract
public int ExecutorID { get; private set; }
[DisplayName("Заказчик")]
public int CustomerName { get; private set; }
public string CustomerName { get; private set; } = string.Empty;
[Browsable(false)]
public ExecutorPost ExecutorPost { get; private set; }
@ -60,21 +60,6 @@ public class Contract
};
}
public static Contract CreateEntity(TempServiceContract tempServiceContract, IEnumerable<Service> services)
{
return new Contract
{
Id = tempServiceContract.Id,
CustomerID = tempServiceContract.CustomerID,
ExecutorID = tempServiceContract.ExecutorID,
Category = tempServiceContract.Category,
ConclusionDate = tempServiceContract.ConclusionDate,
Deadline = tempServiceContract.Deadline,
PaymentAmount = tempServiceContract.PaymentAmount,
Services = services
};
}
public void SetServices(IEnumerable<Service> services)
{
if (services != null && services.Any())

View File

@ -12,11 +12,8 @@ public class ServiceContract
[Browsable(false)]
public int ContractId { get; private set; }
[DisplayName("Заказчик")]
public int CustomerName { get; private set; }
[DisplayName("Исполнитель")]
public int ExecutorName { get; private set; }
[DisplayName("Описание услуги")]
public string ServiceDescription { get; private set; } = string.Empty;
public static ServiceContract CreateElement(int id, int serviceId, int contractId)
{

View File

@ -39,7 +39,7 @@ internal class ChartReport
{
return _contractRepository
.ReadContracts(dateFrom: dateTime.Date, dateTo: dateTime.Date.AddDays(1))
.GroupBy(x => x.CustomerID, (key, group) => new
.GroupBy(x => x.CustomerName, (key, group) => new
{
Id = key,
Count = group.Sum(x => x.Services.Count())

View File

@ -46,21 +46,18 @@ internal class TableReport
.GroupBy(x => x.ConclusionDate)
.Select(x => new
{
x.First().ExecutorID,
x.First().ExecutorFullName,
Date = x.First().ConclusionDate,
CountIn = (int?)x.Count(),
CountOut = (int?)null
})
.Union(
_contractRepository
.ReadContracts()
.Where(x => x.Deadline >= startDate
&& x.Deadline <= endDate
&& x.CustomerID == customerId)
.ReadContracts(customerId: customerId, dateFrom: startDate, dateTo: endDate)
.GroupBy(x => x.Deadline)
.Select(x => new
{
x.First().ExecutorID,
x.First().ExecutorFullName,
Date = x.First().Deadline,
CountIn = (int?)null,
CountOut = (int?)x.Count()
@ -71,7 +68,7 @@ internal class TableReport
return new List<string[]>() { item }
.Union(data.Select(x => new string[] {
x.ExecutorID.ToString(),
x.ExecutorFullName,
x.Date.ToString(),
x.CountIn?.ToString() ?? string.Empty,
x.CountOut?.ToString() ?? string.Empty

View File

@ -101,10 +101,12 @@ public class ContractRepository : IContractRepository
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var querySelect = @$"
SELECT c.*, sc.ServiceId, s.Description,
SELECT c.*,
CONCAT(cu.Name, ' ', cu.Contact) as CustomerName,
e.Post as ExecutorPost,
e.Name as ExecutorName
e.Name as ExecutorName,
sc.ServiceId,
s.Description as ServiceDescription
FROM Contracts c
INNER JOIN ServiceContract sc ON sc.ContractId = c.Id
LEFT JOIN Services s ON sc.ServiceId = s.Id
@ -112,9 +114,35 @@ public class ContractRepository : IContractRepository
LEFT JOIN Executors e ON e.Id = c.ExecutorId
{builder.Build()};
";
var serviceDict = new Dictionary<int, List<Service>>();
var contracts = connection.Query<Contract, Service>(querySelect, new { dateFrom, dateTo, customerId, executorId });
var contractsDict = new Dictionary<int, List<ServiceContract>>();
var contracts = connection.Query<Contract, ServiceContract, Contract>(querySelect, (contract, serviceContract) =>
{
if (!contractsDict.TryGetValue(contract.Id, out var sc))
{
sc = [];
contractsDict.Add(contract.Id, sc);
}
sc.Add(serviceContract);
return contract;
}, splitOn: "ServiceId", param: new
{
dateFrom,
dateTo,
customerId,
executorId
});
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(contracts));
return contractsDict.Select(x =>
{
var c = contracts.First(y => y.Id == x.Key);
c.SetServices(x.Value.Select(x => Service.CreateEntity(x.ServiceId, x.ServiceDescription)));
return c;
}).ToArray();
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка при чтении объектов");