Explorar o código

名单变更,找出新加人员并通知名单变更的ServiceBus

CrazyIter_Bin %!s(int64=4) %!d(string=hai) anos
pai
achega
4f9ce4b87b

+ 3 - 0
TEAMModelFunction/CourseServiceBus.cs

@@ -38,6 +38,9 @@ namespace TEAMModelFunction
                 await _dingDing.SendBotMsg($"ServiceBus,CourseChange:{msg}", GroupNames.醍摩豆服務運維群組);
                 var jsonMsg = JsonDocument.Parse(msg);
                 CourseChange courseChange = msg.ToObject<CourseChange>();
+                if (courseChange == null) {
+                    return;
+                }
                 //根据新增名单获取 新增的学生id 及timdid
                 (List<string> addTmdids, List<Students> addStudents) = await TriggerStuActivity.GetStuList(client, courseChange.addList, courseChange.school);
                 //根据删除名单获取 新增的学生id 及timdid

+ 1 - 1
TEAMModelFunction/StuListServiceBus.cs

@@ -89,7 +89,7 @@ namespace TEAMModelFunction
             }
             foreach (MQActivity activity in datas) {
                 //已经完结的不再允许加入
-                if (activity.progress.Equals("finish")) {
+                if (activity.progress.Equals("finish")||activity.progress.Equals("pending")) {
                     continue;
                 }
                 //学生新加入名单的

+ 114 - 16
TEAMModelOS/Controllers/School/CourseController.cs

@@ -22,6 +22,7 @@ using TEAMModelOS.SDK.Helper.Common.StringHelper;
 using System.Dynamic;
 using Azure;
 using TEAMModelOS.SDK.Models.Cosmos.Common;
+using Azure.Messaging.ServiceBus;
 
 namespace TEAMModelOS.Controllers
 {
@@ -35,12 +36,13 @@ namespace TEAMModelOS.Controllers
         private AzureCosmosFactory _azureCosmos;
         private readonly DingDing _dingDing;
         private readonly Option _option;
-
-        public CourseController(AzureCosmosFactory azureCosmos, DingDing dingDing, IOptionsSnapshot<Option> option)
+        private readonly AzureServiceBusFactory _serviceBus;
+        public CourseController(AzureCosmosFactory azureCosmos, DingDing dingDing, IOptionsSnapshot<Option> option , AzureServiceBusFactory serviceBus)
         {
             _azureCosmos = azureCosmos;
             _dingDing = dingDing;
             _option = option?.Value;
+            _serviceBus = serviceBus;
         }
 
         /// <summary>
@@ -191,35 +193,131 @@ namespace TEAMModelOS.Controllers
                 var client = _azureCosmos.GetCosmosClient();
                 string originCode = stuList.code;
                 stuList.code = stuList.pk + "-" + stuList.code;
+              
                 if (scope.ToString().Equals("school", StringComparison.OrdinalIgnoreCase))
                 {
+                    StuListChange change = new StuListChange() { 
+                        scope=$"{scope}",
+                        originCode= originCode
+                    };
+                    var query = $"SELECT distinct value(c)  FROM c    where  c.id='{stuList.id}'";
+                    List<StuList> odlStus = new List<StuList>();
+                    await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<StuList>(queryText: query,
+                            requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"StuList-{originCode}") }))
+                    {
+                        odlStus.Add(item);
+                    }
                     if (stuList.students.IsNotEmpty()) {
-                        var query = $"SELECT distinct value(c)  FROM c    where  c.id='{stuList.id}'";
-                        List<StuList> odlStus = new List<StuList>();
-                        await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<StuList>(queryText: query,
-                                requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"StuList-{originCode}") }))
-                        {
-                            odlStus.Add(item);
-                        }
+                       
                         if (odlStus.Count > 0 && odlStus[0].students.IsNotEmpty())
                         {
                             StuList oldStu = odlStus[0];
-                            foreach (var stu in oldStu.students) {
-                                stuList.students.ForEach(x => { 
-                                  //  x.id
-                                });
+                            foreach (var stu in stuList.students) {
+                                bool flag = false;
+                                //判断新增名单成员不在已经存在的名单
+                                foreach (var old in oldStu.students) {
+                                    if (old.id == stu.id && old.code == stu.code) {
+                                        flag = true;
+                                    }
+                                }
+                                if (flag == false) {
+                                    change.stujoin.Add(stu);
+                                } 
                             }
                         }
                     }
