public bool AddUpdateObjectiveQuestion(IEnumerable<QuestionViewModels> models, out string ErrMsg) { using (var ctx = new EvaluationSysEntities()) { try { ErrMsg = null; if (models == null) throw new ArgumentNullException("models"); if (models.Any(a => string.IsNullOrEmpty(a.Topic))) throw new Exception("题目不能为空"); var ids = models.Where(w => w.Id != default(int)).Select(s => s.Id).ToList(); var ups = (from c in ctx.TopicRecord where ids.Contains(c.Id) && c.PID == ProjectId && c.Type == "A" select c).ToList(); ups.ForEach(e => { var model = models.FirstOrDefault(f => f.Id == e.Id); e.Heading = model.Topic ?? ""; e.Aspect = model.Aspect ?? ""; e.KeyA = model.a ?? ""; e.KeyB = model.b ?? ""; e.KeyC = model.c ?? ""; e.KeyD = model.d ?? ""; e.KeyE = model.e ?? ""; e.KeyF = model.f ?? ""; e.Weight = model.Weight; e.Competency = e.Competency ?? ""; }); List<TopicRecord> tr = new List<TopicRecord>(); foreach (var model in models.Where(w => w.Id == default(int)).ToList()) { TopicRecord topic= new TopicRecord { Type = model.type ?? "A", Heading = model.Topic ?? "", KeyA = model.a ?? "", KeyB = model.b ?? "", KeyC = model.c ?? "", KeyD = model.d ?? "", KeyE = model.e ?? "", KeyF = model.f ?? "", Aspect = model.Aspect ?? "", Competency = model.Competency ?? "", Weight = model.Weight, PID = ProjectId, CreateTime = DateTime.Now, }; ctx.Entry(topic).State = System.Data.Entity.EntityState.Added; ctx.TopicRecord.Add(topic); } var dels = (from c in ctx.TopicRecord where !ids.Contains(c.Id) && c.PID == ProjectId && c.Type == "A" select c).ToList(); dels.ForEach(e => { ctx.Entry(e).State = System.Data.Entity.EntityState.Deleted; }); ctx.TopicRecord.RemoveRange(dels); return ctx.SaveChanges() >= 0; } catch (Exception ex) { ErrMsg = ex.GetBaseException().Message; return false; } } }
public bool AddUpdateRelation(IEnumerable<string> relations, out string ErrMsg) { ErrMsg = null; try { using (var ctx = new EvaluationSysEntities()) { var dels = ctx.Typedefs.Where(w => w.PID == ProjectId && w.Type == "relation").ToList(); dels.ForEach(e => ctx.Entry(e).State = System.Data.Entity.EntityState.Deleted); ctx.Typedefs.RemoveRange(dels); foreach (string r in relations) { if (string.IsNullOrEmpty(r)) continue; ctx.Typedefs.Add(new Typedefs { Id = Guid.NewGuid(), Code = "", CreateTime = DateTime.Now, Name = r ?? "", PID = ProjectId, Type = "relation" }); } return ctx.SaveChanges() >= 0; } } catch (Exception ex) { ErrMsg = ex.GetBaseException().Message; return false; } }
public bool DeleteAssess(IEnumerable<int> Ids, out string ErrMsg) { ErrMsg = null; try { using (var ctx = new EvaluationSysEntities()) { var dels = (from c in ctx.AssessObj where Ids.Contains(c.Id) && c.PID == ProjectId select c).ToList(); var subs = (from c in ctx.SubjectiveItems where Ids.Contains(c.AssessId) && c.PID == ProjectId select c).ToList(); subs.ForEach(e => ctx.Entry(e).State = System.Data.Entity.EntityState.Deleted); dels.ForEach(e => ctx.Entry(e).State = System.Data.Entity.EntityState.Deleted); ctx.SubjectiveItems.RemoveRange(subs); ctx.AssessObj.RemoveRange(dels); return ctx.SaveChanges() >= 0; } } catch (Exception ex) { ErrMsg = ex.GetBaseException().Message; return false; } }
public bool DeleteBatch(IEnumerable<int> ids, out string ErrMsg) { try { ErrMsg = null; using (var ctx = new EvaluationSysEntities()) { var qusetion = ctx.TopicRecord.Where(w => ids.Contains(w.Id) && w.PID == ProjectId).Select(s => new { s, s.SubjectiveItems }).ToList(); if (qusetion != null) { qusetion.ForEach(e => { ctx.Entry(e.s).State = System.Data.Entity.EntityState.Deleted; ctx.SubjectiveItems.RemoveRange(e.SubjectiveItems); }); ctx.TopicRecord.RemoveRange(qusetion.Select(s=>s.s)); } return ctx.SaveChanges() >= 0; } } catch (Exception ex) { ErrMsg = ex.GetBaseException().Message; return false; } }
public bool DeletePaper(IEnumerable<int> Ids, out string ErrMsg) { ErrMsg = null; try { if (Ids == null) throw new ArgumentNullException("Ids"); using (var ctx = new EvaluationSysEntities()) { var dels = (from c in ctx.TestPage where Ids.Contains(c.Id) && c.PID == ProjectId select c).ToList(); var ass = (from c in ctx.AssessObj where Ids.Contains(c.TestPageID ?? 1) && c.PID == ProjectId select c).ToList(); var aids = ass.Select(s => s.Id).ToList(); var subs = (from c in ctx.SubjectiveItems where aids.Contains(c.AssessId) && c.PID == ProjectId select c).ToList(); subs.ForEach(e => ctx.Entry(e).State = System.Data.Entity.EntityState.Deleted); ass.ForEach(e => ctx.Entry(e).State = System.Data.Entity.EntityState.Deleted); dels.ForEach(e => ctx.Entry(e).State = System.Data.Entity.EntityState.Deleted); ctx.SubjectiveItems.RemoveRange(subs); ctx.AssessObj.RemoveRange(ass); ctx.TestPage.RemoveRange(dels); return ctx.SaveChanges() >= 0; } } catch (Exception ex) { ErrMsg = ex.GetBaseException().Message; return false; } }