Ejemplo n.º 1
0
        public static List <LOAItemOferta> resetLivro(List <LOAGrupoOfertas> agregado,
                                                      List <LOFDadosOferta> livro,
                                                      List <LOFDadosOferta> listaLOF)
        {
            List <LOAItemOferta> retorno = new List <LOAItemOferta>();

            for (int i = 0; i < livro.Count; i++)
            {
                LOFDadosOferta oferta = livro[i];
                oferta.Posicao = i;
                listaLOF.Add(oferta);
                livro.RemoveAt(i);
            }

            for (int i = agregado.Count - 1; i >= 0; i--)
            {
                LOAGrupoOfertas grupo = agregado[i];
                agregado.RemoveAt(i);

                LOAItemOferta itemOferta = new LOAItemOferta();
                itemOferta.Indice     = i;
                itemOferta.Preco      = grupo.Preco;
                itemOferta.Quantidade = grupo.Quantidade;
                itemOferta.QtdeOrdens = grupo.QtdeOrdens;
                itemOferta.Acao       = ConstantesMDS.HTTP_OFERTAS_TIPO_ACAO_EXCLUIR;

                retorno.Add(itemOferta);
            }

            return(retorno);
        }
Ejemplo n.º 2
0
        public static List <LOAItemOferta> removeOferta(List <LOAGrupoOfertas> agregado,
                                                        List <LOFDadosOferta> livro,
                                                        LOFDadosOferta oferta)
        {
            List <LOAItemOferta> retorno = new List <LOAItemOferta>();

            LOAItemOferta itemLOA = new LOAItemOferta();

            LOFDadosOferta ofertaremovida = livro[oferta.Posicao];

            if (logger.IsDebugEnabled)
            {
                logger.Debug("Remover oferta [" + ofertaremovida.IDOferta + "] do grupo [" + ofertaremovida.Preco +
                             "][" + ofertaremovida.Quantidade + "] posicao [" + oferta.Posicao + "]");
            }

            livro.RemoveAt(oferta.Posicao);
            itemLOA.Acao = ConstantesMDS.HTTP_OFERTAS_TIPO_ACAO_ALTERAR;

            for (int i = 0; i < agregado.Count; i++)
            {
                LOAGrupoOfertas grupo = agregado[i];

                if (grupo.Preco.CompareTo(ofertaremovida.Preco) == 0)
                {
                    if (LOAGrupoOfertas.excluiOferta(grupo, ofertaremovida, 1))
                    {
                        if (logger.IsDebugEnabled)
                        {
                            logger.Debug("Oferta [" + oferta.IDOferta + "] eh a ultima do grupo [" + ofertaremovida.Preco + "] removendo grupo");
                        }
                        itemLOA.Acao = ConstantesMDS.HTTP_OFERTAS_TIPO_ACAO_EXCLUIR;
                        agregado.RemoveAt(i);
                    }
                    else
                    {
                        agregado[i] = grupo;
                    }

                    itemLOA.Indice     = i;
                    itemLOA.Quantidade = grupo.Quantidade;
                    itemLOA.Preco      = grupo.Preco;
                    itemLOA.QtdeOrdens = grupo.QtdeOrdens;

                    retorno.Add(itemLOA);

                    return(retorno);
                }
            }

            logger.Error("removeOferta: Nao encontrou indice para o grupo de preco [" + ofertaremovida.Preco + "]");

            return(retorno);
        }
