ServiceBusTopic.cs 6.1 KB

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