Kaynağa Gözat

处理名单查询以及新增删除批注

zhouj1203@hotmail.com 3 yıl önce
ebeveyn
işleme
e7b62b8866

+ 73 - 94
TEAMModelOS/Controllers/Common/ExamController.cs

@@ -792,7 +792,7 @@ namespace TEAMModelOS.Controllers
                 List<Task<string>> tasks = new List<Task<string>>();
                 foreach (ExamClassResult result in examClassResults)
                 {
-                    int index = result.studentIds.IndexOf(userId.ToString());
+                    //int index = result.studentIds.IndexOf(userId.ToString());
                     //存放客观题作答详情
                     if (result.ans.Count == 0)
                     {
@@ -806,46 +806,11 @@ namespace TEAMModelOS.Controllers
                             result.ans.Add(anc);
                         }
                     }
-                    //处理新增学生作答信息
-                    //List<List<string>> oq = new List<List<string>>();
-                    //classResult.studentAnswers[index] = ans;
-                    if (index == -1)
-                    {
-                        List<double> ansPoint = new List<double>();
-                        List<List<string>> anc = new List<List<string>>();
-                        List<List<Details>> mark = new List<List<Details>>();
-                        foreach (List<string> num in standard)
-                        {
-                            List<string> newAns = new List<string>();
-                            foreach (string san in num)
-                            {
-                                newAns.Add(san);
-                            }
-                            anc.Add(newAns);
-                            ansPoint.Add(-1);
-                            mark.Add(new List<Details>());
-                        }
-                        result.mark.Add(mark);
-                        result.ans.Add(anc);
-                        result.studentIds.Add(userId.ToString());
-                        result.studentScores.Add(ansPoint);
-                        result.studentAnswers.Add(new List<string>());
-                        result.sum.Add(0);
-                    }
-                    //int flagCount = 0;
-                    /*foreach (List<string> str in ans) {
-
-                            if (str.Count == 0) {
-                                flagCount++;
-                        }
-                    }    */
                     int newIndex = result.studentIds.IndexOf(userId.ToString());
-                    /*                    if (flagCount != standard.Count)
-                                        {*/
-                    StringBuilder builder = new StringBuilder();
-                    builder.Append(result.examId).Append("/");
-                    builder.Append(result.subjectId).Append("/");
-                    builder.Append(userId).Append("/");
+                    StringBuilder builder = new();
+                    builder.Append(result.examId).Append('/');
+                    builder.Append(result.subjectId).Append('/');
+                    builder.Append(userId).Append('/');
                     builder.Append("ans.json");
                     /*string FileName = result.examId + "/" + result.subjectId + "/" + studentId;
                     string blob = FileName + "/" + "ans.json";*/
@@ -1295,46 +1260,25 @@ namespace TEAMModelOS.Controllers
                     ids.AddRange(result.studentIds);
                     //codes.AddRange(result.scIds);
                 }
+                List<string> cid = new()
+                {
+                    classId.GetString()
+                };
+                (List<RMember> members, List<RGroupList> groups) = await GroupListService.GetStutmdidListids(_coreAPIHttpService, client, _dingDing, cid, $"{school}");
+                //await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<GroupList>(classId.GetString, new PartitionKey($"Exam-{code}"));
                 ///获取真实的名称 
                 List<ufo> ufos = new();
                 List<string> delIds = new();
