TriggerSurvey.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using Azure.Cosmos;
  2. using Azure.Messaging.ServiceBus;
  3. using Microsoft.Azure.Documents;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Text;
  7. using System.Text.Json;
  8. using System.Threading.Tasks;
  9. using TEAMModelOS.SDK.DI;
  10. using TEAMModelOS.SDK.Extension;
  11. using TEAMModelOS.SDK.Models;
  12. using TEAMModelOS.SDK.Models.Cosmos;
  13. namespace TEAMModelFunction
  14. {
  15. public class TriggerSurvey
  16. {
  17. public static async void Trigger(AzureCosmosFactory _azureCosmos, AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, DingDing _dingDing,
  18. CosmosClient client, Document input, string code, long stime, long etime, string school)
  19. {
  20. Survey survey = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Survey>(input.Id, new Azure.Cosmos.PartitionKey($"{code}"));
  21. //messageSurvey.ScheduledEnqueueTime = DateTimeOffset.FromUnixTimeMilliseconds(stime);
  22. //string msgid = messageSurvey.MessageId;
  23. List<ChangeRecord> changeRecords = await _azureStorage.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", input.Id }, { "PartitionKey", survey.progress } });
  24. //ChangeRecord surveyRecord = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ChangeRecord>(input.Id, new Azure.Cosmos.PartitionKey($"{survey.progress}"));
  25. switch (survey.progress)
  26. {
  27. case "pending":
  28. var messageSurvey = new ServiceBusMessage(new { id = input.Id, progress = "going", code = code }.ToJsonString());
  29. messageSurvey.ApplicationProperties.Add("name", "Survey");
  30. if (changeRecords.Count > 0)
  31. {
  32. await _serviceBus.GetServiceBusClient().cancelMessage("active-task", changeRecords[0].sequenceNumber);
  33. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageSurvey, DateTimeOffset.FromUnixTimeMilliseconds(stime));
  34. changeRecords[0].sequenceNumber = start;
  35. await _azureStorage.SaveOrUpdate<ChangeRecord>(changeRecords[0]);
  36. //await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(surveyRecord, surveyRecord.id, new Azure.Cosmos.PartitionKey($"{surveyRecord.code}"));
  37. }
  38. else
  39. {
  40. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageSurvey, DateTimeOffset.FromUnixTimeMilliseconds(stime));
  41. ChangeRecord changeRecord = new ChangeRecord
  42. {
  43. RowKey = input.Id,
  44. PartitionKey = "pending",
  45. sequenceNumber = start,
  46. msgId = messageSurvey.MessageId
  47. };
  48. await _azureStorage.Save<ChangeRecord>(changeRecord);
  49. //await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(changeRecord, new Azure.Cosmos.PartitionKey($"{changeRecord.code}"));
  50. }
  51. break;
  52. case "going":
  53. var tcode = code.Replace("Survey-", "");
  54. ActivityData data;
  55. if (survey.scope == "school" || survey.scope == "teacher")
  56. {
  57. data = new ActivityData
  58. {
  59. id = survey.id,
  60. code = $"Activity-{tcode}",
  61. type = "survey",
  62. name = survey.name,
  63. startTime = survey.startTime,
  64. endTime = survey.endTime,
  65. scode = survey.code,
  66. scope = survey.scope,
  67. classes = survey.classes,
  68. tmdids = survey.tmdids
  69. };
  70. await client.GetContainer("TEAMModelOS", "School").UpsertItemAsync<ActivityData>(data, new Azure.Cosmos.PartitionKey(data.code));
  71. }
  72. else if (survey.scope == "private")
  73. {
  74. data = new ActivityData
  75. {
  76. id = survey.id,
  77. code = $"Activity-Common",
  78. type = "survey",
  79. name = survey.name,
  80. startTime = survey.startTime,
  81. endTime = survey.endTime,
  82. scode = survey.code,
  83. scope = survey.scope,
  84. classes = survey.classes,
  85. // tmdids = survey.tmdids
  86. };
  87. await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync<ActivityData>(data, new Azure.Cosmos.PartitionKey(data.code));
  88. }
  89. var messageSurveyEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = code }.ToJsonString());
  90. messageSurveyEnd.ApplicationProperties.Add("name", "Survey");
  91. if (changeRecords.Count > 0)
  92. {
  93. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageSurveyEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
  94. await _serviceBus.GetServiceBusClient().cancelMessage("active-task", changeRecords[0].sequenceNumber);
  95. changeRecords[0].sequenceNumber = end;
  96. await _azureStorage.SaveOrUpdate<ChangeRecord>(changeRecords[0]);
  97. }
  98. else
  99. {
  100. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageSurveyEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
  101. ChangeRecord changeRecord = new ChangeRecord
  102. {
  103. RowKey = input.Id,
  104. PartitionKey = "going",
  105. sequenceNumber = end,
  106. msgId = messageSurveyEnd.MessageId
  107. };
  108. await _azureStorage.Save<ChangeRecord>(changeRecord);
  109. }
  110. break;
  111. }
  112. }
  113. }
  114. }