Лабораторная работа №4 Шаг 2/2
This commit is contained in:
parent
1e3874d74c
commit
dcaac2153e
@ -23,7 +23,7 @@ internal class ChartReport
|
||||
{
|
||||
new PdfBuilder(filePath)
|
||||
.AddHeader("Поставки топлива")
|
||||
.AddPieChart("Виды топлива", GetData(dateTime))
|
||||
.AddPieChart($"Поставленные виды топлива на {dateTime:dd.MM.yyyy}", GetData(dateTime))
|
||||
.Build();
|
||||
return true;
|
||||
}
|
||||
@ -39,8 +39,7 @@ internal class ChartReport
|
||||
.ToDictionary(f => f.Id, f => f.Type);
|
||||
|
||||
return _contractorFuelRepository
|
||||
.ReadContractorFuels()
|
||||
.Where(x => x.Date.Date == dateTime.Date)
|
||||
.ReadContractorFuels(dateFrom: dateTime.Date, dateTo: dateTime.Date.AddDays(1))
|
||||
.SelectMany(x => x.ContractorFuelFuel)
|
||||
.GroupBy(x => x.FuelId)
|
||||
.Select(g => (Caption: fuelNames[g.Key].ToString(), Value: (double)g.Sum(x => x.Quantity)))
|
||||
|
@ -23,7 +23,7 @@ internal class TableReport
|
||||
{
|
||||
new ExcelBuilder(filePath)
|
||||
.AddHeader("Сводка по движению топлива", 0, 3)
|
||||
.AddParagraph("за период", 0)
|
||||
.AddParagraph($"за период {startDate:dd.MM.yyyy} по {endDate:dd.MM.yyyy}", 0)
|
||||
.AddTable([10, 15, 15], GetData(fuelId, startDate, endDate))
|
||||
.Build();
|
||||
return true;
|
||||
@ -38,13 +38,11 @@ internal class TableReport
|
||||
private List<string[]> GetData(int fuelId, DateTime startDate, DateTime endDate)
|
||||
{
|
||||
var data = _contractorFuelRepository
|
||||
.ReadContractorFuels()
|
||||
.Where(x => x.Date >= startDate && x.Date <= endDate && x.ContractorFuelFuel.Any(y => y.FuelId == fuelId))
|
||||
.ReadContractorFuels(startDate, endDate, fuelId)
|
||||
.Select(x => new { x.Date, CountIn = x.ContractorFuelFuel.FirstOrDefault(y => y.FuelId == fuelId)?.Quantity, CountOut = (int?)null})
|
||||
.Union(
|
||||
_fuelSaleRepository
|
||||
.ReadFuelSales()
|
||||
.Where(x => x.SaleDate >= startDate && x.SaleDate <= endDate && x.FuelFuelSale.Any(y => y.FuelId == fuelId))
|
||||
.ReadFuelSales(startDate, endDate, fuelId)
|
||||
.Select(x => new { Date = x.SaleDate, CountIn = (int?)null, CountOut = x.FuelFuelSale.FirstOrDefault(y => y.FuelId == fuelId)?.Quantity })
|
||||
)
|
||||
.OrderBy(x => x.Date);
|
||||
@ -62,9 +60,9 @@ internal class TableReport
|
||||
return
|
||||
new List<string[]>() { item }
|
||||
.Union( groupedData
|
||||
.Select(x => new string[] { x.Date.ToString("dd.MM.yyyy"), x.TotalIn.ToString()!, x.TotalOut.ToString()! }))
|
||||
.Select(x => new string[] { x.Date.ToString("dd.MM.yyyy"), x.TotalIn?.ToString("N0") ?? string.Empty, x.TotalOut?.ToString("N0") ?? string.Empty }))
|
||||
.Union(
|
||||
new[] { new string[] { "Всего", groupedData.Sum(x => x.TotalIn).ToString()!, groupedData.Sum(x => x.TotalOut).ToString()! } }
|
||||
new[] { new string[] { "Всего", groupedData.Sum(x => x.TotalIn)?.ToString("N0") ?? string.Empty, groupedData.Sum(x => x.TotalOut)?.ToString("N0") ?? string.Empty } }
|
||||
)
|
||||
.ToList();
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ namespace ProjectGasStation.Repositories;
|
||||
|
||||
public interface IContractorFuelRepository
|
||||
{
|
||||
IEnumerable<ContractorFuel> ReadContractorFuels(DateTime? dateFrom = null, DateTime? dateTo = null, int? contractorId = null,
|
||||
IEnumerable<ContractorFuel> ReadContractorFuels(DateTime? dateFrom = null, DateTime? dateTo = null,
|
||||
int? fuelId = null);
|
||||
void CreateContractorFuel(ContractorFuel contractorFuel);
|
||||
void DeleteContractorFuel(int id);
|
||||
|
@ -4,8 +4,8 @@ namespace ProjectGasStation.Repositories;
|
||||
|
||||
public interface IFuelSaleRepository
|
||||
{
|
||||
IEnumerable<FuelSale> ReadFuelSales(DateTime? dateFrom = null, DateTime? dateTo = null, int? gasStationId = null,
|
||||
int? fuelId = null, int? salespersonId = null, int? shiftId = null);
|
||||
IEnumerable<FuelSale> ReadFuelSales(DateTime? dateFrom = null, DateTime? dateTo = null,
|
||||
int? fuelId = null);
|
||||
void CreateFuelSale(FuelSale fuel);
|
||||
void DeleteFuelSale(int id);
|
||||
}
|
||||
|
@ -86,14 +86,27 @@ public class ContractorFuelRepository : IContractorFuelRepository
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<ContractorFuel> ReadContractorFuels(DateTime? dateFrom = null, DateTime? dateTo = null, int? contractorId = null,
|
||||
public IEnumerable<ContractorFuel> ReadContractorFuels(DateTime? dateFrom = null, DateTime? dateTo = null,
|
||||
int? fuelId = null)
|
||||
{
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
var builder = new QueryBuilder();
|
||||
if (dateFrom.HasValue)
|
||||
{
|
||||
builder.AddCondition("cf.Date >= @dateFrom");
|
||||
}
|
||||
if (dateTo.HasValue)
|
||||
{
|
||||
builder.AddCondition("cf.Date <= @dateTo");
|
||||
}
|
||||
if (fuelId.HasValue)
|
||||
{
|
||||
builder.AddCondition("cff.FuelId = @fuelId");
|
||||
}
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = @"SELECT
|
||||
var querySelect = $@"SELECT
|
||||
cf.*,
|
||||
c.Name as ContractorName,
|
||||
cff.FuelId,
|
||||
@ -102,7 +115,8 @@ public class ContractorFuelRepository : IContractorFuelRepository
|
||||
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";
|
||||
LEFT JOIN Fuel f on f.Id = cff.FuelId
|
||||
{builder.Build()}";
|
||||
var contractorsDict = new Dictionary<int, List<ContractorFuelFuel>>();
|
||||
|
||||
var contractorFuels = connection.Query<ContractorFuel, ContractorFuelFuel, ContractorFuel>(querySelect,
|
||||
@ -116,7 +130,7 @@ public class ContractorFuelRepository : IContractorFuelRepository
|
||||
|
||||
ccf.Add(contractorFuel);
|
||||
return contractor;
|
||||
}, splitOn: "FuelId");
|
||||
}, splitOn: "FuelId", param: new { dateFrom, dateTo, fuelId });
|
||||
_logger.LogDebug("Полученные объекты: {json}",
|
||||
JsonConvert.SerializeObject(contractorFuels));
|
||||
|
||||
|
@ -55,13 +55,26 @@ public class FuelSaleRepository : IFuelSaleRepository
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<FuelSale> ReadFuelSales(DateTime? dateFrom = null, DateTime? dateTo = null, int? gasStationId = null, int? fuelId = null, int? salespersonId = null, int? shiftId = null)
|
||||
public IEnumerable<FuelSale> ReadFuelSales(DateTime? dateFrom = null, DateTime? dateTo = null, int? fuelId = null)
|
||||
{
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
var builder = new QueryBuilder();
|
||||
if (dateFrom.HasValue)
|
||||
{
|
||||
builder.AddCondition("fs.SaleDate >= @dateFrom");
|
||||
}
|
||||
if (dateTo.HasValue)
|
||||
{
|
||||
builder.AddCondition("fs.SaleDate <= @dateTo");
|
||||
}
|
||||
if (fuelId.HasValue)
|
||||
{
|
||||
builder.AddCondition("ffs.FuelId = @fuelId");
|
||||
}
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = @"SELECT
|
||||
var querySelect = $@"SELECT
|
||||
fs.*,
|
||||
CONCAT(s.LastName, ' ', s.FirstName) as SalespersonName,
|
||||
sh.Type as TypeShift,
|
||||
@ -72,7 +85,8 @@ public class FuelSaleRepository : IFuelSaleRepository
|
||||
LEFT JOIN Salesperson s on s.Id = fs.SalespersonId
|
||||
LEFT JOIN Shift sh on sh.id = fs.ShiftId
|
||||
INNER JOIN FuelFuelSale ffs on ffs.FuelSaleId = fs.Id
|
||||
LEFT JOIN Fuel f on f.id = ffs.FuelId";
|
||||
LEFT JOIN Fuel f on f.id = ffs.FuelId
|
||||
{builder.Build()}";
|
||||
var salesDict = new Dictionary<int, List<FuelFuelSale>>();
|
||||
|
||||
var fuelSales = connection.Query<FuelSale, FuelFuelSale, FuelSale>(querySelect,
|
||||
@ -86,7 +100,7 @@ public class FuelSaleRepository : IFuelSaleRepository
|
||||
|
||||
fss.Add(fuelSale);
|
||||
return sale;
|
||||
}, splitOn: "FuelId");
|
||||
}, splitOn: "FuelId", param: new { dateFrom, dateTo, fuelId });
|
||||
_logger.LogDebug("Полученные объекты: {json}",
|
||||
JsonConvert.SerializeObject(fuelSales));
|
||||
|
||||
|
@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ProjectGasStation.Repositories.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}";
|
||||
}
|
||||
}
|
||||
|
@ -83,9 +83,18 @@ public class ShiftRepository : IShiftRepository
|
||||
_logger.LogInformation("Получение всех объектов");
|
||||
try
|
||||
{
|
||||
var builder = new QueryBuilder();
|
||||
if (dateFrom.HasValue)
|
||||
{
|
||||
builder.AddCondition("Date >= @dateFrom");
|
||||
}
|
||||
if (dateTo.HasValue)
|
||||
{
|
||||
builder.AddCondition("Date <= @dateTo");
|
||||
}
|
||||
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
||||
var querySelect = "SELECT * FROM Shift";
|
||||
var shifts = connection.Query<Shift>(querySelect);
|
||||
var querySelect = $"SELECT * FROM Shift {builder.Build()}";
|
||||
var shifts = connection.Query<Shift>(querySelect, new { dateFrom, dateTo });
|
||||
_logger.LogDebug("Полученные объекты: {json}",
|
||||
JsonConvert.SerializeObject(shifts));
|
||||
return shifts;
|
||||
|
Loading…
Reference in New Issue
Block a user