CrazyIter_Bin 3 rokov pred
rodič
commit
3ba50fbe25

+ 146 - 149
TEAMModelFunction/TriggerExam.cs

@@ -178,190 +178,160 @@ namespace TEAMModelFunction
                             #endregion
                             if (examClassResults.Count == 0)
                             {
-                                try
+
+                                foreach (string cla in classes)
                                 {
-                                    foreach (string cla in classes)
+                                    int m = 0;
+                                    foreach (ExamSubject subject in info.subjects)
                                     {
-                                        int m = 0;
-                                        foreach (ExamSubject subject in info.subjects)
+                                        string classCode = "";
+                                        if (string.IsNullOrEmpty(info.school) || !info.scope.Equals("school", StringComparison.OrdinalIgnoreCase))
+                                        {
+                                            classCode = "ExamClassResult-" + info.creatorId;
+                                        }
+                                        else
+                                        {
+                                            classCode = "ExamClassResult-" + info.school;
+                                        }
+                                        ExamClassResult result = new ExamClassResult
+                                        {
+                                            code = classCode,
+                                            examId = info.id,
+                                            id = Guid.NewGuid().ToString(),
+                                            subjectId = subject.id,
+                                            year = info.year,
+                                            scope = info.scope
+                                        };
+                                        result.info.id = cla;
+                                        List<string> ans = new List<string>();
+                                        List<List<string>> anses = new List<List<string>>();
+                                        List<List<Details>> marks = new List<List<Details>>();
+                                        List<double> ansPoint = new List<double>();
+                                        List<string> ids = new List<string>();
+                                        foreach (double p in info.papers[m].point)
+                                        {
+                                            //Details details = new Details();
+                                            //ans.Add(new List<string>());
+                                            anses.Add(new List<string>());
+                                            marks.Add(new List<Details>());
+                                            ansPoint.Add(-1);
+                                        }
+                                        var sresponse = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(cla, new Azure.Cosmos.PartitionKey($"Class-{info.school}"));
+                                        if (sresponse.Status == 200)
                                         {
-                                            string classCode = "";
-                                            if (string.IsNullOrEmpty(info.school) || !info.scope.Equals("school", StringComparison.OrdinalIgnoreCase))
-                                            {
-                                                classCode = "ExamClassResult-" + info.creatorId;
-                                            }
-                                            else
-                                            {
-                                                classCode = "ExamClassResult-" + info.school;
-                                            }
-                                            ExamClassResult result = new ExamClassResult
-                                            {
-                                                code = classCode,
-                                                examId = info.id,
-                                                id = Guid.NewGuid().ToString(),
-                                                subjectId = subject.id,
-                                                year = info.year,
-                                                scope = info.scope
-                                            };
-                                            result.info.id = cla;
-                                            List<string> ans = new List<string>();
-                                            List<List<string>> anses = new List<List<string>>();
-                                            List<List<Details>> marks = new List<List<Details>>();
-                                            List<double> ansPoint = new List<double>();
-                                            List<string> ids = new List<string>();
-                                            foreach (double p in info.papers[m].point)
-                                            {
-                                                //Details details = new Details();
-                                                //ans.Add(new List<string>());
-                                                anses.Add(new List<string>());
-                                                marks.Add(new List<Details>());
-                                                ansPoint.Add(-1);
-                                            }
-                                            var sresponse = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(cla, new Azure.Cosmos.PartitionKey($"Class-{info.school}"));
-                                            if (sresponse.Status == 200)
-                                            {
 
-                                                using var json = await JsonDocument.ParseAsync(sresponse.ContentStream);
-                                                Class classroom = json.ToObject<Class>();
-                                                School sc = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(info.school, new Azure.Cosmos.PartitionKey("Base"));
-                                                foreach (Period period in sc.period)
+                                            using var json = await JsonDocument.ParseAsync(sresponse.ContentStream);
+                                            Class classroom = json.ToObject<Class>();
+                                            School sc = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(info.school, new Azure.Cosmos.PartitionKey("Base"));
+                                            foreach (Period period in sc.period)
+                                            {
+                                                if (period.id.Equals(classroom.periodId))
                                                 {
-                                                    if (period.id.Equals(classroom.periodId))
+                                                    foreach (Semester semester in period.semesters)
                                                     {
-                                                        foreach (Semester semester in period.semesters)
+                                                        if (semester.start == 1)
                                                         {
-                                                            if (semester.start == 1)
+                                                            int year = DateTimeOffset.UtcNow.Year;
+                                                            int month = DateTimeOffset.UtcNow.Month;
+                                                            int day = DateTimeOffset.UtcNow.Day;
+                                                            int time = 0;
+                                                            if (month == semester.month)
+                                                            {
+                                                                time = day >= semester.day ? 0 : 1;
+                                                            }
+                                                            else
                                                             {
-                                                                int year = DateTimeOffset.UtcNow.Year;
-                                                                int month = DateTimeOffset.UtcNow.Month;
-                                                                int day = DateTimeOffset.UtcNow.Day;
-                                                                int time = 0;
-                                                                if (month == semester.month)
-                                                                {
-                                                                    time = day >= semester.day ? 0 : 1;
-                                                                }
-                                                                else
-                                                                {
-                                                                    time = month > semester.month ? 0 : 1;
-                                                                }
-                                                                int eyear = year - time;
-                                                                result.gradeId = (eyear - classroom.year).ToString();
+                                                                time = month > semester.month ? 0 : 1;
                                                             }
+                                                            int eyear = year - time;
+                                                            result.gradeId = (eyear - classroom.year).ToString();
                                                         }
                                                     }
                                                 }
-                                                //result.info.id = classroom.id;
-                                                result.info.name = classroom.name;
-                                                //result.gradeId = classroom.year.ToString();
-                                                //处理班级人数
-                                                await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryStreamIterator(queryText: $"select c.id from c where c.classId = '{classroom.id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Base-{info.school}") }))
+                                            }
+                                            //result.info.id = classroom.id;
+                                            result.info.name = classroom.name;
+                                            //result.gradeId = classroom.year.ToString();
+                                            //处理班级人数
+                                            await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryStreamIterator(queryText: $"select c.id from c where c.classId = '{classroom.id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Base-{info.school}") }))
+                                            {
+                                                using var json_stu = await JsonDocument.ParseAsync(item.ContentStream);
+                                                if (json_stu.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
                                                 {
-                                                    using var json_stu = await JsonDocument.ParseAsync(item.ContentStream);
-                                                    if (json_stu.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
+                                                    var accounts = json_stu.RootElement.GetProperty("Documents").EnumerateArray();
+                                                    while (accounts.MoveNext())
                                                     {
-                                                        var accounts = json_stu.RootElement.GetProperty("Documents").EnumerateArray();
-                                                        while (accounts.MoveNext())
-                                                        {
-                                                            JsonElement account = accounts.Current;
-                                                            ids.Add(account.GetProperty("id").GetString());
-                                                        }
+                                                        JsonElement account = accounts.Current;
+                                                        ids.Add(account.GetProperty("id").GetString());
                                                     }
                                                 }
                                             }
-                                            if (info.scope.Equals("private", StringComparison.OrdinalIgnoreCase))
+                                        }
+                                        if (info.scope.Equals("private", StringComparison.OrdinalIgnoreCase))
+                                        {
+                                            var stuResponse = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemStreamAsync(cla, new Azure.Cosmos.PartitionKey($"StuList"));
+                                            if (stuResponse.Status == 200)
                                             {
-                                                var stuResponse = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemStreamAsync(cla, new Azure.Cosmos.PartitionKey($"StuList"));
-                                                if (stuResponse.Status == 200)
+                                                using var json = await JsonDocument.ParseAsync(stuResponse.ContentStream);
+                                                StuList stuList = json.ToObject<StuList>();
+                                                //result.info.id = stuList.id;
+                                                result.info.name = stuList.name;
+                                                //处理发布对象为自选名单(个人)
+
+                                                foreach (Students stus in stuList.students)
                                                 {
-                                                    using var json = await JsonDocument.ParseAsync(stuResponse.ContentStream);
-                                                    StuList stuList = json.ToObject<StuList>();
-                                                    //result.info.id = stuList.id;
-                                                    result.info.name = stuList.name;
-                                                    //处理发布对象为自选名单(个人)
 
-                                                    foreach (Students stus in stuList.students)
+                                                    if (!ids.Contains(stus.id))
                                                     {
-
-                                                        if (!ids.Contains(stus.id))
-                                                        {
-                                                            ids.Add(stus.id);
-                                                        }
+                                                        ids.Add(stus.id);
                                                     }
-                                                    if (stuList.tmids.Count > 0)
+                                                }
+                                                if (stuList.tmids.Count > 0)
+                                                {
+                                                    foreach (string tid in stuList.tmids)
                                                     {
-                                                        foreach (string tid in stuList.tmids)
-                                                        {
-                                                            ids.Add(tid);
-                                                        }
+                                                        ids.Add(tid);
                                                     }
                                                 }
                                             }
-                                            else
+                                        }
+                                        else
+                                        {
+                                            var stuResponse = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(cla, new Azure.Cosmos.PartitionKey($"StuList-{info.school}"));
+                                            if (stuResponse.Status == 200)
                                             {
-                                                var stuResponse = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(cla, new Azure.Cosmos.PartitionKey($"StuList-{info.school}"));
-                                                if (stuResponse.Status == 200)
+                                                using var json = await JsonDocument.ParseAsync(stuResponse.ContentStream);
+                                                StuList stuList = json.ToObject<StuList>();
+                                                //result.info.id = stuList.id;
+                                                result.info.name = stuList.name;
+                                                //处理发布对象为自选名单(校本)
+                                                foreach (Students stus in stuList.students)
                                                 {
-                                                    using var json = await JsonDocument.ParseAsync(stuResponse.ContentStream);
-                                                    StuList stuList = json.ToObject<StuList>();
-                                                    //result.info.id = stuList.id;
-                                                    result.info.name = stuList.name;
-                                                    //处理发布对象为自选名单(校本)
-                                                    foreach (Students stus in stuList.students)
+                                                    if (!ids.Contains(stus.id))
                                                     {
-                                                        if (!ids.Contains(stus.id))
-                                                        {
-                                                            ids.Add(stus.id);
-                                                        }
+                                                        ids.Add(stus.id);
                                                     }
                                                 }
                                             }
-                                            foreach (string stu in ids)
-                                            {
-                                                result.mark.Add(marks);
-                                                result.studentIds.Add(stu);
-                                                result.studentAnswers.Add(ans);
-                                                result.studentScores.Add(ansPoint);
-                                                result.ans.Add(anses);
-                                                result.sum.Add(0);
-                                            }
-
-                                            //result.progress = info.progress;
-                                            result.school = info.school;
-                                            m++;
-                                            await client.GetContainer(Constant.TEAMModelOS, "Common").CreateItemAsync(result, new Azure.Cosmos.PartitionKey($"{result.code}"));
                                         }
-                                    }
-                                }
-                                catch (Exception ex)
-                                {
-                                    await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-{info.id}-评测going初始化异常{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
-                                }
-                                finally {
-                                    // 发送信息通知
-                                    var messageEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = code }.ToJsonString());
-                                    messageEnd.ApplicationProperties.Add("name", "Exam");
-                                    if (records.Count > 0)
-                                    {
-                                        long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
-                                        await _serviceBus.GetServiceBusClient().cancelMessage(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), records[0].sequenceNumber);
-                                        records[0].sequenceNumber = end;
-                                        await _azureStorage.SaveOrUpdate<ChangeRecord>(records[0]);
-                                        //await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(record, record.id, new Azure.Cosmos.PartitionKey($"{record.code}"));
-                                    }
-                                    else
-                                    {
-                                        long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
-                                        ChangeRecord changeRecord = new ChangeRecord
+                                        foreach (string stu in ids)
                                         {
-                                            RowKey = input.Id,
-                                            PartitionKey = "going",
-                                            sequenceNumber = end,
-                                            msgId = messageEnd.MessageId
-                                        };
-                                        await _azureStorage.Save<ChangeRecord>(changeRecord);
-                                        //await client.GetContainer(Constant.TEAMModelOS, "Common").CreateItemAsync(changeRecord, new Azure.Cosmos.PartitionKey($"{changeRecord.code}"));
+                                            result.mark.Add(marks);
+                                            result.studentIds.Add(stu);
+                                            result.studentAnswers.Add(ans);
+                                            result.studentScores.Add(ansPoint);
+                                            result.ans.Add(anses);
+                                            result.sum.Add(0);
+                                        }
+
+                                        //result.progress = info.progress;
+                                        result.school = info.school;
+                                        m++;
+                                        await client.GetContainer(Constant.TEAMModelOS, "Common").CreateItemAsync(result, new Azure.Cosmos.PartitionKey($"{result.code}"));
                                     }