Ejemplo n.º 3
0
        public static Dictionary <String, String> montarRegistroAgregado(LOAItemOferta itemOferta, int casasDecimais)
        {
            Dictionary <String, String> oferta = new Dictionary <String, String>();

            String preco = "";

            if (itemOferta.Preco == ConstantesMDS.PRECO_LIVRO_OFERTAS_MIN_VALUE ||
                itemOferta.Preco == ConstantesMDS.PRECO_LIVRO_OFERTAS_MAX_VALUE)
            {
                preco = ConstantesMDS.DESCRICAO_OFERTA_ABERTURA;
            }
            else
            {
                if (itemOferta.Preco != Decimal.Zero)
                {
                    preco = String.Format("{0:f" + casasDecimais + "}", itemOferta.Preco).Replace('.', ',');
                }
            }

            oferta.Add(
                ConstantesMDS.HTTP_OFERTAS_ACAO,
                String.Format("{0:d}", itemOferta.Acao));
            oferta.Add(
                ConstantesMDS.HTTP_OFERTAS_POSICAO,
                String.Format("{0:d}", itemOferta.Indice));
            oferta.Add(
                ConstantesMDS.HTTP_OFERTAS_PRECO,
                preco);
            oferta.Add(
                ConstantesMDS.HTTP_OFERTAS_QUANTIDADE,
                itemOferta.Quantidade == 0 ? "" : String.Format("{0:d}", itemOferta.Quantidade));

            if (itemOferta.Acao == ConstantesMDS.HTTP_OFERTAS_TIPO_ACAO_ALTERAR && itemOferta.QtdeOrdens <= 0)
            {
                oferta.Add(
                    ConstantesMDS.HTTP_OFERTAS_QTDE_ORDENS, "N/D");
            }
            else
            {
                oferta.Add(
                    ConstantesMDS.HTTP_OFERTAS_QTDE_ORDENS,
                    itemOferta.QtdeOrdens == 0 ? "" : String.Format("{0:d}", itemOferta.QtdeOrdens));
            }

            return(oferta);
        }
Ejemplo n.º 4
0
        public static List <LOAItemOferta> removeOfertaEPiores(List <LOAGrupoOfertas> agregado,
                                                               List <LOFDadosOferta> livro,
                                                               LOFDadosOferta oferta,
                                                               int sentido,
                                                               List <LOFDadosOferta> listaLOF)
        {
            String nomelivro = "Compra";

            long quantidadeRemovida      = 0;
            List <LOAItemOferta> retorno = new List <LOAItemOferta>();

            if (sentido == LIVRO_VENDA)
            {
                nomelivro = "Venda";
            }

            if (logger.IsDebugEnabled)
            {
                logger.Debug(nomelivro + ":Remover oferta [" + oferta.IDOferta + "] do grupo [" + oferta.Preco + "] e piores");
            }

            LOFDadosOferta ofertaReferencia = livro[oferta.Posicao];
            long           qtdeOrdens       = 0;

            for (int posdel = livro.Count - 1; posdel >= oferta.Posicao; posdel--)
            {
                LOFDadosOferta ofertaremovida = livro[posdel];
                ofertaremovida.Posicao = posdel;
                livro.RemoveAt(posdel);
                listaLOF.Add(ofertaremovida);

                // Se for do mesmo grupo de ofertas, acumula pra remover de uma unica vez
                if (ofertaremovida.Preco.CompareTo(ofertaReferencia.Preco) == 0)
                {
                    quantidadeRemovida += ofertaremovida.Quantidade;
                    qtdeOrdens++;
                }
            }

            int i = agregado.Count - 1;

            while (i >= 0)
            {
                LOAGrupoOfertas grupo = agregado[i];

                if ((grupo.Preco.CompareTo(ofertaReferencia.Preco) < 0 && sentido == LIVRO_COMPRA) ||
                    (grupo.Preco.CompareTo(ofertaReferencia.Preco) > 0 && sentido == LIVRO_VENDA))
                {
                    if (logger.IsDebugEnabled)
                    {
                        logger.Debug(nomelivro + ":Remover grupo do preco [" + grupo.Preco + "]");
                    }

                    // Remove o elemento. A lista sobe, entao novamente
                    // pegamos o elemento no indice atual
                    LOAItemOferta itemOferta = new LOAItemOferta();
                    itemOferta.Acao       = ConstantesMDS.HTTP_OFERTAS_TIPO_ACAO_EXCLUIR;
                    itemOferta.Indice     = i;
                    itemOferta.Preco      = grupo.Preco;
                    itemOferta.Quantidade = grupo.Quantidade;
                    itemOferta.QtdeOrdens = grupo.QtdeOrdens;

                    retorno.Add(itemOferta);

                    agregado.RemoveAt(i);
                    //continue;
                }

                // Remove as melhores ofertas do mesmo grupo da oferta
                if (grupo.Preco.CompareTo(ofertaReferencia.Preco) == 0)
                {
                    if (logger.IsDebugEnabled)
                    {
                        logger.Debug(nomelivro + ":Remover a oferta do grupo do preco [" + grupo.Preco + "] indice [" + i + "]");
                    }

                    ofertaReferencia.Quantidade = quantidadeRemovida;

                    LOAItemOferta itemOferta = new LOAItemOferta();
                    itemOferta.Indice = i;
                    itemOferta.Preco  = grupo.Preco;

                    // Na verdade, ele so recalcula a quantidade,
                    // se zerar, exclui o grupo
                    if (LOAGrupoOfertas.excluiOferta(grupo, ofertaReferencia, qtdeOrdens))
                    {
                        if (logger.IsDebugEnabled)
                        {
                            logger.Debug(nomelivro + ":Oferta [" + ofertaReferencia.IDOferta + "] eh a ultima do grupo [" + ofertaReferencia.Preco + "] removendo grupo");
                        }

                        itemOferta.Acao = ConstantesMDS.HTTP_OFERTAS_TIPO_ACAO_EXCLUIR;
                        agregado.RemoveAt(i);
                    }
                    else
                    {
                        itemOferta.Acao = ConstantesMDS.HTTP_OFERTAS_TIPO_ACAO_ALTERAR;
                    }

                    itemOferta.Quantidade = grupo.Quantidade;
                    itemOferta.QtdeOrdens = grupo.QtdeOrdens;
                    retorno.Add(itemOferta);
                    break;
                }

                if ((grupo.Preco.CompareTo(ofertaReferencia.Preco) > 0 && sentido == LIVRO_COMPRA) ||
                    (grupo.Preco.CompareTo(ofertaReferencia.Preco) < 0 && sentido == LIVRO_VENDA))
                {
                    break;
                }

                i--;
            }

            return(retorno);
        }
