CrazyIter_Bin 3 rokov pred
rodič
commit
6a9036472b

+ 4 - 0
TEAMModelOS.SDK/Models/Cosmos/Common/GroupList.cs

@@ -33,6 +33,7 @@ namespace TEAMModelOS.SDK.Models
         /// </summary>
         public int scount { get; set; }
         public List<Member> members { get; set; } = new List<Member>();
+        public string leader { get; set; }
     }
     public class RGroupList
     {
@@ -67,6 +68,7 @@ namespace TEAMModelOS.SDK.Models
         /// </summary>
         public int scount { get; set; }
         public List<RMember> members { get; set; } = new List<RMember>();
+        public string leader { get; set; }
     }
 
     public class  Member
@@ -127,6 +129,7 @@ namespace TEAMModelOS.SDK.Models
     }
     public class GroupListDto
     {
+        public string pk { get; set; }
         public string id { get; set; }
         public string code { get; set; }
         public string name { get; set; }
@@ -151,6 +154,7 @@ namespace TEAMModelOS.SDK.Models
         /// 校内账号成员数量
         /// </summary>
         public int scount { get; set; }
+        public string leader { get; set; }
     }
     public class CourseGroupList
     {

+ 35 - 35
TEAMModelOS.SDK/Models/Service/GroupListService.cs

@@ -296,7 +296,7 @@ namespace TEAMModelOS.SDK.Models
             else
             {
                 Dictionary<string, List<GroupListDto>> groups = new Dictionary<string, List<GroupListDto>>();
-                List<Student> students = new List<Student>();
+                //List<Student> students = new List<Student>();
                 string sql = string.Join(",", classes.Select(x => $"'{x}'"));
                 if (!string.IsNullOrEmpty(school))
                 {
@@ -315,14 +315,30 @@ namespace TEAMModelOS.SDK.Models
                     classes = classes.Except(schoolList.Select(y => y.id)).ToList();
                     if (classes.IsNotEmpty())
                     {
-                        sql = string.Join(",", classes.Select(x => $"'{x}'"));
-                        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}") }))
+                        string insql = string.Join(",", classes.Select(x => $"'{x}'"));
+                        //搜寻没有关联学生的行政班
+                        await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<GroupListDto>(queryText: $"select   c.id,c.code,c.name,c.no,c.periodId,c.scope,c.school,c.creatorId,c.type,c.year,c.tcount,c.scount,c.teacher.id as leader from c where c.id in ({insql})",
+                                requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Class-{school}") }))
                         {
-                            students.Add(item);
+                            ///行政班(学生搜寻classId动态返回)class
+                            GroupListDto group = new GroupListDto
+                            {
+                                id = item.id,
+                                code = $"GroupList-{school}",
+                                name = item.name,
+                                periodId = item.periodId,
+                                scope = "school",
+                                school = school,
+                                type = "class",
+                                year = item.year,
+                                leader= item.leader,
+                                no= item.no,
+                                pk= "GroupList",
+                            };
+                            groupLists.Add(group);
                         }
                         //取差集,减少二次搜寻
-                        classes = classes.Except(students.Select(y => y.classId)).ToList();
+                        classes = classes.Except(groupLists.Select(y => y.id)).ToList();
                     }
                 }
                 if (classes.IsNotEmpty())
@@ -340,30 +356,6 @@ namespace TEAMModelOS.SDK.Models
                     }
                 }
                 groupLists = groups.SelectMany(x => x.Value).ToList();
-                if (students.IsNotEmpty())
-                {
-                    List<string> sqlList = students.Select(x => x.classId).ToList();
-                    string insql = string.Join(",", sqlList.Select(x => $"'{x}'"));
-                    await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<ClassInfo>(queryText: $"select c.id,c.name ,c.periodId  ,c.year from c where c.id in ({insql})",
-                              requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Class-{school}") }))
-                    {
-                        ///行政班(学生搜寻classId动态返回)class
-                        List<string> smembers = students.Where(x => x.classId.Equals(item.id)).Select(y => y.id).ToList();
-                        GroupListDto group = new GroupListDto
-                        {
-                            id = item.id,
-                            code = $"GroupList-{school}",
-                            name = item.name,
-                            periodId = item.periodId,
-                            scope = "school",
-                            school = school,
-                            type = "class",
-                            year = item.year,
-                            scount = smembers.Count
-                        };
-                        groupLists.Add(group);
-                    }
-                }
             }
             return groupLists;
         }
