Jelajahi Sumber

通知处理

CrazyIter_Bin 4 tahun lalu
induk
melakukan
f4ef26dadc

+ 47 - 35
TEAMModelFunction/MonitorCosmosDB.cs

@@ -48,43 +48,55 @@ namespace TEAMModelFunction
                     string pk = input.GetPropertyValue<string>("pk");
                     if (!string.IsNullOrWhiteSpace(pk))
                     {
-                        int ttl = input.GetPropertyValue<int>("ttl");
-                        long stime = input.GetPropertyValue<long>("startTime");
-                        long etime = input.GetPropertyValue<long>("endTime");
-                        string school = input.GetPropertyValue<string>("school");
-                        string code = input.GetPropertyValue<string>("code");
-                        string creatorId = input.GetPropertyValue<string>("creatorId");
-                        string progress = input.GetPropertyValue<string>("progress");
-                        string scope = input.GetPropertyValue<string>("scope");
-                        string name = input.GetPropertyValue<string>("name");
-                        int? status = input.GetPropertyValue<int?>("status");
-                        var data = new TriggerData {
-                            stime = stime,
-                            etime = etime,
-                            school = school,
-                            code = code,
-                            creatorId = creatorId,
-                            progress = progress,
-                            scope = scope,
-                            ttl = ttl,
-                            id = input.Id,
-                            status = status
-                        };
-                        await _dingDing.SendBotMsg($"CosmosDBTrigger,{pk}触发变更\n{data.ToJsonString()}"  ,
-                                        GroupNames.成都开发測試群組);
-                        switch (pk)
-                        {
-                            case "Exam":
-                                TriggerExam.Trigger(_azureCosmos, _serviceBus, _azureStorage, _dingDing, client,input,code,stime,etime,school);
-                                break;
-                            case "Vote":
-                                TriggerVote.Trigger( _serviceBus, _azureStorage, _dingDing, client, input, data, _azureRedis);
-                                break;
-                            case "Survey":
-                                TriggerSurvey.Trigger( _serviceBus, _azureStorage, _dingDing, client, input, data, _azureRedis);
-                                break;
+                        if (pk.Equals("Receiver", StringComparison.OrdinalIgnoreCase))
+                        { 
+                            ///通知接收者的变更
+                            return;
+                           
+                        }
+                        else {
+                            ///活动类型的变更
+                            int ttl = input.GetPropertyValue<int>("ttl");
+                            long stime = input.GetPropertyValue<long>("startTime");
+                            long etime = input.GetPropertyValue<long>("endTime");
+                            string school = input.GetPropertyValue<string>("school");
+                            string code = input.GetPropertyValue<string>("code");
+                            string creatorId = input.GetPropertyValue<string>("creatorId");
+                            string progress = input.GetPropertyValue<string>("progress");
+                            string scope = input.GetPropertyValue<string>("scope");
+                            string name = input.GetPropertyValue<string>("name");
+                            int? status = input.GetPropertyValue<int?>("status");
+                            var data = new TriggerData
+                            {
+                                stime = stime,
+                                etime = etime,
+                                school = school,
+                                code = code,
+                                creatorId = creatorId,
+                                progress = progress,
+                                scope = scope,
+                                ttl = ttl,
+                                id = input.Id,
+                                status = status
+                            };
+                            await _dingDing.SendBotMsg($"CosmosDBTrigger,{pk}触发变更\n{data.ToJsonString()}",
+                                            GroupNames.成都开发測試群組);
+                            switch (pk)
+                            {
+                                case "Exam":
+                                    TriggerExam.Trigger(_azureCosmos, _serviceBus, _azureStorage, _dingDing, client, input, code, stime, etime, school);
+                                    break;
+                                case "Vote":
+                                    TriggerVote.Trigger(_serviceBus, _azureStorage, _dingDing, client, input, data, _azureRedis);
+                                    break;
+                                case "Survey":
+                                    TriggerSurvey.Trigger(_serviceBus, _azureStorage, _dingDing, client, input, data, _azureRedis);
+                                    break;
+
+                            }
 
                         }
+                        
                     }
                 }
             }

