Browse Source

增加试题数量获取接口

guqiang 1 tháng trước cách đây
mục cha
commit
6b338cf587

+ 12 - 1
src/Hotline.Api/Controllers/Exam/TestPaperController.cs

@@ -85,7 +85,7 @@ namespace Hotline.Api.Controllers.Exam
         }
 
         /// <summary>
-        /// 获取课件分页列表
+        /// 获取试卷试题数
         /// </summary>
         /// <returns></returns>
         [HttpGet(TestPaperApiRoute.Count)]
@@ -106,5 +106,16 @@ namespace Hotline.Api.Controllers.Exam
         {
             await _testPaperService.GetQuestionDtos(testPaperQuestionRequest);
         }
+
+        /// <summary>
+        /// 获取标签试题数
+        /// </summary>
+        /// <param name="tagQuestionCountRequest"></param>
+        /// <returns></returns>
+        [HttpGet(TestPaperApiRoute.GetTagQuestionCount)]
+        public async Task<TagQuestionCountViewResponse> GetTagQuestionCount([FromQuery] TagQuestionCountRequest tagQuestionCountRequest)
+        {
+            return await _testPaperService.GetTagQuestionCount(tagQuestionCountRequest);
+        }
     }
 }

+ 3 - 0
src/Hotline.Application/Exam/Constants/ApiRoutes/TestPaperApiRoute.cs

@@ -1,4 +1,5 @@
 using Exam.Infrastructure.Web.Constants;
+using Quartz.Impl.Triggers;
 
 namespace Hotline.Application.Exam.Constants.ApiRoutes
 {
@@ -7,5 +8,7 @@ namespace Hotline.Application.Exam.Constants.ApiRoutes
         public const string Count = "GetTestPaperQuestionCount";
 
         public const string GetQuestions = "GetQuestions";
+
+        public const string GetTagQuestionCount = "GetTagQuestionCount";
     }
 }

+ 12 - 0
src/Hotline.Application/Exam/Interface/TestPapers/ITestPaperService.cs

@@ -12,10 +12,22 @@ namespace Exam.Application.Interface.TestPapers
 {
     public interface ITestPaperService:IQueryService<TestPaperViewResponse,TestPaperDto,TestPaperPagedRequest>,IApiService<AddTestPaperDto,UpdateTestPaperDto,TestPaper>
     {
+        /// <summary>
+        /// 获取试卷试题数量
+        /// </summary>
+        /// <param name="testPaperQuestionCountRequest"></param>
+        /// <returns></returns>
         public Task<List<TestPaperQuestionCountViewResponse>> GetTestPaperQuestionCount(TestPaperQuestionCountRequest testPaperQuestionCountRequest);
         
         public Task GenerateTestPaper(GenerateTestPaperRequest generateTestPaperRequest,CancellationToken cancellationToken);
 
+        /// <summary>
+        /// 根据标签获取试题
+        /// </summary>
+        /// <param name="testPaperQuestionRequest"></param>
+        /// <returns></returns>
         public Task<List<QuestionDto>> GetQuestionDtos(TestPaperQuestionRequest testPaperQuestionRequest);
+
+        public Task<TagQuestionCountViewResponse> GetTagQuestionCount(TagQuestionCountRequest tagQuestionCountRequest);
     }
 }

+ 25 - 1
src/Hotline.Application/Exam/QueryExtensions/TestPapers/TestPaperQueryExtensions.cs

@@ -1,6 +1,8 @@
-using Exam.Infrastructure.Data.Entity;
+using Exam.ExamManages;
+using Exam.Infrastructure.Data.Entity;
 using Exam.Infrastructure.Extensions;
 using Exam.Infrastructure.Web.Utilities;
+using Exam.Questions;
 using Exam.TestPapers;
 using Hotline.Share.Requests.TestPaper;
 using JiebaNet.Segmenter.Common;
@@ -48,6 +50,17 @@ namespace Hotline.Application.Exam.QueryExtensions.TestPapers
             return expression;
         }
 
+        public static Expression<Func<ExtractRule, bool>> GetExpression(this TestPaperQuestionCountRequest testPaperQuestionCountRequest)
+        {
+            Expression<Func<ExtractRule, bool>> expression = m => m.Id != null;
+
+            expression = ExpressionableUtility.CreateExpression<ExtractRule>()
+               .AndIF(testPaperQuestionCountRequest.TestPaperId.IsNotNullOrEmpty(), x => x.Id == testPaperQuestionCountRequest.ExtractRuleId)
+               .ToExpression();
+
+            return expression;
+        }
+
         public static Expression<Func<TestPaperRule,bool>> GetTestPaperRuleExpression(this TestPaperQuestionCountRequest testPaperQuestionCountRequest)
         {
             Expression<Func<TestPaperRule, bool>> expression = m => m.Id != null;
@@ -69,5 +82,16 @@ namespace Hotline.Application.Exam.QueryExtensions.TestPapers
 
             return expression;
         }
