123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695 |
- using Exam.Infrastructure.Data.Entity;
- using Exam.Infrastructure.Enums;
- using Exam.Infrastructure.Extensions;
- using Exam.Insfrastructure.Service.Service;
- using Exam.Repository.Sqlsugar.Repositories;
- using Exam.Share;
- using Exam.Share.ViewResponses.Exam;
- using Hotline.Application.Exam.Interface.ExamManages;
- using Hotline.Repository.SqlSugar;
- using Hotline.Repository.SqlSugar.DataPermissions;
- using Hotline.Repository.SqlSugar.Exam.Interfaces.ExamManages;
- using Hotline.Repository.SqlSugar.Interface;
- using Hotline.Share.Dtos.Questions;
- using Hotline.Share.Requests.Exam;
- using JiebaNet.Segmenter.Common;
- using MapsterMapper;
- using SqlSugar;
- using XF.Domain.Dependency;
- using XF.Domain.Entities;
- using Hotline.Repository.SqlSugar.Extensions;
- using Hotline.Application.Exam.QueryExtensions.ExamManages;
- using XF.Domain.Exceptions;
- using NetTaste;
- using Exam.Infrastructure.Validation.Validation;
- using Hotline.Application.Exam.Extensions;
- using XF.Domain.Authentications;
- using Hotline.Repository.SqlSugar.Exam.Core.Constants;
- using Exam.Repository.Sqlsugar.Repositories.ExamManages;
- using NPOI.SS.Formula.Functions;
- using Hotline.Exams.TestPapers;
- using Hotline.Application.Exam.Proxy;
- using Hotline.Repository.SqlSugar.Exam.Interfaces.TestPapers;
- using Hotline.Repository.SqlSugar.Exam.Interfaces.Questions;
- using Microsoft.AspNetCore.Http;
- using Hotline.Tools;
- using Hotline.Users;
- using System.Buffers;
- using Exam.Repository.Sqlsugar.Repositories.TestPapers;
- using Hotline.Application.Exam.Core.Extensions;
- using Hotline.Application.Exam.Core.Utilities;
- using Hotline.Exams.ExamManages;
- using Hotline.Exams.Questions;
- using Hotline.Repository.SqlSugar.Exam.Interfaces.Exams;
- using ExamErrorMessage = Hotline.Exams.Validate.ExamErrorMessage;
- using ExamQuestion = Hotline.Exams.Questions.ExamQuestion;
- namespace Hotline.Application.Exam.Service.ExamManages
- {
- public class ExamManageService : ApiService<ExamManage, AddExamManageDto, UpdateExamManageDto, HotlineDbContext>, IExamManageService, IScopeDependency
- {
- private readonly IExamManageRepository _repository;
- private readonly IExamQuestionScoreRepository _examQuestionScoreRepository;
- private readonly IUserExamRepository _userExamRepository;
- private readonly IDataPermissionFilterBuilder _dataPermissionFilterBuilder;
- private readonly ITestPaperRepository _testPaperRepository;
- private readonly ITestPaperItemRepository _testPaperItemRepository;
- private readonly ITestPaperItemAnswerRepository _testPaperItemAnswerRepository;
- private readonly ITestPaperItemOptionsRepository _testPaperItemOptionsRepository;
- private readonly ITestPaperItemSourcewareRepository _testPaperItemSourcewareRepository;
- private readonly ITestPaperItemKnowladgeRepository _testPaperItemKnowladgeRepository;
- private readonly IExamQuestionRepository _examQuestionRepository;
- private readonly IExamQuestionAnswerRepository _examQuestionAnswerRepository;
- private readonly IExamQuestionOptionsRepository _examQuestionOptionsRepository;
- private readonly IExamQuestionSourcewareRepository _examQuestionSourcewareRepository;
- private readonly IExamQuestionKnowladgeRepository _examQuestionKnowladgeRepository;
- private readonly IServiceProvider _serviceProvider;
- private readonly IMapper _mapper;
- private readonly ISessionContext _sessionContext;
- private TestPaperProxy _testPaperProxy;
- private ExamManageProxy _examManageProxy;
- private AddExamManageDto _addExamManageDto;
- public ExamManageService(IExamManageRepository repository,
- IExamQuestionScoreRepository examQuestionScoreRepository,
- IUserExamRepository userExamRepository,
- ITestPaperRepository testPaperRepository,
- ITestPaperItemRepository testPaperItemRepository,
- ITestPaperItemAnswerRepository testPaperItemAnswerRepository,
- ITestPaperItemOptionsRepository testPaperItemOptionsRepository,
- ITestPaperItemSourcewareRepository testPaperItemSourcewareRepository,
- ITestPaperItemKnowladgeRepository testPaperItemKnowladgeRepository,
- IExamQuestionRepository examQuestionRepository,
- IExamQuestionAnswerRepository examQuestionAnswerRepository,
- IExamQuestionOptionsRepository examQuestionOptionsRepository,
- IExamQuestionSourcewareRepository examQuestionSourcewareRepository,
- IExamQuestionKnowladgeRepository examQuestionKnowladgeRepository,
- IDataPermissionFilterBuilder dataPermissionFilterBuilder, IServiceProvider serviceProvider,
- IMapper mapper, ISessionContext sessionContext) : base(repository, mapper, sessionContext)
- {
- this._repository = repository;
- this._examQuestionScoreRepository = examQuestionScoreRepository;
- this._userExamRepository = userExamRepository;
- _dataPermissionFilterBuilder = dataPermissionFilterBuilder;
- this._testPaperRepository = testPaperRepository;
- this._testPaperItemRepository = testPaperItemRepository;
- this._testPaperItemAnswerRepository = testPaperItemAnswerRepository;
- this._testPaperItemOptionsRepository = testPaperItemOptionsRepository;
- this._testPaperItemSourcewareRepository = testPaperItemSourcewareRepository;
- this._testPaperItemKnowladgeRepository = testPaperItemKnowladgeRepository;
- this._examQuestionRepository = examQuestionRepository;
- this._examQuestionAnswerRepository = examQuestionAnswerRepository;
- this._examQuestionOptionsRepository = examQuestionOptionsRepository;
- this._examQuestionSourcewareRepository = examQuestionSourcewareRepository;
- this._examQuestionKnowladgeRepository = examQuestionKnowladgeRepository;
- _serviceProvider = serviceProvider;
- this._mapper = mapper;
- this._sessionContext = sessionContext;
- }
- #region public method
- public async Task<ExamManageDto> GetAsync(EntityQueryRequest entityQueryRequest)
- {
- var entity = await _repository.GetAsync(entityQueryRequest.Id);
- if (entity == null) return null;
- var questionDto = _mapper.Map<ExamManageDto>(entity);
-
- if (questionDto != null)
- {
- questionDto.ExamQuestionScoreDtos = await GetExamQuestionScores(entityQueryRequest, entity);
- if (entity.Mode == Share.Enums.Exams.EExamMode.Random)
- {
- questionDto.TestPaperId = entity.ExtractRuleId;
- }
- questionDto.UserExamDtos = await GetUserExams(entityQueryRequest);
- }
- return questionDto;
- }
- public async Task<(int, List<ExamManageViewResponse>)> GetListAsync(ExamManagePagedRequest queryRequest)
- {
- ISugarQueryable<ExamManageViewResponse> queryable = Queryable(queryRequest);
- var result = await queryable.ToListAsync();
- var total = await queryable.CountAsync();
- return (total, result);
- }
- public async Task<PageViewResponse<ExamManageViewResponse>> GetPagedListAsync(ExamManagePagedRequest queryRequest)
- {
- ISugarQueryable<ExamManageViewResponse> queryable = Queryable(queryRequest);
- var list = await queryable.ToPageListAsync(queryRequest.PageIndex, queryRequest.PageSize);
- var total = await queryable.CountAsync();
- var result = new ExamManagePageViewResponse
- {
- Items = list,
- Pagination = new Pagination(queryRequest.PageIndex, queryRequest.PageSize, total)
- };
- return result;
- }
- public override async Task<string> AddAsync(AddExamManageDto actionRequest, CancellationToken cancellationToken)
- {
- CalcuteTotalScore(actionRequest);
- base.StartTran();
- var id = await base.AddAsync(actionRequest, cancellationToken);
- ResolveQuestionId(actionRequest, id);
- ResolveRandomExtractRuleId(actionRequest);
- base.Entity.ExamQuestionScores = await AddExamQuestionScores(actionRequest, cancellationToken);
- base.Entity.UserExams = await AddUserExam(actionRequest, cancellationToken);
- await base.Complete(base.Entity, OperationConstant.Create);
- await GenerateExamQuestion(new GenerateExamTestPaperRequest
- {
- TestPaperId = actionRequest.Mode == Share.Enums.Exams.EExamMode.Manual ? actionRequest.TestPaperId : null,
- ExtractRuleId = actionRequest.Mode == Share.Enums.Exams.EExamMode.Random ? actionRequest.TestPaperId : null,
- ExamManageId = id
- }, cancellationToken);
- return id;
- }
- private void ResolveRandomExtractRuleId(AddExamManageDto actionRequest)
- {
- if (actionRequest.Mode == Share.Enums.Exams.EExamMode.Random)
- {
- base.Entity.ExtractRuleId = actionRequest.TestPaperId;
- base.Entity.TestPaperId = string.Empty;
- }
- }
- public override async Task UpdateAsync(UpdateExamManageDto actionRequest, CancellationToken cancellationToken)
- {
- CalcuteTotalScore(actionRequest);
- base.StartTran();
- await base.UpdateAsync(actionRequest, cancellationToken);
- ResolveQuestionId(actionRequest, actionRequest.Id);
- ResolveRandomExtractRuleId(actionRequest);
- MapAddUpdateExamManage(actionRequest);
- base.Entity.ExamQuestionScores = await ModifyExamQuestionScores(actionRequest, cancellationToken);
- base.Entity.UserExams = await ModifyUserExam(actionRequest, cancellationToken);
- await base.Complete(base.Entity, OperationConstant.Update);
- await GenerateExamQuestion(new GenerateExamTestPaperRequest {
- TestPaperId = actionRequest.Mode == Share.Enums.Exams.EExamMode.Manual? actionRequest.TestPaperId:null,
- ExtractRuleId = actionRequest.Mode == Share.Enums.Exams.EExamMode.Random ? actionRequest.TestPaperId : null,
- ExamManageId = actionRequest.Id
- }, cancellationToken);
- }
- private void MapAddUpdateExamManage(UpdateExamManageDto actionRequest)
- {
- _addExamManageDto = _mapper.Map<AddExamManageDto>(actionRequest);
- _addExamManageDto.UserExamDtos = new List<AddUserExamDto>();
- _addExamManageDto.ExamQuestionScoreDtos = new List<AddExamQuestionScoreDto>();
- actionRequest.UserExamDtos.ForEach(x =>
- {
- x.OperationStatus = x.Id != null ? EEOperationStatus.Update : EEOperationStatus.Add;
- if(x.OperationStatus== EEOperationStatus.Add)
- {
- _addExamManageDto.UserExamDtos.Add(_mapper.Map<AddUserExamDto>(x));
- }
- });
- actionRequest.ExamQuestionScoreDtos.ForEach(x =>
- {
- x.OperationStatus = x.Id != null ? EEOperationStatus.Update : EEOperationStatus.Add;
- if (x.OperationStatus == EEOperationStatus.Add)
- {
- _addExamManageDto.ExamQuestionScoreDtos.Add(_mapper.Map<AddExamQuestionScoreDto>(x));
- }
- });
- }
- public override async Task DeleteAsync(EntityQueryRequest entityQueryRequest, CancellationToken cancellationToken)
- {
- await base.DeleteAsync(entityQueryRequest, cancellationToken);
- var tmpEntityQueryRequest = ExpressionableUtility.CreateExpression<ExamQuestionScoreBak>()
- .AndIF(entityQueryRequest.Id.IsNotNullOrEmpty(), x => x.ExamManageId == entityQueryRequest.Id)
- .AndIF(entityQueryRequest.Ids.IsNotNullOrEmpty(), x => entityQueryRequest.Ids.Contains(x.ExamManageId))
- .ToEntityQueryRequest<ExamQuestionScoreBak>();
- await DeleteExamQuestionScores(tmpEntityQueryRequest, cancellationToken);
- tmpEntityQueryRequest = ExpressionableUtility.CreateExpression<ExamUserExam>()
- .AndIF(entityQueryRequest.Id.IsNotNullOrEmpty(), x => x.ExamId == entityQueryRequest.Id)
- .AndIF(entityQueryRequest.Ids.IsNotNullOrEmpty(), x => entityQueryRequest.Ids.Contains(x.ExamId))
- .ToEntityQueryRequest<ExamUserExam>();
- await DeleteUserExam(tmpEntityQueryRequest, cancellationToken);
- }
- public async Task GenerateTestPaper(GenerateExamTestPaperRequest generateExamTestPaperRequest, CancellationToken cancellationToken)
- {
- var expression = generateExamTestPaperRequest.GetExpression();
- var examManage = await _repository.GetAsync(expression);
- if (examManage != null)
- {
- var tagQuestionCounts = await GetTagQuestions(examManage);
- var questions = await GetQuestions(tagQuestionCounts);
- _testPaperProxy = new TestPaperProxy(_testPaperRepository,
- _testPaperItemRepository,
- _testPaperItemAnswerRepository,
- _testPaperItemOptionsRepository,
- _testPaperItemSourcewareRepository,
- _testPaperItemKnowladgeRepository,
- _dataPermissionFilterBuilder,
- _serviceProvider,
- _mapper,
- _sessionContext
- );
- await _testPaperProxy.DeleteTestPaperItems(generateExamTestPaperRequest.TestPaperId, cancellationToken);
- await _testPaperProxy.GenerateQuestion(questions, generateExamTestPaperRequest.TestPaperId, cancellationToken);
- }
- }
- public async Task GenerateExamQuestion(GenerateExamTestPaperRequest generateExamTestPaperRequest, CancellationToken cancellationToken)
- {
- var expression = generateExamTestPaperRequest.GetExpression();
- var examManages = await _repository.Queryable().Where(expression).ToListAsync();
- var examManage = examManages.FirstOrDefault();
- if (examManage != null)
- {
- var questions = new List<ExamQuestion>();
- if (examManage.Mode == Share.Enums.Exams.EExamMode.Random)
- {
- var tagQuestionCounts = await GetTagQuestions(examManage);
- questions = await GetQuestions(tagQuestionCounts);
- }
- else
- {
- questions = await GetQuestions(examManage);
- }
- _examManageProxy = new ExamManageProxy(_repository,
- _examQuestionRepository,
- _examQuestionAnswerRepository,
- _examQuestionOptionsRepository,
- _examQuestionSourcewareRepository,
- _examQuestionKnowladgeRepository,
- _dataPermissionFilterBuilder,
- _serviceProvider,
- _mapper,
- _sessionContext
- );
- await _examManageProxy.DeleteExamQuestions(generateExamTestPaperRequest.ExamManageId, cancellationToken);
- await _examManageProxy.GenerateQuestion(questions, generateExamTestPaperRequest.ExamManageId, cancellationToken);
- }
- }
- private async Task<List<ExamQuestion>> GetQuestions(ExamManage examManage)
- {
- var questionRepository = new ExamRepository<ExamQuestion>(_uow, _dataPermissionFilterBuilder, _serviceProvider);
- var questionTable = questionRepository.Queryable();
- var testPaperItemTable = _testPaperItemRepository.Queryable().Where(x => x.TestPaperId == examManage.TestPaperId);
- var questions = questionTable.InnerJoin(testPaperItemTable, (q, t) => q.Id == t.QuestionId)
- .Select((q, t) => q);
- return await questions.ToListAsync();
- }
- private async Task<List<ExamTagQuestion>> GetTagQuestions(ExamManage examManage)
- {
- var extractRuleRepository = new ExamRepository<ExamExtractRule>(_uow, _dataPermissionFilterBuilder, _serviceProvider);
- var ruleTagRepository = new ExamRepository<ExamRuleTag>(_uow, _dataPermissionFilterBuilder, _serviceProvider);
- var tagQuestionRepository = new ExamRepository<ExamTagQuestion>(_uow, _dataPermissionFilterBuilder, _serviceProvider);
-
- var extractRuleTable = extractRuleRepository.Queryable().Where(x=>x.Id == examManage.ExtractRuleId && x.RuleType == examManage.ExamType);
- var ruleTagTable = ruleTagRepository.Queryable();
- var tagQuestionTable = tagQuestionRepository.Queryable();
- var tagQuestions = await tagQuestionTable
- .InnerJoin(ruleTagTable, (q, rt) => q.TagId == rt.TagId)
- .InnerJoin(extractRuleTable, (q, rt, x) => rt.RuleId == x.Id)
- .Select((q, rt, x) => q).ToListAsync();
- return tagQuestions;
- }
- #endregion
- #region private method
- private void ResolveQuestionId(AddExamManageDto actionRequest, string id)
- {
- actionRequest.UserExamDtos.ForEach(x => x.ExamId = id);
- actionRequest.ExamQuestionScoreDtos.ForEach(x => x.ExamManageId = id);
- }
- private void ResolveQuestionId(UpdateExamManageDto actionRequest, string id)
- {
- actionRequest.UserExamDtos.ForEach(x => x.ExamId = id);
- actionRequest.ExamQuestionScoreDtos.ForEach(x => x.ExamManageId = id);
- }
- private async Task<List<ExamQuestionScoreBak>> AddExamQuestionScores(AddExamManageDto actionRequest, CancellationToken cancellationToken)
- {
- if (actionRequest.ExamQuestionScoreDtos == null) return null;
- actionRequest.ExamQuestionScoreDtos.ResolveOperationStatus();
- var examQuestionScoreDtos = actionRequest.ExamQuestionScoreDtos.Where(x => x.OperationStatus == EEOperationStatus.Add).ToList();
- var examQuestionScores = _mapper.Map<List<ExamQuestionScoreBak>>(examQuestionScoreDtos);
- examQuestionScores.ToInsert(_sessionContext);
- await _examQuestionScoreRepository.ValidateAddAsync(examQuestionScores, cancellationToken);
- return examQuestionScores;
- }
- private async Task<List<ExamUserExam>> AddUserExam(AddExamManageDto actionRequest, CancellationToken cancellationToken)
- {
- if (actionRequest.UserExamDtos == null) return null;
- actionRequest.UserExamDtos.ResolveOperationStatus();
- var userExamDtos = actionRequest.UserExamDtos.Where(x => x.OperationStatus == EEOperationStatus.Add).ToList();
- var userExams = new List<ExamUserExam>();
- //_mapper.Map<List<UserExam>>(userExamDtos);
- userExamDtos.ForEach(x =>
- {
- userExams.Add(_mapper.Map<ExamUserExam>(x));
- });
- userExams.ToInsert(_sessionContext);
- await _userExamRepository.ValidateAddAsync(userExams, cancellationToken);
- return userExams;
- }
- private async Task<List<ExamUserExam>> UpdateUserExam(UpdateExamManageDto actionRequest, List<ExamUserExam> all, CancellationToken cancellationToken)
- {
- if (actionRequest.UserExamDtos == null) return null;
- var userExamDtos = actionRequest.UserExamDtos.Where(x => x.OperationStatus == EEOperationStatus.Update).ToList();
- var ids = userExamDtos.Select(x => x.Id);
- var userExams = all.Where(x => ids.Contains(x.Id)).ToList();
- //userExams = _mapper.Map<List<UpdateUserExamDto>, List<UserExam>>(userExamDtos, userExams);
- userExams.ForEach(item =>
- {
- var userExamDto = userExamDtos.FirstOrDefault(x => x.Id == item.Id);
- if (userExamDto != null)
- _mapper.Map<UpdateUserExamDto, ExamUserExam>(userExamDto, item);
- });
- userExams.ToUpdate(_sessionContext);
- await _userExamRepository.ValidateUpdateAsync(userExams, cancellationToken);
- return userExams;
- }
- private async Task<List<ExamQuestionScoreBak>> UpdateExamQuestionScores(UpdateExamManageDto actionRequest, List<ExamQuestionScoreBak> all, CancellationToken cancellationToken)
- {
- if (actionRequest.ExamQuestionScoreDtos == null) return null;
- var examQuestionScoreDtos = actionRequest.ExamQuestionScoreDtos.Where(x => x.OperationStatus == EEOperationStatus.Update).ToList();
- var ids = examQuestionScoreDtos.Select(x => x.Id);
- var examQuestionScores = all.Where(x => ids.Contains(x.Id)).ToList();
- //examQuestionScores = _mapper.Map<List<UpdateExamQuestionScoreDto>, List<ExamQuestionScore>>(examQuestionScoreDtos, examQuestionScores);
- examQuestionScores.ForEach(item =>
- {
- var examQuestionScoreDto = examQuestionScoreDtos.FirstOrDefault(x => x.Id == item.Id);
- if (examQuestionScoreDto != null)
- item = _mapper.Map<UpdateExamQuestionScoreDto, ExamQuestionScoreBak>(examQuestionScoreDto, item);
- });
- examQuestionScores.ToUpdate(_sessionContext);
- await _examQuestionScoreRepository.ValidateUpdateAsync(examQuestionScores, cancellationToken);
- return examQuestionScores;
- }
- private async Task DeleteUserExam(EntityQueryRequest entityQueryRequest, CancellationToken cancellationToken)
- {
- await _userExamRepository.DeleteWithValidateAsync(entityQueryRequest, cancellationToken);
- }
- private async Task DeleteExamQuestionScores(EntityQueryRequest entityQueryRequest, CancellationToken cancellationToken)
- {
- await _examQuestionScoreRepository.DeleteWithValidateAsync(entityQueryRequest, cancellationToken);
- }
- private async Task<List<ExamUserExam>> ModifyUserExam(UpdateExamManageDto actionRequest, CancellationToken cancellationToken)
- {
- if (actionRequest.UserExamDtos == null) return null;
- var all = await _userExamRepository.Queryable().Where(x => x.ExamId == actionRequest.Id).ToListAsync();
- actionRequest.UserExamDtos.ResolveOperationStatus(all);
- var userExams = new List<ExamUserExam>();
- userExams.AddRangeExt(await AddUserExam(_addExamManageDto, cancellationToken));
- userExams.AddRangeExt(await UpdateUserExam(actionRequest, all, cancellationToken));
- var questionOptionsDtos = actionRequest.UserExamDtos.Where(x => x.OperationStatus == EEOperationStatus.Delete);
- var ids = questionOptionsDtos.Select(m => m.Id);
- EntityQueryRequest entityQueryRequest = ResovleDelete<ExamUserExam>(ids);
- await DeleteUserExam(entityQueryRequest, cancellationToken);
- return userExams;
- }
- private async Task<List<ExamQuestionScoreBak>> ModifyExamQuestionScores(UpdateExamManageDto actionRequest, CancellationToken cancellationToken)
- {
- if (actionRequest.ExamQuestionScoreDtos == null) return null;
- var all = await _examQuestionScoreRepository.Queryable().Where(x => x.ExamManageId == actionRequest.Id).ToListAsync();
- var examQuestionScores = new List<ExamQuestionScoreBak>();
- examQuestionScores.AddRangeExt(await AddExamQuestionScores(_addExamManageDto, cancellationToken));
- examQuestionScores.AddRangeExt(await UpdateExamQuestionScores(actionRequest, all, cancellationToken));
- var examQuestionScoreDtos = actionRequest.ExamQuestionScoreDtos.Where(x => x.OperationStatus == EEOperationStatus.Delete);
- var ids = examQuestionScoreDtos.Select(m => m.Id);
- EntityQueryRequest entityQueryRequest = ResovleDelete<ExamQuestionScoreBak>(ids);
- await DeleteExamQuestionScores(entityQueryRequest, cancellationToken);
- return examQuestionScores;
- }
- private EntityQueryRequest ResovleDelete<T>(IEnumerable<string> ids) where T : class, IEntity<string>, new()
- {
- Expressionable<T> expressionable = ExpressionableUtility.CreateExpression<T>();
- expressionable.AndIF(ids.Any(), x => ids.Contains(x.Id));
- var entityQueryRequest = new EntityQueryRequest
- {
- Expression = ids.Any() ? expressionable.ToExpression() : null
- };
- return entityQueryRequest;
- }
- private async Task<List<UserExamDto>> GetUserExams(EntityQueryRequest entityQueryRequest)
- {
- var userRepository = new ExamRepository<User>(_uow, _dataPermissionFilterBuilder, _serviceProvider);
- var userExams = _userExamRepository.Queryable().Where(x => x.ExamId == entityQueryRequest.Id);
- var userTable = userRepository.Queryable();
- var userExamDtos = userExams.InnerJoin(userTable,(e,u)=>e.UserId == u.Id).Select((e, u) => new UserExamDto
- {
- Id = e.Id,
- Name = u.Name,
- ExamId = e.ExamId,
- UserId = e.UserId,
- FullOrgName = u.FullOrgName
- });
- return await userExamDtos.ToListAsync();
- }
- private async Task<List<ExamQuestionScoreDto>> GetExamQuestionScores(EntityQueryRequest entityQueryRequest,ExamManage examManage)
- {
- var examQuestionScores = _examQuestionScoreRepository.Queryable().Where(x => x.ExamManageId == entityQueryRequest.Id);
- var examQuestionScoreDtos = examQuestionScores.Select(q => new ExamQuestionScoreDto
- {
- Id = q.Id,
- QuestionType = q.QuestionType,
- ExamManageId = q.ExamManageId,
- Score = q.Score
- });
- if(examManage.Mode == Share.Enums.Exams.EExamMode.Random)
- {
- var tagQuestionRepository = new ExamRepository<ExamTagQuestion>(_uow, _dataPermissionFilterBuilder, _serviceProvider);
- var tagQuestionTable = tagQuestionRepository.Queryable().Where(x => x.RuleId == examManage.ExtractRuleId);
- var result = examQuestionScoreDtos.InnerJoin(tagQuestionTable, (e, t) => e.QuestionType == t.QuestionType).Select((e, t) => new ExamQuestionScoreDto
- {
- Id = e.Id,
- QuestionType = e.QuestionType,
- ExamManageId = e.ExamManageId,
- Score = e.Score,
- Count = t.Count
- }).Distinct();
- return await result.ToListAsync();
- }
- else
- {
- var testPaperItemRepository = new ExamRepository<ExamTestPaperItem>(_uow, _dataPermissionFilterBuilder, _serviceProvider);
- var testPaperItemTable = testPaperItemRepository.Queryable().Where(x => x.TestPaperId == examManage.TestPaperId);
- var result = examQuestionScoreDtos.InnerJoin(testPaperItemTable, (e, t) => e.QuestionType == t.QuestionType)
- .GroupBy((e, t) => new
- {
- Id = e.Id,
- QuestionType = e.QuestionType,
- ExamManageId = e.ExamManageId,
- Score = e.Score,
- })
- .Select((e, t) => new ExamQuestionScoreDto
- {
- Id = e.Id,
- QuestionType = e.QuestionType,
- ExamManageId = e.ExamManageId,
- Score = e.Score,
- Count = SqlFunc.AggregateCount(t.Id)
- });
- return await result.ToListAsync();
- }
- }
- private void CalcuteTotalScore(AddExamManageDto actionRequest)
- {
- if (actionRequest.ExamQuestionScoreDtos == null) return;
- var examQuestionScoreDtos = actionRequest.ExamQuestionScoreDtos.Where(x => x.OperationStatus != EEOperationStatus.Delete);
- var totalScore = examQuestionScoreDtos.Sum(x => x.Count * x.Score);
- if (totalScore != actionRequest.TotalScore)
- {
- throw new UserFriendlyException(ExamErrorMessage.ServiceError, string.Format(ExamErrorMessage.IsNotEqual, "试题分数总和", typeof(AddExamManageDto).GetDescription(nameof(AddExamManageDto.TotalScore))));
- }
- }
- private ISugarQueryable<ExamManageViewResponse> Queryable(ExamManagePagedRequest queryRequest)
- {
- var expression = queryRequest.GetExpression();
- var questionTable = _repository.Queryable().Where(expression);
- var queryable = questionTable.Select(e => new ExamManageViewResponse
- {
- Id = e.Id,
- Code = e.Code,
- Count = e.Count,
- CutoffScore = e.CutoffScore,
- EndTime = e.EndTime,
- ExamStatus = e.ExamStatus,
- ExamType = e.ExamType,
- Mode = e.Mode,
- Name = e.Name,
- Remark = e.Name,
- SortIndex = e.SortIndex,
- StartTime = e.StartTime,
- TimeSpan = e.TimeSpan,
- TotalScore = e.TotalScore,
- });
- return queryable;
- }
- private async Task<List<ExamQuestion>> GetQuestions(List<ExamTagQuestion> tagQuestionCounts)
- {
- var questionRepository = new ExamRepository<ExamQuestion>(_uow, _dataPermissionFilterBuilder, _serviceProvider);
- var questionTagRepository = new ExamRepository<ExamQuestionTag>(_uow, _dataPermissionFilterBuilder, _serviceProvider);
- var questionTable = questionRepository.Queryable();
- var questionTagTable = questionTagRepository.Queryable();
- if (tagQuestionCounts != null && tagQuestionCounts.Any())
- {
- var questionQuerables = new List<ISugarQueryable<ExamQuestion>>();
- tagQuestionCounts.ForEach(item =>
- {
- ISugarQueryable<ExamQuestion> queryable = questionTable.InnerJoin(questionTagTable, (q, t) => t.Id == t.QuestionId)
- .Where((q, t) => q.QuestionType == item.QuestionType && t.TagId == item.TagId).Take(item.Count).Select((q, t) => q);
- questionQuerables.Add(queryable);
- });
- var queryResult = questionRepository.UnionAll(questionQuerables.ToArray());
- return await queryResult.ToListAsync();
- }
- else
- {
- return null;
- }
- }
- #endregion
- }
- }
|