public JsonResult getRepoEnrollDev(getFilter filter)
        {
            var result = from o in dbContext.RepoEnrollDevices
                         select o;

            if (filter.name != null)
            {
                result = result.Where(c => c.Name.Contains(filter.name));
            }

            if (filter.device_id != null)
            {
                result = result.Where(c => c.DevId == filter.device_id);
            }

            if (filter.ciudad != null)
            {
                result = result.Where(c => c.Ciudad.Contains(filter.ciudad));
            }

            if (filter.hasPhoto != null)
            {
                Console.WriteLine("Filtro enrolamiento:" + filter.hasPhoto);
                var hasPhoto = (filter.hasPhoto == "1") ? true : false;
                result = result.Where(c => c.hasPhoto == hasPhoto);
            }

            var count = result.Count();


            return(new JsonResult(new { Count = count, Data = result }));
        }
        public JsonResult getRecoDia(getFilter filter)
        {
            var proc_start_date = new SqlParameter("@start_date", Convert.ToDateTime(filter.start_date));
            var proc_end_date   = new SqlParameter("@end_date", Convert.ToDateTime(filter.end_date));
            var proc_t_alarma   = new SqlParameter("@t_alarma", 37.3);
            var proc_ex_company = new SqlParameter("@ex_company", "QA");

            var result = dbContext.RecoDia
                         .FromSqlRaw("EXEC DISTRIBUCION_EVENTOS @start_date, @end_date, @t_alarma, @ex_company ", proc_start_date, proc_end_date, proc_t_alarma, proc_ex_company)
                         .ToList();


            // var result = from o in dbContext.RecoDia
            //     select o;

            // if (filter.device_id != null)
            // result = result.Where(c => c.DevId == filter.device_id);

            // if (filter.ciudad != null)
            // result = result.Where(c => c.Ciudad.Contains(filter.ciudad));

            // // Console.WriteLine("Start Time: {0} End Time : {1}", DateTime.Parse(filter.start_date).ToString(), DateTime.Parse(filter.end_date).ToString());

            // if (filter.start_date != null)
            // result = result.Where(c => c.time >= DateTime.Parse(filter.start_date));

            // if (filter.end_date != null)
            // result = result.Where(c => c.time <= DateTime.Parse(filter.end_date));

            var count = result.Count();


            return(new JsonResult(new { Count = count, Data = result }));
        }
        public JsonResult getReconocimientos(getFilter filter)
        {
            var result = from o in dbContext.Reconocimientos
                         select o;

            if (filter.device_id != null)
            {
                result = result.Where(c => c.DevId == filter.device_id);
            }

            if (filter.ciudad != null)
            {
                result = result.Where(c => c.Ciudad.Contains(filter.ciudad));
            }

            //Console.WriteLine("Start Time: {0}", DateTime.Parse(filter.start_date).ToString());
            if (filter.start_date != null)
            {
                result = result.Where(c => c.DateTime >= DateTime.Parse(filter.start_date));
            }

            if (filter.end_date != null)
            {
                result = result.Where(c => c.DateTime <= DateTime.Parse(filter.end_date));
            }

            var count = result.Count();


            return(new JsonResult(new { Count = count, Data = result }));
        }
        public JsonResult getSopoRecoPersona(getFilter filter)
        {
            var result = from o in dbContext.SopoRecoPersonas
                         select o;



            if (filter.ciudad != null)
            {
                result = result.Where(c => c.Ciudad.Contains(filter.ciudad));
            }

            if (filter.name != null)
            {
                result = result.Where(c => c.Name.Contains(filter.name));
            }

            if (filter.company != null)
            {
                result = result.Where(c => c.Empresa.Contains(filter.company));
            }

            if (filter.eXcompany != null)
            {
                result = result.Where(c => !c.Empresa.Contains(filter.eXcompany));
            }

            // Console.WriteLine("Start Time: {0} End Time : {1}", DateTime.Parse(filter.start_date).ToString(), DateTime.Parse(filter.end_date).ToString());

            if (filter.start_date != null)
            {
                result = result.Where(c => c.Time >= DateTime.Parse(filter.start_date));
            }

            if (filter.end_date != null)
            {
                result = result.Where(c => c.Time <= DateTime.Parse(filter.end_date));
            }

            var count = result.Count();


            return(new JsonResult(new { Count = count, Data = result }));
        }
        public JsonResult dailyPdf()
        {
            var Renderer = new IronPdf.HtmlToPdf();

            IronPdf.License.LicenseKey         = "IRONPDF-899018AD4E-162958-D0D743-E5E35E6DCF-A6A2B1D8-UEx011F047486B48D8-COMMUNITY.TRIAL.EXPIRES.28.AUG.2020";
            Renderer.PrintOptions.MarginTop    = 20; //millimeters
            Renderer.PrintOptions.MarginBottom = 40;
            Renderer.PrintOptions.CssMediaType = PdfPrintOptions.PdfCssMediaType.Print;

            Renderer.PrintOptions.Header = new SimpleHeaderFooter()
            {
                CenterText      = "{pdf-title}",
                DrawDividerLine = true,
                FontSize        = 16
            };
            Renderer.PrintOptions.Footer = new SimpleHeaderFooter()
            {
                LeftText        = "{date} {time}",
                RightText       = "Pagina {page} de {total-pages}",
                DrawDividerLine = true,
                FontSize        = 14
            };

            var html = System.IO.File.ReadAllText(Path.Combine("wwwroot/Assets", "Informe.html"));
            //Console.WriteLine(html);


            var filter = new getFilter();
            // var today = DateTime.Now.ToString("yyyy-MM-dd 12:00");

            var today     = DateTime.Today;
            var tomorrow  = today.AddDays(1);
            var yesterday = today.AddDays(-1);

            Console.WriteLine("Hoy:" + today.ToString("yyyy-MM-dd 12:00"));
            Console.WriteLine("Ayer:" + yesterday.ToString("yyyy-MM-dd 12:00"));

            filter.start_date = yesterday.ToString("yyyy-MM-dd 12:00");
            filter.end_date   = today.ToString("yyyy-MM-dd 12:00");

            var enrolados = from e in dbContext.RepoEnrolamientos
                            select e;

            var eventosPersona = from e in dbContext.SopoRecoPersonas
                                 select e;

            var eventosRecon = from e in dbContext.SopoEvRecoDias
                               select e;

            var alarmas = eventosRecon.Where(c => c.Temperature >= 37.3);

            var totalPersonas = eventosPersona.Count();
            var totalEventos  = eventosRecon.Count();
            var totalAlarmas  = alarmas.Count();

            var proc_start_date = new SqlParameter("@start_date", Convert.ToDateTime(filter.start_date));
            var proc_end_date   = new SqlParameter("@end_date", Convert.ToDateTime(filter.end_date));
            var proc_t_alarma   = new SqlParameter("@t_alarma", 37.3);

            var result = dbContext.RecoDia
                         .FromSqlRaw("EXEC DISTRIBUCION_EVENTOS @start_date, @end_date, @t_alarma ", proc_start_date, proc_end_date, proc_t_alarma)
                         .ToList();

            var count = result.Count();

            var totalEnrolados = enrolados.Count();

            var enroladosOk = enrolados.Where(c => c.hasPhoto == true);

            var okEnrolados = enroladosOk.Count();

            var enroladosNew = enrolados.Where(c => c.Time >= DateTime.Parse(filter.start_date));

            enroladosNew = enroladosNew.Where(c => c.Time <= DateTime.Parse(filter.end_date));

            var newEnrolados = enroladosNew.Count();

            string htmlTable = "";

            int tEventos  = 0;
            int tPersonas = 0;
            int tAlarmas  = 0;

            foreach (var item in result)
            {
                Console.WriteLine("{0} {1} {2} {3}\n", item.Fecha,
                                  item.Sitio, item.Recos, item.Personas);
                htmlTable += "<tr><td>" + item.Fecha + "</td>" + "<td>" + item.Ciudad + "</td>" + "<td>" + item.Sitio + "</td>" + "<td class='numero'>" + item.Recos + "</td>" + "<td class='numero'>" + item.Personas + "</td>" + "<td class='numero'>" + item.Alertas + "</td></tr>";
                tEventos  += item.Recos;
                tPersonas += item.Personas;
                tAlarmas  += item.Alertas;
            }

            htmlTable += "<tr><td> Totales </td><td></td><td></td><td class='numero'>" + tEventos + "</td>" + "<td class='numero'>" + tPersonas + " ** </td>" + "<td class='numero'>" + tAlarmas + "</td></tr>";
            Console.WriteLine(htmlTable);


            // Cargar los datos
            html = html.Replace("{{start_date}}", filter.start_date.ToString());
            html = html.Replace("{{end_date}}", filter.end_date.ToString());
            html = html.Replace("{{totalDatos}}", totalEnrolados.ToString());
            html = html.Replace("{{totalEnrolados}}", okEnrolados.ToString());
            html = html.Replace("{{nuevosEnrolados}}", newEnrolados.ToString());
            html = html.Replace("{{tablaEventos}}", htmlTable);
            html = html.Replace("{{totalEventos}}", totalEventos.ToString());
            html = html.Replace("{{totalPersonas}}", totalPersonas.ToString());
            html = html.Replace("{{totalAlertas}}", totalAlarmas.ToString());
            html = html.Replace("{{totalEventosP}}", tEventos.ToString());
            html = html.Replace("{{totalPersonasP}}", tPersonas.ToString());
            html = html.Replace("{{totalAlertasP}}", tAlarmas.ToString());


            var PDF        = Renderer.RenderHtmlAsPdf(html);
            var OutputPath = "wwwroot/Reports/ReporteDiario.pdf";

            PDF.SaveAs(OutputPath);

            SendMail(OutputPath, "*****@*****.**");
            SendMail(OutputPath, "*****@*****.**");
            SendMail(OutputPath, "*****@*****.**");
            // This neat trick opens our PDF file so we can see the result in our default PDF viewer
            // System.Diagnostics.Process.Start(OutputPath);
            return(new JsonResult(new { Status = "Success" }));
        }
        public JsonResult getEventos(getFilter filter)
        {
            //var result = dbContext.Persons
            //    .Where(t => t.DevId == filter.device_id);



            var result = from o in dbContext.Persons
                         select o;

            if (filter.name != null)
            {
                result = result.Where(c => c.Name.Contains(filter.name));
            }

            if (filter.device_id != null)
            {
                result = result.Where(c => c.DevId == filter.device_id);
            }

            if (filter.mask != null)
            {
                result = result.Where(c => c.Mask == Int16.Parse(filter.mask));
            }

            if (filter.document != null)
            {
                result = result.Where(c => c.UserId == Int16.Parse(filter.document));
            }

            if (filter.tmin != null)
            {
                result = result.Where(c => c.Temperature >= Double.Parse(filter.tmin));
            }


            if (filter.tmax != null)
            {
                result = result.Where(c => c.Temperature <= Double.Parse(filter.tmax));
            }


            //Console.WriteLine("Start Time: {0}", DateTime.Parse(filter.start_date).ToString());
            if (filter.start_date != null)
            {
                result = result.Where(c => c.RegisterTime >= DateTime.Parse(filter.start_date));
            }

            if (filter.end_date != null)
            {
                result = result.Where(c => c.RegisterTime <= DateTime.Parse(filter.end_date));
            }


            // if (filter.groupBy != null) {
            //     switch (filter.groupBy)
            //     {
            //         case "name":
            //             result = result.GroupBy(n => new { n.Name });
            //             break;
            //         case "device":
            //             result = result.GroupBy(n => new { n.DevId });
            //             break;
            //         default:
            //     }
            // }



            // IQueryable<IGrouping<int, Person>> groups = result.GroupBy(x => x.UserId);

            if (filter.hasId != null)
            {
                result = result.Where(c => c.UserId > 0);
            }

            var count = result.Count();

            return(new JsonResult(new { Count = count, Data = result }));
        }
        public JsonResult getSopoEvRecoDia(getFilter filter)
        {
            var result = from o in dbContext.SopoEvRecoDias
                         select o;

            if (filter.device_id != null)
            {
                result = result.Where(c => c.DevId == filter.device_id);
            }

            if (filter.document != null)
            {
                result = result.Where(c => c.DocId.Contains(filter.document));
            }

            if (filter.company != null)
            {
                result = result.Where(c => c.Empresa.Contains(filter.company));
            }

            if (filter.eXcompany != null)
            {
                result = result.Where(c => !c.Empresa.Contains(filter.eXcompany));
            }

            if (filter.name != null)
            {
                result = result.Where(c => c.Name.Contains(filter.name));
            }

            if (filter.ciudad != null)
            {
                result = result.Where(c => c.Ciudad.Contains(filter.ciudad));
            }

            //Console.WriteLine("Start Time: {0}", DateTime.Parse(filter.start_date).ToString());
            if (filter.start_date != null)
            {
                result = result.Where(c => c.DateTime >= DateTime.Parse(filter.start_date));
            }

            if (filter.end_date != null)
            {
                result = result.Where(c => c.DateTime <= DateTime.Parse(filter.end_date));
            }

            if (filter.tmin != null)
            {
                result = result.Where(c => c.Temperature >= Double.Parse(filter.tmin));
            }


            if (filter.tmax != null)
            {
                result = result.Where(c => c.Temperature <= Double.Parse(filter.tmax));
            }


            if (filter.smin != null)
            {
                result = result.Where(c => c.Similar >= Double.Parse(filter.smin));
            }


            if (filter.smax != null)
            {
                result = result.Where(c => c.Similar <= Double.Parse(filter.smax));
            }

            var count = result.Count();


            return(new JsonResult(new { Count = count, Data = result }));
        }
        public JsonResult getSopoRecoDia(getFilter filter)
        {
            var result = from o in dbContext.SopoRecoDias
                         select o;

            if (filter.device_id != null)
            {
                result = result.Where(c => c.DevId == filter.device_id);
            }

            if (filter.document != null)
            {
                result = result.Where(c => c.DocId.Contains(filter.document));
            }

            if (filter.company != null)
            {
                result = result.Where(c => c.Empresa.Contains(filter.company));
            }

            if (filter.eXcompany != null)
            {
                result = result.Where(c => !c.Empresa.Contains(filter.eXcompany));
            }

            if (filter.name != null)
            {
                result = result.Where(c => c.Name.Contains(filter.name));
            }

            if (filter.ciudad != null)
            {
                result = result.Where(c => c.Ciudad.Contains(filter.ciudad));
            }

            //Console.WriteLine("Start Time: {0}", DateTime.Parse(filter.start_date).ToString());
            if (filter.start_date != null)
            {
                result = result.Where(c => c.DateTime >= DateTime.Parse(filter.start_date));
            }

            if (filter.end_date != null)
            {
                result = result.Where(c => c.DateTime <= DateTime.Parse(filter.end_date));
            }

            if (filter.tmin != null)
            {
                result = result.Where(c => c.Temperature >= Double.Parse(filter.tmin));
            }


            if (filter.tmax != null)
            {
                result = result.Where(c => c.Temperature <= Double.Parse(filter.tmax));
            }


            if (filter.smin != null)
            {
                result = result.Where(c => c.Similar >= Double.Parse(filter.smin));
            }


            if (filter.smax != null)
            {
                result = result.Where(c => c.Similar <= Double.Parse(filter.smax));
            }


            var count = result.Count();


            // var personas = result.Select(e=>new {e.DocId, e.DevId, e.Ciudad, e.Sitio})
            //                      .Distinct()
            //                      .ToList();

            // List<SopoRecoDia> personas = result.GroupBy(e=>new{e.Ciudad, e.Sitio, e.DevId, e.DocId})
            //                     .Select(e=>new {e.Key.Ciudad, e.Key.Sitio, e.Key.DevId, e.Key.DocId})
            //                     .ToList();

            // Console.WriteLine("Registros : " + count);
            // Console.WriteLine("Personas");
            // Console.WriteLine(personas);


            return(new JsonResult(new { Count = count, Data = result }));
        }
        public JsonResult getRepoEnroll(getFilter filter)
        {
            var result = from o in dbContext.RepoEnrolamientos
                         select o;

            if (filter.name != null)
            {
                result = result.Where(c => c.Name.Contains(filter.name));
            }

            if (filter.company != null)
            {
                result = result.Where(c => c.Empresa.Contains(filter.company));
            }

            if (filter.eXcompany != null)
            {
                result = result.Where(c => !c.Empresa.Contains(filter.eXcompany));
            }

            if (filter.ciudad != null)
            {
                result = result.Where(c => c.Ciudad.Contains(filter.ciudad));
            }

            //Console.WriteLine("Start Time: {0}", DateTime.Parse(filter.start_date).ToString());
            if (filter.start_date != null)
            {
                result = result.Where(c => c.Time >= DateTime.Parse(filter.start_date));
            }

            if (filter.end_date != null)
            {
                result = result.Where(c => c.Time <= DateTime.Parse(filter.end_date));
            }

            if (filter.start_update != null)
            {
                result = result.Where(c => c.Updated >= DateTime.Parse(filter.start_update));
            }

            if (filter.end_update != null)
            {
                result = result.Where(c => c.Updated <= DateTime.Parse(filter.end_update));
            }

            //||
            if (filter.hasPhoto != null)
            {
                var hasFoto = (filter.hasPhoto == "true") ? true : false;
                Console.WriteLine("El filtro es :" + filter.hasPhoto);
                if (filter.hasPhoto == "true")
                {
                    result = result.Where(c => c.hasPhoto == true);
                }
                else
                {
                    result = result.Where(c => c.hasPhoto == false);
                }
            }

            var count = result.Count();


            return(new JsonResult(new { Count = count, Data = result }));
        }