-                                }                               
+                                }
+
+                                                             
                             }
                             else
                             {
@@ -381,6 +351,33 @@ namespace TEAMModelFunction
                         {
                             await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-{info.id}-评测going状态异常{e.Message}\n{e.StackTrace}", GroupNames.成都开发測試群組);
                         }
+                        finally
+                        {
+                            // 发送信息通知
+                            var messageEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = code }.ToJsonString());
+                            messageEnd.ApplicationProperties.Add("name", "Exam");
+                            if (records.Count > 0)
+                            {
+                                long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
+                                await _serviceBus.GetServiceBusClient().cancelMessage(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), records[0].sequenceNumber);
+                                records[0].sequenceNumber = end;
+                                await _azureStorage.SaveOrUpdate<ChangeRecord>(records[0]);
+                                //await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(record, record.id, new Azure.Cosmos.PartitionKey($"{record.code}"));
+                            }
+                            else
+                            {
+                                long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
+                                ChangeRecord changeRecord = new ChangeRecord
+                                {
+                                    RowKey = input.Id,
+                                    PartitionKey = "going",
+                                    sequenceNumber = end,
+                                    msgId = messageEnd.MessageId
+                                };
+                                await _azureStorage.Save<ChangeRecord>(changeRecord);
+                                //await client.GetContainer(Constant.TEAMModelOS, "Common").CreateItemAsync(changeRecord, new Azure.Cosmos.PartitionKey($"{changeRecord.code}"));
+                            }
+                        }
                         break;
                     case "finish":
                         int fno = 0;

