예제 #1
0
        private RegistroCmv CarregaClima(DateTime dia, int Filial)
        {
            DalConnection dal    = new DalConnection(configuration, logger);
            RegistroCmv   result = new RegistroCmv();

            try
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("SELECT c.maxtempc, c.mintempc, c.avgtempc, c.precipmm");
                sb.AppendLine("FROM GS_MVW_FILIAIS f");
                sb.AppendLine("INNER JOIN clima c on c.city = f.cidade");
                sb.AppendLine("                   and c.dia = &DIA");
                sb.AppendLine("WHERE f.codigo = &FIL");

                Dictionary <string, object> param = new Dictionary <string, object>
                {
                    { "DIA", ((dia.Year - 1900) * 100 + dia.Month) * 100 + dia.Day },
                    { "FIL", Filial }
                };

                var dt = dal.ExecuteQuery(sb, param);

                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        foreach (DataColumn dc in dt.Columns)
                        {
                            switch (dc.ColumnName.ToUpper())
                            {
                            case "MAXTEMPC": result.MaxTempC = dr[dc] != DBNull.Value ? (float)Convert.ToDouble(dr[dc]) : 0; break;

                            case "MINTEMPC": result.MinTempC = dr[dc] != DBNull.Value ? (float)Convert.ToDouble(dr[dc]) : 0; break;

                            case "AVGTEMPC": result.AvgTempC = dr[dc] != DBNull.Value ? (float)Convert.ToDouble(dr[dc]) : 0; break;

                            case "PRECIPMM": result.PrecipMm = dr[dc] != DBNull.Value ? (float)Convert.ToDouble(dr[dc]) : 0; break;

                            default: break;
                            }
                        }
                    }
                }

                return(result);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dal = null;
            }
        }
