ExamManageService.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. using Exam.Infrastructure.Data.Entity;
  2. using Exam.Infrastructure.Enums;
  3. using Exam.Infrastructure.Extensions;
  4. using Exam.Insfrastructure.Service.Service;
  5. using Exam.Repository.Sqlsugar.Repositories;
  6. using Exam.Share;
  7. using Exam.Share.ViewResponses.Exam;
  8. using Hotline.Application.Exam.Interface.ExamManages;
  9. using Hotline.Repository.SqlSugar;
  10. using Hotline.Repository.SqlSugar.DataPermissions;
  11. using Hotline.Repository.SqlSugar.Exam.Interfaces.ExamManages;
  12. using Hotline.Repository.SqlSugar.Interface;
  13. using Hotline.Share.Dtos.Questions;
  14. using Hotline.Share.Requests.Exam;
  15. using JiebaNet.Segmenter.Common;
  16. using MapsterMapper;
  17. using SqlSugar;
  18. using XF.Domain.Dependency;
  19. using XF.Domain.Entities;
  20. using Hotline.Repository.SqlSugar.Extensions;
  21. using Hotline.Application.Exam.QueryExtensions.ExamManages;
  22. using XF.Domain.Exceptions;
  23. using NetTaste;
  24. using Exam.Infrastructure.Validation.Validation;
  25. using Hotline.Application.Exam.Extensions;
  26. using XF.Domain.Authentications;
  27. using Hotline.Repository.SqlSugar.Exam.Core.Constants;
  28. using Exam.Repository.Sqlsugar.Repositories.ExamManages;
  29. using NPOI.SS.Formula.Functions;
  30. using Hotline.Exams.TestPapers;
  31. using Hotline.Application.Exam.Proxy;
  32. using Hotline.Repository.SqlSugar.Exam.Interfaces.TestPapers;
  33. using Hotline.Repository.SqlSugar.Exam.Interfaces.Questions;
  34. using Microsoft.AspNetCore.Http;
  35. using Hotline.Tools;
  36. using Hotline.Users;
  37. using System.Buffers;
  38. using Exam.Repository.Sqlsugar.Repositories.TestPapers;
  39. using Hotline.Application.Exam.Core.Extensions;
  40. using Hotline.Application.Exam.Core.Utilities;
  41. using Hotline.Exams.ExamManages;
  42. using Hotline.Exams.Questions;
  43. using Hotline.Repository.SqlSugar.Exam.Interfaces.Exams;
  44. using ExamErrorMessage = Hotline.Exams.Validate.ExamErrorMessage;
  45. using ExamQuestion = Hotline.Exams.Questions.ExamQuestion;
  46. namespace Hotline.Application.Exam.Service.ExamManages
  47. {
  48. public class ExamManageService : ApiService<ExamManage, AddExamManageDto, UpdateExamManageDto, HotlineDbContext>, IExamManageService, IScopeDependency
  49. {
  50. private readonly IExamManageRepository _repository;
  51. private readonly IExamQuestionScoreRepository _examQuestionScoreRepository;
  52. private readonly IUserExamRepository _userExamRepository;
  53. private readonly IDataPermissionFilterBuilder _dataPermissionFilterBuilder;
  54. private readonly ITestPaperRepository _testPaperRepository;
  55. private readonly ITestPaperItemRepository _testPaperItemRepository;
  56. private readonly ITestPaperItemAnswerRepository _testPaperItemAnswerRepository;
  57. private readonly ITestPaperItemOptionsRepository _testPaperItemOptionsRepository;
  58. private readonly ITestPaperItemSourcewareRepository _testPaperItemSourcewareRepository;
  59. private readonly ITestPaperItemKnowladgeRepository _testPaperItemKnowladgeRepository;
  60. private readonly IExamQuestionRepository _examQuestionRepository;
  61. private readonly IExamQuestionAnswerRepository _examQuestionAnswerRepository;
  62. private readonly IExamQuestionOptionsRepository _examQuestionOptionsRepository;
  63. private readonly IExamQuestionSourcewareRepository _examQuestionSourcewareRepository;
  64. private readonly IExamQuestionKnowladgeRepository _examQuestionKnowladgeRepository;
  65. private readonly IServiceProvider _serviceProvider;
  66. private readonly IMapper _mapper;
  67. private readonly ISessionContext _sessionContext;
  68. private TestPaperProxy _testPaperProxy;
  69. private ExamManageProxy _examManageProxy;
  70. private AddExamManageDto _addExamManageDto;
  71. public ExamManageService(IExamManageRepository repository,
  72. IExamQuestionScoreRepository examQuestionScoreRepository,
  73. IUserExamRepository userExamRepository,
  74. ITestPaperRepository testPaperRepository,
  75. ITestPaperItemRepository testPaperItemRepository,
  76. ITestPaperItemAnswerRepository testPaperItemAnswerRepository,
  77. ITestPaperItemOptionsRepository testPaperItemOptionsRepository,
  78. ITestPaperItemSourcewareRepository testPaperItemSourcewareRepository,
  79. ITestPaperItemKnowladgeRepository testPaperItemKnowladgeRepository,
  80. IExamQuestionRepository examQuestionRepository,
  81. IExamQuestionAnswerRepository examQuestionAnswerRepository,
  82. IExamQuestionOptionsRepository examQuestionOptionsRepository,
  83. IExamQuestionSourcewareRepository examQuestionSourcewareRepository,
  84. IExamQuestionKnowladgeRepository examQuestionKnowladgeRepository,
  85. IDataPermissionFilterBuilder dataPermissionFilterBuilder, IServiceProvider serviceProvider,
  86. IMapper mapper, ISessionContext sessionContext) : base(repository, mapper, sessionContext)
  87. {
  88. this._repository = repository;
  89. this._examQuestionScoreRepository = examQuestionScoreRepository;
  90. this._userExamRepository = userExamRepository;
  91. _dataPermissionFilterBuilder = dataPermissionFilterBuilder;
  92. this._testPaperRepository = testPaperRepository;
  93. this._testPaperItemRepository = testPaperItemRepository;
  94. this._testPaperItemAnswerRepository = testPaperItemAnswerRepository;
  95. this._testPaperItemOptionsRepository = testPaperItemOptionsRepository;
  96. this._testPaperItemSourcewareRepository = testPaperItemSourcewareRepository;
  97. this._testPaperItemKnowladgeRepository = testPaperItemKnowladgeRepository;
  98. this._examQuestionRepository = examQuestionRepository;
  99. this._examQuestionAnswerRepository = examQuestionAnswerRepository;
  100. this._examQuestionOptionsRepository = examQuestionOptionsRepository;
  101. this._examQuestionSourcewareRepository = examQuestionSourcewareRepository;
  102. this._examQuestionKnowladgeRepository = examQuestionKnowladgeRepository;
  103. _serviceProvider = serviceProvider;
  104. this._mapper = mapper;
  105. this._sessionContext = sessionContext;
  106. }
  107. #region public method
  108. public async Task<ExamManageDto> GetAsync(EntityQueryRequest entityQueryRequest)
  109. {
  110. var entity = await _repository.GetAsync(entityQueryRequest.Id);
  111. if (entity == null) return null;
  112. var questionDto = _mapper.Map<ExamManageDto>(entity);
  113. if (questionDto != null)
  114. {
  115. questionDto.ExamQuestionScoreDtos = await GetExamQuestionScores(entityQueryRequest, entity);
  116. if (entity.Mode == Share.Enums.Exams.EExamMode.Random)
  117. {
  118. questionDto.TestPaperId = entity.ExtractRuleId;
  119. }
  120. questionDto.UserExamDtos = await GetUserExams(entityQueryRequest);
  121. }
  122. return questionDto;
  123. }
  124. public async Task<(int, List<ExamManageViewResponse>)> GetListAsync(ExamManagePagedRequest queryRequest)
  125. {
  126. ISugarQueryable<ExamManageViewResponse> queryable = Queryable(queryRequest);
  127. var result = await queryable.ToListAsync();
  128. var total = await queryable.CountAsync();
  129. return (total, result);
  130. }
  131. public async Task<PageViewResponse<ExamManageViewResponse>> GetPagedListAsync(ExamManagePagedRequest queryRequest)
  132. {
  133. ISugarQueryable<ExamManageViewResponse> queryable = Queryable(queryRequest);
  134. var list = await queryable.ToPageListAsync(queryRequest.PageIndex, queryRequest.PageSize);
  135. var total = await queryable.CountAsync();
  136. var result = new ExamManagePageViewResponse
  137. {
  138. Items = list,
  139. Pagination = new Pagination(queryRequest.PageIndex, queryRequest.PageSize, total)
  140. };
  141. return result;
  142. }
  143. public override async Task<string> AddAsync(AddExamManageDto actionRequest, CancellationToken cancellationToken)
  144. {
  145. CalcuteTotalScore(actionRequest);
  146. base.StartTran();
  147. var id = await base.AddAsync(actionRequest, cancellationToken);
  148. ResolveQuestionId(actionRequest, id);
  149. ResolveRandomExtractRuleId(actionRequest);
  150. base.Entity.ExamQuestionScores = await AddExamQuestionScores(actionRequest, cancellationToken);
  151. base.Entity.UserExams = await AddUserExam(actionRequest, cancellationToken);
  152. await base.Complete(base.Entity, OperationConstant.Create);
  153. await GenerateExamQuestion(new GenerateExamTestPaperRequest
  154. {
  155. TestPaperId = actionRequest.Mode == Share.Enums.Exams.EExamMode.Manual ? actionRequest.TestPaperId : null,
  156. ExtractRuleId = actionRequest.Mode == Share.Enums.Exams.EExamMode.Random ? actionRequest.TestPaperId : null,
  157. ExamManageId = id
  158. }, cancellationToken);
  159. return id;
  160. }
  161. private void ResolveRandomExtractRuleId(AddExamManageDto actionRequest)
  162. {
  163. if (actionRequest.Mode == Share.Enums.Exams.EExamMode.Random)
  164. {
  165. base.Entity.ExtractRuleId = actionRequest.TestPaperId;
  166. base.Entity.TestPaperId = string.Empty;
  167. }
  168. }
  169. public override async Task UpdateAsync(UpdateExamManageDto actionRequest, CancellationToken cancellationToken)
  170. {
  171. CalcuteTotalScore(actionRequest);
  172. base.StartTran();
  173. await base.UpdateAsync(actionRequest, cancellationToken);
  174. ResolveQuestionId(actionRequest, actionRequest.Id);
  175. ResolveRandomExtractRuleId(actionRequest);
  176. MapAddUpdateExamManage(actionRequest);
  177. base.Entity.ExamQuestionScores = await ModifyExamQuestionScores(actionRequest, cancellationToken);
  178. base.Entity.UserExams = await ModifyUserExam(actionRequest, cancellationToken);
  179. await base.Complete(base.Entity, OperationConstant.Update);
  180. await GenerateExamQuestion(new GenerateExamTestPaperRequest {
  181. TestPaperId = actionRequest.Mode == Share.Enums.Exams.EExamMode.Manual? actionRequest.TestPaperId:null,
  182. ExtractRuleId = actionRequest.Mode == Share.Enums.Exams.EExamMode.Random ? actionRequest.TestPaperId : null,
  183. ExamManageId = actionRequest.Id
  184. }, cancellationToken);
  185. }
  186. private void MapAddUpdateExamManage(UpdateExamManageDto actionRequest)
  187. {
  188. _addExamManageDto = _mapper.Map<AddExamManageDto>(actionRequest);
  189. _addExamManageDto.UserExamDtos = new List<AddUserExamDto>();
  190. _addExamManageDto.ExamQuestionScoreDtos = new List<AddExamQuestionScoreDto>();
  191. actionRequest.UserExamDtos.ForEach(x =>
  192. {
  193. x.OperationStatus = x.Id != null ? EEOperationStatus.Update : EEOperationStatus.Add;
  194. if(x.OperationStatus== EEOperationStatus.Add)
  195. {
  196. _addExamManageDto.UserExamDtos.Add(_mapper.Map<AddUserExamDto>(x));
  197. }
  198. });
  199. actionRequest.ExamQuestionScoreDtos.ForEach(x =>
  200. {
  201. x.OperationStatus = x.Id != null ? EEOperationStatus.Update : EEOperationStatus.Add;
  202. if (x.OperationStatus == EEOperationStatus.Add)
  203. {
  204. _addExamManageDto.ExamQuestionScoreDtos.Add(_mapper.Map<AddExamQuestionScoreDto>(x));
  205. }
  206. });
  207. }
  208. public override async Task DeleteAsync(EntityQueryRequest entityQueryRequest, CancellationToken cancellationToken)
  209. {
  210. await base.DeleteAsync(entityQueryRequest, cancellationToken);
  211. var tmpEntityQueryRequest = ExpressionableUtility.CreateExpression<ExamQuestionScoreBak>()
  212. .AndIF(entityQueryRequest.Id.IsNotNullOrEmpty(), x => x.ExamManageId == entityQueryRequest.Id)
  213. .AndIF(entityQueryRequest.Ids.IsNotNullOrEmpty(), x => entityQueryRequest.Ids.Contains(x.ExamManageId))
  214. .ToEntityQueryRequest<ExamQuestionScoreBak>();
  215. await DeleteExamQuestionScores(tmpEntityQueryRequest, cancellationToken);
  216. tmpEntityQueryRequest = ExpressionableUtility.CreateExpression<ExamUserExam>()
  217. .AndIF(entityQueryRequest.Id.IsNotNullOrEmpty(), x => x.ExamId == entityQueryRequest.Id)
  218. .AndIF(entityQueryRequest.Ids.IsNotNullOrEmpty(), x => entityQueryRequest.Ids.Contains(x.ExamId))
  219. .ToEntityQueryRequest<ExamUserExam>();
  220. await DeleteUserExam(tmpEntityQueryRequest, cancellationToken);
  221. }
  222. public async Task GenerateTestPaper(GenerateExamTestPaperRequest generateExamTestPaperRequest, CancellationToken cancellationToken)
  223. {
  224. var expression = generateExamTestPaperRequest.GetExpression();
  225. var examManage = await _repository.GetAsync(expression);
  226. if (examManage != null)
  227. {
  228. var tagQuestionCounts = await GetTagQuestions(examManage);
  229. var questions = await GetQuestions(tagQuestionCounts);
  230. _testPaperProxy = new TestPaperProxy(_testPaperRepository,
  231. _testPaperItemRepository,
  232. _testPaperItemAnswerRepository,
  233. _testPaperItemOptionsRepository,
  234. _testPaperItemSourcewareRepository,
  235. _testPaperItemKnowladgeRepository,
  236. _dataPermissionFilterBuilder,
  237. _serviceProvider,
  238. _mapper,
  239. _sessionContext
  240. );
  241. await _testPaperProxy.DeleteTestPaperItems(generateExamTestPaperRequest.TestPaperId, cancellationToken);
  242. await _testPaperProxy.GenerateQuestion(questions, generateExamTestPaperRequest.TestPaperId, cancellationToken);
  243. }
  244. }
  245. public async Task GenerateExamQuestion(GenerateExamTestPaperRequest generateExamTestPaperRequest, CancellationToken cancellationToken)
  246. {
  247. var expression = generateExamTestPaperRequest.GetExpression();
  248. var examManages = await _repository.Queryable().Where(expression).ToListAsync();
  249. var examManage = examManages.FirstOrDefault();
  250. if (examManage != null)
  251. {
  252. var questions = new List<ExamQuestion>();
  253. if (examManage.Mode == Share.Enums.Exams.EExamMode.Random)
  254. {
  255. var tagQuestionCounts = await GetTagQuestions(examManage);
  256. questions = await GetQuestions(tagQuestionCounts);
  257. }
  258. else
  259. {
  260. questions = await GetQuestions(examManage);
  261. }
  262. _examManageProxy = new ExamManageProxy(_repository,
  263. _examQuestionRepository,
  264. _examQuestionAnswerRepository,
  265. _examQuestionOptionsRepository,
  266. _examQuestionSourcewareRepository,
  267. _examQuestionKnowladgeRepository,
  268. _dataPermissionFilterBuilder,
  269. _serviceProvider,
  270. _mapper,
  271. _sessionContext
  272. );
  273. await _examManageProxy.DeleteExamQuestions(generateExamTestPaperRequest.ExamManageId, cancellationToken);
  274. await _examManageProxy.GenerateQuestion(questions, generateExamTestPaperRequest.ExamManageId, cancellationToken);
  275. }
  276. }
  277. private async Task<List<ExamQuestion>> GetQuestions(ExamManage examManage)
  278. {
  279. var questionRepository = new ExamRepository<ExamQuestion>(_uow, _dataPermissionFilterBuilder, _serviceProvider);
  280. var questionTable = questionRepository.Queryable();
  281. var testPaperItemTable = _testPaperItemRepository.Queryable().Where(x => x.TestPaperId == examManage.TestPaperId);
  282. var questions = questionTable.InnerJoin(testPaperItemTable, (q, t) => q.Id == t.QuestionId)
  283. .Select((q, t) => q);
  284. return await questions.ToListAsync();
  285. }
  286. private async Task<List<ExamTagQuestion>> GetTagQuestions(ExamManage examManage)
  287. {
  288. var extractRuleRepository = new ExamRepository<ExamExtractRule>(_uow, _dataPermissionFilterBuilder, _serviceProvider);
  289. var ruleTagRepository = new ExamRepository<ExamRuleTag>(_uow, _dataPermissionFilterBuilder, _serviceProvider);
  290. var tagQuestionRepository = new ExamRepository<ExamTagQuestion>(_uow, _dataPermissionFilterBuilder, _serviceProvider);
  291. var extractRuleTable = extractRuleRepository.Queryable().Where(x=>x.Id == examManage.ExtractRuleId && x.RuleType == examManage.ExamType);
  292. var ruleTagTable = ruleTagRepository.Queryable();
  293. var tagQuestionTable = tagQuestionRepository.Queryable();
  294. var tagQuestions = await tagQuestionTable
  295. .InnerJoin(ruleTagTable, (q, rt) => q.TagId == rt.TagId)
  296. .InnerJoin(extractRuleTable, (q, rt, x) => rt.RuleId == x.Id)
  297. .Select((q, rt, x) => q).ToListAsync();
  298. return tagQuestions;
  299. }
  300. #endregion
  301. #region private method
  302. private void ResolveQuestionId(AddExamManageDto actionRequest, string id)
  303. {
  304. actionRequest.UserExamDtos.ForEach(x => x.ExamId = id);
  305. actionRequest.ExamQuestionScoreDtos.ForEach(x => x.ExamManageId = id);
  306. }
  307. private void ResolveQuestionId(UpdateExamManageDto actionRequest, string id)
  308. {
  309. actionRequest.UserExamDtos.ForEach(x => x.ExamId = id);
  310. actionRequest.ExamQuestionScoreDtos.ForEach(x => x.ExamManageId = id);
  311. }
  312. private async Task<List<ExamQuestionScoreBak>> AddExamQuestionScores(AddExamManageDto actionRequest, CancellationToken cancellationToken)
  313. {
  314. if (actionRequest.ExamQuestionScoreDtos == null) return null;
  315. actionRequest.ExamQuestionScoreDtos.ResolveOperationStatus();
  316. var examQuestionScoreDtos = actionRequest.ExamQuestionScoreDtos.Where(x => x.OperationStatus == EEOperationStatus.Add).ToList();
  317. var examQuestionScores = _mapper.Map<List<ExamQuestionScoreBak>>(examQuestionScoreDtos);
  318. examQuestionScores.ToInsert(_sessionContext);
  319. await _examQuestionScoreRepository.ValidateAddAsync(examQuestionScores, cancellationToken);
  320. return examQuestionScores;
  321. }
  322. private async Task<List<ExamUserExam>> AddUserExam(AddExamManageDto actionRequest, CancellationToken cancellationToken)
  323. {
  324. if (actionRequest.UserExamDtos == null) return null;
  325. actionRequest.UserExamDtos.ResolveOperationStatus();
  326. var userExamDtos = actionRequest.UserExamDtos.Where(x => x.OperationStatus == EEOperationStatus.Add).ToList();
  327. var userExams = new List<ExamUserExam>();
  328. //_mapper.Map<List<UserExam>>(userExamDtos);
  329. userExamDtos.ForEach(x =>
  330. {
  331. userExams.Add(_mapper.Map<ExamUserExam>(x));
  332. });
  333. userExams.ToInsert(_sessionContext);
  334. await _userExamRepository.ValidateAddAsync(userExams, cancellationToken);
  335. return userExams;
  336. }
  337. private async Task<List<ExamUserExam>> UpdateUserExam(UpdateExamManageDto actionRequest, List<ExamUserExam> all, CancellationToken cancellationToken)
  338. {
  339. if (actionRequest.UserExamDtos == null) return null;
  340. var userExamDtos = actionRequest.UserExamDtos.Where(x => x.OperationStatus == EEOperationStatus.Update).ToList();
  341. var ids = userExamDtos.Select(x => x.Id);
  342. var userExams = all.Where(x => ids.Contains(x.Id)).ToList();
  343. //userExams = _mapper.Map<List<UpdateUserExamDto>, List<UserExam>>(userExamDtos, userExams);
  344. userExams.ForEach(item =>
  345. {
  346. var userExamDto = userExamDtos.FirstOrDefault(x => x.Id == item.Id);
  347. if (userExamDto != null)
  348. _mapper.Map<UpdateUserExamDto, ExamUserExam>(userExamDto, item);
  349. });
  350. userExams.ToUpdate(_sessionContext);
  351. await _userExamRepository.ValidateUpdateAsync(userExams, cancellationToken);
  352. return userExams;
  353. }
  354. private async Task<List<ExamQuestionScoreBak>> UpdateExamQuestionScores(UpdateExamManageDto actionRequest, List<ExamQuestionScoreBak> all, CancellationToken cancellationToken)
  355. {
  356. if (actionRequest.ExamQuestionScoreDtos == null) return null;
  357. var examQuestionScoreDtos = actionRequest.ExamQuestionScoreDtos.Where(x => x.OperationStatus == EEOperationStatus.Update).ToList();
  358. var ids = examQuestionScoreDtos.Select(x => x.Id);
  359. var examQuestionScores = all.Where(x => ids.Contains(x.Id)).ToList();
  360. //examQuestionScores = _mapper.Map<List<UpdateExamQuestionScoreDto>, List<ExamQuestionScore>>(examQuestionScoreDtos, examQuestionScores);
  361. examQuestionScores.ForEach(item =>
  362. {
  363. var examQuestionScoreDto = examQuestionScoreDtos.FirstOrDefault(x => x.Id == item.Id);
  364. if (examQuestionScoreDto != null)
  365. item = _mapper.Map<UpdateExamQuestionScoreDto, ExamQuestionScoreBak>(examQuestionScoreDto, item);
  366. });
  367. examQuestionScores.ToUpdate(_sessionContext);
  368. await _examQuestionScoreRepository.ValidateUpdateAsync(examQuestionScores, cancellationToken);
  369. return examQuestionScores;
  370. }
  371. private async Task DeleteUserExam(EntityQueryRequest entityQueryRequest, CancellationToken cancellationToken)
  372. {
  373. await _userExamRepository.DeleteWithValidateAsync(entityQueryRequest, cancellationToken);
  374. }
  375. private async Task DeleteExamQuestionScores(EntityQueryRequest entityQueryRequest, CancellationToken cancellationToken)
  376. {
  377. await _examQuestionScoreRepository.DeleteWithValidateAsync(entityQueryRequest, cancellationToken);
  378. }
  379. private async Task<List<ExamUserExam>> ModifyUserExam(UpdateExamManageDto actionRequest, CancellationToken cancellationToken)
  380. {
  381. if (actionRequest.UserExamDtos == null) return null;
  382. var all = await _userExamRepository.Queryable().Where(x => x.ExamId == actionRequest.Id).ToListAsync();
  383. actionRequest.UserExamDtos.ResolveOperationStatus(all);
  384. var userExams = new List<ExamUserExam>();
  385. userExams.AddRangeExt(await AddUserExam(_addExamManageDto, cancellationToken));
  386. userExams.AddRangeExt(await UpdateUserExam(actionRequest, all, cancellationToken));
  387. var questionOptionsDtos = actionRequest.UserExamDtos.Where(x => x.OperationStatus == EEOperationStatus.Delete);
  388. var ids = questionOptionsDtos.Select(m => m.Id);
  389. EntityQueryRequest entityQueryRequest = ResovleDelete<ExamUserExam>(ids);
  390. await DeleteUserExam(entityQueryRequest, cancellationToken);
  391. return userExams;
  392. }
  393. private async Task<List<ExamQuestionScoreBak>> ModifyExamQuestionScores(UpdateExamManageDto actionRequest, CancellationToken cancellationToken)
  394. {
  395. if (actionRequest.ExamQuestionScoreDtos == null) return null;
  396. var all = await _examQuestionScoreRepository.Queryable().Where(x => x.ExamManageId == actionRequest.Id).ToListAsync();
  397. var examQuestionScores = new List<ExamQuestionScoreBak>();
  398. examQuestionScores.AddRangeExt(await AddExamQuestionScores(_addExamManageDto, cancellationToken));
  399. examQuestionScores.AddRangeExt(await UpdateExamQuestionScores(actionRequest, all, cancellationToken));
  400. var examQuestionScoreDtos = actionRequest.ExamQuestionScoreDtos.Where(x => x.OperationStatus == EEOperationStatus.Delete);
  401. var ids = examQuestionScoreDtos.Select(m => m.Id);
  402. EntityQueryRequest entityQueryRequest = ResovleDelete<ExamQuestionScoreBak>(ids);
  403. await DeleteExamQuestionScores(entityQueryRequest, cancellationToken);
  404. return examQuestionScores;
  405. }
  406. private EntityQueryRequest ResovleDelete<T>(IEnumerable<string> ids) where T : class, IEntity<string>, new()
  407. {
  408. Expressionable<T> expressionable = ExpressionableUtility.CreateExpression<T>();
  409. expressionable.AndIF(ids.Any(), x => ids.Contains(x.Id));
  410. var entityQueryRequest = new EntityQueryRequest
  411. {
  412. Expression = ids.Any() ? expressionable.ToExpression() : null
  413. };
  414. return entityQueryRequest;
  415. }
  416. private async Task<List<UserExamDto>> GetUserExams(EntityQueryRequest entityQueryRequest)
  417. {
  418. var userRepository = new ExamRepository<User>(_uow, _dataPermissionFilterBuilder, _serviceProvider);
  419. var userExams = _userExamRepository.Queryable().Where(x => x.ExamId == entityQueryRequest.Id);
  420. var userTable = userRepository.Queryable();
  421. var userExamDtos = userExams.InnerJoin(userTable,(e,u)=>e.UserId == u.Id).Select((e, u) => new UserExamDto
  422. {
  423. Id = e.Id,
  424. Name = u.Name,
  425. ExamId = e.ExamId,
  426. UserId = e.UserId,
  427. FullOrgName = u.FullOrgName
  428. });
  429. return await userExamDtos.ToListAsync();
  430. }
  431. private async Task<List<ExamQuestionScoreDto>> GetExamQuestionScores(EntityQueryRequest entityQueryRequest,ExamManage examManage)
  432. {
  433. var examQuestionScores = _examQuestionScoreRepository.Queryable().Where(x => x.ExamManageId == entityQueryRequest.Id);
  434. var examQuestionScoreDtos = examQuestionScores.Select(q => new ExamQuestionScoreDto
  435. {
  436. Id = q.Id,
  437. QuestionType = q.QuestionType,
  438. ExamManageId = q.ExamManageId,
  439. Score = q.Score
  440. });
  441. if(examManage.Mode == Share.Enums.Exams.EExamMode.Random)
  442. {
  443. var tagQuestionRepository = new ExamRepository<ExamTagQuestion>(_uow, _dataPermissionFilterBuilder, _serviceProvider);
  444. var tagQuestionTable = tagQuestionRepository.Queryable().Where(x => x.RuleId == examManage.ExtractRuleId);
  445. var result = examQuestionScoreDtos.InnerJoin(tagQuestionTable, (e, t) => e.QuestionType == t.QuestionType).Select((e, t) => new ExamQuestionScoreDto
  446. {
  447. Id = e.Id,
  448. QuestionType = e.QuestionType,
  449. ExamManageId = e.ExamManageId,
  450. Score = e.Score,
  451. Count = t.Count
  452. }).Distinct();
  453. return await result.ToListAsync();
  454. }
  455. else
  456. {
  457. var testPaperItemRepository = new ExamRepository<ExamTestPaperItem>(_uow, _dataPermissionFilterBuilder, _serviceProvider);
  458. var testPaperItemTable = testPaperItemRepository.Queryable().Where(x => x.TestPaperId == examManage.TestPaperId);
  459. var result = examQuestionScoreDtos.InnerJoin(testPaperItemTable, (e, t) => e.QuestionType == t.QuestionType)
  460. .GroupBy((e, t) => new
  461. {
  462. Id = e.Id,
  463. QuestionType = e.QuestionType,
  464. ExamManageId = e.ExamManageId,
  465. Score = e.Score,
  466. })
  467. .Select((e, t) => new ExamQuestionScoreDto
  468. {
  469. Id = e.Id,
  470. QuestionType = e.QuestionType,
  471. ExamManageId = e.ExamManageId,
  472. Score = e.Score,
  473. Count = SqlFunc.AggregateCount(t.Id)
  474. });
  475. return await result.ToListAsync();
  476. }
  477. }
  478. private void CalcuteTotalScore(AddExamManageDto actionRequest)
  479. {
  480. if (actionRequest.ExamQuestionScoreDtos == null) return;
  481. var examQuestionScoreDtos = actionRequest.ExamQuestionScoreDtos.Where(x => x.OperationStatus != EEOperationStatus.Delete);
  482. var totalScore = examQuestionScoreDtos.Sum(x => x.Count * x.Score);
  483. if (totalScore != actionRequest.TotalScore)
  484. {
  485. throw new UserFriendlyException(ExamErrorMessage.ServiceError, string.Format(ExamErrorMessage.IsNotEqual, "试题分数总和", typeof(AddExamManageDto).GetDescription(nameof(AddExamManageDto.TotalScore))));
  486. }
  487. }
  488. private ISugarQueryable<ExamManageViewResponse> Queryable(ExamManagePagedRequest queryRequest)
  489. {
  490. var expression = queryRequest.GetExpression();
  491. var questionTable = _repository.Queryable().Where(expression);
  492. var queryable = questionTable.Select(e => new ExamManageViewResponse
  493. {
  494. Id = e.Id,
  495. Code = e.Code,
  496. Count = e.Count,
  497. CutoffScore = e.CutoffScore,
  498. EndTime = e.EndTime,
  499. ExamStatus = e.ExamStatus,
  500. ExamType = e.ExamType,
  501. Mode = e.Mode,
  502. Name = e.Name,
  503. Remark = e.Name,
  504. SortIndex = e.SortIndex,
  505. StartTime = e.StartTime,
  506. TimeSpan = e.TimeSpan,
  507. TotalScore = e.TotalScore,
  508. });
  509. return queryable;
  510. }
  511. private async Task<List<ExamQuestion>> GetQuestions(List<ExamTagQuestion> tagQuestionCounts)
  512. {
  513. var questionRepository = new ExamRepository<ExamQuestion>(_uow, _dataPermissionFilterBuilder, _serviceProvider);
  514. var questionTagRepository = new ExamRepository<ExamQuestionTag>(_uow, _dataPermissionFilterBuilder, _serviceProvider);
  515. var questionTable = questionRepository.Queryable();
  516. var questionTagTable = questionTagRepository.Queryable();
  517. if (tagQuestionCounts != null && tagQuestionCounts.Any())
  518. {
  519. var questionQuerables = new List<ISugarQueryable<ExamQuestion>>();
  520. tagQuestionCounts.ForEach(item =>
  521. {
  522. ISugarQueryable<ExamQuestion> queryable = questionTable.InnerJoin(questionTagTable, (q, t) => t.Id == t.QuestionId)
  523. .Where((q, t) => q.QuestionType == item.QuestionType && t.TagId == item.TagId).Take(item.Count).Select((q, t) => q);
  524. questionQuerables.Add(queryable);
  525. });
  526. var queryResult = questionRepository.UnionAll(questionQuerables.ToArray());
  527. return await queryResult.ToListAsync();
  528. }
  529. else
  530. {
  531. return null;
  532. }
  533. }
  534. #endregion
  535. }
  536. }