+ 1 - 1
TEAMModelOS.SDK/Models/Cosmos/Common/GroupList.cs

@@ -17,7 +17,7 @@ namespace TEAMModelOS.SDK.Models
         public string school { get; set; }
         public string creatorId { get; set; }
         /// <summary>
-        /// student 学生名单类型,教研组research,学科组subject,好友friend,管理manage等
+        /// 学生名单类型student ,教研组research,学科组subject,好友friend,管理manage,群组group
         /// </summary>
         public string type { get; set; } = "student";
         public List<Member> members { get; set; } = new List<Member>();

+ 10 - 4
TEAMModelOS/Controllers/Item/ItemController.cs

@@ -79,12 +79,18 @@ namespace TEAMModelOS.Controllers
                     ItemCond itemCond = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<ItemCond>($"{tmdid}", new PartitionKey($"ItemCond"));
                     return Ok(new { itemConds = new List<ItemCond>() { itemCond } });
                 }
-                else {
+                else
+                {
                     return Ok(new { itemConds = new List<ItemCond>() { } });
                 }
-            } catch (Exception ex) {
-                await _dingDing.SendBotMsg($"OS,{_option.Location},item/cond-count()\n{ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
-                return Ok(new { itemConds = new List<ItemCond>() {  } });
+            }
+            catch (CosmosException ex)
+            {
+                return Ok(new { itemConds = new List<ItemCond>() { } });
+            }
+            catch (Exception ex)
+            {
+                return Ok(new { itemConds = new List<ItemCond>() { } });
             }
         }
         [ProducesDefaultResponseType]