-                //List<ufo> students = new List<ufo>();
-                if (ids.Count > 0)
-                {
-                    List<string> sIds = new();
-                    List<string> tIds = new();
-                    List<ufo> students = new List<ufo>();
-                    await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryStreamIterator(queryText: $"select distinct c.id,c.name from c where c.id in ({string.Join(",", ids.Select(o => $"'{o}'"))}) and c.pk = 'Base'",requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-{school}") }))
+                if (groups.Count > 0) {
+                    string scode = groups[0].school;                   
+                    if (ids.Count > 0)
                     {
-                        using var stuJson = await JsonDocument.ParseAsync(item.ContentStream);
-                        if (stuJson.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
-                        {
-                            var accounts = stuJson.RootElement.GetProperty("Documents").EnumerateArray();
-                            while (accounts.MoveNext())
-                            {
-                                JsonElement account = accounts.Current;
-                                ufo fo = new()
-                                {
-                                    id = account.GetProperty("id").GetString(),
-                                    name = account.GetProperty("name").GetString(),
-                                    //schoolId = account.GetProperty("schoolId").GetString(),
-                                    type = 2
-                                };
-                                students.Add(fo);
-                            }
-                        }
-                    }
-                    if (students.Count > 0)
-                    {
-                        ufos.AddRange(students);
-                        foreach (ufo f in students)
-                        {
-                            sIds.Add(f.id);
-                        }
-                        if (ids.Count > sIds.Count)
+                        List<string> sIds = new();
+                        List<string> tIds = new();
+                        List<ufo> students = new List<ufo>();
+                        if (string.IsNullOrEmpty(scode))
                         {
-                            tIds = ids.Except(sIds).ToList();
-                            var content = new StringContent(tIds.ToJsonString(), Encoding.UTF8, "application/json");
+                            var content = new StringContent(ids.ToJsonString(), Encoding.UTF8, "application/json");
                             string json = await _coreAPIHttpService.GetUserInfos(content);
 
                             if (!string.IsNullOrWhiteSpace(json))
@@ -1350,6 +1294,7 @@ namespace TEAMModelOS.Controllers
                                             tIds.Add(fo.id);
                                         }
                                         ufos.AddRange(tmdInfos);
+                                        delIds = ids.Except(tIds).ToList();
                                     }
                                 }
                                 catch (Exception ex)
@@ -1358,36 +1303,70 @@ namespace TEAMModelOS.Controllers
                                 }
                             }
                         }
-                        delIds = ids.Except(sIds.Union(tIds)).ToList();
-                    }
-                    else
-                    {
-                        var content = new StringContent(ids.ToJsonString(), Encoding.UTF8, "application/json");
-                        string json = await _coreAPIHttpService.GetUserInfos(content);
-
-                        if (!string.IsNullOrWhiteSpace(json))
+                        else
                         {
-                            try
+                            await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryStreamIterator(queryText: $"select distinct c.id,c.name from c where c.id in ({string.Join(",", ids.Select(o => $"'{o}'"))}) and c.pk = 'Base'",
+                                requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-{scode}") }))
                             {
-                                List<ufo> tmdInfos = json.ToObject<List<ufo>>();
-                                if (tmdInfos.IsNotEmpty())
+                                using var stuJson = await JsonDocument.ParseAsync(item.ContentStream);
+                                if (stuJson.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
                                 {
-                                    foreach (ufo fo in tmdInfos)
+                                    var accounts = stuJson.RootElement.GetProperty("Documents").EnumerateArray();
+                                    while (accounts.MoveNext())
                                     {
-                                        fo.type = 1;
-                                        tIds.Add(fo.id);
+                                        JsonElement account = accounts.Current;
+                                        ufo fo = new()
+                                        {
+                                            id = account.GetProperty("id").GetString(),
+                                            name = account.GetProperty("name").GetString(),
+                                            //schoolId = account.GetProperty("schoolId").GetString(),
+                                            type = 2
+                                        };
+                                        students.Add(fo);
                                     }
-                                    ufos.AddRange(tmdInfos);
-                                    delIds = ids.Except(tIds).ToList();
                                 }
                             }
-                            catch (Exception ex)
+                            if (students.Count > 0)
                             {
-                                await _dingDing.SendBotMsg($"{_coreAPIHttpService.options.Get("Default").location}用户转换失败:{_coreAPIHttpService.options.Get("Default").url}{json}\n {ex.StackTrace}", GroupNames.成都开发測試群組);
+                                ufos.AddRange(students);
+                                foreach (ufo f in students)
+                                {
+                                    sIds.Add(f.id);
+                                }
+                                if (ids.Count > sIds.Count)
+                                {
+                                    tIds = ids.Except(sIds).ToList();
+                                    var content = new StringContent(tIds.ToJsonString(), Encoding.UTF8, "application/json");
+                                    string json = await _coreAPIHttpService.GetUserInfos(content);
+
+                                    if (!string.IsNullOrWhiteSpace(json))
+                                    {
+                                        try
+                                        {
+                                            List<ufo> tmdInfos = json.ToObject<List<ufo>>();
+                                            if (tmdInfos.IsNotEmpty())
+                                            {
+                                                foreach (ufo fo in tmdInfos)
+                                                {
+                                                    fo.type = 1;
+                                                    tIds.Add(fo.id);
+                                                }
+                                                ufos.AddRange(tmdInfos);
+                                            }
+                                        }
+                                        catch (Exception ex)
+                                        {
+                                            await _dingDing.SendBotMsg($"{_coreAPIHttpService.options.Get("Default").location}用户转换失败:{_coreAPIHttpService.options.Get("Default").url}{json}\n {ex.StackTrace}", GroupNames.成都开发測試群組);
+                                        }
+                                    }
+                                }
+                                delIds = ids.Except(sIds.Union(tIds)).ToList();
                             }
-                        }
+                        }                                             
                     }
                 }
