56 lines
2.5 KiB
C#
56 lines
2.5 KiB
C#
|
|
using Dapper; using System.Data;
|
||
|
|
using System.Threading.Tasks;
|
||
|
|
using Microsoft.Extensions.Logging;
|
||
|
|
using GestionIntegral.Api.Models.Distribucion;
|
||
|
|
using System;
|
||
|
|
using System.Linq;
|
||
|
|
|
||
|
|
namespace GestionIntegral.Api.Data.Repositories.Distribucion
|
||
|
|
{
|
||
|
|
public class PorcPagoRepository : IPorcPagoRepository
|
||
|
|
{
|
||
|
|
private readonly DbConnectionFactory _cf; private readonly ILogger<PorcPagoRepository> _log;
|
||
|
|
public PorcPagoRepository(DbConnectionFactory cf, ILogger<PorcPagoRepository> log) { _cf = cf; _log = log; }
|
||
|
|
|
||
|
|
public async Task<bool> DeleteByPublicacionIdAsync(int idPublicacion, int idUsuarioAuditoria, IDbTransaction transaction)
|
||
|
|
{
|
||
|
|
const string selectSql = "SELECT * FROM dbo.dist_PorcPago WHERE Id_Publicacion = @IdPublicacion";
|
||
|
|
var itemsToDelete = await transaction.Connection!.QueryAsync<PorcPago>(selectSql, new { IdPublicacion = idPublicacion }, transaction);
|
||
|
|
|
||
|
|
if (itemsToDelete.Any())
|
||
|
|
{
|
||
|
|
const string insertHistoricoSql = @"
|
||
|
|
INSERT INTO dbo.dist_PorcPago_H (Id_Porcentaje, Id_Publicacion, Id_Distribuidor, VigenciaD, VigenciaH, Porcentaje, Id_Usuario, FechaMod, TipoMod)
|
||
|
|
VALUES (@IdPorcentaje, @IdPublicacion, @IdDistribuidor, @VigenciaD, @VigenciaH, @Porcentaje, @Id_Usuario, @FechaMod, @TipoMod);";
|
||
|
|
|
||
|
|
foreach (var item in itemsToDelete)
|
||
|
|
{
|
||
|
|
await transaction.Connection!.ExecuteAsync(insertHistoricoSql, new
|
||
|
|
{
|
||
|
|
item.IdPorcentaje, // Mapea a @Id_Porcentaje si usas nombres con _ en SQL
|
||
|
|
item.IdPublicacion,
|
||
|
|
item.IdDistribuidor,
|
||
|
|
item.VigenciaD,
|
||
|
|
item.VigenciaH,
|
||
|
|
item.Porcentaje,
|
||
|
|
Id_Usuario = idUsuarioAuditoria,
|
||
|
|
FechaMod = DateTime.Now,
|
||
|
|
TipoMod = "Eliminado (Cascada)"
|
||
|
|
}, transaction);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
const string deleteSql = "DELETE FROM dbo.dist_PorcPago WHERE Id_Publicacion = @IdPublicacion";
|
||
|
|
try
|
||
|
|
{
|
||
|
|
await transaction.Connection!.ExecuteAsync(deleteSql, new { IdPublicacion = idPublicacion }, transaction);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
catch (System.Exception e)
|
||
|
|
{
|
||
|
|
_log.LogError(e, "Error al eliminar PorcPago por IdPublicacion: {idPublicacion}", idPublicacion);
|
||
|
|
throw;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|