|
@@ -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.成都开发測試群組);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|