-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormGrupos.cs
629 lines (574 loc) · 25.8 KB
/
FormGrupos.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
using RpG_Software.Model;
using SQLite;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace RpG_Software
{
public partial class FormGrupos : Form
{
//aqui que começa a mágica
public FormGrupos()
{
InitializeComponent();
// listVGrupos.SelectedIndexChanged += teste;
}
private SQLiteAsyncConnection bdconn;
private string[] id_pessoa = new string[500];
private int[] id_grupo = new int[500];
int[] id_fac = new int[300];
int[] id_facaux = new int[300];
private async void Form2_Load(object sender, EventArgs e)
{
bdconn = new SQLiteAsyncConnection("bd_rpg.db");
await AlocarAutomaticamente();
}
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);
}
}
public async Task CriaGruposAuto()
{
int countPMasculinoCrianca = await bdconn.Table<Pessoa>().Where(x => x.Sexo == "Masculino" && x.Idade < 9).CountAsync();
int countPMasculinoJuvenil = await bdconn.Table<Pessoa>().Where(x => x.Sexo == "Masculino" && x.Idade > 8 && x.Idade < 13).CountAsync();
int countPMasculinoAdoles = await bdconn.Table<Pessoa>().Where(x=> x.Sexo=="Masculino" && x.Idade > 12 && x.Idade < 18).CountAsync();
int countPFemininoCrianca = await bdconn.Table<Pessoa>().Where(x => x.Sexo == "Feminino" && x.Idade < 9).CountAsync();
int countPFemininoJuvenil = await bdconn.Table<Pessoa>().Where(x => x.Sexo == "Feminino" && x.Idade > 8 && x.Idade < 13).CountAsync();
int countPFemininoAdoles = await bdconn.Table<Pessoa>().Where(x => x.Sexo == "Feminino" && x.Idade > 8 && x.Idade < 18).CountAsync();
int countPMasculinoMaior = await bdconn.Table<Pessoa>().Where(x => x.Sexo == "Masculino" && x.Idade >= 18 && x.ehPastor != true).CountAsync();
int countPFemininoMaior = await bdconn.Table<Pessoa>().Where(x => x.Sexo == "Feminino" && x.Idade >= 18 && x.ehPastor != true && x.ehEsposaDePastor != true).CountAsync();
int countPastores = await bdconn.Table<Pessoa>().Where(x => x.Sexo == "Masculino" && x.Idade >= 18 && x.ehPastor == true).CountAsync();
int countPastoras = await bdconn.Table<Pessoa>().Where(x => x.Sexo == "Feminino" && x.Idade >= 18 && (x.ehPastor == true || x.ehEsposaDePastor == true)).CountAsync();
int qtdGruposMasculinosCrianca = countPMasculinoCrianca < 8 ? 1 : Convert.ToInt16(Math.Round(countPMasculinoCrianca / 8.0) + 0.5);
int qtdGruposMasculinosJuvenil = countPMasculinoJuvenil < 8 ? 1 : Convert.ToInt16(Math.Round(countPMasculinoJuvenil / 8.0) + 0.5);
int qtdGruposMasculinosAdoles= countPMasculinoAdoles < 8 ? 1 : Convert.ToInt16(Math.Round(countPMasculinoAdoles / 8.0) + 0.5);
int qtdGruposFemininosCrianca = countPFemininoCrianca < 8 ? 1 : Convert.ToInt16(Math.Round(countPFemininoCrianca / 8.0) + 0.5);
int qtdGruposFemininosJuvenil = countPFemininoJuvenil < 8 ? 1 : Convert.ToInt16(Math.Round(countPMasculinoJuvenil / 8.0) + 0.5);
int qtdGruposFemininosAdoles = countPFemininoAdoles < 8 ? 1 : Convert.ToInt16(Math.Round(countPFemininoAdoles / 8.0) + 0.5);
int qtdGruposMasculinosMaior = Convert.ToInt16(Math.Round(countPMasculinoMaior / 11.0) + 0.5);
int qtdGruposFemininosMaior = Convert.ToInt16(Math.Round(countPFemininoMaior / 11.0) + 0.5);
int qtdGruposPastores = countPastores < 9 ? 1 : Convert.ToInt16(Math.Round(countPastores / 8.0) + 0.5);
int qtdGruposPastoras = countPastoras < 9 ? 1 : Convert.ToInt16(Math.Round(countPastoras / 8.0) + 0.5);
Grupo g = new Grupo();
for (int i = 0; i < qtdGruposMasculinosCrianca; i++)
{
g = new Grupo("Masculino", 7, 9);
await bdconn.InsertAsync(g);
}
for (int i = 0; i < qtdGruposMasculinosJuvenil; i++)
{
g = new Grupo("Masculino", 9, 13);
await bdconn.InsertAsync(g);
}
for (int i = 0; i < qtdGruposMasculinosAdoles; i++)
{
g = new Grupo("Masculino", 13, 18);
await bdconn.InsertAsync(g);
}
for (int i = 0; i < qtdGruposMasculinosMaior; i++)
{
g = new Grupo("Masculino", 18, 100);
await bdconn.InsertAsync(g);
}
for (int i = 0; i < qtdGruposFemininosCrianca; i++)
{
g = new Grupo("Feminino", 7, 9);
await bdconn.InsertAsync(g);
}
for (int i = 0; i < qtdGruposFemininosJuvenil; i++)
{
g = new Grupo("Feminino", 9, 13);
await bdconn.InsertAsync(g);
}
for (int i = 0; i < qtdGruposFemininosAdoles; i++)
{
g = new Grupo("Feminino", 13, 18);
await bdconn.InsertAsync(g);
}
for (int i = 0; i < qtdGruposFemininosMaior; i++)
{
g = new Grupo("Feminino", 18, 100);
await bdconn.InsertAsync(g);
}
for (int i = 0; i < qtdGruposFemininosMaior; i++)
{
g = new Grupo("Feminino", 18, 100);
await bdconn.InsertAsync(g);
}
for (int i = 0; i < qtdGruposPastores; i++)
{
g = new Grupo("Masculino", 18, 100, true);
await bdconn.InsertAsync(g);
}
for (int i = 0; i < qtdGruposPastoras; i++)
{
g = new Grupo("Feminino", 18, 100, true);
await bdconn.InsertAsync(g);
}
}
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;
}
public async Task AtualizarListadeGrupos(bool filtrar)
{
try
{
List<Pessoa_Grupo> PGs = await bdconn.Table<Pessoa_Grupo>().ToListAsync();
List<Grupo> grupo = await bdconn.Table<Grupo>().ToListAsync();
grupo = grupo.Where(x => PGs.Any(c => c.grupoID == x.ID)).ToList();
listVGrupos.Items.Clear();
listVPessoas.Items.Clear();
//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 =";
if (filtrar)
{
grupo = grupo.Where(x => x.Sexo == comboBox1.SelectedItem as string).ToList();
}
toolStripProgressBar1.Value = 0;
toolStripProgressBar1.Minimum = 0;
toolStripProgressBar1.Maximum = grupo.Count - 1;
for (int i = 0; i < grupo.Count; i++)
{
// var ps = await bdconn.QueryAsync<Pessoa>(query + grupo[i].ID);
toolStripProgressBar1.Value = i;
List<Pessoa> pf = await PegarPessoasDeTalGrupo(grupo[i].ID);
if (pf.Count == 0)
{
//continue;
}
id_grupo[i] = grupo[i].ID;
grupo[i].AddGeral(pf);
string pessoas = grupo[i].Descricao + " - ";
for (int j = 0; j < grupo[i].Membro.Count; j++)
{
pessoas += grupo[i].Membro[j].Nome.Split(' ')[0] + ", ";
}
if (!listVGrupos.Items.Contains(new ListViewItem(pessoas)))
{
listVGrupos.Items.Add(pessoas);
}
}
}
catch (Exception)
{
}
}
public async Task<List<Pessoa>> PegarPessoasDeTalGrupo(int id)
{
List<Pessoa_Grupo> PGs = await bdconn.Table<Pessoa_Grupo>().ToListAsync();
var filtrado = PGs.Where(pg => pg.grupoID == id);
List<Pessoa> pf = new List<Pessoa>();
foreach (var item in filtrado)
{
pf.Add(await bdconn.Table<Pessoa>().Where(p => p.ID == item.pessoaID).FirstOrDefaultAsync());
}
return pf;
}
private async void listVGrupos_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
if (listVGrupos.InvokeRequired)
{
listVGrupos.Invoke((MethodInvoker)async delegate ()
{
await PreencheListVPessoas(sender);
await PreencheCmbsFaci(sender);
});
}
else
{
await PreencheListVPessoas(sender);
await PreencheCmbsFaci(sender);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private async Task PreencheListVPessoas(object sender)
{
var lista = sender as ListView;
listVPessoas.Items.Clear();
if (lista.SelectedIndices.Count == 0)
{
return;
}
// var grupo = await bdconn.Table<Grupo>().Where(x => x.ID == id_grupo[lista.SelectedIndices[0]]).FirstOrDefaultAsync();
List<Pessoa> pf = await PegarPessoasDeTalGrupo(id_grupo[lista.SelectedIndices[0]]);
int i = 0;
foreach (var p in pf)
{
id_pessoa[i] = p.ID;
var item = new ListViewItem(p.Nome);
item.SubItems.Add(p.Idade.ToString());
listVPessoas.Items.Add(item);
i++;
}
lblqtdP.Text = "Quantidade de pessoas: " + i;
}
private async Task PreencheCmbsFaci(object sender)
{
try
{
var lista = sender as ListView;
cmbFac.Items.Clear(); cmbFacAux.Items.Clear();
if (lista.SelectedIndices.Count == 0)
{
return;
}
int id = id_grupo[lista.SelectedIndices[0]];
string sexo = bdconn.FindAsync<Grupo>(x => x.ID == id).Result.Sexo;
int fac = bdconn.FindAsync<Grupo>(x => x.ID == id).Result.facilitadorid;
int faca = bdconn.FindAsync<Grupo>(x => x.ID == id).Result.facilitadorauxid;
var facs = await bdconn.Table<Facilitador>().Where(x => x.Sexo == sexo).ToListAsync();
int i = 0;
foreach (var item in facs)
{
cmbFac.Items.Add(item.Nome);
cmbFacAux.Items.Add(item.Nome);
id_fac[i] = item.ID;
id_facaux[i] = item.ID;
if (item.ID == faca)
{
cmbFacAux.SelectedIndex = i;
}
if (item.ID == fac)
{
cmbFac.SelectedIndex = i;
}
i++;
}
}
catch (Exception ex)
{
}
}
private async void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex < 2)
{
await AtualizarListadeGrupos(true);
}
else
{
await AtualizarListadeGrupos(false);
}
}
private async void btExportar_Click(object sender, EventArgs e)
{
List<Pessoa_Grupo> PGs = await bdconn.Table<Pessoa_Grupo>().ToListAsync();
List<Grupo> grupo = await bdconn.Table<Grupo>().ToListAsync();
grupo = grupo.Where(x => PGs.Any(c => c.grupoID == x.ID)).ToList();
PGs = null;
string path = Application.StartupPath + "\\grupos\\";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
for (int i = 0; i < grupo.Count; i++)
{
try
{
var pf = await PegarPessoasDeTalGrupo(grupo[i].ID);
if (pf.Count == 0)
{
continue;
}
var pfnova = pf.Select(x => new {x.Nome}).ToList();
int idfac = grupo[i].facilitadorid;
Facilitador facilitador = null;
Facilitador facilitadoraux = null;
try
{
facilitador = await bdconn.FindAsync<Facilitador>(x => x.ID == idfac);
idfac = grupo[i].facilitadorauxid;
facilitadoraux = await bdconn.FindAsync<Facilitador>(x => x.ID == idfac);
}
catch (Exception)
{
}
string file = path + (i + 1) + " - " + grupo[i].Descricao + ".csv";
if (!File.Exists(file))
{
File.Create(file).Close();
}
using (var st = new StreamWriter(file))
{
st.WriteLine(string.Format("Facilitador(a): {0}", facilitador == null ? "" : facilitador.Nome));
st.WriteLine(string.Format("Facilitador(a) auxiliar: {0}", facilitadoraux == null ? "" : facilitadoraux.Nome));
var csvW = new CsvHelper.CsvWriter(st);
csvW.WriteRecords(pfnova);
st.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
var resposta = MessageBox.Show("Deseja visitar a pasta de onde estão todos os arquivos?", "Exportação bem-sucedida!", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (resposta == DialogResult.Yes)
{
Process.Start(path);
}
}
private async void listVPessoas_SelectedIndexChanged(object sender, EventArgs e)
{
if (listVPessoas.InvokeRequired)
{
listVPessoas.Invoke((MethodInvoker)async delegate ()
{
if (listVPessoas.SelectedIndices.Count == 0)
{
groupBox1.Enabled = false;
}
else
{
await PreenchecmmGrupos();
groupBox1.Enabled = true;
}
});
}
else
{
if (listVPessoas.SelectedIndices.Count == 0)
{
groupBox1.Enabled = false;
}
else
{
await PreenchecmmGrupos();
groupBox1.Enabled = true;
}
}
}
private int[] id_cmbG = new int[100];
public async Task PreenchecmmGrupos()
{
try
{
var index = id_grupo[GetIndexlvG()];
var g = await bdconn.Table<Grupo>().Where(x => x.ID == index).FirstOrDefaultAsync();
var ip = id_pessoa[listVPessoas.SelectedIndices[0]];
var p = await bdconn.Table<Pessoa>().Where(x => x.ID == ip).FirstOrDefaultAsync();
var lista = await bdconn.Table<Grupo>().Where(x => x.ID != g.ID && x.Sexo == g.Sexo && x.ehEspecial == g.ehEspecial).ToListAsync();
if (p.Idade < 18)
{
lista = lista.Where(x => x.IdadeMax <= 18).ToList();
}
else
{
lista = lista.Where(x => x.IdadeMax == 100).ToList();
}
cmBGrupos.Items.Clear();
int i = 0;
foreach (var item in lista)
{
List<Pessoa> pf = await PegarPessoasDeTalGrupo(item.ID);
if (pf.Count == 0)
{
continue;
}
item.AddGeral(pf);
string pessoas = "";
for (int j = 0; j < item.Membro.Count; j++)
{
pessoas += item.Membro[j].Nome.Split(' ')[0] + ", ";
}
id_cmbG[i] = item.ID;
i++;
cmBGrupos.Items.Add(pessoas);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private int GetIndexlvG()
{
int index = 0;
if (listVGrupos.InvokeRequired)
{
listVGrupos.Invoke((MethodInvoker)delegate ()
{
index = listVGrupos.SelectedIndices[0];
});
} else
{
index = listVGrupos.SelectedIndices[0];
}
return index;
}
private async void btAplicarTroca_Click(object sender, EventArgs e)
{
if (cmBGrupos.SelectedIndex != -1)
{
for (int i = 0; i < listVPessoas.SelectedIndices.Count; i++)
{
var index = id_pessoa[listVPessoas.SelectedIndices[i]];
var pg = await bdconn.FindAsync<Pessoa_Grupo>(x => x.pessoaID == index);
pg.grupoID = id_cmbG[cmBGrupos.SelectedIndex];
int r = await bdconn.UpdateAsync(pg);
if (r == 1)
{
MessageBox.Show("Foi alterado com sucesso!", "Sucesso!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
await AtualizarListadeGrupos(false);
cmBGrupos.Items.Clear();
}
}
private async void btAlocarAuto_Click(object sender, EventArgs e)
{
var resp = MessageBox.Show("Tem certeza?", "Atenção!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (resp == DialogResult.Yes)
{
listVGrupos.Items.Clear();
listVPessoas.Items.Clear();
cmBGrupos.Items.Clear();
lblqtdP.Text = "Quantidade de pessoas: ";
await bdconn.DropTableAsync<Grupo>();
await bdconn.DropTableAsync<Pessoa_Grupo>();
await AlocarAutomaticamente();
}
}
private async void cmbFac_SelectedIndexChanged(object sender, EventArgs e)
{
var cmb = sender as ComboBox;
if (cmb.SelectedIndex == -1)
{
return;
}
try
{
int id = id_grupo[GetIndexlvG()];
var grupo = await bdconn.FindAsync<Grupo>(x => x.ID == id);
if(!cmb.Name.Contains("Aux"))
grupo.setFacilitador(id_fac[cmb.SelectedIndex].ToString());
else
grupo.setFacilitadorAuxiliar(id_facaux[cmb.SelectedIndex].ToString());
await bdconn.UpdateAsync(grupo);
}
catch (Exception)
{
}
}
}
}