public bool NewTKB(int lop, string namHoc) { try { if (namHoc.Length > 0) { DeleteTKB(lop, namHoc); using (var db = new TKBEntities()) { ThoiKhoaBieu tkb = new ThoiKhoaBieu() { Lop = db.Lops.FirstOrDefault(p => p.MaLop == lop), NamHoc = namHoc }; db.AddToThoiKhoaBieux(tkb); db.SaveChanges(); return true; } } return false; } catch (Exception ex) { return false; } }
private ThoiKhoaBieu BuildTKB(string namHoc, int buoi, List<MonHoc> dsMonHoc, List<Lop> dsLop, ref List<Phong> dsPhong, List<GiaoVien> dsGiaoVien, List<ThoiKhoaBieu> lstTKB, int l, List<List<ChiTietTKB>> arrTKB, Phong phong) { ThoiKhoaBieu tkb = new ThoiKhoaBieu(); tkb.Lop = dsLop[l]; tkb.NamHoc = namHoc; int tietDau = buoi == 1 ? 0 : Int32.Parse(ConfigurationManager.AppSettings["TongSoTietSang"]); int tietCuoi = buoi == 1 ? (tietDau + Int32.Parse(ConfigurationManager.AppSettings["TongSoTietSang"])) : (tietDau + Int32.Parse(ConfigurationManager.AppSettings["TongSoTietChieu"])); for (int k = 0; k < dsMonHoc.Count; k++) { MonHoc mh = dsMonHoc[k]; GiaoVien giaoVien = null; List<GiaoVien> dsGV_MH = GetDSGiaoVienTheoMonHoc(mh.MaMonHoc, dsGiaoVien); for (int i = 0; i < 7; i++) // 7days: from Monday to Sunday { for (int j = tietDau; j < tietCuoi; j++) { if (arrTKB[i][j].MonHoc == null) // Còn trống { if (KiemTraMonHoc(arrTKB, mh, i, j)) { ChiTietTKB ctTKB = new ChiTietTKB(); // Xếp giáo viên vào môn học ở ngày i tiết j if ((giaoVien != null && KiemTraGiaoVien(lstTKB, arrTKB, giaoVien, mh.MaMonHoc, i, j)) || GetGiaoVien(lstTKB, arrTKB, dsGV_MH, mh.MaMonHoc, i, j, out giaoVien)) { ctTKB.ThoiKhoaBieu = tkb; ctTKB.MonHoc = mh; ctTKB.GiaoVien = giaoVien; ctTKB.Phong = phong; ctTKB.TietBatDau = j; ctTKB.TietKetThuc = j; ctTKB.Thu = i; tkb.ChiTietTKBs.Add(ctTKB); arrTKB[i][j].MonHoc = mh; arrTKB[i][j].GiaoVien = giaoVien; arrTKB[i][j].Phong = phong; arrTKB[i][j].TietBatDau = j; arrTKB[i][j].TietKetThuc = j; arrTKB[i][j].Thu = i; } } } } } } return tkb; }
private void SaveTKB(List<ThoiKhoaBieu> lstTKB, List<Lop> dsLop) { using (var db = new TKBEntities()) { foreach (ThoiKhoaBieu oTKB in lstTKB) { if (oTKB.MaTKB == 0) { ThoiKhoaBieu tkb = new ThoiKhoaBieu() { Lop = db.Lops.FirstOrDefault(p => p.MaLop == oTKB.Lop.MaLop), NamHoc = oTKB.NamHoc }; db.AddToThoiKhoaBieux(tkb); db.SaveChanges(); foreach (ChiTietTKB ct in oTKB.ChiTietTKBs.ToList()) { ChiTietTKB chiTiet = new ChiTietTKB() { ThoiKhoaBieu = tkb, MonHoc = db.MonHocs.FirstOrDefault(p => p.MaMonHoc == ct.MonHoc.MaMonHoc), GiaoVien = db.GiaoViens.FirstOrDefault(p => p.MaGiaoVien == ct.GiaoVien.MaGiaoVien), Phong = db.Phongs.FirstOrDefault(p => p.MaPhong == ct.Phong.MaPhong), TietBatDau = ct.TietBatDau + 1, TietKetThuc = ct.TietKetThuc + 1, Thu = ct.Thu + 2 }; db.AddToChiTietTKBs(chiTiet); db.SaveChanges(); } } } } }