ServiceBusTopic.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.Json;
  4. using System.Threading.Tasks;
  5. using Azure.Cosmos;
  6. using Microsoft.Azure.WebJobs;
  7. using Microsoft.Azure.WebJobs.Host;
  8. using Microsoft.Extensions.Logging;
  9. using TEAMModelOS.SDK.DI;
  10. using TEAMModelOS.SDK.Extension;
  11. namespace TEAMModelFunction
  12. {
  13. public class ServiceBusTopic
  14. {
  15. private readonly AzureCosmosFactory _azureCosmos;
  16. private readonly DingDing _dingDing;
  17. public ServiceBusTopic( AzureCosmosFactory azureCosmos, DingDing dingDing)
  18. {
  19. _azureCosmos = azureCosmos;
  20. _dingDing = dingDing;
  21. }
  22. [FunctionName("exam")]
  23. public async Task ExamBus([ServiceBusTrigger("active-task", "exam", Connection = "AzureServiceBusConnectionString")] string mySbMsg)
  24. {
  25. try
  26. {
  27. var json = JsonDocument.Parse(mySbMsg);
  28. json.RootElement.TryGetProperty("id", out JsonElement id);
  29. json.RootElement.TryGetProperty("name", out JsonElement name);
  30. json.RootElement.TryGetProperty("code", out JsonElement code);
  31. //Dictionary<string, object> keyValuePairs = mySbMsg.ToObject<Dictionary<string, object>>();
  32. var client = _azureCosmos.GetCosmosClient();
  33. /* keyValuePairs.TryGetValue("id", out object id);
  34. keyValuePairs.TryGetValue("name", out object name);
  35. keyValuePairs.TryGetValue("code", out object code);*/
  36. //keyValuePairs.TryGetValue("status", out object progress);
  37. if (name.ToString().Equals("Exam", StringComparison.OrdinalIgnoreCase))
  38. {
  39. ExamInfo exam = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ExamInfo>(id.ToString(), new PartitionKey($"{code}"));
  40. if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(exam.startTime) > 0 && DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(exam.endTime) < 0)
  41. {
  42. exam.progress = "going";
  43. }
  44. else if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(exam.endTime) > 0)
  45. {
  46. exam.progress = "finish";
  47. }
  48. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(exam, id.ToString(), new PartitionKey($"{code}"));
  49. }
  50. } catch (Exception ex) {
  51. await _dingDing.SendBotMsg($"ServiceBus,ExamBus()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  52. }
  53. }
  54. [FunctionName("vote")]
  55. public async Task VoteBus([ServiceBusTrigger("active-task", "vote", Connection = "AzureServiceBusConnectionString")] string mySbMsg)
  56. {
  57. try {
  58. var jsonMsg = JsonDocument.Parse(mySbMsg);
  59. jsonMsg.RootElement.TryGetProperty("id", out JsonElement id);
  60. jsonMsg.RootElement.TryGetProperty("name", out JsonElement name);
  61. jsonMsg.RootElement.TryGetProperty("code", out JsonElement code);
  62. //Dictionary<string, object> keyValuePairs = mySbMsg.ToObject<Dictionary<string, object>>();
  63. var client = _azureCosmos.GetCosmosClient();
  64. /*keyValuePairs.TryGetValue("id", out object id);
  65. keyValuePairs.TryGetValue("name", out object name);
  66. keyValuePairs.TryGetValue("code", out object code);*/
  67. //keyValuePairs.TryGetValue("status", out object progress);
  68. if (name.ToString().Equals("Vote", StringComparison.OrdinalIgnoreCase))
  69. {
  70. Vote vote;
  71. var sresponse = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(id.ToString(), new PartitionKey($"{code}"));
  72. if (sresponse.Status == 200)
  73. {
  74. using var json = await JsonDocument.ParseAsync(sresponse.ContentStream);
  75. vote = json.ToObject<Vote>();
  76. if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(vote.startTime) > 0 && DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(vote.endTime) < 0)
  77. {
  78. vote.progress = "going";
  79. }
  80. else if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(vote.endTime) > 0)
  81. {
  82. vote.progress = "finish";
  83. }
  84. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(vote, id.ToString(), new PartitionKey($"{code}"));
  85. }
  86. }
  87. } catch (Exception ex){
  88. await _dingDing.SendBotMsg($"ServiceBus,VoteBus()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  89. }
  90. }
  91. [FunctionName("survey")]
  92. public async Task SurveyBus([ServiceBusTrigger("active-task", "survey", Connection = "AzureServiceBusConnectionString")] string mySbMsg)
  93. {
  94. try {
  95. var jsonMsg = JsonDocument.Parse(mySbMsg);
  96. jsonMsg.RootElement.TryGetProperty("id", out JsonElement id);
  97. jsonMsg.RootElement.TryGetProperty("name", out JsonElement name);
  98. jsonMsg.RootElement.TryGetProperty("code", out JsonElement code);
  99. //Dictionary<string, object> keyValuePairs = mySbMsg.ToObject<Dictionary<string, object>>();
  100. var client = _azureCosmos.GetCosmosClient();
  101. /*keyValuePairs.TryGetValue("id", out object id);
  102. keyValuePairs.TryGetValue("name", out object name);
  103. keyValuePairs.TryGetValue("code", out object code);*/
  104. //keyValuePairs.TryGetValue("status", out object progress);
  105. if (name.ToString().Equals("Survey", StringComparison.OrdinalIgnoreCase))
  106. {
  107. Survey survey = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Survey>(id.ToString(), new PartitionKey($"{code}"));
  108. if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(survey.startTime) > 0 && DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(survey.endTime) < 0)
  109. {
  110. survey.progress = "going";
  111. }
  112. else if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(survey.endTime) > 0)
  113. {
  114. survey.progress = "finish";
  115. }
  116. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(survey, id.ToString(), new PartitionKey($"{code}"));
  117. }
  118. }
  119. catch (Exception ex){
  120. await _dingDing.SendBotMsg($"ServiceBus,VoteBus()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  121. }
  122. }
  123. }
  124. }