лаб 4 финиш

This commit is contained in:
Anya 2025-02-10 15:40:11 +04:00
parent 7de0767e00
commit d2c14ef22c
10 changed files with 119 additions and 178 deletions

View File

@ -15,5 +15,5 @@ public enum Driver_License
C = 4, C = 4,
D = 8, D = 8,
BE = 16, BE = 16,
CE = 32 CE = 32,
} }

View File

@ -1,47 +1,19 @@
using System.ComponentModel; using ProjectFuel.Entities.Enums;
using ProjectFuel.Entities.Enums;
using ProjectFuel.Entities.Enums;
using ProjectFuel.Entities;
namespace ProjectFuel.Entities; namespace ProjectFuel.Entities;
public class Trip public class Trip
{ {
public int Trip_ID { get; private set; } public int Trip_ID { get; private set; }
[DisplayName("Дата начала")]
public DateTime Start_Date { get; private set; } public DateTime Start_Date { get; private set; }
[DisplayName("Дата конца")]
public DateTime End_Date { get; private set; } public DateTime End_Date { get; private set; }
[DisplayName("Смена")]
public Shift Shift { get; private set; } public Shift Shift { get; private set; }
[DisplayName("Израсходованное топливо")]
public float Fuel_Consumption { get; private set; } public float Fuel_Consumption { get; private set; }
[Browsable(false)]
public int Car_ID { get; private set; } public int Car_ID { get; private set; }
[DisplayName("Автомобиль")]
public string CarName { get; private set; } = string.Empty;
[Browsable(false)]
public int Driver_ID { get; private set; } public int Driver_ID { get; private set; }
public IEnumerable<TripRoute> Routes { get; private set; } = [];
[DisplayName("Водитель")] public static Trip CreateOperation(int trip_id, DateTime start_date, DateTime end_date, Shift shift, float consumption, int car_id, int driver_id, IEnumerable<TripRoute> routes)
public string DriverName { get; private set; } = string.Empty;
[DisplayName("Маршруты")]
public string Route => Routes != null ?
string.Join(", ", Routes.Select(x => $"{x.RouteName}")) :
string.Empty;
[Browsable(false)]
public IEnumerable<Trip_Route> Routes { get; private set; } = [];
public static Trip CreateOperation(int trip_id, DateTime start_date, DateTime end_date, Shift shift, float consumption, int car_id, int driver_id, IEnumerable<Trip_Route> routes)
{ {
return new Trip return new Trip
{ {
@ -55,10 +27,4 @@ public class Trip
Routes = routes Routes = routes
}; };
} }
public void SetTrips(IEnumerable<Trip_Route> routes)
{
if (routes != null && routes.Any())
Routes = routes;
}
} }

View File

@ -6,18 +6,19 @@ using System.Threading.Tasks;
namespace ProjectFuel.Entities; namespace ProjectFuel.Entities;
public class Trip_Route public class TripRoute
{ {
public int Trip_ID { get; private set; } public int Trip_ID { get; private set; }
public int Route_ID { get; private set; } public int Route_ID { get; private set; }
public string RouteName { get; private set; } = string.Empty; public int Fuel_Init { get; private set; }
public static Trip_Route CreateElement(int trip_id, int route_id) public static TripRoute CreateOperation(int trip_Id, int route_ID, int fuel_Init)
{ {
return new Trip_Route return new TripRoute
{ {
Trip_ID = trip_id, Trip_ID = trip_Id,
Route_ID = route_id Route_ID = route_ID,
Fuel_Init = fuel_Init
}; };
} }
} }

View File

