Exemplo n.º 1
0
        public async Task <bool> AlocarPessoa(Pessoa p)
        {
            List <Grupo> grupo = await bdconn.Table <Grupo>().ToListAsync();

            List <Pessoa_Grupo> PGs = await bdconn.Table <Pessoa_Grupo>().ToListAsync();

            //int length = await bdconn.Table<Pessoa_Grupo>().CountAsync();
            //var query = "SELECT P.ID, P.Nome FROM (Pessoa AS P INNER JOIN Pessoa_Grupo AS PG ON P.ID = PG.pessoaID) WHERE PG.grupoID =";
            //Random rnd = new Random();
            for (int i = 0; i < grupo.Count; i++)
            {
                //int index = rnd.Next(grupo.Count() - 1);
                //var g = grupo[index];
                //var ps = await bdconn.QueryAsync<Pessoa>(query + grupo[i].ID);
                var           filtrado = PGs.Where(pg => pg.grupoID == grupo[i].ID);
                List <Pessoa> pf       = new List <Pessoa>();
                foreach (var item in filtrado)
                {
                    pf.Add(await bdconn.Table <Pessoa>().Where(pp => pp.ID == item.pessoaID).FirstOrDefaultAsync());
                }
                grupo[i].AddGeral(pf);
                if (grupo[i].addMembro(p))
                {
                    Pessoa_Grupo pg = new Pessoa_Grupo(grupo[i].ID, p.ID);
                    await bdconn.InsertAsync(pg);

                    return(true);
                }

                //grupo.RemoveAt(index);
            }
            return(false);
        }
Exemplo n.º 2
0
        private async Task AlocarAutomaticamente()
        {
            try
            {
                await bdconn.CreateTableAsync <Grupo>();

                await bdconn.CreateTableAsync <Pessoa_Grupo>();

                //await bdconn.ExecuteAsync("CREATE TABLE pessoa_grupo(pessoa_grupo_id INTEGER PRIMARY KEY,grupoID varchar(255), pessoaID varchar(255))");
                int countP = await bdconn.Table <Pessoa>().CountAsync();

                int countPG = await bdconn.Table <Pessoa_Grupo>().CountAsync();

                int countG = await bdconn.Table <Grupo>().CountAsync();

                var listP = await bdconn.Table <Pessoa>().ToListAsync();

                Random rnd = new Random();
                toolStripProgressBar1.Value   = 0;
                toolStripProgressBar1.Minimum = 0;
                toolStripProgressBar1.Maximum = countP - countPG;
                while (countP != countPG)
                {
                    int index = rnd.Next(listP.Count() - 1);
                    var p     = listP[index];
                    Debug.WriteLine(listP.Count());
                    if (await bdconn.Table <Pessoa_Grupo>().Where(x => x.pessoaID == p.ID).FirstOrDefaultAsync() == null)
                    {
                        //vou alocar a pessoa em algum grupo automaticamente
                        if (countG == 0)
                        {
                            await CriaGruposAuto();

                            countG = await bdconn.Table <Grupo>().CountAsync();
                        }

                        if (await AlocarPessoa(p))
                        {
                            listP.RemoveAt(index);
                        }
                        else
                        {
                            bool temResto = true;
                            foreach (var item in listP)
                            {
                                if (await AlocarPessoa(item))
                                {
                                    temResto = false;
                                }
                                if (temResto == false)
                                {
                                    listP.Remove(item);
                                    break;
                                }
                            }
                            if (temResto)
                            {
                                Grupo g;
                                foreach (var item in listP)
                                {
                                    int idademin = 18, idademax = 100;
                                    if (item.Idade < 18)
                                    {
                                        idademin = 7;
                                        idademax = 18;
                                    }
                                    g = new Grupo(item.Sexo, idademin, idademax, (item.ehPastor || item.ehEsposaDePastor) ? true : false);
                                    await bdconn.InsertAsync(g);

                                    Pessoa_Grupo pessoa_Grupo = new Pessoa_Grupo(g.ID, item.ID);
                                    await bdconn.InsertAsync(pessoa_Grupo);
                                }
                                break;
                            }
                        }
                    }
                    else
                    {
                        listP.RemoveAt(index);
                    }
                    if (toolStripProgressBar1.Value < toolStripProgressBar1.Maximum)
                    {
                        toolStripProgressBar1.Value = countP - listP.Count;
                    }
                    countPG = await bdconn.Table <Pessoa_Grupo>().CountAsync();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                await AtualizarListadeGrupos(false);
            }
        }