private PollingView GetPollingViewPrivate(PollingEntity polling) { var pollingview = (PollingView) new PollingView().ConvertAPIModel(polling); _pollingView = pollingview; var pqv = new List <PollingQuestionView>(); if (polling.PollingQuestions != null) { var pollingQuestion = polling.PollingQuestions.Where(a => a.IsDeleted != true).OrderBy(a => a.OrderIndex).ThenBy(a => a.Id).ToList(); foreach (PollingQuestionEntity poll in pollingQuestion) { var pollingQuestionView = (PollingQuestionView) new PollingQuestionView().ConvertAPIModel(poll); var pov = poll.PollingOptionEntities.Where(a => a.IsDeleted != true).Select( polloption => (PollingOptionView) new PollingOptionView().ConvertAPIModel(polloption)) .OrderBy(a => a.OrderIndex).ThenBy(a => a.Id).ToList(); pollingQuestionView.PollingOptionEntities = pov; pqv.Add(pollingQuestionView); } } pollingview.PollingQuestions = pqv; return(pollingview); }
/// <summary> /// 某个polling所有question正确人数和参与人数 /// </summary> /// <param name="polling"></param> /// <returns>question列表,每个question一行</returns> private List <PollingResultCustomView> PollingResultPerson(PollingView polling) { //var polling = Repository.Entities.FirstOrDefault(a => a.Id == pollingId); var pollingResultPersons = new List <PollingResultCustomView>(); //var pollingAnswers = _pollingResultService.GetList(polling.Id); var pollingAnswers = _pollingResultService.GetBatchResultsByPollingId(polling.Id, 0, null, null); var pollingResult = PollingResult(polling.Id, pollingAnswers, polling); // 循环得到每个question的答题情况 foreach (var question in polling.PollingQuestions) { var pollingResultPerson = new PollingResultCustomView(); int person = 0; int answerPersons = 0; foreach (var questionresult in pollingResult) { if (questionresult.CustomStatus.Equals("正确") && questionresult.QuestionId == question.Id) { person += 1; answerPersons += 1; } if (questionresult.CustomStatus.Equals("错误") && questionresult.QuestionId == question.Id) { answerPersons += 1; } } pollingResultPerson.QuestionId = question.Id; pollingResultPerson.RightPersons = person; pollingResultPerson.answerPersons = answerPersons; pollingResultPersons.Add(pollingResultPerson); } return(pollingResultPersons); }
/// <summary> /// 适用于后台编辑页面(纯polling信息) /// </summary> /// <param name="pollingId"></param> /// <returns></returns> public PollingView GetPollingView(int pollingId) { var polling = Repository.Entities.Where(a => a.IsDeleted != true && a.Id == pollingId).Include(x => x.PollingQuestions.Select(y => y.PollingOptionEntities)).FirstOrDefault(); if (polling != null) { var pollingview = (PollingView) new PollingView().ConvertAPIModel(polling); _pollingView = pollingview; var pqv = new List <PollingQuestionView>(); if (polling.PollingQuestions != null) { var pollingQuestion = polling.PollingQuestions.Where(a => a.IsDeleted != true).OrderBy(a => a.OrderIndex).ThenBy(a => a.Id).ToList(); foreach (PollingQuestionEntity poll in pollingQuestion) { var pollingQuestionView = (PollingQuestionView) new PollingQuestionView().ConvertAPIModel(poll); var pov = poll.PollingOptionEntities.Where(a => a.IsDeleted != true).Select( polloption => (PollingOptionView) new PollingOptionView().ConvertAPIModel(polloption)) .OrderBy(a => a.OrderIndex).ThenBy(a => a.Id).ToList(); pollingQuestionView.PollingOptionEntities = pov; pqv.Add(pollingQuestionView); } } pollingview.PollingQuestions = pqv; return(pollingview); } return(default(PollingView)); }
/// <summary> /// 前台投票前页面 /// </summary> /// <param name="query"></param> /// <param name="lillyid"></param> /// <param name="pollingId"></param> /// <returns></returns> public PollingView GetPollingDetailView(int pollingId) { return(_cacheManager.Get(string.Format(CachePrefix, pollingId), 1, () => { PollingView pollingview = GetPollingView(pollingId); if (pollingview.PollingQuestions != null && pollingview.PollingQuestions.Count > 0) { pollingview.PollingQuestions.ToList().ForEach(x => { if (x.Type == 1) { x.optionName = "(单选)"; } else if (x.Type == 0 || x.Type == x.PollingOptionEntities.Count) { x.optionName = "(多选)"; } else if (x.Type < x.PollingOptionEntities.Count) { x.optionName = "(最多选" + x.Type + "项)"; } else if (x.Type == 999) { x.optionName = "文本"; } }); } if (pollingview.Type == 1) { pollingview.Title = "有奖问答"; } else if (pollingview.Type == 2) { pollingview.Title = "投票"; } if (pollingview.StartDateTime > DateTime.Now) { pollingview.StatusName = "未开始"; } else if (pollingview.EndDateTime > DateTime.Now && pollingview.StartDateTime < DateTime.Now) { pollingview.StatusName = "提交"; } else if (pollingview.EndDateTime < DateTime.Now) { pollingview.StatusName = "已结束"; } return pollingview; })); }
public PollingView GetPollingDetailView(Func <PollingEntity, bool> query, string lillyid = null) { PollingView pollingview = GetPollingView(query, lillyid); if (pollingview.PollingQuestions != null && pollingview.PollingQuestions.Count > 0) { pollingview.PollingQuestions.ToList().ForEach(x => { if (x.Type == 1) { x.optionName = "(单选)"; } else if (x.Type == 0 || x.Type == x.PollingOptionEntities.Count) { x.optionName = "(多选)"; } else if (x.Type < x.PollingOptionEntities.Count) { x.optionName = "(最多选" + x.Type + "项)"; } else if (x.Type == 999) { x.optionName = "文本"; } }); } if (pollingview.Type == 1) { pollingview.Title = "有奖问答"; } else if (pollingview.Type == 2) { pollingview.Title = "投票"; } if (pollingview.StartDateTime > DateTime.Now) { pollingview.StatusName = "未开始"; } else if (pollingview.EndDateTime > DateTime.Now && pollingview.StartDateTime < DateTime.Now) { pollingview.StatusName = "提交"; } else if (pollingview.EndDateTime < DateTime.Now) { pollingview.StatusName = "已结束"; } return(pollingview); }
/// <summary> /// 适用于前台投票提交后页面(投票) /// </summary> /// <param name="query"></param> /// <param name="LillyId"></param> /// <returns></returns> public PollingView GetPollingVote(int pollingId, string LillyId) { PollingView pollingview = GetPollingDetailView(pollingId); //var vote = _pollingResultService.Repository.Entities.Where(x => x.PollingId == pollingview.Id && x.UserId == LillyId) // .Select(x => new { UserId = x.UserId.ToUpper(), Answer = x.Answer, AnswerText = x.AnswerText, QuestionId = x.QuestionId }).ToList(); //refactor by john xie var query = _pollingResultService.Repository.Entities.Where(x => x.PollingId == pollingview.Id && x.IsDeleted != true); var voteQuery = query.Where(x => x.UserId == LillyId).Select( x => new { UserId = x.UserId.ToUpper(), x.Answer, x.AnswerText, x.QuestionId }).Future(); var groupQuery = query.GroupBy(x => new { x.QuestionId, x.Answer }).Select(a => new { a.Key.QuestionId, a.Key.Answer, Count = a.Count() }).Future(); var vote = voteQuery.ToList(); var group = groupQuery.ToList().Select(x => new ResultGroup { AnswerId = x.Answer, CountNumber = x.Count, QuestionId = x.QuestionId }).ToList(); if (pollingview.PollingQuestions != null && pollingview.PollingQuestions.Count > 0) { pollingview.PollingQuestions.ToList().ForEach(x => { x.PollingOptionEntities.ForEach(y => { if (vote.Where(v => v.UserId == LillyId).Any(z => z.Answer == y.Id)) { y.SelectName = "(已选)"; } }); x.PollingQuestionResult = vote.Where(m => m.Answer == 0 && m.QuestionId == x.Id).Select(n => n.AnswerText).FirstOrDefault();//文本题答案 //百分比 GetOptionPercent(x.PollingOptionEntities, group); //给每个选项都加上百分数和票数 }); } return(pollingview); }
/// <summary> /// 将PollingResultList转换成每个question一条答案的方式,比如多选的多条Result转成1条(ABC) /// </summary> /// <param name="pollingID"></param> /// <param name="_pollingEntity"></param> /// <returns></returns> public List <PollingResultCustomView> PollingResult(int pollingId, IList <PollingResultEntity> pollingResultEntity, PollingView pollingView) { var pollingCustomView = new PollingCustomView { PollingId = pollingId }; //var pollingView = GetPollingView(pollingId); foreach (var question in pollingView.PollingQuestions) { var pollingOptions = question.PollingOptionEntities.Where(a => (a.IsDeleted != true)).ToList(); int i = 0; foreach (var option in pollingOptions) { option.OptionIndex = i.ToString();//toABCD(i); i++; } } // 开始处理答案 var pollingAnswers = pollingResultEntity; // 同一个人对同一个question的答案(多选),需要汇总到一个里。 //var empDetails = WeChatCommonService.lstUserWithDeptTag; var grouped = pollingAnswers.GroupBy(a => new { a.UserId, a.QuestionId }); foreach (var g in grouped) { var question = pollingView.PollingQuestions.FirstOrDefault(a => a.Id == g.Key.QuestionId); if (question == null || question.Type == 99) { continue; } //var emp = empDetails.SingleOrDefault(a => a.userid.Equals(g.Key.UserId, StringComparison.InvariantCultureIgnoreCase)); var newAnswerResult = new PollingResultCustomView() { QuestionId = question.Id, QuestionTitle = question.Title, UserId = g.Key.UserId, //UserName = (emp != null ? emp.name : ""), //UserDeptLv1 = (emp != null ? emp.deptLvs[2] : ""), //UserDeptLv2 = (emp != null ? emp.deptLvs[3] : ""), //UserDeptLv3 = (emp != null ? emp.deptLvs[4] : ""), }; pollingCustomView.Results.Add(newAnswerResult); foreach (var v in g.ToList()) { var option = question.PollingOptionEntities.FirstOrDefault(a => a.Id == v.Answer && (a.IsDeleted != true)); if (option == null) { continue; } int optionIndex = int.Parse(option.OptionIndex); //var answer = newAnswerResult.Answers[optionIndex]; newAnswerResult.AnswerTime = v.CreatedDate.GetValueOrDefault(); newAnswerResult.Answers[optionIndex].AnswerId = v.Answer; newAnswerResult.Answers[optionIndex].AnswerText = v.AnswerText; var answered = newAnswerResult.Answers.Aggregate("", (current, answer) => current + (answer.AnswerId == 0 ? "N" : "Y")); newAnswerResult.CustomAnswer = ConvertYNToABCStatic(answered, 10); var questionstr = pollingView.PollingQuestions.FirstOrDefault(a => a.Id == newAnswerResult.QuestionId); if (questionstr != null) { newAnswerResult.RightAnswers = questionstr.RightAnswers; } if (newAnswerResult.CustomAnswer == newAnswerResult.RightAnswers) { newAnswerResult.CustomStatus = "正确"; // : "错误"; newAnswerResult.IsRight = true; } else { newAnswerResult.CustomStatus = "错误"; newAnswerResult.IsRight = false; } //newAnswerResult.CustomStatus = newAnswerResult.CustomAnswer == newAnswerResult.RightAnswers ? "正确" : "错误"; } } return(pollingCustomView.Results); }
public override int UpdateView <T>(T objModalSrc) { var pollingView = (PollingView)(IViewModel)objModalSrc; // 在后台根据传过来的值区分哪些option or question是插入哪些是更新 // 先从后台拉一下当前的数据,组一个oldPollingView var oldPollingView = new PollingView(); oldPollingView.ConvertAPIModel(Repository.Entities.AsNoTracking().FirstOrDefault(a => a.Id == pollingView.Id && a.IsDeleted != true)); oldPollingView.PollingQuestions = _pollingQuestionService.GetPollingQuestion(pollingView.Id); foreach (var oldQuestion in oldPollingView.PollingQuestions) { oldQuestion.PollingOptionEntities = _pollingOptionService.GetPollingOptions(oldQuestion.Id); } // 因为直接更新会级联更新,所以这里把孩子都删了更新,然后再加上孩子们 var newQuestions = pollingView.PollingQuestions; pollingView.PollingQuestions = null; base.UpdateView(pollingView, new List <string> { "Name", "Type", "StartDateTime", "EndDateTime", "StandardScore", "ReplyMessage" }); pollingView.PollingQuestions = newQuestions; var allUpdates = newQuestions.AsParallel().Where(x => oldPollingView.PollingQuestions.AsParallel().Any(y => x.Id == y.Id && (x.Score != y.Score || string.Compare(x.RightAnswers, y.RightAnswers, StringComparison.OrdinalIgnoreCase) != 0))).Select(x => new { x.Id, x.Score, x.RightAnswers }).ToList(); if (allUpdates.Any()) { var ids = allUpdates.Select(x => x.Id).ToList(); var oldAnswers = _pollingAnswerService.Repository.Entities.Where(x => ids.Contains(x.QuestionId) && x.IsDeleted != true).ToList(); var updateScoreQuestionList = allUpdates.AsParallel().Where(x => oldPollingView.PollingQuestions.AsParallel().Any(y => x.Id == y.Id && x.Score != y.Score)).Select(x => new { x.Id, x.Score }).ToList(); var updateRightAnswerQuestionList = allUpdates.AsParallel().Where(x => oldPollingView.PollingQuestions.AsParallel().Any(y => x.Id == y.Id && string.Compare(x.RightAnswers, y.RightAnswers, StringComparison.OrdinalIgnoreCase) != 0)).Select(x => new { x.Id, x.RightAnswers }).ToList(); if (updateScoreQuestionList.Any() && oldAnswers.Any()) { Parallel.ForEach(oldAnswers, x => { var newQuestion = updateScoreQuestionList.AsParallel().First(y => y.Id == x.QuestionId); x.Score = newQuestion.Score.GetValueOrDefault(); }); oldAnswers.ForEach(x => _pollingAnswerService.Repository.Update(x, new List <string> { "Score" })); } if (updateRightAnswerQuestionList.Any() && oldAnswers.Any()) { Parallel.ForEach(oldAnswers, x => { var newQuestion = updateRightAnswerQuestionList.AsParallel().First(y => y.Id == x.QuestionId); x.Status = string.Compare(x.SelectAnswer, newQuestion.RightAnswers, StringComparison.OrdinalIgnoreCase) == 0; x.RightAnswer = newQuestion.RightAnswers; }); oldAnswers.ForEach(x => _pollingAnswerService.Repository.Update(x, new List <string> { "RightAnswer", "Status" })); } } // 检查OldPollingView相对于NewPollingView删除了哪些 foreach (var oldQuestion in oldPollingView.PollingQuestions) { // 如果id不在新polling里,就是删掉了 var sameQuestionInNew = pollingView.PollingQuestions.FirstOrDefault(a => a.Id == oldQuestion.Id && a.IsDeleted != true); if (sameQuestionInNew == null) { _pollingQuestionService.Repository.Delete(oldQuestion.Id); // 如果删除了question的话,会自动删除option // 我们再次手动删除answer表里相关的option _pollingResultService.DeleteByQuestionId(oldQuestion.Id); PollingQuestionView question = oldQuestion; _pollingAnswerService.Repository.Entities.Where(x => x.QuestionId == question.Id).Delete(); continue; } // 如果没删,检查option。 // 如果option被删了,同时去删除用户填写过答案的answer foreach (var oldOption in oldQuestion.PollingOptionEntities) { if (sameQuestionInNew.PollingOptionEntities == null || sameQuestionInNew.PollingOptionEntities.FirstOrDefault(a => a.Id == oldOption.Id && a.IsDeleted != true) == null) { _pollingOptionService.Repository.Delete(oldOption.Id); _pollingResultService.DeleteByOptionId(oldOption.Id); PollingOptionView option = oldOption; _pollingAnswerService.Repository.Entities.Where(x => x.QuestionId == option.Id).Delete(); } } } // 除了删除的,就是添加和更新的 // 检查NewPollingView相对于OldPollingView添加了哪些 foreach (var newQuestion in pollingView.PollingQuestions) { // 如果id是0,肯定新加的,执行级联添加 if (newQuestion.Id == 0) { newQuestion.PollingId = pollingView.Id; _pollingQuestionService.InsertView(newQuestion); continue; } // 如果不是0,更新自己,并且检查option var options = newQuestion.PollingOptionEntities; newQuestion.PollingOptionEntities = null; _pollingQuestionService.UpdateView(newQuestion); newQuestion.PollingOptionEntities = options; if (newQuestion.PollingOptionEntities != null && newQuestion.PollingOptionEntities.Any()) { foreach (var newOption in newQuestion.PollingOptionEntities) { if (newOption.Id == 0) { newOption.QuestionId = newQuestion.Id; _pollingOptionService.InsertView(newOption); } else { _pollingOptionService.UpdateView(newOption, new List <string> { "OptionName", "Picture", "Type" }); } } } } return(1); }
public override int UpdateView <T>(T objModalSrc) { int iRet; var pollingView = (PollingView)(IViewModel)objModalSrc; // 在后台根据传过来的值区分哪些option or question是插入哪些是更新 // 先从后台拉一下当前的数据,组一个oldPollingView PollingView oldPollingView = new PollingView(); oldPollingView.ConvertAPIModel(Repository.Entities.AsNoTracking().FirstOrDefault(a => a.Id == pollingView.Id && a.IsDeleted != true)); oldPollingView.PollingQuestions = _pollingQuestionService.GetPollingQuestion(pollingView.Id); foreach (var oldQuestion in oldPollingView.PollingQuestions) { oldQuestion.PollingOptionEntities = _pollingOptionService.GetPollingOptions(oldQuestion.Id); } // 因为直接更新会级联更新,所以这里把孩子都删了更新,然后再加上孩子们 var newQuestions = pollingView.PollingQuestions; pollingView.PollingQuestions = null; base.UpdateView(pollingView, new List <string> { "Name", "Type", "StartDateTime", "EndDateTime", "StandardScore", "ReplyMessage" }); pollingView.PollingQuestions = newQuestions; // 检查OldPollingView相对于NewPollingView删除了哪些 foreach (var oldQuestion in oldPollingView.PollingQuestions) { // 如果id不在新polling里,就是删掉了 var sameQuestionInNew = pollingView.PollingQuestions.FirstOrDefault(a => a.Id == oldQuestion.Id && a.IsDeleted != true); if (sameQuestionInNew == null) { _pollingQuestionService.Repository.Delete(oldQuestion.Id); // 如果删除了question的话,会自动删除option // 我们再次手动删除answer表里相关的option var answers = _pollingResultService.DeleteByQuestionId(oldQuestion.Id); continue; } // 如果没删,检查option。 // 如果option被删了,同时去删除用户填写过答案的answer else { foreach (var oldOption in oldQuestion.PollingOptionEntities) { if (sameQuestionInNew.PollingOptionEntities == null || sameQuestionInNew.PollingOptionEntities.FirstOrDefault(a => a.Id == oldOption.Id && a.IsDeleted != true) == null) { _pollingOptionService.Repository.Delete(oldOption.Id); _pollingResultService.DeleteByOptionId(oldOption.Id); continue; } } } } // 除了删除的,就是添加和更新的 // 检查NewPollingView相对于OldPollingView添加了哪些 foreach (var newQuestion in pollingView.PollingQuestions) { // 如果id是0,肯定新加的,执行级联添加 if (newQuestion.Id == 0) { newQuestion.PollingId = pollingView.Id; _pollingQuestionService.InsertView(newQuestion); continue; } // 如果不是0,更新自己,并且检查option else { var options = newQuestion.PollingOptionEntities; newQuestion.PollingOptionEntities = null; _pollingQuestionService.UpdateView(newQuestion); newQuestion.PollingOptionEntities = options; if (newQuestion.PollingOptionEntities != null && newQuestion.PollingOptionEntities.Any()) { foreach (var newOption in newQuestion.PollingOptionEntities) { if (newOption.Id == 0) { newOption.QuestionId = newQuestion.Id; _pollingOptionService.InsertView(newOption); } else { _pollingOptionService.UpdateView(newOption, new List <string> { "OptionName", "Picture", "Type" }); } } } } } return(1); }