130 lines
4.6 KiB
C#
130 lines
4.6 KiB
C#
using Dapper;
|
|
using Microsoft.Extensions.Logging;
|
|
using Newtonsoft.Json;
|
|
using Npgsql;
|
|
using ProjectGarage.Entities;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.SqlClient;
|
|
using System.Linq;
|
|
using System.Runtime.Intrinsics.X86;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace ProjectGarage.Repositories.Implementations;
|
|
|
|
public class ReplenishmentRepository : IReplenishmentRepository
|
|
{
|
|
private readonly IConnectionString _connectionString;
|
|
|
|
private readonly ILogger<ReplenishmentRepository> _logger;
|
|
|
|
public ReplenishmentRepository(IConnectionString connectionString, ILogger<ReplenishmentRepository> logger)
|
|
{
|
|
_connectionString = connectionString;
|
|
_logger = logger;
|
|
}
|
|
public void CreateFuelReplenishment(FuelReplenishment replenishment)
|
|
{
|
|
_logger.LogInformation("Добавление объекта");
|
|
_logger.LogDebug("Объект: {json}",
|
|
JsonConvert.SerializeObject(replenishment));
|
|
try
|
|
{
|
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
|
connection.Open();
|
|
using var transaction = connection.BeginTransaction();
|
|
var queryInsert = @"
|
|
INSERT INTO fuelreplenishment (DriverId, ReplenishmentDate)
|
|
VALUES (@DriverId, @ReplenishmentDate);
|
|
SELECT MAX(Id) FROM fuelreplenishment";
|
|
var ReplenishmentId = connection.QueryFirst<int>(queryInsert, replenishment, transaction);
|
|
var querySubInsert = @"
|
|
INSERT INTO fuel_fuelreplenishment (ReplenishmentId, FuelId, Amount)
|
|
VALUES (@ReplenishmentId, @FuelId, @Amount)";
|
|
foreach (var elem in replenishment.FuelFuelReplenishments)
|
|
{
|
|
connection.Execute(querySubInsert, new
|
|
{
|
|
ReplenishmentId,
|
|
elem.FuelId,
|
|
elem.Amount
|
|
}, transaction);
|
|
}
|
|
transaction.Commit();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public void DeleteFuelReplenishment(int id)
|
|
{
|
|
_logger.LogInformation("Удаление объекта");
|
|
_logger.LogDebug("Объект: {id}", id);
|
|
try
|
|
{
|
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
|
var queryDelete = @"
|
|
DELETE FROM fuelreplenishment
|
|
WHERE Id=@id";
|
|
connection.Execute(queryDelete, new { id });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public IEnumerable<FuelReplenishment> ReadFuelReplenishment(DateTime? dateForm = null,
|
|
DateTime? dateTo = null, int? fuelId = null, int? driverId = null, int? routeId = null)
|
|
{
|
|
_logger.LogInformation("Получение всех объектов");
|
|
try
|
|
{
|
|
using var connection = new
|
|
NpgsqlConnection(_connectionString.ConnectionString);
|
|
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
|
|
LEFT JOIN driver d on d.Id = fr.DriverId
|
|
INNER JOIN fuel_fuelreplenishment ffr ON ffr.ReplenishmentId = fr.Id
|
|
LEFT JOIN fuel f on f.Id = ffr.FuelId";
|
|
var replenishmentDict = new Dictionary<int, List<FuelFuelReplenishment>>();
|
|
|
|
var fuelReplenishment = connection.Query<FuelReplenishment, FuelFuelReplenishment, FuelReplenishment>(querySelect,
|
|
(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)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
|
throw;
|
|
}
|
|
}
|
|
}
|