2023-02-02 10:50:02 -05:00
|
|
|
|
using MSAdminUsuarios.Context;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Data.SqlClient;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Transactions;
|
2023-03-15 15:57:59 -05:00
|
|
|
|
using Microsoft.EntityFrameworkCore.Storage;
|
2023-03-21 18:55:21 -05:00
|
|
|
|
using RabbitMQ.Client;
|
2024-06-15 11:02:19 -05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2023-02-02 10:50:02 -05:00
|
|
|
|
|
|
|
|
|
namespace MSAdminUsuarios.Controllers
|
|
|
|
|
{
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("[controller]")]
|
|
|
|
|
public class PerfilesPorUsuarioController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly ModelContext _context;
|
2023-03-21 18:55:21 -05:00
|
|
|
|
//private readonly IModel _channel;
|
2023-03-15 15:57:59 -05:00
|
|
|
|
|
2023-02-02 10:50:02 -05:00
|
|
|
|
public PerfilesPorUsuarioController(ModelContext context)
|
|
|
|
|
{
|
|
|
|
|
_context = context;
|
2023-03-21 18:55:21 -05:00
|
|
|
|
//_channel = channel;
|
2023-02-02 10:50:02 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public async Task<IActionResult> GetPerfilesPorUsuario()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-06-15 11:02:19 -05:00
|
|
|
|
|
|
|
|
|
List<PERFILPORUSUARIO> consulta = await _context.PERFILESPORUSUARIOs.OrderBy(p => p.NU_FK_PFL).Where(p => p.BL_ESTADO_PFLXUSU > 0).ToListAsync();
|
|
|
|
|
|
|
|
|
|
/*
|
2023-02-02 10:50:02 -05:00
|
|
|
|
var consulta = from dm in _context.PERFILESPORUSUARIOs
|
|
|
|
|
where dm.BL_ESTADO_PFLXUSU > 0
|
|
|
|
|
select dm;
|
2024-06-15 11:02:19 -05:00
|
|
|
|
*/
|
2023-02-02 10:50:02 -05:00
|
|
|
|
|
|
|
|
|
return Ok(consulta);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest(ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public async Task<IActionResult> GuardarPerfilesPorUsuario(PERFILPORUSUARIO[] guardar)
|
|
|
|
|
{
|
2023-03-21 18:55:21 -05:00
|
|
|
|
using (var dbContextTransaction = _context.Database.BeginTransaction())
|
2023-02-02 10:50:02 -05:00
|
|
|
|
{
|
2023-03-21 18:55:21 -05:00
|
|
|
|
List<string> error = new(); //Inicializamos variable para guardar errores
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
//var error = await GuardarPerfiles(guardar, _context);
|
2023-03-15 15:57:59 -05:00
|
|
|
|
|
2023-03-21 18:55:21 -05:00
|
|
|
|
var count = 0;
|
2023-02-02 10:50:02 -05:00
|
|
|
|
|
2023-03-21 18:55:21 -05:00
|
|
|
|
// Se cambia a estado 0 todos los perfiles por usuario existentes
|
2023-12-06 14:11:53 -05:00
|
|
|
|
var existe_plfxusu = _context.PERFILESPORUSUARIOs.Where(x => x.BL_ESTADO_PFLXUSU == 1 && x.TX_UUID_USUMS == guardar[0].TX_UUID_USUMS).ToList();
|
2023-02-02 10:50:02 -05:00
|
|
|
|
|
2023-03-21 18:55:21 -05:00
|
|
|
|
foreach (var cu in existe_plfxusu)
|
2023-02-02 10:50:02 -05:00
|
|
|
|
{
|
2023-03-21 18:55:21 -05:00
|
|
|
|
cu.BL_ESTADO_PFLXUSU = 0;
|
|
|
|
|
_context.PERFILESPORUSUARIOs.Update(cu);
|
2023-03-21 09:28:53 -05:00
|
|
|
|
await _context.SaveChangesAsync();
|
2023-02-02 10:50:02 -05:00
|
|
|
|
}
|
2023-03-21 18:55:21 -05:00
|
|
|
|
|
|
|
|
|
foreach (var pxu in guardar)
|
2023-02-02 10:50:02 -05:00
|
|
|
|
{
|
2023-03-21 18:55:21 -05:00
|
|
|
|
count++;
|
|
|
|
|
//Se comprueba existencia de cada pxu en base de datos
|
|
|
|
|
var existe_pxu = (from dm in _context.PERFILESPORUSUARIOs
|
|
|
|
|
where dm.NU_FK_PFL == pxu.NU_FK_PFL
|
2023-12-06 14:11:53 -05:00
|
|
|
|
&& dm.TX_UUID_USUMS == pxu.TX_UUID_USUMS
|
2023-03-21 18:55:21 -05:00
|
|
|
|
select dm).ToList();
|
|
|
|
|
|
|
|
|
|
if (existe_pxu.Count() > 0)
|
2023-03-15 15:57:59 -05:00
|
|
|
|
{
|
2023-03-21 18:55:21 -05:00
|
|
|
|
//Si existe activamos estado 1 del perfil
|
|
|
|
|
existe_pxu[0].BL_ESTADO_PFLXUSU = 1;
|
|
|
|
|
_context.Update(existe_pxu[0]);
|
|
|
|
|
await _context.SaveChangesAsync();
|
2023-03-15 15:57:59 -05:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2023-03-21 18:55:21 -05:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (pxu == null)
|
|
|
|
|
{
|
|
|
|
|
error.Add($"Perfil por usuario #{count} sin valores");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pxu.BL_ESTADO_PFLXUSU = 1;
|
|
|
|
|
_context.PERFILESPORUSUARIOs.Add(pxu);
|
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
|
}
|
2023-02-02 10:50:02 -05:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-21 18:55:21 -05:00
|
|
|
|
dbContextTransaction.Commit();
|
2023-03-21 09:28:53 -05:00
|
|
|
|
|
2023-03-21 18:55:21 -05:00
|
|
|
|
List<string> all = new List<string>();
|
2023-03-21 09:28:53 -05:00
|
|
|
|
|
2023-03-21 18:55:21 -05:00
|
|
|
|
all.Add($"Total registros: {guardar.Count().ToString()}");
|
|
|
|
|
all.Add($"Exitosos: {(guardar.Count() - error.Count()).ToString()}");
|
|
|
|
|
all.Add($"Erroneos: {error.Count().ToString()}");
|
2023-03-21 09:28:53 -05:00
|
|
|
|
|
2023-03-21 18:55:21 -05:00
|
|
|
|
var flag_r = 0;
|
|
|
|
|
foreach (var r in error)
|
|
|
|
|
{
|
|
|
|
|
flag_r++;
|
|
|
|
|
all.Add($"Error #{flag_r}: {r}");
|
|
|
|
|
}
|
2023-03-21 09:28:53 -05:00
|
|
|
|
|
2023-03-21 18:55:21 -05:00
|
|
|
|
if (error.Count() > 0)
|
|
|
|
|
{
|
|
|
|
|
return Conflict(all.ToList());
|
|
|
|
|
}
|
|
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
2023-03-21 09:28:53 -05:00
|
|
|
|
{
|
2023-03-21 18:55:21 -05:00
|
|
|
|
//hacemos rollback si hay excepción
|
|
|
|
|
dbContextTransaction.Rollback();
|
|
|
|
|
return BadRequest(ex.Message);
|
2023-03-21 09:28:53 -05:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-21 18:55:21 -05:00
|
|
|
|
};
|
|
|
|
|
}
|
2023-03-15 15:57:59 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2023-02-02 10:50:02 -05:00
|
|
|
|
}
|
|
|
|
|
|