+                //List<ufo> students = new List<ufo>();
+                
 
                 /*if (StringHelper.getKeyCount(requert) == 1 && requert.TryGetProperty("code", out JsonElement code))
                 {

+ 93 - 18
TEAMModelOS/Controllers/Teacher/CommentController.cs

@@ -65,7 +65,8 @@ namespace TEAMModelOS.Controllers
                     var bx = comment.boxes.Find(x => x.type.Equals($"{_type}"));
                     if (bx != null)
                     {
-                        if (!bx.comment.Contains($"{_comment}")) {
+                        if (!bx.comment.Contains($"{_comment}"))
+                        {
                             bx.comment.Add($"{_comment}");
                         }
                     }
@@ -74,19 +75,21 @@ namespace TEAMModelOS.Controllers
                         comment.boxes.Add(new Box { type = $"{_type}", comment = new List<string> { $"{_comment}" } });
                     }
                 }
-                else {
+                else
+                {
                     comment.boxes = new List<Box>() { new Box { type = $"{_type}", comment = new List<string> { $"{_comment}" } } };
                 }
-                comment = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync<Comment>(comment,userid, new PartitionKey("Comment"));
+                comment = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync<Comment>(comment, userid, new PartitionKey("Comment"));
             }
-            catch (CosmosException ex) {
+            catch (CosmosException ex)
+            {
                 comment = new Comment
                 {
                     id = userid,
                     code = "Comment",
                     pk = "Comment",
                     ttl = -1,
-                    boxes = new List<Box>() { new Box { type=$"{_type}", comment= new List<string> { $"{_comment}"} } },
+                    boxes = new List<Box>() { new Box { type = $"{_type}", comment = new List<string> { $"{_comment}" } } },
                 };
                 comment = await client.GetContainer(Constant.TEAMModelOS, "Teacher").CreateItemAsync<Comment>(comment, new PartitionKey("Comment"));
             }
@@ -111,7 +114,8 @@ namespace TEAMModelOS.Controllers
             {
                 comment = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<Comment>(userid, new PartitionKey("Comment"));
             }
-            catch (CosmosException ex) {
+            catch (CosmosException ex)
+            {
                 comment = new Comment
                 {
                     id = userid,
@@ -136,14 +140,16 @@ namespace TEAMModelOS.Controllers
         public async Task<IActionResult> UpsertComment(Comment comment)
         {
             var (userid, name, _, school) = HttpContext.GetAuthTokenInfo();
-            try {
+            try
+            {
                 var client = _azureCosmos.GetCosmosClient();
                 comment.pk = "Comment";
                 comment.id = userid;
                 comment.code = "Comment";
                 comment.ttl = -1;
                 comment = await client.GetContainer(Constant.TEAMModelOS, "Teacher").UpsertItemAsync<Comment>(comment, new PartitionKey(comment.code));
-            } catch (CosmosException ex ) { }
+            }
+            catch (CosmosException ex) { }
             return Ok(new { comment });
         }
 
@@ -218,14 +224,16 @@ namespace TEAMModelOS.Controllers
                                 }
                             }
                         }
-                        else {
+                        else
+                        {
                             result.mark[index][itemIndex.GetInt32()].Add(ans);
                         }
                     }
-                    else {
+                    else
+                    {
                         result.mark[index][itemIndex.GetInt32()].Add(ans);
                     }
-                    
+
                     /*result.mark[index][itemIndex.GetInt32()].Add(ans);
                     if (result.mark == null || result.mark.Count == 0)
                     {
@@ -240,14 +248,81 @@ namespace TEAMModelOS.Controllers
                     classResult = await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(result, result.id, new PartitionKey($"{result.code}"));
                 }
                 await Task.WhenAll(tasks);
-               /* //变更blob 大小
-                ExamInfo info = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<ExamInfo>(id.ToString(), new Azure.Cosmos.PartitionKey($"Exam-{school}"));
-                info.size = await _azureStorage.GetBlobContainerClient(school.ToString()).GetBlobsSize($"exam/{id}");
-                var messageBlob = new ServiceBusMessage(new { id = Guid.NewGuid().ToString(), progress = "annotation", root = $"exam/{id}", name = school }.ToJsonString());
+                /* //变更blob 大小
+                 ExamInfo info = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<ExamInfo>(id.ToString(), new Azure.Cosmos.PartitionKey($"Exam-{school}"));
+                 info.size = await _azureStorage.GetBlobContainerClient(school.ToString()).GetBlobsSize($"exam/{id}");
+                 var messageBlob = new ServiceBusMessage(new { id = Guid.NewGuid().ToString(), progress = "annotation", root = $"exam/{id}", name = school }.ToJsonString());
+
+                 messageBlob.ApplicationProperties.Add("name", "BlobRoot");
+                 var ActiveTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
+                 await _serviceBus.GetServiceBusClient().SendMessageAsync(ActiveTask, messageBlob);*/
+                return Ok(new { classResult });
+            }
+            catch (Exception e)
+            {
+                await _dingDing.SendBotMsg($"OS,{_option.Location},teacher/comment/upsertAnswer()\n{e.Message}", GroupNames.醍摩豆服務運維群組);
+                return BadRequest();
+            }
+
+        }
+
+        //批注
+        [ProducesDefaultResponseType]
+        //[AuthToken(Roles = "teacher")]
+        [HttpPost("delete-answer")]
+        [Authorize(Roles = "IES")]
+        public async Task<IActionResult> deleteAnswer(JsonElement request)
+        {
 
-                messageBlob.ApplicationProperties.Add("name", "BlobRoot");
-                var ActiveTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
-                await _serviceBus.GetServiceBusClient().SendMessageAsync(ActiveTask, messageBlob);*/
+            if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
+            //if (!request.TryGetProperty("mark", out JsonElement mark)) return BadRequest();
+            if (!request.TryGetProperty("studentId", out JsonElement studentId)) return BadRequest();
+            if (!request.TryGetProperty("tmdId", out JsonElement tId)) return BadRequest();
+            if (!request.TryGetProperty("subjectId", out JsonElement subjectId)) return BadRequest();
+            //if (!request.TryGetProperty("classId", out JsonElement classId)) return BadRequest();
+            if (!request.TryGetProperty("index", out JsonElement itemIndex)) return BadRequest();
+            //根据不同评测的类型返回对应的编码
+            if (!request.TryGetProperty("code", out JsonElement school)) return BadRequest();
+            try
+            {
+                var client = _azureCosmos.GetCosmosClient();
+                List<ExamClassResult> examClassResults = new List<ExamClassResult>();
+                await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryStreamIterator(
+                    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}") }))
+                {
+                    using var json = await JsonDocument.ParseAsync(item.ContentStream);
+                    if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
+                    {
+                        foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
+                        {
+                            examClassResults.Add(obj.ToObject<ExamClassResult>());
+                        }
+                    }
+                }
+                ExamClassResult classResult = new ExamClassResult();
+                //Details ans = mark.ToObject<Details>();
+                List<List<string>> standard = new List<List<string>>();
+                //List<double> points = new List<double>();
+                List<Task<string>> tasks = new List<Task<string>>();
+                foreach (ExamClassResult result in examClassResults)
+                {
+                    int index = result.studentIds.IndexOf(studentId.ToString());
+                    List<Details> details = result.mark[index][itemIndex.GetInt32()];
+                    if (details.Count > 0)
+                    {
+                        List<Details> ds = details.Where(x => x.tmdId.Equals(tId.GetString())).ToList();
+                        if (ds.Count > 0)
+                        {
+                            for (int i = 0; i < ds.Count; i++)
+                            {
+                                ds.RemoveAt(i);
+                            }
+                        }
+                    }
+                    classResult = await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(result, result.id, new PartitionKey($"{result.code}"));
+                }
+                await Task.WhenAll(tasks);
                 return Ok(new { classResult });
             }
             catch (Exception e)