2024-11-19 00:15:28 +04:00
|
|
|
|
using System.Data.SqlClient;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using Dapper;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Npgsql;
|
|
|
|
|
using ProjectGSM.Entities;
|
2024-12-16 23:03:26 +04:00
|
|
|
|
using ProjectGSM.Query;
|
2024-11-05 03:59:13 +04:00
|
|
|
|
|
|
|
|
|
namespace ProjectGSM.Repositories.Implementations;
|
|
|
|
|
|
|
|
|
|
public class StatusHistoryRepository : IStatusHistoryRepository
|
|
|
|
|
{
|
2024-11-19 00:15:28 +04:00
|
|
|
|
private readonly IConnectionString _connectionString;
|
|
|
|
|
private readonly ILogger<StatusHistoryRepository> _logger;
|
2024-12-16 23:03:26 +04:00
|
|
|
|
|
2024-11-19 00:15:28 +04:00
|
|
|
|
public StatusHistoryRepository(IConnectionString connectionString,
|
|
|
|
|
ILogger<StatusHistoryRepository> logger)
|
|
|
|
|
{
|
|
|
|
|
_connectionString = connectionString;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
2024-12-16 23:03:26 +04:00
|
|
|
|
|
2024-11-05 03:59:13 +04:00
|
|
|
|
public IEnumerable<StatusHistory> ReadStatusHistories(DateTime? dateForm = null, DateTime? dateTo = null,
|
2024-12-16 23:03:26 +04:00
|
|
|
|
int? caseId = null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Получение всех объектов");
|
|
|
|
|
try
|
2024-11-05 03:59:13 +04:00
|
|
|
|
{
|
2024-12-16 23:03:26 +04:00
|
|
|
|
var builder = new QueryBuilder();
|
|
|
|
|
if (dateForm.HasValue)
|
2024-11-19 00:15:28 +04:00
|
|
|
|
{
|
2024-12-16 23:03:26 +04:00
|
|
|
|
builder.AddCondition("sh.CreatedAt >= @dateForm");
|
2024-11-19 00:15:28 +04:00
|
|
|
|
}
|
2024-12-16 23:03:26 +04:00
|
|
|
|
if (dateTo.HasValue)
|
2024-11-19 00:15:28 +04:00
|
|
|
|
{
|
2024-12-16 23:03:26 +04:00
|
|
|
|
builder.AddCondition("sh.CreatedAt <= @dateTo");
|
|
|
|
|
}
|
|
|
|
|
if (caseId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
builder.AddCondition("sh.caseid = @caseId");
|
2024-11-19 00:15:28 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-16 23:03:26 +04:00
|
|
|
|
using var connection = new
|
|
|
|
|
NpgsqlConnection(_connectionString.ConnectionString);
|
|
|
|
|
var querySelect =
|
2024-12-17 01:21:56 +04:00
|
|
|
|
@$"SELECT sh.*, c.description as CaseDescription FROM status_histories as sh LEFT JOIN cases as c ON c.id = sh.caseid {builder.Build()}";
|
2024-12-16 23:03:26 +04:00
|
|
|
|
var statusHistories =
|
|
|
|
|
connection.Query<StatusHistory>(querySelect, new { dateForm, dateTo, caseId });
|
|
|
|
|
_logger.LogDebug("Полученные объекты: {json}",
|
|
|
|
|
JsonSerializer.Serialize(statusHistories));
|
|
|
|
|
return statusHistories;
|
2024-11-05 03:59:13 +04:00
|
|
|
|
}
|
2024-12-16 23:03:26 +04:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка при чтении объектов");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-05 03:59:13 +04:00
|
|
|
|
|
|
|
|
|
public void CreateStatusHistory(StatusHistory statusHistory)
|
|
|
|
|
{
|
2024-11-19 00:15:28 +04:00
|
|
|
|
_logger.LogInformation("Добавление объекта");
|
|
|
|
|
_logger.LogDebug("Объект: {json}",
|
|
|
|
|
JsonSerializer.Serialize(statusHistory));
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using var connection = new
|
|
|
|
|
NpgsqlConnection(_connectionString.ConnectionString);
|
|
|
|
|
var queryInsert = @"
|
|
|
|
|
INSERT INTO status_histories (caseid, status, price, createdat)
|
|
|
|
|
VALUES (@CaseId, @Status, @Price, @CreatedAt)";
|
|
|
|
|
connection.Execute(queryInsert, statusHistory);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка при добавлении объекта");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2024-11-05 03:59:13 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void DeleteStatusHistory(int statusId, int caseId)
|
|
|
|
|
{
|
2024-11-19 00:15:28 +04:00
|
|
|
|
_logger.LogInformation("Удаление объекта");
|
|
|
|
|
_logger.LogDebug("Идентификаторы: {statusId}, {caseId}", statusId, caseId);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using var connection = new
|
|
|
|
|
NpgsqlConnection(_connectionString.ConnectionString);
|
|
|
|
|
var queryDelete = @"DELETE FROM status_histories WHERE status = @Status AND caseid = @CaseId";
|
|
|
|
|
connection.Execute(queryDelete, new { StatusId = statusId, CaseId = caseId });
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Ошибка при удалении объекта");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2024-11-05 03:59:13 +04:00
|
|
|
|
}
|
|
|
|
|
}
|