|
@@ -1,10 +1,586 @@
|
|
|
-using System;
|
|
|
-using System.Collections.Generic;
|
|
|
+using DocumentFormat.OpenXml.Drawing;
|
|
|
+using DocumentFormat.OpenXml.Drawing.Charts;
|
|
|
+using DocumentFormat.OpenXml.Office2010.Excel;
|
|
|
+using Exam.Application.Interface.Exam;
|
|
|
+using Exam.ExamManages;
|
|
|
+using Exam.Infrastructure.Data.Entity;
|
|
|
+using Exam.Infrastructure.Data.Interface;
|
|
|
+using Exam.Infrastructure.Enums;
|
|
|
+using Exam.Infrastructure.Validation.Validation;
|
|
|
+using Exam.Infrastructure.Web.Utilities;
|
|
|
+using Exam.Insfrastructure.Service.Service;
|
|
|
+using Exam.Questions;
|
|
|
+using Exam.Repository.Sqlsugar.Repositories;
|
|
|
+using Exam.Share;
|
|
|
+using Exam.Share.Dtos.ExamManage;
|
|
|
+using Exam.Share.ViewResponses.Exam;
|
|
|
+using Exam.TestPapers;
|
|
|
+using Hotline.Application.Exam.Core.Extensions;
|
|
|
+using Hotline.Application.Exam.QueryExtensions.ExamManages;
|
|
|
+using Hotline.Exams.ExamManages;
|
|
|
+using Hotline.Repository.SqlSugar;
|
|
|
+using Hotline.Repository.SqlSugar.DataPermissions;
|
|
|
+using Hotline.Repository.SqlSugar.Exam.Interfaces.ExamManages;
|
|
|
+using Hotline.Repository.SqlSugar.Extensions;
|
|
|
+using Hotline.Repository.SqlSugar.Interface;
|
|
|
+using Hotline.Share.Dtos.ExamManages;
|
|
|
+using Hotline.Share.Requests.Exam;
|
|
|
+using Hotline.Share.ViewResponses;
|
|
|
+using Hotline.Share.ViewResponses.Exam;
|
|
|
+using JiebaNet.Segmenter.Common;
|
|
|
+using MapsterMapper;
|
|
|
+using SqlSugar;
|
|
|
using System.Linq;
|
|
|
-using System.Text;
|
|
|
-using System.Threading.Tasks;
|
|
|
+using XF.Domain.Authentications;
|
|
|
+using XF.Domain.Dependency;
|
|
|
+using XF.Domain.Exceptions;
|
|
|
|
|
|
namespace Hotline.Application.Exam.Service.ExamManages
|
|
|
{
|
|
|
-
|
|
|
+ public class UserExamService : ApiService<UserExam, AddUserExamDto, UpdateUserExamDto, HotlineDbContext>, IUserExamService, IScopeDependency
|
|
|
+ {
|
|
|
+ private readonly IUserExamRepository _repository;
|
|
|
+ private readonly IUserExamItemRepository _userExamItemRepository;
|
|
|
+ private readonly IUserExamItemOptionRepository _userExamItemOptionRepository;
|
|
|
+ private readonly IDataPermissionFilterBuilder _dataPermissionFilterBuilder;
|
|
|
+ private readonly IServiceProvider _serviceProvider;
|
|
|
+ private readonly IMapper _mapper;
|
|
|
+ private readonly ISessionContext _sessionContext
|
|
|
+ ;
|
|
|
+
|
|
|
+ public UserExamService(IUserExamRepository repository,
|
|
|
+ IUserExamItemRepository userExamItemRepository,
|
|
|
+ IUserExamItemOptionRepository userExamItemOptionRepository,
|
|
|
+ IDataPermissionFilterBuilder dataPermissionFilterBuilder, IServiceProvider serviceProvider,
|
|
|
+ IMapper mapper, ISessionContext sessionContext) : base(repository, mapper)
|
|
|
+ {
|
|
|
+ this._repository = repository;
|
|
|
+ this._userExamItemRepository = userExamItemRepository;
|
|
|
+ this._userExamItemOptionRepository = userExamItemOptionRepository;
|
|
|
+ this._dataPermissionFilterBuilder = dataPermissionFilterBuilder;
|
|
|
+ this._serviceProvider = serviceProvider;
|
|
|
+ this._mapper = mapper;
|
|
|
+ this._sessionContext = sessionContext;
|
|
|
+ }
|
|
|
+
|
|
|
+ #region public method
|
|
|
+ public Task<UserExamDto> GetAsync(EntityQueryRequest entityQueryRequest)
|
|
|
+ {
|
|
|
+ throw new NotImplementedException();
|
|
|
+ }
|
|
|
+ public Task<(int, List<UserExamResultViewResponse>)> GetListAsync(UserExamPagedRequest queryRequest)
|
|
|
+ {
|
|
|
+ throw new NotImplementedException();
|
|
|
+ }
|
|
|
+
|
|
|
+ public async Task<ExamQuestionDto> GetExamQuestionDto(ExamQuestionRequest examQuestionRequest)
|
|
|
+ {
|
|
|
+ var expression = examQuestionRequest.GetExpression();
|
|
|
+ var quesetion = await new ExamRepository<Question>(_repository.UOW, _dataPermissionFilterBuilder, _serviceProvider).Queryable().Where(expression).FirstAsync();
|
|
|
+
|
|
|
+ if (quesetion != null)
|
|
|
+ {
|
|
|
+ var examQuestionDto = _mapper.Map<ExamQuestionDto>(quesetion);
|
|
|
+
|
|
|
+ if (examQuestionDto.QuestionType == Share.Enums.Exams.EQuestionType.Multi
|
|
|
+ || examQuestionDto.QuestionType == Share.Enums.Exams.EQuestionType.Single
|
|
|
+ || examQuestionDto.QuestionType == Share.Enums.Exams.EQuestionType.Judge
|
|
|
+ )
|
|
|
+ {
|
|
|
+ var questionOptions = await new ExamRepository<QuestionOptions>(_repository.UOW, _dataPermissionFilterBuilder, _serviceProvider).Queryable().Where(x => x.QuestionId == quesetion.Id).ToListAsync();
|
|
|
+
|
|
|
+ if (questionOptions != null)
|
|
|
+ {
|
|
|
+ examQuestionDto.QuestionOptions = new List<ExamQuestionOptionsDto>();
|
|
|
+
|
|
|
+ questionOptions.ForEach(item =>
|
|
|
+ {
|
|
|
+ examQuestionDto.QuestionOptions.Add(_mapper.Map<ExamQuestionOptionsDto>(item));
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return examQuestionDto;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ throw new UserFriendlyException(ErrorMessage.ServiceError, string.Format(ErrorMessage.IsNotExists, nameof(Question)));
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public async Task<List<ExamQuestionViewResponse>> GetExamQuestionViewResponses(ExamQuestionGroupRequest examQuestionGroupRequest)
|
|
|
+ {
|
|
|
+ var expression = examQuestionGroupRequest.GetExpression();
|
|
|
+ var examManageTable = new ExamRepository<ExamManage>(_repository.UOW, _dataPermissionFilterBuilder, _serviceProvider).Queryable().Where(expression);
|
|
|
+ var testPaperItemTable = new ExamRepository<TestPaperItem>(_repository.UOW, _dataPermissionFilterBuilder, _serviceProvider).Queryable();
|
|
|
+ var questionTable = new ExamRepository<Question>(_repository.UOW, _dataPermissionFilterBuilder, _serviceProvider).Queryable();
|
|
|
+
|
|
|
+ var queryable = await examManageTable.InnerJoin(testPaperItemTable, (e, i) => e.TestPaperId == i.TestPaperId)
|
|
|
+ .InnerJoin(questionTable, (e, i, q) => i.QuestionId == q.Id)
|
|
|
+ .Select((e, i, q) => q).ToListAsync();
|
|
|
+
|
|
|
+ var result = queryable.GroupBy(x => x.QuestionType).Select(m => new ExamQuestionViewResponse
|
|
|
+ {
|
|
|
+ QuestionType = m.Key,
|
|
|
+ Questions = m.Select(n => new SimpleViewResponse
|
|
|
+ {
|
|
|
+ Id = n.Id
|
|
|
+ }).ToList<IViewResponse>()
|
|
|
+ }).ToList();
|
|
|
+
|
|
|
+ return result;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public async Task<PageViewResponse<UserExamResultViewResponse>> GetPagedListAsync(UserExamPagedRequest queryRequest)
|
|
|
+ {
|
|
|
+ SqlSugar.ISugarQueryable<UserExamResultViewResponse> queryable = GetQueryable(queryRequest);
|
|
|
+
|
|
|
+ var list = await queryable.ToPageListAsync(queryRequest.PageIndex, queryRequest.PageSize);
|
|
|
+ var total = await queryable.CountAsync();
|
|
|
+
|
|
|
+ var result = new UserExamResultPageViewResponse
|
|
|
+ {
|
|
|
+ Items = list,
|
|
|
+ Pagination = new Pagination(queryRequest.PageIndex, queryRequest.PageSize, total)
|
|
|
+ };
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ public async Task<GradingExamQuestionDto> GradingAsync(GradingExtamItemDto gradingExtamItemDto, CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+ var userExamItem = await _userExamItemRepository.GetAsync(m => m.Id == gradingExtamItemDto.UserExamItemId);
|
|
|
+
|
|
|
+ if (userExamItem != null)
|
|
|
+ {
|
|
|
+ userExamItem = _mapper.Map<GradingExtamItemDto, UserExamItem>(gradingExtamItemDto, userExamItem);
|
|
|
+
|
|
|
+ await _userExamItemRepository.UpdateWithValidateAsync(userExamItem, cancellationToken);
|
|
|
+ }
|
|
|
+
|
|
|
+ return await GetNextExamQuestion(gradingExtamItemDto);
|
|
|
+ }
|
|
|
+
|
|
|
+ public async Task SubmitAsync(SubmitExamDto submitExamDto, CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+ var userExam = await _repository.GetAsync(x => x.Id == submitExamDto.Id);
|
|
|
+
|
|
|
+ if (userExam != null)
|
|
|
+ {
|
|
|
+ userExam = _mapper.Map<SubmitExamDto, UserExam>(submitExamDto, userExam);
|
|
|
+
|
|
|
+ await _repository.UpdateWithValidateAsync(userExam, cancellationToken);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public async Task<UserExamQuestionDto> ExamAsync(AddUserExamItemDto addUserExamItemDto, CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+ await AddUserExamItem(addUserExamItemDto, cancellationToken);
|
|
|
+
|
|
|
+ await AddUserExamItemOptions(addUserExamItemDto, cancellationToken);
|
|
|
+
|
|
|
+ await AddExamAnswer(addUserExamItemDto,cancellationToken);
|
|
|
+
|
|
|
+ var gradingExamQuestionDto = await GetNextExamQuestion(addUserExamItemDto);
|
|
|
+
|
|
|
+ return gradingExamQuestionDto;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public async Task<UserExamQuestionDto> UpdateExamAsync(UpdateUserExamItemDto updateUserExamItemDto, CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+ await UpdateUserExamItem(updateUserExamItemDto, cancellationToken);
|
|
|
+
|
|
|
+ await ModifyUserItemOptions(updateUserExamItemDto, cancellationToken);
|
|
|
+
|
|
|
+ await UpdateExamAnswer(updateUserExamItemDto,cancellationToken);
|
|
|
+
|
|
|
+ var gradingExamQuestionDto = await GetNextExamQuestion(updateUserExamItemDto);
|
|
|
+
|
|
|
+ return gradingExamQuestionDto;
|
|
|
+ }
|
|
|
+ public async Task StartUserExamAsync(StartUserExamDto startUserExamDto, CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+ var userExam = await _repository.GetAsync(x => x.Id == startUserExamDto.Id);
|
|
|
+
|
|
|
+ if (userExam == null) return;
|
|
|
+
|
|
|
+ userExam.ExamStatus = Share.Enums.Exams.EExamStatus.Executing;
|
|
|
+
|
|
|
+ userExam.ToUpdate();
|
|
|
+
|
|
|
+ await _repository.UpdateWithValidateAsync(userExam, cancellationToken);
|
|
|
+ }
|
|
|
+
|
|
|
+ public async Task CompleteGradingAsync(GradingExamDto gradingExtamDto, CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+ var userExam = await _repository.GetAsync(x => x.Id == gradingExtamDto.Id);
|
|
|
+
|
|
|
+ if (userExam == null) return;
|
|
|
+
|
|
|
+ var userExamItems = await _userExamItemRepository.Queryable().Where(x => x.UserExamId == gradingExtamDto.Id).ToListAsync();
|
|
|
+
|
|
|
+ if (userExamItems != null)
|
|
|
+ {
|
|
|
+ var totalScore = userExamItems.Sum(x => x.Score);
|
|
|
+ userExam.Score = totalScore;
|
|
|
+
|
|
|
+ var examManage = await new ExamRepository<ExamManage>(_repository.UOW, _dataPermissionFilterBuilder, _serviceProvider).GetAsync(x => x.Id == userExam.ExamId);
|
|
|
+ userExam.IsSuccess = totalScore >= examManage.CutoffScore;
|
|
|
+
|
|
|
+ userExam.ExamStatus = Share.Enums.Exams.EExamStatus.Complete;
|
|
|
+
|
|
|
+ userExam.ToUpdate();
|
|
|
+
|
|
|
+ await _repository.UpdateWithValidateAsync(userExam,cancellationToken);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public async Task<List<GradingExamQuestionDto>> GetGradingExamQuestion(GradingExamRequest gradingExamRequest)
|
|
|
+ {
|
|
|
+ var expression = gradingExamRequest.GetExpression();
|
|
|
+ var userExamTable = _repository.Queryable().Where(expression);
|
|
|
+
|
|
|
+ var userExamItemTable = _userExamItemRepository.Queryable();
|
|
|
+ var userExamItemOptionTable = _userExamItemOptionRepository.Queryable();
|
|
|
+ var examAnswerTable = new ExamRepository<ExamAnswer>(_repository.UOW, _dataPermissionFilterBuilder, _serviceProvider).Queryable();
|
|
|
+ var questionTable = new ExamRepository<Question>(_repository.UOW, _dataPermissionFilterBuilder, _serviceProvider).Queryable();
|
|
|
+ var quesitonOptionTable = new ExamRepository<QuestionOptions>(_repository.UOW, _dataPermissionFilterBuilder, _serviceProvider).Queryable();
|
|
|
+
|
|
|
+ var queryResult = await userExamTable.InnerJoin(userExamItemTable, (e, i) => e.Id == i.UserExamId)
|
|
|
+ .InnerJoin(questionTable, (e, i, q) => i.QuestionId == q.Id)
|
|
|
+ .LeftJoin(userExamItemOptionTable, (e, i, q, o) => i.Id == o.UserExamItemId)
|
|
|
+ .LeftJoin(quesitonOptionTable, (e, i, q, o, qo) => o.QuestionOptionId == qo.Id)
|
|
|
+ .LeftJoin(examAnswerTable, (e, i, q, o, qo, a) => i.UserExamId == a.UserExamItemId)
|
|
|
+ .Select(
|
|
|
+ (e, i, q, o, qo, a) => new GradingExamQuestionTempDto
|
|
|
+ {
|
|
|
+ Id = i.Id,
|
|
|
+ QuestionType = q.QuestionType,
|
|
|
+ Answer = a != null ? a.Answer : string.Empty,
|
|
|
+ Title = q.Title,
|
|
|
+ QuestionOptionId = o.Id,
|
|
|
+ UserExamItemId = i.Id,
|
|
|
+ Content = qo.Content,
|
|
|
+ IsAnswer = qo.IsAnswer
|
|
|
+ }
|
|
|
+ ).ToListAsync();
|
|
|
+
|
|
|
+ var gradingExamQuestionDtos = queryResult.GroupBy(x => new
|
|
|
+ {
|
|
|
+ Id = x.Id,
|
|
|
+ QuestionType = x.QuestionType
|
|
|
+ }).Select(g => new GradingExamQuestionDto
|
|
|
+ {
|
|
|
+ Answer = g.FirstOrDefault().Answer,
|
|
|
+ QuestionType = g.Key.QuestionType,
|
|
|
+ Id = g.Key.Id,
|
|
|
+ Title = g.FirstOrDefault().Title,
|
|
|
+ UserExamItemOptionDtos = g.Select(i=>new GradingUserExamItemOptionDto
|
|
|
+ {
|
|
|
+ QuestionOptionId =i.QuestionOptionId,
|
|
|
+ UserExamItemId =i.UserExamItemId,
|
|
|
+ Content = i.Content,
|
|
|
+ IsAnswer = i.IsAnswer
|
|
|
+
|
|
|
+ }).ToList()
|
|
|
+ }).ToList();
|
|
|
+
|
|
|
+ return gradingExamQuestionDtos;
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ #region private method
|
|
|
+
|
|
|
+
|
|
|
+ private async Task<GradingExamQuestionDto> GetNextExamQuestion(GradingExtamItemDto gradingExtamItemDto)
|
|
|
+ {
|
|
|
+ // TODO: 获取未阅卷的第一道题
|
|
|
+ var current = _userExamItemRepository.Queryable().Where(x => x.Id == gradingExtamItemDto.UserExamItemId);
|
|
|
+ var userExamItemTable = _userExamItemRepository.Queryable().Where(x => !x.IsCheck);
|
|
|
+
|
|
|
+ var userExamItem = current.InnerJoin(userExamItemTable, (c, u) => c.UserExamId == u.UserExamId).OrderBy(x => x.SortIndex).First();
|
|
|
+
|
|
|
+ if (userExamItem != null)
|
|
|
+ {
|
|
|
+ var question = new ExamRepository<Question>(_repository.UOW, _dataPermissionFilterBuilder, _serviceProvider).Queryable().Where(x => x.Id == userExamItem.Id).First();
|
|
|
+
|
|
|
+ if (question == null) return null;
|
|
|
+
|
|
|
+ var gradingExamQuestionDto = new GradingExamQuestionDto();
|
|
|
+
|
|
|
+ gradingExamQuestionDto = _mapper.Map<Question, GradingExamQuestionDto>(question, gradingExamQuestionDto);
|
|
|
+
|
|
|
+ if (question.QuestionType.CheckSelectType())
|
|
|
+ {
|
|
|
+ var userExamItemOptionTable = _userExamItemOptionRepository.Queryable().Where(x => x.UserExamItemId == userExamItem.Id);
|
|
|
+ var quesitonOptionTable = new ExamRepository<QuestionOptions>(_repository.UOW, _dataPermissionFilterBuilder, _serviceProvider).Queryable();
|
|
|
+
|
|
|
+ var queryResult = userExamItemOptionTable.InnerJoin(quesitonOptionTable, (u, q) => u.QuestionOptionId == q.Id)
|
|
|
+ .Select((u, q) => new GradingUserExamItemOptionDto
|
|
|
+ {
|
|
|
+ Content = q.Content,
|
|
|
+ QuestionOptionId = u.QuestionOptionId,
|
|
|
+ UserExamItemId = u.UserExamItemId,
|
|
|
+ IsAnswer = q.IsAnswer
|
|
|
+ });
|
|
|
+
|
|
|
+ gradingExamQuestionDto.UserExamItemOptionDtos = queryResult.ToList();
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ var examAnswer = new ExamRepository<ExamAnswer>(_repository.UOW, _dataPermissionFilterBuilder, _serviceProvider).Queryable().Where(x => x.UserExamItemId == userExamItem.Id).First();
|
|
|
+
|
|
|
+ gradingExamQuestionDto.Answer = examAnswer.Answer ?? string.Empty;
|
|
|
+ }
|
|
|
+
|
|
|
+ return gradingExamQuestionDto;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private async Task AddExamAnswer(AddUserExamItemDto addUserExamItemDto, CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+ if (addUserExamItemDto.QuestionType.CheckSelectType()) return;
|
|
|
+
|
|
|
+ var examAnswer = new ExamAnswer
|
|
|
+ {
|
|
|
+ UserId = _sessionContext.UserId,
|
|
|
+ Answer = addUserExamItemDto.Answer
|
|
|
+ };
|
|
|
+
|
|
|
+ examAnswer.ToInsert();
|
|
|
+
|
|
|
+ await new ExamRepository<ExamAnswer>(_repository.UOW, _dataPermissionFilterBuilder, _serviceProvider).AddWithValidateAsync(examAnswer, cancellationToken);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private async Task<UserExamQuestionDto> GetNextExamQuestion(AddUserExamItemDto addUserExamItemDto)
|
|
|
+ {
|
|
|
+ // TODO: 获取未阅卷的第一道题
|
|
|
+ var userExamItem = _userExamItemRepository.Queryable().Where(x => x.UserExamId == addUserExamItemDto.UserExamId && !x.IsCheck).OrderBy(o => o.SortIndex).First();
|
|
|
+
|
|
|
+
|
|
|
+ if (userExamItem != null)
|
|
|
+ {
|
|
|
+ var question = new ExamRepository<Question>(_repository.UOW, _dataPermissionFilterBuilder, _serviceProvider).Queryable().Where(x => x.Id == userExamItem.Id).First();
|
|
|
+
|
|
|
+ if (question == null) return null;
|
|
|
+
|
|
|
+ var userExamQuestionDto = new UserExamQuestionDto();
|
|
|
+
|
|
|
+ userExamQuestionDto = _mapper.Map<Question, UserExamQuestionDto>(question, userExamQuestionDto);
|
|
|
+
|
|
|
+ if (question.QuestionType.CheckSelectType())
|
|
|
+ {
|
|
|
+ var userExamItemOptionTable = _userExamItemOptionRepository.Queryable().Where(x => x.UserExamItemId == userExamItem.Id);
|
|
|
+ var quesitonOptionTable = new ExamRepository<QuestionOptions>(_repository.UOW, _dataPermissionFilterBuilder, _serviceProvider).Queryable();
|
|
|
+
|
|
|
+ var queryResult = userExamItemOptionTable.InnerJoin(quesitonOptionTable, (u, q) => u.QuestionOptionId == q.Id)
|
|
|
+ .Select((u, q) => new UserExamItemOptionDto
|
|
|
+ {
|
|
|
+ Content = q.Content,
|
|
|
+ QuestionOptionId = u.QuestionOptionId,
|
|
|
+ UserExamItemId = u.UserExamItemId
|
|
|
+ });
|
|
|
+
|
|
|
+ userExamQuestionDto.UserExamItemOptionDtos = queryResult.ToList();
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ var examAnswer = new ExamRepository<ExamAnswer>(_repository.UOW, _dataPermissionFilterBuilder, _serviceProvider).Queryable().Where(x => x.UserExamItemId == userExamItem.Id).First();
|
|
|
+
|
|
|
+ userExamQuestionDto.Answer = examAnswer.Answer ?? string.Empty;
|
|
|
+ }
|
|
|
+
|
|
|
+ return userExamQuestionDto;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private async Task UpdateExamAnswer(UpdateUserExamItemDto updateUserExamItemDto, CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+ var repository = new ExamRepository<ExamAnswer>(_repository.UOW, _dataPermissionFilterBuilder, _serviceProvider);
|
|
|
+
|
|
|
+ var examAnswerTable = repository.Queryable();
|
|
|
+
|
|
|
+ var userExamItemTable = _userExamItemRepository.Queryable().Where(x => x.Id == updateUserExamItemDto.Id);
|
|
|
+
|
|
|
+ var examAnswer = await examAnswerTable.InnerJoin(userExamItemTable, (e, u) => e.UserExamItemId == u.Id).Select((e, u) => e).FirstAsync();
|
|
|
+
|
|
|
+ if (!updateUserExamItemDto.QuestionType.CheckSelectType())
|
|
|
+ {
|
|
|
+ examAnswer.Answer = updateUserExamItemDto.Answer;
|
|
|
+
|
|
|
+ examAnswer.ToUpdate();
|
|
|
+
|
|
|
+ await repository.AddWithValidateAsync(examAnswer, cancellationToken);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private async Task ModifyUserItemOptions(UpdateUserExamItemDto updateUserExamItemDto, CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+ await UpdateUserItemOptions(updateUserExamItemDto, cancellationToken);
|
|
|
+
|
|
|
+ var addUserExamItemDto = _mapper.Map<AddUserExamItemDto>(updateUserExamItemDto);
|
|
|
+
|
|
|
+ addUserExamItemDto.UserExamItemOptionDtos = new List<AddUserExamItemOptionDto>();
|
|
|
+
|
|
|
+ updateUserExamItemDto.UserExamItemOptionDtos.ForEach(item =>
|
|
|
+ {
|
|
|
+ addUserExamItemDto.UserExamItemOptionDtos.Add(_mapper.Map<AddUserExamItemOptionDto>(item));
|
|
|
+ });
|
|
|
+
|
|
|
+ await AddUserExamItemOptions(addUserExamItemDto, cancellationToken);
|
|
|
+
|
|
|
+ if (updateUserExamItemDto.QuestionType.CheckSelectType())
|
|
|
+ {
|
|
|
+ var ids = updateUserExamItemDto.UserExamItemOptionDtos.Where(x => x.OperationStatus == EEOperationStatus.Delete).Select(x => x.Id);
|
|
|
+ var entityQuestionRequest = new EntityQueryRequest
|
|
|
+ {
|
|
|
+ Expression = ExpressionableUtility.CreateExpression<UserExamItemOptions>()
|
|
|
+ .AndIF(updateUserExamItemDto.Id.IsNotEmpty(), x => ids.Contains(x.Id)).ToExpression()
|
|
|
+ };
|
|
|
+
|
|
|
+ await DeleteUserExamItemOptions(entityQuestionRequest, cancellationToken);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private async Task UpdateUserItemOptions(UpdateUserExamItemDto updateUserExamItemDto, CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+
|
|
|
+ if (updateUserExamItemDto.QuestionType.CheckSelectType())
|
|
|
+ {
|
|
|
+
|
|
|
+ var userExamItemOptions = await _userExamItemOptionRepository.Queryable().Where(x => x.UserExamItemId == updateUserExamItemDto.Id).ToListAsync();
|
|
|
+
|
|
|
+ var entities = new List<UserExamItemOptions>();
|
|
|
+
|
|
|
+ if (updateUserExamItemDto.UserExamItemOptionDtos != null)
|
|
|
+ {
|
|
|
+ updateUserExamItemDto.UserExamItemOptionDtos.Where(m => m.OperationStatus == EEOperationStatus.Update).ToList().ForEach(x =>
|
|
|
+ {
|
|
|
+ var entity = userExamItemOptions.FirstOrDefault(m => m.Id == x.Id);
|
|
|
+ if (entity != null)
|
|
|
+ {
|
|
|
+ entities.Add(_mapper.Map<UpdateUserExamItemOptionDto, UserExamItemOptions>(x, entity));
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ entities.ToUpdate();
|
|
|
+
|
|
|
+ await _userExamItemOptionRepository.UpdateWithValidateAsync(entities, cancellationToken);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private async Task UpdateUserExamItem(UpdateUserExamItemDto updateUserExamItemDto, CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+ var userExamItem = await _userExamItemRepository.GetAsync(x => x.Id == updateUserExamItemDto.Id);
|
|
|
+
|
|
|
+ userExamItem = _mapper.Map<UpdateUserExamItemDto, UserExamItem>(updateUserExamItemDto, userExamItem);
|
|
|
+
|
|
|
+ userExamItem.ToUpdate();
|
|
|
+
|
|
|
+ await _userExamItemRepository.UpdateWithValidateAsync(userExamItem, cancellationToken);
|
|
|
+
|
|
|
+ if (updateUserExamItemDto.QuestionType.CheckSelectType())
|
|
|
+ {
|
|
|
+ if (updateUserExamItemDto.UserExamItemOptionDtos != null)
|
|
|
+ {
|
|
|
+ updateUserExamItemDto.UserExamItemOptionDtos.ForEach(x => x.UserExamItemId = updateUserExamItemDto.Id);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private async Task AddUserExamItemOptions(AddUserExamItemDto addUserExamItemDto, CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+ var userExamItemOptions = new List<UserExamItemOptions>();
|
|
|
+
|
|
|
+ if (addUserExamItemDto.QuestionType.CheckSelectType())
|
|
|
+ {
|
|
|
+ if (addUserExamItemDto.UserExamItemOptionDtos != null)
|
|
|
+ {
|
|
|
+ addUserExamItemDto.UserExamItemOptionDtos.Where(x => x.OperationStatus == EEOperationStatus.Add).ToList().ForEach(x =>
|
|
|
+ {
|
|
|
+ userExamItemOptions.Add(_mapper.Map<UserExamItemOptions>(x));
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ userExamItemOptions.ToInsert();
|
|
|
+
|
|
|
+ await _userExamItemOptionRepository.AddWithValidateAsync(userExamItemOptions, cancellationToken);
|
|
|
+ }
|
|
|
+
|
|
|
+ private async Task AddUserExamItem(AddUserExamItemDto addUserExamItemDto, CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+ var userExamItem = _mapper.Map<UserExamItem>(addUserExamItemDto);
|
|
|
+
|
|
|
+ userExamItem.ToInsert();
|
|
|
+
|
|
|
+ var id = await _userExamItemRepository.AddWithValidateAsync(userExamItem, cancellationToken);
|
|
|
+
|
|
|
+ if (addUserExamItemDto.QuestionType.CheckSelectType())
|
|
|
+ {
|
|
|
+ if (addUserExamItemDto.UserExamItemOptionDtos != null)
|
|
|
+ {
|
|
|
+ addUserExamItemDto.UserExamItemOptionDtos.ForEach(x => x.UserExamItemId = id);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private async Task DeleteUserExamItemOptions(EntityQueryRequest entityQueryRequest, CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+ await _userExamItemOptionRepository.DeleteWithValidateAsync(entityQueryRequest, cancellationToken);
|
|
|
+ }
|
|
|
+
|
|
|
+ private SqlSugar.ISugarQueryable<UserExamResultViewResponse> GetQueryable(UserExamPagedRequest queryRequest)
|
|
|
+ {
|
|
|
+ if (_sessionContext.UserId != null)
|
|
|
+ {
|
|
|
+ queryRequest.UserId = _sessionContext.UserId;
|
|
|
+ }
|
|
|
+ var expression = queryRequest.GetExpression();
|
|
|
+ var userExamTable = _repository.Queryable().Where(expression);
|
|
|
+
|
|
|
+ var examManageExpression = queryRequest.GetExamManageExpression();
|
|
|
+ var examManageTable = new ExamRepository<ExamManage>(_repository.UOW, _dataPermissionFilterBuilder, _serviceProvider).Queryable().Where(examManageExpression);
|
|
|
+
|
|
|
+ var queryable = userExamTable.InnerJoin(examManageTable, (u, e) => u.ExamId == e.Id).Select((u, e) => new UserExamResultViewResponse
|
|
|
+ {
|
|
|
+ Id = u.Id,
|
|
|
+ CutoffScore = e.CutoffScore,
|
|
|
+ TotalScore = e.TotalScore,
|
|
|
+ ExamName = e.Name,
|
|
|
+ Score = u.Score ?? 0,
|
|
|
+ Status = u.Status,
|
|
|
+ SortIndex = u.SortIndex,
|
|
|
+ ExamStatus = u.ExamStatus,
|
|
|
+ IsSuccess = u.IsSuccess
|
|
|
+ });
|
|
|
+ return queryable;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ }
|
|
|
}
|