Selaa lähdekoodia

阅卷相关API

zhouj1203@hotmail.com 4 vuotta sitten
vanhempi
commit
9d4c8bc879

+ 37 - 0
TEAMModelFunction/TriggerCorrect.cs

@@ -3,6 +3,8 @@ using Azure.Messaging.ServiceBus;
 using Microsoft.Azure.Documents;
 using Microsoft.Azure.Documents;
 using System;
 using System;
 using System.Collections.Generic;
 using System.Collections.Generic;
+using System.Text.Json;
+using System.Threading.Tasks;
 using TEAMModelOS.SDK.DI;
 using TEAMModelOS.SDK.DI;
 using TEAMModelOS.SDK.Extension;
 using TEAMModelOS.SDK.Extension;
 using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
 using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
@@ -102,6 +104,41 @@ namespace TEAMModelFunction
                                 string ecode = correct.scode;
                                 string ecode = correct.scode;
                                 //评测科目
                                 //评测科目
                                 string subjectId = sub.id;
                                 string subjectId = sub.id;
+                                //生成临时作答数据存放到redis
+                                var redisClient = _azureRedis.GetRedisClient(8);
+                                ExamInfo info = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ExamInfo>(eid, new Azure.Cosmos.PartitionKey(ecode));
+                                List<ExamClassResult> classResults = new List<ExamClassResult>();
+
+                                if (info.scope.Equals("school"))
+                                {
+
+                                    await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryIterator<ExamClassResult>(
+                                    queryText: $"select value(c) from c where c.examId = '{eid}' and c.subjectId = '{subjectId}'",
+                                    requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"ExamClassResult-{info.school}") }))
+                                    {
+                                        classResults.Add(item);
+                                    }
+                                }
+                                else
+                                {
+                                    await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryIterator<ExamClassResult>(
+                                        queryText: $"select value(c) from c where c.examId = '{eid}' and c.subjectId = '{subjectId}'",
+                                        requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"ExamClassResult-{info.creatorId}") }))
+                                    {
+                                        classResults.Add(item);
+                                    }
+                                }
+                                List<Task<bool>> tasks = new List<Task<bool>>();
+                                foreach (ExamClassResult examClass in classResults)
+                                {
+                                    foreach (string stuId in examClass.studentIds)
+                                    {
+                                        int index = examClass.studentIds.IndexOf(stuId);
+                                        tasks.Add(redisClient.HashSetAsync($"Exam:Scoring:{eid}-{subjectId}", stuId, new { ans = examClass.studentAnswers[index][0], score = examClass.studentScores[index] }.ToJsonString()));
+                                    }
+
+                                }
+                                await Task.WhenAll(tasks);
                             }
                             }
                         }
                         }
                         var messageCorrectEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = tdata.code }.ToJsonString());
                         var messageCorrectEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = tdata.code }.ToJsonString());

+ 174 - 9
TEAMModelOS/Controllers/Common/ExamController.cs

@@ -37,8 +37,10 @@ namespace TEAMModelOS.Controllers
         private readonly DingDing _dingDing;
         private readonly DingDing _dingDing;
         private readonly Option _option;
         private readonly Option _option;
         private readonly AzureStorageFactory _azureStorage;
         private readonly AzureStorageFactory _azureStorage;
+        private readonly AzureRedisFactory _azureRedis;
 
 
-        public ExamController(AzureCosmosFactory azureCosmos, AzureServiceBusFactory serviceBus, SnowflakeId snowflakeId, DingDing dingDing, IOptionsSnapshot<Option> option, AzureStorageFactory azureStorage)
+        public ExamController(AzureCosmosFactory azureCosmos, AzureServiceBusFactory serviceBus, SnowflakeId snowflakeId, DingDing dingDing, 
+            IOptionsSnapshot<Option> option, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis)
         {
         {
             _azureCosmos = azureCosmos;
             _azureCosmos = azureCosmos;
             _serviceBus = serviceBus;
             _serviceBus = serviceBus;
@@ -46,6 +48,7 @@ namespace TEAMModelOS.Controllers
             _dingDing = dingDing;
             _dingDing = dingDing;
             _option = option?.Value;
             _option = option?.Value;
             _azureStorage = azureStorage;
             _azureStorage = azureStorage;
+            _azureRedis = azureRedis;
         }
         }
 
 
         /// <summary>
         /// <summary>
@@ -340,7 +343,7 @@ namespace TEAMModelOS.Controllers
                 List<ExamInfo> examInfo = new List<ExamInfo>();
                 List<ExamInfo> examInfo = new List<ExamInfo>();
                 await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryText: query, continuationToken: token, requestOptions: new QueryRequestOptions() { MaxItemCount = topcout, PartitionKey = new PartitionKey($"Exam-{code}") }))
                 await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryText: query, continuationToken: token, requestOptions: new QueryRequestOptions() { MaxItemCount = topcout, PartitionKey = new PartitionKey($"Exam-{code}") }))
                 {
                 {
-                   
+
                     using var json = await JsonDocument.ParseAsync(item.ContentStream);
                     using var json = await JsonDocument.ParseAsync(item.ContentStream);
                     if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
                     if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
                     {
                     {
@@ -350,11 +353,12 @@ namespace TEAMModelOS.Controllers
                             examInfo.Add(obj.ToObject<ExamInfo>());
                             examInfo.Add(obj.ToObject<ExamInfo>());
                         }
                         }
                     }
                     }