+ 133 - 0
TEAMModelFunction/NoticeServiceBus.cs

@@ -0,0 +1,133 @@
+using Azure.Messaging.ServiceBus;
+using Microsoft.Azure.WebJobs;
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.Text.Json;
+using System.Threading.Tasks;
+using TEAMModelOS.SDK.DI;
+using TEAMModelOS.SDK.Extension;
+using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
+using TEAMModelOS.SDK.Models;
+
+namespace TEAMModelFunction
+{
+   
+    public class NoticeServiceBus
+    {
+        private readonly AzureCosmosFactory _azureCosmos;
+        private readonly AzureStorageFactory _azureStorage;
+        private readonly AzureServiceBusFactory _serviceBus;
+        private readonly DingDing _dingDing;
+        public NoticeServiceBus(AzureCosmosFactory azureCosmos, DingDing dingDing,AzureStorageFactory azureStorage, AzureServiceBusFactory serviceBus)
+        {
+            _azureStorage = azureStorage;
+            _azureCosmos = azureCosmos;
+            _dingDing = dingDing;
+            _serviceBus = serviceBus;
+        }
+        [FunctionName("Notice")]
+        public async  Task Notice([ServiceBusTrigger("active-task", "notice", Connection = "Azure:ServiceBus:ConnectionString")] string msg) {
+            var client = _azureCosmos.GetCosmosClient();
+            try {
+                await _dingDing.SendBotMsg($"NoticeServiceBus-Notice:\n发起通知{msg}", GroupNames.成都开发測試群組);
+                List<Task<string>> tasks = new List<Task<string>>();
+                List<Task> sessionTasks = new List<Task>();
+                var jsonMsg = JsonDocument.Parse(msg);
+                Notice notice = msg.ToObject<Notice>();
+                var blobcntr = "";
+                if (notice.scope.Equals("school"))
+                {
+                    blobcntr = notice.school;
+                }
+                else {
+                    blobcntr = notice.creatorId;
+                }
+                var urlNotice = $"{notice.sid}.json";
+                var blobNotice = new { 
+                    notice.sid, 
+                    notice.scode,
+                    notice.spk,
+                    notice.scope,
+                    notice.school,
+                    notice.type,
+                    notice.level,
+                    notice.data,
+                    notice.stime,
+                    notice.etime,
+                    notice.creatorId
+                };
+                tasks.Add(_azureStorage.UploadFileByContainer(blobcntr, blobNotice.ToJsonString(), "notice", urlNotice));
+                var urlReceiver = $"{notice.sid}_receiver.json";
+                var blobReceiver = new
+                {
+                    notice.stuids,
+                    notice.tmdids
+                };
+                tasks.Add(_azureStorage.UploadFileByContainer(blobcntr, blobReceiver.ToJsonString(), "notice", urlReceiver));
+                long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
+                // "ttl":2592000,不能超过30天(2592000),一天(3600),一周(25200)
+                long ttl = notice.etime - now;
+                long day30 = 2592000L * 1000;
+                if (ttl > day30) {
+                    ttl = day30;
+                }
+                if (notice.stuids.IsNotEmpty()) {
+                    //  List<Receiver> receivers = new List<Receiver>();
+                    foreach (var stu in notice.stuids) {
+                        Receiver receiver = new Receiver
+                        {
+                            id = notice.sid,
+                            pk = "Receiver",
+                            status = 0,
+                            school = stu.schoolId,
+                            scope = "school",
+                            code = $"Receiver-{stu.schoolId}-{stu.id}",
+                            ttl = (int)ttl,
+                            ctime= now,
+                            urlNotice=$"notice/{urlNotice}"
+                        };
+                        await client.GetContainer("TEAMModelOS", "Student").UpsertItemAsync<Receiver>(receiver,new Azure.Cosmos.PartitionKey(receiver.code));
+                        //  /student/{stuid}/receive/{notice.sid}.json
+                        //存放通知到学生容器空间
+                        var url = $"{stu.id}/receive/{notice.sid}.json";
+                        tasks.Add(_azureStorage.UploadFileByContainer(blobcntr, receiver.ToJsonString(), "student", url));
+                        var messageBlob = new ServiceBusMessage(notice.ToJsonString()) { SessionId =$"{stu.schoolId}-{stu.id}"};
+                        messageBlob.ApplicationProperties.Add("name", "Receiver");
+                        sessionTasks.Add(_serviceBus.GetServiceBusClient().SendMessageAsync("active-task", messageBlob));
+                    }
+                }
+                if (notice.tmdids.IsNotEmpty())
+                {
+                    foreach (var tmdid in notice.tmdids)
+                    {
+                        Receiver receiver = new Receiver
+                        {
+                            id = notice.sid,
+                            pk = "Receiver",
+                            status = 0,
+                            //school = stu.schoolId,
+                            scope = "school",
+                            code = $"Receiver-{tmdid}",
+                            ttl = (int)ttl,
+                            ctime = now,
+                            urlNotice = $"notice/{urlNotice}"
+                        };
+                        await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync<Receiver>(receiver, new Azure.Cosmos.PartitionKey(receiver.code));
+                        //  /student/{stuid}/receive/{notice.sid}.json
+                        //存放通知到学生容器空间
+                        var url = $"{notice.sid}.json";
+                        tasks.Add(_azureStorage.UploadFileByContainer(blobcntr, receiver.ToJsonString(), "receive", url));
+                        var messageBlob = new ServiceBusMessage(notice.ToJsonString()) { SessionId = $"{tmdid}" };
+                        messageBlob.ApplicationProperties.Add("name", "Receiver");
+                        sessionTasks.Add(_serviceBus.GetServiceBusClient().SendMessageAsync("active-task", messageBlob));
+                    }
+                }
+                await Task.WhenAll(sessionTasks);
+                await Task.WhenAll(tasks);
+            } catch (Exception ex) {
+                 await _dingDing.SendBotMsg($"NoticeServiceBus-Notice\n{ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
+            }
+        }
+    }
+}

+ 23 - 0
TEAMModelFunction/TriggerExam.cs

@@ -178,6 +178,29 @@ namespace TEAMModelFunction
                         });
                     }
                     await TriggerStuActivity.SaveStuActivity(client, stuActivities, tmdActivities);
