private ActionResult exportToCSV(PollingCustomView pollingCustomView) { string headline = "userid,name,deptlv1,deptlv2,deptlv3,questionid,questionname,A,B,C,D,E,F,G,H,I,J\r\n"; StringBuilder sb = new StringBuilder(headline); foreach (var question in pollingCustomView.Results) { sb.Append(string.Format("{0},{1},{2},{3},{4},{5},{6}", question.UserId, question.UserName, question.UserDeptLv1, question.UserDeptLv2, question.UserDeptLv3, question.QuestionId, question.QuestionTitle)); foreach (var answer in question.Answers) { sb.Append(answer.AnswerId == 0 ? ",N" : ",Y"); } sb.Append("\r\n"); } string fileHeadName = pollingCustomView.PollingName; MemoryStream stream = new MemoryStream(); byte[] bits = System.Text.Encoding.UTF8.GetBytes(sb.ToString()); byte[] bom = new[] { (byte)0xEF, (byte)0xBB, (byte)0xBF }; stream.Write(bom, 0, bom.Length); stream.Write(bits, 0, bits.Length); string fileName = fileHeadName + "_" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".csv"; return(File(stream.ToArray(), "text/comma-separated-values", fileName)); }
/// <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 List <PollingResultCustomView> PollingResult(int pollingId) { // 1. 取出polling,创建每个question和answer的列表 var polling = Repository.Entities.FirstOrDefault(a => a.Id == pollingId && (a.IsDeleted == null || a.IsDeleted == false)); if (polling == null) { throw new Exception("Polling为空!"); } var pollingCustomView = new PollingCustomView { PollingId = pollingId }; foreach (var question in polling.PollingQuestions) { var pollingQuestions = question.PollingOptionEntities.Where(a => (a.IsDeleted == null || a.IsDeleted == false)); for (int i = 0; i < pollingQuestions.Count(); i++) { var option = question.PollingOptionEntities.ElementAt(i); option.OptionIndex = i.ToString();//toABCD(i); } } // 开始处理答案 var pollingAnswers = _pollingResultService.Repository.Entities.Where(a => a.PollingId == pollingId && (a.IsDeleted == null || a.IsDeleted == false)); // 同一个人对同一个question的答案(多选),需要汇总到一个里。 foreach (var pollingAnswer in pollingAnswers) { var question = polling.PollingQuestions.FirstOrDefault(a => a.Id == pollingAnswer.QuestionId); if (question != null && question.Type == 99) { continue; } // 如果是多选题的话,需要看是否已经有过,有过的话就合并答案 //var newAnswerResult = pollingCustomView.Results.FirstOrDefault(a => a.QuestionId == question.Id && a.UserId == pollingAnswer.UserId); var newAnswerResult = pollingCustomView.Results.FirstOrDefault(a => a.QuestionId == question.Id && a.UserId.Equals(pollingAnswer.UserId, StringComparison.InvariantCultureIgnoreCase)); var empDetails = WeChatCommonService.lstUserWithDeptTag; if (newAnswerResult == null) { var emp = empDetails.SingleOrDefault(a => a.userid.Equals(pollingAnswer.UserId, StringComparison.InvariantCultureIgnoreCase)); if (question != null) { newAnswerResult = new PollingResultCustomView() { QuestionId = question.Id, QuestionTitle = question.Title, UserId = pollingAnswer.UserId, UserName = (emp != null ? emp.name : ""), UserDeptLv1 = (emp != null ? emp.deptLvs[2] : ""), UserDeptLv2 = (emp != null ? emp.deptLvs[3] : ""), UserDeptLv3 = (emp != null ? emp.deptLvs[4] : ""), AnswerTime = pollingAnswer.CreatedDate.GetValueOrDefault() } } ; pollingCustomView.Results.Add(newAnswerResult); } if (question != null) { var option = question.PollingOptionEntities.FirstOrDefault(a => a.Id == pollingAnswer.Answer && (a.IsDeleted == null || a.IsDeleted == false)); if (option == null) { continue; } int optionIndex = int.Parse(option.OptionIndex); var answer = newAnswerResult.Answers[optionIndex]; newAnswerResult.Answers[optionIndex].AnswerId = pollingAnswer.Answer; newAnswerResult.Answers[optionIndex].AnswerText = pollingAnswer.AnswerText; } } foreach (var questionresult in pollingCustomView.Results) { var answered = questionresult.Answers.Aggregate("", (current, answer) => current + (answer.AnswerId == 0 ? "N" : "Y")); questionresult.CustomAnswer = ConvertYNToABCStatic(answered, 10); var questionstr = polling.PollingQuestions.FirstOrDefault(a => a.Id == questionresult.QuestionId); if (questionstr != null) { questionresult.RightAnswers = questionstr.RightAnswers; } questionresult.CustomStatus = questionresult.CustomAnswer == questionresult.RightAnswers ? "正确" : "错误"; } return(pollingCustomView.Results); }
public ActionResult GetUserAnswers(int pollingId) { // 1. 取出polling,创建每个question和answer的列表 var polling = _pollingService.Repository.Entities.FirstOrDefault(a => a.Id == pollingId && (a.IsDeleted != true)); if (polling == null) { throw new Exception("Polling丢了!"); } PollingCustomView pollingCustomView = new PollingCustomView(); pollingCustomView.PollingId = pollingId; pollingCustomView.PollingName = polling.Name; // 把option转成ABCD foreach (var question in polling.PollingQuestions) { var pollingQuestions = question.PollingOptionView.Where(a => (a.IsDeleted == null || a.IsDeleted == false)); for (int i = 0; i < pollingQuestions.Count(); i++) { var option = pollingQuestions.ElementAt(i); option.OptionIndex = i.ToString();//toABCD(i); } } // 在处理答案之前,先把用户和部门关系搞出来 var employees = WeChatCommonService.lstUserWithDeptTag; // 开始处理答案 var pollingAnswers = _pollingResultService.Repository.Entities.Where(a => a.PollingId == pollingId && (a.IsDeleted != true)); // 同一个人对同一个question的答案(多选),需要汇总到一个里。 foreach (var pollingAnswer in pollingAnswers) { var question = polling.PollingQuestions.FirstOrDefault(a => a.Id == pollingAnswer.QuestionId); if (question.Type == 99) { continue; } // 如果是多选题的话,需要看是否已经有过,有过的话就合并答案 var newAnswerResult = pollingCustomView.Results.FirstOrDefault(a => a.QuestionId == question.Id && a.UserId == pollingAnswer.UserId); if (newAnswerResult == null) { var emp = employees.FirstOrDefault(a => a.userid == pollingAnswer.UserId); newAnswerResult = new PollingResultCustomView() { QuestionId = question.Id, QuestionTitle = question.Title, UserId = pollingAnswer.UserId, UserName = (emp == null ? "NA" : emp.name), UserDeptLv1 = (emp == null ? "NA" : emp.deptLvs[2]), UserDeptLv2 = (emp == null ? "NA" : emp.deptLvs[3]), UserDeptLv3 = (emp == null ? "NA" : emp.deptLvs[4]), AnswerTime = pollingAnswer.CreatedDate.Value }; pollingCustomView.Results.Add(newAnswerResult); } var option = question.PollingOptionView.FirstOrDefault(a => a.Id == pollingAnswer.Answer && (a.IsDeleted != true)); if (option == null) { continue; } int optionIndex = int.Parse(option.OptionIndex); var answer = newAnswerResult.Answers[optionIndex]; newAnswerResult.Answers[optionIndex].AnswerId = pollingAnswer.Answer; newAnswerResult.Answers[optionIndex].AnswerText = pollingAnswer.AnswerText; } return(exportToCSV(pollingCustomView)); }