NoticeServiceBus.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.Json;
  4. using System.Threading.Tasks;
  5. using Azure.Cosmos;
  6. using Azure.Messaging.ServiceBus;
  7. using Microsoft.Azure.WebJobs;
  8. using Microsoft.Azure.WebJobs.Host;
  9. using Microsoft.Extensions.Logging;
  10. using StackExchange.Redis;
  11. using TEAMModelOS.SDK.DI;
  12. using TEAMModelOS.SDK.Extension;
  13. using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
  14. using TEAMModelOS.SDK.Models;
  15. using TEAMModelOS.SDK.Models.Cosmos.Common;
  16. namespace TEAMModelFunction
  17. {
  18. public class NoticeServiceBus
  19. {
  20. private readonly AzureCosmosFactory _azureCosmos;
  21. private readonly DingDing _dingDing;
  22. private readonly AzureStorageFactory _azureStorage;
  23. private readonly AzureRedisFactory _azureRedis;
  24. private readonly AzureServiceBusFactory _serviceBus;
  25. public NoticeServiceBus(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis, AzureServiceBusFactory serviceBus)
  26. {
  27. _azureCosmos = azureCosmos;
  28. _dingDing = dingDing;
  29. _azureStorage = azureStorage;
  30. _azureRedis = azureRedis;
  31. _serviceBus = serviceBus;
  32. }
  33. [FunctionName("Notice")]
  34. public async Task Notice([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "notice", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
  35. {
  36. var client = _azureRedis.GetRedisClient(8);
  37. try
  38. {
  39. Console.WriteLine(msg);
  40. // List<Task<string>> tasks = new List<Task<string>>();
  41. //List<Task> sessionTasks = new List<Task>();
  42. var jsonMsg = JsonDocument.Parse(msg);
  43. Notice notice = msg.ToObject<Notice>();
  44. var blobcntr = "";
  45. if (notice.scope.Equals("school"))
  46. {
  47. blobcntr = notice.school;
  48. }
  49. else
  50. {
  51. blobcntr = notice.creatorId;
  52. }
  53. if (string.IsNullOrEmpty(blobcntr))
  54. {
  55. return;
  56. }
  57. #if DEBUG
  58. await _dingDing.SendBotMsg($"NoticeServiceBus-Notice:\n发起通知{msg}", GroupNames.成都开发測試群組);
  59. #endif
  60. var urlNotice = $"{notice.msgId}.json";
  61. var blobNotice = new
  62. {
  63. notice.type,
  64. notice.priority,
  65. notice.body,
  66. notice.creation,
  67. notice.expire,
  68. notice.creatorId
  69. };
  70. await _azureStorage.UploadFileByContainer(blobcntr, blobNotice.ToJsonString(), "notice", urlNotice);
  71. var urlReceiver = $"{notice.msgId}_receiver.json";
  72. var blobReceiver = new
  73. {
  74. notice.stuids,
  75. notice.tmdids
  76. };
  77. await _azureStorage.UploadFileByContainer(blobcntr, blobReceiver.ToJsonString(), "notice", urlReceiver);
  78. long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  79. // "ttl":2592000,不能超过30天(2592000),一天(3600),一周(25200)
  80. if (notice.stuids.IsNotEmpty())
  81. {
  82. // List<Receiver> receivers = new List<Receiver>();
  83. foreach (var stu in notice.stuids)
  84. {
  85. Receiver receiver = new Receiver
  86. {
  87. id = notice.msgId,
  88. pk = "Receiver",
  89. status = 0,
  90. scope = "school",
  91. ctime = now,
  92. urlNotice = $"/notice/{urlNotice}",
  93. rid = $"{stu.schoolId}-{stu.id}"
  94. };
  95. var url = $"{stu.id}/receive/{notice.msgId}.json";
  96. await client.HashSetAsync($"Notice:Receiver:{stu.schoolId}-{stu.id}", notice.msgId, $"/student/{url}");
  97. // await client.GetContainer("TEAMModelOS", "Common").UpsertItemAsync<Receiver>(receiver,new Azure.Cosmos.PartitionKey(receiver.code));
  98. // /student/{stuid}/receive/{notice.sid}.json
  99. //存放通知到学生容器空间
  100. await _azureStorage.UploadFileByContainer(blobcntr, receiver.ToJsonString(), "student", url);
  101. var messageBlob = new ServiceBusMessage(receiver.ToJsonString()) { SessionId = $"{stu.schoolId}-{stu.id}" };
  102. // messageBlob.ApplicationProperties.Add("name", "Receiver");
  103. await _serviceBus.GetServiceBusClient().SendMessageAsync("notice-task", messageBlob);
  104. }
  105. }
  106. if (notice.tmdids.IsNotEmpty())
  107. {
  108. foreach (var tmdid in notice.tmdids)
  109. {
  110. Receiver receiver = new Receiver
  111. {
  112. id = notice.msgId,
  113. pk = "Receiver",
  114. status = 0,
  115. //school = stu.schoolId,
  116. scope = "school",
  117. ctime = now,
  118. urlNotice = $"/notice/{urlNotice}",
  119. rid = $"{tmdid}"
  120. };
  121. var url = $"{notice.msgId}.json";
  122. await client.HashSetAsync($"Notice:Receiver:{tmdid}", notice.msgId, new { now, url = $"/receive/{url}" }.ToJsonString());
  123. //await client.GetContainer("TEAMModelOS", "Common").UpsertItemAsync<Receiver>(receiver, new Azure.Cosmos.PartitionKey(receiver.code));
  124. // /student/{stuid}/receive/{notice.sid}.json
  125. //存放通知到学生容器空间
  126. await _azureStorage.UploadFileByContainer(blobcntr, receiver.ToJsonString(), "receive", url);
  127. var messageBlob = new ServiceBusMessage(receiver.ToJsonString()) { SessionId = $"{tmdid}" };
  128. //messageBlob.ApplicationProperties.Add("name", "Receiver");
  129. await _serviceBus.GetServiceBusClient().SendMessageAsync("notice-task", messageBlob);
  130. }
  131. }
  132. }
  133. catch (Exception ex)
  134. {
  135. await _dingDing.SendBotMsg($"NoticeServiceBus-Notice\n{ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  136. }
  137. }
  138. }
  139. }