+                    //向学生或醍摩豆账号发起通知
+                    #region
+                    Notice notice = new Notice()
+                    {
+                        sid = info.id,
+                        scode = info.code,
+                        spk = info.pk,
+                        scope = info.scope,
+                        school = info.school,
+                        stime = info.startTime,
+                        etime = info.endTime,
+                        creatorId = info.creatorId,
+                        stuids = studentss,
+                        tmdids = tmdids,
+                        type = "exam-join",//评测参加通知
+                        level = 1,
+                        data = new { }.ToJsonString()
+
+                    };
+                    var messageBlob = new ServiceBusMessage(notice.ToJsonString());
+                    messageBlob.ApplicationProperties.Add("name", "Notice");
+                    await _serviceBus.GetServiceBusClient().SendMessageAsync("active-task", messageBlob);
+                    #endregion
                     if (examClassResults.Count == 0)
                     {
                         foreach (string cla in info.classes)

+ 1 - 1
TEAMModelFunction/TriggerStuActivity.cs

@@ -87,7 +87,7 @@ namespace TEAMModelFunction
                 }
             });
             students.ForEach(x => {
-                studentss.Add(new Students { id = x.id, code = x.code });
+                studentss.Add(new Students { id = x.id, code = x.code,schoolId=x.schoolId });
             });
             return (tmdids,studentss);
         }

+ 23 - 0
TEAMModelFunction/TriggerSurvey.cs

@@ -128,6 +128,29 @@ namespace TEAMModelFunction
                             });
                         }
                         await TriggerStuActivity.SaveStuActivity(client, stuActivities, tmdActivities);
