вторая часть
This commit is contained in:
parent
6cb8b9cf25
commit
afeabbee26
@ -25,8 +25,8 @@ public class ChartReport
|
||||
{
|
||||
new PdfBuilder(filePath)
|
||||
.AddHeader("Траты людей")
|
||||
.AddPieChart("Виды трат", GetData(expenseId, dateTime))
|
||||
.Build();
|
||||
.AddPieChart($"Траты за {dateTime:dd MMMM yyyy}", GetData(expenseId, dateTime))
|
||||
.Build();
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
@ -38,14 +38,13 @@ public class ChartReport
|
||||
private List<(string Caption, double Value)> GetData(int expenseId, DateTime dateTime)
|
||||
{
|
||||
return _peopleExpense
|
||||
.ReadPeopleExpense()
|
||||
.Where(x => x.DataReciept.Date == dateTime.Date)
|
||||
.GroupBy(x => x.PeopleId, (key, group) => new
|
||||
.ReadPeopleExpense(dateTime.Date, dateTime.Date.AddDays(1), expenseId)
|
||||
.GroupBy(x => x.PeopleName, (key, group) => new
|
||||
{
|
||||
Id = key,
|
||||
PeopleName = key,
|
||||
Count = group.Sum(x => x.ExpensePeopleExpenses.FirstOrDefault(y => y.ExpenseId == expenseId)?.Sum ?? 0)
|
||||
})
|
||||
.Select(x => (x.Id.ToString(), (double)x.Count))
|
||||
.Select(x => (x.PeopleName, (double)x.Count))
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ public class TableReport
|
||||
{
|
||||
new ExcelBuilder(filePath)
|
||||
.AddHeader("Сводка по движению денег", 0, 4)
|
||||
.AddParagraph("за период", 0)
|
||||
.AddParagraph($"за период с {startDate:dd.MM.yyyy} по {endDate:dd.MM.yyyy}",0)
|
||||
.AddTable([15, 10, 15, 15], GetData(incomeId, expenseId, startDate, endDate))
|
||||
.Build();
|
||||
return true;
|
||||
@ -40,21 +40,22 @@ public class TableReport
|
||||
private List<string[]> GetData(int incomeId,int expenseId, DateTime startDate, DateTime endDate)
|
||||
{
|
||||
var data = _peopleIncomeRepository
|
||||
.ReadPeopleIncome()
|
||||
.Where(x => x.DataReciept >= startDate && x.DataReciept <= endDate && x.IncomePeopleIncomes.Any(y => y.IncomeId == incomeId))
|
||||
.Select(x => new { x.PeopleId, Date = x.DataReciept, CountIn = x.IncomePeopleIncomes.FirstOrDefault(y => y.IncomeId == incomeId)?.Sum, CountOut = (int?)null })
|
||||
.ReadPeopleIncome(dateForm: startDate, dateTo: endDate, incomeId, expenseId)
|
||||
.Select(x => new { x.PeopleName, Date = x.DataReciept,
|
||||
CountIn = x.IncomePeopleIncomes.FirstOrDefault(y => y.IncomeId == incomeId)?.Sum, CountOut = (int?)null })
|
||||
.Union(
|
||||
_peopleExpenseRepository
|
||||
.ReadPeopleExpense()
|
||||
.Where(x => x.DataReciept >= startDate && x.DataReciept <= endDate && x.ExpensePeopleExpenses.Any(y => y.ExpenseId == expenseId))
|
||||
.Select(x => new { x.PeopleId, Date = x.DataReciept, CountIn = (int?)null, CountOut = x.ExpensePeopleExpenses.FirstOrDefault(y => y.ExpenseId == expenseId)?.Sum }))
|
||||
.OrderBy(x => x.Date);
|
||||
_peopleExpenseRepository
|
||||
.ReadPeopleExpense(dateForm: startDate, dateTo: endDate, incomeId, expenseId)
|
||||
.Select(x => new { x.PeopleName, Date = x.DataReciept, CountIn = (int?)null,
|
||||
CountOut = x.ExpensePeopleExpenses.FirstOrDefault(y => y.ExpenseId == expenseId)?.Sum }))
|
||||
.OrderBy(x => x.Date);
|
||||
return
|
||||
new List<string[]>() { item }
|
||||
.Union(data.Select(x => new string[] {x.PeopleId.ToString(), x.Date.ToString(), x.CountIn?.ToString() ?? string.Empty,x.CountOut?.ToString() ?? string.Empty }))
|
||||
.Union(
|
||||
[[ "Всего", "", data.Sum(x => x.CountIn ?? 0).ToString(), data.Sum(x => x.CountOut ?? 0).ToString() ]]
|
||||
)
|
||||
data.Select(x => new string[] {x.PeopleName, x.Date.ToString("dd.MM.yyyy"), x.CountIn?.ToString("N0") ?? string.Empty,
|
||||
x.CountOut?.ToString("N0") ?? string.Empty }))
|
||||
.Union(
|
||||
[[ "Всего", "", data.Sum(x => x.CountIn ?? 0).ToString("N0"), data.Sum(x => x.CountOut ?? 0).ToString("N0") ]])
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
|
@ -90,14 +90,58 @@ public class PeopleExpenseRepository : IPeopleExpense
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
var builder = new QueryBuilder();
|
||||
if (dateForm.HasValue)
|
||||
{
|
||||
builder.AddCondition("pe.DataReciept >= @dateForm");
|
||||
}
|
||||
if (dateTo.HasValue)
|
||||
{
|
||||
builder.AddCondition("pe.DataReciept <= @dateTo");
|
||||
}
|
||||
if (peopleId.HasValue)
|
||||
{
|
||||
builder.AddCondition("pe.PeopleId = @peopleId");
|
||||
}
|
||||
if (expenseId.HasValue)
|
||||
{
|
||||
builder.AddCondition("epe.ExpenseId = @expenseId");
|
||||
}
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = @"SELECT pe.*, epe.ExpenseId, epe.Sum FROM PeopleExpense pe
|
||||
INNER JOIN ExpensePeopleExpense epe on epe.PeopleExpenseId = pe.Id";
|
||||
var peopleExpenses = connection.Query<TempExpensePeopleExpense>(querySelect);
|
||||
var querySelect = $@"SELECT
|
||||
pe.*,
|
||||
CONCAT(p.Name, ' ', p.LastName) as PeopleName,
|
||||
epe.ExpenseId,
|
||||
epe.Sum,
|
||||
e.Name as ExpenseName
|
||||
FROM PeopleExpense pe
|
||||
LEFT JOIN People p on p.Id = pe.PeopleId
|
||||
INNER JOIN ExpensePeopleExpense epe on epe.PeopleExpenseId = pe.Id
|
||||
LEFT JOIN Expense e on e.Id = epe.ExpenseId
|
||||
{builder.Build()}";
|
||||
var expenseDict = new Dictionary<int, List<ExpensePeopleExpense>>();
|
||||
|
||||
var peopleExpenses = connection.Query<PeopleExpense, ExpensePeopleExpense, PeopleExpense>(querySelect,
|
||||
(expense, peopleExpenses) =>
|
||||
{
|
||||
if (!expenseDict.TryGetValue(expense.Id, out var epe))
|
||||
{
|
||||
epe = [];
|
||||
expenseDict.Add(expense.Id, epe);
|
||||
}
|
||||
|
||||
epe.Add(peopleExpenses);
|
||||
return expense;
|
||||
}, splitOn: "ExpenseId", param: new { dateForm, dateTo, peopleId, expenseId });
|
||||
_logger.LogDebug("Полученные объекты: {json}",
|
||||
JsonConvert.SerializeObject(peopleExpenses));
|
||||
return peopleExpenses.GroupBy(x => x.Id, y => y, (key, value) => PeopleExpense.CreateOperation(value.First(),
|
||||
value.Select(z => ExpensePeopleExpense.CreateElement(0, z.ExpenseId, z.Sum)))).ToList();
|
||||
|
||||
return expenseDict.Select(x =>
|
||||
{
|
||||
var pe = peopleExpenses.First(y => y.Id == x.Key);
|
||||
pe.SetExpensePeopleExpenses(x.Value);
|
||||
return pe;
|
||||
}).ToArray();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
@ -94,52 +94,56 @@ public class PeopleIncomeRepository : IPeopleIncome
|
||||
try
|
||||
{
|
||||
var builder = new QueryBuilder();
|
||||
if (dateFrom.HasValue)
|
||||
if (dateForm.HasValue)
|
||||
{
|
||||
builder.AddCondition("cf.Date >= @dateFrom");
|
||||
builder.AddCondition("pi.DataReciept >= @dateForm");
|
||||
}
|
||||
if (dateTo.HasValue)
|
||||
{
|
||||
builder.AddCondition("cf.Date <= @dateTo");
|
||||
builder.AddCondition("pi.DataReciept <= @dateTo");
|
||||
}
|
||||
if (fuelId.HasValue)
|
||||
if (peopleId.HasValue)
|
||||
{
|
||||
builder.AddCondition("cff.FuelId = @fuelId");
|
||||
builder.AddCondition("pi.PeopleId = @peopleId");
|
||||
}
|
||||
if (incomeId.HasValue)
|
||||
{
|
||||
builder.AddCondition("ipi.IncomeId = @incomeId");
|
||||
}
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = $@"SELECT
|
||||
cf.*,
|
||||
c.Name as ContractorName,
|
||||
cff.FuelId,
|
||||
cff.Quantity,
|
||||
f.Type as FuelName
|
||||
FROM ContractorFuel cf
|
||||
LEFT JOIN Contractor c on c.Id = cf.ContractorId
|
||||
INNER JOIN ContractorFuelFuel cff on cff.ContractorFuelId = cf.Id
|
||||
LEFT JOIN Fuel f on f.Id = cff.FuelId
|
||||
pi.*,
|
||||
CONCAT(p.Name, ' ', p.LastName) as PeopleName,
|
||||
ipi.IncomeId,
|
||||
ipi.Sum,
|
||||
i.Name as IncomeName
|
||||
FROM PeopleIncome pi
|
||||
LEFT JOIN People p on p.Id = pi.PeopleId
|
||||
INNER JOIN IncomePeopleIncome ipi on ipi.PeopleIncomeId = pi.Id
|
||||
LEFT JOIN Income i on i.Id = ipi.IncomeId
|
||||
{builder.Build()}";
|
||||
var contractorsDict = new Dictionary<int, List<ContractorFuelFuel>>();
|
||||
var incomeDict = new Dictionary<int, List<IncomePeopleIncome>>();
|
||||
|
||||
var contractorFuels = connection.Query<ContractorFuel, ContractorFuelFuel, ContractorFuel>(querySelect,
|
||||
(contractor, contractorFuel) =>
|
||||
var peopleIncomes = connection.Query<PeopleIncome, IncomePeopleIncome, PeopleIncome>(querySelect,
|
||||
(income, peopleIncomes) =>
|
||||
{
|
||||
if (!contractorsDict.TryGetValue(contractor.Id, out var ccf))
|
||||
if (!incomeDict.TryGetValue(income.Id, out var ipi))
|
||||
{
|
||||
ccf = [];
|
||||
contractorsDict.Add(contractor.Id, ccf);
|
||||
ipi = [];
|
||||
incomeDict.Add(income.Id, ipi);
|
||||
}
|
||||
|
||||
ccf.Add(contractorFuel);
|
||||
return contractor;
|
||||
}, splitOn: "FuelId", param: new { dateFrom, dateTo, fuelId });
|
||||
ipi.Add(peopleIncomes);
|
||||
return income;
|
||||
}, splitOn: "IncomeId", param: new { dateForm, dateTo, peopleId, incomeId });
|
||||
_logger.LogDebug("Полученные объекты: {json}",
|
||||
JsonConvert.SerializeObject(contractorFuels));
|
||||
JsonConvert.SerializeObject(peopleIncomes));
|
||||
|
||||
return contractorsDict.Select(x =>
|
||||
return incomeDict.Select(x =>
|
||||
{
|
||||
var cf = contractorFuels.First(y => y.Id == x.Key);
|
||||
cf.SetContractorFuelFuel(x.Value);
|
||||
return cf;
|
||||
var pi = peopleIncomes.First(y => y.Id == x.Key);
|
||||
pi.SetIncomePeopleIncomes(x.Value);
|
||||
return pi;
|
||||
}).ToArray();
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectFamilyBudget.Repositories.Implementations;
|
||||
|
||||
public 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}";
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user