@ -30,13 +30,11 @@
{ {
label1 = new Label(); label1 = new Label();
label2 = new Label(); label2 = new Label();
label3 = new Label();
label4 = new Label(); label4 = new Label();
label5 = new Label(); label5 = new Label();
label6 = new Label(); label6 = new Label();
dateTimePickerStartDate = new DateTimePicker(); dateTimePickerStartDate = new DateTimePicker();
dateTimePickerEndDate = new DateTimePicker(); dateTimePickerEndDate = new DateTimePicker();
comboBoxShift = new ComboBox();
numericUpDownConsumptionRate = new NumericUpDown(); numericUpDownConsumptionRate = new NumericUpDown();
comboBoxCarID = new ComboBox(); comboBoxCarID = new ComboBox();
comboBoxDriverID = new ComboBox(); comboBoxDriverID = new ComboBox();
@ -44,8 +42,10 @@
buttonCancel = new Button(); buttonCancel = new Button();
groupBox = new GroupBox(); groupBox = new GroupBox();
dataGridViewRoutes = new DataGridView(); dataGridViewRoutes = new DataGridView();
label3 = new Label();
comboBoxShift = new ComboBox();
ColumnRoute = new DataGridViewComboBoxColumn(); ColumnRoute = new DataGridViewComboBoxColumn();
ColumnEndPoint = new DataGridViewTextBoxColumn(); ColumnFuelInit = new DataGridViewTextBoxColumn();
((System.ComponentModel.ISupportInitialize)numericUpDownConsumptionRate).BeginInit(); ((System.ComponentModel.ISupportInitialize)numericUpDownConsumptionRate).BeginInit();
groupBox.SuspendLayout(); groupBox.SuspendLayout();
((System.ComponentModel.ISupportInitialize)dataGridViewRoutes).BeginInit(); ((System.ComponentModel.ISupportInitialize)dataGridViewRoutes).BeginInit();
@ -54,108 +54,101 @@
// label1 // label1
// //
label1.AutoSize = true; label1.AutoSize = true;
label1.Location = new Point(52, 38); label1.Location = new Point(28, 18);
label1.Margin = new Padding(2, 0, 2, 0);
label1.Name = "label1"; label1.Name = "label1";
label1.Size = new Size(149, 32); label1.Size = new Size(74, 15);
label1.TabIndex = 0; label1.TabIndex = 0;
label1.Text = "Дата начала"; label1.Text = "Дата начала";
// //
// label2 // label2
// //
label2.AutoSize = true; label2.AutoSize = true;
label2.Location = new Point(61, 104); label2.Location = new Point(33, 49);
label2.Margin = new Padding(2, 0, 2, 0);
label2.Name = "label2"; label2.Name = "label2";
label2.Size = new Size(138, 32); label2.Size = new Size(68, 15);
label2.TabIndex = 1; label2.TabIndex = 1;
label2.Text = "Дата конца"; label2.Text = "Дата конца";
// //
// label3
//
label3.AutoSize = true;
label3.Location = new Point(62, 163);
label3.Name = "label3";
label3.Size = new Size(85, 32);
label3.TabIndex = 2;
label3.Text = "Смена";
//
// label4 // label4
// //
label4.AutoSize = true; label4.AutoSize = true;
label4.Location = new Point(59, 219); label4.Location = new Point(32, 103);
label4.Margin = new Padding(2, 0, 2, 0);
label4.Name = "label4"; label4.Name = "label4";
label4.Size = new Size(185, 32); label4.Size = new Size(93, 15);
label4.TabIndex = 3; label4.TabIndex = 3;
label4.Text = "Расход топлива"; label4.Text = "Расход топлива";
// //
// label5 // label5
// //
label5.AutoSize = true; label5.AutoSize = true;
label5.Location = new Point(55, 276); label5.Location = new Point(30, 129);
label5.Margin = new Padding(2, 0, 2, 0);
label5.Name = "label5"; label5.Name = "label5";
label5.Size = new Size(107, 32); label5.Size = new Size(55, 15);
label5.TabIndex = 4; label5.TabIndex = 4;
label5.Text = "Машина"; label5.Text = "Машина";
// //
// label6 // label6
// //
label6.AutoSize = true; label6.AutoSize = true;
label6.Location = new Point(61, 339); label6.Location = new Point(33, 159);
label6.Margin = new Padding(2, 0, 2, 0);
label6.Name = "label6"; label6.Name = "label6";
label6.Size = new Size(117, 32); label6.Size = new Size(58, 15);
label6.TabIndex = 5; label6.TabIndex = 5;
label6.Text = "Водитель"; label6.Text = "Водитель";
// //
// dateTimePickerStartDate // dateTimePickerStartDate
// //
dateTimePickerStartDate.Location = new Point(303, 31); dateTimePickerStartDate.Location = new Point(163, 15);
dateTimePickerStartDate.Margin = new Padding(2, 1, 2, 1);
dateTimePickerStartDate.Name = "dateTimePickerStartDate"; dateTimePickerStartDate.Name = "dateTimePickerStartDate";
dateTimePickerStartDate.Size = new Size(400, 39); dateTimePickerStartDate.Size = new Size(217, 23);
dateTimePickerStartDate.TabIndex = 6; dateTimePickerStartDate.TabIndex = 6;
// //
// dateTimePickerEndDate // dateTimePickerEndDate
// //
dateTimePickerEndDate.Location = new Point(295, 106); dateTimePickerEndDate.Location = new Point(159, 50);
dateTimePickerEndDate.Margin = new Padding(2, 1, 2, 1);
dateTimePickerEndDate.Name = "dateTimePickerEndDate"; dateTimePickerEndDate.Name = "dateTimePickerEndDate";
dateTimePickerEndDate.Size = new Size(400, 39); dateTimePickerEndDate.Size = new Size(217, 23);
dateTimePickerEndDate.TabIndex = 7; dateTimePickerEndDate.TabIndex = 7;
// //
// comboBoxShift
//
comboBoxShift.FormattingEnabled = true;
comboBoxShift.Location = new Point(292, 171);
comboBoxShift.Name = "comboBoxShift";
comboBoxShift.Size = new Size(242, 40);
comboBoxShift.TabIndex = 8;
//
// numericUpDownConsumptionRate // numericUpDownConsumptionRate
// //
numericUpDownConsumptionRate.Location = new Point(288, 232); numericUpDownConsumptionRate.Location = new Point(155, 109);
numericUpDownConsumptionRate.Margin = new Padding(2, 1, 2, 1);
numericUpDownConsumptionRate.Name = "numericUpDownConsumptionRate"; numericUpDownConsumptionRate.Name = "numericUpDownConsumptionRate";
numericUpDownConsumptionRate.Size = new Size(240, 39); numericUpDownConsumptionRate.Size = new Size(129, 23);
numericUpDownConsumptionRate.TabIndex = 9; numericUpDownConsumptionRate.TabIndex = 9;
// //
// comboBoxCarID // comboBoxCarID
// //
comboBoxCarID.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxCarID.FormattingEnabled = true; comboBoxCarID.FormattingEnabled = true;
comboBoxCarID.Location = new Point(281, 288); comboBoxCarID.Location = new Point(151, 135);
comboBoxCarID.Margin = new Padding(2, 1, 2, 1);
comboBoxCarID.Name = "comboBoxCarID"; comboBoxCarID.Name = "comboBoxCarID";
comboBoxCarID.Size = new Size(242, 40); comboBoxCarID.Size = new Size(132, 23);
comboBoxCarID.TabIndex = 10; comboBoxCarID.TabIndex = 10;
// //
// comboBoxDriverID // comboBoxDriverID
// //
comboBoxDriverID.FormattingEnabled = true; comboBoxDriverID.FormattingEnabled = true;
comboBoxDriverID.Location = new Point(274, 348); comboBoxDriverID.Location = new Point(148, 163);
comboBoxDriverID.Margin = new Padding(2, 1, 2, 1);
comboBoxDriverID.Name = "comboBoxDriverID"; comboBoxDriverID.Name = "comboBoxDriverID";
comboBoxDriverID.Size = new Size(242, 40); comboBoxDriverID.Size = new Size(132, 23);
comboBoxDriverID.TabIndex = 11; comboBoxDriverID.TabIndex = 11;
// //
// buttonTripSave // buttonTripSave
// //
buttonTripSave.Location = new Point(79, 649); buttonTripSave.Location = new Point(43, 304);
buttonTripSave.Margin = new Padding(2, 1, 2, 1);
buttonTripSave.Name = "buttonTripSave"; buttonTripSave.Name = "buttonTripSave";
buttonTripSave.Size = new Size(150, 46); buttonTripSave.Size = new Size(81, 22);
buttonTripSave.TabIndex = 14; buttonTripSave.TabIndex = 14;
buttonTripSave.Text = "Сохранить"; buttonTripSave.Text = "Сохранить";
buttonTripSave.UseVisualStyleBackColor = true; buttonTripSave.UseVisualStyleBackColor = true;
@ -163,9 +156,10 @@
// //
// buttonCancel // buttonCancel
// //
buttonCancel.Location = new Point(346, 649); buttonCancel.Location = new Point(186, 304);
buttonCancel.Margin = new Padding(2, 1, 2, 1);
buttonCancel.Name = "buttonCancel"; buttonCancel.Name = "buttonCancel";
buttonCancel.Size = new Size(150, 46); buttonCancel.Size = new Size(81, 22);
buttonCancel.TabIndex = 15; buttonCancel.TabIndex = 15;
buttonCancel.Text = "Отмена"; buttonCancel.Text = "Отмена";
buttonCancel.UseVisualStyleBackColor = true; buttonCancel.UseVisualStyleBackColor = true;
@ -174,9 +168,11 @@
// groupBox // groupBox
// //
groupBox.Controls.Add(dataGridViewRoutes); groupBox.Controls.Add(dataGridViewRoutes);
groupBox.Location = new Point(52, 421); groupBox.Location = new Point(28, 198);
groupBox.Margin = new Padding(2, 1, 2, 1);
groupBox.Name = "groupBox"; groupBox.Name = "groupBox";
groupBox.Size = new Size(410, 222); groupBox.Padding = new Padding(2, 1, 2, 1);
groupBox.Size = new Size(308, 104);
groupBox.TabIndex = 16; groupBox.TabIndex = 16;
groupBox.TabStop = false; groupBox.TabStop = false;
groupBox.Text = "Маршруты"; groupBox.Text = "Маршруты";
@ -185,38 +181,58 @@
// //
dataGridViewRoutes.AllowUserToResizeColumns = false; dataGridViewRoutes.AllowUserToResizeColumns = false;
dataGridViewRoutes.AllowUserToResizeRows = false; dataGridViewRoutes.AllowUserToResizeRows = false;
dataGridViewRoutes.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dataGridViewRoutes.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; dataGridViewRoutes.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
dataGridViewRoutes.Columns.AddRange(new DataGridViewColumn[] { ColumnRoute, ColumnEndPoint }); dataGridViewRoutes.Columns.AddRange(new DataGridViewColumn[] { ColumnRoute, ColumnFuelInit });
dataGridViewRoutes.Dock = DockStyle.Fill; dataGridViewRoutes.Dock = DockStyle.Fill;
dataGridViewRoutes.Location = new Point(3, 35); dataGridViewRoutes.Location = new Point(2, 17);
dataGridViewRoutes.Margin = new Padding(5); dataGridViewRoutes.Margin = new Padding(3, 2, 3, 2);
dataGridViewRoutes.MultiSelect = false; dataGridViewRoutes.MultiSelect = false;
dataGridViewRoutes.Name = "dataGridViewRoutes"; dataGridViewRoutes.Name = "dataGridViewRoutes";
dataGridViewRoutes.RowHeadersVisible = false; dataGridViewRoutes.RowHeadersVisible = false;
dataGridViewRoutes.RowHeadersWidth = 51; dataGridViewRoutes.RowHeadersWidth = 51;
dataGridViewRoutes.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataGridViewRoutes.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridViewRoutes.Size = new Size(404, 184); dataGridViewRoutes.Size = new Size(304, 86);
dataGridViewRoutes.TabIndex = 1; dataGridViewRoutes.TabIndex = 1;
// //
// label3
//
label3.AutoSize = true;
label3.Location = new Point(33, 76);
label3.Margin = new Padding(2, 0, 2, 0);
label3.Name = "label3";
label3.Size = new Size(43, 15);
label3.TabIndex = 2;
label3.Text = "Смена";
//
// comboBoxShift
//
comboBoxShift.FormattingEnabled = true;
comboBoxShift.Location = new Point(157, 80);
comboBoxShift.Margin = new Padding(2, 1, 2, 1);
comboBoxShift.Name = "comboBoxShift";
comboBoxShift.Size = new Size(132, 23);
comboBoxShift.TabIndex = 8;
//
// ColumnRoute // ColumnRoute
// //
ColumnRoute.FillWeight = 92.25092F;
ColumnRoute.HeaderText = "Маршрут"; ColumnRoute.HeaderText = "Маршрут";
ColumnRoute.MinimumWidth = 6; ColumnRoute.MinimumWidth = 6;
ColumnRoute.Name = "ColumnRoute"; ColumnRoute.Name = "ColumnRoute";
ColumnRoute.Width = 125;
// //
// ColumnEndPoint // ColumnFuelInit
// //
ColumnEndPoint.HeaderText = "Конечная точка"; ColumnFuelInit.FillWeight = 107.749069F;
ColumnEndPoint.MinimumWidth = 6; ColumnFuelInit.HeaderText = "Изначальное кол-во топлива";
ColumnEndPoint.Name = "ColumnEndPoint"; ColumnFuelInit.MinimumWidth = 6;
ColumnEndPoint.Width = 125; ColumnFuelInit.Name = "ColumnFuelInit";
// //
// FormTrip // FormTrip
// //
AutoScaleDimensions = new SizeF(13F, 32F); AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(843, 745); ClientSize = new Size(454, 349);
Controls.Add(groupBox); Controls.Add(groupBox);
Controls.Add(buttonCancel); Controls.Add(buttonCancel);
Controls.Add(buttonTripSave); Controls.Add(buttonTripSave);
@ -232,6 +248,7 @@
Controls.Add(label3); Controls.Add(label3);
Controls.Add(label2); Controls.Add(label2);
Controls.Add(label1); Controls.Add(label1);
Margin = new Padding(2, 1, 2, 1);
Name = "FormTrip"; Name = "FormTrip";
StartPosition = FormStartPosition.CenterParent; StartPosition = FormStartPosition.CenterParent;
Text = "FormTrip"; Text = "FormTrip";
@ -246,13 +263,11 @@
private Label label1; private Label label1;
private Label label2; private Label label2;
private Label label3;
private Label label4; private Label label4;
private Label label5; private Label label5;
private Label label6; private Label label6;
private DateTimePicker dateTimePickerStartDate; private DateTimePicker dateTimePickerStartDate;
private DateTimePicker dateTimePickerEndDate; private DateTimePicker dateTimePickerEndDate;
private ComboBox comboBoxShift;
private NumericUpDown numericUpDownConsumptionRate; private NumericUpDown numericUpDownConsumptionRate;
private ComboBox comboBoxCarID; private ComboBox comboBoxCarID;
private ComboBox comboBoxDriverID; private ComboBox comboBoxDriverID;
@ -260,7 +275,9 @@
private Button buttonCancel; private Button buttonCancel;
private GroupBox groupBox; private GroupBox groupBox;
private DataGridView dataGridViewRoutes; private DataGridView dataGridViewRoutes;
private Label label3;
private ComboBox comboBoxShift;
private DataGridViewComboBoxColumn ColumnRoute; private DataGridViewComboBoxColumn ColumnRoute;
private DataGridViewTextBoxColumn ColumnEndPoint; private DataGridViewTextBoxColumn ColumnFuelInit;
} }
} }

