первый этап сделан, проверить sql запрос ffr

This commit is contained in:
Baryshev Dmitry 2024-12-18 15:04:16 +04:00
parent 288050d220
commit aacae48a73
9 changed files with 85 additions and 27 deletions

View File

@ -11,6 +11,7 @@ public class FuelFuelReplenishment
public int Id { get; private set; } public int Id { get; private set; }
public int FuelId { get; private set; } public int FuelId { get; private set; }
public int Amount { get; private set; } public int Amount { get; private set; }
public string FuelName { get; private set; } = string.Empty;
public static FuelFuelReplenishment CreateElement(int id, int fuelId, int amount) public static FuelFuelReplenishment CreateElement(int id, int fuelId, int amount)
{ {
return new FuelFuelReplenishment return new FuelFuelReplenishment

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -9,10 +10,23 @@ namespace ProjectGarage.Entities;
public class FuelReplenishment public class FuelReplenishment
{ {
public int Id { get; private set; } public int Id { get; private set; }
[Browsable(false)]
public int DriverId { get; private set; } public int DriverId { get; private set; }
[DisplayName("Водитель")]
public string DriverName { get; private set; } = string.Empty;
[DisplayName("Дата поставки")]
public DateTime ReplenishmentDate { get; private set; } public DateTime ReplenishmentDate { get; private set; }
[DisplayName("Топлива")]
public string Fuel => FuelFuelReplenishments != null ?
string.Join(", ", FuelFuelReplenishments.Select(x => $"{x.FuelName} {x.Amount}")) : string.Empty;
public IEnumerable<FuelFuelReplenishment> FuelFuelReplenishments { get; private set;} = []; public IEnumerable<FuelFuelReplenishment> FuelFuelReplenishments { get; private set;} = [];
[Browsable(false)]
public static FuelReplenishment CreateOpeartion(int id, int driverId, IEnumerable<FuelFuelReplenishment> fuelFuelReplenishments) public static FuelReplenishment CreateOpeartion(int id, int driverId, IEnumerable<FuelFuelReplenishment> fuelFuelReplenishments)
{ {
return new FuelReplenishment return new FuelReplenishment
@ -24,14 +38,11 @@ public class FuelReplenishment
}; };
} }
public static FuelReplenishment CreateOpeartion(TempFuelReplenishment tempFuelReplenishment, IEnumerable<FuelFuelReplenishment> fuelFuelReplenishments) public void SetFuelFuelReplenishments(IEnumerable<FuelFuelReplenishment> fuelFuelReplenishments)
{ {
return new FuelReplenishment if (fuelFuelReplenishments != null && fuelFuelReplenishments.Any())
{ {
Id = tempFuelReplenishment.Id, FuelFuelReplenishments = fuelFuelReplenishments;
DriverId = tempFuelReplenishment.DriverId, }
ReplenishmentDate = tempFuelReplenishment.ReplenishmentDate,
FuelFuelReplenishments = fuelFuelReplenishments
};
} }
} }

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -10,15 +11,29 @@ public class Transportation
{ {
public int Id { get; set; } public int Id { get; set; }
[Browsable(false)]
public int FuelId { get; set; } public int FuelId { get; set; }
[Browsable(false)]
public int RouteId { get; set; } public int RouteId { get; set; }
[Browsable(false)]
public int DriverId { get; set; } public int DriverId { get; set; }
public int Amount { get; set; } [DisplayName("Топливо")]
public string FuelName { get; private set; } = string.Empty;
public DateTime TransportationDate { get; set; } [DisplayName("Водитель")]
public string DriverName { get; private set; } = string.Empty;
[DisplayName("Маршрут")]
public string RouteName { get; private set; } = string.Empty;
[DisplayName("Дата кормежки")]
public DateTime TransportationDate { get; private set; }
[DisplayName("Кол-во топлива")]
public int Amount { get; set; }
public static Transportation CreateTransportation(int id, int fuel_id, int route_id, int driver_id, int amount) public static Transportation CreateTransportation(int id, int fuel_id, int route_id, int driver_id, int amount)
{ {

View File

@ -35,11 +35,10 @@ public class ExcelBuilder
public ExcelBuilder AddHeader(string header, int startIndex, int count) public ExcelBuilder AddHeader(string header, int startIndex, int count)
{ {
CreateCell(startIndex, _rowIndex, header, CreateCell(startIndex, _rowIndex, header,
StyleIndex.SimpleTextWithoutBorder); StyleIndex.BoldTextWithBorder);
for (int i = startIndex + 1; i < startIndex + count; ++i) for (int i = startIndex + 1; i < startIndex + count; ++i)
{ {
CreateCell(i, _rowIndex, "", CreateCell(i, _rowIndex, "", StyleIndex.SimpleTextWithoutBorder);
StyleIndex.SimpleTextWithoutBorder);
} }
_mergeCells.Append(new MergeCell() _mergeCells.Append(new MergeCell()
{ {
@ -54,7 +53,7 @@ public class ExcelBuilder
public ExcelBuilder AddParagraph(string text, int columnIndex) public ExcelBuilder AddParagraph(string text, int columnIndex)
{ {
CreateCell(columnIndex, _rowIndex++, text, CreateCell(columnIndex, _rowIndex++, text,
StyleIndex.SimpleTextWithoutBorder); StyleIndex.SimpleTextWithBorder);
return this; return this;
} }
public ExcelBuilder AddTable(int[] columnsWidths, List<string[]> data) public ExcelBuilder AddTable(int[] columnsWidths, List<string[]> data)
@ -83,7 +82,7 @@ public class ExcelBuilder
for (var j = 0; j < data.First().Length; ++j) for (var j = 0; j < data.First().Length; ++j)
{ {
CreateCell(j, _rowIndex, data.First()[j], CreateCell(j, _rowIndex, data.First()[j],
StyleIndex.SimpleTextWithoutBorder); StyleIndex.BoldTextWithBorder);
} }
_rowIndex++; _rowIndex++;
for (var i = 1; i < data.Count - 1; ++i) for (var i = 1; i < data.Count - 1; ++i)
@ -91,14 +90,14 @@ public class ExcelBuilder
for (var j = 0; j < data[i].Length; ++j) for (var j = 0; j < data[i].Length; ++j)
{ {
CreateCell(j, _rowIndex, data[i][j], CreateCell(j, _rowIndex, data[i][j],
StyleIndex.SimpleTextWithoutBorder); StyleIndex.SimpleTextWithBorder);
} }
_rowIndex++; _rowIndex++;
} }
for (var j = 0; j < data.Last().Length; ++j) for (var j = 0; j < data.Last().Length; ++j)
{ {
CreateCell(j, _rowIndex, data.Last()[j], CreateCell(j, _rowIndex, data.Last()[j],
StyleIndex.SimpleTextWithoutBorder); StyleIndex.BoldTextWithBorder);
} }
_rowIndex++; _rowIndex++;
return this; return this;

View File

@ -7,6 +7,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data.SqlClient; using System.Data.SqlClient;
using System.Linq; using System.Linq;
using System.Runtime.Intrinsics.X86;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -78,23 +79,46 @@ VALUES (@ReplenishmentId, @FuelId, @Amount)";
} }
} }
public IEnumerable<FuelReplenishment> ReadFuelReplenishment(DateTime? dateForm = null, public IEnumerable<FuelReplenishment> ReadFuelReplenishment(DateTime? dateForm = null,
DateTime? dateTo = null, int? fuelId = null, int? driverId = null, int? routeId = null) DateTime? dateTo = null, int? fuelId = null, int? driverId = null, int? routeId = null)
{ {
_logger.LogInformation("Получение всех объектов"); _logger.LogInformation("Получение всех объектов");
try try
{ {
using var connection = new using var connection = new
NpgsqlConnection(_connectionString.ConnectionString); NpgsqlConnection(_connectionString.ConnectionString);
var querySelect = @"SELECT fr.*, ffr.FuelId, ffr.Amount var querySelect = @"SELECT
fr.*,
CONCAT(d.Fname, ' ', d.Lname) as DriverName,
ffr.FuelId,
ffr.Amount,
CONCAT(f.FuelName, ' ', f.Price) as FuelName
FROM fuelreplenishment fr FROM fuelreplenishment fr
INNER JOIN fuel_fuelReplenishment ffr ON ffr.ReplenishmentId = fr.Id"; LEFT JOIN driver d on d.Id = fr.DriverId
var replenishments = connection.Query<TempFuelReplenishment>(querySelect); INNER JOIN fuel_fuelreplenishment ffr ON ffr.ReplenishmentId = fr.Id
_logger.LogDebug("Полученные объекты: {json}", LEFT JOIN fuel f on f.Id = ffr.FuelId";
JsonConvert.SerializeObject(replenishments)); var replenishmentDict = new Dictionary<int, List<FuelFuelReplenishment>>();
return replenishments.GroupBy(x => x.Id, y => y,
(key, value) => FuelReplenishment.CreateOpeartion(value.First(), var fuelReplenishment = connection.Query<FuelReplenishment, FuelFuelReplenishment, FuelReplenishment>(querySelect,
value.Select(z => FuelFuelReplenishment.CreateElement(0, z.FuelId, z.Amount)))).ToList(); (replenishment, fuelReplenishments) =>
{
if (!replenishmentDict.TryGetValue(replenishment.Id, out var ffr))
{
ffr = [];
replenishmentDict.Add(replenishment.Id, ffr);
}
ffr.Add(fuelReplenishments);
return replenishment;
}, splitOn: "FuelId", param: new { dateForm, dateTo, fuelId, driverId });
_logger.LogDebug("Полученные объекты: {json}", JsonConvert.SerializeObject(fuelReplenishment));
return replenishmentDict.Select(x =>
{
var fr = fuelReplenishment.First(y => y.Id == x.Key);
fr.SetFuelFuelReplenishments(x.Value);
return fr;
}).ToArray();
} }
catch (Exception ex) catch (Exception ex)
{ {

View File

@ -51,7 +51,15 @@ VALUES (@DriverId, @RouteId, @FuelId, @Amount, @TransportationDate)";
try try
{ {
using var connection = new NpgsqlConnection(_connectionString.ConnectionString); using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var querySelect = "SELECT * FROM transportation"; var querySelect = @"SELECT
t.*,
r.RouteName as RouteName,
CONCAT(d.Fname, ' ', d.Lname) as DriverName,
CONCAT(f.FuelName, ' ', f.Price) as FuelName
FROM transportation t
LEFT JOIN driver d ON d.Id = t.DriverId
LEFT JOIN fuel f ON f.Id = t.FuelId
LEFT JOIN route r ON r.Id = t.RouteId";
var transportations = connection.Query<Transportation>(querySelect); var transportations = connection.Query<Transportation>(querySelect);
_logger.LogDebug("Полученные объекты: {json}", _logger.LogDebug("Полученные объекты: {json}",
JsonConvert.SerializeObject(transportations)); JsonConvert.SerializeObject(transportations));

Binary file not shown.

Binary file not shown.

Binary file not shown.