ServiceBusTopic.cs 5.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. public ServiceBusTopic( AzureCosmosFactory azureCosmos)
  17. {
  18. _azureCosmos = azureCosmos;
  19. }
  20. [FunctionName("ServiceBusTopic")]
  21. public async Task ExamBus([ServiceBusTrigger("active-task", "active-exam-recive-task", Connection = "ConnectionBusName")] string mySbMsg)
  22. {
  23. Dictionary<string, object> keyValuePairs = mySbMsg.ToObject<Dictionary<string, object>>();
  24. var client = _azureCosmos.GetCosmosClient();
  25. keyValuePairs.TryGetValue("id", out object id);
  26. keyValuePairs.TryGetValue("name", out object name);
  27. keyValuePairs.TryGetValue("code", out object code);
  28. //keyValuePairs.TryGetValue("status", out object progress);
  29. if (name.ToString().Equals("ExamInfo",StringComparison.OrdinalIgnoreCase)) {
  30. ExamInfo exam = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ExamInfo>(id.ToString(), new PartitionKey($"{code}"));
  31. if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(exam.startTime) > 0 && DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(exam.endTime) < 0) {
  32. exam.progress = "going";
  33. } else if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(exam.endTime) > 0) {
  34. exam.progress = "finish";
  35. }
  36. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(exam, id.ToString(), new PartitionKey($"{code}"));
  37. }
  38. }
  39. public async Task VoteBus([ServiceBusTrigger("active-task", "active-vote-recive-task", Connection = "ConnectionBusName")] string mySbMsg)
  40. {
  41. Dictionary<string, object> keyValuePairs = mySbMsg.ToObject<Dictionary<string, object>>();
  42. var client = _azureCosmos.GetCosmosClient();
  43. keyValuePairs.TryGetValue("id", out object id);
  44. keyValuePairs.TryGetValue("name", out object name);
  45. keyValuePairs.TryGetValue("code", out object code);
  46. //keyValuePairs.TryGetValue("status", out object progress);
  47. if (name.ToString().Equals("Vote",StringComparison.OrdinalIgnoreCase)) {
  48. Vote vote;
  49. var sresponse = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(id.ToString(), new PartitionKey($"{code}"));
  50. if (sresponse.Status == 200)
  51. {
  52. using var json = await JsonDocument.ParseAsync(sresponse.ContentStream);
  53. vote = json.ToObject<Vote>();
  54. if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(vote.startTime) > 0 && DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(vote.endTime) < 0)
  55. {
  56. vote.progress = "going";
  57. }
  58. else if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(vote.endTime) > 0)
  59. {
  60. vote.progress = "finish";
  61. }
  62. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(vote, id.ToString(), new PartitionKey($"{code}"));
  63. }
  64. }
  65. }
  66. public async Task SurveyBus([ServiceBusTrigger("active-task", "active-survey-recive-task", Connection = "ConnectionBusName")] string mySbMsg)
  67. {
  68. Dictionary<string, object> keyValuePairs = mySbMsg.ToObject<Dictionary<string, object>>();
  69. var client = _azureCosmos.GetCosmosClient();
  70. keyValuePairs.TryGetValue("id", out object id);
  71. keyValuePairs.TryGetValue("name", out object name);
  72. keyValuePairs.TryGetValue("code", out object code);
  73. //keyValuePairs.TryGetValue("status", out object progress);
  74. if (name.ToString().Equals("Survey",StringComparison.OrdinalIgnoreCase))
  75. {
  76. Survey survey = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Survey>(id.ToString(), new PartitionKey($"{code}"));
  77. if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(survey.startTime) > 0 && DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(survey.endTime) < 0)
  78. {
  79. survey.progress = "going";
  80. }
  81. else if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(survey.endTime) > 0)
  82. {
  83. survey.progress = "finish";
  84. }
  85. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(survey, id.ToString(), new PartitionKey($"{code}"));
  86. }
  87. }
  88. }
  89. }