private void saveKlassPredmet() { foreach (DataGridViewRow dgvu in dataGridViewUschitelKlass.Rows) { if (Convert.ToInt32(dgvu.Cells[4].Value) == 0) { uchitelKlassPredmet = new UchitelKlassPredmet(); } else { int id = Convert.ToInt32(dgvu.Cells[4].Value); uchitelKlassPredmet = DBobjects.Entities.UchitelKlassPredmet.FirstOrDefault(p => p.ID_UchitelKlassPredmet == id); } if (Convert.ToBoolean(dgvu.Cells[3].Value) == true) { uchitelKlassPredmet.ID_Uchitel = UCHITEL.ID_Uchitel; uchitelKlassPredmet.ID_KlassPredmet = Convert.ToInt32(dgvu.Cells[0].Value); if (DBobjects.Entities.UchitelKlassPredmet.Where(p => p.ID_UchitelKlassPredmet == uchitelKlassPredmet.ID_UchitelKlassPredmet).Count() == 0) { DBobjects.Entities.UchitelKlassPredmet.Add(uchitelKlassPredmet); } DBobjects.Entities.SaveChanges(); } else if (uchitelKlassPredmet.ID_UchitelKlassPredmet != 0 && Convert.ToBoolean(dgvu.Cells[3].Value) == false) { DeleteKabinetPredmet(); } } }
private void GenerateSchedule() { DBobjects.Entities.Clear(); int maxIdKabinet = DBobjects.Entities.Kabinet.OrderByDescending(p => p.ID_Kabinet).FirstOrDefault().ID_Kabinet + 1; int maxIdKlass = DBobjects.Entities.Klass.OrderByDescending(p => p.ID_Klass).FirstOrDefault().ID_Klass + 1; int maxIdUchitel = DBobjects.Entities.Uchitel.OrderByDescending(p => p.ID_Uchitel).FirstOrDefault().ID_Uchitel + 1; int[,,] ZanKabinet = new int [maxIdKabinet, 6, 7]; int[,,] ZanKlass = new int[maxIdKlass, 6, 7]; int[,,] ZanUchitel = new int[maxIdUchitel, 6, 7]; int[,] FreeKabs = new int[30, 2]; Klass [] klasses = DBobjects.Entities.Klass.ToArray(); Random RANDMASS = new Random(); for (int i = klasses.Length - 1; i >= 1; i--) { int j = RANDMASS.Next(i + 1); Klass tmpM = klasses[j]; klasses[j] = klasses[i]; klasses[i] = tmpM; } int Lk = klasses.Count(); int ik = 0; List <Predmet> predmets = new List <Predmet>(); while ((ik < Lk)) { var tmp = klasses[ik]; List <KlassPredmet> klassPr = DBobjects.Entities.KlassPredmet.Where(p => p.ID_Klass == tmp.ID_Klass).ToList(); foreach (KlassPredmet klPr in klassPr) { predmets.Add(klPr.Predmet); } predmets = predmets.Distinct().ToList();//список предметов для класса int Lp = predmets.Count(); for (int ip = 0; ip < Lp; ip++) { var tmpP = predmets[ip]; if (DBobjects.Entities.UchitelKlassPredmet.Where(p => p.KlassPredmet.ID_Predmet == tmpP.ID_Predmet && p.KlassPredmet.Klass.ID_Klass == tmp.ID_Klass).Count() > 0) { UchitelKlassPredmet klascPrUch = DBobjects.Entities.UchitelKlassPredmet.FirstOrDefault(p => p.KlassPredmet.ID_Predmet == tmpP.ID_Predmet && p.KlassPredmet.Klass.ID_Klass == tmp.ID_Klass); int uc = klascPrUch.ID_Uchitel; int N = klascPrUch.KlassPredmet.UrokovVNedelyu; List <Kabinet> kabinets = new List <Kabinet>(); for (int i = 1; i <= N; i++) { List <KabinetPredmet> kabPredm = DBobjects.Entities.KabinetPredmet.Where(p => p.ID_Predmet == tmpP.ID_Predmet).ToList(); foreach (KabinetPredmet kabPr in kabPredm) { kabinets.Add(kabPr.Kabinet); } int Lkab = kabinets.Count(); for (int ikab = 0; ikab < Lkab;) { var tmpKab = kabinets[ikab]; int fk = -1; for (int iwd = 1; iwd <= 5; iwd++) { for (int iur = 1; iur <= 6; iur++) { if (ZanKabinet[tmpKab.ID_Kabinet, iwd, iur] == 0 && ZanKlass[tmp.ID_Klass, iwd, iur] == 0 && ZanUchitel[uc, iwd, iur] == 0) { fk = fk + 1; FreeKabs[fk, 0] = iwd; FreeKabs[fk, 1] = iur; } } } if (fk >= 0) { Random rand = new Random(); var rndwu = rand.Next(fk); var wd = FreeKabs[rndwu, 0]; var ur = FreeKabs[rndwu, 1]; ZanKabinet[tmpKab.ID_Kabinet, wd, ur] = 1; ZanKlass[tmp.ID_Klass, wd, ur] = 1; ZanUchitel[uc, wd, ur] = 1; Urok urok = new Urok(); urok.ID_Kabinet = tmpKab.ID_Kabinet; urok.ID_Klass = tmp.ID_Klass; urok.ID_Predmet = tmpP.ID_Predmet; urok.ID_Uchitel = uc; urok.Weekday = wd; urok.Nomer_uroka = ur; DBobjects.Entities.Urok.Add(urok); DBobjects.Entities.SaveChanges(); } break; } } } } ik++; } }