Browse Source

优化删除逻辑,优化Function 代码

zhouj1203@hotmail.com 2 năm trước cách đây
mục cha
commit
75cbfac32c

+ 1 - 1
TEAMModelOS.FunctionV4/CosmosDB/TriggerArt.cs

@@ -234,7 +234,7 @@ namespace TEAMModelOS.FunctionV4.CosmosDB
                             break;
                         case "finish":
                             //判定是否是区级创建的活动内容
-                            if (!art.lost.Any())
+                            if (art.lost.Count == 0)
                             {
                                 if (art.owner.Equals("area") && string.IsNullOrEmpty(art.pId))
                                 {

+ 91 - 85
TEAMModelOS/Controllers/Common/ExamController.cs

@@ -33,6 +33,7 @@ using DocumentFormat.OpenXml.Office2016.Excel;
 using OpenXmlPowerTools;
 using TEAMModelOS.SDK.Services;
 using TEAMModelOS.SDK.Models.Service.BI;
+using MathNet.Numerics.RootFinding;
 
 namespace TEAMModelOS.Controllers
 {
@@ -54,7 +55,7 @@ namespace TEAMModelOS.Controllers
         private readonly CoreAPIHttpService _coreAPIHttpService;
         private readonly IWebHostEnvironment _environment;
         public ExamController(IWebHostEnvironment environment, CoreAPIHttpService coreAPIHttpService, AzureCosmosFactory azureCosmos, AzureServiceBusFactory serviceBus, SnowflakeId snowflakeId, DingDing dingDing,
-            IOptionsSnapshot<Option> option, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis,   IConfiguration configuration)
+            IOptionsSnapshot<Option> option, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis, IConfiguration configuration)
         {
             _azureCosmos = azureCosmos;
             _serviceBus = serviceBus;
@@ -226,7 +227,7 @@ namespace TEAMModelOS.Controllers
                             request.size = await _azureStorage.GetBlobContainerClient(request.creatorId).GetBlobsSize($"exam/{request.id}");
                             await BlobService.RefreshBlobRoot(new BlobRefreshMessage { progress = "update", root = $"exam", name = request.creatorId }, _serviceBus, _configuration, _azureRedis);
                         }
-                        
+
                         request.progress = info.progress;
                         int n = 0;
                         foreach (PaperSimple simple in request.papers)
@@ -344,102 +345,104 @@ namespace TEAMModelOS.Controllers
                 if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
                 if (!request.TryGetProperty("scope", out JsonElement scope)) return BadRequest();
                 var client = _azureCosmos.GetCosmosClient();
-                ExamInfo exam = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<ExamInfo>(id.GetString(), new PartitionKey($"Exam-{code}"));
-                bool flag = false;
-                //必须是本人或者这个学校的管理者才能删除
-                if (exam.creatorId == userid)
-                {
-                    flag = true;
-                }
-                else
+                ExamInfo exam;
+                var response = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemStreamAsync(id.GetString(), new PartitionKey($"Exam-{code}"));
+                if (response.Status == 200)
                 {
-                    if (exam.scope == "school" && exam.school.Equals(school))
+                    using var jsonExam = await JsonDocument.ParseAsync(response.ContentStream);
+                    exam = jsonExam.ToObject<ExamInfo>();
+
+                    bool flag = false;
+                    //必须是本人或者这个学校的管理者才能删除
+                    if (exam.creatorId == userid)
                     {
                         flag = true;
                     }
-                }
-                if (flag)
-                {
-                    exam.status = 404;
-                    exam = await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(exam, exam.id, new PartitionKey($"{exam.code}"));
-                    //string school_code = code.ToString().Substring(typeof(ExamClassResult).Name.Length + 1);
-                    //var response = await client.GetContainer(Constant.TEAMModelOS, "Common").DeleteItemStreamAsync(id.ToString(), new PartitionKey($"Exam-{code}"));
-                    //删除blob 相关资料
-                    await _azureStorage.GetBlobServiceClient().DeleteBlobs(_dingDing, code.ToString(), new List<string> { $"exam/{id}" });
-                    //通知评测删除信息
-                    await BlobService.RefreshBlobRoot(new BlobRefreshMessage { progress = "delete", root = $"exam", name = $"{code}" }, _serviceBus, _configuration, _azureRedis);
-                    //删除阅卷配置
-                    var cresponse = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemStreamAsync(id.ToString(), new PartitionKey($"Correct-{code}"));
-                    if (cresponse.Status == 200)
+                    else
                     {
-                        using var json = await JsonDocument.ParseAsync(cresponse.ContentStream);
-                        Correct correct = json.ToObject<Correct>();
-                        if (correct.subs.IsNotEmpty())
+                        if (exam.scope == "school" && exam.school.Equals(school))
                         {
-                            foreach (var sub in correct.subs)
+                            flag = true;
+                        }
+                    }
+                    if (flag)
+                    {
+                        exam.status = 404;
+                        exam = await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(exam, exam.id, new PartitionKey($"{exam.code}"));
+                        //string school_code = code.ToString().Substring(typeof(ExamClassResult).Name.Length + 1);
+                        //var response = await client.GetContainer(Constant.TEAMModelOS, "Common").DeleteItemStreamAsync(id.ToString(), new PartitionKey($"Exam-{code}"));
+                        //删除blob 相关资料
+                        await _azureStorage.GetBlobServiceClient().DeleteBlobs(_dingDing, code.ToString(), new List<string> { $"exam/{id}" });
+                        //通知评测删除信息
+                        await BlobService.RefreshBlobRoot(new BlobRefreshMessage { progress = "delete", root = $"exam", name = $"{code}" }, _serviceBus, _configuration, _azureRedis);
+                        //删除阅卷配置
+                        var cresponse = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemStreamAsync(id.ToString(), new PartitionKey($"Correct-{code}"));
+                        if (cresponse.Status == 200)
+                        {
+                            using var json = await JsonDocument.ParseAsync(cresponse.ContentStream);
+                            Correct correct = json.ToObject<Correct>();
+                            if (correct.subs.IsNotEmpty())
                             {
-                                //List<string> correctIds = new List<string>();
-                                if (sub.markers.IsNotEmpty())
+                                foreach (var sub in correct.subs)
                                 {
-                                    foreach (var marker in sub.markers)
+                                    //List<string> correctIds = new List<string>();
+                                    if (sub.markers.IsNotEmpty())
                                     {
-                                        await ExamService.deleteAsync(client, id.GetString(), marker.id);
+                                        foreach (var marker in sub.markers)
+                                        {
+                                            await ExamService.deleteAsync(client, id.GetString(), marker.id);
+                                        }
                                     }
-                                }
-                                if (sub.err.Count > 0)
-                                {
-                                    foreach (var tId in sub.err)
+                                    if (sub.err.Count > 0)
                                     {
-                                        await ExamService.deleteAsync(client, id.GetString(), tId);
+                                        foreach (var tId in sub.err)
+                                        {
+                                            await ExamService.deleteAsync(client, id.GetString(), tId);
+                                        }
                                     }
-                                }
-                                if (sub.arb.Count > 0)
-                                {
-                                    foreach (var tId in sub.arb)
+                                    if (sub.arb.Count > 0)
                                     {
-                                        await ExamService.deleteAsync(client, id.GetString(), tId);
+                                        foreach (var tId in sub.arb)
+                                        {
+                                            await ExamService.deleteAsync(client, id.GetString(), tId);
+                                        }
                                     }
                                 }
                             }
+                            await client.GetContainer(Constant.TEAMModelOS, "Common").DeleteItemStreamAsync(id.GetString(), new PartitionKey($"Correct-{code}"));
                         }
-                        await client.GetContainer(Constant.TEAMModelOS, "Common").DeleteItemStreamAsync(id.GetString(), new PartitionKey($"Correct-{code}"));
-                    }
 
-                    List<ExamClassResult> examClassResults = new List<ExamClassResult>();
-                    await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIterator<ExamClassResult>(queryText: $"select c.id from c where c.examId = '{id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"ExamClassResult-{code}") }))
-                    {
-                        examClassResults.Add(item);
-                    }
-                    List<string> resultIds = new List<string>();
-                    await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryStreamIterator(queryText: $"select c.id from c ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"ExamResult-{id}") }))
-                    {
-                        using var json = await JsonDocument.ParseAsync(item.ContentStream);
-                        if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
+                        List<ExamClassResult> examClassResults = new List<ExamClassResult>();
+                        await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIterator<ExamClassResult>(queryText: $"select c.id from c where c.examId = '{id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"ExamClassResult-{code}") }))
                         {
-                            var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
-                            while (accounts.MoveNext())
+                            examClassResults.Add(item);
+                        }
+                        List<string> resultIds = new List<string>();
+                        await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryStreamIterator(queryText: $"select c.id from c ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"ExamResult-{id}") }))
+                        {
+                            using var json = await JsonDocument.ParseAsync(item.ContentStream);
+                            if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
                             {
-                                JsonElement account = accounts.Current;
-                                resultIds.Add(account.GetProperty("id").GetString());
+                                var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
+                                while (accounts.MoveNext())
+                                {
+                                    JsonElement account = accounts.Current;
+                                    resultIds.Add(account.GetProperty("id").GetString());
+                                }
                             }
                         }
+                        foreach (ExamClassResult classResult in examClassResults)
+                        {
+                            await client.GetContainer(Constant.TEAMModelOS, "Common").DeleteItemStreamAsync(classResult.id, new PartitionKey($"ExamClassResult-{code}"));
+                        }
+                        await client.GetContainer(Constant.TEAMModelOS, "Common").DeleteItemsStreamAsync(resultIds, $"ExamResult-{id}");
                     }
-                    foreach (ExamClassResult classResult in examClassResults)
-                    {
-                        await client.GetContainer(Constant.TEAMModelOS, "Common").DeleteItemStreamAsync(classResult.id, new PartitionKey($"ExamClassResult-{code}"));
-                    }
-                    await client.GetContainer(Constant.TEAMModelOS, "Common").DeleteItemsStreamAsync(resultIds, $"ExamResult-{id}");
-                    /*foreach (ExamResult result in results)
-                    {
-                        await client.GetContainer(Constant.TEAMModelOS, "Common").DeleteItemStreamAsync(result.id, new PartitionKey($"ExamResult-{id}"));
-                    }*/
-                    //await _azureCosmos.DeleteAll(examClassResults);
+                    return Ok(new { id });
                 }
-
-                return Ok(new { id });
-
-
-
+                else {
+                    return NotFound(new { msg = "未找到对应内容" });
+                }
+               
             }
             catch (Exception e)
             {
@@ -1135,10 +1138,11 @@ namespace TEAMModelOS.Controllers
                     }
                     else
                     {
-                        if (request.TryGetProperty("artId", out JsonElement artId) && request.TryGetProperty("quotaId", out JsonElement quotaId)) {
-                            await getArtInfoAsync(client, artId.GetString(), school, result.sum[newIndex], id.GetString(), subjectId.GetString(), quotaId.GetString(), userId, picture, name,userType,ids);
+                        if (request.TryGetProperty("artId", out JsonElement artId) && request.TryGetProperty("quotaId", out JsonElement quotaId))
+                        {
+                            await getArtInfoAsync(client, artId.GetString(), school, result.sum[newIndex], id.GetString(), subjectId.GetString(), quotaId.GetString(), userId, picture, name, userType, ids);
                         };
-                        
+
                         if ($"{scope}".Equals(Constant.ScopeStudent))
                         {
                             StuActivity activity = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Student").ReadItemAsync<StuActivity>(id.ToString(), new PartitionKey($"Activity-{school}-{userId}"));
@@ -1157,7 +1161,7 @@ namespace TEAMModelOS.Controllers
                             activity.taskStatus = 1;
                             await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync<StuActivity>(activity, id.ToString(), new PartitionKey($"Activity-{userId}"));
                         }
-                    }                   
+                    }
                     classResult = await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(result, result.id, new PartitionKey($"{result.code}"));
                 }
                 await Task.WhenAll(tasks);
@@ -1171,7 +1175,7 @@ namespace TEAMModelOS.Controllers
                 return BadRequest();
             }
         }
-        private async Task getArtInfoAsync(CosmosClient client, string artId, string school, double score, string acId, string subject, string quotaId, string userid, string picture, string name,int userType, List<string> cIds)
+        private async Task getArtInfoAsync(CosmosClient client, string artId, string school, double score, string acId, string subject, string quotaId, string userid, string picture, string name, int userType, List<string> cIds)
         {
             var aresponse = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(artId, new PartitionKey($"Art-{school}"));
             ArtEvaluation art;
@@ -1187,12 +1191,13 @@ namespace TEAMModelOS.Controllers
                     using var json_1 = await JsonDocument.ParseAsync(response.ContentStream);
                     artResult = json_1.ToObject<StudentArtResult>();
                     //bool flage = artResult.results.Exists(a => a.taskId == acId);
-                    artResult.results.ForEach(a => {
+                    artResult.results.ForEach(a =>
+                    {
                         if (a.taskId == acId)
                         {
                             a.score = score;
                         }
-                    });                                
+                    });
                     await client.GetContainer("TEAMModelOS", "Student").ReplaceItemAsync(artResult, artResult.id, new PartitionKey($"{artResult.code}"));
                 }
                 else
@@ -1222,14 +1227,15 @@ namespace TEAMModelOS.Controllers
                                 taskId = task.acId,
                                 subjectId = task.subject
                             };
-                            if (!string.IsNullOrEmpty(quotaResult.taskId)) {
+                            if (!string.IsNullOrEmpty(quotaResult.taskId))
+                            {
                                 if (quotaResult.taskId.Equals(acId))
                                 {
                                     quotaResult.score = score;
                                 }
-                            }                          
+                            }
                             artResult.results.Add(quotaResult);
-                        }                         
+                        }
                     }
                     await client.GetContainer("TEAMModelOS", "Student").CreateItemAsync(artResult, new PartitionKey($"{artResult.code}"));
                 }
@@ -2977,7 +2983,7 @@ namespace TEAMModelOS.Controllers
                                             ids = sub.arb;
                                         }
                                         //request.subs.Select(r => r.markers).ToList();
-                                        
+
                                         await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync(scoring, scoring.id, new PartitionKey(scoring.code));
                                         string sql = $"select c.id, c.name ,c.lang as code from c where c.id in ({string.Join(",", ids.Select(x => $"'{x}'"))})";
                                         List<IdNameCode> idNameCodes = new List<IdNameCode>();