ServiceBusTopic.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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("progress", out JsonElement progress);
  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. ExamInfo exam = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ExamInfo>(id.ToString(), new PartitionKey($"{code}"));
  38. /* if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(exam.startTime) > 0 && DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(exam.endTime) < 0)
  39. {*/
  40. exam.progress = progress.ToString(); ;
  41. /* }
  42. else if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(exam.endTime) > 0)
  43. {*/
  44. /* exam.progress = "finish";
  45. }*/
  46. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(exam, id.ToString(), new PartitionKey($"{code}"));
  47. } catch (Exception ex) {
  48. await _dingDing.SendBotMsg($"ServiceBus,ExamBus()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  49. }
  50. }
  51. [FunctionName("vote")]
  52. public async Task VoteBus([ServiceBusTrigger("active-task", "vote", Connection = "AzureServiceBusConnectionString")] string mySbMsg)
  53. {
  54. try {
  55. var jsonMsg = JsonDocument.Parse(mySbMsg);
  56. jsonMsg.RootElement.TryGetProperty("id", out JsonElement id);
  57. jsonMsg.RootElement.TryGetProperty("progress", out JsonElement progress);
  58. jsonMsg.RootElement.TryGetProperty("code", out JsonElement code);
  59. //Dictionary<string, object> keyValuePairs = mySbMsg.ToObject<Dictionary<string, object>>();
  60. var client = _azureCosmos.GetCosmosClient();
  61. /*keyValuePairs.TryGetValue("id", out object id);
  62. keyValuePairs.TryGetValue("name", out object name);
  63. keyValuePairs.TryGetValue("code", out object code);*/
  64. //keyValuePairs.TryGetValue("status", out object progress);
  65. /* if (name.ToString().Equals("Vote", StringComparison.OrdinalIgnoreCase))
  66. {*/
  67. Vote vote;
  68. var sresponse = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(id.ToString(), new PartitionKey($"{code}"));
  69. if (sresponse.Status == 200)
  70. {
  71. using var json = await JsonDocument.ParseAsync(sresponse.ContentStream);
  72. vote = json.ToObject<Vote>();
  73. vote.progress = progress.ToString();
  74. /*if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(vote.startTime) > 0 && DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(vote.endTime) < 0)
  75. {
  76. vote.progress = "going";
  77. }
  78. else if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(vote.endTime) > 0)
  79. {
  80. vote.progress = "finish";
  81. }*/
  82. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(vote, id.ToString(), new PartitionKey($"{code}"));
  83. }
  84. /* }*/
  85. } catch (Exception ex){
  86. await _dingDing.SendBotMsg($"ServiceBus,VoteBus()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  87. }
  88. }
  89. [FunctionName("survey")]
  90. public async Task SurveyBus([ServiceBusTrigger("active-task", "survey", Connection = "AzureServiceBusConnectionString")] string mySbMsg)
  91. {
  92. try {
  93. var jsonMsg = JsonDocument.Parse(mySbMsg);
  94. jsonMsg.RootElement.TryGetProperty("id", out JsonElement id);
  95. jsonMsg.RootElement.TryGetProperty("progress", out JsonElement progress);
  96. jsonMsg.RootElement.TryGetProperty("code", out JsonElement code);
  97. //Dictionary<string, object> keyValuePairs = mySbMsg.ToObject<Dictionary<string, object>>();
  98. var client = _azureCosmos.GetCosmosClient();
  99. /*keyValuePairs.TryGetValue("id", out object id);
  100. keyValuePairs.TryGetValue("name", out object name);
  101. keyValuePairs.TryGetValue("code", out object code);*/
  102. //keyValuePairs.TryGetValue("status", out object progress);
  103. /*if (name.ToString().Equals("Survey", StringComparison.OrdinalIgnoreCase))
  104. {*/
  105. Survey survey = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Survey>(id.ToString(), new PartitionKey($"{code}"));
  106. /*if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(survey.startTime) > 0 && DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(survey.endTime) < 0)
  107. {
  108. survey.progress = "going";
  109. }
  110. else if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(survey.endTime) > 0)
  111. {
  112. survey.progress = "finish";
  113. }*/
  114. survey.progress = progress.ToString();
  115. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(survey, id.ToString(), new PartitionKey($"{code}"));
  116. //}
  117. }
  118. catch (Exception ex){
  119. await _dingDing.SendBotMsg($"ServiceBus,VoteBus()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  120. }
  121. }
  122. }
  123. }