TestPaperService.cs 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. using DocumentFormat.OpenXml.Office2010.Excel;
  2. using Exam.Infrastructure.Data.Entity;
  3. using Exam.Infrastructure.Data.Interface;
  4. using Exam.Infrastructure.Enums;
  5. using Exam.Infrastructure.Extensions;
  6. using Exam.Repository.Sqlsugar;
  7. using Exam.Share.ViewResponses.TestPaper;
  8. using Hotline.Application.Exam.Core.Extensions;
  9. using Hotline.Application.Exam.Extensions;
  10. using Hotline.Application.Exam.Proxy;
  11. using Hotline.Application.Exam.QueryExtensions.TestPapers;
  12. using Hotline.Exams.TestPapers;
  13. using Hotline.Repository.SqlSugar;
  14. using Hotline.Repository.SqlSugar.DataPermissions;
  15. using Hotline.Repository.SqlSugar.Exam.Core.Constants;
  16. using Hotline.Repository.SqlSugar.Exam.Interfaces.Questions;
  17. using Hotline.Repository.SqlSugar.Exam.Interfaces.TestPapers;
  18. using Hotline.Repository.SqlSugar.Exam.Repositories.TestPapers;
  19. using Hotline.Share.Dtos.Questions;
  20. using Hotline.Share.Dtos.TestPapers;
  21. using Hotline.Share.Dtos.Trains;
  22. using Hotline.Share.Requests.Exam;
  23. using Hotline.Share.Requests.TestPaper;
  24. using Hotline.Share.Tools;
  25. using Hotline.Share.ViewResponses.Exam;
  26. using Hotline.Share.ViewResponses.Questions;
  27. using JiebaNet.Segmenter.Common;
  28. using MapsterMapper;
  29. using Microsoft.Extensions.DependencyInjection;
  30. using NPOI.SS.Formula.Functions;
  31. using SqlSugar;
  32. using System.Collections.Immutable;
  33. using Hotline.Application.Exam.Core.Utilities;
  34. using Hotline.Application.Exam.Interface.TestPapers;
  35. using Hotline.Exams.ExamManages;
  36. using Hotline.Exams.Questions;
  37. using Hotline.Repository.SqlSugar.Exam.Repositories;
  38. using XF.Domain.Authentications;
  39. using XF.Domain.Dependency;
  40. using XF.Domain.Repository;
  41. using ExamQuestion = Hotline.Exams.Questions.ExamQuestion;
  42. using ExamQuestionAnswer = Hotline.Exams.Questions.ExamQuestionAnswer;
  43. using ExamQuestionKnowladge = Hotline.Exams.Questions.ExamQuestionKnowladge;
  44. using ExamQuestionOptions = Hotline.Exams.Questions.ExamQuestionOptions;
  45. using ExamQuestionSourceware = Hotline.Exams.Questions.ExamQuestionSourceware;
  46. using Hotline.Repository.SqlSugar.Exam.Service;
  47. using Hotline.Repository.SqlSugar.Exam.Extensions;
  48. namespace Hotline.Application.Exam.Service.TestPapers
  49. {
  50. public class TestPaperService : ApiService<ExamTestPaper, AddTestPaperDto, UpdateTestPaperDto, HotlineDbContext>, ITestPaperService, IScopeDependency
  51. {
  52. private readonly ITestPaperRepository _repository;
  53. private readonly ITestPaperItemRepository _testPaperItemRepository;
  54. private readonly ITestPaperRuleRepository _testPaperRuleRepository;
  55. private readonly ITestPaperRuleTagRepository _testPaperRuleTagRepository;
  56. private readonly ITestPaperItemAnswerRepository _testPaperItemAnswerRepository;
  57. private readonly ITestPaperItemOptionsRepository _testPaperItemOptionsRepository;
  58. private readonly ITestPaperItemSourcewareRepository _testPaperItemSourcewareRepository;
  59. private readonly ITestPaperItemKnowladgeRepository _testPaperItemKnowladgeRepository;
  60. private readonly IDataPermissionFilterBuilder _dataPermissionFilterBuilder;
  61. private readonly IServiceProvider _serviceProvider;
  62. private readonly ISessionContext _sessionContext;
  63. private readonly IMapper _mapper;
  64. private TestPaperProxy _testPaperProxy;
  65. private AddTestPaperDto _addTestPaperDto;
  66. public TestPaperService(ITestPaperRepository repository,
  67. ITestPaperRuleRepository testPaperRuleRepository,
  68. ITestPaperRuleTagRepository testPaperRuleTagRepository,
  69. ITestPaperItemRepository testPaperItemRepository,
  70. ITestPaperItemAnswerRepository testPaperItemAnswerRepository,
  71. ITestPaperItemOptionsRepository testPaperItemOptionsRepository,
  72. ITestPaperItemSourcewareRepository testPaperItemSourcewareRepository,
  73. ITestPaperItemKnowladgeRepository testPaperItemKnowladgeRepository,
  74. IDataPermissionFilterBuilder dataPermissionFilterBuilder, IServiceProvider serviceProvider,
  75. ISessionContext sessionContext,
  76. IMapper mapper) : base(repository, mapper, sessionContext)
  77. {
  78. _repository = repository;
  79. _testPaperItemRepository = testPaperItemRepository;
  80. _testPaperRuleRepository = testPaperRuleRepository;
  81. _testPaperRuleTagRepository = testPaperRuleTagRepository;
  82. this._testPaperItemAnswerRepository = testPaperItemAnswerRepository;
  83. this._testPaperItemOptionsRepository = testPaperItemOptionsRepository;
  84. this._testPaperItemSourcewareRepository = testPaperItemSourcewareRepository;
  85. this._testPaperItemKnowladgeRepository = testPaperItemKnowladgeRepository;
  86. _dataPermissionFilterBuilder = dataPermissionFilterBuilder;
  87. _serviceProvider = serviceProvider;
  88. this._sessionContext = sessionContext;
  89. _mapper = mapper;
  90. }
  91. #region public method
  92. public async Task<TestPaperDto> GetAsync(EntityQueryRequest entityQueryRequest)
  93. {
  94. var entity = await _repository.GetAsync(entityQueryRequest.Id);
  95. var testPaperDto = _mapper.Map<TestPaperDto>(entity);
  96. testPaperDto.TestPaperItemDtos = await GetTestPaperItemDtos(entityQueryRequest);
  97. testPaperDto.TestPaperRuleDtos = await GetTestPaperRuleDtos(entityQueryRequest);
  98. return testPaperDto;
  99. }
  100. public async Task<(int, List<TestPaperViewResponse>)> GetListAsync(TestPaperPagedRequest queryRequest)
  101. {
  102. queryRequest.Status = queryRequest.Status ?? Share.Enums.Exams.EPublicStatus.Valid;
  103. ISugarQueryable<TestPaperViewResponse> queryable = QueryResult(queryRequest);
  104. var result = await queryable.ToListAsync();
  105. var total = await queryable.CountAsync();
  106. return (total, result);
  107. }
  108. public async Task<PageViewResponse<TestPaperViewResponse>> GetPagedListAsync(TestPaperPagedRequest queryRequest)
  109. {
  110. ISugarQueryable<TestPaperViewResponse> queryable = QueryResult(queryRequest);
  111. var list = await queryable.ToPageListAsync(queryRequest.PageIndex, queryRequest.PageSize);
  112. var total = await queryable.CountAsync();
  113. var result = new TestPaperPageViewResponse
  114. {
  115. Items = list,
  116. Pagination = new Pagination(queryRequest.PageIndex, queryRequest.PageSize, total)
  117. };
  118. return result;
  119. }
  120. /// <summary>
  121. /// 新增试卷
  122. /// </summary>
  123. /// <param name="actionRequest"></param>
  124. /// <param name="cancellationToken"></param>
  125. /// <returns></returns>
  126. public override async Task<string> AddAsync(AddTestPaperDto actionRequest, CancellationToken cancellationToken)
  127. {
  128. base.StartTran();
  129. var id = await base.AddAsync(actionRequest, cancellationToken);
  130. ResolveTestPaperId(actionRequest, id);
  131. base.Entity.TestPaperItems = await AddTestPaperItems(actionRequest, cancellationToken);
  132. var testPaperItemAnswers = await AddTestPaperItemAnswer(actionRequest, cancellationToken);
  133. var testPaperItemOptions = await AddTestPaperItemOptions(actionRequest, cancellationToken);
  134. var testPaperItemKnowlagdes = await AddTestPaperItemKnowladgeAsync(actionRequest, cancellationToken);
  135. var testPaperItemSourcewares = await AddTestPaperItemSourcewareAsync(actionRequest, cancellationToken);
  136. base.Entity.TestPaperItems.ForEach(item =>
  137. {
  138. item.TestPaperItemSourcewares = testPaperItemSourcewares.Where(x => item.Id == x.TestPaperItemId).ToList();
  139. item.TestPaperItemAnswers = testPaperItemAnswers.Where(x => item.Id == x.TestPaperItemId).ToList();
  140. item.TestPaperItemKnowlagdes = testPaperItemKnowlagdes.Where(x => item.Id == x.TestPaperItemId).ToList();
  141. item.TestPaperItemOptionses = testPaperItemOptions.Where(x => item.Id == x.TestPaperItemId).ToList();
  142. });
  143. //base.Entity.TestPaperRules = await AddTestPaperRules(actionRequest, cancellationToken);
  144. //var testPaperRuleTags = await AddTestPaperRuleTags(actionRequest, cancellationToken);
  145. //base.Entity.TestPaperRules?.ForEach(item =>
  146. //{
  147. // item.TestPaperRuleTags = testPaperRuleTags.Where(m => item.Id == m.TestPaperRuleId).ToList();
  148. //});
  149. await base.Complete(base.Entity, OperationConstant.Create);
  150. //await GenerateTestPaper(new GenerateTestPaperRequest { TestPaperId = id }, cancellationToken);
  151. return id;
  152. }
  153. /// <summary>
  154. /// 修改试卷
  155. /// </summary>
  156. /// <param name="actionRequest"></param>
  157. /// <param name="cancellationToken"></param>
  158. /// <returns></returns>
  159. public override async Task UpdateAsync(UpdateTestPaperDto actionRequest, CancellationToken cancellationToken)
  160. {
  161. base.StartTran();
  162. await base.UpdateAsync(actionRequest, cancellationToken);
  163. ResolveTestPaperId(actionRequest, actionRequest.Id);
  164. _addTestPaperDto = _mapper.Map<AddTestPaperDto>(actionRequest);
  165. base.Entity.TestPaperItems = await ModifyTestPaperItems(actionRequest, cancellationToken);
  166. var testPaperItemAnswers = await ModifyTestPaperItemAnswer(actionRequest, cancellationToken);
  167. var testPaperItemOptions = await ModifyTestPaperItemOptions(actionRequest, cancellationToken);
  168. var testPaperItemKnowlagdes = await ModifyTestPaperItemKnowladgeAsync(actionRequest, cancellationToken);
  169. var testPaperItemSourcewares = await ModifyTestPaperItemSourcewareAsync(actionRequest, cancellationToken);
  170. base.Entity.TestPaperItems.ForEach(item =>
  171. {
  172. item.TestPaperItemAnswers = testPaperItemAnswers.Where(x => item.Id == x.TestPaperItemId).ToList();
  173. item.TestPaperItemSourcewares = testPaperItemSourcewares.Where(x => item.Id == x.TestPaperItemId).ToList();
  174. item.TestPaperItemKnowlagdes = testPaperItemKnowlagdes.Where(x => item.Id == x.TestPaperItemId).ToList();
  175. item.TestPaperItemOptionses = testPaperItemOptions.Where(x => item.Id == x.TestPaperItemId).ToList();
  176. });
  177. await base.Complete(base.Entity, OperationConstant.Update);
  178. //await GenerateTestPaper(new GenerateTestPaperRequest { TestPaperId = actionRequest.Id }, cancellationToken);
  179. }
  180. /// <summary>
  181. /// 删除试卷
  182. /// </summary>
  183. /// <param name="entityQueryRequest"></param>
  184. /// <param name="cancellationToken"></param>
  185. /// <returns></returns>
  186. public override async Task DeleteAsync(EntityQueryRequest entityQueryRequest, CancellationToken cancellationToken)
  187. {
  188. var testPaperRuleTags = await GetTestPaperRuleTags(entityQueryRequest);
  189. await base.DeleteAsync(entityQueryRequest, cancellationToken);
  190. var tempEntityQueryRequest = ExpressionableUtility.CreateExpression<ExamTestPaperItem>()
  191. .AndIF(entityQueryRequest.Id.IsNotNullOrEmpty(), x => x.TestPaperId == entityQueryRequest.Id)
  192. .AndIF(entityQueryRequest.Ids.IsNotNullOrEmpty(), x => entityQueryRequest.Ids.Contains(x.TestPaperId)).ToEntityQueryRequest<ExamTestPaperItem>();
  193. await DeleteTestPaperItems(tempEntityQueryRequest, cancellationToken);
  194. tempEntityQueryRequest = ExpressionableUtility.CreateExpression<ExamTestPaperRule>()
  195. .AndIF(entityQueryRequest.Id.IsNotNullOrEmpty(), x => x.TestPaperId == entityQueryRequest.Id)
  196. .AndIF(entityQueryRequest.Ids.IsNotNullOrEmpty(), x => entityQueryRequest.Ids.Contains(x.TestPaperId)).ToEntityQueryRequest<ExamTestPaperRule>();
  197. await DeleteTestPaperRules(tempEntityQueryRequest, cancellationToken);
  198. var ids = testPaperRuleTags.Select(x => x.Id).ToList();
  199. tempEntityQueryRequest = ExpressionableUtility.CreateExpression<ExamTestPaperRuleTag>()
  200. .AndIF(ids.IsNotNull(), x => ids.Contains(x.Id)).ToEntityQueryRequest<ExamTestPaperRuleTag>();
  201. await DeleteTestPaperRuleTags(tempEntityQueryRequest, cancellationToken);
  202. }
  203. /// <summary>
  204. /// 获取试卷试题数量
  205. /// </summary>
  206. /// <returns></returns>
  207. /// <exception cref="NotImplementedException"></exception>
  208. public async Task<List<TestPaperQuestionCountViewResponse>> GetTestPaperQuestionCount(TestPaperQuestionCountRequest testPaperQuestionCountRequest)
  209. {
  210. var testPaperQuestionCountViewResponses = new List<TestPaperQuestionCountViewResponse>();
  211. if (testPaperQuestionCountRequest.TestPaperId.IsNotNullOrEmpty())
  212. {
  213. testPaperQuestionCountViewResponses = await CalcuteManualQuestionCount(testPaperQuestionCountRequest);
  214. }
  215. else if(testPaperQuestionCountRequest.ExtractRuleId.IsNotNullOrEmpty())
  216. {
  217. testPaperQuestionCountViewResponses = await CalcuteRandomQuestionCount(testPaperQuestionCountRequest);
  218. }
  219. return testPaperQuestionCountViewResponses;
  220. }
  221. /// <summary>
  222. /// 根据规则获取试题
  223. /// </summary>
  224. /// <param name="testPaperQuestionRequest"></param>
  225. /// <returns></returns>
  226. /// <exception cref="NotImplementedException"></exception>
  227. public async Task<List<SimpleQuestionViewResponse>> GetQuestionDtos(TestPaperQuestionRequest testPaperQuestionRequest)
  228. {
  229. var questions = await GetQuestions(testPaperQuestionRequest);
  230. return questions;
  231. }
  232. /// <summary>
  233. /// 组卷
  234. /// </summary>
  235. /// <param name="generateTestPaperRequest"></param>
  236. /// <param name="cancellationToken"></param>
  237. /// <returns></returns>
  238. public async Task GenerateTestPaper(GenerateTestPaperRequest generateTestPaperRequest, CancellationToken cancellationToken)
  239. {
  240. var testPaper = await _repository.GetAsync(x => x.Id == generateTestPaperRequest.TestPaperId);
  241. if (testPaper != null)
  242. {
  243. //随机组卷
  244. if (testPaper.Mode == Share.Enums.Exams.EExamMode.Random)
  245. {
  246. var questions = await GetQuestions(generateTestPaperRequest);
  247. _testPaperProxy = new TestPaperProxy(_repository,
  248. _testPaperItemRepository,
  249. _testPaperItemAnswerRepository,
  250. _testPaperItemOptionsRepository,
  251. _testPaperItemSourcewareRepository,
  252. _testPaperItemKnowladgeRepository,
  253. _dataPermissionFilterBuilder,
  254. _serviceProvider,
  255. _mapper,
  256. _sessionContext);
  257. //删除已经生成的试卷试题
  258. await _testPaperProxy.DeleteTestPaperItems(generateTestPaperRequest.TestPaperId, cancellationToken);
  259. await _testPaperProxy.GenerateQuestion(questions, generateTestPaperRequest.TestPaperId, cancellationToken);
  260. }
  261. }
  262. }
  263. private async Task<List<SimpleQuestionViewResponse>> GetQuestions(TestPaperQuestionRequest testPaperQuestionRequest)
  264. {
  265. var questionRepository = new ExamRepository<ExamQuestion>(_uow, _dataPermissionFilterBuilder, _serviceProvider);
  266. var questionTagRepository = new ExamRepository<ExamQuestionTag>(_uow, _dataPermissionFilterBuilder, _serviceProvider);
  267. var expression = testPaperQuestionRequest.GetExpression();
  268. var questionExpression = testPaperQuestionRequest.GetQuestionExpression();
  269. var questionTagTable = questionTagRepository.Queryable().Where(expression);
  270. var questionTable = questionRepository.Queryable().Where(questionExpression);
  271. var questions = await questionTable.LeftJoin(questionTagTable, (q, qt) => q.Id == qt.QuestionId)
  272. .Select((q, qt) => q).Take(testPaperQuestionRequest.Count).OrderBy(q => SqlFunc.GetRandom()).ToListAsync();
  273. var questionDtos = new List<SimpleQuestionViewResponse>();
  274. questions.ForEach(item =>
  275. {
  276. var questionDto = _mapper.Map<SimpleQuestionViewResponse>(item);
  277. if (questionDto != null)
  278. {
  279. questionDtos.Add(questionDto);
  280. }
  281. });
  282. return questionDtos;
  283. }
  284. private async Task<List<ExamQuestion>> GetQuestions(GenerateTestPaperRequest generateTestPaperRequest)
  285. {
  286. var questionRepository = new ExamRepository<ExamQuestion>(_uow, _dataPermissionFilterBuilder, _serviceProvider);
  287. var questionTagRepository = new ExamRepository<ExamQuestionTag>(_uow, _dataPermissionFilterBuilder, _serviceProvider);
  288. var testPaperRuleTable = _testPaperRuleRepository.Queryable().Where(x => x.TestPaperId == generateTestPaperRequest.TestPaperId);
  289. var testPaperTagTable = _testPaperRuleTagRepository.Queryable();
  290. var questionTagTable = questionTagRepository.Queryable();
  291. var questionTable = questionRepository.Queryable();
  292. var count = await testPaperRuleTable.Select(x => x.Count).FirstAsync();
  293. var questions = await questionTable.LeftJoin(questionTagTable, (q, qt) => q.Id == qt.QuestionId)
  294. .LeftJoin(testPaperTagTable, (q, qt, tt) => qt.TagId == tt.TagId)
  295. .LeftJoin(testPaperRuleTable, (q, qt, tt, tpr) => tt.TestPaperRuleId == tpr.Id)
  296. .Where((q, qt, tt, tpr) => q.QuestionType == tpr.QuestionType)
  297. .Select((q, qt, tt, tpr) => q).Take(count).OrderBy(q => SqlFunc.GetRandom()).ToListAsync();
  298. return questions;
  299. }
  300. /// <summary>
  301. /// 获取标签试题数
  302. /// </summary>
  303. /// <param name="tagQuestionCountRequest"></param>
  304. /// <returns></returns>
  305. /// <exception cref="NotImplementedException"></exception>
  306. public async Task<TagQuestionCountViewResponse> GetTagQuestionCount(TagQuestionCountRequest tagQuestionCountRequest)
  307. {
  308. var expression = tagQuestionCountRequest.GetExpression();
  309. var questionExpression = tagQuestionCountRequest.GetQuestionExpression();
  310. var tagQuestionRepository = new ExamRepository<ExamQuestionTag>(_uow, _dataPermissionFilterBuilder, _serviceProvider);
  311. var quesitonRepository = new ExamRepository<ExamQuestion>(_uow, _dataPermissionFilterBuilder, _serviceProvider);
  312. var tagQuestionTable = tagQuestionRepository.Queryable().Where(expression);
  313. var questionTable = quesitonRepository.Queryable().Where(questionExpression);
  314. var taqQuestions = await tagQuestionTable.InnerJoin(questionTable, (t, q) => t.QuestionId == q.Id).GroupBy((t, q) => q.Id)
  315. .Select((t, q) =>q.Id)
  316. .ToListAsync();
  317. var result = new TagQuestionCountViewResponse
  318. {
  319. TotalCount = taqQuestions != null ? taqQuestions.Count() : 0
  320. };
  321. return result;
  322. }
  323. #endregion
  324. #region private method
  325. /// <summary>
  326. /// 获取查询结果
  327. /// </summary>
  328. /// <param name="queryRequest"></param>
  329. /// <returns></returns>
  330. private ISugarQueryable<TestPaperViewResponse> QueryResult(TestPaperPagedRequest queryRequest)
  331. {
  332. var expression = queryRequest.GetExpression();
  333. var query = _repository.Queryable().Where(expression);
  334. var querable = query.OrderBy(o => o.SortIndex).Select(m => new TestPaperViewResponse
  335. {
  336. Name = m.Name,
  337. ExamType = m.ExamType,
  338. Mode = m.Mode,
  339. Code = m.Code,
  340. Id = m.Id,
  341. Status = m.Status,
  342. SortIndex = m.SortIndex,
  343. Remark = m.Remark,
  344. });
  345. return querable;
  346. }
  347. private async Task<List<ExamTestPaperItemOptions>> ModifyTestPaperItemOptions(UpdateTestPaperDto actionRequest, CancellationToken cancellationToken)
  348. {
  349. if (actionRequest.TestPaperItemDtos == null) return null;
  350. var questionIds = actionRequest.TestPaperItemDtos.Where(x => x.QuestionType.CheckSelectType()).Select(x => x.QuestionId).ToList();
  351. var entityQueryRequest = new EntityQueryRequest
  352. {
  353. Expression = ExpressionableUtility.CreateExpression<ExamTestPaperItemOptions>()
  354. .AndIF(questionIds.IsNotNullOrEmpty(), x => questionIds.Contains(x.QuestionId)).ToExpression()
  355. };
  356. await DeleteTestPaperItemOptions(entityQueryRequest, cancellationToken);
  357. return await AddTestPaperItemOptions(_addTestPaperDto, cancellationToken);
  358. }
  359. private async Task DeleteTestPaperItemOptions(EntityQueryRequest entityQueryRequest, CancellationToken cancellationToken)
  360. {
  361. await _testPaperItemOptionsRepository.DeleteWithValidateAsync(entityQueryRequest, cancellationToken);
  362. }
  363. private async Task<List<ExamTestPaperItemAnswer>> ModifyTestPaperItemAnswer(UpdateTestPaperDto actionRequest, CancellationToken cancellationToken)
  364. {
  365. if (actionRequest.TestPaperItemDtos == null) return null;
  366. var questionIds = actionRequest.TestPaperItemDtos.Where(x => !x.QuestionType.CheckSelectType()).Select(x => x.QuestionId).ToList();
  367. var entityQueryRequest = new EntityQueryRequest
  368. {
  369. Expression = ExpressionableUtility.CreateExpression<ExamTestPaperItemAnswer>()
  370. .AndIF(questionIds.IsNotNullOrEmpty(), x => questionIds.Contains(x.QuestionId)).ToExpression()
  371. };
  372. await DeleteTestPaperItemAnswer(entityQueryRequest, cancellationToken);
  373. return await AddTestPaperItemAnswer(_addTestPaperDto, cancellationToken);
  374. }
  375. private async Task DeleteTestPaperItemAnswer(EntityQueryRequest entityQueryRequest, CancellationToken cancellationToken)
  376. {
  377. await _testPaperItemAnswerRepository.DeleteWithValidateAsync(entityQueryRequest, cancellationToken);
  378. }
  379. private async Task<List<ExamTestPaperRule>> AddTestPaperRules(AddTestPaperDto actionRequest, CancellationToken cancellationToken)
  380. {
  381. if (actionRequest.TestPaperRuleDtos == null) return null;
  382. if (actionRequest.Mode == Share.Enums.Exams.EExamMode.Manual) return null;
  383. actionRequest.TestPaperRuleDtos.ResolveOperationStatus();
  384. var testPaperRuleDtos = actionRequest.TestPaperRuleDtos.Where(x => x.OperationStatus == EEOperationStatus.Add).ToList();
  385. var testPaperRules = _mapper.Map<List<ExamTestPaperRule>>(testPaperRuleDtos);
  386. testPaperRules.ToInsert(_sessionContext);
  387. await _testPaperRuleRepository.ValidateAddAsync(testPaperRules, cancellationToken);
  388. return testPaperRules;
  389. }
  390. private async Task<List<ExamTestPaperItem>> AddTestPaperItems(AddTestPaperDto actionRequest, CancellationToken cancellationToken)
  391. {
  392. if (actionRequest.TestPaperItemDtos == null) return null;
  393. actionRequest.TestPaperItemDtos.ResolveOperationStatus();
  394. var testPaperItemDtos = actionRequest.TestPaperItemDtos.Where(x => x.OperationStatus == EEOperationStatus.Add).ToList();
  395. var testPaperItems = _mapper.Map<List<ExamTestPaperItem>>(testPaperItemDtos);
  396. //试题排序,为后续考试顺序出题做准备
  397. var index = 0;
  398. testPaperItems.ForEach(x =>
  399. {
  400. x.SortIndex = ++index;
  401. });
  402. testPaperItems.ToInsert(_sessionContext);
  403. await _testPaperItemRepository.ValidateAddAsync(testPaperItems, cancellationToken);
  404. return testPaperItems;
  405. }
  406. private async Task<List<ExamTestPaperRuleTag>> AddTestPaperRuleTags(AddTestPaperDto actionRequest, CancellationToken cancellationToken)
  407. {
  408. if (actionRequest.TestPaperRuleDtos == null) return null;
  409. actionRequest.TestPaperRuleDtos.ResolveOperationStatus();
  410. var testPaperRuleDtos = actionRequest.TestPaperRuleDtos.Where(x => x.OperationStatus == EEOperationStatus.Add).ToList();
  411. var testPaperRuleTagDtos = new List<AddTestPaperRuleTagDto>();
  412. testPaperRuleDtos.ForEach(item =>
  413. {
  414. var testPaperRuleId = string.Empty;
  415. if (base.Entity != null)
  416. {
  417. //var testPaperRule = base.Entity.TestPaperRules.FirstOrDefault(x => x.TestPaperId == item.TestPaperId
  418. //&& x.QuestionType == item.QuestionType && x.Count == item.Count && x.DifficultyLevel == item.DifficultyLevel);
  419. //testPaperRuleId = testPaperRule?.Id;
  420. }
  421. item.TestPaperRuleTagDtos.ForEach(x =>
  422. {
  423. x.TestPaperRuleId = testPaperRuleId;
  424. });
  425. testPaperRuleTagDtos.AddRange(item.TestPaperRuleTagDtos);
  426. });
  427. testPaperRuleTagDtos.ResolveOperationStatus();
  428. var testPaperRuleTags = _mapper.Map<List<ExamTestPaperRuleTag>>(testPaperRuleTagDtos);
  429. testPaperRuleTags.ToInsert(_sessionContext);
  430. await _testPaperRuleTagRepository.ValidateAddAsync(testPaperRuleTags, cancellationToken);
  431. return testPaperRuleTags;
  432. }
  433. private async Task<List<ExamTestPaperRule>> ModifyTestPaperRules(UpdateTestPaperDto actionRequest, CancellationToken cancellationToken)
  434. {
  435. if (actionRequest.TestPaperRuleDtos == null) return null;
  436. if (actionRequest.Mode == Share.Enums.Exams.EExamMode.Manual) return null;
  437. var all = await _testPaperRuleRepository.Queryable().Where(x => x.TestPaperId == actionRequest.Id).ToListAsync();
  438. actionRequest.TestPaperRuleDtos.ResolveOperationStatus(all);
  439. var testPaperRules = new List<ExamTestPaperRule>();
  440. testPaperRules.AddRangeExt(await AddTestPaperRules(actionRequest, cancellationToken));
  441. testPaperRules.AddRangeExt(await UpdateTestPaperRules(actionRequest,all, cancellationToken));
  442. var entityQueryRequest = ExpressionableUtility.CreateExpression<ExamTestPaperRule>()
  443. .AndIF(actionRequest.Id.IsNotNullOrEmpty(), x => x.TestPaperId == actionRequest.Id).ToEntityQueryRequest<ExamTestPaperRule>();
  444. await DeleteTestPaperRules(entityQueryRequest, cancellationToken);
  445. return testPaperRules;
  446. }
  447. private async Task DeleteTestPaperRules(EntityQueryRequest entityQueryRequest, CancellationToken cancellationToken)
  448. {
  449. await _testPaperRuleRepository.DeleteWithValidateAsync(entityQueryRequest, cancellationToken);
  450. }
  451. private async Task<List<ExamTestPaperRule>> UpdateTestPaperRules(UpdateTestPaperDto actionRequest,List<ExamTestPaperRule> all, CancellationToken cancellationToken)
  452. {
  453. if (actionRequest.TestPaperRuleDtos == null) return null;
  454. var testPaperRuleDtos = actionRequest.TestPaperRuleDtos.Where(x => x.OperationStatus == EEOperationStatus.Update).ToList();
  455. var ids = testPaperRuleDtos.Select(x => x.Id);
  456. var testPaperRules = all.Where(x => ids.Contains(x.TestPaperId)).ToList();
  457. var entities = new List<ExamTestPaperRule>();
  458. foreach (var testPaperRuleDto in testPaperRuleDtos)
  459. {
  460. var entity = testPaperRules.FirstOrDefault(x => x.Id == testPaperRuleDto.Id);
  461. entity = _mapper.Map(testPaperRuleDto, entity);
  462. entity.TestPaperId = actionRequest.Id;
  463. }
  464. entities.ToUpdate(_sessionContext);
  465. await _testPaperRuleRepository.ValidateUpdateAsync(entities, cancellationToken);
  466. return entities;
  467. }
  468. private async Task<List<ExamTestPaperRuleTag>> ModifyTestPaperRuleTags(UpdateTestPaperDto actionRequest, CancellationToken cancellationToken)
  469. {
  470. if (actionRequest.TestPaperRuleDtos == null) return null;
  471. var ruleIds = actionRequest.TestPaperRuleDtos.Select(x => x.Id);
  472. var all = await _testPaperRuleTagRepository.Queryable().Where(x => ruleIds.Contains(x.TestPaperRuleId)).ToListAsync();
  473. actionRequest.TestPaperRuleDtos.ResolveOperationStatus(all);
  474. var testPaperRuleTags = new List<ExamTestPaperRuleTag>();
  475. testPaperRuleTags.AddRangeExt(await AddTestPaperRuleTags(actionRequest, cancellationToken));
  476. testPaperRuleTags.AddRangeExt(await UpdateTestPaperRuleTags(actionRequest, all, cancellationToken));
  477. var ids = actionRequest.TestPaperRuleDtos.Where(x => x.OperationStatus == EEOperationStatus.Delete).Select(x => x.Id).ToList();
  478. var entityQueryRequest = ExpressionableUtility.CreateExpression<ExamTestPaperRuleTag>()
  479. .AndIF(ids.IsNullOrEmpty(), x => ids.Contains(x.TestPaperRuleId)).ToEntityQueryRequest<ExamTestPaperRuleTag>();
  480. await DeleteTestPaperRuleTags(entityQueryRequest, cancellationToken);
  481. return testPaperRuleTags;
  482. }
  483. private async Task DeleteTestPaperRuleTags(EntityQueryRequest entityQueryRequest, CancellationToken cancellationToken)
  484. {
  485. await _testPaperRuleTagRepository.DeleteWithValidateAsync(entityQueryRequest, cancellationToken);
  486. }
  487. private async Task<List<ExamTestPaperRuleTag>> UpdateTestPaperRuleTags(UpdateTestPaperDto actionRequest,List<ExamTestPaperRuleTag> all, CancellationToken cancellationToken)
  488. {
  489. if (actionRequest.TestPaperRuleDtos == null) return null;
  490. var ruleIds = actionRequest.TestPaperRuleDtos.Where(x => x.OperationStatus == EEOperationStatus.Update).Select(x => x.Id).ToList();
  491. var testPaperRuleTags = all.Where(x => ruleIds.Contains(x.TestPaperRuleId)).ToList();
  492. var entities = new List<ExamTestPaperRuleTag>();
  493. foreach (var testPaperRuleTagDto in actionRequest.TestPaperRuleDtos.SelectMany(x=>x.TestPaperRuleTagDtos))
  494. {
  495. var entity = testPaperRuleTags.FirstOrDefault(x => x.Id == testPaperRuleTagDto.Id);
  496. entity = _mapper.Map<UpdateTestPaperRuleTagDto, ExamTestPaperRuleTag>(testPaperRuleTagDto,entity);
  497. }
  498. entities.ToUpdate(_sessionContext);
  499. await _testPaperRuleTagRepository.ValidateUpdateAsync(entities, cancellationToken);
  500. return entities;
  501. }
  502. private async Task<List<ExamTestPaperItem>> ModifyTestPaperItems(UpdateTestPaperDto actionRequest, CancellationToken cancellationToken)
  503. {
  504. if (actionRequest.TestPaperItemDtos == null) return null;
  505. var all = await _testPaperItemRepository.Queryable().Where(x => x.TestPaperId == actionRequest.Id).ToListAsync();
  506. actionRequest.TestPaperItemDtos.ResolveOperationStatus(all);
  507. var testPaperItems = new List<ExamTestPaperItem>();
  508. testPaperItems.AddRangeExt(await AddTestPaperItems(_addTestPaperDto, cancellationToken));
  509. testPaperItems.AddRangeExt(await UpdateTestPaperItems(actionRequest,all, cancellationToken));
  510. var entityQueryRequest = ExpressionableUtility.CreateExpression<ExamTestPaperItem>()
  511. .AndIF(actionRequest.Id.IsNotNullOrEmpty(), x => x.TestPaperId == actionRequest.Id).ToEntityQueryRequest<ExamTestPaperItem>();
  512. await DeleteTestPaperItems(entityQueryRequest, cancellationToken);
  513. return testPaperItems;
  514. }
  515. private async Task DeleteTestPaperItems(EntityQueryRequest entityQueryRequest, CancellationToken cancellationToken)
  516. {
  517. await _testPaperItemRepository.DeleteWithValidateAsync(entityQueryRequest, cancellationToken);
  518. }
  519. private async Task<List<ExamTestPaperItem>> UpdateTestPaperItems(UpdateTestPaperDto actionRequest,List<ExamTestPaperItem> all, CancellationToken cancellationToken)
  520. {
  521. if (actionRequest.TestPaperItemDtos == null) return null;
  522. var testPaperItemDtos = actionRequest.TestPaperItemDtos.Where(x => x.OperationStatus == EEOperationStatus.Update).ToList();
  523. var ids = testPaperItemDtos.Select(x => x.Id);
  524. var testPaperItems = all.Where(x => ids.Contains(x.TestPaperId)).ToList();
  525. var entities = new List<ExamTestPaperItem>();
  526. foreach (var testPaperItemDto in testPaperItemDtos)
  527. {
  528. var entity = testPaperItems.FirstOrDefault(x => x.Id == testPaperItemDto.Id);
  529. entity = _mapper.Map(testPaperItemDto, entity);
  530. entity.TestPaperId = actionRequest.Id;
  531. }
  532. entities.ToUpdate(_sessionContext);
  533. await _testPaperItemRepository.ValidateUpdateAsync(entities, cancellationToken);
  534. return entities;
  535. }
  536. /// <summary>
  537. /// 获取试卷规则
  538. /// </summary>
  539. /// <param name="entityQueryRequest"></param>
  540. /// <returns></returns>
  541. /// <exception cref="NotImplementedException"></exception>
  542. private async Task<List<TestPaperRuleDto>> GetTestPaperRuleDtos(EntityQueryRequest entityQueryRequest)
  543. {
  544. var testPaperRuleTable = _testPaperRuleRepository.Queryable().Where(x => x.TestPaperId == entityQueryRequest.Id);
  545. var testPaperRuleTags = await GetTestPaperRuleTags(entityQueryRequest);
  546. var queryable = testPaperRuleTable.Select(t => new TestPaperRuleDto
  547. {
  548. Id = t.Id,
  549. TestPaperId = t.TestPaperId,
  550. Count = t.Count,
  551. DifficultyLevel = t.DifficultyLevel,
  552. SortIndex = t.SortIndex,
  553. QuestionType = t.QuestionType,
  554. });
  555. var testPaperRuleTagDtos = _mapper.Map<List<TestPaperRuleTagDto>>(testPaperRuleTags);
  556. var testPaperRuleList = await queryable.ToListAsync();
  557. testPaperRuleList.ForEach(x => x.TestPaperRuleTagDtos = testPaperRuleTagDtos.Where(n => n.TestPaperRuleId == x.Id).ToList());
  558. return testPaperRuleList;
  559. }
  560. private async Task<List<TestPaperItemDto>> GetTestPaperItemDtos(EntityQueryRequest entityQueryRequest)
  561. {
  562. var testPaperRuleTable = _testPaperItemRepository.Queryable().Where(x => x.TestPaperId == entityQueryRequest.Id);
  563. var queryable = testPaperRuleTable.Select(t => new TestPaperItemDto
  564. {
  565. Id = t.Id,
  566. QuestionId = t.QuestionId,
  567. DifficultyLevel = t.DifficultyLevel,
  568. Title = t.Title,
  569. TestPaperId = t.TestPaperId,
  570. QuestionType = t.QuestionType
  571. });
  572. return await queryable.ToListAsync();
  573. }
  574. private void ResolveTestPaperId(AddTestPaperDto actionRequest, string id)
  575. {
  576. actionRequest.TestPaperRuleDtos?.ForEach(x =>
  577. {
  578. x.TestPaperId = id;
  579. });
  580. actionRequest.TestPaperItemDtos?.ForEach(x => x.TestPaperId = id);
  581. }
  582. private void ResolveTestPaperId(UpdateTestPaperDto actionRequest, string id)
  583. {
  584. actionRequest.TestPaperRuleDtos?.ForEach(x =>
  585. {
  586. x.TestPaperId = id;
  587. });
  588. actionRequest.TestPaperItemDtos?.ForEach(x => x.TestPaperId = id);
  589. }
  590. private async Task<List<ExamTestPaperRuleTag>> GetTestPaperRuleTags(EntityQueryRequest entityQueryRequest)
  591. {
  592. var expression = entityQueryRequest.GetTestPaperRuleExpression();
  593. var testPageRuleTable = _testPaperRuleRepository.Queryable().Where(expression);
  594. var testPageRuleTagTable = _testPaperRuleTagRepository.Queryable();
  595. var query = testPageRuleTagTable.InnerJoin(testPageRuleTable, (t, r) => t.TestPaperRuleId == r.Id).Select((t, r) => t);
  596. return await query.ToListAsync();
  597. }
  598. private async Task<List<TestPaperQuestionCountViewResponse>> CalcuteRandomQuestionCount(TestPaperQuestionCountRequest testPaperQuestionCountRequest)
  599. {
  600. var testPaperQuestionCountViewResponses = new List<TestPaperQuestionCountViewResponse>();
  601. var extractRuleRepository = new ExamRepository<ExamExtractRule>(_uow, _dataPermissionFilterBuilder, _serviceProvider);
  602. var ruleTagRepository = new ExamRepository<ExamRuleTag>(_uow, _dataPermissionFilterBuilder, _serviceProvider);
  603. var tagQuestionRepository = new ExamRepository<ExamTagQuestion>(_uow, _dataPermissionFilterBuilder, _serviceProvider);
  604. var expression = testPaperQuestionCountRequest.GetExpression();
  605. var exatractTable = extractRuleRepository.Queryable().Where(expression);
  606. var ruleTagTable = ruleTagRepository.Queryable();
  607. var tagQuestionTable = tagQuestionRepository.Queryable();
  608. var testPaperRules = await exatractTable.InnerJoin(ruleTagTable,(e,rt)=>e.Id == rt.RuleId)
  609. .InnerJoin(tagQuestionTable,(e,rt,tq)=>rt.RuleId == tq.RuleId && rt.TagId == tq.TagId)
  610. .GroupBy((e, rt, tq)=> tq.QuestionType)
  611. .Select((e,rt,tq)=> new ExamTagQuestion
  612. {
  613. QuestionType = tq.QuestionType,
  614. Count = SqlFunc.AggregateSum(tq.Count)
  615. })
  616. .ToListAsync();
  617. if (testPaperRules != null)
  618. {
  619. testPaperRules.ForEach(x => testPaperQuestionCountViewResponses.Add(new TestPaperQuestionCountViewResponse
  620. {
  621. QuestionType = x.QuestionType,
  622. Count = x.Count
  623. }));
  624. }
  625. return testPaperQuestionCountViewResponses;
  626. }
  627. private async Task<List<TestPaperQuestionCountViewResponse>> CalcuteManualQuestionCount(TestPaperQuestionCountRequest testPaperQuestionCountRequest)
  628. {
  629. var testPaperQuestionCountViewResponses = new List<TestPaperQuestionCountViewResponse>();
  630. var expression = testPaperQuestionCountRequest.GetTestPaperItemsExpression();
  631. var testPaperItemTable = _testPaperItemRepository.Queryable().Where(expression);
  632. testPaperQuestionCountViewResponses = await testPaperItemTable.GroupBy(t=>t.QuestionType).Select(t => new TestPaperQuestionCountViewResponse
  633. {
  634. QuestionType = t.QuestionType,
  635. Count = SqlFunc.AggregateCount(t.Id)
  636. }).ToListAsync();
  637. return testPaperQuestionCountViewResponses;
  638. }
  639. private async Task<List<ExamTestPaperItemOptions>> AddTestPaperItemOptions(AddTestPaperDto actionRequest, CancellationToken cancellationToken)
  640. {
  641. var questionOptionRepository = new ExamRepository<ExamQuestionOptions>(_uow, _dataPermissionFilterBuilder, _serviceProvider);
  642. var quesitonIds = actionRequest.TestPaperItemDtos.Select(x => x.QuestionId);
  643. var questionOptions = await questionOptionRepository.Queryable().Where(x => quesitonIds.Contains(x.QuestionId)).ToListAsync();
  644. var testPaperItemOptions = new List<ExamTestPaperItemOptions>();
  645. actionRequest.TestPaperItemDtos.Where(x => x.QuestionType.CheckSelectType()).ToList().ForEach(x =>
  646. {
  647. var testPaperItem = base.Entity?.TestPaperItems.FirstOrDefault(n => n.QuestionId == x.QuestionId) ?? null;
  648. var options = questionOptions.Where(n => x.QuestionId == n.QuestionId).ToList();
  649. if (options != null)
  650. {
  651. options.ForEach(item =>
  652. {
  653. var testPaperItemOption = _mapper.Map<ExamQuestionOptions, ExamTestPaperItemOptions>(item);
  654. testPaperItemOption.TestPaperItemId = testPaperItem?.Id;
  655. testPaperItemOption.QuestionOptionId = item.Id;
  656. testPaperItemOption.ToInsert(_sessionContext);
  657. testPaperItemOptions.Add(testPaperItemOption);
  658. });
  659. }
  660. });
  661. await _testPaperItemOptionsRepository.ValidateAddAsync(testPaperItemOptions, cancellationToken);
  662. return testPaperItemOptions;
  663. }
  664. private async Task<List<ExamTestPaperItemAnswer>> AddTestPaperItemAnswer(AddTestPaperDto actionRequest, CancellationToken cancellationToken)
  665. {
  666. var questionOptionRepository = new ExamRepository<ExamQuestionAnswer>(_uow, _dataPermissionFilterBuilder, _serviceProvider);
  667. var quesitonIds = actionRequest.TestPaperItemDtos?.Select(x => x.QuestionId);
  668. var questionAnswer = await questionOptionRepository.Queryable().Where(x => quesitonIds.Contains(x.QuestionId)).ToListAsync();
  669. var testPaperItemAnswers = new List<ExamTestPaperItemAnswer>();
  670. actionRequest.TestPaperItemDtos.Where(x => !x.QuestionType.CheckSelectType()).ToList().ForEach(x =>
  671. {
  672. var testPaperItem = base.Entity?.TestPaperItems.FirstOrDefault(n => n.QuestionId == x.QuestionId) ?? null;
  673. var options = questionAnswer.Where(n => x.QuestionId == n.QuestionId).ToList();
  674. options.ForEach(item =>
  675. {
  676. var testPaperItemAnswer = _mapper.Map<ExamQuestionAnswer, ExamTestPaperItemAnswer>(item);
  677. testPaperItemAnswer.TestPaperItemId = testPaperItem?.Id;
  678. testPaperItemAnswer.QuestionAnswerId = item.Id;
  679. testPaperItemAnswer.ToInsert(_sessionContext);
  680. testPaperItemAnswers.Add(testPaperItemAnswer);
  681. });
  682. });
  683. await _testPaperItemAnswerRepository.ValidateAddAsync(testPaperItemAnswers, cancellationToken);
  684. return testPaperItemAnswers;
  685. }
  686. private async Task<List<ExamTestPaperItemSourceware>> AddTestPaperItemSourcewareAsync(AddTestPaperDto actionRequest, CancellationToken cancellationToken)
  687. {
  688. var questionSourcewareRepository = new ExamRepository<ExamQuestionSourceware>(_uow, _dataPermissionFilterBuilder, _serviceProvider);
  689. var quesitonIds = actionRequest.TestPaperItemDtos.Select(x => x.QuestionId);
  690. var questionSourceware = await questionSourcewareRepository.Queryable().Where(x => quesitonIds.Contains(x.QuestionId)).ToListAsync();
  691. var testPaperItemSourcewares = new List<ExamTestPaperItemSourceware>();
  692. actionRequest.TestPaperItemDtos.Where(x => x.QuestionType.CheckSelectType()).ToList().ForEach(x =>
  693. {
  694. var testPaperItem = base.Entity?.TestPaperItems.FirstOrDefault(n => n.QuestionId == x.QuestionId) ?? null;
  695. var options = questionSourceware.Where(n => x.QuestionId == n.QuestionId).ToList();
  696. if (options != null)
  697. {
  698. options.ForEach(item =>
  699. {
  700. var testPaperItemSourceware = _mapper.Map<ExamQuestionSourceware, ExamTestPaperItemSourceware>(item);
  701. testPaperItemSourceware.TestPaperItemId = testPaperItem?.Id;
  702. testPaperItemSourceware.ToInsert(_sessionContext);
  703. testPaperItemSourcewares.Add(testPaperItemSourceware);
  704. });
  705. }
  706. });
  707. await _testPaperItemSourcewareRepository.ValidateAddAsync(testPaperItemSourcewares, cancellationToken);
  708. return testPaperItemSourcewares;
  709. }
  710. private async Task<List<ExamTestPaperItemKnowladge>> AddTestPaperItemKnowladgeAsync(AddTestPaperDto actionRequest, CancellationToken cancellationToken)
  711. {
  712. var questionKnowladgeRepository = new ExamRepository<ExamQuestionKnowladge>(_uow, _dataPermissionFilterBuilder, _serviceProvider);
  713. var quesitonIds = actionRequest.TestPaperItemDtos.Select(x => x.QuestionId);
  714. var questionKnowladge = await questionKnowladgeRepository.Queryable().Where(x => quesitonIds.Contains(x.QuestionId)).ToListAsync();
  715. var testPaperItemKnowladges = new List<ExamTestPaperItemKnowladge>();
  716. actionRequest.TestPaperItemDtos.Where(x => x.QuestionType.CheckSelectType()).ToList().ForEach(x =>
  717. {
  718. var testPaperItem = base.Entity?.TestPaperItems.FirstOrDefault(n => n.QuestionId == x.QuestionId) ?? null;
  719. var options = questionKnowladge.Where(n => x.QuestionId == n.QuestionId).ToList();
  720. if (options != null)
  721. {
  722. options.ForEach(item =>
  723. {
  724. var testPaperItemKnowladge = _mapper.Map<ExamQuestionKnowladge, ExamTestPaperItemKnowladge>(item);
  725. testPaperItemKnowladge.TestPaperItemId = testPaperItem?.Id;
  726. testPaperItemKnowladge.ToInsert(_sessionContext);
  727. testPaperItemKnowladges.Add(testPaperItemKnowladge);
  728. });
  729. }
  730. });
  731. await _testPaperItemKnowladgeRepository.ValidateAddAsync(testPaperItemKnowladges, cancellationToken);
  732. return testPaperItemKnowladges;
  733. }
  734. private async Task<List<ExamTestPaperItemSourceware>> ModifyTestPaperItemSourcewareAsync(UpdateTestPaperDto actionRequest, CancellationToken cancellationToken)
  735. {
  736. if (actionRequest.TestPaperItemDtos == null) return null;
  737. var questionIds = actionRequest.TestPaperItemDtos.Where(x => x.QuestionType.CheckSelectType()).Select(x => x.QuestionId).ToList();
  738. var entityQueryRequest = new EntityQueryRequest
  739. {
  740. Expression = ExpressionableUtility.CreateExpression<ExamTestPaperItemSourceware>()
  741. .AndIF(questionIds.IsNotNullOrEmpty(), x => questionIds.Contains(x.QuestionId)).ToExpression()
  742. };
  743. await DeleteTestPaperItemSourcewareAsync(entityQueryRequest, cancellationToken);
  744. return await AddTestPaperItemSourcewareAsync(_addTestPaperDto, cancellationToken);
  745. }
  746. private async Task DeleteTestPaperItemSourcewareAsync(EntityQueryRequest entityQueryRequest, CancellationToken cancellationToken)
  747. {
  748. await _testPaperItemSourcewareRepository.DeleteWithValidateAsync(entityQueryRequest, cancellationToken);
  749. }
  750. private async Task<List<ExamTestPaperItemKnowladge>> ModifyTestPaperItemKnowladgeAsync(UpdateTestPaperDto actionRequest, CancellationToken cancellationToken)
  751. {
  752. if (actionRequest.TestPaperItemDtos == null) return null;
  753. var questionIds = actionRequest.TestPaperItemDtos.Where(x => x.QuestionType.CheckSelectType()).Select(x => x.QuestionId).ToList();
  754. var entityQueryRequest = new EntityQueryRequest
  755. {
  756. Expression = ExpressionableUtility.CreateExpression<ExamTestPaperItemKnowladge>()
  757. .AndIF(questionIds.IsNotNullOrEmpty(), x => questionIds.Contains(x.QuestionId)).ToExpression()
  758. };
  759. await DeleteTestPaperItemKnowladgeAsync(entityQueryRequest, cancellationToken);
  760. return await AddTestPaperItemKnowladgeAsync(_addTestPaperDto, cancellationToken);
  761. }
  762. private async Task DeleteTestPaperItemKnowladgeAsync(EntityQueryRequest entityQueryRequest, CancellationToken cancellationToken)
  763. {
  764. await _testPaperItemKnowladgeRepository.DeleteWithValidateAsync(entityQueryRequest, cancellationToken);
  765. }
  766. #endregion
  767. #region protected method
  768. protected override async Task CompleteAdd(ExamTestPaper entity)
  769. {
  770. await base.AddNav(entity)
  771. //.Include(x => x.TestPaperRules)
  772. //.ThenInclude(x => x.TestPaperRuleTags)
  773. .Include(x => x.TestPaperItems)
  774. .ThenInclude(x => x.TestPaperItemAnswers)
  775. .Include(x => x.TestPaperItems, new InsertNavOptions
  776. {
  777. OneToManyIfExistsNoInsert = true
  778. })
  779. .ThenInclude(x => x.TestPaperItemOptionses)
  780. .Include(x => x.TestPaperItems, new InsertNavOptions
  781. {
  782. OneToManyIfExistsNoInsert = true
  783. })
  784. .ThenInclude(x => x.TestPaperItemKnowlagdes)
  785. .Include(x => x.TestPaperItems, new InsertNavOptions
  786. {
  787. OneToManyIfExistsNoInsert = true
  788. })
  789. .ThenInclude(x => x.TestPaperItemSourcewares).ExecuteCommandAsync();
  790. }
  791. protected override async Task CompleteUpdate(ExamTestPaper entity)
  792. {
  793. await base.UpdateNav(entity)
  794. //.Include(x => x.TestPaperRules)
  795. //.ThenInclude(x => x.TestPaperRuleTags)
  796. .Include(x => x.TestPaperItems)
  797. .ThenInclude(x => x.TestPaperItemAnswers)
  798. .Include(x => x.TestPaperItems, new UpdateNavOptions
  799. {
  800. OneToManyInsertOrUpdate = true
  801. })
  802. .ThenInclude(x => x.TestPaperItemOptionses)
  803. .Include(x => x.TestPaperItems, new UpdateNavOptions
  804. {
  805. OneToManyInsertOrUpdate = true
  806. })
  807. .ThenInclude(x => x.TestPaperItemKnowlagdes)
  808. .Include(x => x.TestPaperItems, new UpdateNavOptions
  809. {
  810. OneToManyInsertOrUpdate = true
  811. })
  812. .ThenInclude(x => x.TestPaperItemSourcewares).ExecuteCommandAsync();
  813. }
  814. #endregion
  815. }
  816. }