+                        //向学生或醍摩豆账号发起通知
+                        #region
+                        Notice notice = new Notice()
+                        {
+                            sid = survey.id,
+                            scode = survey.code,
+                            spk = survey.pk,
+                            scope = survey.scope,
+                            school = survey.school,
+                            stime = survey.startTime,
+                            etime = survey.endTime,
+                            creatorId = survey.creatorId,
+                            stuids = students,
+                            tmdids = tmdids,
+                            type = "survey-join",//问卷参加参加通知
+                            level = 1,
+                            data = new { }.ToJsonString()
+
+                        };
+                        var messageBlob = new ServiceBusMessage(notice.ToJsonString());
+                        messageBlob.ApplicationProperties.Add("name", "Notice");
+                        await _serviceBus.GetServiceBusClient().SendMessageAsync("active-task", messageBlob);
+                        #endregion
                         await _dingDing.SendBotMsg($"问卷调查{tdata.id}写入完成!", GroupNames.成都开发測試群組);
                         var messageSurveyEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = tdata.code }.ToJsonString());
                         messageSurveyEnd.ApplicationProperties.Add("name", "Survey");

+ 23 - 0
TEAMModelFunction/TriggerVote.cs

@@ -137,6 +137,29 @@ namespace TEAMModelFunction
                             });
                         }
                         await TriggerStuActivity.SaveStuActivity(client, stuActivities, tmdActivities);
+                        //向学生或醍摩豆账号发起通知
+                        #region
+                        Notice notice =   new Notice()
+                        {
+                            sid=vote.id,
+                            scode=vote.code,
+                            spk=vote.pk,
+                            scope=vote.scope,
+                            school=vote.school,
+                            stime=vote.startTime,
+                            etime=vote.endTime,
+                            creatorId=vote.creatorId,
+                            stuids=students,
+                            tmdids=tmdids,
+                            type="vote-join",//投票参加通知
+                            level=1,
+                            data=new { }.ToJsonString()
+
+                        };
+                        var messageBlob = new ServiceBusMessage(notice.ToJsonString());
+                        messageBlob.ApplicationProperties.Add("name", "Notice");
+                        await _serviceBus.GetServiceBusClient().SendMessageAsync("active-task", messageBlob);
+                        #endregion
                         var messageVoteEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = tdata.code }.ToJsonString());
                         messageVoteEnd.ApplicationProperties.Add("name", "Vote");
                         if (voteRecords.Count > 0)

+ 67 - 14
TEAMModelOS.SDK/Models/Cosmos/Common/Notice.cs

@@ -2,8 +2,9 @@ using Microsoft.Azure.Cosmos.Table;
 using System;
 using System.Collections.Generic;
 using System.Text;
+using TEAMModelOS.SDK.Models.Cosmos.Common;
 
