| | | 1 | | using FAU.Entidades; |
| | | 2 | | using FAU.Logica.DTOs; |
| | | 3 | | using FAU.Logica.Services; |
| | | 4 | | using Microsoft.AspNetCore.Authorization; |
| | | 5 | | using Microsoft.AspNetCore.Mvc; |
| | | 6 | | |
| | | 7 | | namespace FAU.API.Controllers; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Gestión de personal militar |
| | | 11 | | /// </summary> |
| | | 12 | | [ApiController] |
| | | 13 | | [Route("api/[controller]")] |
| | | 14 | | [Authorize] |
| | | 15 | | public class PersonalController : ControllerBase |
| | | 16 | | { |
| | | 17 | | private readonly IPersonalService _personalService; |
| | | 18 | | private readonly ILogger<PersonalController> _logger; |
| | | 19 | | |
| | 22 | 20 | | public PersonalController( |
| | 22 | 21 | | IPersonalService personalService, |
| | 22 | 22 | | ILogger<PersonalController> logger) |
| | | 23 | | { |
| | 22 | 24 | | _personalService = personalService; |
| | 22 | 25 | | _logger = logger; |
| | 22 | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Listar personal con filtros y paginación |
| | | 30 | | /// GET /api/personal?buscar=&cargoId=&estado=&ley=&page=&pageSize=&orderBy=&sort= |
| | | 31 | | /// </summary> |
| | | 32 | | [HttpGet] |
| | | 33 | | [FAU.API.Security.RequirePermission(PermisoEnum.VerPersonal)] |
| | | 34 | | public async Task<IActionResult> GetPersonal( |
| | | 35 | | [FromQuery] string? buscar = null, |
| | | 36 | | [FromQuery] long? cargoId = null, |
| | | 37 | | [FromQuery] string? estado = null, |
| | | 38 | | [FromQuery] string? ley = null, |
| | | 39 | | [FromQuery] int page = 1, |
| | | 40 | | [FromQuery] int pageSize = 25, |
| | | 41 | | [FromQuery] string? orderBy = null, |
| | | 42 | | [FromQuery] string sort = "asc") |
| | | 43 | | { |
| | | 44 | | try |
| | | 45 | | { |
| | | 46 | | // Validación de parámetros |
| | 3 | 47 | | if (page < 1) page = 1; |
| | 3 | 48 | | if (pageSize < 1) pageSize = 25; |
| | 3 | 49 | | if (pageSize > 100) pageSize = 100; |
| | | 50 | | |
| | 3 | 51 | | var result = await _personalService.GetPersonalListAsync( |
| | 3 | 52 | | buscar, cargoId, estado, ley, page, pageSize, orderBy, sort); |
| | | 53 | | |
| | 2 | 54 | | return Ok(ApiResponse<object>.SuccessResult(result)); |
| | | 55 | | } |
| | 1 | 56 | | catch (Exception ex) |
| | | 57 | | { |
| | 1 | 58 | | _logger.LogError(ex, "Error al obtener listado de personal"); |
| | 1 | 59 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 60 | | } |
| | 3 | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Obtener detalle completo de personal |
| | | 65 | | /// GET /api/personal/{id} |
| | | 66 | | /// Retorna: {id, datosPersonales, datosLaborales, historial} |
| | | 67 | | /// </summary> |
| | | 68 | | [HttpGet("{id}")] |
| | | 69 | | [FAU.API.Security.RequirePermission(PermisoEnum.VerPersonal)] |
| | | 70 | | public async Task<IActionResult> GetPersonalDetalle(long id) |
| | | 71 | | { |
| | | 72 | | try |
| | | 73 | | { |
| | 3 | 74 | | var detalle = await _personalService.GetDetalleAsync(id); |
| | | 75 | | |
| | 2 | 76 | | if (detalle == null) |
| | | 77 | | { |
| | 1 | 78 | | return NotFound(ApiResponse<string>.ErrorResult("Personal no encontrado")); |
| | | 79 | | } |
| | | 80 | | |
| | 1 | 81 | | return Ok(ApiResponse<PersonalDetalleDto>.SuccessResult(detalle)); |
| | | 82 | | } |
| | 1 | 83 | | catch (Exception ex) |
| | | 84 | | { |
| | 1 | 85 | | _logger.LogError(ex, "Error al obtener detalle de personal {Id}", id); |
| | 1 | 86 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 87 | | } |
| | 3 | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// Alta de personal |
| | | 92 | | /// POST /api/personal |
| | | 93 | | /// Body: { ci, nombre, apellido, fechaNacimiento, email, telefono, direccion, codigoPostal, departamento, localidad |
| | | 94 | | /// Response: { id, creado:true } |
| | | 95 | | /// </summary> |
| | | 96 | | [HttpPost] |
| | | 97 | | [FAU.API.Security.RequirePermission(PermisoEnum.CrearPersonal)] |
| | | 98 | | public async Task<IActionResult> CreatePersonal([FromBody] CreatePersonalRequest request) |
| | | 99 | | { |
| | | 100 | | try |
| | | 101 | | { |
| | 4 | 102 | | var (success, personalId, error) = await _personalService.CreatePersonalAsync(request); |
| | | 103 | | |
| | 3 | 104 | | if (!success) |
| | | 105 | | { |
| | 2 | 106 | | if (error?.Contains("ya existe") == true || error?.Contains("única") == true) |
| | | 107 | | { |
| | 1 | 108 | | return Conflict(ApiResponse<string>.ErrorResult(error)); |
| | | 109 | | } |
| | 1 | 110 | | return BadRequest(ApiResponse<string>.ErrorResult(error ?? "Error al crear personal")); |
| | | 111 | | } |
| | | 112 | | |
| | 1 | 113 | | return CreatedAtAction( |
| | 1 | 114 | | nameof(GetPersonalDetalle), |
| | 1 | 115 | | new { id = personalId }, |
| | 1 | 116 | | ApiResponse<object>.SuccessResult(new { id = personalId, creado = true })); |
| | | 117 | | } |
| | 1 | 118 | | catch (Exception ex) |
| | | 119 | | { |
| | 1 | 120 | | _logger.LogError(ex, "Error al crear personal"); |
| | 1 | 121 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 122 | | } |
| | 4 | 123 | | } |
| | | 124 | | |
| | | 125 | | /// <summary> |
| | | 126 | | /// Modificar personal |
| | | 127 | | /// PUT /api/personal/{id} |
| | | 128 | | /// Body: { ...campos editables..., estado? } |
| | | 129 | | /// Response: { actualizado:true } |
| | | 130 | | /// </summary> |
| | | 131 | | [HttpPut("{id}")] |
| | | 132 | | [FAU.API.Security.RequirePermission(PermisoEnum.ActualizarPersonal)] |
| | | 133 | | public async Task<IActionResult> UpdatePersonal(long id, [FromBody] UpdatePersonalRequest request) |
| | | 134 | | { |
| | | 135 | | try |
| | | 136 | | { |
| | 4 | 137 | | var (success, error) = await _personalService.UpdatePersonalAsync(id, request); |
| | | 138 | | |
| | 3 | 139 | | if (!success) |
| | | 140 | | { |
| | 2 | 141 | | if (error?.Contains("no encontrado") == true) |
| | | 142 | | { |
| | 1 | 143 | | return NotFound(ApiResponse<string>.ErrorResult(error)); |
| | | 144 | | } |
| | 1 | 145 | | return BadRequest(ApiResponse<string>.ErrorResult(error ?? "Error al actualizar personal")); |
| | | 146 | | } |
| | | 147 | | |
| | 1 | 148 | | return Ok(ApiResponse<object>.SuccessResult(new { actualizado = true })); |
| | | 149 | | } |
| | 1 | 150 | | catch (Exception ex) |
| | | 151 | | { |
| | 1 | 152 | | _logger.LogError(ex, "Error al actualizar personal {Id}", id); |
| | 1 | 153 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 154 | | } |
| | 4 | 155 | | } |
| | | 156 | | |
| | | 157 | | /// <summary> |
| | | 158 | | /// Registrar baja de personal |
| | | 159 | | /// DELETE /api/personal/{id}/baja |
| | | 160 | | /// Body: { motivoId, motivoBajaId, fechaBaja, observaciones } |
| | | 161 | | /// Response: { id, estado:'Inactivo' } |
| | | 162 | | /// </summary> |
| | | 163 | | [HttpDelete("{id}/baja")] |
| | | 164 | | [FAU.API.Security.RequirePermission(PermisoEnum.BajaPersonal)] |
| | | 165 | | public async Task<IActionResult> RegistrarBaja(long id, [FromBody] RegistrarBajaRequest request) |
| | | 166 | | { |
| | | 167 | | try |
| | | 168 | | { |
| | 4 | 169 | | var (success, error) = await _personalService.RegistrarBajaAsync(id, request); |
| | | 170 | | |
| | 3 | 171 | | if (!success) |
| | | 172 | | { |
| | 2 | 173 | | if (error?.Contains("no encontrado") == true) |
| | | 174 | | { |
| | 1 | 175 | | return NotFound(ApiResponse<string>.ErrorResult(error)); |
| | | 176 | | } |
| | 1 | 177 | | return BadRequest(ApiResponse<string>.ErrorResult(error ?? "Error al registrar baja")); |
| | | 178 | | } |
| | | 179 | | |
| | 1 | 180 | | return Ok(ApiResponse<object>.SuccessResult( |
| | 1 | 181 | | new { id = id, estado = "Inactivo" }, |
| | 1 | 182 | | "Baja registrada exitosamente")); |
| | | 183 | | } |
| | 1 | 184 | | catch (Exception ex) |
| | | 185 | | { |
| | 1 | 186 | | _logger.LogError(ex, "Error al registrar baja de personal {Id}", id); |
| | 1 | 187 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 188 | | } |
| | 4 | 189 | | } |
| | | 190 | | |
| | | 191 | | /// <summary> |
| | | 192 | | /// Registrar ascenso |
| | | 193 | | /// POST /api/personal/{id}/ascenso |
| | | 194 | | /// Body: { nuevoGradoId, fechaAscenso, Observacion, unidadId, leyId } |
| | | 195 | | /// Response: { id, gradoActualizado:true } |
| | | 196 | | /// </summary> |
| | | 197 | | [HttpPost("{id}/ascenso")] |
| | | 198 | | [FAU.API.Security.RequirePermission(PermisoEnum.AscensoPersonal)] |
| | | 199 | | public async Task<IActionResult> RegistrarAscenso(long id, [FromBody] FAU.Logica.DTOs.AscensoRequest request) |
| | | 200 | | { |
| | | 201 | | try |
| | | 202 | | { |
| | 4 | 203 | | var (success, error) = await _personalService.RegistrarAscensoAsync(id, request); |
| | | 204 | | |
| | 3 | 205 | | if (!success) |
| | | 206 | | { |
| | 2 | 207 | | _logger.LogWarning("Error al registrar ascenso para PersonaId {Id}: {Error}", id, error); |
| | 2 | 208 | | if (error?.Contains("no encontrado") == true) |
| | | 209 | | { |
| | 1 | 210 | | return NotFound(ApiResponse<string>.ErrorResult(error)); |
| | | 211 | | } |
| | 1 | 212 | | return BadRequest(ApiResponse<string>.ErrorResult(error ?? "Error al registrar ascenso")); |
| | | 213 | | } |
| | | 214 | | |
| | 1 | 215 | | return Ok(ApiResponse<object>.SuccessResult(new { id = id, gradoActualizado = true }, "Ascenso registrado ex |
| | | 216 | | } |
| | 1 | 217 | | catch (Exception ex) |
| | | 218 | | { |
| | 1 | 219 | | _logger.LogError(ex, "Error al registrar ascenso de personal {Id}", id); |
| | 1 | 220 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 221 | | } |
| | 4 | 222 | | } |
| | | 223 | | |
| | | 224 | | /// GET /api/personal/reporte-movimientos?mes=5&anio=2026&tipo=todos |
| | | 225 | | [HttpGet("reporte-movimientos")] |
| | | 226 | | [Authorize(Roles = "Administrador,Supervisor")] |
| | | 227 | | public async Task<IActionResult> GetReporteMovimientos( |
| | | 228 | | [FromQuery] int mes, |
| | | 229 | | [FromQuery] int anio, |
| | | 230 | | [FromQuery] string? tipo = "todos") |
| | | 231 | | { |
| | 0 | 232 | | if (mes < 1 || mes > 12) |
| | 0 | 233 | | return BadRequest(ApiResponse<string>.ErrorResult("El mes debe estar entre 1 y 12")); |
| | 0 | 234 | | if (anio < 2000 || anio > 2100) |
| | 0 | 235 | | return BadRequest(ApiResponse<string>.ErrorResult("El año no es válido")); |
| | | 236 | | |
| | 0 | 237 | | var tipoValido = tipo?.ToLower(); |
| | 0 | 238 | | if (tipoValido != null && tipoValido != "todos" && tipoValido != "alta" && tipoValido != "baja" && tipoValido != |
| | 0 | 239 | | return BadRequest(ApiResponse<string>.ErrorResult("El tipo debe ser: alta, baja, ascenso o todos")); |
| | | 240 | | |
| | | 241 | | try |
| | | 242 | | { |
| | 0 | 243 | | var reporte = await _personalService.ObtenerReporteMovimientosAsync(mes, anio, tipoValido); |
| | 0 | 244 | | return Ok(ApiResponse<object>.SuccessResult(new { items = reporte, total = reporte.Count })); |
| | | 245 | | } |
| | 0 | 246 | | catch (Exception ex) |
| | | 247 | | { |
| | 0 | 248 | | _logger.LogError(ex, "Error al obtener reporte de movimientos mes={Mes} anio={Anio}", mes, anio); |
| | 0 | 249 | | return StatusCode(500, ApiResponse<string>.ErrorResult("Error interno del servidor")); |
| | | 250 | | } |
| | 0 | 251 | | } |
| | | 252 | | } |