116 lines
3.8 KiB
C#
116 lines
3.8 KiB
C#
using Dapper;
|
|
using GasStation.Entities;
|
|
using Microsoft.Extensions.Logging;
|
|
using Newtonsoft.Json;
|
|
using Npgsql;
|
|
using static System.Runtime.InteropServices.JavaScript.JSType;
|
|
|
|
namespace GasStation.Repositories.Implementations;
|
|
|
|
public class SellingRepository : ISellingRepository
|
|
{
|
|
private readonly IConnectionString _connectionString;
|
|
|
|
private readonly ILogger<SellingRepository> _logger;
|
|
|
|
public SellingRepository(IConnectionString connectionString, ILogger<SellingRepository> logger)
|
|
{
|
|
_connectionString = connectionString;
|
|
_logger = logger;
|
|
}
|
|
|
|
public void CreateSelling(Selling selling)
|
|
{
|
|
_logger.LogInformation("Добавление объекта");
|
|
_logger.LogDebug("Объект: {json}", JsonConvert.SerializeObject(selling));
|
|
|
|
try
|
|
{
|
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
|
connection.Open();
|
|
using var transaction = connection.BeginTransaction();
|
|
var queryInsert = @"
|
|
INSERT INTO selling (sellingDateTime, gasmanId)
|
|
VALUES (@SellingDateTime, @GasmanId);
|
|
SELECT MAX(Id) FROM selling";
|
|
var Id = connection.QueryFirst<int>(queryInsert, selling, transaction);
|
|
var querySubInsert = @"
|
|
INSERT INTO product_selling (id, productID, count)
|
|
VALUES (@Id, @ProductID, @Count)";
|
|
foreach (var elem in selling.ProdutcSellings)
|
|
{
|
|
connection.Execute(querySubInsert, new { Id, elem.ProductID, elem.Count }, transaction);
|
|
}
|
|
transaction.Commit();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public IEnumerable<Selling> ReadSelling(DateTime? dateTime = null, int? count = null, int? gasmanID = null)
|
|
{
|
|
_logger.LogInformation("Получение всех объектов");
|
|
try
|
|
{
|
|
var builder = new QueryBuilder();
|
|
if (dateTime.HasValue)
|
|
{
|
|
builder.AddCondition("s.SellingDateTime = @dateTime");
|
|
}
|
|
if (count.HasValue)
|
|
{
|
|
builder.AddCondition("s.Count = @count");
|
|
}
|
|
if (gasmanID.HasValue)
|
|
{
|
|
builder.AddCondition("s.GasmanID = @gasmanID");
|
|
}
|
|
|
|
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
|
|
var querySelect = @$"
|
|
SELECT
|
|
s.*,
|
|
g.GasmanName,
|
|
CONCAT(p.ProductType, ' ', p.ProductName) as ProductName,
|
|
ps.ProductId,
|
|
ps.Count
|
|
FROM Selling s
|
|
LEFT JOIN Gasman g on g.ID = s.GasmanID
|
|
INNER JOIN Product_Selling ps ON ps.ID = s.ID
|
|
LEFT JOIN Product p on p.Id = ps.ProductID
|
|
{builder.Build()}";
|
|
var sellingDict = new Dictionary<int, List<ProductSelling>>();
|
|
var selling = connection.Query<Selling, ProductSelling, Selling>(querySelect, (sell, sellings) =>
|
|
{
|
|
if (!sellingDict.TryGetValue(sell.Id, out var ps))
|
|
{
|
|
ps = [];
|
|
sellingDict.Add(sell.Id, ps);
|
|
}
|
|
ps.Add(sellings);
|
|
return sell;
|
|
}, splitOn: "ProductName", param: new
|
|
{dateTime, count, gasmanID});
|
|
_logger.LogDebug("Полученные объекты: {json}",
|
|
JsonConvert.SerializeObject(selling));
|
|
|
|
return sellingDict.Select(x =>
|
|
{
|
|
var s = selling.First(y => y.Id == x.Key);
|
|
s.SetProductSelling(x.Value);
|
|
return s;
|
|
}).ToArray();
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
|