浏览代码

教师区级管理

CrazyIter_Bin 3 年之前
父节点
当前提交
d62fd83393

+ 8 - 1
TEAMModelOS.SDK/Models/Cosmos/Teacher/Teacher.cs

@@ -11,7 +11,8 @@ namespace TEAMModelOS.SDK.Models
         public string picture { get; set; }
         public int size { get; set; }
         public string defaultSchool { get; set; }
-        public List<School> schools { get; set; }
+        public List<School> schools { get; set; }= new List<School>();
+        public List<TeacherArea> areas { get; set; } = new List<TeacherArea>();
         public class School
         {
             public string schoolId { get; set; }
@@ -19,5 +20,11 @@ namespace TEAMModelOS.SDK.Models
             public string status { get; set; }
             public long time { get; set; }
         }
+        public class TeacherArea
+        {
+            public string areaId { get; set; }
+            public string name { get; set; }
+            public string status { get; set; }
+        }
     }
 }

+ 25 - 89
TEAMModelOS.SDK/Models/Service/TriggerStuActivity.cs

@@ -150,6 +150,12 @@ namespace TEAMModelOS.SDK
                             //item.from = "SchStuList";
                             classInfos.Add(item);
                         }
+                        await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<ClassInfo>(queryText: $"select c.id,c.name from c where c.id in ({sql})",
+                        requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"TchList-{school}") }))
+                        {
+                            //item.from = "SchStuList";
+                            classInfos.Add(item);
+                        }
                     }
                     List<StuList> tchLists = new List<StuList>();
                     await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<ClassInfo>(queryText: $"select c.id,c.name from c where c.id in ({sql})",
@@ -357,60 +363,35 @@ namespace TEAMModelOS.SDK
                     classes.Add(ss);
                 }
                 List<TmdInfo> tmdinfos = new List<TmdInfo>();
-                List<Students> studentss = new List<Students>();
                 List<string> tmdids = new List<string>();
                 List<StuInfo> stuInfos = new List<StuInfo>();
                 if (!classes.IsNotEmpty()) { return (tmdinfos, new List<StuInfo>(), null); }
                 List<string> sqlList = new List<string>();
                 classes.ForEach(x => { sqlList.Add($" '{x}' "); });
                 string sql = string.Join(" , ", sqlList);
-                List<StuList> schList = new List<StuList>();
-                List<Student> students = new List<Student>();
+                List<TchList> tchLists = new List<TchList>();
+              
                 if (!string.IsNullOrEmpty(school))
                 {
-                    await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<StuList>(queryText: $"select value(c) from c where c.id in ({sql})",
-                    requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"StuList-{school}") }))
+                    await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<TchList>(queryText: $"select value(c) from c where c.id in ({sql})",
+                    requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"TchList-{school}") }))
                     {
-                        schList.Add(item);
+                        tchLists.Add(item);
                     }
-                    await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryIterator<Student>(queryText: $"select value(c) from c where c.classId in ({sql})",
-                        requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Base-{school}") }))
-                    {
-                        students.Add(item);
-                    }
-                }
-                List<StuList> tchLists = new List<StuList>();
-                await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<StuList>(queryText: $"select value(c) from c where c.id in ({sql})",
-                    requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"StuList") }))
-                {
-                    tchLists.Add(item);
+                   
                 }
+                
 
-                foreach (var x in schList)
-                {
-                    if (x.students.IsNotEmpty())
-                    {
-                        studentss.AddRange(x.students);
-                    }
-                    if (x.tmids.IsNotEmpty())
-                    {
-                        tmdids.AddRange(x.tmids);
-                    }
-                    classes.Remove(x.id);
-                }
                 foreach (var x in tchLists)
                 {
-                    if (x.students.IsNotEmpty())
-                    {
-                        studentss.AddRange(x.students);
-                    }
-                    if (x.tmids.IsNotEmpty())
+                   
+                    if (x.teachers.IsNotEmpty())
                     {
-                        tmdids.AddRange(x.tmids);
+                        tmdids.AddRange(x.teachers);
                     }
                     classes.Remove(x.id);
                 }