View File

@ -11,7 +11,6 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using ProjectFuel.Repositories.Implementations; using ProjectFuel.Repositories.Implementations;
using ProjectFuel.Entities;
namespace ProjectFuel.Forms_ namespace ProjectFuel.Forms_
{ {
@ -38,7 +37,7 @@ namespace ProjectFuel.Forms_
ColumnRoute.DisplayMember = "Start_Point"; ColumnRoute.DisplayMember = "Start_Point";
ColumnRoute.ValueMember = "Route_ID"; ColumnRoute.ValueMember = "Route_ID";
comboBoxShift.DataSource = Enum.GetValues(typeof(Shift));
} }
private void ButtonSave_Click(object sender, EventArgs e) private void ButtonSave_Click(object sender, EventArgs e)
@ -60,15 +59,15 @@ namespace ProjectFuel.Forms_
} }
private void ButtonCancel_Click(object sender, EventArgs e) => Close(); private void ButtonCancel_Click(object sender, EventArgs e) => Close();
private List<Trip_Route> CreateListDriversFromDataGrid() private List<TripRoute> CreateListDriversFromDataGrid()
{ {
var list = new List<Trip_Route>(); var list = new List<TripRoute>();
foreach (DataGridViewRow row in dataGridViewRoutes.Rows) foreach (DataGridViewRow row in dataGridViewRoutes.Rows)
{ {
if (row.Cells["ColumnRoute"].Value == null) if (row.Cells["ColumnRoute"].Value == null || row.Cells["ColumnFuelInit"].Value == null)
continue; continue;
list.Add(Trip_Route.CreateElement(0, Convert.ToInt32(row.Cells["ColumnRoute"].Value))); list.Add(TripRoute.CreateOperation(0, Convert.ToInt32(row.Cells["ColumnRoute"].Value), Convert.ToInt32(row.Cells["ColumnFuelInit"].Value)));
} }
return list; return list;
} }