+
+        public static Expression<Func<QuestionTag,bool>> GetExpression(this TagQuestionCountRequest tagQuestionCountRequest)
+        {
+            Expression<Func<QuestionTag, bool>> expression = m => m.Id != null;
+
+            expression = ExpressionableUtility.CreateExpression<QuestionTag>()
+               .AndIF(tagQuestionCountRequest.TagId.IsNotNullOrEmpty(), x => x.TagId == tagQuestionCountRequest.TagId)
+               .ToExpression();
+
+            return expression;
+        }
     }
 }

+ 52 - 18
src/Hotline.Application/Exam/Service/TestPapers/TestPaperService.cs

@@ -259,21 +259,18 @@ namespace Hotline.Application.Exam.Service.TestPapers
         /// <exception cref="NotImplementedException"></exception>
         public async Task<List<TestPaperQuestionCountViewResponse>> GetTestPaperQuestionCount(TestPaperQuestionCountRequest testPaperQuestionCountRequest)
         {
-            var expression = testPaperQuestionCountRequest.GetTestPaperCountExpression();
-            var testPaper = await _repository.Queryable().Where(expression).FirstAsync();
-
             var testPaperQuestionCountViewResponses = new List<TestPaperQuestionCountViewResponse>();
-            if (testPaper != null)
+
+
+            if (testPaperQuestionCountRequest.TestPaperId.IsNotNullOrEmpty())
             {
-                if(testPaper.Mode == Share.Enums.Exams.EExamMode.Random)
-                {
-                    testPaperQuestionCountViewResponses = await CalcuteRandomQuestionCount(testPaperQuestionCountRequest);
-                }
-                else if(testPaper.Mode == Share.Enums.Exams.EExamMode.Manual)
-                {
-                    testPaperQuestionCountViewResponses = await CalcuteManualQuestionCount(testPaperQuestionCountRequest);
-                }
+                testPaperQuestionCountViewResponses = await CalcuteManualQuestionCount(testPaperQuestionCountRequest);
+            }
+            else
+            {
+                testPaperQuestionCountViewResponses = await CalcuteRandomQuestionCount(testPaperQuestionCountRequest);
             }
+             
             return testPaperQuestionCountViewResponses;
         }
 
@@ -331,10 +328,11 @@ namespace Hotline.Application.Exam.Service.TestPapers
             var questionTagTable = questionTagRepository.Queryable();
             var questionTable = questionRepository.Queryable();
 
-            var questions = await questionTable.InnerJoin(questionTagTable, (q,qt)=>q.Id == qt.QuestionId)
-                .Where((q, qt) => q.DifficultyLevel == testPaperQuestionRequest.DifficultyLevel 
-            && q.QuestionType == testPaperQuestionRequest.QuestionType && testPaperQuestionRequest.TagIds.Contains(qt.TagId)
-            )
+            var questions = await questionTable.InnerJoin(questionTagTable, (q, qt) => q.Id == qt.QuestionId)
+                .Where((q, qt) => q.DifficultyLevel == testPaperQuestionRequest.DifficultyLevel
+            && q.QuestionType == testPaperQuestionRequest.QuestionType
+            ).WhereIF(testPaperQuestionRequest.DifficultyLevel.IsNotNull(), (q, qt) => q.DifficultyLevel == testPaperQuestionRequest.DifficultyLevel)
+            .WhereIF(testPaperQuestionRequest.TagIds.IsNotNull(), (q, qt) => testPaperQuestionRequest.TagIds.Contains(qt.TagId))
                 .Select((q, qt) => q).Take(testPaperQuestionRequest.Count).OrderBy(SqlFunc.GetRandom()).ToListAsync();
 
             var questionDtos = new List<QuestionDto>();
@@ -369,6 +367,31 @@ namespace Hotline.Application.Exam.Service.TestPapers
         }
 
 
+        /// <summary>
+        /// 获取标签试题数
+        /// </summary>
+        /// <param name="tagQuestionCountRequest"></param>
+        /// <returns></returns>
+        /// <exception cref="NotImplementedException"></exception>
+        public async Task<TagQuestionCountViewResponse> GetTagQuestionCount(TagQuestionCountRequest tagQuestionCountRequest)
+        {
+            var expression = tagQuestionCountRequest.GetExpression();
+
+            var tagQuestionRepository = new ExamRepository<QuestionTag>(_repository.UOW, _dataPermissionFilterBuilder, _serviceProvider);
+
+            var tagQuestionTable = tagQuestionRepository.Queryable().Where(expression);
+
+            var taqQuestions = await tagQuestionTable.ToListAsync();
+
+            var result = taqQuestions.GroupBy(x => x.TagId).Select(m => new TagQuestionCountViewResponse
+            {
+                TagId = m.Key,
+                TotalCount = m.Count()
+            });
+
+            return result.FirstOrDefault();
+        }
+
         #endregion
 
         #region private method
