лабораторная 4 не конец

This commit is contained in:
qkrlnt 2024-12-09 12:59:41 +04:00
parent 4662c2e508
commit 5d151fc44f
29 changed files with 190 additions and 86 deletions

View File

@ -1,4 +1,5 @@
using FuelAccounting.Entities.Enums;
using System.ComponentModel;
namespace FuelAccounting.Entities;
@ -6,12 +7,18 @@ public class Car
{
public int Id { get; private set; }
[DisplayName("Модель")]
public string Model { get; private set; } = string.Empty;
[DisplayName("Категория")]
public CarCategory Category { get; private set; }
[Browsable(false)]
public int DriverID { get; private set; }
[DisplayName("Закрепленный водитель")]
public string DriverName { get; private set; } = string.Empty;
public static Car CreateEntity(int id, string model, CarCategory category, int driverId)
{
return new Car

View File

@ -1,4 +1,5 @@
using FuelAccounting.Entities.Enums;
using System.ComponentModel;
namespace FuelAccounting.Entities;
@ -6,10 +7,15 @@ public class Driver
{
public int Id { get; private set; }
[DisplayName("Имя")]
public string FirstName { get; private set; } = string.Empty;
[DisplayName("Фамилия")]
public string LastName { get; private set; } = string.Empty;
public string DriverName => $"{FirstName} {LastName}";
[DisplayName("Категория прав")]
public DriverLicenceCategory DriverLicenceCategory { get; private set; }
public static Driver CreateEntity(int id, string firstName, string lastName, DriverLicenceCategory driverLicenceCategory)

View File

@ -1,17 +1,38 @@
namespace FuelAccounting.Entities;
using System.ComponentModel;
namespace FuelAccounting.Entities;
public class Equipage
{
public int Id { get; private set; }
[Browsable(false)]
public int CarId { get; private set; }
[Browsable(false)]
public int DriverId { get; private set; }
[Browsable(false)]
public int ShiftId { get; private set; }
[Browsable(false)]
public IEnumerable<RoutesEqipage> RoutesEqipage { get; private set; } = [];
[DisplayName("Автомобиль")]
public string CarModel { get; private set; } = string.Empty;
[DisplayName("Водитель")]
public string DriverName { get; private set; } = string.Empty;
[DisplayName("Смена")]
public string ShiftDescription { get; private set; } = string.Empty;
[DisplayName("Маршруты")]
public string Route => RoutesEqipage != null ?
string.Join(", ", RoutesEqipage.Select(x => $"{x.RouteDescription} {x.Count}")) :
string.Empty;
[DisplayName("Дата выезда")]
public DateTime EquipageDate { get; private set; }
public static Equipage CreateOperation(int id, int carId, int driverId, int shiftId, IEnumerable<RoutesEqipage> routesEqipage)
@ -27,17 +48,11 @@ public class Equipage
};
}
public static Equipage CreateOperation(TempRoutesEquipage tempRoutesEquipage,
IEnumerable<RoutesEqipage> routesEqipage)
public void SetRoutesEquipage(IEnumerable<RoutesEqipage> routesEquipage)
{
return new Equipage
if (routesEquipage != null && routesEquipage.Any())
{
Id = tempRoutesEquipage.Id,
CarId = tempRoutesEquipage.CarId,
DriverId = tempRoutesEquipage.DriverId,
ShiftId = tempRoutesEquipage.ShiftId,
RoutesEqipage = routesEqipage,
EquipageDate = tempRoutesEquipage.EquipageDate
};
RoutesEqipage = routesEquipage;
}
}
}

View File

