TestPaperDAL.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using EMISOnline.DataLogic.Repositories;
  6. using System.Xml.Linq;
  7. using EMISOnline.Entities;
  8. using EMISOnline.ViewModel.ExamView;
  9. using System.Linq.Expressions;
  10. using System.Data.SqlClient;
  11. using System.Data;
  12. namespace EMISOnline.DataLogic.ExamSetting
  13. {
  14. public class TestPaperDAL:SqlHelper
  15. {
  16. public VTestQestionRepository VquestionRepository { get; set; }
  17. public paper_question_setRepository question_setRepository { get; set; }
  18. public questionRepository questionRepository { get; set; }
  19. public base_question_typeRepository typeRepository { get; set; }
  20. public question_libaryRepository libaryRepository { get; set; }
  21. public onlinetestRepository onlinetestRepository { get; set; }
  22. public ExamResultRepository ExamResultRepository { get; set; }
  23. public onlinetest_manRepository manRepository { get; set; }
  24. public exchangeRepository exchangeRepository { get; set; }
  25. public UserRepository UserRepository { get; set; }
  26. public void SetQuestion(decimal paperid, decimal diff_min, decimal diff_max, int usedcount_min, int usedcount_max, decimal wrong_rate)
  27. {
  28. var paper = PaperRepository.Entities.Single(q => q.test_paper_id == paperid);
  29. //题库范围
  30. var libary_set_idstr = paper.question_libary_set_id;
  31. var libary_set_ids = libary_set_idstr.Split(',').Select(q => decimal.Parse(q)).ToArray();
  32. //得到按题型设置的题目数和分数
  33. XElement xml = XElement.Parse(paper.question_setting_xml);
  34. var question_set = from c in xml.Descendants("QType")
  35. select new
  36. {
  37. QType = c.Attribute("QTypeID").Value,
  38. PSum = c.Attribute("PSum").Value,
  39. TSum = c.Attribute("TSum").Value,
  40. QScore = c.Attribute("QScore").Value,
  41. };
  42. var questions = (from tq in vQuestion()
  43. where tq.difficulty_degree >= diff_min
  44. && tq.difficulty_degree <= diff_max
  45. && tq.used_count >= usedcount_min
  46. && tq.used_count <= usedcount_max
  47. && tq.wrong_percent >= wrong_rate
  48. && tq.filled == false
  49. && (libary_set_ids.Contains(tq.test_question_libary_id.Value))
  50. select tq).ToList();
  51. List<XElement> listxml = new List<XElement>();
  52. foreach (var set in question_set.ToList())
  53. {
  54. var tquestion = questions.Where(q => q.base_question_type_id.ToString() == set.QType).Select(q => new
  55. {
  56. q.test_question_Id,
  57. set.PSum,
  58. set.QScore
  59. }).ToList();
  60. foreach (var testquest in tquestion)
  61. {
  62. XElement x = new XElement("QuestionXml");
  63. x.SetElementValue("Question_id", testquest.test_question_Id);
  64. x.SetElementValue("Score", testquest.PSum);
  65. listxml.Add(x);
  66. }
  67. }
  68. XElement qtxml = new XElement("ArrayOfQuestionXml", listxml);
  69. test_paper_question_set paper_question_set = new test_paper_question_set()
  70. {
  71. test_paper_id = paperid,
  72. questionXml = qtxml.ToString(),
  73. order = 0
  74. };
  75. question_setRepository.UnitOfWork.Add(paper_question_set);
  76. question_setRepository.UnitOfWork.Commit();
  77. // --题库范围
  78. //declare @libary_set_id varchar(200)
  79. //select @libary_set_id=question_libary_set_id from test_paper where test_paper_id =@test_paper_id
  80. //select * into #libary_set_id from dbo.SplitString(@libary_set_id,',',1)
  81. //--得到按题型设置的题目数和分数
  82. //select d.c.value('(@QTypeID)[1]','varchar(20)') QType,
  83. //d.c.value('(@PSum)[1]','varchar(20)') PSum,
  84. //d.c.value('(@TSum)[1]','varchar(20)') TSum,
  85. //d.c.value('(@QScore)[1]','varchar(20)') QScore
  86. //into #question_set
  87. //from test_paper
  88. //cross apply
  89. //test_paper.question_setting_xml.nodes('/paper_set/QType') as d(c) where test_paper_id =@test_paper_id
  90. //--按条件筛选问题
  91. //select *
  92. //into #questions
  93. //from iexam_v_test_question where difficulty_degree >= @diff_min
  94. //and difficulty_degree <= @diff_max
  95. //and used_count >= @usedcount_min
  96. //and used_count <= @usedcount_max
  97. //and wrong_percent >= @wrong_rate
  98. //and filled = 0
  99. //and test_question_libary_id in (select Value from #libary_set_id)
  100. //--问题存储临时表
  101. //create table #q_selected(
  102. //test_question_Id decimal null,
  103. //QScore decimal null)
  104. //declare @QType decimal,@PSum int,@TSum decimal,@QScore decimal
  105. //declare type_Cursor cursor for
  106. //select * from #question_set
  107. //open type_Cursor
  108. //fetch next from type_Cursor into @QType,@PSum,@TSum,@QScore
  109. //while @@FETCH_STATUS=0
  110. //begin
  111. //insert into #q_selected
  112. //select top(@PSum) test_question_Id,@QScore from #questions where base_question_type_id= @QType order by rid
  113. //fetch next from type_Cursor into @QType,@PSum,@TSum,@QScore
  114. //end
  115. //close type_Cursor
  116. //DEALLOCATE type_Cursor
  117. //insert into test_paper_question_set(test_paper_id,questionXml,[order]) select @test_paper_id,
  118. //(select test_question_Id as Question_id,QScore as Score from #q_selected
  119. //for xml path('QuestionXml'),root('ArrayOfQuestionXml')) x,0
  120. //drop table #libary_set_id
  121. //drop table #question_set
  122. //drop table #questions
  123. //drop table #q_selected
  124. //end
  125. //GO
  126. }
  127. public IQueryable<ExamQuestionView> vQuestion()
  128. {
  129. var query = from question in questionRepository.Entities
  130. join type in typeRepository.Entities on question.base_question_type_id equals type.base_question_type_id
  131. join libary in libaryRepository.Entities on question.test_question_libary_id equals libary.test_question_libary_id
  132. select new ExamQuestionView
  133. {
  134. rid = Guid.NewGuid(),
  135. test_question_Id = question.test_question_Id,
  136. base_question_type_id = question.base_question_type_id,
  137. content = question.content,
  138. question_file_id = question.question_file_id,
  139. difficulty_degree = question.difficulty_degree,
  140. answers = question.answers,
  141. is_order = question.is_order,
  142. answers_note = question.answers_note,
  143. note = question.note,
  144. test_question_libary_id = question.test_question_libary_id,
  145. used_count = question.used_count,
  146. right_percent = question.right_percent,
  147. is_vaild = question.is_vaild,
  148. created_date = question.created_date,
  149. wrong_count = question.wrong_count,
  150. score = question.score,
  151. filled = question.filled,
  152. time_of_filling = question.time_of_filling,
  153. created_by = question.created_by,
  154. wrong_percent = question.wrong_percent,
  155. question_type_name = type.Name,
  156. questione_lib_name = libary.name
  157. };
  158. return query;
  159. }
  160. /// <summary>
  161. /// 修改分配的试卷ID
  162. /// </summary>
  163. /// <param name="test_id"></param>
  164. /// <param name="userid"></param>
  165. /// <param name="paperID"></param>
  166. /// <returns></returns>
  167. public bool UpdatePaperID(decimal test_id, string userid, decimal paperID)
  168. {
  169. //删除原试卷数据
  170. var paper = from p in PaperRepository.Entities
  171. join t in onlinetestRepository.Entities on p.test_paper_parent_id equals t.test_paper_id
  172. join r in ExamResultRepository.Entities on p.test_paper_id equals r.test_paper_id
  173. where t.onlinetest_id == test_id
  174. where r.user_id == userid
  175. select p;
  176. if (paper != null && paper.Count() > 0)
  177. {
  178. var paperid = paper.ToArray()[0].test_paper_id;
  179. Expression<Func<test_paper_question_set, bool>> whereExpression = (q => q.test_paper_id == paperid);
  180. question_setRepository.UnitOfWork.Delete(whereExpression);
  181. //context.test_paper_question_set_fortest.Delete(q => q.test_paper_id == paperid);
  182. Expression<Func<test_paper, bool>> paperExpression = (q => q.test_paper_id == paperid);
  183. PaperRepository.UnitOfWork.Delete(paperExpression);
  184. question_setRepository.UnitOfWork.Commit();
  185. }
  186. var result = ExamResultRepository.Entities.Where(q => q.onlinetest_id == test_id && q.user_id == userid).FirstOrDefault();
  187. if (result != null)
  188. {
  189. result.test_paper_id = paperID;
  190. question_setRepository.UnitOfWork.Commit();
  191. return true;
  192. }
  193. return false;
  194. }
  195. public void AddExchange(decimal test_id, int type, int receiveType, string receiver, int extendIntData)
  196. {
  197. test_exchange exchange = new test_exchange()
  198. {
  199. onlinetest_id = (int)test_id,
  200. type = type,
  201. receiveType = receiveType,
  202. receiver = receiver,
  203. content = null,
  204. extendIntData = extendIntData,
  205. handled = false,
  206. sendTime = DateTime.Now
  207. };
  208. exchangeRepository.UnitOfWork.Add(exchange);
  209. //// insert into dbo.test_exchange
  210. ////(onlinetest_id,[type],receiveType,receiver,content,extendIntData,extendStringData,handled,sendTime )
  211. ////values(@test_id,@type,@receiveType,@recevier,@content,@extendIntData,@extendStringData,0,GETDATE())
  212. //Execute("dbo.sp_insert_exchange",
  213. // new KeyValuePair<string, object>("@test_id", test_id),
  214. // new KeyValuePair<string, object>("@type", type),
  215. // new KeyValuePair<string, object>("@receiveType", receiveType),
  216. // new KeyValuePair<string, object>("@recevier", receiver),
  217. // new KeyValuePair<string, object>("@content", null),
  218. // new KeyValuePair<string, object>("@extendIntData", extendIntData),
  219. // new KeyValuePair<string, object>("@extendStringData", null));
  220. }
  221. public DataSet PaperBuilder(decimal test_id, decimal paper_id)
  222. {
  223. var ds = Query("dbo.sp_fetch_paper",
  224. new KeyValuePair<string, object>("@onlinetest_id", test_id),
  225. new KeyValuePair<string, object>("@test_paper_id", paper_id));
  226. return (ds);
  227. }
  228. public object ListExchange(decimal test_id, int receiveType, string receiver, DateTime? sendTime)
  229. {
  230. var ds = Query("dbo.sp_get_exchange",
  231. new KeyValuePair<string, object>("@test_id", test_id),
  232. new KeyValuePair<string, object>("@receiveType", receiveType),
  233. new KeyValuePair<string, object>("@recevier", receiver),
  234. new KeyValuePair<string, object>("@sendTime", sendTime));
  235. var tb = ds.Tables[0];
  236. var query = tb.AsEnumerable().Select(t => new { type = t.Field<int>("type"), extendInt = t.Field<int>("extendIntData"), id = t.Field<decimal>("id"), userid = t.Field<string>("receiver") }).ToArray();
  237. return query;
  238. }
  239. public bool RootReadPaper(decimal testid, string userid)
  240. {
  241. var result = true;
  242. try
  243. {
  244. var q = onlinetestRepository.GetSingle(to => to.onlinetest_id == testid);
  245. //&& q.test_method_set_id.Value == 0
  246. if (q.isReadPaper.Value == true)
  247. {
  248. var readuser = Guid.Parse(q.created_by);
  249. var query = UserRepository.GetSingle(e => e.UserID == readuser);
  250. Execute("dbo.sp_autoReadPaper",
  251. new KeyValuePair<string, object>("@onlinetest_id", testid),
  252. new KeyValuePair<string, object>("@readerUser", query.Name),
  253. new KeyValuePair<string, object>("@userids", userid));
  254. }
  255. }
  256. catch (Exception esx)
  257. {
  258. result = false;
  259. string message = esx.Message;
  260. }
  261. return result;
  262. }
  263. }
  264. }