ExamPaperDAL.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using EMISOnline.DataLogic.Repositories;
  6. using System.Linq.Expressions;
  7. using EMISOnline.Entities;
  8. using EMISOnline.ViewModel.ExamView;
  9. using System.Xml.Linq;
  10. using Bowin.Common.Linq;
  11. using System.Data;
  12. namespace EMISOnline.DataLogic.ExamSetting
  13. {
  14. public class ExamPaperDAL : SqlHelper
  15. {
  16. public onlinetestRepository OnlinetestRepository { get; set; }
  17. public UserRepository UserRepository { get; set; }
  18. public questionRepository questionRepository { get; set; }
  19. public paper_question_setRepository question_setRepository { get; set; }
  20. public base_question_typeRepository question_typeRepository { get; set; }
  21. /// <summary>
  22. /// 根据条件查找试卷信息
  23. /// </summary>
  24. /// <param name="maximumRows"></param>
  25. /// <param name="startRowIndex"></param>
  26. /// <param name="sortClause"></param>
  27. /// <param name="totalRow"></param>
  28. /// <param name="expWhere"></param>
  29. /// <returns></returns>
  30. public IQueryable<ListPaperView> ListPaper(int maximumRows, int startRowIndex, Expression<Func<test_paper, bool>> expWhere)
  31. {
  32. var q = PaperRepository.Entities;
  33. q = q.Where(expWhere);
  34. var query = from tq in q
  35. join user in UserRepository.Entities on tq.created_by equals user.LoginID into ruser
  36. from us in ruser.DefaultIfEmpty()
  37. orderby tq.test_paper_id descending
  38. select new ListPaperView
  39. {
  40. test_paper_id = tq.test_paper_id,
  41. PaperName = tq.PaperName,
  42. paper_score = tq.paper_score,
  43. used_count = tq.used_count,
  44. created_date = tq.created_date,
  45. created_by = us != null ? us.Name : "",
  46. is_dynamic = tq.is_dynamic ? "动态" : "静态",
  47. isEnable = tq.is_vaild.Value ? "是" : "否"
  48. };
  49. return query;
  50. }
  51. /// <summary>
  52. /// 根据一序列的条件查找不同题型的题数是多少
  53. /// </summary>
  54. /// <param name="lib_ids"></param>
  55. /// <param name="diff_min"></param>
  56. /// <param name="diff_max"></param>
  57. /// <param name="err_rate"></param>
  58. /// <param name="usecount_min"></param>
  59. /// <param name="usecount_max"></param>
  60. /// <param name="question_type"></param>
  61. /// <returns></returns>
  62. public object CountQuesSumByCondition(List<decimal> lib_ids, decimal diff_min, decimal diff_max, decimal err_rate,
  63. int usecount_min, int usecount_max)
  64. {
  65. var query = from ques in questionRepository.Entities
  66. where lib_ids.Contains(ques.test_question_libary_id.Value)
  67. && ((ques.difficulty_degree.HasValue ? ques.difficulty_degree.Value : 0) >= diff_min && (ques.difficulty_degree.HasValue ? ques.difficulty_degree.Value : 0) <= diff_max)
  68. && (ques.wrong_percent.HasValue ? ques.wrong_percent.Value : 0) >= err_rate
  69. && ((ques.used_count.HasValue ? ques.used_count.Value : 0) >= usecount_min && (ques.used_count.HasValue ? ques.used_count.Value : 0) <= usecount_max)
  70. // && ques.base_question_type_id == question_type
  71. && ques.filled == false
  72. group ques by ques.base_question_type_id into res
  73. select new
  74. {
  75. Number = res.Count(),
  76. TypeID = res.Key
  77. };
  78. return query;//.ToDictionary(q => q.TypeID.Value, q => q.Number);
  79. }
  80. /// <summary>
  81. /// 根据试卷
  82. /// </summary>
  83. /// <param name="paperid"></param>
  84. /// <returns></returns>
  85. public DataTable GetQuestionByPaperID(decimal paperid)
  86. {
  87. var paperset = question_setRepository.GetSingle(q => q.test_paper_id == paperid);
  88. XElement xml = XElement.Parse(paperset.questionXml);
  89. var question_set = from c in xml.Descendants("QuestionXml")
  90. select new
  91. {
  92. Question_id = decimal.Parse(c.Element("Question_id").Value),
  93. Score = c.Element("Score").Value,
  94. };
  95. var query = from set in question_set
  96. join q_set in questionRepository.Entities on set.Question_id equals q_set.test_question_Id
  97. join type in question_typeRepository.Entities on q_set.base_question_type_id equals type.base_question_type_id
  98. orderby q_set.created_date descending
  99. select new
  100. {
  101. q_set.test_question_Id,
  102. q_set.content,
  103. q_set.base_question_type_id,
  104. question_type = type.Name,
  105. q_set.created_date,
  106. score = set.Score,
  107. q_set.difficulty_degree,
  108. q_set.used_count,
  109. q_set.created_by
  110. };
  111. return query.ToTable();
  112. }
  113. /// <summary>
  114. /// 按照设定条件抽取题库
  115. /// </summary>
  116. /// <param name="test_question_libary_id">题库id,id,id</param>
  117. /// <param name="difficulty_degree_min">难度最小</param>
  118. /// <param name="difficulty_degree_max">难度最大</param>
  119. /// <param name="wrong_percent">错误率</param>
  120. /// <param name="used_count_min">使用次数最小</param>
  121. /// <param name="used_count_max">使用次数最大</param>
  122. /// <returns></returns>
  123. public System.Data.DataTable SelectQuestions(string test_question_libary_id, string difficulty_degree_min,
  124. string difficulty_degree_max, string wrong_percent, string used_count_min, string used_count_max)
  125. {
  126. var ds = Query("dbo.SP_test_questions_filter", new KeyValuePair<string, object>("@test_question_libary_id", test_question_libary_id),
  127. new KeyValuePair<string, object>("@difficulty_degree_min", difficulty_degree_min),
  128. new KeyValuePair<string, object>("@difficulty_degree_max", difficulty_degree_max),
  129. new KeyValuePair<string, object>("@wrong_percent", wrong_percent),
  130. new KeyValuePair<string, object>("@used_count_min", used_count_min), new KeyValuePair<string, object>("@used_count_max", used_count_max));
  131. return ds.Tables[0];
  132. }
  133. public bool DeletePaper(decimal PaperID)
  134. {
  135. question_setRepository.UnitOfWork.Delete<test_paper_question_set>(q => q.test_paper_id == PaperID);
  136. //PaperRepository.UnitOfWork.Delete<test_paper>(q => q.test_paper_id == PaperID);
  137. PaperRepository.UnitOfWork.Commit();
  138. return true;
  139. }
  140. /// <summary>
  141. /// 根据试卷id获取试卷信息以json格式返回
  142. /// </summary>
  143. /// <param name="paperid"></param>
  144. /// <returns></returns>
  145. public object GetPaperQuestions(decimal paperid)
  146. {
  147. var question_set = PaperQuestion_set(paperid);
  148. var query = from ques_set in question_set
  149. join ques in questionRepository.Entities on ques_set.Question_id equals ques.test_question_Id into questions
  150. from q in questions.DefaultIfEmpty()
  151. where q.filled == false
  152. select new
  153. {
  154. question_id = q.test_question_Id,
  155. typeName = q.test_base_question_type.Name,
  156. q.content,
  157. q.base_question_type_id,
  158. hasImg = q.question_file_id.HasValue,
  159. url = q.question_file_id.HasValue ? q.test_question_file.url : "",
  160. score = ques_set.Score,
  161. rightAnswer = q.answers,
  162. answers = q.test_question_provid_answer.Select(a => new
  163. {
  164. content = a.answer_name,
  165. answer_id = a.provid_answer_id,
  166. hasImg = a.file_id.HasValue,
  167. url = a.file_id.HasValue ? a.test_question_file.url : "",
  168. })
  169. };
  170. var count = query.Count();
  171. var type_quetions = from basetype in question_typeRepository.Entities
  172. orderby basetype.diplay_order
  173. select new
  174. {
  175. id = basetype.base_question_type_id,
  176. name = basetype.Name,
  177. tscore = (from q in query
  178. where q.base_question_type_id.Value == basetype.base_question_type_id
  179. select q.score).Sum(),
  180. questions = (from q in query
  181. where q.base_question_type_id.Value == basetype.base_question_type_id
  182. select q).ToArray()
  183. };
  184. return type_quetions.Where(q => q.questions.Count() > 0).ToArray();
  185. }
  186. public List<QuestionXml> PaperQuestion_set(decimal paperid)
  187. {
  188. var paperset = question_setRepository.GetSingle(q => q.test_paper_id == paperid);
  189. XElement xml = XElement.Parse(paperset.questionXml);
  190. var question_set = from c in xml.Descendants("QuestionXml")
  191. select new QuestionXml
  192. {
  193. Question_id = decimal.Parse(c.Element("Question_id").Value),
  194. Score = decimal.Parse(c.Element("Score").Value),
  195. };
  196. return question_set.ToList();
  197. }
  198. }
  199. }