@ -1,19 +1,30 @@
namespace FuelAccounting.Entities;
using System.ComponentModel;
namespace FuelAccounting.Entities;
public class Refueling
{
public int Id { get; private set; }
[Browsable(false)]
public int CarId { get; private set; }
[DisplayName("Автомобиль")]
public string CarModel { get; private set; } = string.Empty;
[DisplayName("Километров пойдено")]
public double Kilometers { get; private set; }
[DisplayName("Литров заправлено")]
public double LitersSpent { get; private set; }
[DisplayName("Тип топлива")]
public string TypeOfFuel { get; private set; } = string.Empty;
[DisplayName("Дата заправки")]
public DateTime RefuelingDate { get; private set; }
public static Refueling CreateOperation(int id, int carId, double kilometers, double litersSpent, string typeOfFuel)
{
return new Refueling

View File

@ -1,11 +1,15 @@
namespace FuelAccounting.Entities;
using System.ComponentModel;
namespace FuelAccounting.Entities;
public class Route
{
public int Id { get; private set; }
[DisplayName("Описание")]
public string Description { get; private set; } = string.Empty;
public static Route CreateEntity(int id, string description)
{
return new Route

View File

@ -6,6 +6,8 @@ public class RoutesEqipage
public int RouteId { get; private set; }
public string RouteDescription { get; private set; } = string.Empty;
public int Count { get; private set; }
public static RoutesEqipage CreateElement(int id, int routeId, int count)

View File

@ -1,11 +1,15 @@
namespace FuelAccounting.Entities;
using System.ComponentModel;
namespace FuelAccounting.Entities;
public class Shift
{
public int Id { get; private set; }
[DisplayName("Количество часов")]
public int AmountOfHours { get; private set; }
[DisplayName("Описание")]
public string Description { get; private set; } = string.Empty;
public static Shift CreateEntity(int id, int amountOfHours, string description)

View File

@ -1,18 +0,0 @@
namespace FuelAccounting.Entities;
public class TempRoutesEquipage
{
public int Id { get; private set; }
public int CarId { get; private set; }
public int DriverId { get; private set; }
public int ShiftId { get; private set; }
public int RouteId { get; private set; }
public int Count { get; private set; }
public DateTime EquipageDate { get; private set; }
}

View File

@ -52,9 +52,9 @@
label2.AutoSize = true;
label2.Location = new Point(12, 101);
label2.Name = "label2";
label2.Size = new Size(170, 15);
label2.Size = new Size(142, 15);
label2.TabIndex = 1;
label2.Text = "ID закрепленного сотрудника";
label2.Text = "Закрепленный водитель";
//
// label3
//
@ -104,9 +104,9 @@
// comboBoxDriverId
//
comboBoxDriverId.FormattingEnabled = true;
comboBoxDriverId.Location = new Point(188, 98);
comboBoxDriverId.Location = new Point(167, 98);
comboBoxDriverId.Name = "comboBoxDriverId";
comboBoxDriverId.Size = new Size(84, 23);
comboBoxDriverId.Size = new Size(105, 23);
comboBoxDriverId.TabIndex = 8;
//
// FormCar
@ -123,6 +123,7 @@
Controls.Add(label2);
Controls.Add(label1);
Name = "FormCar";
StartPosition = FormStartPosition.CenterScreen;
Text = "Автомобиль";
ResumeLayout(false);
PerformLayout();

View File

@ -24,7 +24,14 @@ namespace FuelAccounting.Forms
textBoxModel.Text = car.Model;
comboBoxCategory.SelectedItem = car.Category;
comboBoxDriverId.SelectedItem = car.DriverID;
if (car.DriverID != 0)
{
comboBoxDriverId.SelectedValue = car.DriverID;
}
else
{
comboBoxDriverId.SelectedIndex = 0;
}
_carId = value;
}
catch (Exception ex)
@ -44,7 +51,7 @@ namespace FuelAccounting.Forms
comboBoxCategory.DataSource = Enum.GetValues(typeof(CarCategory));
comboBoxDriverId.DataSource = driversRepository.ReadDrivers();
comboBoxDriverId.DisplayMember = "FirstName";
comboBoxDriverId.DisplayMember = "DriverName";
comboBoxDriverId.ValueMember = "Id";
}

View File

@ -106,6 +106,7 @@
Controls.Add(dataGridViewData);
Controls.Add(panelButtons);
Name = "FormCars";
StartPosition = FormStartPosition.CenterScreen;
Text = "Автомобили";
Load += FormCars_Load;
((System.ComponentModel.ISupportInitialize)dataGridViewData).EndInit();

View File

@ -84,7 +84,11 @@ namespace FuelAccounting.Forms
}
}
private void LoadList() => dataGridViewData.DataSource = _carsRepository.ReadCars();
private void LoadList()
{
dataGridViewData.DataSource = _carsRepository.ReadCars();
dataGridViewData.Columns["Id"].Visible = false;
}
private bool TryGetIdentifierFromSelectedRow(out int id)
{

View File

@ -121,6 +121,7 @@
Controls.Add(label1);
Controls.Add(checkedListBoxDriverLicenceCategory);
Name = "FormDriver";
StartPosition = FormStartPosition.CenterScreen;
Text = "FormDriver";
ResumeLayout(false);
PerformLayout();

View File

@ -106,6 +106,7 @@
Controls.Add(dataGridViewData);
Controls.Add(panelButtons);
Name = "FormDrivers";
StartPosition = FormStartPosition.CenterScreen;
Text = "Водители";
Load += FormDrivers_Load;
((System.ComponentModel.ISupportInitialize)dataGridViewData).EndInit();

View File

@ -94,7 +94,12 @@ namespace FuelAccounting.Forms
}
}
private void LoadList() => dataGridViewData.DataSource = _driversRepository.ReadDrivers();
private void LoadList()
{
dataGridViewData.DataSource = _driversRepository.ReadDrivers();
dataGridViewData.Columns["Id"].Visible = false;
dataGridViewData.Columns["DriverName"].Visible = false;
}
private bool TryGetIdentifierFromSelectedRow(out int id)
{

View File

@ -169,6 +169,7 @@
Controls.Add(comboBoxCar);
Controls.Add(label1);
Name = "FormEquipage";
StartPosition = FormStartPosition.CenterScreen;
Text = "Выезд";
groupBoxRoutes.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)dataGridViewRoutes).EndInit();