-namespace TEAMModelOS.SDK.Models.Table
+namespace TEAMModelOS.SDK.Models
 {
 
 
@@ -31,7 +32,7 @@ namespace TEAMModelOS.SDK.Models.Table
      */
     /// <summary>
     /// 通知主体存放位置:/notice/{业务id}.json
-    /// 通知主体存放位置:  /notice/{业务id}.json
+ 
     /// 活跃通知放在CosmosDB  Common表中,并设置"ttl":2592000,不能超过30天(2592000),一天(3600),一周(25200),允许删除
     /// 过期通知存放至blob中,存放位置, 允许被删除,直接删除记录
     ///             stuid:/student/stuid/receiver/xxxx时间戳排序.json
@@ -39,30 +40,53 @@ namespace TEAMModelOS.SDK.Models.Table
     /// </summary>
     public class Notice
     {
+
         /// <summary>
-        /// 业务的id
+        /// 源数据的id
         /// </summary>
-        public string id { get; set; }
+        public string sid { get; set; }
         /// <summary>
-        /// Notice-{hbcn/tmdid}
+        /// 源数据的code
         /// </summary>
-        public string code { get; set; }
         public string scode { get; set; }
-        public string school { get; set; }
-        public string scope { get; set; }
+        /// <summary>
+        /// 源数据的pk
+        /// </summary>
         public string spk { get; set; }
         /// <summary>
-        /// 通知
+        /// 源数据的学校编码
         /// </summary>
-        public string pk { get; set; } = "Notice";
+        public string school { get; set; }
+        /// <summary>
+        /// 源数据的scope
+        /// </summary>
+        public string scope { get; set; }
         /// <summary>
-        /// 类型
+        /// 通知的业务类型,做什么事情用,具体业务类型再定义。 vote-join 
         /// </summary>
         public string type { get; set; }
+        /// <summary>
+        /// 重要等级1置顶,2非常重要,3重要,4普通消息,5不重要
+        /// </summary>
         public int level { get; set; }
+        /// <summary>
+        /// 传输数据josn
+        /// </summary>
         public string data { get; set; }
-        public int stime { get; set; }
-        public int etime { get; set; }
+        /// <summary>
+        /// 创建时间
+        /// </summary>
+        public long stime { get; set; }
+        /// <summary>
+        /// 到期时间,发给接收者的cosmosDB ttl是从当前时间到结束时间为止
+        /// </summary>
+        public long etime { get; set; }
+        /// <summary>
+        /// 创建者
+        /// </summary>
+        public string creatorId { get; set; }
+
+        /// 通知主体的被通知人存放位置:  /notice/{业务id}_receiver.json
         /// <summary>
         /// 被通知的醍摩豆账号
         /// </summary>
@@ -70,8 +94,10 @@ namespace TEAMModelOS.SDK.Models.Table
         /// <summary>
         /// 被通知的学校学生账号
         /// </summary>
-        public List<string> stuids { get; set; }
+        public List<Students> stuids { get; set; }
     }
+
+
     /*
     {
         "id":"通知id"
@@ -81,6 +107,13 @@ namespace TEAMModelOS.SDK.Models.Table
         "ttl":2592000,不能超过30天(2592000),一天(3600),一周(25200)
     }
      */
+    /// <summary>
+    /// id  :源数据id
+    /// code:Receiver-{hbcn}-{rid}
+    /// ttl :Notice的etime-当前时间戳
+    /// pk  :Receiver
+    /// </summary>
+    /// /student/{stuid}/
     public class Receiver : CosmosEntity
     {
         public string pk { get; set; } = "Receiver";
@@ -88,6 +121,26 @@ namespace TEAMModelOS.SDK.Models.Table
         /// 0 已发送,1已查看,2已处理,-1已过期
         /// </summary>
         public int status { get; set; }=0;
+        /// <summary>
+        /// 通知接收者
+        /// </summary>
+        public string rid { get; set; }
+        /// <summary>
+        /// 当rid是学校的学生时,学校编码不能为空
+        /// </summary>
+        public string school { get; set; }
+        /// <summary>
+        /// 源数据的scope   school则接收者是学校的学生/teacher接收者是醍摩豆账号
+        /// </summary>
+        public string scope { get; set; }
+        /// <summary>
+        /// 创建时间
+        /// </summary>
+        public long ctime { get; set; }
+        /// <summary>
+        /// 通知url
+        /// </summary>
+        public string urlNotice { get; set; }
 
     }
 }

+ 1 - 0
TEAMModelOS.SDK/Models/Cosmos/Common/StuList.cs

@@ -19,6 +19,7 @@ namespace TEAMModelOS.SDK.Models.Cosmos.Common
     {
         public string id { get; set; }
         public string code { get; set; }
+        public string schoolId { get; set; }
     }
     public class CourseInfo
     {

+ 1 - 1
TEAMModelOS/Controllers/Common/TriggerStuActivity.cs

@@ -90,7 +90,7 @@ namespace OS.Funct
                 }
             });
             students.ForEach(x => {
-                studentss.Add(new Students { id = x.id, code = x.code });
+                studentss.Add(new Students { id = x.id, code = x.code,schoolId=x.schoolId });
             });
             return (tmdids,studentss);
         }

+ 1 - 0
TEAMModelOS/Controllers/Core/ImportController.cs

@@ -266,6 +266,7 @@ namespace TEAMModelOS.Controllers
             }
             var doc = _DOXC2HTMLTranslator.Translate(file.OpenReadStream());
              (List<HTEXLib.DOCX.Models.ItemInfo> tests, List<string> error) = _HTML2ITEMV3Translator.Translate(doc);
+
             return Ok(new { tests, emferror= error });
         }