339 lines
16 KiB
C#
339 lines
16 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using ULA.Cathay.CreditoModel.Credito;
|
|
using Ultimus.Utilitarios;
|
|
using Ultimus.Interfaces;
|
|
using System.Data.Entity;
|
|
using System.Data.Entity.Validation;
|
|
using ULA.Cathay.CreditoModel.DAL.CorporativoDatosRecientes;
|
|
using ULA.Cathay.CreditoModel.CorporativoDatosRecientes;
|
|
using ULA.Cathay.CreditoModel.Entidades;
|
|
|
|
namespace ULA.Cathay.CreditoModel.DAL.Credito
|
|
{
|
|
public class HojaSeleccionDAL
|
|
{
|
|
private UltimusLogs logs = new UltimusLogs("HojaSeleccionDAL");
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public List<HojaSeleccion> ObtenerValorCriteriosSolicitud(HojaSeleccion model)
|
|
{
|
|
List<HojaSeleccion> respuesta = new List<HojaSeleccion>();
|
|
|
|
try
|
|
{
|
|
using (CreditoContex dbContext = new CreditoContex())
|
|
{
|
|
|
|
var datos = (from d in dbContext.CriterioSeleccion
|
|
join e in dbContext.HojaSeleccion.Include(e => e.Moneda)
|
|
on new { IdCriterioSeleccion = d.IdCriterioSeleccion, IdSolicitud = model.IdSolicitud }
|
|
equals new { IdCriterioSeleccion = e.IdCriterioSeleccion, IdSolicitud = e.IdSolicitud }
|
|
into ej
|
|
from e in ej.DefaultIfEmpty()
|
|
where (e.IdSolicitud == model.IdSolicitud || (int?)e.IdSolicitud == null) && d.Estado == true
|
|
&& (e.IdCriterioSeleccion == model.IdCriterioSeleccion || model.IdCriterioSeleccion == 0)
|
|
select new
|
|
{
|
|
IdSolicitud = model.IdSolicitud,
|
|
IdCriterioSeleccion = d.IdCriterioSeleccion,
|
|
IdTipoSeleccion = e != null ? (int?)e.IdTipoSeleccion : null,
|
|
ValorCriterio = e != null ? e.ValorCriterio : null,
|
|
Monto = e != null ? (decimal?)e.Monto : null,
|
|
IdMoneda = e != null ? (int?)e.IdMoneda : null,
|
|
MontoEquivalente = e != null ? (decimal?)e.MontoEquivalente : null,
|
|
CriterioSeleccion = d,
|
|
Moneda = e != null ? e.Moneda : null,
|
|
}).ToList();
|
|
foreach (var dato in datos)
|
|
{
|
|
respuesta.Add(new HojaSeleccion
|
|
{
|
|
IdSolicitud = dato.IdSolicitud,
|
|
IdCriterioSeleccion = dato.IdCriterioSeleccion,
|
|
IdTipoSeleccion = dato.IdTipoSeleccion,
|
|
ValorCriterio = dato.ValorCriterio,
|
|
Monto = dato.Monto,
|
|
IdMoneda = dato.IdMoneda,
|
|
MontoEquivalente = dato.MontoEquivalente,
|
|
CriterioSeleccion = dato.CriterioSeleccion,
|
|
Moneda = dato.Moneda,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
catch (DbEntityValidationException ex)
|
|
{
|
|
logs.Error(ex);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logs.Error(ex);
|
|
}
|
|
|
|
return respuesta;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="filtro"></param>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public List<JsonCatalogos> OpcionesValoresCriterioSeleccion(string filtro = null, int? id = null, int? id2 = null)
|
|
{
|
|
List<JsonCatalogos> registros = null;
|
|
List<MatrizSeleccionRelacion> ListaFiltradaMatrizSeleccionRelacion = null;
|
|
try
|
|
{
|
|
using (CreditoContex db = new CreditoContex())
|
|
{
|
|
#region MatrizSeleccionRelacion
|
|
|
|
List<MatrizSeleccionRelacion> ListaMatrizSeleccionRelacion = db.MatrizSeleccionRelacion.Where(x => x.IdCriterioSeleccionRelacion == id && x.IdTipoRelacion == EnumTipoRelacionCriterioSeleccion.REGLA_DEPENDENCIA_ENTRE_CRITERIOS_CATALOGOS && x.Estado == true).ToList();
|
|
|
|
if (ListaMatrizSeleccionRelacion != null && ListaMatrizSeleccionRelacion.Count > 0)
|
|
{
|
|
List<HojaSeleccion> ListaHojaSeleccion = ObtenerValorCriteriosSolicitud(new HojaSeleccion() { IdSolicitud = id2.Value });
|
|
|
|
ListaFiltradaMatrizSeleccionRelacion = (from _itemMtzSeleccionRelacion in ListaMatrizSeleccionRelacion
|
|
join _itemHojaSeleccion in ListaHojaSeleccion
|
|
on new { IdCrit = (_itemMtzSeleccionRelacion.IdCriterioSeleccion??0), ValorCrit = _itemMtzSeleccionRelacion.ValorCriterioSeleccion }
|
|
equals new { IdCrit = _itemHojaSeleccion.IdCriterioSeleccion, ValorCrit = _itemHojaSeleccion.ValorCriterio }
|
|
select _itemMtzSeleccionRelacion).ToList();
|
|
}
|
|
|
|
#endregion
|
|
|
|
if (string.IsNullOrWhiteSpace(filtro))
|
|
registros = db.MatrizSeleccion.Where(t => t.Estado == true && t.IdCriterioSeleccion == id).Select(t => new JsonCatalogos()
|
|
{
|
|
codigo = t.IdTipoSeleccion.ToString(),
|
|
descripcion = t.Valor,
|
|
CodigoRespuesta = t.IdCriterioSeleccion.ToString()
|
|
}).ToList();
|
|
else
|
|
registros = db.MatrizSeleccion.Where(t => t.Estado == true && t.IdCriterioSeleccion == id && t.Valor.Trim().ToLower().Contains(filtro.Trim().ToLower()) == true).Select(t => new JsonCatalogos()
|
|
{
|
|
codigo = t.IdTipoSeleccion.ToString(),
|
|
descripcion = t.Valor,
|
|
CodigoRespuesta = t.IdCriterioSeleccion.ToString()
|
|
}).ToList();
|
|
|
|
|
|
if (ListaFiltradaMatrizSeleccionRelacion != null && ListaFiltradaMatrizSeleccionRelacion.Count > 0) {
|
|
registros = (from registro in registros
|
|
join itemFiltrado in ListaFiltradaMatrizSeleccionRelacion
|
|
on registro.descripcion equals itemFiltrado.ValorCriterioSeleccionRelacion
|
|
select registro).ToList();
|
|
}
|
|
}
|
|
}
|
|
catch (DbEntityValidationException ex1)
|
|
{
|
|
logs.Error(ex1);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logs.Error(ex);
|
|
}
|
|
|
|
return registros;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Insertar y Actualizar los valores de los criterios de selección para cada solicitud de crédito
|
|
/// </summary>
|
|
/// <param name="model">Información del parametros de selcción (modelo de HojaSeleccion)</param>
|
|
/// <returns>Resultado de la operación</returns>
|
|
public BaseJson InsertarActualizar(HojaSeleccion model)
|
|
{
|
|
HojaSeleccion ModeloActual = new HojaSeleccion();
|
|
MatrizSeleccion Matriz = new MatrizSeleccion();
|
|
BaseJson ValorRetorno = new BaseJson();
|
|
ClienteCriteriosSeleccionDal Conn = new ClienteCriteriosSeleccionDal();
|
|
ClienteCriteriosSeleccion CriteriosCliente = new ClienteCriteriosSeleccion();
|
|
Moneda ObjMoneda = new Moneda();
|
|
decimal ValorCriterio = 0;
|
|
bool EsNumerico = false;
|
|
|
|
try
|
|
{
|
|
ValorRetorno.CodigoRespuesta = TipoCodigoRespuesta.EXITOSO.ToString();
|
|
using (CreditoContex dataContex = new CreditoContex())
|
|
{
|
|
if (model.IdTipoSeleccion == null && dataContex.CriterioSeleccion.Where(q => q.IdCriterioSeleccion == model.IdCriterioSeleccion).FirstOrDefault().EsPonderado.Value)
|
|
{
|
|
EsNumerico = decimal.TryParse(model.ValorCriterio, out ValorCriterio);
|
|
Matriz = (from m in dataContex.MatrizSeleccion
|
|
where m.IdCriterioSeleccion == model.IdCriterioSeleccion
|
|
&& ((m.IdTipoValor == 1 && m.ValorInferior <= (EsNumerico ? (decimal?)ValorCriterio : null) && m.ValorSuperior >= (EsNumerico ? (decimal?)ValorCriterio : null))
|
|
|| ((m.IdTipoValor == 2 || m.IdTipoValor == 3) && m.Valor == model.ValorCriterio))
|
|
select m).FirstOrDefault();
|
|
|
|
if (Matriz != null)
|
|
{
|
|
model.IdTipoSeleccion = Matriz.IdTipoSeleccion;
|
|
model.Peso = Matriz.Peso;
|
|
}
|
|
else
|
|
{
|
|
ValorRetorno.CodigoRespuesta = TipoCodigoRespuesta.ADVERTENCIA.ToString();
|
|
ValorRetorno.DescripcionRespuesta = "No se Pudo Determinar un Valor en la Configuración del Criterios de selección, Para el valor especificado.";
|
|
}
|
|
}
|
|
else if (model.IdTipoSeleccion != null)
|
|
{
|
|
Matriz = (from m in dataContex.MatrizSeleccion
|
|
where m.IdCriterioSeleccion == model.IdCriterioSeleccion && m.IdTipoSeleccion == model.IdTipoSeleccion
|
|
select m).FirstOrDefault();
|
|
if (Matriz != null)
|
|
model.Peso = Matriz.Peso;
|
|
else
|
|
{
|
|
ValorRetorno.CodigoRespuesta = TipoCodigoRespuesta.ADVERTENCIA.ToString();
|
|
ValorRetorno.DescripcionRespuesta = "No se Pudo Determinar un Valor del Peso en la Configuración del Criterios de selección, Para el valor especificado.";
|
|
}
|
|
}
|
|
else
|
|
model.Peso = null;
|
|
|
|
if (ValorRetorno.CodigoRespuesta == TipoCodigoRespuesta.EXITOSO.ToString())
|
|
{
|
|
ModeloActual = dataContex.HojaSeleccion.Where(e => e.IdSolicitud == model.IdSolicitud && e.IdCriterioSeleccion == model.IdCriterioSeleccion).FirstOrDefault();
|
|
if (ModeloActual != null)
|
|
{
|
|
model.UsuarioModificado = model.UsuarioCreador;
|
|
model.FechaModificacion = DateTime.Now;
|
|
model.FechaCreacion = ModeloActual.FechaCreacion;
|
|
model.UsuarioCreador = ModeloActual.UsuarioCreador;
|
|
dataContex.Entry(ModeloActual).State = System.Data.Entity.EntityState.Detached;
|
|
dataContex.HojaSeleccion.Attach(model);
|
|
dataContex.Entry(model).State = System.Data.Entity.EntityState.Modified;
|
|
}
|
|
else
|
|
{
|
|
model.FechaCreacion = DateTime.Now;
|
|
dataContex.HojaSeleccion.Add(model);
|
|
}
|
|
dataContex.SaveChanges();
|
|
ObjMoneda = dataContex.Moneda.Where(q => q.IdMoneda == model.IdMoneda).FirstOrDefault();
|
|
}
|
|
}
|
|
if (ValorRetorno.CodigoRespuesta == TipoCodigoRespuesta.EXITOSO.ToString())
|
|
{
|
|
CriteriosCliente.CopyValueProperties(model);
|
|
CriteriosCliente.DescripcionMoneda = ObjMoneda != null ? ObjMoneda.Descripcion : "";
|
|
Conn.InsertarActualizar(CriteriosCliente, model.IdSolicitud);
|
|
}
|
|
}
|
|
catch (DbEntityValidationException ex1)
|
|
{
|
|
logs.Error(ex1);
|
|
ValorRetorno.CodigoRespuesta = TipoCodigoRespuesta.ERROR.ToString();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logs.Error(ex);
|
|
ValorRetorno.CodigoRespuesta = TipoCodigoRespuesta.ERROR.ToString();
|
|
}
|
|
finally
|
|
{
|
|
ModeloActual = null;
|
|
Matriz = null;
|
|
CriteriosCliente = null;
|
|
Conn = null;
|
|
ObjMoneda = null;
|
|
}
|
|
|
|
return ValorRetorno;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Eliminar los valores de los criterios de selección para cada solicitud de crédito
|
|
/// </summary>
|
|
/// <param name="model">Información del parametros de selcción (modelo de HojaSeleccion)</param>
|
|
public BaseJson EliminarDatos(HojaSeleccion model)
|
|
{
|
|
HojaSeleccion ModeloActual = new HojaSeleccion();
|
|
BaseJson ValorRetorno = new BaseJson();
|
|
|
|
try
|
|
{
|
|
ValorRetorno.CodigoRespuesta = TipoCodigoRespuesta.EXITOSO.ToString();
|
|
using (CreditoContex dataContex = new CreditoContex())
|
|
{
|
|
ModeloActual = dataContex.HojaSeleccion.Where(e => e.IdSolicitud == model.IdSolicitud && e.IdCriterioSeleccion == model.IdCriterioSeleccion).FirstOrDefault();
|
|
if (ModeloActual != null)
|
|
{
|
|
dataContex.HojaSeleccion.Remove(ModeloActual);
|
|
dataContex.SaveChanges();
|
|
}
|
|
}
|
|
}
|
|
catch (DbEntityValidationException ex1)
|
|
{
|
|
logs.Error(ex1);
|
|
ValorRetorno.CodigoRespuesta = TipoCodigoRespuesta.ERROR.ToString();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logs.Error(ex);
|
|
ValorRetorno.CodigoRespuesta = TipoCodigoRespuesta.ERROR.ToString();
|
|
}
|
|
finally
|
|
{
|
|
ModeloActual = null;
|
|
}
|
|
|
|
return ValorRetorno;
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Eliminar los valores de los criterios de selección relacionados al criterio principal para cada solicitud de crédito
|
|
/// </summary>
|
|
/// <param name="model">Información del parametros de selección (modelo de HojaSeleccion)</param>
|
|
public BaseJson EliminarDatosCriteriosRelacion(HojaSeleccion model)
|
|
{
|
|
MatrizSeleccionRelacionDAL ConnMtz = new MatrizSeleccionRelacionDAL();
|
|
BaseJson ValorRetorno = new BaseJson();
|
|
|
|
try
|
|
{
|
|
ValorRetorno.CodigoRespuesta = TipoCodigoRespuesta.EXITOSO.ToString();
|
|
|
|
List<MatrizSeleccionRelacion> ListaMatrizSeleccionRelacion = ConnMtz.Obtener().Where(x => x.IdCriterioSeleccion == model.IdCriterioSeleccion && x.IdTipoRelacion == EnumTipoRelacionCriterioSeleccion.REGLA_DEPENDENCIA_ENTRE_CRITERIOS_CATALOGOS && x.Estado == true).ToList();
|
|
|
|
if (ListaMatrizSeleccionRelacion != null && ListaMatrizSeleccionRelacion.Count > 0)
|
|
{
|
|
foreach (MatrizSeleccionRelacion itemMatrizSeleccionRelacion in ListaMatrizSeleccionRelacion)
|
|
{
|
|
ValorRetorno = EliminarDatos(new HojaSeleccion() { IdSolicitud = model.IdSolicitud, IdCriterioSeleccion = itemMatrizSeleccionRelacion.IdCriterioSeleccionRelacion.Value });
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logs.Error(ex);
|
|
ValorRetorno.CodigoRespuesta = TipoCodigoRespuesta.ERROR.ToString();
|
|
}
|
|
finally
|
|
{
|
|
}
|
|
|
|
return ValorRetorno;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|