-
+               
                 if (tmdids.IsNotEmpty())
                 {
                     List<TmdInfo> infos = new List<TmdInfo>();
@@ -419,54 +400,20 @@ namespace TEAMModelOS.SDK
                     var insql = string.Join(",", inids);
                     var queryslt = $"SELECT  value(c) FROM c where c.id in ({insql})";
                     //合并代码/
-                    await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryIterator<TmdInfo>(queryText: queryslt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
+                    await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<TmdInfo>(queryText: queryslt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
                     {
                         infos.Add(item);
                     }
                     tmdinfos.AddRange(infos);
                 }
-                if (studentss.IsNotEmpty())
-                {
-                    List<string> inidstus = new List<string>();
-                    foreach (Students stu in studentss)
-                    {
-                        var querystu = $"SELECT  c.id,c.code,c.name,c.picture,c.classId,c.year,c.schoolId FROM c where c.id = '{stu.id}'";
-                        await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryIterator<StuInfo>(queryText: querystu, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{stu.code}") }))
-                        {
-                            stuInfos.Add(item);
-                        }
-                    }
-                    /*studentss.Select(x => x.id).ToList().ForEach(x => { inidstus.Add($"'{x}'"); });
-                    var insqlstu = string.Join(",", inidstus);
-                    var querystu = $"SELECT  c.id,c.code,c.name,c.picture,c.classId,c.year,c.schoolId FROM c where c.id in ({insqlstu})";
-                    await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryIterator<StuInfo>(queryText: querystu, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-{school}") }))
-                    {
-                        stuInfos.Add(item);
-                    }*/
-                }
-                students.ForEach(x =>
-                {
-                    if (!stuInfos.Select(y => y.classId).ToList().Contains(x.id))
-                    {
-                        stuInfos.Add(new StuInfo { id = x.id, code = x.code, schoolId = x.schoolId, classId = x.classId, name = x.name, picture = x.picture, year = x.year, groupId = x.groupId, groupName = x.groupName, no = x.no });
-                    }
-                });
+               
+               
                 List<ClassListInfo> classInfo = new List<ClassListInfo>();
-                schList.ForEach(x => {
+                tchLists.ForEach(x => {
                     ClassListInfo classListInfo = new ClassListInfo { id = x.id, name = x.name };
-                    if (x.students.IsNotEmpty())
-                    {
-                        x.students.ForEach(y => {
-                            var stuinfo = stuInfos.Where(z => z.id.Equals(y.id)).FirstOrDefault();
-                            if (stuinfo != null)
-                            {
-                                classListInfo.stuInfos.Add(stuinfo);
-                            }
-                        });
-                    }
-                    if (x.tmids.IsNotEmpty())
+                    if (x.teachers.IsNotEmpty())
                     {
-                        x.tmids.ForEach(y => {
+                        x.teachers.ForEach(y => {
                             var tmdinfo = tmdinfos.Where(z => z.id.Equals(y)).FirstOrDefault();
                             if (tmdinfo != null)
                             {
@@ -478,19 +425,9 @@ namespace TEAMModelOS.SDK
                 });
                 tchLists.ForEach(x => {
                     ClassListInfo classListInfo = new ClassListInfo { id = x.id, name = x.name };
-                    if (x.students.IsNotEmpty())
-                    {
-                        x.students.ForEach(y => {
-                            var stuinfo = stuInfos.Where(z => z.id.Equals(y.id)).FirstOrDefault();
-                            if (stuinfo != null)
-                            {
-                                classListInfo.stuInfos.Add(stuinfo);
-                            }
-                        });
-                    }
-                    if (x.tmids.IsNotEmpty())
+                    if (x.teachers.IsNotEmpty())
                     {
-                        x.tmids.ForEach(y => {
+                        x.teachers.ForEach(y => {
                             var tmdinfo = tmdinfos.Where(z => z.id.Equals(y)).FirstOrDefault();
                             if (tmdinfo != null)
                             {
@@ -507,7 +444,6 @@ namespace TEAMModelOS.SDK
                 classInfos.ForEach(x =>
                 {
                     ClassListInfo classListInfo = new ClassListInfo { id = x.id, name = x.name };
-                    var list = students.Where(y => y.classId.Equals(x.id)).ToList();
                     list.ForEach(z => { classListInfo.stuInfos.Add(new StuInfo { id = z.id, code = z.code, schoolId = z.schoolId, classId = z.classId, name = z.name, picture = z.picture, year = z.year, groupId = z.groupId, groupName = z.groupName, no = z.no }); });
                     classInfo.Add(classListInfo);
                 });

+ 6 - 6
TEAMModelOS/Controllers/School/TchListController.cs

@@ -66,7 +66,7 @@ namespace TEAMModelOS.Controllers
                 if (!requert.TryGetProperty("scope", out JsonElement scope)) return BadRequest();
                 requert.TryGetProperty("ids", out JsonElement _stuids);
                 var client = _azureCosmos.GetCosmosClient();
-                List<StuList> stuList = new();
+                List<TchList> tchLists = new();
                 List<string> ids = new();
                 if (!_stuids.ValueKind.Equals(JsonValueKind.Undefined) && _stuids.ValueKind.Equals(JsonValueKind.Array))
                 {
@@ -79,11 +79,11 @@ namespace TEAMModelOS.Controllers
                     string sql = $" and c.id in ({string.Join(",", ids.Select(o => $"'{o}'"))})";
                     query.Append(sql);
                 }
-                await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<StuList>(queryText: query.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"StuList-{code}") }))
+                await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<TchList>(queryText: query.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"TchList-{code}") }))
                 {
-                    stuList.Add(item);
+                    tchLists.Add(item);
                 }
-                return Ok(new { stuList });
+                return Ok(new { tchLists });
             }
             catch (Exception ex)
             {
@@ -106,7 +106,7 @@ namespace TEAMModelOS.Controllers
                 List<dynamic> list = new List<dynamic>();
                 foreach (string cls in classes)
                 {
-                    (List<TmdInfo> tmdids, List<StuInfo> students, List<ClassListInfo> classLists) = await TriggerStuActivity.GetStuListInStu(client, _dingDing, new List<string> { cls }, $"{_code}");
+                    (List<TmdInfo> tmdids, List<StuInfo> students, List<ClassListInfo> classLists) = await TriggerStuActivity.GetTchList(client, _dingDing, new List<string> { cls }, $"{_code}");
                     if (classLists.IsNotEmpty())
                     {
                         list.Add(new { tmdids, students, @class = classLists.First() });
@@ -265,7 +265,7 @@ namespace TEAMModelOS.Controllers
                         await _serviceBus.GetServiceBusClient().SendMessageAsync(ActiveTask, messageChange);
                     }
                 }
-                tchList = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").UpsertItemAsync(tchList, new PartitionKey($"StuList-{tchList.school}"));
+                tchList = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").UpsertItemAsync(tchList, new PartitionKey($"TchList-{tchList.school}"));
                 return tchList;
             }
             catch (Exception ex)

+ 49 - 47
TEAMModelOS/Controllers/Teacher/InitController.cs

@@ -23,6 +23,7 @@ using TEAMModelOS.SDK.Models.Service;
 using Microsoft.Extensions.Configuration;
 using System.Net.Http;
 using TEAMModelOS.SDK;
+using static TEAMModelOS.SDK.Models.Teacher;
 
 namespace TEAMModelOS.Controllers
 {
@@ -150,6 +151,7 @@ namespace TEAMModelOS.Controllers
                 jwt.Payload.TryGetValue("name", out object name);
                 jwt.Payload.TryGetValue("picture", out object picture);
                 List<object> schools = new List<object>();
+                List<TeacherArea> areas = new List<TeacherArea>();
                 string defaultschool = null;
                 //TODO 取得Teacher 個人相關數據(課程清單、虛擬教室清單、歷史紀錄清單等),學校數據另外API處理,多校切換時不同
                 var client = _azureCosmos.GetCosmosClient();
@@ -163,61 +165,61 @@ namespace TEAMModelOS.Controllers
                     tsize = teacher.size;
                     ///教师的总空间 包含 个人空间和学校赠送的空间累加
                     total = teacher.size;
+                    areas = teacher.areas;
                     //检查是否有加入学校,如果加入学校,则当个人空间size是0G的时候,则免费获得一个G空间,但无论加入多少个学校,只能获取一次 1G的免费空间。没有加入学校则默认0G空间,除非自己购买空间
-                  
                     if (teacher.schools.IsNotEmpty()) {
-                        foreach (var sc in teacher.schools) {
-                            string statusNow = sc.status!=null?sc.status:"";
-                            if (statusNow .Equals("join") || statusNow .Equals("invite") || statusNow.Equals("request")) {
-                                dynamic schoolExtobj = new ExpandoObject();
-                                var schoolJson = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{sc.schoolId}", new PartitionKey("Base"));
-                                var school = await JsonDocument.ParseAsync(schoolJson.ContentStream);
-                                schoolExtobj.schoolId = sc.schoolId;
-                                schoolExtobj.name = school.RootElement.GetProperty("name") ;
-                                schoolExtobj.status =sc.status;
-                                schoolExtobj.time = sc.time;
-                                schoolExtobj.picture = school.RootElement.GetProperty("picture");
-                                school.RootElement.TryGetProperty("areaId",out  JsonElement areaId);
-                                schoolExtobj.areaId = $"{areaId}";
-                                Area area = null;
-                                if (!string.IsNullOrEmpty($"{areaId}"))
-                                {
+                    foreach (var sc in teacher.schools) {
+                        string statusNow = sc.status!=null?sc.status:"";
+                        if (statusNow .Equals("join") || statusNow .Equals("invite") || statusNow.Equals("request")) {
+                            dynamic schoolExtobj = new ExpandoObject();
+                            var schoolJson = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{sc.schoolId}", new PartitionKey("Base"));
+                            var school = await JsonDocument.ParseAsync(schoolJson.ContentStream);
+                            schoolExtobj.schoolId = sc.schoolId;
+                            schoolExtobj.name = school.RootElement.GetProperty("name") ;
+                            schoolExtobj.status =sc.status;
+                            schoolExtobj.time = sc.time;
+                            schoolExtobj.picture = school.RootElement.GetProperty("picture");
+                            school.RootElement.TryGetProperty("areaId",out  JsonElement areaId);
+                            schoolExtobj.areaId = $"{areaId}";
+                            Area area = null;
+                            if (!string.IsNullOrEmpty($"{areaId}"))
+                            {
                                     
-                                    try
-                                    {
-                                        area = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<Area>($"{areaId}", new PartitionKey("Base-Area"));
-                                    }
-                                    catch (CosmosException ex)
-                                    {
-                                        area = null;
-                                    }
-                                }
-                                schoolExtobj.area = area;
-                                var sctch = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(id, new PartitionKey($"Teacher-{sc.schoolId}"));
-                                if (sctch.Status == 200)
+                                try
                                 {
-                                    var jsonDoc = await JsonDocument.ParseAsync(sctch.ContentStream);
-                                    SchoolTeacher schoolTeacher = jsonDoc.RootElement.ToObject<SchoolTeacher>();
-                                    schoolTeacher.name = $"{name}";
-                                    schoolTeacher.picture = $"{picture}";
-                                    await  client.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync(schoolTeacher, id, new PartitionKey($"Teacher-{sc.schoolId}"));
-                                    if (jsonDoc.RootElement.TryGetProperty("size", out JsonElement _size) && _size.ValueKind.Equals(JsonValueKind.Number))
-                                    {
-                                        total += _size.GetInt32();
-                                        schoolExtobj.size = _size.GetInt32();
-                                    }
-                                    else { schoolExtobj.size = 0; }
+                                    area = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<Area>($"{areaId}", new PartitionKey("Base-Area"));
                                 }
-                                else {
-                                    schoolExtobj.size=0;
+                                catch (CosmosException ex)
+                                {
+                                    area = null;
                                 }
-                                if (statusNow.Equals("join")) {
-                                    //初始化
-                                  await  TmdUserService.JoinSchool(client, teacher.id, teacher.picture, teacher.name, sc.schoolId, sc.name);
+                            }
+                            schoolExtobj.area = area;
+                            var sctch = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(id, new PartitionKey($"Teacher-{sc.schoolId}"));
+                            if (sctch.Status == 200)
+                            {
+                                var jsonDoc = await JsonDocument.ParseAsync(sctch.ContentStream);
+                                SchoolTeacher schoolTeacher = jsonDoc.RootElement.ToObject<SchoolTeacher>();
+                                schoolTeacher.name = $"{name}";
+                                schoolTeacher.picture = $"{picture}";
+                                await  client.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync(schoolTeacher, id, new PartitionKey($"Teacher-{sc.schoolId}"));
+                                if (jsonDoc.RootElement.TryGetProperty("size", out JsonElement _size) && _size.ValueKind.Equals(JsonValueKind.Number))
+                                {
+                                    total += _size.GetInt32();
+                                    schoolExtobj.size = _size.GetInt32();
                                 }
-                                schools.Add(schoolExtobj);
+                                else { schoolExtobj.size = 0; }
                             }
+                            else {
+                                schoolExtobj.size=0;
+                            }
+                            if (statusNow.Equals("join")) {
+                                //初始化
+                                await  TmdUserService.JoinSchool(client, teacher.id, teacher.picture, teacher.name, sc.schoolId, sc.name);
+                            }
+                            schools.Add(schoolExtobj);
                         }
+                    }
                         //如果包含任何申请,邀请,加入学校的记录 且个人空间未分配2G  则默认分配个人空间至2G.
                         if ( teacher.size<2 && teacher.schools.Count > 0)
                         {
@@ -283,7 +285,7 @@ namespace TEAMModelOS.Controllers
                 //取得Teacher Blob 容器位置及SAS 
                 await _azureStorage.GetBlobContainerClient(id).CreateIfNotExistsAsync(PublicAccessType.None); //嘗試創建Teacher私有容器,如存在則不做任何事,保障容器一定存在
                 var (blob_uri, blob_sas) = _azureStorage.GetBlobContainerSAS(id, BlobContainerSasPermissions.Write | BlobContainerSasPermissions.Read | BlobContainerSasPermissions.List | BlobContainerSasPermissions.Delete);
-                return Ok(new { location = _option.Location, auth_token, blob_uri, blob_sas, schools, defaultschool, courses, total, tsize });
+                return Ok(new { location = _option.Location, auth_token, blob_uri, blob_sas, schools, defaultschool, courses, total, tsize, areas});
 
             }
             catch (CosmosException ex) {