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 SetConfigs(SettingsViewModels settings, out string ErrorMsg) { ErrorMsg = null; try { using(var ctx = new EvaluationSysEntities()) { var query = (from c in ctx.SettingConfig where c.ConfigStatus >= 0 select c).ToList(); foreach(PropertyInfo p in typeof(SettingsViewModels).GetProperties()) { var setting = query.FirstOrDefault(f => f.Name == p.Name); if(setting == null) { ctx.SettingConfig.Add(new SettingConfig { Id = Guid.NewGuid(), ConfigStatus = 1, CreateTime = DateTime.Now, Name = p.Name, Value = Convert.ToString(p.GetValue(settings)), OperateTime = DateTime.Now }); } else { setting.Value = Convert.ToString(p.GetValue(settings)); setting.OperateTime = DateTime.Now; } } return ctx.SaveChanges() >= 0; } } catch(Exception ex) { ErrorMsg = 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 UpdateAssessAttr(AssessViewModels model, IEnumerable<string> PropertyNames, out string ErrMsg) { ErrMsg = null; try { using(var ctx = new EvaluationSysEntities()) { var query = (from c in ctx.AssessObj where c.Id == model.AssessId select c).FirstOrDefault(); if(query == null) { ErrMsg = "不正确的参数"; return false; } foreach(string name in PropertyNames) { var p = typeof(AssessViewModels).GetProperty(name); if (p == null) continue; var pi = typeof(AssessObj).GetProperty(name); if (pi != null) pi.SetValue(query, p.GetValue(model).ConvertObject(pi.PropertyType)); } return ctx.SaveChanges() >= 0; } } catch (Exception ex) { ErrMsg = ex.GetBaseException().Message; return false; } }
public bool ResetAssessStatus(IEnumerable<int> AssessIds, out string ErrMsg) { ErrMsg = null; try { using(var ctx = new EvaluationSysEntities()) { var assess = (from c in ctx.AssessObj where AssessIds.Contains(c.Id) && c.PID == ProjectId && c.Status != 0 select c).ToList(); if(assess.Count == 0) { ErrMsg = "不存在有效的评估对象(正在进行的评估不能被重置)"; return false; } assess.ForEach(e => { e.Status = -1; e.EndTime = DateTime.Now; }); return ctx.SaveChanges() >= 0; } } catch (Exception ex) { ErrMsg = ex.GetBaseException().Message; return false; } }
public bool AddUpdateAssess(AssessViewModels model, out int AssessId, out string ErrMsg) { ErrMsg = null; AssessId = model.AssessId ?? 0; try { AssessObj obj = null; DateTime deadline = DateTime.Parse("1900-1-1"); using (var ctx = new EvaluationSysEntities()) { var setting = (from c in ctx.SettingConfig where c.Name == "DeadLine" select c.Value).FirstOrDefault(); var userinfo = ctx.UserInfo.FirstOrDefault(f => f.Id == model.UserId && f.PID == ProjectId); if (userinfo == null) throw new Exception("不正确的用户ID"); DateTime.TryParse(setting, out deadline); if (!model.AssessId.HasValue) { obj = new AssessObj { IsUserID = model.IsUserId, UserID = model.UserId, SysEndTime = deadline, Bind = model.Relation, TestPageID = model.PaperId, Status = -1, PID = ProjectId, LastName = userinfo.LastName, FirstName = userinfo.FirstName, Frequency = "", BindPageSave = "" }; ctx.AssessObj.Add(obj); } else { var update = (from c in ctx.AssessObj where c.Id == model.AssessId && c.PID == ProjectId select c).FirstOrDefault(); if (update != null) { update.IsUserID = model.IsUserId; update.UserID = model.UserId; update.Bind = model.Relation; update.SysEndTime = deadline; update.TestPageID = model.PaperId; } } int ret = ctx.SaveChanges(); if (!model.AssessId.HasValue) AssessId = obj.Id; return ret >= 0; } } catch (Exception ex) { ErrMsg = ex.GetBaseException().Message; return false; } }
public bool AnswersSave(QuestionnaireViewModels models, out string ErrMsg) { ErrMsg = null; try { using (var ctx = new EvaluationSysEntities()) { var assess = ctx.AssessObj.FirstOrDefault(f => f.Id == models.AssessId && f.PID == ProjectId && f.UserID == models.UserId && f.Status >= -1); if (assess == null) throw new ArgumentException("Invaild Parameter", nameof(models.AssessId)); List<string> tmpans = new List<string>(); List<int> tmpsub = new List<int>(); if (!string.IsNullOrEmpty(assess.KeyArr)) tmpans.AddRange(assess.KeyArr.Split(',')); if (!string.IsNullOrEmpty(assess.SubjectArr)) tmpsub.AddRange(assess.SubjectArr.Split(',').ToList().ConvertAll(int.Parse)); if (models.Answers == null) return true; foreach (var ans in models.Answers) { if (ans.TopicType == "A") { var objcet = tmpans.FirstOrDefault(w => int.Parse(w.Split('-')[0]) == ans.TopicId); if (objcet != null) tmpans.Remove(objcet); tmpans.Add(ans.Answer); } else { if (!tmpsub.Contains(ans.TopicId)) tmpsub.Add(ans.TopicId); var subject = ctx.TopicRecord.FirstOrDefault(f => f.Id == ans.TopicId && f.PID == ProjectId)?.SubjectiveItems.FirstOrDefault(f => f.AssessId == assess.Id); if (subject == null) { ctx.SubjectiveItems.Add(new SubjectiveItems { TopicId = ans.TopicId, PID = ProjectId, AssessId = assess.Id, CreateTime = DateTime.Now, Answers = ans.Answer, }); } else { subject.Answers = ans.Answer; } } } if (models.TotalPage == models.Offset) assess.Status = 1; assess.EndTime = DateTime.Now; tmpans = tmpans.OrderBy(o => int.Parse(o.Split('-')[0])).ToList(); assess.KeyArr = string.Join(",", tmpans); assess.SubjectArr = string.Join(",", tmpsub); return ctx.SaveChanges() >= 0; } } catch (Exception ex) { ErrMsg = errmsg = ex.GetBaseException().Message; return false; } }
public bool SaveImportModel(UserImportModels model, out string ErrMsg) { ErrMsg = null; try { if (string.IsNullOrEmpty(model.UserNo) || string.IsNullOrEmpty(model.UserName)) { ErrMsg = "用户编号或用户姓名不能为空"; return false; } if (CheckUserNoRepeat(model.UserNo)) throw new Exception("包含重复的用户编号"); using (var ctx = new EvaluationSysEntities()) { ctx.UserInfo.Add(new UserInfo { Id = Guid.NewGuid(), UserName = model.UserName, FirstName = model.FirstName, LastName = model.LastName, BM = model.Department, ZW = model.Position, Email = model.EmailAddress, OperatrTime = DateTime.Now, UserNum = model.UserNo, PID = ProjectId, }); return ctx.SaveChanges() > 0; } } catch (Exception ex) { ErrMsg = ex.GetBaseException().Message; return false; } }
public bool SendEmail(Guid UserId, out string ErrMsg) { ErrMsg = null; DateTime deadline = DateTime.Now.AddDays(30); try { using (var ctx = new EvaluationSysEntities()) { var setting = (from c in ctx.SettingConfig where c.Name == "DeadLine" select c.Value).FirstOrDefault(); DateTime.TryParse(setting, out deadline); var user = (from c in ctx.UserInfo where c.Id == UserId && c.PID == ProjectId select c).FirstOrDefault(); if (user == null) { ErrMsg = "不合法的用户"; return false; } var assess = (from c in ctx.AssessObj where c.UserID == UserId && c.PID == ProjectId select c).ToList(); string address = user.Email; string cc = ctx.SettingConfig.FirstOrDefault(f => f.Name == "CCAddress")?.Value; if (string.IsNullOrEmpty(address)) { ErrMsg = "未配置或错误的用户邮箱"; return false; } Guid id = Guid.Parse("843A5405-41B8-4F3C-A0B6-2AF5F953F851"); var template = (from c in ctx.EmailTemplate where c.Id == id select c).FirstOrDefault(); if (template == null) { ErrMsg = "未配置或找不到邮件模板"; return false; } string html = template.EmailContent.Replace("{userId}", user.Id.ToString("N")).Replace("{userName}", user.UserName); html = html.Replace("{deadLine}", deadline.ToString("yyyy年MM月dd日")); if (!emailUtils.SendEmail(address, template.EmailHead, html, out ErrMsg, cc)) { assess.ForEach(a => { a.EndTime = DateTime.Now; a.Status = -10; }); ctx.SaveChanges(); return false; } assess.ForEach(a => { a.EndTime = DateTime.Now; a.Status = 10; }); ctx.SaveChanges(); } return true; } catch (Exception ex) { ErrMsg = ex.GetBaseException().Message; return false; } }
public bool UpdateQuestion(QuestionViewModels model, out string ErrMsg) { try { ErrMsg = null; using (var ctx = new EvaluationSysEntities()) { var entity = ctx.TopicRecord.FirstOrDefault(a => a.Id == model.Id && a.PID == ProjectId); if (entity == null) throw new Exception("No such TopicId"); if (string.IsNullOrEmpty(model.Topic)) throw new Exception("题目不能为空"); entity.Heading = model.Topic; entity.KeyA = model.a ?? ""; entity.KeyB = model.b ?? ""; entity.KeyC = model.c ?? ""; entity.KeyD = model.d ?? ""; entity.KeyE = model.e ?? ""; entity.KeyF = model.f ?? ""; entity.Type = model.type ?? "A"; entity.Weight = model.Weight; entity.Aspect = model.Aspect ?? ""; entity.Competency = model.Competency ?? ""; return ctx.SaveChanges() >= 0; } } catch (DbEntityValidationException e) { string error = ""; foreach (var i in e.EntityValidationErrors) { foreach (var j in i.ValidationErrors) { error = j.PropertyName + ":" + j.ErrorMessage; } } ErrMsg = error; return false; } 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 DeleteQuestion(int id, out string ErrMsg) { try { ErrMsg = null; using (var ctx = new EvaluationSysEntities()) { var qusetion = ctx.TopicRecord.FirstOrDefault(f => f.Id == id && f.PID == ProjectId); if (qusetion != null) { ctx.TopicRecord.Remove(qusetion); } 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; } }
public bool AppUpdatePaper(PaperViewModels paper, out string ErrMsg) { ErrMsg = null; try { if (string.IsNullOrEmpty(paper.PaperName)) { throw new ArgumentNullException("试卷名称不能为空", "PaperName"); } using (var ctx = new EvaluationSysEntities()) { if (paper.PaperId == null || paper.PaperId == default(int)) { ctx.TestPage.Add(new TestPage { Name = paper.PaperName, TopicArr = paper.TopicArrs, CreateTime = DateTime.Now, PID = ProjectId }); } else { var query = ctx.TestPage.FirstOrDefault(f => f.Id == paper.PaperId); query.Name = paper.PaperName; query.TopicArr = paper.TopicArrs; query.CreateTime = DateTime.Now; } 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 SaveImportModel(AssessImportModels models, out string ErrMsg) { ErrMsg = null; try { DateTime deadline = DateTime.Now; using (var ctx = new EvaluationSysEntities()) { if(string.IsNullOrEmpty(models.UserNo)|| string.IsNullOrEmpty(models.IsUserNo)) { ErrMsg = "用户编号不能为空"; return false; } var setting = (from c in ctx.SettingConfig where c.Name == "DeadLine" select c.Value).FirstOrDefault(); DateTime.TryParse(setting, out deadline); var user = (from c in ctx.UserInfo where c.PID == ProjectId && c.UserNum == models.UserNo select c).FirstOrDefault(); var isuser = (from c in ctx.UserInfo where c.PID == ProjectId && c.UserNum == models.IsUserNo select c).FirstOrDefault(); if (user == null || isuser == null) { ErrMsg = "错误用户编号"; return false; } var paper = (from c in ctx.TestPage where c.PID == ProjectId && c.Name == models.PaperName select c.Id).FirstOrDefault(); if (paper == default(int)) { ErrMsg = "不正确的试卷名称"; return false; } ctx.AssessObj.Add(new AssessObj { IsUserID = isuser.Id, UserID = user.Id, SysEndTime = deadline, Bind = models.UserRelation, TestPageID = paper, Status = -1, PID = ProjectId, LastName = user.LastName, FirstName = user.FirstName, Frequency = "", BindPageSave = "" }); return ctx.SaveChanges() > 0; } } catch (Exception ex) { ErrMsg = ex.GetBaseException().Message; return false; } }
public bool AddUpdateSubjectiveQuestion(IEnumerable<QuestionViewModels> models, out string ErrMsg) { using (var ctx = new EvaluationSysEntities()) { try { ErrMsg = null; if (models == null) throw new ArgumentNullException("models"); 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 == "B" select c).ToList(); ups.ForEach(e => { var model = models.FirstOrDefault(f => f.Id == e.Id); e.Heading = model.Topic ?? ""; e.Aspect = model.Aspect ?? ""; e.Weight = model.Weight; e.Competency = model.Competency ?? ""; }); List<TopicRecord> tr = new List<TopicRecord>(); foreach (QuestionViewModels model in models.Where(w => w.Id == default(int)).ToList()) { if (string.IsNullOrEmpty(model.Topic)) continue; tr.Add(new TopicRecord { Type = model.type ?? "B", Heading = model.Topic, KeyA = "", KeyB = "", KeyC = "", KeyD = "", KeyE = "", KeyF = "", Aspect = "", Competency = "", Weight = 0, PID = ProjectId, CreateTime = DateTime.Now, }); } ctx.TopicRecord.AddRange(tr); return ctx.SaveChanges() >= 0; } catch (Exception ex) { ErrMsg = ex.GetBaseException().Message; return false; } } }
public bool AddUpdateEmailTemplate(EmailTempletViewModels model, out Guid TID, out string ErrMsg) { if (model == null) throw new ArgumentNullException("model"); try { ErrMsg = null; TID = Guid.NewGuid(); using (var ctx = new EvaluationSysEntities()) { if (model.TID == Guid.Empty) { ctx.EmailTemplate.Add(new EmailTemplate { Id = TID, EmailHead = model.EmailHead, EmailContent = model.EmailTemplet, CreateTime = DateTime.Now }); } else { var query = ctx.EmailTemplate.FirstOrDefault(f => f.Id == model.TID); if (query == null) throw new Exception("不正确的模板ID"); query.EmailHead = model.EmailHead ?? ""; query.EmailContent = model.EmailTemplet ?? ""; query.CreateTime = DateTime.Now; } return ctx.SaveChanges() >= 0; } } catch (Exception ex) { TID = Guid.Empty; ErrMsg = ex.GetBaseException().Message; return false; } }
public bool AddUpdateUserInfo(UserViewModels model, out Guid UserId, out string ErrMsg) { ErrMsg = null; UserId = Guid.NewGuid(); try { if(CheckUserNoRepeat(model.UserNo)) { ErrMsg = "项目内用户编号不可重复"; return false; } using (var ctx = new EvaluationSysEntities()) { if (model.UserId == default(Guid)) { ctx.UserInfo.Add(new UserInfo { Id = UserId, BM = model.Department, ZW = model.Position, Email = model.EmailAddr, UserName = model.UserName, UserNum = model.UserNo, PID = ProjectId, OperatrTime = DateTime.Now, }); } else { UserId = model.UserId; var user = (from c in ctx.UserInfo where c.Id == model.UserId && c.PID == ProjectId && c.ProjectMng.Status >= 0 select c).FirstOrDefault(); if (user != null) { user.UserName = model.UserName ?? ""; user.UserNum = model.UserNo ?? ""; user.Email = model.EmailAddr; user.BM = model.Department ?? ""; user.ZW = model.Position ?? ""; user.OperatrTime = DateTime.Now; } } return ctx.SaveChanges() >= 0; } } catch (Exception ex) { ErrMsg = ex.GetBaseException().Message; return false; } }