using Exferia_Aplicacion.General; using Exferia_EntityFramework; using Exferia_General; using Exferia_KairosPRO._1_Modelos; using Exferia_KairosPRO._1_Modelos.Herencia.Capas; using Exferia_KairosPRO_EF; using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading; using System.Transactions; using static Exferia_Aplicacion.General.Enumerados; namespace Exferia_KairosPRO._2_Datos { public class KRS_PerfilesCabecera_Datos : ABS_Datos { #region Listado private List g_lst_INTERNO_KRS_PerfilesCabecera_Listado_Modelo = null; public override List Obtener_Listado(List _lst_CamposConRelacionABuscar, List _lst_INTERNO_Filtro_OpcionPadre_Modelo, List _lst_INTERNO_Filtro_Modelo) { KairosProEntities obj_KairosProEntities = null; List lst_KRS = null; try { obj_KairosProEntities = new KairosProEntities(); obj_KairosProEntities.Configuration.LazyLoadingEnabled = false; //Consulta................................................................. ConsultaRellenar_Listado(obj_KairosProEntities, _lst_CamposConRelacionABuscar, -1, null, _lst_INTERNO_Filtro_OpcionPadre_Modelo, _lst_INTERNO_Filtro_Modelo); //........................................................................ if ((g_lst_INTERNO_KRS_PerfilesCabecera_Listado_Modelo != null) && (g_lst_INTERNO_KRS_PerfilesCabecera_Listado_Modelo.Count() > 0)) { lst_KRS = g_lst_INTERNO_KRS_PerfilesCabecera_Listado_Modelo.Cast().ToList(); } } catch (Control_Errores) { throw; } catch (Exception ex) { throw new Control_Errores("", ex, nameof(KRS_PerfilesCabecera_Datos) + "/" + nameof(Obtener_Listado), true); } finally { obj_KairosProEntities.Database.Connection.Close(); obj_KairosProEntities.Dispose(); } return lst_KRS; } public override dynamic Obtener_RegistroListado(List _lst_CamposConRelacionABuscar, List _lst_INTERNO_Filtro_OpcionPadre_Modelo, List _lst_INTERNO_Filtro_Modelo, long _lng_id, List _lst_id) { KairosProEntities obj_KairosProEntities = null; dynamic dnm_KRS = null; try { obj_KairosProEntities = new KairosProEntities(); obj_KairosProEntities.Configuration.LazyLoadingEnabled = false; //Consulta ...................................................... ConsultaRellenar_Listado(obj_KairosProEntities, _lst_CamposConRelacionABuscar, _lng_id, _lst_id, _lst_INTERNO_Filtro_OpcionPadre_Modelo, _lst_INTERNO_Filtro_Modelo); //............................................................... if ((g_lst_INTERNO_KRS_PerfilesCabecera_Listado_Modelo != null) && (g_lst_INTERNO_KRS_PerfilesCabecera_Listado_Modelo.Count() > 0)) { dnm_KRS = g_lst_INTERNO_KRS_PerfilesCabecera_Listado_Modelo.Cast().ToList(); } } catch (Control_Errores) { throw; } catch (Exception ex) { throw new Control_Errores("", ex, nameof(KRS_PerfilesCabecera_Datos) + "/" + nameof(Obtener_RegistroListado), true); } finally { obj_KairosProEntities.Database.Connection.Close(); obj_KairosProEntities.Dispose(); } return dnm_KRS; } protected override void ConsultaRellenar_Listado(KairosProEntities _dbcontext, List _lst_CamposConRelacionABuscar, long _lng_id, List _lst_id, List _lst_INTERNO_Filtro_OpcionPadre_Modelo, List _lst_INTERNO_Filtro_Modelo) { try { //Datos del Padre si los tuviera ......................... long lng_Ejercicio_Padre_id = -1; string str_Ejercicio_Padre_Opcion = ""; if (_lst_INTERNO_Filtro_OpcionPadre_Modelo != null && _lst_INTERNO_Filtro_OpcionPadre_Modelo.Count > 0) { foreach (INTERNO_Filtro_OpcionPadre_Modelo mdl_INTERNO_Filtro_OpcionPadre_Modelo in _lst_INTERNO_Filtro_OpcionPadre_Modelo) { //Opciones if (mdl_INTERNO_Filtro_OpcionPadre_Modelo.NOMBRE_CAMPO.Equals(nameof(INTERNO_KRS_PerfilesCabecera_Listado_Modelo.idEjercicio))) { lng_Ejercicio_Padre_id = mdl_INTERNO_Filtro_OpcionPadre_Modelo.VALOR; str_Ejercicio_Padre_Opcion = mdl_INTERNO_Filtro_OpcionPadre_Modelo.NOMBRE_CAMPO; } } } //............................................................ string str_SQL = " SELECT " + nameof(INTERNO_KRS_PerfilesCabecera_Listado_Modelo.id) + " = " + nameof(KRS_PerfilesCabecera) + "." + nameof(KRS_PerfilesCabecera.id) + "," + nameof(INTERNO_KRS_PerfilesCabecera_Listado_Modelo.codigoCliente) + " = " + nameof(KRS_PerfilesCabecera) + "." + nameof(KRS_PerfilesCabecera.codigoCliente) + "," + nameof(INTERNO_KRS_PerfilesCabecera_Listado_Modelo.codigo) + " = " + nameof(KRS_PerfilesCabecera) + "." + nameof(KRS_PerfilesCabecera.codigo) + "," + nameof(INTERNO_KRS_PerfilesCabecera_Listado_Modelo.descripcion) + " = " + nameof(KRS_PerfilesCabecera) + "." + nameof(KRS_PerfilesCabecera.descripcion) + "," + //ids nameof(INTERNO_KRS_PerfilesCabecera_Listado_Modelo.idEjercicio) + " = " + nameof(KRS_PerfilesCabecera) + "." + nameof(KRS_PerfilesCabecera.idEjercicio) + " FROM " + nameof(KRS_PerfilesCabecera) + " WHERE " + nameof(KRS_PerfilesCabecera) + "." + nameof(KRS_PerfilesCabecera.fechaBorrado) + " IS null AND " + nameof(KRS_PerfilesCabecera) + "." + nameof(KRS_PerfilesCabecera.codigoCliente) + "=@codigoCliente AND " + (_lng_id > -1 ? nameof(KRS_PerfilesCabecera) + "." + (nameof(KRS_PerfilesCabecera.id) + "=@id and ") : " 1 = 1 and " ) + //Filtro por datos del padre relacionado ( !str_Ejercicio_Padre_Opcion.Equals("") ? nameof(KRS_PerfilesCabecera) + "." + nameof(KRS_PerfilesCabecera.idEjercicio) + "=@idEjercicio " : " 1 = 1 " ) ; //Parametros object[] arr_Parametros = new object[] { new SqlParameter("@codigoCliente" , Variables.G_LNG_IDCLIENTE_LICENCIAVALIDADO), new SqlParameter("@id" , _lng_id), new SqlParameter("@idEjercicio" , lng_Ejercicio_Padre_id) }; //Rellenar el Modelo g_lst_INTERNO_KRS_PerfilesCabecera_Listado_Modelo = _dbcontext.Database.SqlQuery(str_SQL, arr_Parametros).ToList(); } catch (Control_Errores) { throw; } catch (Exception ex) { throw new Control_Errores("", ex, nameof(KRS_PerfilesCabecera_Datos) + "/" + nameof(ConsultaRellenar_Listado), true); } } #endregion #region Listado Secundario public override List Obtener_Listado_Secundario(List _lst_CamposConRelacionABuscar, List _lst_INTERNO_Filtro_OpcionPadre_Modelo, List _lst_INTERNO_Filtro_Modelo) { return null; } public override dynamic Obtener_RegistroListado_Secundario(List _lst_CamposConRelacionABuscar, List _lst_INTERNO_Filtro_OpcionPadre_Modelo, List _lst_INTERNO_Filtro_Modelo, long _lng_id, List _lst_id) { return null; } protected override void ConsultaRellenar_Listado_Secundario(KairosProEntities _dbcontext, List _lst_CamposConRelacionABuscar, long _lng_id, List _lst_id, List _lst_INTERNO_Filtro_OpcionPadre_Modelo, List _lst_INTERNO_Filtro_Modelo) { } #endregion #region F3 private INTERNO_KRS_PerfilesCabecera_Listado_Modelo g_mdl_INTERNO_KRS_PerfilesCabecera_Listado_Modelo_F3 = null; public override dynamic Obtener_F3(string _str_filtro, string _str_CampoBusqueda, List _lst_INTERNO_Filtro_OpcionPadre_Modelo, List _lst_INTERNO_Filtro_Modelo) { KairosProEntities obj_KairosProEntities = null; dynamic dnm_MAE = null; try { obj_KairosProEntities = new KairosProEntities(); obj_KairosProEntities.Configuration.LazyLoadingEnabled = false; //Consulta ...................................................... ConsultaRellenar_F3(obj_KairosProEntities, _str_CampoBusqueda, _str_filtro, -1, _lst_INTERNO_Filtro_OpcionPadre_Modelo, _lst_INTERNO_Filtro_Modelo); // .............................................................. if (g_mdl_INTERNO_KRS_PerfilesCabecera_Listado_Modelo_F3 != null) { dnm_MAE = g_mdl_INTERNO_KRS_PerfilesCabecera_Listado_Modelo_F3; } } catch (Control_Errores) { throw; } catch (Exception ex) { throw new Control_Errores("", ex, nameof(KRS_PerfilesCabecera_Datos) + "/" + nameof(Obtener_F3), true); } finally { obj_KairosProEntities.Database.Connection.Close(); obj_KairosProEntities.Dispose(); } return dnm_MAE; } public override dynamic Obtener_F3_PorId(long _lng_id, List _lst_INTERNO_Filtro_OpcionPadre_Modelo, List _lst_INTERNO_Filtro_Modelo) { KairosProEntities obj_KairosProEntities = null; dynamic dnm_MAE = null; try { obj_KairosProEntities = new KairosProEntities(); obj_KairosProEntities.Configuration.LazyLoadingEnabled = false; //Conuslta ...................................................... ConsultaRellenar_F3(obj_KairosProEntities, "", "", _lng_id, _lst_INTERNO_Filtro_OpcionPadre_Modelo, _lst_INTERNO_Filtro_Modelo); // .............................................................. if (g_mdl_INTERNO_KRS_PerfilesCabecera_Listado_Modelo_F3 != null) { dnm_MAE = g_mdl_INTERNO_KRS_PerfilesCabecera_Listado_Modelo_F3; } } catch (Control_Errores) { throw; } catch (Exception ex) { throw new Control_Errores("", ex, nameof(KRS_PerfilesCabecera_Datos) + "/" + nameof(Obtener_F3_PorId), true); } finally { obj_KairosProEntities.Database.Connection.Close(); obj_KairosProEntities.Dispose(); } return dnm_MAE; } protected override void ConsultaRellenar_F3(KairosProEntities _dbcontext, string _str_CampoBusqueda, string _str_Filtro, long _lng_id, List _lst_INTERNO_Filtro_OpcionPadre_Modelo, List _lst_INTERNO_Filtro_Modelo) { try { //Datos del Padre si los tuviera ......................... long lng_Ejercicio_Padre_id = -1; string str_Ejercicio_Padre_Opcion = ""; if (_lst_INTERNO_Filtro_OpcionPadre_Modelo != null && _lst_INTERNO_Filtro_OpcionPadre_Modelo.Count > 0) { foreach (INTERNO_Filtro_OpcionPadre_Modelo mdl_INTERNO_Filtro_OpcionPadre_Modelo in _lst_INTERNO_Filtro_OpcionPadre_Modelo) { //Opciones if (mdl_INTERNO_Filtro_OpcionPadre_Modelo.NOMBRE_CAMPO.Equals(nameof(INTERNO_KRS_PerfilesCabecera_Listado_Modelo.idEjercicio))) { lng_Ejercicio_Padre_id = mdl_INTERNO_Filtro_OpcionPadre_Modelo.VALOR; str_Ejercicio_Padre_Opcion = mdl_INTERNO_Filtro_OpcionPadre_Modelo.NOMBRE_CAMPO; } } } //............................................................ string str_SQL = " SELECT " + nameof(INTERNO_KRS_PerfilesCabecera_Listado_Modelo.id) + " = " + nameof(KRS_PerfilesCabecera) + "." + nameof(KRS_PerfilesCabecera.id) + "," + nameof(INTERNO_KRS_PerfilesCabecera_Listado_Modelo.codigoCliente) + " = " + nameof(KRS_PerfilesCabecera) + "." + nameof(KRS_PerfilesCabecera.codigoCliente) + "," + nameof(INTERNO_KRS_PerfilesCabecera_Listado_Modelo.codigo) + " = " + nameof(KRS_PerfilesCabecera) + "." + nameof(KRS_PerfilesCabecera.codigo) + "," + nameof(INTERNO_KRS_PerfilesCabecera_Listado_Modelo.descripcion) + " = " + nameof(KRS_PerfilesCabecera) + "." + nameof(KRS_PerfilesCabecera.descripcion) + "," + //ids nameof(INTERNO_KRS_PerfilesCabecera_Listado_Modelo.idEjercicio) + " = " + nameof(KRS_PerfilesCabecera) + "." + nameof(KRS_PerfilesCabecera.idEjercicio) + " FROM " + nameof(KRS_PerfilesCabecera) + " WHERE " + nameof(KRS_PerfilesCabecera) + "." + nameof(KRS_PerfilesCabecera.fechaBorrado) + " IS null AND " + nameof(KRS_PerfilesCabecera) + "." + nameof(KRS_PerfilesCabecera.codigoCliente) + "=@codigoCliente AND " + (_lng_id > -1 ? nameof(KRS_PerfilesCabecera) + "." + (nameof(KRS_PerfilesCabecera.id) + "=@id and ") : ( //Filtrar por Codigo (_str_CampoBusqueda.Equals(nameof(KRS_PerfilesCabecera.codigo))) ? nameof(KRS_PerfilesCabecera) + "." + nameof(KRS_PerfilesCabecera.codigo) + "=@filtro and " //Filtrar por Descripcion : nameof(KRS_PerfilesCabecera) + "." + nameof(KRS_PerfilesCabecera.descripcion) + "=@filtro and " ) ) + //Filtro por datos del padre relacionado ( !str_Ejercicio_Padre_Opcion.Equals("") ? nameof(KRS_PerfilesCabecera) + "." + nameof(KRS_PerfilesCabecera.idEjercicio) + "=@idEjercicio " : " 1 = 1 " ); //Parametros object[] arr_Parametros = new object[] { new SqlParameter("@codigoCliente" , Variables.G_LNG_IDCLIENTE_LICENCIAVALIDADO), new SqlParameter("@id" , _lng_id), new SqlParameter("@idEjercicio" , lng_Ejercicio_Padre_id), new SqlParameter("@filtro" , _str_Filtro) }; //Rellenar el Modelo g_mdl_INTERNO_KRS_PerfilesCabecera_Listado_Modelo_F3 = _dbcontext.Database.SqlQuery(str_SQL, arr_Parametros).FirstOrDefault(); } catch (Control_Errores) { throw; } catch (Exception ex) { throw new Control_Errores("", ex, nameof(KRS_PerfilesCabecera_Datos) + "/" + nameof(ConsultaRellenar_F3), true); } } #endregion #region Obtener #region Obtener Simple private dynamic g_dnm_Modelo_Obtener = null; private GEN_EjerciciosActivos_Empresa g_mdl_GEN_EjerciciosActivos_Empresa = null; private static int g_int_TotalHilos_Obtener = 1; private bool[] g_bol_Hilos_Terminados_Obtener = new bool[g_int_TotalHilos_Obtener]; public override dynamic Obtener(long _lng_id) { KairosProEntities obj_KairosProEntities = null; try { obj_KairosProEntities = new KairosProEntities(); obj_KairosProEntities.Configuration.LazyLoadingEnabled = false; string str_SQL = " SELECT " + nameof(KRS_PerfilesCabecera.id) + "," + nameof(KRS_PerfilesCabecera.codigoCliente) + "," + nameof(KRS_PerfilesCabecera.idEjercicio) + "," + nameof(KRS_PerfilesCabecera.codigo) + "," + nameof(KRS_PerfilesCabecera.descripcion) + "," + nameof(KRS_PerfilesCabecera.fechaBorrado) + " FROM " + nameof(KRS_PerfilesCabecera) + " WHERE " + nameof(KRS_PerfilesCabecera.id) + "=@id and " + nameof(KRS_PerfilesCabecera.codigoCliente) + "=@codigoCliente "; //Parametros object[] arr_Parametros = new object[] { new SqlParameter("@id", _lng_id), new SqlParameter("@codigoCliente", Variables.G_LNG_IDCLIENTE_LICENCIAVALIDADO) }; //Rellenar el Modelo g_dnm_Modelo_Obtener = obj_KairosProEntities.Database.SqlQuery(str_SQL, arr_Parametros).FirstOrDefault(); #region Rellenar tablas Relacionadas if (g_dnm_Modelo_Obtener != null) { //reiniciar Valores de los hilos for (int int_Contador = 0; int_Contador < g_int_TotalHilos_Obtener; int_Contador++) { g_bol_Hilos_Terminados_Obtener[int_Contador] = false; } //Ejercicio g_mdl_GEN_EjerciciosActivos_Empresa = Datos_Generales.GEN_EjerciciosActivos_Empresa_Obtener_Por_Id(g_dnm_Modelo_Obtener.idEjercicio); //KRS_PerfilesLinea ########################################################################################## ThreadStart obj_ThreadStart_PERFILESLINEAS = new ThreadStart(Obtener_PerfilesLineas_Modelo_Obtener); Thread obj_Thread_PERFILESLINEAS = new Thread(obj_ThreadStart_PERFILESLINEAS); obj_Thread_PERFILESLINEAS.Start(); //Esperando a que terminen todos los hilos ......................................... bool bol_Termino = false; while (bol_Termino == false) { bol_Termino = true; foreach (bool bol_Termino_Hilo in g_bol_Hilos_Terminados_Obtener) { if (bol_Termino_Hilo == false) { bol_Termino = false; break; } } Thread.Sleep(10); } //................................................................................. } #endregion } catch (Control_Errores) { throw; } catch (Exception ex) { throw new Control_Errores("", ex, nameof(KRS_PerfilesCabecera_Datos) + "/" + nameof(Obtener), true); } finally { obj_KairosProEntities.Database.Connection.Close(); obj_KairosProEntities.Dispose(); } return g_dnm_Modelo_Obtener; } #region Tablas Relacionadas private void Obtener_PerfilesLineas_Modelo_Obtener() { try { KRS_PerfilesLinea_Datos obj_KRS_PerfilesLinea_Datos = new KRS_PerfilesLinea_Datos(); g_dnm_Modelo_Obtener.KRS_PerfilesLinea = obj_KRS_PerfilesLinea_Datos.Obtener_ModeloListado_Por_PerfilCabeceraEjercicio(g_dnm_Modelo_Obtener.id, g_mdl_GEN_EjerciciosActivos_Empresa,true); } catch (Control_Errores) { } finally { g_bol_Hilos_Terminados_Obtener[0] = true; } } #endregion #endregion public override dynamic Obtener_Registro_PorCampo(string _str_CampoBusqueda, string _str_Filtro) { throw new NotImplementedException(); } #endregion #region MODELOS #region Modelo Simple private KRS_PerfilesCabecera g_mdl_KRS_PerfilesCabecera = null; public KRS_PerfilesCabecera Obtener_Modelo_Por_Id(long _lng_id,long _lng_CodigoCliente) { try { string str_SQL = Obtener_Consulta_Modelo() + //WHERE " WHERE " + nameof(KRS_PerfilesCabecera) + "." + nameof(KRS_PerfilesCabecera.id) + "=@id and " + nameof(KRS_PerfilesCabecera) + "." + nameof(KRS_PerfilesCabecera.codigoCliente) + "=@codigoCliente "; //Parametros object[] arr_Parametros = new object[] { new SqlParameter("@id", _lng_id), new SqlParameter("@codigoCliente", _lng_CodigoCliente) }; //Ejecutar y rellenar el modelo Ejecutar_Consulta_Modelo(str_SQL, arr_Parametros); } catch (Control_Errores) { throw; } catch (Exception ex) { throw new Control_Errores("", ex, nameof(KRS_PerfilesCabecera_Datos) + "/" + nameof(Obtener_Modelo_Por_Id), true); } return g_mdl_KRS_PerfilesCabecera; } private void Ejecutar_Consulta_Modelo(string _str_SQL, object[] _arr_Parametros) { KairosProEntities obj_KairosProEntities = null; try { //Conexion obj_KairosProEntities = new KairosProEntities(); obj_KairosProEntities.Configuration.LazyLoadingEnabled = false; //Rellenar el Modelo g_mdl_KRS_PerfilesCabecera = obj_KairosProEntities.Database.SqlQuery(_str_SQL, _arr_Parametros).FirstOrDefault(); } catch (Control_Errores) { throw; } catch (Exception ex) { throw new Control_Errores("", ex, nameof(KRS_PerfilesCabecera_Datos) + "/" + nameof(Ejecutar_Consulta_Modelo), true); } finally { obj_KairosProEntities.Database.Connection.Close(); obj_KairosProEntities.Dispose(); } } #endregion #region Modelo Listado private List g_lst_KRS_PerfilesCabecera = null; private GEN_EjerciciosActivos_Empresa g_mdl_GEN_EjerciciosActivos_Empresa_ModeloListado = null; public List Obtener_ModeloListado_Por_Ids(List _lst_Ids, GEN_EjerciciosActivos_Empresa _mdl_GEN_EjerciciosActivos_Empresa, bool _bol_Cargar_PerfilesLineas) { try { g_mdl_GEN_EjerciciosActivos_Empresa_ModeloListado = _mdl_GEN_EjerciciosActivos_Empresa; if (_lst_Ids != null && _lst_Ids.Count > 0) { List lst_Parametros = new List(); string str_Filtros_IDS = Funciones.Formar_ContenidoIN_ParaConsultaWhere(nameof(KRS_PerfilesCabecera), nameof(KRS_PerfilesCabecera.id), G_ENUM_FORMARCONTENIDOIN_CONDICIONANTES.NADA, _lst_Ids); //Consulta string str_SQL = Obtener_Consulta_Modelo() + //WHERE " WHERE " + str_Filtros_IDS; //Parametros object[] arr_Parametros = lst_Parametros.ToArray(); //Ejecutar y rellenar el listado Ejecutar_Consulta_Modelo_Listado(str_SQL, arr_Parametros, _bol_Cargar_PerfilesLineas); } } catch (Control_Errores) { throw; } catch (Exception ex) { throw new Control_Errores("", ex, nameof(KRS_PerfilesCabecera_Datos) + "/" + nameof(Obtener_ModeloListado_Por_Ids), true); } return g_lst_KRS_PerfilesCabecera; } #region Ejecutar Consulta //Listado de modelos Relacionados private List g_lst_KRS_PerfilesLinea = null; //Hilos private static int g_int_TotalHilos_Modelo_Listado = 1; private bool[] g_bol_Hilos_Terminados_Modelo_Listado = new bool[g_int_TotalHilos_Modelo_Listado]; private void Ejecutar_Consulta_Modelo_Listado(string _str_SQL, object[] _arr_Parametros, bool _bol_Cargar_PerfilesLineas) { KairosProEntities obj_KairosProEntities = null; try { //Conexion obj_KairosProEntities = new KairosProEntities(); obj_KairosProEntities.Configuration.LazyLoadingEnabled = false; //Rellenar el modelo g_lst_KRS_PerfilesCabecera = obj_KairosProEntities.Database.SqlQuery(_str_SQL, _arr_Parametros).ToList(); #region Rellenar tablas Relacionadas if (g_lst_KRS_PerfilesCabecera != null && g_lst_KRS_PerfilesCabecera.Count > 0) { //Si tiene que buscar alguna tabla relacionada if (_bol_Cargar_PerfilesLineas) { #region PRIMERO BUSCO LOS DATOS DE LAS TABLAS RELACIONADAS //reiniciar Valores de los hilos for (int int_Contador = 0; int_Contador < g_int_TotalHilos_Modelo_Listado; int_Contador++) { g_bol_Hilos_Terminados_Modelo_Listado[int_Contador] = false; } //KRS_PerfilesLinea ############################################# if (_bol_Cargar_PerfilesLineas) { ThreadStart obj_ThreadStart_PERFILESLINEAS = new ThreadStart(Obtener_PerfilesLineas_Modelo_Listado); Thread obj_Thread_PERFILESLINEAS = new Thread(obj_ThreadStart_PERFILESLINEAS); obj_Thread_PERFILESLINEAS.Start(); } else { g_bol_Hilos_Terminados_Modelo_Listado[0] = true; } //Esperando a que terminen todos los hilos ......................................... bool bol_Termino = false; while (bol_Termino == false) { bol_Termino = true; foreach (bool bol_Termino_Hilo in g_bol_Hilos_Terminados_Modelo_Listado) { if (bol_Termino_Hilo == false) { bol_Termino = false; break; } } Thread.Sleep(10); } //................................................................................. #endregion #region DESPUES SE LO VOY ASIGNANDO A LOS REGISTRO UNO A UNO foreach (KRS_PerfilesCabecera mdl_KRS_PerfilesCabecera in g_lst_KRS_PerfilesCabecera) { //KRS_PerfilesLinea ############################################# if (_bol_Cargar_PerfilesLineas && g_lst_KRS_PerfilesLinea != null && g_lst_KRS_PerfilesLinea.Count > 0) { mdl_KRS_PerfilesCabecera.KRS_PerfilesLinea = g_lst_KRS_PerfilesLinea.Where(m => m.idPerfilCabecera == mdl_KRS_PerfilesCabecera.id).ToList(); } } #endregion } } #endregion } catch (Control_Errores) { throw; } catch (Exception ex) { throw new Control_Errores("", ex, nameof(KRS_PerfilesCabecera_Datos) + "/" + nameof(Ejecutar_Consulta_Modelo_Listado), true); } finally { obj_KairosProEntities.Database.Connection.Close(); obj_KairosProEntities.Dispose(); } } #region Tablas relacionadas private void Obtener_PerfilesLineas_Modelo_Listado() { try { KRS_PerfilesLinea_Datos obj_KRS_PerfilesLinea_Datos = new KRS_PerfilesLinea_Datos(); g_lst_KRS_PerfilesLinea = obj_KRS_PerfilesLinea_Datos.Obtener_ModeloListado_Por_PerfilesCabecerasEjercicio(g_lst_KRS_PerfilesCabecera.Select(m => m.id).ToList(), g_mdl_GEN_EjerciciosActivos_Empresa_ModeloListado, true); } catch (Control_Errores) { } finally { g_bol_Hilos_Terminados_Modelo_Listado[0] = true; } } #endregion #endregion #endregion private string Obtener_Consulta_Modelo() { return " SELECT " + nameof(KRS_PerfilesCabecera) + "." + nameof(KRS_PerfilesCabecera.id) + " , " + nameof(KRS_PerfilesCabecera) + "." + nameof(KRS_PerfilesCabecera.codigoCliente) + " , " + nameof(KRS_PerfilesCabecera) + "." + nameof(KRS_PerfilesCabecera.idEjercicio) + " , " + nameof(KRS_PerfilesCabecera) + "." + nameof(KRS_PerfilesCabecera.codigo) + " , " + nameof(KRS_PerfilesCabecera) + "." + nameof(KRS_PerfilesCabecera.descripcion) + " , " + nameof(KRS_PerfilesCabecera) + "." + nameof(KRS_PerfilesCabecera.fechaBorrado) + " FROM " + nameof(KRS_PerfilesCabecera); } #endregion #region Grabar public override INTERNO_ValorDevuelto_Modelo Grabar(object _mdl_obj) { KairosProEntities obj_KairosProEntities = null; INTERNO_ValorDevuelto_Modelo mdl_INTERNO_ValorDevuelto_Modelo = new INTERNO_ValorDevuelto_Modelo(); mdl_INTERNO_ValorDevuelto_Modelo.TodoCorrecto = true; try { obj_KairosProEntities = new KairosProEntities(); obj_KairosProEntities.Configuration.LazyLoadingEnabled = false; KRS_PerfilesCabecera mdl_KRS_PerfilesCabecera_AGrabar = (KRS_PerfilesCabecera)_mdl_obj; KRS_PerfilesCabecera mdl_KRS_PerfilesCabecera_DatosViejos = new KRS_PerfilesCabecera(); KRS_PerfilesCabecera mdl_KRS_PerfilesCabecera_Final = Obtener_Modelo_Por_Id(mdl_KRS_PerfilesCabecera_AGrabar.id, Variables.G_LNG_IDCLIENTE_LICENCIAVALIDADO); if (mdl_KRS_PerfilesCabecera_Final != null) { //Tipo de Accion que se va a realizar mdl_INTERNO_ValorDevuelto_Modelo.TipoAccion = G_ENUM_TIPOACCION.INT_MODIFICAR; //Validado el codigo del cliente, se lo asignamos antes de grabar mdl_KRS_PerfilesCabecera_AGrabar.codigoCliente = Variables.G_LNG_IDCLIENTE_LICENCIAVALIDADO; //Datos viejos para la trazabilidad Funciones.CopiarPropiedadesObjetos(mdl_KRS_PerfilesCabecera_Final, mdl_KRS_PerfilesCabecera_DatosViejos, false, false); //Cargar los Datos del modelo Funciones.CopiarPropiedadesObjetos(mdl_KRS_PerfilesCabecera_AGrabar, mdl_KRS_PerfilesCabecera_Final, false, false); #region Update string str_SQL_Update = " UPDATE " + nameof(KRS_PerfilesCabecera) + " SET " + nameof(KRS_PerfilesCabecera.codigoCliente) + "=@codigoCliente," + nameof(KRS_PerfilesCabecera.idEjercicio) + "=@idEjercicio," + nameof(KRS_PerfilesCabecera.codigo) + "=@codigo," + nameof(KRS_PerfilesCabecera.descripcion) + "=@descripcion," + nameof(KRS_PerfilesCabecera.fechaBorrado) + "=@fechaBorrado" + " WHERE " + nameof(KRS_PerfilesCabecera.id) + "=@id"; //Parametros object[] arr_Parametros_Update = new object[] { new SqlParameter("@codigoCliente", mdl_KRS_PerfilesCabecera_Final.codigoCliente), new SqlParameter("@idEjercicio", mdl_KRS_PerfilesCabecera_Final.idEjercicio), new SqlParameter("@codigo", mdl_KRS_PerfilesCabecera_Final.codigo), new SqlParameter("@descripcion", mdl_KRS_PerfilesCabecera_Final.descripcion), mdl_KRS_PerfilesCabecera_Final.fechaBorrado != null ? new SqlParameter("@fechaBorrado", mdl_KRS_PerfilesCabecera_Final.fechaBorrado.Value) : new SqlParameter("@fechaBorrado", DBNull.Value), new SqlParameter("@id", mdl_KRS_PerfilesCabecera_Final.id) }; #endregion //Update obj_KairosProEntities.Database.ExecuteSqlCommand(str_SQL_Update, arr_Parametros_Update); //Id de la tabla mdl_INTERNO_ValorDevuelto_Modelo.Id = mdl_KRS_PerfilesCabecera_Final.id; } else { mdl_KRS_PerfilesCabecera_Final = new KRS_PerfilesCabecera(); //Tipo de Accion que se va a realizar mdl_INTERNO_ValorDevuelto_Modelo.TipoAccion = G_ENUM_TIPOACCION.INT_INSERTAR; //Validado el codigo del cliente, se lo asignamos antes de grabar mdl_KRS_PerfilesCabecera_AGrabar.codigoCliente = Variables.G_LNG_IDCLIENTE_LICENCIAVALIDADO; //Cargar los Datos del modelo Funciones.CopiarPropiedadesObjetos(mdl_KRS_PerfilesCabecera_AGrabar, mdl_KRS_PerfilesCabecera_Final, false, false); #region INSERT string str_SQL_Insert = " INSERT INTO " + nameof(KRS_PerfilesCabecera) + " (" + nameof(KRS_PerfilesCabecera.codigoCliente) + "," + nameof(KRS_PerfilesCabecera.idEjercicio) + "," + nameof(KRS_PerfilesCabecera.codigo) + "," + nameof(KRS_PerfilesCabecera.descripcion) + "," + nameof(KRS_PerfilesCabecera.fechaBorrado) + " ) " + " OUTPUT INSERTED." + nameof(KRS_PerfilesCabecera.id) + " VALUES " + " ( " + "@codigoCliente," + "@idEjercicio," + "@codigo," + "@descripcion," + "@fechaBorrado" + " ) "; //Parametros object[] arr_Parametros_Insert = new object[] { new SqlParameter("@codigoCliente", mdl_KRS_PerfilesCabecera_Final.codigoCliente), new SqlParameter("@idEjercicio", mdl_KRS_PerfilesCabecera_Final.idEjercicio), new SqlParameter("@codigo", mdl_KRS_PerfilesCabecera_Final.codigo), new SqlParameter("@descripcion", mdl_KRS_PerfilesCabecera_Final.descripcion), mdl_KRS_PerfilesCabecera_Final.fechaBorrado != null ? new SqlParameter("@fechaBorrado", mdl_KRS_PerfilesCabecera_Final.fechaBorrado.Value) : new SqlParameter("@fechaBorrado", DBNull.Value) }; #endregion //Id de la tabla mdl_INTERNO_ValorDevuelto_Modelo.Id = obj_KairosProEntities.Database.SqlQuery(str_SQL_Insert, arr_Parametros_Insert).Single(); } // Grabar un registro de trazabilidad con los datos de la transaccion realizada. USR_Trazabilidad_Datos.Grabar_Trazabilidad(typeof(KRS_PerfilesCabecera), mdl_KRS_PerfilesCabecera_DatosViejos, mdl_KRS_PerfilesCabecera_Final, mdl_INTERNO_ValorDevuelto_Modelo.TipoAccion, obj_KairosProEntities); } catch (Control_Errores) { throw; } catch (Exception ex) { throw new Control_Errores("", ex, nameof(KRS_PerfilesCabecera_Datos) + "/" + nameof(Grabar), true); } finally { //Finalizar transaccion obj_KairosProEntities.Database.Connection.Close(); obj_KairosProEntities.Dispose(); } return mdl_INTERNO_ValorDevuelto_Modelo; } #endregion #region Borrar public override INTERNO_ValorDevuelto_Modelo Borrar(long _lng_id) { KairosProEntities obj_KairosProEntities = null; INTERNO_ValorDevuelto_Modelo mdl_INTERNO_ValorDevuelto_Modelo = new INTERNO_ValorDevuelto_Modelo(); mdl_INTERNO_ValorDevuelto_Modelo.TodoCorrecto = true; //Tipo de Accion que se va a realizar mdl_INTERNO_ValorDevuelto_Modelo.TipoAccion = G_ENUM_TIPOACCION.INT_ELIMINAR; bool bol_ActualizarFechaBorrado = false;//Este campo se utiliza por si no se puede borrar, que actualice la fecha de borrado // Se utiliza la transaccion por la llamada al procedimiento de almacenado using (TransactionScope transaccion_SQL = new TransactionScope(TransactionScopeOption.Required)) { try { //Primer Intento de Borrado obj_KairosProEntities = new KairosProEntities(); obj_KairosProEntities.Configuration.LazyLoadingEnabled = false; // Ver si existe ###################################################################### KRS_PerfilesCabecera mdl_KRS_PerfilesCabecera = Obtener_Modelo_Por_Id(_lng_id, Variables.G_LNG_IDCLIENTE_LICENCIAVALIDADO); //###################################################################################### if (mdl_KRS_PerfilesCabecera != null) { //Se intenta borrar, si diera un error porque esta relacaionado se graba la fecha de baja try { #region Delete string str_SQL_Delete = " DELETE FROM " + nameof(KRS_PerfilesCabecera) + " WHERE " + nameof(KRS_PerfilesCabecera.id) + "=@id and " + nameof(KRS_PerfilesCabecera.codigoCliente) + "=@codigoCliente "; //Parametros object[] arr_Parametros_Delete = new object[] { new SqlParameter("@id", _lng_id), new SqlParameter("@codigoCliente", Variables.G_LNG_IDCLIENTE_LICENCIAVALIDADO) }; #endregion //Delete obj_KairosProEntities.Database.ExecuteSqlCommand(str_SQL_Delete, arr_Parametros_Delete); // Grabar un registro de trazabilidad con los datos de la transaccion realizada. USR_Trazabilidad_Datos.Grabar_Trazabilidad(typeof(KRS_PerfilesCabecera), null, mdl_KRS_PerfilesCabecera, G_ENUM_TIPOACCION.INT_ELIMINAR, obj_KairosProEntities); } catch (Exception ex) { var sqlException = ex.GetBaseException() as SqlException; if (sqlException != null) { if (sqlException.Errors.Count > 0) { switch (sqlException.Errors[0].Number) { case Control_Errores.G_INT_ERRORCONTROLADO_BORRAR_CONFLICTO_CLAVEFORANEA: // Foreign Key violation bol_ActualizarFechaBorrado = true; break; default: throw; } } } else { throw; } } } // Actualizar la fecha de baja , al no poder borrarlo por tener relaciones if (bol_ActualizarFechaBorrado) { #region Update string str_SQL_Update = " UPDATE " + nameof(KRS_PerfilesCabecera) + " SET " + nameof(KRS_PerfilesCabecera.fechaBorrado) + "=@fechaBorrado " + " WHERE " + nameof(KRS_PerfilesCabecera.id) + "=@id and " + nameof(KRS_PerfilesCabecera.codigoCliente) + "=@codigoCliente "; //Parametros object[] arr_Parametros_Update = new object[] { new SqlParameter("@fechaBorrado", DateTime.Now), new SqlParameter("@id", _lng_id) }; #endregion //Update obj_KairosProEntities.Database.ExecuteSqlCommand(str_SQL_Update, arr_Parametros_Update); // Grabar un registro de trazabilidad con los datos de la transaccion realizada. USR_Trazabilidad_Datos.Grabar_Trazabilidad(typeof(KRS_PerfilesCabecera), null, mdl_KRS_PerfilesCabecera, G_ENUM_TIPOACCION.INT_ELIMINAR, obj_KairosProEntities); } // Devolver también el Objeto mdl_INTERNO_ValorDevuelto_Modelo.Id = _lng_id; // Se termina la Transaccion Correctamente transaccion_SQL.Complete(); } catch (Control_Errores) { throw; } catch (Exception ex) { throw new Control_Errores("", ex, nameof(KRS_PerfilesCabecera_Datos) + "/" + nameof(Borrar), true); } finally { obj_KairosProEntities.Database.Connection.Close(); obj_KairosProEntities.Dispose(); // Finalizar transaccion transaccion_SQL.Dispose(); } } return mdl_INTERNO_ValorDevuelto_Modelo; } public override INTERNO_ValorDevuelto_Modelo Borrar(object _mdl_obj) { throw new NotImplementedException(); } public override INTERNO_ValorDevuelto_Modelo Borrar(long _lng_id, long _lng_idAuxiliar) { throw new NotImplementedException(); } #endregion } }