View File

@ -5,7 +5,7 @@ using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Serilog; using Serilog;
using Unity.Microsoft.Logging; using Unity.Microsoft.Logging;
using FuelAndLubricants.Repositories.Implementations;
namespace ProjectFuel namespace ProjectFuel
{ {

View File

@ -48,10 +48,10 @@ public class TableReport
private List<string[]> GetData(int carId, DateTime startDate, DateTime endDate) private List<string[]> GetData(int carId, DateTime startDate, DateTime endDate)
{ {
var refills = _refillRepository var refills = _refillRepository
.ReadRefills(dateFrom: startDate, dateTo: endDate, carId: carId) .ReadRefills()
.Where(x => x.Refill_Date >= startDate && x.Refill_Date <= endDate && x.Car_ID == carId)
.Select(x => new .Select(x => new
{ {
x.CarName,
CarId = x.Car_ID, CarId = x.Car_ID,
Date = x.Refill_Date, Date = x.Refill_Date,
RefillAmount = (float?)x.Refill_Amount, RefillAmount = (float?)x.Refill_Amount,
@ -60,10 +60,10 @@ public class TableReport
.AsEnumerable(); .AsEnumerable();
var trips = _tripRepository var trips = _tripRepository
.ReadTrips(dateFrom: startDate, dateTo: endDate, carId: carId) .ReadTrips()
.Where(x => x.Start_Date >= startDate && x.End_Date <= endDate && x.Car_ID == carId)
.Select(x => new .Select(x => new
{ {
x.CarName,
CarId = x.Car_ID, CarId = x.Car_ID,
Date = x.Start_Date, Date = x.Start_Date,
RefillAmount = (float?)null, RefillAmount = (float?)null,

View File

@ -1,12 +1,11 @@
using ProjectFuel.Entities; using ProjectFuel.Entities;
using ProjectFuel.Entities;
namespace ProjectFuel.Repositories; namespace ProjectFuel.Repositories;
public interface ITripRepository public interface ITripRepository
{ {
IEnumerable<Trip> ReadTrips(DateTime? dateFrom = null, DateTime? dateTo = null, int? carId = null, int? driverId = null); IEnumerable<Trip> ReadTrips(DateTime? dateFrom = null, DateTime? dateTo = null, int? carId = null, int? driverId = null, int? routeId = null);
void CreateTrip(Trip trip); void CreateTrip(Trip trip);
} }

View File

@ -1,13 +1,11 @@
using Dapper; using Dapper;
using ProjectFuel.Entities;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using Npgsql; using Npgsql;
using ProjectFuel.Entities; using ProjectFuel.Entities;
using ProjectFuel.Repositories.Implementations;
using ProjectFuel.Repositories; using ProjectFuel.Repositories;
namespace FuelAndLubricants.Repositories.Implementations; namespace ProjectFuel.Repositories.Implementations;
public class TripRepository : ITripRepository public class TripRepository : ITripRepository
{ {
@ -20,7 +18,6 @@ public class TripRepository : ITripRepository
_connectionString = connectionString; _connectionString = connectionString;
_logger = logger; _logger = logger;
} }
public void CreateTrip(Trip trip) public void CreateTrip(Trip trip)
{ {
_logger.LogInformation("Добавление объекта"); _logger.LogInformation("Добавление объекта");
@ -37,14 +34,15 @@ public class TripRepository : ITripRepository
SELECT MAX(Trip_ID) FROM Trip"; SELECT MAX(Trip_ID) FROM Trip";
var tripId = connection.QueryFirst<int>(queryInsert, trip, transaction); var tripId = connection.QueryFirst<int>(queryInsert, trip, transaction);
var querySubInsert = @" var querySubInsert = @"
INSERT INTO Trip_Route (Trip_ID, Route_ID) INSERT INTO Trip_Route (Trip_ID, Route_ID, Fuel_Init)
VALUES (@tripId, @Route_ID)"; VALUES (@tripId, @Route_ID, @Fuel_Init)";
foreach (var elem in trip.Routes) foreach (var elem in trip.Routes)
{ {
connection.Execute(querySubInsert, new connection.Execute(querySubInsert, new
{ {
tripId, tripId,
elem.Route_ID elem.Route_ID,
elem.Fuel_Init
}, transaction); }, transaction);
} }
transaction.Commit(); transaction.Commit();
@ -56,57 +54,18 @@ public class TripRepository : ITripRepository
} }
} }
public IEnumerable<Trip> ReadTrips(DateTime? dateFrom = null, DateTime? dateTo = null, int? carId = null, int? driverId = null)
public IEnumerable<Trip> ReadTrips(DateTime? dateFrom = null, DateTime? dateTo = null, int? carId = null, int? driverId = null, int? routeId = null)
{ {
_logger.LogInformation("Получение всех объектов"); _logger.LogInformation("Получение всех объектов");
try try
{ {
var builder = new QuerryBuilder();
if (dateFrom != null)
builder.AddCondition("t.Start_Date >= @dateFrom");
if (dateTo != null)
builder.AddCondition("t.End_Date <= @dateTo");
if (carId != null)
builder.AddCondition("t.Car_ID = @carId");
if (driverId != null)
builder.AddCondition("t.Driver_ID = @driverId");
using var connection = new NpgsqlConnection(_connectionString.ConnectionString); using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var querySelect = $@"SELECT var querySelect = "SELECT * FROM Trip";
t.*, var trips = connection.Query<Trip>(querySelect);
CONCAT(c.Car_Mark, ' ', c.Car_Model) AS CarName,
CONCAT(d.Firstname, ' ', d.Secondname) AS DriverName,
tr.Route_ID,
CONCAT(r.Start_Point, ' ', r.End_Point) AS RouteName
FROM Trip t
LEFT JOIN Car c ON t.Car_ID = c.Car_ID
LEFT JOIN Driver d ON t.Driver_ID = d.Driver_ID
INNER JOIN Trip_Route tr ON t.Trip_ID = tr.Trip_ID
LEFT JOIN Route r ON r.Route_ID = tr.Route_ID
{builder.Build()}";
var tripDict = new Dictionary<int, List<Trip_Route>>();
var trips = connection.Query<Trip, Trip_Route, Trip>(querySelect,
(trip, tripRoute) =>
{
if (!tripDict.TryGetValue(trip.Trip_ID, out var tr))
{
tr = new List<Trip_Route>();
tripDict.Add(trip.Trip_ID, tr);
}
tr.Add(tripRoute);
return trip;
}, splitOn: "Route_ID", param: new { dateFrom, dateTo, carId, driverId });
_logger.LogDebug("Полученные объекты: {json}", _logger.LogDebug("Полученные объекты: {json}",
JsonConvert.SerializeObject(trips)); JsonConvert.SerializeObject(trips));
return tripDict.Select(x => return trips;
{
var t = trips.First(y => y.Trip_ID == x.Key);
t.SetTrips(x.Value);
return t;
}).ToList();
} }
catch (Exception ex) catch (Exception ex)
{ {