@@ -777,10 +800,21 @@ namespace Hotline.Application.Exam.Service.TestPapers
         private async Task<List<TestPaperQuestionCountViewResponse>> CalcuteRandomQuestionCount(TestPaperQuestionCountRequest testPaperQuestionCountRequest)
         {
             var testPaperQuestionCountViewResponses = new List<TestPaperQuestionCountViewResponse>();
+            var extractRuleRepository = new ExamRepository<ExtractRule>(_repository.UOW, _dataPermissionFilterBuilder, _serviceProvider);
+            var ruleTagRepository = new ExamRepository<RuleTag>(_repository.UOW, _dataPermissionFilterBuilder, _serviceProvider);
+            var tagQuestionRepository = new ExamRepository<TagQuestion>(_repository.UOW, _dataPermissionFilterBuilder, _serviceProvider);
+
+
+            var expression = testPaperQuestionCountRequest.GetExpression();
+            var exatractTable = extractRuleRepository.Queryable().Where(expression);
+            var ruleTagTable = ruleTagRepository.Queryable();
+            var tagQuestionTable = tagQuestionRepository.Queryable();
 
-            var expression = testPaperQuestionCountRequest.GetTestPaperRuleExpression();
 
-            var testPaperRules = await _testPaperRuleRepository.Queryable().Where(expression).ToListAsync();
+            var testPaperRules = await exatractTable.InnerJoin(ruleTagTable,(e,rt)=>e.Id == rt.RuleId)
+                .InnerJoin(tagQuestionTable,(e,rt,tq)=>rt.TagId == tq.TagId)
+                .Select((e,rt,tq)=>tq)
+                .ToListAsync();
 
             if (testPaperRules != null)
             {

+ 9 - 0
src/Hotline.Share/Requests/TestPaper/TagQuestionCountRequest.cs

@@ -0,0 +1,9 @@
+using Exam.Infrastructure.Data.Interface;
+
+namespace Hotline.Share.Requests.TestPaper
+{
+    public class TagQuestionCountRequest : IQueryRequest
+    {
+        public string TagId { get; set; }
+    }
+}

+ 6 - 0
src/Hotline.Share/Requests/TestPaper/TestPaperQuestionCountRequest.cs

@@ -10,5 +10,11 @@ namespace Hotline.Share.Requests.TestPaper
         /// </summary>
         [Description("试卷Id")]
         public string TestPaperId { get; set; }
+
+        /// <summary>
+        /// 抽题规则Id
+        /// </summary>
+        [Description("抽题规则Id")]
+        public string ExtractRuleId { get; set; }
     }
 }

+ 1 - 1
src/Hotline.Share/Requests/TestPaper/TestPaperQuestionRequest.cs

@@ -28,7 +28,7 @@ namespace Hotline.Share.Requests.TestPaper
         /// 难度
         /// </summary>
         [Description("难度")]
-        public EDifficultyLevel DifficultyLevel { get; set; }
+        public EDifficultyLevel? DifficultyLevel { get; set; }
 
     }
 }

+ 11 - 7
src/Hotline.Share/ViewResponses/Exam/TagQuestionViewResponse.cs

@@ -4,19 +4,22 @@ using System.ComponentModel;
 
 namespace Hotline.Share.ViewResponses.Exam
 {
-    public class TagQuestionViewResponse : IViewResponse
+    public class TagQuestionViewResponse : TagQuestionCountViewResponse
     {
-        /// <summary>
-        /// 标签Id
-        /// </summary>
-        [Description("标签Id")]
-        public string TagId { get; set; }
-
         /// <summary>
         /// 试题类型
         /// </summary>
         [Description("试题类型")]
         public EQuestionType QuestionType { get; set; }
+    }
+
+    public class TagQuestionCountViewResponse : IViewResponse
+    {
+        /// <summary>
+        /// 标签Id
+        /// </summary>
+        [Description("标签Id")]
+        public string TagId { get; set; }
 
         /// <summary>
         /// 总数
@@ -24,6 +27,7 @@ namespace Hotline.Share.ViewResponses.Exam
         [Description("总数")]
         public int TotalCount { get; set; }
 
+
         /// <summary>
         /// 主键
         /// </summary>