View File

@ -30,7 +30,7 @@ namespace FuelAccounting.Forms
comboBoxCar.ValueMember = "Id";
comboBoxDriver.DataSource = driversRepository.ReadDrivers();
comboBoxDriver.DisplayMember = "FirstName";
comboBoxDriver.DisplayMember = "DriverName";
comboBoxDriver.ValueMember = "Id";
comboBoxShift.DataSource = shiftRepository.ReadShifts();

View File

@ -30,8 +30,8 @@
{
dataGridViewData = new DataGridView();
panelButtons = new Panel();
buttonAdd = new Button();
buttonDelete = new Button();
buttonAdd = new Button();
((System.ComponentModel.ISupportInitialize)dataGridViewData).BeginInit();
panelButtons.SuspendLayout();
SuspendLayout();
@ -63,17 +63,6 @@
panelButtons.Size = new Size(160, 450);
panelButtons.TabIndex = 10;
//
// buttonAdd
//
buttonAdd.BackgroundImage = Properties.Resources.pngimg_com___plus_PNG84;
buttonAdd.BackgroundImageLayout = ImageLayout.Stretch;
buttonAdd.Location = new Point(18, 12);
buttonAdd.Name = "buttonAdd";
buttonAdd.Size = new Size(130, 130);
buttonAdd.TabIndex = 2;
buttonAdd.UseVisualStyleBackColor = true;
buttonAdd.Click += ButtonAdd_Click;
//
// buttonDelete
//
buttonDelete.BackgroundImage = Properties.Resources.images;
@ -85,6 +74,17 @@
buttonDelete.UseVisualStyleBackColor = true;
buttonDelete.Click += ButtonDelete_Click;
//
// buttonAdd
//
buttonAdd.BackgroundImage = Properties.Resources.pngimg_com___plus_PNG84;
buttonAdd.BackgroundImageLayout = ImageLayout.Stretch;
buttonAdd.Location = new Point(18, 12);
buttonAdd.Name = "buttonAdd";
buttonAdd.Size = new Size(130, 130);
buttonAdd.TabIndex = 2;
buttonAdd.UseVisualStyleBackColor = true;
buttonAdd.Click += ButtonAdd_Click;
//
// FormEquipages
//
AutoScaleDimensions = new SizeF(7F, 15F);
@ -93,6 +93,7 @@
Controls.Add(dataGridViewData);
Controls.Add(panelButtons);
Name = "FormEquipages";
StartPosition = FormStartPosition.CenterScreen;
Text = "Выезды";
Load += FormEquipages_Load;
((System.ComponentModel.ISupportInitialize)dataGridViewData).EndInit();

View File

@ -53,7 +53,12 @@ namespace FuelAccounting.Forms
}
}
private void LoadList() => dataGridViewData.DataSource = _equipageRepository.ReadEquipages();
private void LoadList()
{
dataGridViewData.DataSource = _equipageRepository.ReadEquipages();
dataGridViewData.Columns["Id"].Visible = false;
dataGridViewData.Columns["EquipageDate"].DefaultCellStyle.Format = "dd MMMM yyyy hh:mm";
}
private void ButtonDelete_Click(object sender, EventArgs e)
{

View File

@ -74,7 +74,7 @@
label4.AutoSize = true;
label4.Location = new Point(12, 126);
label4.Name = "label4";
label4.Size = new Size(75, 15);
label4.Size = new Size(76, 15);
label4.TabIndex = 3;
label4.Text = "Тип топлива";
//
@ -149,6 +149,7 @@
Controls.Add(label2);
Controls.Add(label1);
Name = "FormRefueling";
StartPosition = FormStartPosition.CenterScreen;
Text = "FormRefueling";
((System.ComponentModel.ISupportInitialize)numericUpDownKm).EndInit();
((System.ComponentModel.ISupportInitialize)numericUpDownLiters).EndInit();

View File

@ -44,7 +44,7 @@
buttonAdd.Size = new Size(130, 130);
buttonAdd.TabIndex = 2;
buttonAdd.UseVisualStyleBackColor = true;
buttonAdd.Click += buttonAdd_Click;
buttonAdd.Click += ButtonAdd_Click;
//
// dataGridViewData
//
@ -80,6 +80,7 @@
Controls.Add(dataGridViewData);
Controls.Add(panelButtons);
Name = "FormRefuelings";
StartPosition = FormStartPosition.CenterScreen;
Text = "Заправки";
Load += FormRefuelings_Load;
((System.ComponentModel.ISupportInitialize)dataGridViewData).EndInit();

View File

@ -40,7 +40,7 @@ namespace FuelAccounting.Forms
}
}
private void buttonAdd_Click(object sender, EventArgs e)
private void ButtonAdd_Click(object sender, EventArgs e)
{
try
{
@ -53,6 +53,11 @@ namespace FuelAccounting.Forms
}
}
private void LoadList() => dataGridViewData.DataSource = _refuelingRepository.ReadRefuelings();
private void LoadList()
{
dataGridViewData.DataSource = _refuelingRepository.ReadRefuelings();
dataGridViewData.Columns["Id"].Visible = false;
dataGridViewData.Columns["RefuelingDate"].DefaultCellStyle.Format = "dd.MM.yyyy hh:mm";
}
}
}

