public void GetCampeonatoGrupo(int IDInscrito, int IDInscritoOld) { try { string sql = ""; SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder { DataSource = "SQL5028.SmarterASP.NET", UserID = "DB_9D81D5_societypro_admin", Password = "******", InitialCatalog = "DB_9D81D5_societypro" }; using (SqlConnection connection = new SqlConnection(builder.ConnectionString)) { connection.Open(); sql = "select * from [dbo].[CampeonatoGrupo] where IDInscrito = " + IDInscritoOld; using (SqlCommand command = new SqlCommand(sql, connection)) { IDataReader dtreader = command.ExecuteReader(); DataTable dtresult = new DataTable(); dtresult.Load(dtreader); dtreader.Close(); for (int i = 0; i < dtresult.Rows.Count; i++) { GroupChampionship c = new GroupChampionship { Active = true, RegisterDate = Convert.ToDateTime(dtresult.Rows[i]["dDataCadastro"].ToString()), GroupId = (Group)Convert.ToInt32(dtresult.Rows[i]["IDGrupo"].ToString()), SubscriptionId = IDInscrito }; Db.GroupChampionships.Add(c); } Db.SaveChanges(); } } } catch (SqlException e) { e.Message.ToString(); } }
public void AutomaticGroupChampionship(int championshipId, int quantity) { var result = _groupChampionshipRepository.GetAll().Result; var resultSubscription = _subscriptionRepository.GetAll().Result; var groupChampionship = result.Where(p => p.Subscription.ChampionshipId == championshipId); int quantityGroupChampionship = groupChampionship.Count(); if (quantityGroupChampionship > 0) { throw new Exception(); } Random randNum = new Random(); int quantityTeam = 0; int quantityGroup = 0; int count = 0; var subscriptions = resultSubscription.Where(p => p.ChampionshipId == championshipId); var quantitySubscription = subscriptions.Count(); int[] times = new int[quantitySubscription]; foreach (var e in subscriptions) { times[count] = e.Id; count++; } quantityTeam = times.Count(); if (quantityTeam % quantity == 0) { quantityGroup = quantityTeam / quantity; } else { quantityGroup = (quantityTeam / quantity) + 1; } count = 0; for (int i = 1; i <= quantityGroup; i++) { for (int j = 1; j <= quantity; j++) { GroupChampionship groupChampionshipAdd = new GroupChampionship { SubscriptionId = times[randNum.Next(0, quantityTeam - 1)], GroupId = (Group)i, RegisterDate = DateTime.Now }; var championshipRepositoryCount = result.Where(p => p.SubscriptionId == groupChampionshipAdd.SubscriptionId).Count(); if (championshipRepositoryCount == 0) { _groupChampionshipRepository.Add(groupChampionshipAdd); } else { if (i == quantityGroup) { for (int z = 0; z < quantityTeam; z++) { GroupChampionship lastGroupChampionship = new GroupChampionship { SubscriptionId = times[z], GroupId = (Group)i, RegisterDate = DateTime.Now }; var lastChampionshipRepositoryCount = result.Where(p => p.SubscriptionId == lastGroupChampionship.SubscriptionId).Count(); if (lastChampionshipRepositoryCount == 0) { _groupChampionshipRepository.Add(lastGroupChampionship); } } return; } j -= 1; } } } }