Все работает, кроме подсчета суммы и нужно формочку поменять

This commit is contained in:
Tonb73 2024-12-07 10:47:04 +03:00
parent 9226bb13b9
commit 368960a0f4
7 changed files with 95 additions and 25 deletions

View File

@ -103,20 +103,38 @@ WHERE Id = @Id";
}
}
public IEnumerable<AddMoney> ReadAddMoneys()
public IEnumerable<AddMoney> ReadAddMoneys(DateTime? dateFrom = null, DateTime? dateTo = null, int? addMoneyId = null, int? clientId = null)
{
_logger.LogInformation("Получение всех объектов");
try
{
var builder = new QueryBuilder();
if (dateFrom.HasValue)
{
builder.AddCondition("am.Date >= @dateFrom");
}
if (dateTo.HasValue)
{
builder.AddCondition("am.Date <= @dateTo");
}
if (addMoneyId.HasValue)
{
builder.AddCondition("am.Id =@addMoneyId");
}
if (clientId.HasValue)
{
builder.AddCondition("am.ClientId =@clientId");
}
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var querySelect = @"SELECT
var querySelect = $@"SELECT
am.*,
c.FullName AS ClientName
FROM
AddMoneys am
LEFT JOIN
Clients c ON c.Id = am.ClientId";
var AddMoneys = connection.Query<AddMoney>(querySelect);
Clients c ON c.Id = am.ClientId
{builder.Build()}";
var AddMoneys = connection.Query<AddMoney>(querySelect, new {dateFrom, dateTo, addMoneyId, clientId});
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(AddMoneys));
return AddMoneys;

View File

@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectTourAgency.Implementations;
internal class QueryBuilder
{
private readonly StringBuilder _builder;
public QueryBuilder()
{
_builder = new();
}
public QueryBuilder AddCondition(string condition)
{
if(_builder.Length > 0)
{
_builder.Append(" AND ");
}
_builder.Append(condition);
return this;
}
public string Build()
{
if (_builder.Length == 0)
{
return string.Empty;
}
return $"WHERE {_builder}";
}
}

View File

@ -81,8 +81,24 @@ WHERE Id = @id";
_logger.LogInformation("Получение всех объектов");
try
{
var builder = new QueryBuilder();
if (dateFrom.HasValue)
{
builder.AddCondition("t.DepartureDate >= @dateFrom");
}
if (dateTo.HasValue)
{
builder.AddCondition("t.DepartureDate <= @dateTo");
}
if(tourId.HasValue)
{
builder.AddCondition("t.Id = @tourId");
}
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var querySelect = @"
var querySelect = $@"
SELECT
t.*,
CONCAT(r.Departure,'-', r.Destination) AS RouteName,
@ -95,12 +111,12 @@ FROM
LEFT JOIN
Routes r ON r.Id = t.RouteId
LEFT JOIN
Employees e ON e.Id = t.EmployeeId -- соединяем с таблицей Employees
Employees e ON e.Id = t.EmployeeId
LEFT JOIN
ClientTour ct ON ct.TourId = t.Id -- соединяем с таблицей ClientTour
ClientTour ct ON ct.TourId = t.Id
LEFT JOIN
Clients c ON c.Id = ct.ClientId;
";
Clients c ON c.Id = ct.ClientId
{builder.Build()}";
var clientTourDict = new Dictionary<int, List<ClientTour>>();
@ -114,7 +130,7 @@ LEFT JOIN
}
ct.Add(tours);
return clientTour;
}, splitOn: "ClientId");
}, splitOn: "ClientId",param: new {dateFrom, dateTo, tourId, clientId});
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(tours));
return clientTourDict.Select(x =>

View File

@ -43,8 +43,7 @@ internal class ChartReport
private List<(string Caption, double Value)> GetData(DateTime dateTime)
{
return _addMoneyRepository
.ReadAddMoneys()
.Where(x => x.Date.Date == dateTime.Date)
.ReadAddMoneys(dateFrom: dateTime.Date, dateTo: dateTime.Date.AddDays(1))
.GroupBy(x => x.ClientId, (key, group) => new { Id = key, Count = group.Count() })
.Select(x => (x.Id.ToString(), (double)x.Count))
.ToList();

View File

@ -31,7 +31,7 @@ namespace ProjectTourAgency.Reports
{
new ExcelBuilder(filePath)
.AddHeader("Сводка по движению корма", 0, 4)
.AddParagraph("за период", 0)
.AddParagraph($"за период с {startDate:dd.MM.yyyy} по {endDate:dd.MM.yyyy}", 0)
.AddTable(new[] { 10, 10, 15, 15 }, GetData(tourId, startDate, endDate))
.Build();
return true;
@ -46,25 +46,25 @@ namespace ProjectTourAgency.Reports
private List<string[]> GetData(int tourId, DateTime startDate, DateTime endDate)
{
var tourData = _tourRepository.ReadTours()
.Where(x => x.DepartureDate >= startDate && x.DepartureDate <= endDate && x.ClientTours.Any(y => y.TourId == tourId))
var tourData = _tourRepository.ReadTours(startDate, endDate, tourId)
.SelectMany(x => x.ClientTours
.Where(y => y.TourId == tourId)
.Select(y => new {
ClientId = (int?)y.ClientId,
ClientId = y.ClientId,
ClientName = y.CLientName,
Date = x.DepartureDate,
CountIn = (int?)null,
CountOut = (int?)y.Cost
}));
var clientIds = tourData.Select(x => x.ClientId).Distinct().ToList();
var clientId = tourData.Select(x =>x.ClientId).Distinct().ToList();
var addMoneyData = _addMoneyRepository.ReadAddMoneys()
.Where(x => x.Date >= startDate && x.Date <= endDate && clientIds.Contains(x.ClientId))
var addMoneyData = _addMoneyRepository.ReadAddMoneys(startDate, endDate)
.Where(x => clientId.Contains(x.ClientId))
.Select(x => new {
ClientId = (int?)x.ClientId,
ClientId = x.ClientId,
ClientName = x.ClientName,
Date = x.Date,
CountIn = (int?)x.MoneyAmount,
CountOut = (int?)null
@ -77,15 +77,15 @@ namespace ProjectTourAgency.Reports
return new List<string[]>() { item }
.Union(data.Select(x => new string[] {
x.ClientId.ToString(),
x.ClientName.ToString(),
x.Date.ToString(),
x.CountIn?.ToString() ?? string.Empty,
x.CountOut?.ToString() ?? string.Empty }))
.Union(new[] { new string[] {
"Всего",
"",
data.Sum(x => x.CountIn ?? 0).ToString(),
data.Sum(x => x.CountOut ?? 0).ToString()
data.Sum(x => x.CountIn ?? 0).ToString("NO"),
data.Sum(x => x.CountOut ?? 0).ToString("NO")
}})
.ToList();
}

View File

@ -9,7 +9,7 @@ namespace ProjectTourAgency.Repositories;
public interface IAddMoneyRepository
{
IEnumerable<AddMoney> ReadAddMoneys();
IEnumerable<AddMoney> ReadAddMoneys(DateTime? dateFrom = null, DateTime? dateTo = null, int? addMoneyId = null, int? clientId = null);
AddMoney ReadAddMoneyById(int id);