-                    if (iscontinuation) {
+                    if (iscontinuation)
+                    {
                         continuationToken = item.GetContinuationToken();
                         continuationToken = item.GetContinuationToken();
                         break;
                         break;
                     }
                     }
-                    
+
                 }
                 }
                 //List<string> examIds = new List<string>();
                 //List<string> examIds = new List<string>();
                 /* List<ExamResult> examResults = new List<ExamResult>();
                 /* List<ExamResult> examResults = new List<ExamResult>();
@@ -772,7 +776,7 @@ namespace TEAMModelOS.Controllers
                 //if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
                 //if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
                 if (!request.TryGetProperty("point", out JsonElement point)) return BadRequest();
                 if (!request.TryGetProperty("point", out JsonElement point)) return BadRequest();
                 if (!request.TryGetProperty("studentId", out JsonElement studentId)) return BadRequest();
                 if (!request.TryGetProperty("studentId", out JsonElement studentId)) return BadRequest();
-                if (!request.TryGetProperty("classId", out JsonElement classId)) return BadRequest();
+                //if (!request.TryGetProperty("classId", out JsonElement classId)) return BadRequest();
                 //此参数表明此次操作对象为个人还是校本的评测内容
                 //此参数表明此次操作对象为个人还是校本的评测内容
                 if (!request.TryGetProperty("code", out JsonElement school)) return BadRequest();
                 if (!request.TryGetProperty("code", out JsonElement school)) return BadRequest();
                 if (!request.TryGetProperty("subjectId", out JsonElement subjectId)) return BadRequest();
                 if (!request.TryGetProperty("subjectId", out JsonElement subjectId)) return BadRequest();
@@ -781,12 +785,24 @@ namespace TEAMModelOS.Controllers
                 List<double> ans = point.ToObject<List<double>>();
                 List<double> ans = point.ToObject<List<double>>();
                 var client = _azureCosmos.GetCosmosClient();
                 var client = _azureCosmos.GetCosmosClient();
                 List<ExamClassResult> examClassResults = new List<ExamClassResult>();
                 List<ExamClassResult> examClassResults = new List<ExamClassResult>();
-                await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryIterator<ExamClassResult>(
-                    queryText: $"select value(c) from c where c.examId = '{id}' and c.subjectId = '{subjectId}' and c.info.id = '{classId}'",
-                    requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"ExamClassResult-{school}") }))
+                if (request.TryGetProperty("classId", out JsonElement classId))
                 {
                 {
-                    examClassResults.Add(item);
+                    await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryIterator<ExamClassResult>(
+                       queryText: $"select value(c) from c where c.examId = '{id}' and c.subjectId = '{subjectId}' and c.info.id = '{classId}'",
+                       requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"ExamClassResult-{school}") }))
+                    {
+                        examClassResults.Add(item);
+                    }
                 }
                 }
+                else {
+                    await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryIterator<ExamClassResult>(
+                           queryText: $"select value(c) from c where c.examId = '{id}' and c.subjectId = '{subjectId}' and array_contains(c.studentIds,'{studentId}')",
+                           requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"ExamClassResult-{school}") }))
+                    {
+                        examClassResults.Add(item);
+                    }
+                }
+               
                 ExamClassResult classResult = new ExamClassResult();
                 ExamClassResult classResult = new ExamClassResult();
                 //ExamInfo classResult = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ExamInfo>(id.ToString(), new PartitionKey($"{code}"));
                 //ExamInfo classResult = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ExamInfo>(id.ToString(), new PartitionKey($"{code}"));
                 //ExamClassResult classResult = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ExamClassResult>(id.ToString(), new PartitionKey($"{code}"));
                 //ExamClassResult classResult = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ExamClassResult>(id.ToString(), new PartitionKey($"{code}"));
@@ -1307,5 +1323,154 @@ namespace TEAMModelOS.Controllers
             }
             }
 
 
         }
         }
+
+
+        /*//查询任务列表
+        [ProducesDefaultResponseType]
+        //[AuthToken(Roles = "Student")]
+        [HttpPost("scoring")]
+        public async Task<IActionResult> Scoring(JsonElement requert)
+        {
+            //ResponseBuilder builder = ResponseBuilder.custom();
+            //var (id, school) = HttpContext.GetAuthTokenInfo();
+            try
+            {
+                if (!requert.TryGetProperty("id", out JsonElement id)) return BadRequest();
+                if (!requert.TryGetProperty("code", out JsonElement code)) return BadRequest();
+                if (!requert.TryGetProperty("subjectId", out JsonElement subjectId)) return BadRequest();
+                var client = _azureCosmos.GetCosmosClient();
+                ExamInfo info = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ExamInfo>(id.ToString(), new PartitionKey($"Exam-{code}"));
+                int index = 0;
+                foreach (ExamSubject subject in info.subjects) {
+                    if (!subject.id.Equals(subjectId.ToString()))
+                    {
+                        index++;
+                    }
+                    else {
+                        break;
+                    } 
+                }
+                List<string> stuAns = new();
+                Dictionary<string, List<List<double>>> keyValues = new();
+                List<ExamClassResult> classResults = new();
+                if (info.scope.Equals("school"))
+                {
+
+                    await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryIterator<ExamClassResult>(
+                    queryText: $"select value(c) from c where c.examId = '{id}' and c.subjectId = '{subjectId}'",
+                    requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"ExamClassResult-{info.school}") }))
+                    {
+                        classResults.Add(item);
+                    }
+                }
+                else
+                {
+                    await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryIterator<ExamClassResult>(
+                        queryText: $"select value(c) from c where c.examId = '{id}' and c.subjectId = '{subjectId}'",
+                        requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"ExamClassResult-{info.creatorId}") }))
+                    {
+                        classResults.Add(item);
+                    }
+                }
+                List<List<double>> itemScore = new();
+                for (int i = 0; i < info.papers[index].point.Count; i++)
+                {
+                    List<double> score = new();
+                    foreach (ExamClassResult examClass in classResults)
+                    {
+                        foreach (List<double> sc in examClass.studentScores)
+                        {
+                            score.Add(sc[i]);
+                        }
+                    }
+                    itemScore.Add(score);
+                }
+                foreach (ExamClassResult examClass in classResults)
+                {
+                    foreach (List<string> ans in examClass.studentAnswers)
+                    {
+                        if (ans.Count > 0)
+                        {
+                            stuAns.Add(ans[0]);
+                        }
+                        else
+                        {
+                            stuAns.Add("");
+                        }
+                    }
+                }
+                //info = await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(info, info.id, new PartitionKey($"Exam-{code}"));
+                return Ok(new { score = itemScore , stuAns, paper = info.papers });
+            }
+            catch (Exception ex)
+            {
+                await _dingDing.SendBotMsg($"OS,{_option.Location},exam/scoring\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
+                return BadRequest();
+            }
+
+        }*/
+
+        [ProducesDefaultResponseType]
+        //[AuthToken(Roles = "Student")]
+        [HttpPost("scoring")]
+        public async Task<IActionResult> scoring(JsonElement requert)
+        {
+            //ResponseBuilder builder = ResponseBuilder.custom();
+            //var (id, school) = HttpContext.GetAuthTokenInfo();
+            try
+            {
+                if (!requert.TryGetProperty("id", out JsonElement id)) return BadRequest();
+                if (!requert.TryGetProperty("code", out JsonElement code)) return BadRequest();
+                if (!requert.TryGetProperty("subjectId", out JsonElement subjectId)) return BadRequest();
+                var client = _azureCosmos.GetCosmosClient();
+                var redisClient = _azureRedis.GetRedisClient(8);
+                ExamInfo info = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ExamInfo>(id.ToString(), new PartitionKey($"Exam-{code}"));
+                List<ExamClassResult> classResults = new();
+                List<dynamic> recs = new List<dynamic>();
+                var record = await redisClient.HashGetAllAsync($"Exam:Scoring:{id}-{subjectId}");
+                foreach (var rcd in record)
+                {
+                    var value = JsonDocument.Parse(rcd.Value.ToString());
+                    recs.Add(new { stuId = rcd.Name.ToString(), ans = value });
+                }
+                //var json = JsonDocument.Parse(record);
+                /*if (info.scope.Equals("school"))
+                {
+
+                    await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryIterator<ExamClassResult>(
+                    queryText: $"select value(c) from c where c.examId = '{id}' and c.subjectId = '{subjectId}'",
+                    requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"ExamClassResult-{info.school}") }))
+                    {
+                        classResults.Add(item);
+                    }
+                }
+                else
+                {
+                    await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryIterator<ExamClassResult>(
+                        queryText: $"select value(c) from c where c.examId = '{id}' and c.subjectId = '{subjectId}'",
+                        requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"ExamClassResult-{info.creatorId}") }))
+                    {
+                        classResults.Add(item);
+                    }
+                }
+                List<Task<bool>> tasks = new List<Task<bool>>();
+                foreach (ExamClassResult examClass in classResults) {                  
+                    foreach (string stuId in examClass.studentIds) {
+                        int index = examClass.studentIds.IndexOf(stuId);
+                        tasks.Add(redisClient.HashSetAsync($"Exam:Scoring:{id}-{subjectId}", stuId, new { ans = examClass.studentAnswers[index][0] , score = examClass.studentScores[index] }.ToJsonString()));
+                    }
+                   
+                }
+                await Task.WhenAll(tasks);*/
+                //info = await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(info, info.id, new PartitionKey($"Exam-{code}"));
+                return Ok(recs);
+            }
+            catch (Exception ex)
+            {
+                await _dingDing.SendBotMsg($"OS,{_option.Location},exam/scoring\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
+                return BadRequest();
+            }
+
+        }
     }
     }
 }
 }