예제 #2
0
        public ModelInput TransformaDados(RegistroCmv registroCmv)
        {
            ModelInput modelOutput = new ModelInput
            {
                Label       = registroCmv.Valor,
                ANO         = registroCmv.Dia.Year,
                DIA_SEG     = registroCmv.Dia.DayOfWeek == DayOfWeek.Monday ? 1 : 0,
                DIA_TER     = registroCmv.Dia.DayOfWeek == DayOfWeek.Tuesday ? 1 : 0,
                DIA_QUA     = registroCmv.Dia.DayOfWeek == DayOfWeek.Wednesday ? 1 : 0,
                DIA_QUI     = registroCmv.Dia.DayOfWeek == DayOfWeek.Thursday ? 1 : 0,
                DIA_SEX     = registroCmv.Dia.DayOfWeek == DayOfWeek.Friday ? 1 : 0,
                DIA_SAB     = registroCmv.Dia.DayOfWeek == DayOfWeek.Saturday ? 1 : 0,
                MES_FEV     = registroCmv.Dia.Month == 2 ? 1 : 0,
                MES_MAR     = registroCmv.Dia.Month == 3 ? 1 : 0,
                MES_ABR     = registroCmv.Dia.Month == 4 ? 1 : 0,
                MES_MAI     = registroCmv.Dia.Month == 5 ? 1 : 0,
                MES_JUN     = registroCmv.Dia.Month == 6 ? 1 : 0,
                MES_JUL     = registroCmv.Dia.Month == 7 ? 1 : 0,
                MES_AGO     = registroCmv.Dia.Month == 8 ? 1 : 0,
                MES_SET     = registroCmv.Dia.Month == 9 ? 1 : 0,
                MES_OUT     = registroCmv.Dia.Month == 10 ? 1 : 0,
                MES_NOV     = registroCmv.Dia.Month == 11 ? 1 : 0,
                MES_DEZ     = registroCmv.Dia.Month == 12 ? 1 : 0,
                SEM_MES_SEG = GetWeekNumberOfMonth(registroCmv.Dia) == 2 ? 1 : 0,
                SEM_MES_TER = GetWeekNumberOfMonth(registroCmv.Dia) == 3 ? 1 : 0,
                SEM_MES_QUA = GetWeekNumberOfMonth(registroCmv.Dia) == 4 ? 1 : 0,
                SEM_MES_QUI = GetWeekNumberOfMonth(registroCmv.Dia) == 5 ? 1 : 0,
                FERIADO     = registroCmv.Feriado ? 1 : 0,
                MAXTEMPC    = registroCmv.MaxTempC,
                MINTEMPC    = registroCmv.MinTempC,
                AVGTEMPC    = registroCmv.AvgTempC,
                PRECIPMM    = registroCmv.PrecipMm,
            };

            return(modelOutput);
        }
        private List <RegistroCmv> ListarHistorico(int Filial, int Secao, int Grupo, int SubGrupo)
        {
            DalConnection dal = new DalConnection(configuration, logger);

            try
            {
                List <RegistroCmv> registroCmvs = new List <RegistroCmv>();

                StringBuilder sb = new StringBuilder();
                sb.AppendLine("select RMS7TO_DATE(g.dia) DIA,");
                sb.AppendLine("       case when df.dia_meta is null");
                sb.AppendLine("              and d.dia_meta is null then 0 else 1 end feriado,");
                sb.AppendLine("       C.MAXTEMPC,");
                sb.AppendLine("       C.MINTEMPC,");
                sb.AppendLine("       C.AVGTEMPC,");
                sb.AppendLine("       C.PRECIPMM,");
                sb.AppendLine("       G.VDA_CMV VALOR");
                sb.AppendLine("from gs_agg_coml_sgrp_dia g");
                sb.AppendLine("inner join gs_mvw_filiais f on f.codigo = g.filial");
                sb.AppendLine("left join clima c on c.city = f.cidade");
                sb.AppendLine("                 and c.dia = g.dia");
                sb.AppendLine("left join gs_meta_diasatipico_filial df on df.filial = g.filial");
                sb.AppendLine("                                        and df.dia_meta = rms7to_date(g.dia)");
                sb.AppendLine("left join gs_meta_diasatipico d on d.dia_meta = rms7to_date(g.dia)");
                sb.AppendLine("where g.filial = &FILIAL");
                sb.AppendLine("and g.secao = &SECAO");
                sb.AppendLine("and g.grp = &GRUPO");
                sb.AppendLine("and g.sgrp = &SUBGRUPO");
                sb.AppendLine("and g.dia between 1170101 and 1190831"); // TODO: REMOVER ESSE FILTRO DE DATA EM PRODUÇÃO
                sb.AppendLine("and g.vda_cmv > 0");
                sb.AppendLine("order by g.dia");

                Dictionary <string, object> param = new Dictionary <string, object>
                {
                    { "FILIAL", Filial },
                    { "SECAO", Secao },
                    { "GRUPO", Grupo },
                    { "SUBGRUPO", SubGrupo }
                };

                var dt = dal.ExecuteQuery(sb, param);

                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataRow dataRow in dt.Rows)
                    {
                        RegistroCmv registro = new RegistroCmv();

                        foreach (DataColumn dataColumn in dt.Columns)
                        {
                            switch (dataColumn.ColumnName.ToUpper())
                            {
                            case "DIA": registro.Dia = Convert.ToDateTime(dataRow[dataColumn]); break;

                            case "VALOR": registro.Valor = (float)Convert.ToDouble(dataRow[dataColumn]); break;

                            case "FERIADO": registro.Feriado = Convert.ToInt32(dataRow[dataColumn]) == 1; break;

                            case "MAXTEMPC": registro.MaxTempC = dataRow[dataColumn] != DBNull.Value ? (float)Convert.ToDouble(dataRow[dataColumn]) : 0; break;

                            case "MIMTEMPC": registro.MinTempC = dataRow[dataColumn] != DBNull.Value ? (float)Convert.ToDouble(dataRow[dataColumn]) : 0; break;

                            case "AVGTEMPC": registro.AvgTempC = dataRow[dataColumn] != DBNull.Value ? (float)Convert.ToDouble(dataRow[dataColumn]) : 0; break;

                            case "PRECIPMM": registro.PrecipMm = dataRow[dataColumn] != DBNull.Value ? (float)Convert.ToDouble(dataRow[dataColumn]) : 0; break;

                            default:
                                break;
                            }
                        }

                        registroCmvs.Add(registro);
                    }
                }

                return(registroCmvs);
            }
            catch (OracleException ex)
            {
                if (ex.Message.ToLower().Contains("timeout"))
                {
                    logger.LogError(ex, $"Time out ao consultar historico, sera realizada uma nova tentativa! Filial: {Filial} Categoria: {Secao}/{Grupo}/{SubGrupo}");
                    Task.Delay(1000).Wait();
                    return(ListarHistorico(Filial, Secao, Grupo, SubGrupo));
                }
                else
                {
                    throw ex;
                }
            }
            catch
            {
                throw;
            }
            finally
            {
                dal = null;
            }
        }
