Bläddra i källkod

統測架構
- 統測活動新建修改後派發給所有報名課程(組)並生成個人評量邏輯
- 老師報名後派發給該老師報名課程(組)所有統測活動評量並生成個人評量邏輯

jeff 10 månader sedan
förälder
incheckning
c033de33a5

+ 3 - 0
TEAMModelOS.Function/IESCosmosDBTrigger.cs

@@ -121,6 +121,9 @@ namespace TEAMModelOS.Function
                                 case "QuotaImport":
                                     await TriggerQuotaImport.Trigger(_coreAPIHttpService, _azureCosmos, _serviceBus, _azureStorage, _dingDing, client, element, data, _httpClient, _configuration, _azureRedis);
                                     break;
+                                case "JointExam":
+                                    await TriggerJointExam.Trigger(_coreAPIHttpService, _azureCosmos, _serviceBus, _azureStorage, _dingDing, client, element, data, _httpClient, _configuration, _httpTrigger, _azureRedis);
+                                    break;
                             }
                         }
                     }

+ 40 - 0
TEAMModelOS.Function/IESServiceBusTrigger.cs

@@ -2037,7 +2037,47 @@ namespace TEAMModelOS.Function
             }
            
         }
+        /// <summary>
+        /// 統測評量開始結束
+        /// </summary>
+        /// <param name="message"></param>
+        /// <param name="messageActions"></param>
+        /// <returns></returns>
+        [Function("JointExam")]
+        public async Task JointExamFunc(
+            [ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "jointexam", Connection = "Azure:ServiceBus:ConnectionString")]
+            ServiceBusReceivedMessage message,
+            ServiceBusMessageActions messageActions)
+        {
+            _logger.LogInformation("Message ID: {id}", message.MessageId);
+            _logger.LogInformation("Message Body: {body}", message.Body);
+            _logger.LogInformation("Message Content-Type: {contentType}", message.ContentType);
+            var json = JsonDocument.Parse(message.Body);
+            try
+            {
 
+                json.RootElement.TryGetProperty("id", out JsonElement id);
+                json.RootElement.TryGetProperty("progress", out JsonElement progress);
+                json.RootElement.TryGetProperty("code", out JsonElement code);
+                var client = _azureCosmos.GetCosmosClient();
+                JointExam jointExam = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<JointExam>(id.ToString(), new PartitionKey($"{code}"));
+                jointExam.progress = progress.ToString();
+                await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(jointExam, id.ToString(), new PartitionKey($"{code}"));
+            }
+            catch (CosmosException e)
+            {
+                await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,JointExamBus()-CosmosDB异常{e.Message}\n{e.StackTrace}\n{e.StatusCode}\n{json.RootElement.ToJsonString()}", GroupNames.醍摩豆服務運維群組);
+            }
+            catch (Exception ex)
+            {
+                await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,JointExamBus()\n{ex.Message}\n{ex.StackTrace}\n\n{json.RootElement.ToJsonString()}", GroupNames.醍摩豆服務運維群組);
+            }
+            finally
+            {  // Complete the message
+                await messageActions.CompleteMessageAsync(message);
+            }
+
+        }
         private async Task RefreshBlob(string name, string u)
         {
             long statr = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();

+ 1 - 2
TEAMModelOS.SDK/Models/Service/JointService.cs

@@ -22,7 +22,7 @@ namespace TEAMModelOS.SDK.Models.Service
     public static class JointService
     {
         //取得JointExam生成Exam
-        public static async Task GenerateExamFromJointAsync(CosmosClient client, AzureStorageFactory _azureStorage, AzureServiceBusFactory _serviceBus, CoreAPIHttpService _coreAPIHttpService, AzureRedisFactory _azureRedis, IConfiguration _configuration, DingDing _dingDing, string jointExamId)
+        public static async Task GenerateExamFromJointExamAsync(CosmosClient client, AzureStorageFactory _azureStorage, AzureServiceBusFactory _serviceBus, CoreAPIHttpService _coreAPIHttpService, AzureRedisFactory _azureRedis, IConfiguration _configuration, DingDing _dingDing, JointExam jointExam)
         {
             try
             {
@@ -30,7 +30,6 @@ namespace TEAMModelOS.SDK.Models.Service
                 //取得JointExam
                 List<JointEventClassBase> classes = new List<JointEventClassBase>();
                 List<JointEventGroupBase> stuLists = new List<JointEventGroupBase>();
-                JointExam jointExam = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<JointExam>(jointExamId, new PartitionKey("JointExam"));
                 //取得JointCourse ※examType == "custom" 之後再處理
                 List<JointEventGroupDb> jointCourses = new List<JointEventGroupDb>();
                 if (!jointExam.examType.Equals("custom"))

+ 37 - 2
TEAMModelOS/Controllers/Teacher/JointEventController.cs

@@ -26,6 +26,7 @@ using DocumentFormat.OpenXml.Drawing;
 using DocumentFormat.OpenXml.Wordprocessing;
 using OfficeOpenXml.FormulaParsing.Excel.Functions.Engineering;
 using static System.Runtime.CompilerServices.RuntimeHelpers;
+using TEAMModelOS.SDK.Models.Service;
 
 namespace TEAMModelOS.Controllers.Common
 {
@@ -506,8 +507,41 @@ namespace TEAMModelOS.Controllers.Common
                     jointCourse.creatorName = creatorName;
                 }
                 jointCourse.courseLists = courseLists;
-
                 await client.GetContainer(Constant.TEAMModelOS, "Teacher").UpsertItemAsync<JointEventGroupDb>(jointCourse, new PartitionKey("JointCourse"));
+
+                //取得該老師報名該活動該組別且progress="going"的JointExam並生成Exam
+                List<JointExam> jointExams = new List<JointExam>();
+                ///個人
+                if (jointCourse.scope.Equals("private"))
+                {
+                    StringBuilder sqlJointExam = new($"SELECT * FROM c WHERE c.jointEventId = '{jointCourse.jointEventId}' AND c.jointGroupId = '{jointCourse.jointGroupId}' AND c.progress = 'going' AND c.examType = 'regular' ");
+                    await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryStreamIteratorSql(queryText: sqlJointExam.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("JointExam") }))
+                    {
+                        using var json = await JsonDocument.ParseAsync(item.Content);
+                        if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
+                        {
+                            foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
+                            {
+                                jointExams.Add(obj.ToObject<JointExam>());
+                            }
+                        }
+                    }
+                }
+                ///學校[待做]
+                else if (scope.Equals("school"))
+                {
+
+                }
+                //Exam生成邏輯
+                if (jointExams.Count > 0)
+                {
+                    foreach (var info in jointExams)
+                    {
+                        //生成所有報名教師個人評量
+                        await JointService.GenerateExamFromJointExamAsync(client, _azureStorage, _serviceBus, _coreAPIHttpService, _azureRedis, _configuration, _dingDing, info);
+                    }
+                }
+
                 return Ok(new { errCode = "", err = "", jointCourse });
             }
             catch (Exception e)
@@ -760,6 +794,7 @@ namespace TEAMModelOS.Controllers.Common
                         if (classes != null) jointExam.classes = classes;
                         if (stuLists != null) jointExam.stuLists = stuLists;
                     }
+                    jointExam.pk = "JointExam";
                     jointExam.code = "JointExam";
                     jointExam.id = jointExamId;
                     jointExam.jointEventId = jointEventId;
@@ -776,7 +811,7 @@ namespace TEAMModelOS.Controllers.Common
                 jointExam.examOverwrite = jointEventSchedule.examOverwrite;
                 jointExam.startTime = jointEventSchedule.startTime;
                 jointExam.endTime = jointEventSchedule.endTime;
-                jointExam.progress = (jointExam.startTime > now) ? "pending" : (jointExam.endTime > now) ? "finish" : "going";
+                jointExam.progress = (jointExam.startTime > now) ? "pending" : (now > jointExam.endTime) ? "finish" : "going";
                 jointExam.updateTime = now;
 
                 await client.GetContainer(Constant.TEAMModelOS, "Common").UpsertItemAsync<JointExam>(jointExam, new PartitionKey("JointExam"));