|
@@ -0,0 +1,427 @@
|
|
|
+using Exam.Application.Interface.Exam;
|
|
|
+using Exam.ExamManages;
|
|
|
+using Exam.Infrastructure.Data.Entity;
|
|
|
+using Exam.Infrastructure.Data.Extensions;
|
|
|
+using Exam.Infrastructure.Enums;
|
|
|
+using Exam.Infrastructure.Extensions;
|
|
|
+using Exam.Infrastructure.Web.Extensions;
|
|
|
+using Exam.Infrastructure.Web.Utilities;
|
|
|
+using Exam.Insfrastructure.Service.Service;
|
|
|
+using Exam.Questions;
|
|
|
+using Exam.Repository.Sqlsugar.Repositories;
|
|
|
+using Exam.Share.ViewResponses.Exam;
|
|
|
+using Hotline.Application.Exam.QueryExtensions.ExamManages;
|
|
|
+using Hotline.Repository.SqlSugar;
|
|
|
+using Hotline.Repository.SqlSugar.DataPermissions;
|
|
|
+using Hotline.Repository.SqlSugar.Exam.Interfaces.ExamManages;
|
|
|
+using Hotline.Repository.SqlSugar.Exam.Interfaces.Questions;
|
|
|
+using Hotline.Repository.SqlSugar.Extensions;
|
|
|
+using Hotline.Share.Dtos.TestPapers;
|
|
|
+using Hotline.Share.Requests.Exam;
|
|
|
+using Hotline.Share.Tools;
|
|
|
+using Hotline.Share.ViewResponses.Exam;
|
|
|
+using JiebaNet.Segmenter.Common;
|
|
|
+using MapsterMapper;
|
|
|
+using SqlSugar;
|
|
|
+using XF.Domain.Dependency;
|
|
|
+
|
|
|
+namespace Hotline.Application.Exam.Service.ExamManages
|
|
|
+{
|
|
|
+ public class ExtractRuleService : ApiService<ExtractRule, ExtractRuleDto, HotlineDbContext>,IExtractRuleService, IScopeDependency
|
|
|
+ {
|
|
|
+ private readonly IExtractRuleRepository _repository;
|
|
|
+ private readonly ITagQuestionRepository _tagQuestionRepository;
|
|
|
+ private readonly IRuleTagRepository _ruleTagRepository;
|
|
|
+ private readonly IDataPermissionFilterBuilder _dataPermissionFilterBuilder;
|
|
|
+ private readonly IServiceProvider _serviceProvider;
|
|
|
+ private readonly IMapper _mapper;
|
|
|
+ public ExtractRuleService(IExtractRuleRepository repository,
|
|
|
+ ITagQuestionRepository tagQuestionRepository,
|
|
|
+ IRuleTagRepository ruleTagRepository,
|
|
|
+ IDataPermissionFilterBuilder dataPermissionFilterBuilder, IServiceProvider serviceProvider,
|
|
|
+ IMapper mapper) : base(repository, mapper)
|
|
|
+ {
|
|
|
+ _repository = repository;
|
|
|
+ _tagQuestionRepository = tagQuestionRepository;
|
|
|
+ _ruleTagRepository = ruleTagRepository;
|
|
|
+ _dataPermissionFilterBuilder = dataPermissionFilterBuilder;
|
|
|
+ _serviceProvider = serviceProvider;
|
|
|
+ _mapper = mapper;
|
|
|
+ }
|
|
|
+
|
|
|
+ #region public method
|
|
|
+ /// <summary>
|
|
|
+ ///
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="entityQueryRequest"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public async Task<ExtractRuleDto> GetAsync(EntityQueryRequest entityQueryRequest)
|
|
|
+ {
|
|
|
+ var entity = await _repository.GetAsync(entityQueryRequest.Id);
|
|
|
+
|
|
|
+ var extractRuleDto = _mapper.Map<ExtractRuleDto>(entity);
|
|
|
+
|
|
|
+ if (extractRuleDto != null)
|
|
|
+ {
|
|
|
+ extractRuleDto.RuleTagDtos = await GetRuleTagDtos(entityQueryRequest);
|
|
|
+
|
|
|
+ extractRuleDto.TagQuestionDtos = await GetTagQuestions(entityQueryRequest);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return extractRuleDto;
|
|
|
+ }
|
|
|
+
|
|
|
+ public async Task<(int, List<ExtractRuleViewResponse>)> GetListAsync(ExtractRulePagedRequest queryRequest)
|
|
|
+ {
|
|
|
+ ISugarQueryable<ExtractRuleViewResponse> queryable = QueryResult(queryRequest);
|
|
|
+
|
|
|
+ var result = await queryable.ToListAsync();
|
|
|
+
|
|
|
+ var total = await queryable.CountAsync();
|
|
|
+
|
|
|
+ return (total, result);
|
|
|
+ }
|
|
|
+
|
|
|
+ public async Task<PageViewResponse<ExtractRuleViewResponse>> GetPagedListAsync(ExtractRulePagedRequest queryRequest)
|
|
|
+ {
|
|
|
+ ISugarQueryable<ExtractRuleViewResponse> queryable = QueryResult(queryRequest);
|
|
|
+
|
|
|
+ var list = await queryable.ToPageListAsync(queryRequest.PageIndex, queryRequest.PageSize);
|
|
|
+ var total = await queryable.CountAsync();
|
|
|
+
|
|
|
+ var result = new ExtractRulePageViewResponse
|
|
|
+ {
|
|
|
+ Items = list,
|
|
|
+ Pagination = new Pagination(queryRequest.PageIndex, queryRequest.PageSize, total)
|
|
|
+ };
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 新增抽题规则
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="actionRequest"></param>
|
|
|
+ /// <param name="cancellationToken"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public override async Task AddAsync(ExtractRuleDto actionRequest, CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+ await base.AddAsync(actionRequest, cancellationToken);
|
|
|
+
|
|
|
+ await AddRuleTags(actionRequest,cancellationToken);
|
|
|
+
|
|
|
+ await AddTagQuestions(actionRequest, cancellationToken);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 修改抽题规则
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="actionRequest"></param>
|
|
|
+ /// <param name="cancellationToken"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public override async Task UpdateAsync(ExtractRuleDto actionRequest, CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+ await base.UpdateAsync(actionRequest, cancellationToken);
|
|
|
+
|
|
|
+ await ModifyRuleTags(actionRequest, cancellationToken);
|
|
|
+
|
|
|
+ await ModifyTagQuestions(actionRequest, cancellationToken);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 删除抽题规则
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="entityQueryRequest"></param>
|
|
|
+ /// <param name="cancellationToken"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public override async Task DeleteAsync(EntityQueryRequest entityQueryRequest, CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+ await base.DeleteAsync(entityQueryRequest, cancellationToken);
|
|
|
+
|
|
|
+ var tempEntityQueryRequest = ExpressionableUtility.CreateExpression<TagQuestion>()
|
|
|
+ .AndIF(entityQueryRequest.Id.IsNotNullOrEmpty(), x => x.RuleId == entityQueryRequest.Id)
|
|
|
+ .AndIF(entityQueryRequest.Ids.IsNotNull(), x => entityQueryRequest.Ids.Contains(x.RuleId)).ToEntityQueryRequest<TagQuestion>();
|
|
|
+
|
|
|
+ await DeleteTagQuestions(tempEntityQueryRequest, cancellationToken);
|
|
|
+
|
|
|
+ tempEntityQueryRequest = ExpressionableUtility.CreateExpression<RuleTag>()
|
|
|
+ .AndIF(entityQueryRequest.Id.IsNotNullOrEmpty(), x => x.RuleId == entityQueryRequest.Id)
|
|
|
+ .AndIF(entityQueryRequest.Ids.IsNotNull(), x => entityQueryRequest.Ids.Contains(x.RuleId)).ToEntityQueryRequest<RuleTag>();
|
|
|
+
|
|
|
+
|
|
|
+ await DeleteRuleTags(tempEntityQueryRequest, cancellationToken);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 获取标题试题数
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="tagQuestionRequest"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public async Task<List<TagQuestionViewResponse>> GetTagQuestionCount(TagQuestionRequest tagQuestionRequest)
|
|
|
+ {
|
|
|
+ if (tagQuestionRequest.TagIds.IsNullOrEmpty()) return new List<TagQuestionViewResponse>();
|
|
|
+
|
|
|
+ var expression = tagQuestionRequest.GetExpression();
|
|
|
+ var questionTagTable = new ExamRepository<QuestionTag>(_repository.UOW, _dataPermissionFilterBuilder, _serviceProvider).Queryable().Where(expression);
|
|
|
+ var questionTable = new ExamRepository<Question>(_repository.UOW, _dataPermissionFilterBuilder, _serviceProvider).Queryable();
|
|
|
+
|
|
|
+ var queryable = questionTagTable.LeftJoin(questionTable, (t, q) => t.QuestionId == q.Id)
|
|
|
+ .GroupBy((t,q)=> new { t.TagId, q.QuestionType }).Select((t, q) => new TagQuestionViewResponse
|
|
|
+ {
|
|
|
+ TagId = t.TagId,
|
|
|
+ QuestionType = q.QuestionType,
|
|
|
+ TotalCount = SqlFunc.AggregateCount(new
|
|
|
+ {
|
|
|
+ TagId = t.TagId,
|
|
|
+ QuestionType = q.QuestionType,
|
|
|
+ })
|
|
|
+ });
|
|
|
+
|
|
|
+ return await queryable.ToListAsync();
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ #region private method
|
|
|
+ /// <summary>
|
|
|
+ /// 获取查询结果
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="queryRequest"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ private ISugarQueryable<ExtractRuleViewResponse> QueryResult(ExtractRulePagedRequest queryRequest)
|
|
|
+ {
|
|
|
+ var expression = queryRequest.GetExpression();
|
|
|
+ var query = _repository.Queryable().Where(expression);
|
|
|
+ var querable = query.OrderBy(o => o.SortIndex).Select(m => new ExtractRuleViewResponse
|
|
|
+ {
|
|
|
+ Name = m.Name,
|
|
|
+ Id = m.Id,
|
|
|
+ Status = m.Status,
|
|
|
+ Code = m.Code,
|
|
|
+ SortIndex = m.SortIndex,
|
|
|
+ Remark = m.Remark
|
|
|
+ });
|
|
|
+ return querable;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 新增标签试题
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="actionRequest"></param>
|
|
|
+ /// <param name="cancellationToken"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ private async Task AddTagQuestions(ExtractRuleDto actionRequest, CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+ if (actionRequest.TagQuestionDtos == null) return;
|
|
|
+
|
|
|
+ var tagQuestionDtos = actionRequest.TagQuestionDtos.Where(x => x.OperationStatus == OperationStatus.Add).ToList();
|
|
|
+
|
|
|
+ var tagQuestions = _mapper.Map<List<TagQuestion>>(tagQuestionDtos);
|
|
|
+
|
|
|
+ tagQuestionDtos.InitRequest(actionRequest);
|
|
|
+
|
|
|
+ tagQuestions.ForEach(x => x.RuleId = actionRequest.Id);
|
|
|
+
|
|
|
+ tagQuestions.ToInsert(tagQuestionDtos);
|
|
|
+
|
|
|
+ await _tagQuestionRepository.AddWithValidateAsync(tagQuestions, cancellationToken);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 新增规则标签
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="actionRequest"></param>
|
|
|
+ /// <param name="cancellationToken"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ private async Task AddRuleTags(ExtractRuleDto actionRequest, CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+ if (actionRequest.RuleTagDtos == null) return;
|
|
|
+
|
|
|
+ var ruleTagDtos = actionRequest.RuleTagDtos.Where(x => x.OperationStatus == OperationStatus.Add).ToList();
|
|
|
+
|
|
|
+ var ruleTags = _mapper.Map<List<RuleTag>>(ruleTagDtos);
|
|
|
+
|
|
|
+ ruleTagDtos.InitRequest(actionRequest);
|
|
|
+
|
|
|
+ ruleTags.ForEach(x => x.RuleId = actionRequest.Id);
|
|
|
+
|
|
|
+ ruleTags.ToInsert(ruleTagDtos);
|
|
|
+
|
|
|
+ await _ruleTagRepository.AddWithValidateAsync(ruleTags, cancellationToken);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 修改标签试题数
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="actionRequest"></param>
|
|
|
+ /// <param name="cancellationToken"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ /// <exception cref="NotImplementedException"></exception>
|
|
|
+ private async Task ModifyTagQuestions(ExtractRuleDto actionRequest, CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+ if (actionRequest.TagQuestionDtos == null) return;
|
|
|
+
|
|
|
+ await AddTagQuestions(actionRequest, cancellationToken);
|
|
|
+
|
|
|
+ await UpdateTagQuestions(actionRequest ,cancellationToken);
|
|
|
+
|
|
|
+ var ruleTagDtos = actionRequest.TagQuestionDtos.Where(x => x.OperationStatus == OperationStatus.Delete).ToList();
|
|
|
+ var ids = ruleTagDtos.Select(x => x.Id).ToList();
|
|
|
+ var entityQueyRequest = ExpressionableUtility.CreateExpression<TagQuestion>()
|
|
|
+ .AndIF(ids.IsNotNull(), x => ids.Contains(x.Id)).ToEntityQueryRequest<TagQuestion>();
|
|
|
+
|
|
|
+ await DeleteTagQuestions(entityQueyRequest, cancellationToken);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 修改标签试题数
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="actionRequest"></param>
|
|
|
+ /// <param name="cancellationToken"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ /// <exception cref="NotImplementedException"></exception>
|
|
|
+ private async Task UpdateTagQuestions(ExtractRuleDto actionRequest, CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+ if (actionRequest.TagQuestionDtos == null) return;
|
|
|
+
|
|
|
+ var tagQuestionDtos = actionRequest.TagQuestionDtos.Where(x => x.OperationStatus == OperationStatus.Update).ToList();
|
|
|
+ var ids = tagQuestionDtos.Select(x => x.Id).ToList();
|
|
|
+ var tagQuestions = await _tagQuestionRepository.Queryable().Where(x => ids.Contains(x.Id)).ToListAsync();
|
|
|
+
|
|
|
+ var entitys = new List<TagQuestion>();
|
|
|
+ tagQuestionDtos.ForEach(x =>
|
|
|
+ {
|
|
|
+ var entity = tagQuestions.FirstOrDefault(x => x.Id == x.Id);
|
|
|
+ entity = _mapper.Map(x, entity);
|
|
|
+ entity.RuleId = actionRequest.Id;
|
|
|
+ entitys.Add(entity);
|
|
|
+ });
|
|
|
+ tagQuestionDtos.InitRequest(actionRequest);
|
|
|
+
|
|
|
+ entitys.ToUpdate(tagQuestionDtos);
|
|
|
+
|
|
|
+ await _tagQuestionRepository.UpdateWithValidateAsync(entitys, cancellationToken);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 修改规则标签
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="actionRequest"></param>
|
|
|
+ /// <param name="cancellationToken"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ /// <exception cref="NotImplementedException"></exception>
|
|
|
+ private async Task ModifyRuleTags(ExtractRuleDto actionRequest, CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+ if (actionRequest.RuleTagDtos == null) return;
|
|
|
+
|
|
|
+ await AddRuleTags(actionRequest, cancellationToken);
|
|
|
+
|
|
|
+ await UpdateRuleTags(actionRequest, cancellationToken);
|
|
|
+
|
|
|
+ var ruleTagDtos = actionRequest.RuleTagDtos.Where(x => x.OperationStatus == OperationStatus.Delete).ToList();
|
|
|
+ var ids = ruleTagDtos.Select(x => x.Id).ToList();
|
|
|
+ var entityQueyRequest = ExpressionableUtility.CreateExpression<RuleTag>()
|
|
|
+ .AndIF(ids.IsNotNull(), x => ids.Contains(x.Id)).ToEntityQueryRequest<RuleTag>();
|
|
|
+
|
|
|
+ await DeleteRuleTags(entityQueyRequest, cancellationToken);
|
|
|
+ }
|
|
|
+
|
|
|
+ private async Task UpdateRuleTags(ExtractRuleDto actionRequest, CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+ if (actionRequest.RuleTagDtos == null) return;
|
|
|
+
|
|
|
+ var ruleTagDtos = actionRequest.RuleTagDtos.Where(x => x.OperationStatus == OperationStatus.Update).ToList();
|
|
|
+ var ids = ruleTagDtos.Select(x => x.Id).ToList();
|
|
|
+ var ruleTags = await _ruleTagRepository.Queryable().Where(x=>ids.Contains(x.Id)).ToListAsync();
|
|
|
+
|
|
|
+ var entitys = new List<RuleTag>();
|
|
|
+ ruleTagDtos.ForEach(x =>
|
|
|
+ {
|
|
|
+ var entity = ruleTags.FirstOrDefault(x=>x.Id == x.Id);
|
|
|
+ entity = _mapper.Map(x, entity);
|
|
|
+ entity.RuleId = actionRequest.Id;
|
|
|
+ entitys.Add(entity);
|
|
|
+ });
|
|
|
+ ruleTagDtos.InitRequest(actionRequest);
|
|
|
+
|
|
|
+ entitys.ToUpdate(ruleTagDtos);
|
|
|
+
|
|
|
+ await _ruleTagRepository.UpdateWithValidateAsync(entitys,cancellationToken);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 删除规则标签
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="tempEntityQueryRequest"></param>
|
|
|
+ /// <param name="cancellationToken"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ private async Task DeleteRuleTags(EntityQueryRequest tempEntityQueryRequest, CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+ await _ruleTagRepository.DeleteWithValidateAsync(tempEntityQueryRequest, cancellationToken);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 删除标签试题数
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="tempEntityQueryRequest"></param>
|
|
|
+ /// <param name="cancellationToken"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ private async Task DeleteTagQuestions(EntityQueryRequest tempEntityQueryRequest, CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+ await _tagQuestionRepository.DeleteWithValidateAsync(tempEntityQueryRequest, cancellationToken);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 获取规则标签
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="entityQueryRequest"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ /// <exception cref="NotImplementedException"></exception>
|
|
|
+ private async Task<List<RuleTagDto>> GetRuleTagDtos(EntityQueryRequest entityQueryRequest)
|
|
|
+ {
|
|
|
+ var ruleTagTable = _ruleTagRepository.Queryable().Where(x=>x.RuleId == entityQueryRequest.Id);
|
|
|
+
|
|
|
+ var examTagTable = new ExamRepository<ExamTag>(_repository.UOW, _dataPermissionFilterBuilder, _serviceProvider).Queryable();
|
|
|
+
|
|
|
+
|
|
|
+ var queryable = ruleTagTable.LeftJoin(examTagTable, (r, e) => r.TagId == e.Id).Select((r, e) => new RuleTagDto
|
|
|
+ {
|
|
|
+ Id = r.Id,
|
|
|
+ TagId = r.TagId,
|
|
|
+ Tag = e.Name
|
|
|
+ });
|
|
|
+
|
|
|
+ return await queryable.ToListAsync();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 获取标签试题数
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="entityQueryRequest"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ /// <exception cref="NotImplementedException"></exception>
|
|
|
+ private async Task<List<TagQuestionDto>> GetTagQuestions(EntityQueryRequest entityQueryRequest)
|
|
|
+ {
|
|
|
+ var tagQuestionTable = _tagQuestionRepository.Queryable().Where(x => x.RuleId == entityQueryRequest.Id);
|
|
|
+
|
|
|
+ var examTagTable = new ExamRepository<ExamTag>(_repository.UOW, _dataPermissionFilterBuilder, _serviceProvider).Queryable();
|
|
|
+
|
|
|
+
|
|
|
+ var tagQuestionDtos = tagQuestionTable.LeftJoin(examTagTable, (t, e) => t.TagId == e.Id).Select((t,e)=>new TagQuestionDto
|
|
|
+ {
|
|
|
+ Id = t.Id,
|
|
|
+ TagId =t.TagId,
|
|
|
+ QuestionType = t.QuestionType,
|
|
|
+ Count = t.Count,
|
|
|
+ Tag = e.Name
|
|
|
+ });
|
|
|
+
|
|
|
+ return await tagQuestionDtos.ToListAsync();
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
+ }
|
|
|
+}
|