+ 42 - 6
TEAMModelOS/Controllers/School/GroupListController.cs

@@ -46,24 +46,60 @@ namespace TEAMModelOS.Controllers
         //处理通用名单
         [ProducesDefaultResponseType]
         [AuthToken(Roles = "teacher,admin")]
-        [HttpPost("upsert-stu-list")]
-        public async Task<IActionResult> upsertList(GroupList list)
+        [HttpPost("upsert-group-list")]
+        public async Task<IActionResult> UpsertGroupList(JsonElement json)
         {
             try
             {
+              
                 var (userid, _, _, school) = HttpContext.GetAuthTokenInfo();
-                list.creatorId = userid;
-                list.school = school;
+                
+                
+                if (!json.TryGetProperty("type", out JsonElement _type))
+                {
+                    return BadRequest();
+                }
+                if (!json.TryGetProperty("list", out JsonElement _list))
+                {
+                    return BadRequest();
+                }
+                GroupList list = _list.ToObject<GroupList>();
+                if (list != null) {
+                    list.creatorId = userid;
+                    list.school = school;
+                    list.pk = "GroupList";
+                    list.code =  !list.code.StartsWith("GroupList-")?$"GroupList-{list.code}":list.code;
+
+                    switch (true)
+                    {
+                        //普通学生名单(包含学校教学班名单,个人课程名单),其中学生成员账号类型可以是学校学生账号和醍摩豆ID,分区键为GroupList-hbcn  分区键为GroupList-tmdid
+                        case bool when $"{_type}".Equals("student", StringComparison.OrdinalIgnoreCase):
+                            list.type = "student";
+                            break;
+                        //教研组名单,只有加入学校的老师名单 成员账号类型是醍摩豆ID,保存在学校表,分区键为GroupList-hbcn
+                        case bool when $"{_type}".Equals("research", StringComparison.OrdinalIgnoreCase):
+                            list.type = "research";
+                            break;
+                        //个人好友名单,成员账号类型可以是学校学生账号和醍摩豆ID,分区键为GroupList-tmdid
+                        case bool when $"{_type}".Equals("friend", StringComparison.OrdinalIgnoreCase):
+                            list.type = "friend";
+                            break;
+                        //社交群组类型(包含学校交流群组,个人交流群组),成员账号类型可以是学校学生账号和醍摩豆ID,,分区键为GroupList-hbcn  分区键为GroupList-tmdid
+                        case bool when $"{_type}".Equals("group", StringComparison.OrdinalIgnoreCase):
+                            list.type = "group";
+                            break;
+                    }
+                }
                 return Ok(new { list });
             }
             catch (Exception ex)
             {
-                await _dingDing.SendBotMsg($"OS,{_option.Location},course/upsert-list()\n{ex.Message}{ex.StackTrace}\n{list}", GroupNames.醍摩豆服務運維群組);
+                await _dingDing.SendBotMsg($"OS,{_option.Location},course/upsert-list()\n{ex.Message}{ex.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
                 return BadRequest();
             }
         }
 
-        private async Task<GroupList> UpsertList(GroupList list, string scope) {
+        private async Task<GroupList> UpsertList(GroupList list ) {
             try {
                 var client = _azureCosmos.GetCosmosClient();
                 if (string.IsNullOrEmpty(list.no)) {

+ 8 - 2
TEAMModelOS/Controllers/School/StudentController.cs

@@ -69,6 +69,7 @@ namespace TEAMModelOS.Controllers
                         //id pw name classId no year
                         //retrun 如果有重複則回{ existNo } , 成功則{ id, name, year, classId, no }
                         var importStuds = request.GetProperty("students").EnumerateArray();
+                        Dictionary<string, ListChange> dictChange = new Dictionary<string, ListChange>();
                         while (importStuds.MoveNext())
                         {
                             JsonElement currStud = importStuds.Current;
@@ -89,9 +90,14 @@ namespace TEAMModelOS.Controllers
                             //建立學生
                             studCreateInfo studCreateInfo = new studCreateInfo(id, name, "M", year, pw, classId, no, periodId);
                             var isCreateSuc = await createStudent(schoolId.GetString(), studCreateInfo);
-                            if (isCreateSuc) return this.Ok(new { code = $"Base-{schoolId.GetString()}", id, name, year, classId, no, periodId });
+
+                            if (isCreateSuc) {
+
+                                return this.Ok(new { code = $"Base-{schoolId.GetString()}", id, name, year, classId, no, periodId });
+                            }
                             else return this.Ok(new { code = $"Base-{schoolId.GetString()}", errorId = id });
                         }
+                          
                         break;
                     case "import":
                         //只有ClassNo可以比對
@@ -1656,7 +1662,7 @@ namespace TEAMModelOS.Controllers
                                 }
                             }
                             //將輸入不存在的資料移除。
-                            nonexistentIds.ForEach(o => studentInfos.Remove(o));
+                           // nonexistentIds.ForEach(o => studentInfos.Remove(o));
                         }
                     }
                 }