58 lines
2.7 KiB
C#
58 lines
2.7 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 PorcMonCanillaRepository : IPorcMonCanillaRepository
|
||
|
|
{
|
||
|
|
private readonly DbConnectionFactory _cf; private readonly ILogger<PorcMonCanillaRepository> _log;
|
||
|
|
public PorcMonCanillaRepository(DbConnectionFactory cf, ILogger<PorcMonCanillaRepository> log) { _cf = cf; _log = log; }
|
||
|
|
|
||
|
|
public async Task<bool> DeleteByPublicacionIdAsync(int idPublicacion, int idUsuarioAuditoria, IDbTransaction transaction)
|
||
|
|
{
|
||
|
|
const string selectSql = "SELECT * FROM dbo.dist_PorcMonPagoCanilla WHERE Id_Publicacion = @IdPublicacion";
|
||
|
|
var itemsToDelete = await transaction.Connection!.QueryAsync<PorcMonCanilla>(selectSql, new { IdPublicacion = idPublicacion }, transaction);
|
||
|
|
|
||
|
|
if (itemsToDelete.Any())
|
||
|
|
{
|
||
|
|
const string insertHistoricoSql = @"
|
||
|
|
INSERT INTO dbo.dist_PorcMonPagoCanilla_H (Id_PorcMon, Id_Publicacion, Id_Canilla, VigenciaD, VigenciaH, PorcMon, EsPorcentaje, Id_Usuario, FechaMod, TipoMod)
|
||
|
|
VALUES (@Id_PorcMon, @Id_Publicacion, @Id_Canilla, @VigenciaD, @VigenciaH, @PorcMon, @EsPorcentaje, @Id_Usuario, @FechaMod, @TipoMod);";
|
||
|
|
|
||
|
|
foreach (var item in itemsToDelete)
|
||
|
|
{
|
||
|
|
await transaction.Connection!.ExecuteAsync(insertHistoricoSql, new
|
||
|
|
{
|
||
|
|
Id_PorcMon = item.IdPorcMon, // Mapeo de propiedad a parámetro SQL
|
||
|
|
Id_Publicacion = item.IdPublicacion,
|
||
|
|
Id_Canilla = item.IdCanilla,
|
||
|
|
item.VigenciaD,
|
||
|
|
item.VigenciaH,
|
||
|
|
item.PorcMon,
|
||
|
|
item.EsPorcentaje,
|
||
|
|
Id_Usuario = idUsuarioAuditoria,
|
||
|
|
FechaMod = DateTime.Now,
|
||
|
|
TipoMod = "Eliminado (Cascada)"
|
||
|
|
}, transaction);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
const string deleteSql = "DELETE FROM dbo.dist_PorcMonPagoCanilla 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 PorcMonCanilla por IdPublicacion: {idPublicacion}", idPublicacion);
|
||
|
|
throw;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|