NoticeServiceBus.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. // // List<Task<string>> tasks = new List<Task<string>>();
  40. // //List<Task> sessionTasks = new List<Task>();
  41. // var jsonMsg = JsonDocument.Parse(msg);
  42. // Notice notice = msg.ToObject<Notice>();
  43. // var blobcntr = "";
  44. // if (notice.scope.Equals("school"))
  45. // {
  46. // blobcntr = notice.school;
  47. // }
  48. // else
  49. // {
  50. // blobcntr = notice.creatorId;
  51. // }
  52. // if (string.IsNullOrEmpty(blobcntr))
  53. // {
  54. // return;
  55. // }
  56. //#if DEBUG
  57. // await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-NoticeServiceBus-Notice:\n发起通知{msg}", GroupNames.成都开发測試群組);
  58. //#endif
  59. // long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  60. // long day30 = 2592000L;
  61. // //处理过期时间大于30天
  62. // if (notice.expire - now > day30) {
  63. // notice.expire = now + day30;
  64. // }
  65. // //通知的主体内容
  66. // var urlNotice = $"{notice.msgId}.json";
  67. // var blobNotice = new
  68. // {
  69. // notice.type,
  70. // notice.priority,
  71. // notice.from,
  72. // notice.toservice,
  73. // notice.creation,//创建时间
  74. // notice.expire,//到期时间
  75. // notice.msgId,
  76. // notice.creatorId,
  77. // notice.school,
  78. // notice.scope,
  79. // notice.body,
  80. // };
  81. // //通知的接收人的集合信息
  82. // await _azureStorage.UploadFileByContainer(blobcntr, blobNotice.ToJsonString(), "notice", urlNotice);
  83. // var urlReceiver = $"{notice.msgId}_receiver.json";
  84. // var blobReceiver = new
  85. // {
  86. // notice.stuids,
  87. // notice.tmdids
  88. // };
  89. // await _azureStorage.UploadFileByContainer(blobcntr, blobReceiver.ToJsonString(), "notice", urlReceiver);
  90. // //发送通知给用户
  91. // // "ttl":2592000,不能超过30天(2592000),一天(3600),一周(25200)
  92. // if (notice.stuids.IsNotEmpty())
  93. // {
  94. // // List<Receiver> receivers = new List<Receiver>();
  95. // foreach (var stu in notice.stuids)
  96. // {
  97. // Receiver receiver = new Receiver
  98. // {
  99. // id = notice.msgId,
  100. // status = 0,
  101. // ctime = now,
  102. // urlNotice = $"/notice/{urlNotice}",
  103. // };
  104. // var url = $"{stu.id}/receive/{notice.msgId}.json";
  105. // await client.HashSetAsync($"Notice:Receiver:{stu.schoolId}-{stu.id}", notice.msgId, $"/student/{url}");
  106. // // await client.GetContainer("TEAMModelOS", "Common").UpsertItemAsync<Receiver>(receiver,new Azure.Cosmos.PartitionKey(receiver.code));
  107. // // /student/{stuid}/receive/{notice.sid}.json
  108. // //存放通知到学生容器空间
  109. // await _azureStorage.UploadFileByContainer(blobcntr, receiver.ToJsonString(), "student", url);
  110. // var messageBlob = new ServiceBusMessage(receiver.ToJsonString()) { SessionId = $"{stu.schoolId}-{stu.id}" };
  111. // // messageBlob.ApplicationProperties.Add("name", "Receiver");
  112. // await _serviceBus.GetServiceBusClient().SendMessageAsync("notice-task", messageBlob);
  113. // }
  114. // }
  115. // if (notice.tmdids.IsNotEmpty())
  116. // {
  117. // foreach (var tmdid in notice.tmdids)
  118. // {
  119. // Receiver receiver = new Receiver
  120. // {
  121. // id = notice.msgId,
  122. // status = 0,
  123. // ctime = now,
  124. // urlNotice = $"/notice/{urlNotice}",
  125. // };
  126. // var url = $"{notice.msgId}.json";
  127. // await client.HashSetAsync($"Notice:Receiver:{tmdid}", notice.msgId, receiver.ToJsonString());
  128. // //await client.GetContainer("TEAMModelOS", "Common").UpsertItemAsync<Receiver>(receiver, new Azure.Cosmos.PartitionKey(receiver.code));
  129. // // /student/{stuid}/receive/{notice.sid}.json
  130. // //存放通知到学生容器空间
  131. // await _azureStorage.UploadFileByContainer(blobcntr, receiver.ToJsonString(), "receive", url);
  132. // var messageBlob = new ServiceBusMessage(receiver.ToJsonString()) { SessionId = $"{tmdid}" };
  133. // //messageBlob.ApplicationProperties.Add("name", "Receiver");
  134. // await _serviceBus.GetServiceBusClient().SendMessageAsync("notice-task", messageBlob);
  135. // }
  136. // }
  137. // }
  138. // catch (Exception ex)
  139. // {
  140. // await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-NoticeServiceBus-Notice\n{ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  141. // }
  142. // }
  143. // }
  144. //}