public void MonitorarRoboPlural()
        {
            try
            {
                PersistenciaDB db = new PersistenciaDB(ConfigurationManager.ConnectionStrings["TRADE"].ConnectionString);

                Dictionary <int, ComitenteDiaInfo> dctComit = db.BuscarComitentesDia(lastMonitor);

                logger.Info("Monitorou comitentes no dia com " + dctComit.Count + " itens");

                if (DateTime.Now.Hour >= 10 && DateTime.Now.Hour < 18)
                {
                    if (dctComit.Count == 0)
                    {
                        logger.Warn("NAO RETORNOU NOVOS REGISTROS DESDE [" + lastMonitor.ToString("dd/MM/yyyy HH:mm:ss") + "]");

                        string message = "NAO RETORNOU NOVOS REGISTROS DESDE [" + lastMonitor.ToString("dd/MM/yyyy HH:mm:ss") + "].";

                        message += Environment.NewLine + "Verificar robo da Brasil Plural.";

                        string subject = "Alerta Servico RecalculoFinanceiroBRP: " + DateTime.Now.ToString("HH:mm:ss");

                        Utilities.EnviarEmail(subject, message);

                        lastRun = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 8, 0, 0);
                    }
                    else
                    {
                        lastMonitor = DateTime.Now;
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error("MonitorarRoboPlural(): " + ex.Message, ex);
            }
        }
        public void RecalcularFinanceiro(string param)
        {
            bool bIgnoreTimestamp = false;

            if (!String.IsNullOrEmpty(param))
            {
                if (param.ToLowerInvariant().Equals("ignoretimestamp"))
                {
                    bIgnoreTimestamp = true;
                    EnviarRelatorioRecalculoUltimaHora();
                    dctRecalculado.Clear();
                    initialRun = lastRun;
                    lastRun    = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 8, 0, 0);
                    logger.Info("IGNORANDO TIMESTAMP DA ORDEM");
                }
                else
                {
                    if (bRecalculando)
                    {
                        logger.Warn("Loop de recalculo em andamento, deixando para proxima rodada");
                        return;
                    }
                }
            }

            bRecalculando = true;

            logger.Info("Recalculando ordens executadas desde [" + lastRun.ToString("dd/MM/yyyy HH:mm:ss.fff") + "]");

            ConfigurationManager.RefreshSection("appSettings");

            int faxProcedureInterval = 10 * 1000;

            if (ConfigurationManager.AppSettings["FaxProcedureInterval"] != null)
            {
                faxProcedureInterval = Convert.ToInt32(ConfigurationManager.AppSettings["FaxProcedureInterval"].ToString());
            }


            try
            {
                PersistenciaDB db = new PersistenciaDB(ConfigurationManager.ConnectionStrings["TRADE"].ConnectionString);

                Dictionary <int, ComitenteDiaInfo> dctComit = db.BuscarComitentesDia(lastRun.AddMinutes(-5));

                lastRun = DateTime.Now;

                logger.Info("Comitentes no dia com " + dctComit.Count + " itens");

                foreach (int account in dctComit.Keys)
                {
                    try
                    {
                        db = new PersistenciaDB(ConfigurationManager.ConnectionStrings["SINACOR"].ConnectionString);

                        if (db.EfetuarRecalculo(account, DateTime.Now))
                        {
                            logger.Info("Recalculo da conta [" + account + "] chamada com sucesso");
                            if (!dctRecalculado.ContainsKey(account) && !bIgnoreTimestamp)
                            {
                                dctRecalculado.Add(account, account);
                            }
                        }
                        else
                        {
                            logger.Error("Erro no recalculo da conta [" + account + "]");
                        }

                        if (!bIgnoreTimestamp)
                        {
                            // Calcula o intervalo correto para efetuar todos os recalculos
                            // em 58s
                            faxProcedureInterval = 58000 / dctComit.Keys.Count;
                            Thread.Sleep(faxProcedureInterval);
                        }
                    }
                    catch (Exception ex)
                    {
                        logger.Error("Erro no recalculo da conta [" + account + "]: " + ex.Message, ex);
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error("RecalcularFinanceiro(): " + ex.Message, ex);
            }
            finally
            {
                bRecalculando = false;
            }
        }