public static void 添加试卷设置(试卷设置 试卷设置) { LoveKaoExamEntities db = new LoveKaoExamEntities(); if (试卷设置.设置类型 == 1) { //未设置成练习才能设置成考试 Guid outsideId = db.试卷内容表.FirstOrDefault(a=>a.ID==试卷设置.试卷内容ID).试卷外部信息ID; List<Guid> listContentId = db.试卷内容表.Where(a=>a.试卷外部信息ID==outsideId).Select(a=>a.ID).ToList(); if (db.练习设置表.Any(a => listContentId.Contains(a.试卷内容ID) && a.是否删除 == false) == true) { throw new Exception("该试卷已设置成练习,若要设置成考试,请先删除练习设置!"); } //只有出卷人才能设置 试卷内容 testContent = 试卷内容.试卷内容查询.Where(a => a.ID == 试卷设置.试卷内容ID).First(); if (testContent.提交人ID != 试卷设置.设置人ID) { throw new Exception("您无权设置该试卷!"); } //开始时间不能大于结束时间 if (试卷设置.考试开始时间 >= 试卷设置.考试结束时间) { throw new Exception("考试开始时间不能大于考试结束时间!"); } 考试设置表 examSet = new 考试设置表(); examSet.ID = Guid.NewGuid(); examSet.及格条件 = 试卷设置.及格条件; examSet.考试结束时间 = 试卷设置.考试结束时间; examSet.考试开始时间 = 试卷设置.考试开始时间; examSet.考试时长 = 试卷设置.考试时长; examSet.试卷内容ID = 试卷设置.试卷内容ID; examSet.是否公布考试结果 = 试卷设置.是否公布考试结果; examSet.设置人ID = 试卷设置.设置人ID; examSet.设置时间 = DateTime.Now; examSet.是否删除 = false; List<考生范围表> listArea = new List<考生范围表>(); foreach (Guid examineeId in 试卷设置.考生ID集合) { 考生范围表 area = new 考生范围表(); area.ID = Guid.NewGuid(); area.考生ID = examineeId; area.相关ID = examSet.ID; listArea.Add(area); } db.考试设置表.AddObject(examSet); foreach (var area in listArea) { db.考生范围表.AddObject(area); } db.SaveChanges(); } else { //若试卷已设置成考试,则要在考试结束后或删除考试设置才能设置成练习 Guid outsideId = db.试卷内容表.FirstOrDefault(a=>a.ID==试卷设置.试卷内容ID).试卷外部信息ID; List<Guid> listContentId = db.试卷内容表.Where(a=>a.试卷外部信息ID==outsideId).Select(a => a.ID).ToList(); 考试设置表 examSet = db.考试设置表.Where(a=>listContentId.Contains(a.试卷内容ID)&& a.是否删除 == false) .OrderByDescending(a => a.考试结束时间).FirstOrDefault(); if (examSet != null) { if (examSet.考试结束时间 > DateTime.Now) { throw new Exception("该试卷已设置成考试,若要设置成练习,需在考试结束时间以后或删除考试设置!"); } } //只有出卷人才能设置 试卷内容 testContent = 试卷内容.试卷内容查询.Where(a => a.ID == 试卷设置.试卷内容ID).First(); if (testContent.提交人ID != 试卷设置.设置人ID) { throw new Exception("您无权设置该试卷!"); } //若该试卷以前有设置成练习的记录,且被删除了,则更新设置,并恢复删除,若没有,则添加新的设置 练习设置表 dbExerciseSet = db.练习设置表.FirstOrDefault(a=>a.试卷内容ID==试卷设置.试卷内容ID); if (dbExerciseSet == null) { 练习设置表 exerciseSet = new 练习设置表(); exerciseSet.及格条件 = 试卷设置.及格条件; exerciseSet.考试时长 = 试卷设置.考试时长; exerciseSet.设置人ID = 试卷设置.设置人ID; exerciseSet.设置时间 = DateTime.Now; exerciseSet.试卷内容ID = 试卷设置.试卷内容ID; exerciseSet.是否删除 = false; db.练习设置表.AddObject(exerciseSet); List<考生范围表> listArea = new List<考生范围表>(); foreach (Guid examineeId in 试卷设置.考生ID集合) { 考生范围表 area = new 考生范围表(); area.ID = Guid.NewGuid(); area.考生ID = examineeId; area.相关ID = exerciseSet.试卷内容ID; listArea.Add(area); } foreach (var area in listArea) { db.考生范围表.AddObject(area); } } else { dbExerciseSet.及格条件 = 试卷设置.及格条件; dbExerciseSet.考试时长 = 试卷设置.考试时长; dbExerciseSet.设置时间 = DateTime.Now; dbExerciseSet.是否删除 = false; 考生范围.更新考生范围(试卷设置.试卷内容ID, 试卷设置.考生ID集合, db); } db.SaveChanges(); } }
/// <summary> /// 用于向 考生范围表 EntitySet 添加新对象的方法,已弃用。请考虑改用关联的 ObjectSet<T> 属性的 .Add 方法。 /// </summary> public void AddTo考生范围表(考生范围表 考生范围表) { base.AddObject("考生范围表", 考生范围表); }
public static void 更新考生范围(Guid 相关ID, List<Guid> 考生ID集合, LoveKaoExamEntities db) { List<考生范围表> listOldArea = db.考生范围表.Where(a=>a.相关ID==相关ID).ToList(); List<Guid> listOldAreaId = listOldArea.Select(a => a.考生ID).ToList(); //没变,则不作任何操作,若有变,则先删除原来的,再插入新的 List<Guid> listNewId = listOldAreaId.Except(考生ID集合).ToList(); if (listOldAreaId.Count != 考生ID集合.Count || listOldAreaId.Count == 考生ID集合.Count && listNewId.Count != 0) { foreach (var oldArea in listOldArea) { db.考生范围表.DeleteObject(oldArea); } List<考生范围表> listNewArea = new List<考生范围表>(); foreach (Guid examineeId in 考生ID集合) { 考生范围表 area = new 考生范围表(); area.ID = Guid.NewGuid(); area.相关ID = 相关ID; area.考生ID = examineeId; listNewArea.Add(area); } foreach (var newArea in listNewArea) { db.考生范围表.AddObject(newArea); } } }
/// <summary> /// 创建新的 考生范围表 对象。 /// </summary> /// <param name="id">ID 属性的初始值。</param> /// <param name="相关ID">相关ID 属性的初始值。</param> /// <param name="考生ID">考生ID 属性的初始值。</param> public static 考生范围表 Create考生范围表(global::System.Guid id, global::System.Guid 相关ID, global::System.Guid 考生ID) { 考生范围表 考生范围表 = new 考生范围表(); 考生范围表.ID = id; 考生范围表.相关ID = 相关ID; 考生范围表.考生ID = 考生ID; return 考生范围表; }