@@ -432,7 +424,8 @@ namespace TEAMModelOS.SDK.Models
                     if (classes.IsNotEmpty()) {
                         string insql = string.Join(",", classes.Select(x => $"'{x}'"));
                         //搜寻没有关联学生的行政班
-                        await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<ClassInfo>(queryText: $"select c.id,c.name ,c.periodId  ,c.year from c where c.id in ({insql})",
+                        await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School")
+                            .GetItemQueryIterator<RGroupList>(queryText: $"select  c.id,c.code,c.name,c.no,c.periodId,c.scope,c.school,c.creatorId,c.type,c.year,c.tcount,c.scount,c.teacher.id as leader from c where c.id in ({insql})",
                                 requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Class-{school}") }))
                         {
                             ///行政班(学生搜寻classId动态返回)class
@@ -448,7 +441,10 @@ namespace TEAMModelOS.SDK.Models
                                 type = "class",
                                 year = item.year,
                                 members = smembers,
-                                scount = smembers.Count
+                                scount = smembers.Count,
+                                pk= "GroupList",
+                                leader=item.leader,
+                                no=item.no,
                             };
                             groupLists.Add(group);
                         }
@@ -485,11 +481,12 @@ namespace TEAMModelOS.SDK.Models
                 {
                     List<string> sqlList = students.Select(x => x.classId).ToList();
                     string insql = string.Join(",", sqlList.Select(x => $"'{x}'"));
-                    await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<ClassInfo>(queryText: $"select c.id,c.name ,c.periodId  ,c.year from c where c.id in ({insql})",
+                    await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").
+                        GetItemQueryIterator<RGroupList>(queryText: $"select c.id,c.code,c.name,c.no,c.periodId,c.scope,c.school,c.creatorId,c.type,c.year,c.tcount,c.scount,c.teacher.id as leader from c where c.id in ({insql})",
                               requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Class-{school}") }))
                     {
                         ///行政班(学生搜寻classId动态返回)class
-                        List<RMember> smembers = students.Where(x => x.classId.Equals(item.id)).Select(y => new RMember { id = y.id, code = school, name = y.name, type = 2, picture = y.picture, no = y.no }).ToList();
+                        List<RMember> smembers = students.Where(x => x.classId.Equals(item.id)).Select(y => new RMember { id = y.id, code = school, name = y.name, type = 2, picture = y.picture, no = y.no,classId=y.classId }).ToList();
                         members.AddRange(smembers);
                        
                         RGroupList group = new RGroupList
@@ -503,7 +500,10 @@ namespace TEAMModelOS.SDK.Models
                             type = "class",
                             year = item.year,
                             members = smembers,
-                            scount = smembers.Count
+                            scount = smembers.Count,
+                            no=item.no,
+                            leader=item.leader,
+                            pk= "GroupList"
                         };
                         groupLists.Add(group);
                     }

+ 51 - 11
TEAMModelOS/Controllers/School/GroupListController.cs

@@ -36,7 +36,7 @@ namespace TEAMModelOS.Controllers
         private readonly Option _option;
         private readonly AzureServiceBusFactory _serviceBus;
         private readonly AzureStorageFactory _azureStorage;
-        private const string SummarySql = " c.id,c.code,c.name,c.no,c.periodId,c.scope,c.school,c.creatorId,c.type,c.year,c.tcount,c.scount ";
+        private const string SummarySql = " c.id,c.code,c.name,c.no,c.periodId,c.scope,c.school,c.creatorId,c.type,c.year,c.tcount,c.scount,c.leader ";
         public IConfiguration _configuration { get; set; }
         public GroupListController(AzureCosmosFactory azureCosmos, DingDing dingDing, IOptionsSnapshot<Option> option, AzureServiceBusFactory serviceBus, AzureStorageFactory azureStorage, IConfiguration configuration)
         {
@@ -496,32 +496,72 @@ namespace TEAMModelOS.Controllers
             try
             {
                 var client = _azureCosmos.GetCosmosClient();
+                json.TryGetProperty("type", out JsonElement _type);
                 json.TryGetProperty("code", out JsonElement _code);
                 if (!json.TryGetProperty("scope", out JsonElement scope)) return BadRequest();
                 if (!json.TryGetProperty("id", out JsonElement id)) return BadRequest();
                 string tbname = $"{scope}".Equals("private") ? "Teacher" : "School";
                 string code = "";
-                if ($"{scope}".Equals("private"))
+                if (string.IsNullOrEmpty($"{_type}") || !$"{_type}".Equals("class"))
                 {
-                    //私人名单
-                    code = "GroupList";
+                    if ($"{scope}".Equals("private"))
+                    {
+                        //私人名单
+                        code = "GroupList";
+                    }
+                    else
+                    {
+                        //学校自定义名单
+                        code = !code.StartsWith("GroupList-") ? $"GroupList-{_code}" : code;
+                    }
+                    groupList = await client.GetContainer(Constant.TEAMModelOS, tbname).ReadItemAsync<RGroupList>($"{id}", new PartitionKey($"{code}"));
                 }
-                else
-                {
-                    //学校自定义名单
-                    code = !code.StartsWith("GroupList-") ? $"GroupList-{_code}" : code;
+                else if($"{_type}".Equals("class")) {
+                    try {
+                        code = $"Class-{_code}";
+                        Class clazz = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<Class>($"{id}", new PartitionKey($"{code}"));
+                        if (clazz != null) {
+                            List<Student> students = new List<Student>();
+                            await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryIterator<Student>(queryText: $"select value(c) from c where c.classId = '{clazz.id}'",
+                                requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Base-{_code}") }))
+                            {
+                                students.Add(item);
+                            }     
+                            ///行政班(学生搜寻classId动态返回)class
+                            List<RMember> smembers = students.Where(x => x.classId.Equals(clazz.id)).Select(y => new RMember { id = y.id, code = $"{_code}", name = y.name, type = 2, picture = y.picture, no = y.no,classId=y.classId }).ToList();
+                            groupList = new RGroupList
+                            {
+                                id=clazz.id,
+                                code=$"GroupList-{clazz.school}",
+                                name=clazz.name,
+                                periodId=clazz.periodId,
+                                pk= "GroupList",
+                                year=clazz.year,
+                                school=clazz.school ,
+                                scope = "school",
+                                type = "class",
+                                scount = smembers.Count,
+                                no=clazz.no,
+                                leader=clazz.teacher?.id,
+                                members= smembers,
+                            };
+                        }
+                    }catch (CosmosException ex) {
+                        await _dingDing.SendBotMsg($"OS,{_option.Location},grouplist/get-grouplist-idcode()\n{ex.Message}{ex.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
+                        return Ok(new { groupList });
+                    }
                 }
-                groupList = await client.GetContainer(Constant.TEAMModelOS, tbname).ReadItemAsync<RGroupList>($"{id}", new PartitionKey($"{code}"));
                 (List<RGroupList> groupsData, List<RMember> members) = await GroupListService.GetGroupListMemberInfo(client, $"{groupList.type}", new List<RGroupList> { groupList }, tbname,_dingDing);
                 groupList = groupsData.FirstOrDefault();
             }
             catch (CosmosException ex)
             {
-                await _dingDing.SendBotMsg($"OS,{_option.Location},grouplist/upsert-grouplist()\n{ex.Message}{ex.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
+                await _dingDing.SendBotMsg($"OS,{_option.Location},grouplist/get-grouplist-idcode()\n{ex.Message}{ex.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
+                return Ok(new { groupList });
             }
             catch (Exception ex)
             {
-                await _dingDing.SendBotMsg($"OS,{_option.Location},grouplist/upsert-grouplist()\n{ex.Message}{ex.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
+                await _dingDing.SendBotMsg($"OS,{_option.Location},grouplist/get-grouplist-idcode()\n{ex.Message}{ex.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
             }
             return Ok(new { groupList });
         }