View File

@ -57,7 +57,7 @@
buttonDelete.Size = new Size(130, 130);
buttonDelete.TabIndex = 4;
buttonDelete.UseVisualStyleBackColor = true;
buttonDelete.Click += buttonDelete_Click;
buttonDelete.Click += ButtonDelete_Click;
//
// buttonUpdate
//
@ -68,7 +68,7 @@
buttonUpdate.Size = new Size(130, 130);
buttonUpdate.TabIndex = 3;
buttonUpdate.UseVisualStyleBackColor = true;
buttonUpdate.Click += buttonUpdate_Click;
buttonUpdate.Click += ButtonUpdate_Click;
//
// buttonAdd
//
@ -79,7 +79,7 @@
buttonAdd.Size = new Size(130, 130);
buttonAdd.TabIndex = 2;
buttonAdd.UseVisualStyleBackColor = true;
buttonAdd.Click += buttonAdd_Click;
buttonAdd.Click += ButtonAdd_Click;
//
// dataGridViewData
//

View File

@ -37,7 +37,7 @@ namespace FuelAccounting.Forms
}
}
private void buttonAdd_Click(object sender, EventArgs e)
private void ButtonAdd_Click(object sender, EventArgs e)
{
try
{
@ -50,7 +50,7 @@ namespace FuelAccounting.Forms
}
}
private void buttonUpdate_Click(object sender, EventArgs e)
private void ButtonUpdate_Click(object sender, EventArgs e)
{
if (!TryGetIdentifierFromSelectedRow(out var findId))
{
@ -70,7 +70,7 @@ namespace FuelAccounting.Forms
}
}
private void buttonDelete_Click(object sender, EventArgs e)
private void ButtonDelete_Click(object sender, EventArgs e)
{
if (!TryGetIdentifierFromSelectedRow(out var findId))
{
@ -93,7 +93,11 @@ namespace FuelAccounting.Forms
}
}
private void LoadList() => dataGridViewData.DataSource = _routeRepository.ReadRoutes();
private void LoadList()
{
dataGridViewData.DataSource = _routeRepository.ReadRoutes();
dataGridViewData.Columns["Id"].Visible = false;
}
private bool TryGetIdentifierFromSelectedRow(out int id)
{

View File

@ -106,6 +106,7 @@
Controls.Add(dataGridViewData);
Controls.Add(panelButtons);
Name = "FormShifts";
StartPosition = FormStartPosition.CenterScreen;
Text = "Смены";
Load += FormShifts_Load;
((System.ComponentModel.ISupportInitialize)dataGridViewData).EndInit();

View File

@ -85,7 +85,11 @@ namespace FuelAccounting.Forms
}
}
private void LoadList() => dataGridViewData.DataSource = _shiftRepository.ReadShifts();
private void LoadList()
{
dataGridViewData.DataSource = _shiftRepository.ReadShifts();
dataGridViewData.Columns["Id"].Visible = false;
}
private bool TryGetIdentifierFromSelectedRow(out int id)
{

View File

@ -111,12 +111,13 @@ WHERE id = @id";
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var querySelect = @"
SELECT
id,
model,
category,
driver_id AS DriverID
FROM Cars";
SELECT
c.id,
c.model,
c.category,
CONCAT(d.first_name, ' ', d.last_name) AS DriverName
FROM Cars c
LEFT JOIN Drivers d on d.id = c.driver_id";
var cars = connection.Query<Car>(querySelect);
_logger.LogDebug("Найденные объекты: {json}", JsonConvert.SerializeObject(cars));
return cars;

View File

@ -81,15 +81,43 @@ WHERE id = @id";
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var querySelect = @"
SELECT e.id, e.car_id AS CarId, e.driver_id AS DriverId, e.shift_id AS ShiftId,
e.equipage_date AS EquipageDate, re.route_id AS RouteId, re.count AS Count
SELECT
e.id,
c.model AS CarModel,
CONCAT(d.first_name, ' ', last_name) AS DriverName,
s.description AS ShiftDescription,
e.equipage_date AS EquipageDate,
re.route_id AS RouteId,
r.description AS RouteDescription,
re.count AS Count
FROM Equipage e
INNER JOIN RoutesEquipage re ON re.equipage_id = e.id";
var equipages = connection.Query<TempRoutesEquipage>(querySelect);
INNER JOIN RoutesEquipage re ON re.equipage_id = e.id
LEFT JOIN Cars c on c.id = e.car_id
LEFT JOIN Drivers d on d.id = e.driver_id
LEFT JOIN Shift s on s.id = e.shift_id
LEFT JOIN Routes r on r.id = re.route_id;";
var equipageDict = new Dictionary<int, List<RoutesEqipage>>();
var equipages = connection.Query<Equipage, RoutesEqipage, Equipage>(querySelect,
(equip, equipages) =>
{
if (!equipageDict.TryGetValue(equip.Id, out var re))
{
re = [];
equipageDict.Add(equip.Id, re);
}
re.Add(equipages);
return equip;
}, splitOn: "RouteId");
_logger.LogDebug("Найденные объекты: {json}", JsonConvert.SerializeObject(equipages));
return equipages.GroupBy(x => x.Id, y => y,
(key, value) => Equipage.CreateOperation(value.First(),
value.Select(z => RoutesEqipage.CreateElement(0, z.RouteId, z.CarId)))).ToList();
return equipageDict.Select(x =>
{
var e = equipages.First(y => y.Id == x.Key);
e.SetRoutesEquipage(x.Value);
return e;
}).ToArray();
}
catch (Exception ex)
{

View File

@ -45,13 +45,14 @@ VALUES (@CarId, @Kilometers, @LitersSpent, @TypeOfFuel, @RefuelingDate)";
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var querySelect = @"
SELECT
id,
car_id AS CarId,
km AS Kilometers,
liters_spent AS LitersSpent,
type_of_fuel AS TypeOfFuel,
refueling_date AS RefuelingDate
FROM Refueling";
r.id,
c.model AS CarModel,
r.km AS Kilometers,
r.liters_spent AS LitersSpent,
r.type_of_fuel AS TypeOfFuel,
r.refueling_date AS RefuelingDate
FROM Refueling r
LEFT JOIN Cars c on c.id = r.car_id";
var refuelings = connection.Query<Refueling>(querySelect);
_logger.LogDebug("Найденные объекты: {json}", JsonConvert.SerializeObject(refuelings));
return refuelings;