ServiceBusTopic.cs 6.1 KB

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