Ejemplo n.º 5
0
        //TODO: em lugar de retornar um int, retornar um objeto
        //TODO: que contenha a acao, indice, preco, e quantidade
        public static List <LOAItemOferta> insereOferta(List <LOAGrupoOfertas> agregado,
                                                        List <LOFDadosOferta> livro,
                                                        LOFDadosOferta oferta,
                                                        int sentido)
        {
            String  nomelivro = "Compra";
            Decimal preco     = oferta.Preco;

            List <LOAItemOferta> retorno = new List <LOAItemOferta>();

            LOAItemOferta itemLOA = new LOAItemOferta();

            if (sentido == LIVRO_VENDA)
            {
                nomelivro = "Venda";
            }

            if (logger.IsDebugEnabled)
            {
                logger.Debug("Insere oferta " + nomelivro + " [" +
                             oferta.IDOferta + "][" +
                             oferta.Preco + "][" +
                             oferta.Quantidade + "][" + oferta.Posicao + "]");
            }

            itemLOA.Acao  = ConstantesMDS.HTTP_OFERTAS_TIPO_ACAO_INCLUIR;
            itemLOA.Preco = preco;

            // Insere a oferta no livro
            livro.Insert(oferta.Posicao, oferta);

            // Se eh a melhor oferta, e nao eh leilao
            // armazena valor para tratamento no leilao
            if (oferta.Posicao == 0 && !oferta.Leilao)
            {
            }

            int i = 0;

            while (i < agregado.Count)
            {
                LOAGrupoOfertas grupo = agregado[i];

                if ((preco.CompareTo(grupo.Preco) > 0 && sentido == LIVRO_COMPRA) ||
                    (preco.CompareTo(grupo.Preco) < 0 && sentido == LIVRO_VENDA))
                {
                    if (logger.IsDebugEnabled)
                    {
                        logger.Debug(nomelivro + ": Inclui novo grupo na posicao [" + i + "]");
                    }

                    LOAGrupoOfertas novogrupo = new LOAGrupoOfertas(preco, oferta, i);
                    agregado.Insert(i, novogrupo);

                    itemLOA.Quantidade = novogrupo.Quantidade;
                    itemLOA.Indice     = i;
                    itemLOA.QtdeOrdens = novogrupo.QtdeOrdens;

                    retorno.Add(itemLOA);

                    return(retorno);
                }

                if (preco.CompareTo(grupo.Preco) == 0)
                {
                    if (logger.IsDebugEnabled)
                    {
                        logger.Debug(nomelivro + ": Encontrou grupo na posicao [" + i + "]");
                    }

                    LOAGrupoOfertas.incluiOferta(grupo, oferta);
                    grupo.Indice = i;

                    agregado[i] = grupo;

                    itemLOA.Acao       = ConstantesMDS.HTTP_OFERTAS_TIPO_ACAO_ALTERAR;
                    itemLOA.Quantidade = grupo.Quantidade;
                    itemLOA.Indice     = i;
                    itemLOA.QtdeOrdens = grupo.QtdeOrdens;

                    retorno.Add(itemLOA);

                    return(retorno);
                }

                if (oferta.Posicao == 0)
                {
                    if ((preco.CompareTo(grupo.Preco) < 0 && sentido == LIVRO_COMPRA) ||
                        (preco.CompareTo(grupo.Preco) > 0 && sentido == LIVRO_VENDA))
                    {
                        logger.Error(nomelivro + ": Inconsistencia, inserindo na posicao 0 com preco pior que o topo");

                        itemLOA            = new LOAItemOferta();
                        itemLOA.Acao       = ConstantesMDS.HTTP_OFERTAS_TIPO_ACAO_EXCLUIR;
                        itemLOA.Quantidade = grupo.Quantidade;
                        itemLOA.Indice     = i;
                        itemLOA.QtdeOrdens = grupo.QtdeOrdens;
                        itemLOA.Preco      = grupo.Preco;

                        agregado.RemoveAt(i);

                        retorno.Add(itemLOA);

                        continue;
                    }
                }

                i++;
            }

            if (logger.IsDebugEnabled)
            {
                logger.Debug(nomelivro + ": Inclui novo grupo no final do livro, posicao [" + i + "]");
            }

            LOAGrupoOfertas novogrupo1 = new LOAGrupoOfertas(preco, oferta, i);

            itemLOA      = new LOAItemOferta();
            itemLOA.Acao = ConstantesMDS.HTTP_OFERTAS_TIPO_ACAO_INCLUIR;

            itemLOA.Preco      = preco;
            itemLOA.Indice     = i;
            itemLOA.Quantidade = novogrupo1.Quantidade;
            itemLOA.QtdeOrdens = novogrupo1.QtdeOrdens;
            agregado.Add(novogrupo1);

            retorno.Add(itemLOA);

            return(retorno);
        }
