ServiceBusTopic.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 Run([ServiceBusTrigger("test_topic_ActiveTask", "test_topic_ReciveTask", Connection = "ConnectionBusName")] string mySbMsg, ILogger log)
  22. {
  23. log.LogInformation($"C# ServiceBus topic trigger function processed message: {mySbMsg}");
  24. Dictionary<string, object> keyValuePairs = mySbMsg.ToObject<Dictionary<string, object>>();
  25. var client = _azureCosmos.GetCosmosClient();
  26. keyValuePairs.TryGetValue("id", out object id);
  27. keyValuePairs.TryGetValue("name", out object name);
  28. keyValuePairs.TryGetValue("code", out object code);
  29. //keyValuePairs.TryGetValue("status", out object progress);
  30. if (name.ToString().Equals("ExamInfo")) {
  31. ExamInfo exam = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ExamInfo>(id.ToString(), new PartitionKey($"{code}"));
  32. if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(exam.startTime) > 0 && DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(exam.endTime) < 0) {
  33. exam.progress = "going";
  34. } else if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(exam.endTime) > 0) {
  35. exam.progress = "finish";
  36. }
  37. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(exam, id.ToString(), new PartitionKey($"{code}"));
  38. } else if (name.ToString().Equals("Survey")) {
  39. Survey survey = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Survey>(id.ToString(), new PartitionKey($"{code}"));
  40. if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(survey.startTime) > 0 && DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(survey.endTime) < 0)
  41. {
  42. survey.progress = "going";
  43. }
  44. else if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(survey.endTime) > 0)
  45. {
  46. survey.progress = "finish";
  47. }
  48. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(survey, id.ToString(), new PartitionKey($"{code}"));
  49. } else if (name.ToString().Equals("Vote")) {
  50. Vote vote;
  51. var sresponse = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(id.ToString(), new PartitionKey($"{code}"));
  52. if (sresponse.Status == 200)
  53. {
  54. using var json = await JsonDocument.ParseAsync(sresponse.ContentStream);
  55. vote = json.ToObject<Vote>();
  56. if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(vote.startTime) > 0 && DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(vote.endTime) < 0)
  57. {
  58. vote.progress = "going";
  59. }
  60. else if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().CompareTo(vote.endTime) > 0)
  61. {
  62. vote.progress = "finish";
  63. }
  64. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(vote, id.ToString(), new PartitionKey($"{code}"));
  65. }
  66. }
  67. }
  68. }
  69. }