TriggerVote.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 static class TriggerVote
  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, AzureRedisFactory _azureRedis)
  19. {
  20. Vote vote = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Vote>(input.Id, new Azure.Cosmos.PartitionKey($"{code}"));
  21. List<ChangeRecord> voteRecords = await _azureStorage.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", input.Id }, { "PartitionKey", vote.progress } });
  22. //ChangeRecord voteRecord = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ChangeRecord>(input.Id, new Azure.Cosmos.PartitionKey($"{vote.progress}"));
  23. if (vote.ttl >= 1)
  24. {
  25. //TODO 处理TTL删除业务
  26. _azureRedis.GetRedisClient(8).KeyDelete($"Vote:Record:{vote.id}_{vote.code}");
  27. _azureRedis.GetRedisClient(8).KeyDelete($"Vote:Count:{vote.id}_{vote.code}");
  28. return;
  29. }
  30. else {
  31. switch (vote.progress)
  32. {
  33. case "pending":
  34. var messageVote = new ServiceBusMessage(new { id = input.Id, progress = "going", code = code }.ToJsonString());
  35. messageVote.ApplicationProperties.Add("name", "Vote");
  36. if (voteRecords.Count > 0)
  37. {
  38. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVote, DateTimeOffset.FromUnixTimeMilliseconds(stime));
  39. await _serviceBus.GetServiceBusClient().cancelMessage("active-task", voteRecords[0].sequenceNumber);
  40. voteRecords[0].sequenceNumber = start;
  41. await _azureStorage.SaveOrUpdate<ChangeRecord>(voteRecords[0]);
  42. //await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(voteRecord, voteRecord.id, new Azure.Cosmos.PartitionKey($"{voteRecord.code}"));
  43. }
  44. else
  45. {
  46. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVote, DateTimeOffset.FromUnixTimeMilliseconds(stime));
  47. ChangeRecord changeRecord = new ChangeRecord
  48. {
  49. RowKey = input.Id,
  50. PartitionKey = "pending",
  51. sequenceNumber = start,
  52. msgId = messageVote.MessageId
  53. };
  54. await _azureStorage.Save<ChangeRecord>(changeRecord);
  55. //await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(changeRecord, new Azure.Cosmos.PartitionKey($"{changeRecord.code}"));
  56. }
  57. break;
  58. case "going":
  59. var tcode = code.Replace("Vote-", "");
  60. ActivityData data;
  61. if (vote.scope == "school" || vote.scope == "teacher")
  62. {
  63. data = new ActivityData
  64. {
  65. id = vote.id,
  66. code = $"Activity-{tcode}",
  67. type = "vote",
  68. name = vote.name,
  69. startTime = vote.startTime,
  70. endTime = vote.endTime,
  71. scode = vote.code,
  72. scope = vote.scope,
  73. classes = vote.classes,
  74. tmdids = vote.tmdids,
  75. owner=vote.owner
  76. };
  77. await client.GetContainer("TEAMModelOS", "School").UpsertItemAsync<ActivityData>(data, new Azure.Cosmos.PartitionKey(data.code));
  78. }
  79. else if (vote.scope == "private")
  80. {
  81. data = new ActivityData
  82. {
  83. id = vote.id,
  84. code = $"Activity-Common",
  85. type = "vote",
  86. name = vote.name,
  87. startTime = vote.startTime,
  88. endTime = vote.endTime,
  89. scode = vote.code,
  90. scope = vote.scope,
  91. classes = vote.classes,
  92. owner = vote.owner
  93. // tmdids = vote.tmdids
  94. };
  95. await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync<ActivityData>(data, new Azure.Cosmos.PartitionKey(data.code));
  96. }
  97. var messageVoteEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = code }.ToJsonString());
  98. messageVoteEnd.ApplicationProperties.Add("name", "Vote");
  99. if (voteRecords.Count > 0)
  100. {
  101. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVoteEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
  102. await _serviceBus.GetServiceBusClient().cancelMessage("active-task", voteRecords[0].sequenceNumber);
  103. voteRecords[0].sequenceNumber = end;
  104. await _azureStorage.SaveOrUpdate<ChangeRecord>(voteRecords[0]);
  105. }
  106. else
  107. {
  108. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVoteEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
  109. ChangeRecord changeRecord = new ChangeRecord
  110. {
  111. RowKey = input.Id,
  112. PartitionKey = "going",
  113. sequenceNumber = end,
  114. msgId = messageVoteEnd.MessageId
  115. };
  116. await _azureStorage.Save<ChangeRecord>(changeRecord);
  117. }
  118. break;
  119. case "finish":
  120. //获取投票活动的所有投票记录
  121. var records= await _azureRedis.GetRedisClient(8).HashGetAllAsync($"Vote:Record:{vote.id}_{vote.code}");
  122. //获取投票活动的选项及投票数
  123. var counts= _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Vote:Count:{vote.id}_{vote.code}");
  124. if (counts != null && counts.Length > 0) {
  125. foreach (var count in counts) {
  126. vote.options.ForEach(x => {
  127. //重新赋值
  128. if (x.code.Equals(count.Element.ToString())) {
  129. x.count = (int)count.Score;
  130. }
  131. });
  132. }
  133. }
  134. List<Task<string>> tasks = new List<Task<string>>();
  135. List<dynamic> recordsBlob = new List<dynamic>();
  136. foreach (var rcd in records) {
  137. var key = rcd.Name.ToString().Split("-")[0];
  138. var value = rcd.Value.ToString().ToObject<JsonElement>();
  139. recordsBlob.Add(new { key,value});
  140. tasks.Add(_azureStorage.UploadFileByContainer(vote.owner, value.ToJsonString(), "vote", $"{vote.id}/{key}.json"));
  141. }
  142. //处理活动方的记录
  143. string url = $"vote/{vote.id}/index.json";
  144. vote.recordUrl = url;
  145. tasks.Add(_azureStorage.UploadFileByContainer(vote.owner, recordsBlob.ToJsonString(), "vote", $"{vote.id}/index.json"));
  146. //处理投票者的记录
  147. await Task.WhenAll(tasks);
  148. if (vote.scope == "school" || vote.scope == "teacher")
  149. {
  150. await client.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<Vote>(vote,vote.id, new Azure.Cosmos.PartitionKey(vote.code));
  151. }
  152. else if (vote.scope == "private")
  153. {
  154. await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<Vote>(vote, vote.id, new Azure.Cosmos.PartitionKey(vote.code));
  155. }
  156. break;
  157. }
  158. }
  159. }
  160. }
  161. }