QuestionSettingServices.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using EMISOnline.DataLogic.ExamSetting;
  6. using Bowin.Common.Linq.Entity;
  7. using EMISOnline.ViewModel.ExamView;
  8. using System.Linq.Expressions;
  9. using EMISOnline.Entities;
  10. using System.Linq.Expressions;
  11. using Bowin.Common.Linq;
  12. using EMISOnline.ViewModel;
  13. using Bowin.Common;
  14. using System.Web.Script.Serialization;
  15. using System.Xml.Serialization;
  16. using System.IO;
  17. using System.Xml.Linq;
  18. using EMISOnline.DataLogic.SystemDAL;
  19. using System.Runtime.Remoting.Contexts;
  20. using System.Data;
  21. using System.Text.RegularExpressions;
  22. namespace EMISOnline.CommonLogic.ExamServices
  23. {
  24. public class QuestionSettingServices : IQuestionSettingServices
  25. {
  26. public QuestionSettingDAL SettingDAL { get; set; }
  27. public object GetQuestionList(string Content, decimal? QuestionType, decimal? libaryid, int maximumRows, int startRowIndex)
  28. {
  29. Expression<Func<test_question, bool>> exp = (x => true);
  30. if (!string.IsNullOrEmpty(Content))
  31. exp = exp.And(q => q.content.Contains(Content));
  32. if (QuestionType.HasValue)
  33. exp = exp.And(q => q.base_question_type_id == QuestionType);
  34. if (libaryid.HasValue)
  35. {
  36. var libarys = getSubListid(libaryid.Value);
  37. exp = exp.And(q => libarys.Contains(q.test_question_libary_id.Value ));
  38. }
  39. var question = SettingDAL.GetQuestionList(exp);
  40. return question.OrderBy(q => q.test_question_Id).ToGridResultSet(maximumRows, startRowIndex + 1);
  41. }
  42. public List<decimal> getSubListid(decimal libaryid)
  43. {
  44. IList<test_question_libary> list = new List<test_question_libary>();
  45. SettingDAL.GetSubLibary(null, libaryid, list);
  46. List<decimal> libaryids = list.Select(q => q.test_question_libary_id).ToList();
  47. libaryids.Add(libaryid);
  48. return libaryids;
  49. }
  50. public QuestionSettingView GetQuestion(decimal test_question_Id)
  51. {
  52. var entry = SettingDAL.questionRepository.GetSingle(q => q.test_question_Id == test_question_Id);
  53. test_question_libary libary = GetQuestionLibary(entry.test_question_libary_id.Value);
  54. var question_type = SettingDAL.typeRepository.GetSingle(q => q.base_question_type_id == entry.base_question_type_id);
  55. var question_provid_answer = SettingDAL.answerRepository.Entities.Where(q => q.test_question_Id == test_question_Id).ToList();
  56. var question = new QuestionSettingView()
  57. {
  58. test_question_Id = entry.test_question_Id,
  59. base_question_type_id = entry.base_question_type_id,
  60. content = entry.content,
  61. question_file_id = entry.question_file_id,
  62. note = entry.note,
  63. test_question_libary_id = entry.test_question_libary_id,
  64. score = entry.score,
  65. difficulty_degree = entry.difficulty_degree,
  66. is_vaild = entry.is_vaild,
  67. TypeName = question_type.Name,
  68. question_provid_answer =
  69. question_provid_answer.Select(a => new test_question_provid_answer()
  70. {
  71. answer_name = a.answer_name,
  72. provid_answer_id = a.provid_answer_id,
  73. file_id = a.file_id
  74. }).ToList(),
  75. answers = entry.answers,
  76. libaryName = libary.name
  77. };
  78. return question;
  79. }
  80. public test_question_libary GetQuestionLibary(decimal libary_Id)
  81. {
  82. var question = SettingDAL.libaryRepository.GetSingle(q => q.test_question_libary_id == libary_Id);
  83. return question;
  84. }
  85. public object GetQuestionType(bool? isEmpty)
  86. {
  87. var question = SettingDAL.typeRepository.GetList(q => true);
  88. var result = question.Select(q => new { q.Name, base_question_type_id = q.base_question_type_id.ToString() }).ToList();
  89. if (isEmpty == true)
  90. result.Insert(0, new { Name = "全选", base_question_type_id = "" });
  91. return result;
  92. }
  93. public object GetDifficulty()
  94. {
  95. var question = SettingDAL.paramRepository.GetList(q => q.base_param_parent_id == 3);
  96. return question.Select(q => new { q.name }).ToList();
  97. }
  98. private static Regex regAnser = new Regex(@"^[A-Z][、]");
  99. /// <summary>
  100. /// 保存试题、保存试题答案
  101. /// </summary>
  102. public void InsertByTrans(test_question tq, List<test_question_provid_answer> tqpaList, List<string> rightAnswersList, string userid)
  103. {
  104. //ToDo:保存到数据库
  105. tq.question_file_id = tq.question_file_id == 0 ? null : tq.question_file_id;
  106. tq.created_date = DateTime.Now;
  107. tq.created_by = "0";
  108. tq.used_count = 1;
  109. tq.wrong_count = 0;
  110. string rightAnswers = "";
  111. foreach (test_question_provid_answer tqpa in tqpaList)
  112. {
  113. tqpa.created_by = userid;
  114. tqpa.created_date = DateTime.Now;
  115. tqpa.test_question_Id = tq.test_question_Id;
  116. tq.test_question_provid_answer.Add(tqpa);
  117. }
  118. SettingDAL.questionRepository.UnitOfWork.Add(tq);
  119. SettingDAL.questionRepository.UnitOfWork.Commit();
  120. foreach (test_question_provid_answer tqpa in tq.test_question_provid_answer)
  121. {
  122. foreach (string s in rightAnswersList)
  123. {
  124. if (regAnser.Replace(tqpa.answer_name, "") == regAnser.Replace(s, ""))
  125. {
  126. rightAnswers += tqpa.provid_answer_id + ",";
  127. break;
  128. }
  129. }
  130. }
  131. if (rightAnswers.EndsWith(","))
  132. rightAnswers = rightAnswers.TrimEnd(',');
  133. tq.answers = rightAnswers;
  134. SettingDAL.questionRepository.UnitOfWork.Update(tq);
  135. SettingDAL.questionRepository.UnitOfWork.Commit();
  136. }
  137. /// <summary>
  138. /// 更新试题、更新试题答案
  139. /// </summary>
  140. public void UpdateByTrans(test_question setting, List<test_question_provid_answer> tqpaList, List<string> rightAnswersList, string userid)
  141. {
  142. var question = SettingDAL.questionRepository.GetSingle(q => q.test_question_Id == setting.test_question_Id);
  143. Entry(setting, question);
  144. string rightAnswers = "";
  145. var provid_answer_ids = tqpaList.Select(q => q.provid_answer_id).ToArray();
  146. SettingDAL.answerRepository.UnitOfWork.Delete<test_question_provid_answer>(q => !provid_answer_ids.Contains(q.provid_answer_id) && q.test_question_Id == question.test_question_Id);
  147. foreach (test_question_provid_answer tqpa in tqpaList)
  148. {
  149. foreach (string s in rightAnswersList)
  150. {
  151. if (tqpa.answer_name == s)
  152. {
  153. rightAnswers += tqpa.provid_answer_id + ",";
  154. break;
  155. }
  156. }
  157. tqpa.created_by = userid;
  158. tqpa.created_date = DateTime.Now;
  159. tqpa.test_question_Id = question.test_question_Id;
  160. question.test_question_provid_answer.Add(tqpa);
  161. if (tqpa.provid_answer_id > 0)
  162. SettingDAL.answerRepository.UnitOfWork.Update(tqpa);
  163. else
  164. SettingDAL.answerRepository.UnitOfWork.Add(tqpa);
  165. }
  166. rightAnswers = rightAnswers.TrimEnd(',');
  167. question.answers = rightAnswers;
  168. SettingDAL.questionRepository.UnitOfWork.Commit();
  169. }
  170. public void Entry(test_question entry, test_question question)
  171. {
  172. question.base_question_type_id = entry.base_question_type_id;
  173. question.content = entry.content;
  174. question.question_file_id = entry.question_file_id == 0 ? null : entry.question_file_id;
  175. question.note = entry.note;
  176. question.test_question_libary_id = entry.test_question_libary_id;
  177. question.score = entry.score;
  178. question.difficulty_degree = entry.difficulty_degree;
  179. question.is_vaild = entry.is_vaild;
  180. question.answers = entry.answers; ;
  181. }
  182. }
  183. }