using Exferia_Aplicacion.General;
using Exferia_KairosPRO_EF;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Exferia_KairosPRO._2_Datos
{
    public class TMP_Emails_PausasIncidencias_Datos
    {
        public List<TMP_Emails_PausasIncidencias> Obtener_Listado(long _lng_CodigoCliente)
        {
            KairosProEntities obj_KairosProEntities = null;
            List<TMP_Emails_PausasIncidencias> lst_TMP_Emails_PausasIncidencias = null;

            try
            {
                obj_KairosProEntities = new KairosProEntities();
                obj_KairosProEntities.Configuration.LazyLoadingEnabled = false;

                string str_SQL =
                " SELECT distinct " +
                        nameof(TMP_Emails_PausasIncidencias) + "." + nameof(TMP_Emails_PausasIncidencias.id) + "," +
                        nameof(TMP_Emails_PausasIncidencias) + "." + nameof(TMP_Emails_PausasIncidencias.codigoCliente) + "," +
                        nameof(TMP_Emails_PausasIncidencias) + "." + nameof(TMP_Emails_PausasIncidencias.idEmpleado) + "," +
                        nameof(TMP_Emails_PausasIncidencias) + "." + nameof(TMP_Emails_PausasIncidencias.idPausa) + "," +
                        nameof(TMP_Emails_PausasIncidencias) + "." + nameof(TMP_Emails_PausasIncidencias.idJornadaCabecera) + "," +
                        nameof(TMP_Emails_PausasIncidencias) + "." + nameof(TMP_Emails_PausasIncidencias.idJornadaPausa) + "," +
                        nameof(TMP_Emails_PausasIncidencias) + "." + nameof(TMP_Emails_PausasIncidencias.incidencia_Tipo) + "," +
                        nameof(TMP_Emails_PausasIncidencias) + "." + nameof(TMP_Emails_PausasIncidencias.incidencia_descripcion) + "," +
                        nameof(TMP_Emails_PausasIncidencias) + "." + nameof(TMP_Emails_PausasIncidencias.fecha) + "," +
                        nameof(TMP_Emails_PausasIncidencias) + "." + nameof(TMP_Emails_PausasIncidencias.entradaSalida) + "," +
                        nameof(TMP_Emails_PausasIncidencias) + "." + nameof(TMP_Emails_PausasIncidencias.duracionPausa) + "," +
                        nameof(TMP_Emails_PausasIncidencias) + "." + nameof(TMP_Emails_PausasIncidencias.numeroPausasRealizadas) +
                " FROM " + nameof(TMP_Emails_PausasIncidencias) +
                " WHERE " + 
                            nameof(TMP_Emails_PausasIncidencias) + "." + nameof(TMP_Emails_PausasIncidencias.codigoCliente) + "=@codigoCliente " +
                " ORDER BY " +
                    nameof(TMP_Emails_PausasIncidencias.id) + " ASC ";

                //Parametros
                object[] arr_Parametros = new object[]
                {
                     new SqlParameter("@codigoCliente",  _lng_CodigoCliente)
                };

                //Rellenar el Modelo
                lst_TMP_Emails_PausasIncidencias = obj_KairosProEntities.Database.SqlQuery<TMP_Emails_PausasIncidencias>(str_SQL, arr_Parametros).ToList();
            }
            catch (Control_Errores)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new Control_Errores("", ex, nameof(TMP_Emails_PausasIncidencias_Datos) + "/" + nameof(Obtener_Listado), true);
            }
            finally
            {
                obj_KairosProEntities.Database.Connection.Close();
                obj_KairosProEntities.Dispose();
            }

            return lst_TMP_Emails_PausasIncidencias;
        }

        public void Insertar(TMP_Emails_PausasIncidencias _mdl_TMP_Emails_PausasIncidencias)
        {
            KairosProEntities obj_KairosProEntities = null;

            try
            {
                obj_KairosProEntities = new KairosProEntities();
                obj_KairosProEntities.Configuration.LazyLoadingEnabled = false;

                string str_SQL_Insert = " INSERT INTO " + nameof(TMP_Emails_PausasIncidencias) +
                                               " (" +
                                                        nameof(TMP_Emails_PausasIncidencias.codigoCliente) + "," +
                                                        nameof(TMP_Emails_PausasIncidencias.idEmpleado) + "," +
                                                        nameof(TMP_Emails_PausasIncidencias.idPausa) + "," +
                                                        nameof(TMP_Emails_PausasIncidencias.idJornadaCabecera) + "," +
                                                        nameof(TMP_Emails_PausasIncidencias.idJornadaPausa) + "," +
                                                        nameof(TMP_Emails_PausasIncidencias.incidencia_Tipo) + "," +
                                                        nameof(TMP_Emails_PausasIncidencias.incidencia_descripcion) + "," +
                                                        nameof(TMP_Emails_PausasIncidencias.fecha) + "," +
                                                        nameof(TMP_Emails_PausasIncidencias.entradaSalida) + "," +
                                                        nameof(TMP_Emails_PausasIncidencias.duracionPausa) + "," +
                                                        nameof(TMP_Emails_PausasIncidencias.numeroPausasRealizadas) + 
                                               " ) " +
                                               " OUTPUT INSERTED." + nameof(TMP_Emails_PausasIncidencias.id) +
                                               " VALUES " +
                                               " ( " +
                                                    "@codigoCliente," +
                                                    "@idEmpleado," +
                                                    "@idPausa," +
                                                    "@idJornadaCabecera," +
                                                    "@idJornadaPausa," +
                                                    "@incidencia_Tipo," +
                                                    "@incidencia_descripcion," +
                                                    "@fecha," +
                                                    "@entradaSalida," +
                                                    "@duracionPausa," +
                                                    "@numeroPausasRealizadas" +
                                               " ) ";
                //Parametros
                object[] arr_Parametros_Insert = new object[]
                {
                    new SqlParameter("@codigoCliente",  _mdl_TMP_Emails_PausasIncidencias.codigoCliente),
                    new SqlParameter("@idEmpleado",  _mdl_TMP_Emails_PausasIncidencias.idEmpleado),
                    _mdl_TMP_Emails_PausasIncidencias.idPausa != null
                        ?   new SqlParameter("@idPausa", _mdl_TMP_Emails_PausasIncidencias.idPausa.Value)
                        :   new SqlParameter("@idPausa",  DBNull.Value),
                    _mdl_TMP_Emails_PausasIncidencias.idJornadaCabecera != null
                        ?   new SqlParameter("@idJornadaCabecera", _mdl_TMP_Emails_PausasIncidencias.idJornadaCabecera.Value)
                        :   new SqlParameter("@idJornadaCabecera",  DBNull.Value),
                        _mdl_TMP_Emails_PausasIncidencias.idJornadaPausa != null
                        ?   new SqlParameter("@idJornadaPausa", _mdl_TMP_Emails_PausasIncidencias.idJornadaPausa.Value)
                        :   new SqlParameter("@idJornadaPausa",  DBNull.Value),
                    new SqlParameter("@incidencia_Tipo",  _mdl_TMP_Emails_PausasIncidencias.incidencia_Tipo),
                    new SqlParameter("@incidencia_descripcion",  _mdl_TMP_Emails_PausasIncidencias.incidencia_descripcion),
                    new SqlParameter("@fecha",  _mdl_TMP_Emails_PausasIncidencias.fecha),
                    new SqlParameter("@entradaSalida",  _mdl_TMP_Emails_PausasIncidencias.entradaSalida),
                    new SqlParameter("@duracionPausa",  _mdl_TMP_Emails_PausasIncidencias.duracionPausa),
                    new SqlParameter("@numeroPausasRealizadas",  _mdl_TMP_Emails_PausasIncidencias.numeroPausasRealizadas)
                };

                //Id de la tabla
                obj_KairosProEntities.Database.SqlQuery<long>(str_SQL_Insert, arr_Parametros_Insert).Single();

            }
            catch (Control_Errores)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new Control_Errores("", ex, nameof(TMP_Emails_PausasIncidencias_Datos) + "/" + nameof(Insertar), true);
            }
            finally
            {
                obj_KairosProEntities.Database.Connection.Close();
                obj_KairosProEntities.Dispose();
            }
        }

        public void Borrar(long _lng_id,long _lng_CodigoCliente)
        {
            KairosProEntities obj_KairosProEntities = null;

            try
            {

                obj_KairosProEntities = new KairosProEntities();
                obj_KairosProEntities.Configuration.LazyLoadingEnabled = false;

                #region Delete
                string str_SQL_Delete = " DELETE FROM  " + nameof(TMP_Emails_PausasIncidencias) +
                                        " WHERE " +
                                            nameof(TMP_Emails_PausasIncidencias.id) + "=@id and " +
                                            nameof(TMP_Emails_PausasIncidencias.codigoCliente) + "=@codigoCliente ";
                //Parametros
                object[] arr_Parametros_Delete = new object[]
                {
                    new SqlParameter("@id", _lng_id),
                    new SqlParameter("@codigoCliente", _lng_CodigoCliente)
                };
                #endregion

                //Delete
                obj_KairosProEntities.Database.ExecuteSqlCommand(str_SQL_Delete, arr_Parametros_Delete);
            }
            catch (Control_Errores)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new Control_Errores("", ex, nameof(TMP_Emails_PausasIncidencias_Datos) + "/" + nameof(Borrar), true);
            }
            finally
            {
                
                obj_KairosProEntities.Database.Connection.Close();
                obj_KairosProEntities.Dispose();
            }
        }

    }
}
