public ClientResponse DarDebajaEnviocorreos(GestionCorreo objeto)
 {
     try
     {
         using (conexion = new SqlConnection(ConexionDAO.cnx))
         {
             using (comando = new SqlCommand("sp_upd_darjar_envio_correo", conexion))
             {
                 comando.Parameters.AddWithValue("@tokens", objeto.Tokens);
                 comando.CommandType = CommandType.StoredProcedure;
                 conexion.Open();
                 comando.ExecuteNonQuery();
                 clientResponse.Mensaje = "Se desafilio el envio de correos  satisfactoriamente";
                 clientResponse.Status  = "OK";
             }
         }
     }
     catch (Exception ex)
     {
         clientResponse.Mensaje = ex.Message;
         clientResponse.Status  = "ERROR";
     }
     finally
     {
         conexion.Close();
         conexion.Dispose();
         comando.Dispose();
     }
     return(clientResponse);
 }
 public ClientResponse EliminarGestionCorreo(GestionCorreo objeto)
 {
     try
     {
         using (conexion = new SqlConnection(ConexionDAO.cnx))
         {
             using (comando = new SqlCommand("usp_del_gestioncorreo", conexion))
             {
                 comando.Parameters.AddWithValue("@id", objeto.id);
                 comando.Parameters.AddWithValue("@estado", objeto.id_estado);
                 comando.CommandType = CommandType.StoredProcedure;
                 conexion.Open();
                 comando.ExecuteNonQuery();
                 clientResponse.Mensaje = "Se elimino satisfactoriamente el registro";
                 clientResponse.Status  = "OK";
             }
         }
     }
     catch (Exception ex)
     {
         clientResponse.Mensaje = ex.Message;
         clientResponse.Status  = "ERROR";
     }
     finally
     {
         conexion.Close();
         conexion.Dispose();
         comando.Dispose();
     }
     return(clientResponse);
 }
        public GestionCorreoDAO()
        {
            listgestioncorreo = new List <GestionCorreo>();

            entidad               = null;
            reader                = null;
            clientResponse        = new ClientResponse();
            clientResponse.Status = "OK";
        }
        public ClientResponse getExportarCorreos(int id_cbo_origenfil, int id_cbo_grupo_consultar)
        {
            try
            {
                using (conexion = new SqlConnection(ConexionDAO.cnx))
                {
                    using (comando = new SqlCommand("usp_sel_gestioncorreo_exportar", conexion))
                    {
                        comando.Parameters.AddWithValue("@id_cbo_origenfil", id_cbo_origenfil);
                        comando.Parameters.AddWithValue("@id_grupo_correo", id_cbo_grupo_consultar);
                        comando.CommandType = CommandType.StoredProcedure;
                        conexion.Open();
                        using (reader = comando.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                entidad            = new GestionCorreo();
                                entidad.id         = Convert.ToInt32(reader["id"] == DBNull.Value ? 0 : reader["id"]);
                                entidad.Nombre1    = Convert.ToString(reader["Nombre1"] == DBNull.Value ? "" : reader["Nombre1"]);
                                entidad.Nombre2    = Convert.ToString(reader["Nombre2"] == DBNull.Value ? "" : reader["Nombre2"]);
                                entidad.ApePaterno = Convert.ToString(reader["apePaterno"] == DBNull.Value ? "" : reader["apePaterno"]);
                                entidad.ApeMaterno = Convert.ToString(reader["apeMaterno"] == DBNull.Value ? "" : reader["apeMaterno"]);
                                entidad.Email      = Convert.ToString(reader["email"] == DBNull.Value ? "" : reader["email"]);

                                entidad.id_estado  = Convert.ToInt32(reader["id_estado"] == DBNull.Value ? 0 : reader["id_estado"]);
                                entidad.descestado = Convert.ToString(reader["desestado"] == DBNull.Value ? "" : reader["desestado"]);
                                GrupoCorreo grupocorreo = new GrupoCorreo();
                                grupocorreo.id          = Convert.ToInt32(reader["id_grupo_correo"] == DBNull.Value ? 0 : reader["id_grupo_correo"]);
                                grupocorreo.descripcion = Convert.ToString(reader["descgrupo_correo"] == DBNull.Value ? "" : reader["descgrupo_correo"]);
                                entidad.grupocorreo     = grupocorreo;
                                entidad.fechabaja       = Convert.ToString(reader["fechabaja"] == DBNull.Value ? "" : reader["fechabaja"]);
                                listgestioncorreo.Add(entidad);
                            }
                        }
                        clientResponse.DataJson = JsonConvert.SerializeObject(listgestioncorreo).ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                clientResponse.Mensaje = ex.Message;
                clientResponse.Status  = "ERROR";
            }
            finally
            {
                conexion.Close();
                conexion.Dispose();
                comando.Dispose();
                reader.Dispose();
            }

            return(clientResponse);
        }
        public static ClientResponse InsertGestionCorreo(GestionCorreo objeto)
        {
            ClientResponse response;

            try
            {
                using (GestionCorreoDAO dbGestionCorreo = new GestionCorreoDAO())
                {
                    response = dbGestionCorreo.InsertGestionCorreo(objeto);
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return(response);
        }
        public ClientResponse ActualiarGestionCorreo(GestionCorreo objeto)
        {
            try
            {
                using (conexion = new SqlConnection(ConexionDAO.cnx))
                {
                    using (comando = new SqlCommand("usp_upd_gestioncorreo", conexion))
                    {
                        comando.Parameters.AddWithValue("@id", objeto.id);
                        comando.Parameters.AddWithValue("@id_grupo_correo", objeto.grupocorreo.id);
                        comando.Parameters.AddWithValue("@Nombre1", objeto.Nombre1);
                        comando.Parameters.AddWithValue("@Nombre2", objeto.Nombre2);
                        comando.Parameters.AddWithValue("@apePaterno", objeto.ApePaterno);
                        comando.Parameters.AddWithValue("@apeMaterno", objeto.ApeMaterno);
                        comando.Parameters.AddWithValue("@email", objeto.Email);
                        comando.Parameters.AddWithValue("@id_estado", objeto.id_estado);
                        comando.Parameters.AddWithValue("@UsuarioActualizacion", objeto.UsuarioModificacion);


                        comando.CommandType = CommandType.StoredProcedure;
                        conexion.Open();
                        comando.ExecuteNonQuery();
                        clientResponse.Mensaje = "Se actualizo correo satisfactoriamente";
                        clientResponse.Status  = "OK";
                    }
                }
            }
            catch (Exception ex)
            {
                clientResponse.Mensaje = ex.Message;
                clientResponse.Status  = "ERROR";
            }
            finally
            {
                conexion.Close();
                conexion.Dispose();
                comando.Dispose();
            }
            return(clientResponse);
        }
        public static ClientResponse dardeBaja()
        {
            ClientResponse response;

            try
            {
                GestionCorreo objeto = new GestionCorreo()
                {
                    Tokens = tokens
                };

                using (GestionCorreoDAO dbGestionCorreo = new GestionCorreoDAO())
                {
                    response = dbGestionCorreo.DarDebajaEnviocorreos(objeto);
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return(response);
        }
        public ClientResponse getLstGestionCorreo(int paginaActual, int RegistroXPagina, int id_cbo_origenfil, int id_cbo_grupo_consultar)
        {
            try
            {
                int recordCount = 0;
                using (conexion = new SqlConnection(ConexionDAO.cnx))
                {
                    using (comando = new SqlCommand("usp_sel_gestioncorreo", conexion))
                    {
                        comando.Parameters.AddWithValue("@id_cbo_origenfil", id_cbo_origenfil);
                        comando.Parameters.AddWithValue("@id_grupo_correo", id_cbo_grupo_consultar);
                        comando.Parameters.AddWithValue("@vi_Pagina", paginaActual);
                        comando.Parameters.AddWithValue("@vi_RegistrosporPagina", RegistroXPagina);
                        comando.Parameters.Add("@vi_RecordCount", SqlDbType.Int).Direction = ParameterDirection.Output;
                        comando.CommandType = CommandType.StoredProcedure;
                        conexion.Open();
                        using (reader = comando.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                entidad            = new GestionCorreo();
                                entidad.id         = Convert.ToInt32(reader["id"] == DBNull.Value ? 0 : reader["id"]);
                                entidad.Nombre1    = Convert.ToString(reader["Nombre1"] == DBNull.Value ? "" : reader["Nombre1"]);
                                entidad.Nombre2    = Convert.ToString(reader["Nombre2"] == DBNull.Value ? "" : reader["Nombre2"]);
                                entidad.ApePaterno = Convert.ToString(reader["apePaterno"] == DBNull.Value ? "" : reader["apePaterno"]);
                                entidad.ApeMaterno = Convert.ToString(reader["apeMaterno"] == DBNull.Value ? "" : reader["apeMaterno"]);
                                entidad.Email      = Convert.ToString(reader["email"] == DBNull.Value ? "" : reader["email"]);

                                entidad.id_estado  = Convert.ToInt32(reader["id_estado"] == DBNull.Value ? 0 : reader["id_estado"]);
                                entidad.descestado = Convert.ToString(reader["desestado"] == DBNull.Value ? "" : reader["desestado"]);
                                GrupoCorreo grupocorreo = new GrupoCorreo();
                                grupocorreo.id          = Convert.ToInt32(reader["id_grupo_correo"] == DBNull.Value ? 0 : reader["id_grupo_correo"]);
                                grupocorreo.descripcion = Convert.ToString(reader["descgrupo_correo"] == DBNull.Value ? "" : reader["descgrupo_correo"]);
                                entidad.grupocorreo     = grupocorreo;
                                entidad.fechabaja       = Convert.ToString(reader["fechabaja"] == DBNull.Value ? "" : reader["fechabaja"]);
                                listgestioncorreo.Add(entidad);
                            }
                        }

                        recordCount = Convert.ToInt32(comando.Parameters["@vi_RecordCount"].Value);
                        Pagination responsepaginacion = new Pagination()
                        {
                            TotalItems = recordCount,
                            TotalPages = (int)Math.Ceiling((double)recordCount / 10)
                        };
                        clientResponse.paginacion = JsonConvert.SerializeObject(responsepaginacion).ToString();
                        clientResponse.DataJson   = JsonConvert.SerializeObject(listgestioncorreo).ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                clientResponse.Mensaje = ex.Message;
                clientResponse.Status  = "ERROR";
            }
            finally
            {
                conexion.Close();
                conexion.Dispose();
                comando.Dispose();
                reader.Dispose();
            }

            return(clientResponse);
        }
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Expires     = -1;

            try
            {
                string         hora = DateTime.Now.ToString("yyyyMMddhhmmss");
                ClientResponse responseruta;
                using (ParametrosMaestrosDAO dbParametrosMaestro = new ParametrosMaestrosDAO())
                {
                    responseruta = dbParametrosMaestro.getObjParametroMaestro("RUTA_CARGAEXCELCORREOS");
                }
                ParametrosMaestros rutaexcel  = Newtonsoft.Json.JsonConvert.DeserializeObject <ParametrosMaestros>(responseruta.DataJson);
                HttpPostedFile     postedFile = context.Request.Files["Filedata"];
                listgestioncorreo = new List <GestionCorreo>();

                string savepath = "";
                string tempPath = "";

                tempPath = rutaexcel.valor + hora;
                savepath = tempPath;
                string filename = postedFile.FileName;

                if (!Directory.Exists(savepath))
                {
                    Directory.CreateDirectory(savepath);
                }
                string files = savepath + "/" + filename;
                postedFile.SaveAs(files);

                Excel.Application xlApp;
                Excel.Workbook    xlWorkBook;
                Excel.Worksheet   xlWorkSheet;
                Excel.Range       range;

                string str;
                int    rCnt;
                int    cCnt;
                int    rw = 0;
                int    cl = 0;

                xlApp       = new Excel.Application();
                xlWorkBook  = xlApp.Workbooks.Open(files, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
                xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

                range = xlWorkSheet.UsedRange;
                rw    = range.Rows.Count;
                cl    = 6;// range.Columns.Count; Leemos solo 6 columnas de cada fila

                ClientResponse response;
                using (GrupoCorreoDAO dbGrupoCorreo = new GrupoCorreoDAO())
                {
                    response = dbGrupoCorreo.getGrupoCorreoCombo(2);
                }

                List <GrupoCorreo> list = JsonConvert.DeserializeObject <List <GrupoCorreo> >(response.DataJson);

                for (rCnt = 1; rCnt <= rw; rCnt++)
                {
                    if (rCnt > 1)
                    {
                        entidad = new GestionCorreo();
                        for (cCnt = 1; cCnt <= cl; cCnt++)
                        {
                            if (cCnt == 1)
                            {
                                GrupoCorreo grupocorreo = new GrupoCorreo();
                                grupocorreo.descripcion = str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
                                entidad.grupocorreo     = grupocorreo;
                                //Validamos si el grupo de correo esta registrado en la base de datos
                                grupocorreo.origen = 2;//Carga de Correo externo
                                grupocorreo.estado = 1;
                                GrupoCorreo objeto = list.Where(i => i.descripcion.ToUpper() == grupocorreo.descripcion.ToUpper()).FirstOrDefault();
                                if (objeto != null)
                                {
                                    grupocorreo.id = objeto.id;
                                }
                            }
                            if (cCnt == 2)
                            {
                                entidad.Nombre1 = str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
                            }
                            if (cCnt == 3)
                            {
                                entidad.Nombre2 = str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
                            }
                            if (cCnt == 4)
                            {
                                entidad.ApePaterno = str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
                            }
                            if (cCnt == 5)
                            {
                                entidad.ApeMaterno = str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
                            }
                            if (cCnt == 6)
                            {
                                entidad.Email = str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
                            }
                        }
                        entidad.id_estado = 1;
                        listgestioncorreo.Add(entidad);
                    }
                }

                xlWorkBook.Close(true, null, null);
                xlApp.Quit();

                Marshal.ReleaseComObject(xlWorkSheet);
                Marshal.ReleaseComObject(xlWorkBook);
                Marshal.ReleaseComObject(xlApp);



                if ((System.IO.File.Exists(files)))
                {
                    System.IO.File.Delete(files);
                }

                if (listgestioncorreo.Count() > 0)
                {
                    ClientResponse response1;
                    using (GestionCorreoDAO dbGestionCorreo = new GestionCorreoDAO())
                    {
                        response1 = dbGestionCorreo.InsertGestionCorreoAutomatico(listgestioncorreo);
                    }
                }


                var wapper = new
                {
                    Result  = "Ok",
                    Mensaje = "Se cargaron correctaente la trama."
                };
                context.Response.Write(JsonConvert.SerializeObject(wapper));
            }

            catch (Exception ex)

            {
                var wapper = new
                {
                    Result  = "Error",
                    Mensaje = ex.Message
                };
                context.Response.Write(JsonConvert.SerializeObject(wapper));
            }
        }