예제 #4
0
        public List <RegistroCmv> CarregaPrevisaoDetalhada(DateTime dtIni, DateTime dtFim, int[] Filiais, int[] Categorias)
        {
            DalConnection dal = new DalConnection(configuration, logger);

            try
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("select p.secao, p.grupo, p.subgrupo,");
                sb.AppendLine("       sum(p.cmv) previsao,");
                sb.AppendLine("       sum((r.vda_cmv)) cmv_real");
                sb.AppendLine("from dsdh_previsao_cmv p");
                sb.AppendLine("inner join gs_agg_coml_sgrp_dia r on r.dia = p.dia");
                sb.AppendLine("                                and r.filial = p.filial");
                sb.AppendLine("                                and r.secao = p.secao");
                sb.AppendLine("                                and r.grp = p.grupo");
                sb.AppendLine("                                and r.sgrp = p.subgrupo");
                sb.AppendLine("where p.dia between &DTINI and &DTFIM");
                sb.AppendLine("and r.vda_cmv > 0");

                if (Filiais != null && Filiais.Length > 0)
                {
                    sb.AppendLine("and p.filial in (");

                    for (int i = 0; i < Filiais.Length; i++)
                    {
                        sb.Append($"{Filiais[i]}");
                        if (i + 1 < Filiais.Length)
                        {
                            sb.Append(",");
                        }
                    }

                    sb.AppendLine(")");
                }

                if (Categorias != null && Categorias.Length > 0)
                {
                    sb.AppendLine("and (p.secao, p.grupo, p.subgrupo ) in (");

                    for (int i = 0; i < Categorias.Length; i++)
                    {
                        int secao    = 0;
                        int grupo    = 0;
                        int subgrupo = 0;

                        secao    = (int)Math.Truncate((decimal)Categorias[i] / 1000000);
                        grupo    = (int)Math.Truncate(Math.Truncate((decimal)Categorias[i] / 1000) % 1000);
                        subgrupo = (int)Math.Truncate((decimal)Categorias[i] % 1000);

                        sb.AppendLine($"select {secao} s, {grupo} g, {subgrupo} sg from dual");

                        if (i + 1 < Categorias.Length)
                        {
                            sb.AppendLine("UNION ALL");
                        }
                    }

                    sb.AppendLine(")");
                }

                sb.AppendLine("group by p.secao, p.grupo, p.subgrupo");
                sb.AppendLine("order by p.secao, p.grupo, p.subgrupo");

                Dictionary <string, object> param = new Dictionary <string, object>
                {
                    { "DTINI", ((dtIni.Year - 1900) * 100 + dtIni.Month) * 100 + dtIni.Day },
                    { "DTFIM", ((dtFim.Year - 1900) * 100 + dtFim.Month) * 100 + dtFim.Day },
                };

                var dt = dal.ExecuteQuery(sb, param);

                List <RegistroCmv> registroCmvs = null;

                if (dt != null && dt.Rows.Count > 0)
                {
                    registroCmvs = new List <RegistroCmv>();
                    foreach (DataRow dr in dt.Rows)
                    {
                        RegistroCmv registroCmv = new RegistroCmv();

                        foreach (DataColumn dc in dt.Columns)
                        {
                            switch (dc.ColumnName.ToUpper())
                            {
                            //case "FILIAL": registroCmv.Filial = Convert.ToInt32(dr[dc]); break;
                            case "SECAO": registroCmv.Secao = Convert.ToInt32(dr[dc]); break;

                            case "GRUPO": registroCmv.Grupo = Convert.ToInt32(dr[dc]); break;

                            case "SUBGRUPO": registroCmv.SubGrupo = Convert.ToInt32(dr[dc]); break;

                            case "PREVISAO": registroCmv.Previsao = Convert.ToSingle(dr[dc]); break;

                            case "CMV_REAL":
                                registroCmv.ValorIsNull = dr[dc] == DBNull.Value;
                                registroCmv.Valor       = dr[dc] != DBNull.Value ? Convert.ToSingle(dr[dc]) : 0;
                                break;

                            default:
                                break;
                            }
                        }

                        registroCmvs.Add(registroCmv);
                    }
                }

                return(registroCmvs);
            }
            catch
            {
                throw;
            }
            finally
            {
                dal = null;
            }
        }