Ejemplo n.º 6
0
        public static List <LOAItemOferta> alteraOferta(List <LOAGrupoOfertas> agregado,
                                                        List <LOFDadosOferta> livro,
                                                        LOFDadosOferta oferta,
                                                        int sentido)
        {
            List <LOAItemOferta> retorno = new List <LOAItemOferta>();
            Decimal preco = oferta.Preco;

            if (logger.IsDebugEnabled)
            {
                logger.Debug("Altera oferta [" + oferta.IDOferta + "][" + oferta.Preco + "][" + oferta.Quantidade + "] pos [" + oferta.Posicao + "]");
            }

            LOFDadosOferta old = livro[oferta.Posicao];

            old.Posicao = oferta.Posicao;

            // Se for alteracao da oferta sem mudar o preco
            if (old.Preco.CompareTo(oferta.Preco) == 0)
            {
                livro[oferta.Posicao] = oferta;

                for (int i = 0; i < agregado.Count; i++)
                {
                    LOAGrupoOfertas grupo = agregado[i];

                    if (grupo.Preco.CompareTo(oferta.Preco) == 0)
                    {
                        LOAItemOferta itemOferta = new LOAItemOferta();
                        LOAGrupoOfertas.alteraOferta(grupo, old, oferta);

                        agregado[i] = grupo;

                        itemOferta.Acao       = ConstantesMDS.HTTP_OFERTAS_TIPO_ACAO_ALTERAR;
                        itemOferta.Indice     = i;
                        itemOferta.Quantidade = grupo.Quantidade;
                        itemOferta.Preco      = preco;
                        itemOferta.QtdeOrdens = grupo.QtdeOrdens;

                        retorno.Add(itemOferta);
                        return(retorno);
                    }
                }

                logger.Error("alteraOferta(): Nao encontrou indice para o grupo de preco [" + oferta.Preco + "]");
            }
            else
            {
                if (logger.IsDebugEnabled)
                {
                    logger.Debug("alteraoferta: [" + old.IDOferta + "] mudou para [" +
                                 oferta.IDOferta + "] preco [" +
                                 old.Preco + "]->[" + oferta.Preco + "]");
                }
                List <LOAItemOferta> itemsOfertas = LivroOfertasBase.removeOferta(agregado, livro, old);
                if (itemsOfertas.Count > 0)
                {
                    retorno.AddRange(itemsOfertas);
                }

                itemsOfertas = LivroOfertasBase.insereOferta(agregado, livro, oferta, sentido);
                if (itemsOfertas.Count > 0)
                {
                    retorno.AddRange(itemsOfertas);
                }
            }

            return(retorno);
        }