Example #1
0
        public bool salvarDadosObj(string _id, string _nome, DateTime _dataIniciar, DateTime _dataExpiracao, bool ativo, Dictionary <string, object> _listaMiniGames)
        {
            ObjectId      objToFind = new ObjectId();
            ObjetivoModel objModel  = new ObjetivoModel();

            try
            {
                var _obj = _db.GetCollection <ObjetivoModel>("Objetivo");

                if (_id != null && _id != "")
                {
                    objToFind = new ObjectId(_id);

                    objModel = _obj.AsQueryable().Where(c => c._id.Equals(objToFind)).FirstOrDefault <ObjetivoModel>();
                }


                if (objModel != null && objModel._id.Contains(_id))
                {
                    var update = Builders <ObjetivoModel>
                                 .Update
                                 .Set(u => u.nome, _nome)
                                 .Set(u => u.dataCadastro, objModel.dataCadastro)
                                 .Set(u => u.dataAlteracao, new Utils().convertDatePtBr(DateTime.Now.ToLocalTime()))
                                 .Set(u => u.dataIniciar, new Utils().convertDatePtBr(_dataIniciar))
                                 .Set(u => u.dataExpiracao, new Utils().convertDatePtBr(_dataExpiracao))
                                 .Set(u => u.ativo, ativo);

                    var result = _obj.UpdateOne <ObjetivoModel>(u => u._id.Equals(objToFind), update);


                    return(true);
                }
                else
                {
                    objModel.nome          = _nome;
                    objModel.dataIniciar   = _dataIniciar;
                    objModel.dataExpiracao = _dataExpiracao;
                    objModel.ativo         = ativo;

                    foreach (dynamic dmg in _listaMiniGames)
                    {
                        dynamic       mg      = dmg.Value;
                        MiniGameModel dadosMg = new MiniGameModel(mg.id + " - " + mg.nome, true, mg.parametroObjCompletado, mg.vlrCashIn);

                        objModel.adicionaNaListaMiniGame(dadosMg);
                    }

                    objModel.listaMiniGames = objModel.retornaListaMiniGame();

                    _obj.InsertOne(objModel);

                    return(true);
                }
            }
            catch (Exception xp)
            {
                return(false);
            }
        }
        private bool persistirBancoDeDados(string infoDoc, string infoObj)
        {
            string codMiniGame = string.Empty;
            bool   resp        = false;

            try
            {
                startModelo();
                var _obj = _db.GetCollection <ObjetivoMiniGameModel>("ObjetivoIniciado");

                ObjetivoModel dadosObjModel = this.findObjetivoPrincipal(infoObj);

                if (dadosObjModel != null)
                {
                    string nomeObj = infoDoc + "-" + infoObj;
                    ObjetivoMiniGameModel objFound = this.findObjMiniGameInUser(infoDoc, infoObj);

                    if (objFound == null)
                    {
                        objModel.nome = nomeObj;
                        objModel.setUsuarioGame(new UsuarioModel {
                            nome = infoDoc, documento = infoDoc
                        });
                        objModel.setObjetivoGame(dadosObjModel);

                        dynamic dynList = dadosObjModel.retornaListaMiniGame();

                        foreach (dynamic objList in dynList)
                        {
                            dynamic obj = objList;
                            objModel.addMiniGames(objList);
                        }

                        codMiniGame = objModel._id.ToString();
                        _obj.InsertOne(objModel);

                        resp = true;
                    }


                    var posicaoLista = (objFound != null ? objFound.ListaMiniGames.FindIndex(f => f.nome.Contains(infoObj)) : 0);
                    if (objFound != null && objFound.ativo && resp == false && posicaoLista >= 0)
                    {
                        int posicao = posicaoLista; // objFound.ListaMiniGames.FindIndex(p => p.nome.Contains(infoObj));

                        codMiniGame = objFound._id.ToString();

                        var listaAtualMiniGames = objFound.listarMiniGames();


                        MiniGameModel objTrataValores = listaAtualMiniGames.ElementAt(posicao);

                        if (objTrataValores.ativo && objTrataValores.CashInSent.Equals(false))
                        {
                            string objId         = (string)objTrataValores._id;
                            double secondsTarget = objTrataValores.targetForPayment;
                            double diffInSeconds = this.validaTempoJogando(objTrataValores);


                            var update = Builders <ObjetivoMiniGameModel>
                                         .Update
                                         .Set(u => u.dataCadastro, objFound.dataCadastro)
                                         .Set(u => u.dataAlteracao, new Utils().convertDatePtBr(DateTime.Now.ToLocalTime()))
                                         .Set(u => u.ObjUsr.dataCadastro, objFound.ObjUsr.dataCadastro)
                                         .Set(u => u.ObjUsr.dataAlteracao, new Utils().convertDatePtBr(DateTime.Now.ToLocalTime()))
                                         .Set(u => u.ObjParametros.dataCadastro, objFound.ObjParametros.dataCadastro)
                                         .Set(u => u.ObjParametros.dataAlteracao, new Utils().convertDatePtBr(DateTime.Now.ToLocalTime()))
                                         .Set(u => u.ListaMiniGames.ElementAt(posicao).dataCadastro, objFound.ListaMiniGames.ElementAt(posicao).dataCadastro)
                                         .Set(u => u.ListaMiniGames.ElementAt(posicao).dataAlteracao, new Utils().convertDatePtBr(DateTime.Now.ToLocalTime()))
                                         .Set(u => u.ListaMiniGames.ElementAt(posicao).objDataIniciado, objFound.ListaMiniGames.ElementAt(posicao).objDataIniciado)
                                         .Set(u => u.ListaMiniGames.ElementAt(posicao).objDataFinalizado, new Utils().convertDatePtBr(DateTime.Now.ToLocalTime()))
                                         .Set(u => u.ListaMiniGames.ElementAt(posicao).objTempoJogando, diffInSeconds);

                            var result = _obj.UpdateOne <ObjetivoMiniGameModel>(u => u._id == objFound._id, update);



                            resp = true;
                        }
                    }
                    else if (resp == false && objFound != null && objFound.ativo)
                    {
                        int posicao = posicaoLista; // objFound.ListaMiniGames.FindIndex(p => p.nome.Contains(infoObj));

                        if (posicao >= 0)
                        {
                            codMiniGame = objFound._id.ToString();

                            MiniGameModel objTrataValores = objFound.listarMiniGames().ElementAt(posicao);

                            if (objTrataValores != null && objTrataValores.ativo && objTrataValores.CashInSent.Equals(false))
                            {
                                string objId         = (string)objTrataValores._id;
                                double secondsTarget = objTrataValores.targetForPayment;
                                double diffInSeconds = this.validaTempoJogando(objTrataValores);


                                var update = Builders <ObjetivoMiniGameModel>
                                             .Update
                                             .Set(u => u.dataCadastro, objFound.dataCadastro)
                                             .Set(u => u.dataAlteracao, new Utils().convertDatePtBr(DateTime.Now.ToLocalTime()))
                                             .Set(u => u.ObjUsr.dataCadastro, objFound.ObjUsr.dataCadastro)
                                             .Set(u => u.ObjUsr.dataAlteracao, new Utils().convertDatePtBr(DateTime.Now.ToLocalTime()))
                                             .Set(u => u.ObjParametros.dataCadastro, objFound.ObjParametros.dataCadastro)
                                             .Set(u => u.ObjParametros.dataAlteracao, new Utils().convertDatePtBr(DateTime.Now.ToLocalTime()))
                                             .Set(u => u.ListaMiniGames.ElementAt(posicao).dataCadastro, objFound.ListaMiniGames.ElementAt(posicao).dataCadastro)
                                             .Set(u => u.ListaMiniGames.ElementAt(posicao).dataAlteracao, new Utils().convertDatePtBr(DateTime.Now.ToLocalTime()))
                                             .Set(u => u.ListaMiniGames.ElementAt(posicao).objDataIniciado, objFound.ListaMiniGames.ElementAt(posicao).objDataIniciado)
                                             .Set(u => u.ListaMiniGames.ElementAt(posicao).objDataFinalizado, new Utils().convertDatePtBr(DateTime.Now.ToLocalTime()))
                                             .Set(u => u.ListaMiniGames.ElementAt(posicao).objTempoJogando, diffInSeconds);

                                var result = _obj.UpdateOne <ObjetivoMiniGameModel>(u => u._id == objFound._id, update);

                                resp = true;
                            }
                        }
                        else
                        {
                            var listaAtualMiniGames = dadosObjModel.retornaListaMiniGame(); // objFound.listarMiniGames();

                            MiniGameModel novoMiniGame = new MiniGameModel();

                            foreach (dynamic objList in listaAtualMiniGames)
                            {
                                novoMiniGame = new MiniGameModel {
                                    nome = objList.nome, ativo = true, targetForPayment = objList.targetForPayment, CashPayment = objList.CashPayment
                                };
                                objModel.addMiniGames(novoMiniGame);
                            }

                            List <MiniGameModel> novaListaMiniGames = objModel.listarMiniGames();

                            var update = Builders <ObjetivoMiniGameModel>
                                         .Update
                                         .Set(u => u.dataCadastro, objFound.dataCadastro)
                                         .Set(u => u.dataAlteracao, new Utils().convertDatePtBr(DateTime.Now.ToLocalTime()))
                                         .Set(u => u.ObjUsr.dataCadastro, objFound.ObjUsr.dataCadastro)
                                         .Set(u => u.ObjUsr.dataAlteracao, objFound.ObjUsr.dataCadastro)
                                         .Set(u => u.ObjParametros.dataCadastro, objFound.ObjParametros.dataCadastro)
                                         .Set(u => u.ObjParametros.dataAlteracao, new Utils().convertDatePtBr(DateTime.Now.ToLocalTime()))
                                         .Push(p => p.ListaMiniGames, novoMiniGame);

                            var result = _obj.UpdateOne <ObjetivoMiniGameModel>(u => u._id == objFound._id, update);

                            resp = true;
                        }
                    }

                    if (resp == true)
                    {
                        this.validaDefineObjetivoConcluido(infoDoc, codMiniGame);
                    }
                }


                return(resp);
            }
            catch (Exception xp)
            {
                return(false);
            }
        }