-                    
-                   stuList = await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "School").UpsertItemAsync(stuList, new PartitionKey($"StuList-{originCode}"));
+                    if (stuList.tmids.IsNotEmpty()) {
+                        if (odlStus.Count > 0 && odlStus[0].tmids.IsNotEmpty()) {
+                            StuList oldStu = odlStus[0];
+                            foreach (var tmdid in stuList.tmids) {
+                                bool flag = false;
+                                //判断新增名单成员不在已经存在的名单
+                                foreach (var old in oldStu.tmids)
+                                {
+                                    if (old==tmdid)
+                                    {
+                                        flag = true;
+                                    }
+                                }
+                                if (flag == false)
+                                {
+                                    change.tmdjoin.Add(tmdid);
+                                }
+                            }
+                        }
+                    }
+                    var messageChange = new ServiceBusMessage(change.ToJsonString());
+                    messageChange.ApplicationProperties.Add("name", "StuList");
+                    await _serviceBus.GetServiceBusClient().SendMessageAsync("active-task", messageChange);
+                    stuList = await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "School").UpsertItemAsync(stuList, new PartitionKey($"StuList-{originCode}"));
                 }
                 else
                 {
+                    StuListChange change = new StuListChange()
+                    {
+                        scope = $"{scope}",
+                        originCode = originCode
+                    };
+                    var query = $"SELECT distinct value(c)  FROM c    where  c.id='{stuList.id}'";
+                    List<StuList> odlStus = new List<StuList>();
+                    await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<StuList>(queryText: query,
+                            requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"StuList") }))
+                    {
+                        odlStus.Add(item);
+                    }
+                    if (stuList.students.IsNotEmpty())
+                    {
+
+                        if (odlStus.Count > 0 && odlStus[0].students.IsNotEmpty())
+                        {
+                            StuList oldStu = odlStus[0];
+                            foreach (var stu in stuList.students)
+                            {
+                                bool flag = false;
+                                //判断新增名单成员不在已经存在的名单
+                                foreach (var old in oldStu.students)
+                                {
+                                    if (old.id == stu.id && old.code == stu.code)
+                                    {
+                                        flag = true;
+                                    }
+                                }
+                                if (flag == false)
+                                {
+                                    change.stujoin.Add(stu);
+                                }
+                            }
+                        }
+                    }
+                    if (stuList.tmids.IsNotEmpty())
+                    {
+                        if (odlStus.Count > 0 && odlStus[0].tmids.IsNotEmpty())
+                        {
+                            StuList oldStu = odlStus[0];
+                            foreach (var tmdid in stuList.tmids)
+                            {
+                                bool flag = false;
+                                //判断新增名单成员不在已经存在的名单
+                                foreach (var old in oldStu.tmids)
+                                {
+                                    if (old == tmdid)
+                                    {
+                                        flag = true;
+                                    }
+                                }
+                                if (flag == false)
+                                {
+                                    change.tmdjoin.Add(tmdid);
+                                }
+                            }
+                        }
+                    }
+                    var messageChange = new ServiceBusMessage(change.ToJsonString());
+                    messageChange.ApplicationProperties.Add("name", "StuList");
+                    await _serviceBus.GetServiceBusClient().SendMessageAsync("active-task", messageChange);
                     stuList.code = "StuList";
                     stuList = await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync(stuList, new PartitionKey($"StuList"));
                 }
-
                 return Ok(new { stuList });
             }
             catch (Exception ex)