TriggerVote.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. if (vote.ttl >= 1)
  23. {
  24. //TODO 处理TTL删除业务
  25. _azureRedis.GetRedisClient(8).KeyDelete($"Vote:Record:{vote.id}_{vote.code}");
  26. _azureRedis.GetRedisClient(8).KeyDelete($"Vote:Count:{vote.id}_{vote.code}");
  27. return;
  28. }
  29. else {
  30. switch (vote.progress)
  31. {
  32. case "pending":
  33. var messageVote = new ServiceBusMessage(new { id = input.Id, progress = "going", code = code }.ToJsonString());
  34. messageVote.ApplicationProperties.Add("name", "Vote");
  35. if (voteRecords.Count > 0)
  36. {
  37. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVote, DateTimeOffset.FromUnixTimeMilliseconds(stime));
  38. await _serviceBus.GetServiceBusClient().cancelMessage("active-task", voteRecords[0].sequenceNumber);
  39. voteRecords[0].sequenceNumber = start;
  40. await _azureStorage.SaveOrUpdate<ChangeRecord>(voteRecords[0]);
  41. //await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(voteRecord, voteRecord.id, new Azure.Cosmos.PartitionKey($"{voteRecord.code}"));
  42. }
  43. else
  44. {
  45. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVote, DateTimeOffset.FromUnixTimeMilliseconds(stime));
  46. ChangeRecord changeRecord = new ChangeRecord
  47. {
  48. RowKey = input.Id,
  49. PartitionKey = "pending",
  50. sequenceNumber = start,
  51. msgId = messageVote.MessageId
  52. };
  53. await _azureStorage.Save<ChangeRecord>(changeRecord);
  54. //await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(changeRecord, new Azure.Cosmos.PartitionKey($"{changeRecord.code}"));
  55. }
  56. break;
  57. case "going":
  58. ActivityData data;
  59. if (vote.scope == "school" || vote.scope == "teacher")
  60. {
  61. data = new ActivityData
  62. {
  63. id = vote.id,
  64. code = $"Activity-{vote.owner}",
  65. type = "vote",
  66. name = vote.name,
  67. startTime = vote.startTime,
  68. endTime = vote.endTime,
  69. scode = vote.code,
  70. scope = vote.scope,
  71. classes = vote.classes,
  72. tmdids = vote.tmdids,
  73. progress= "going",
  74. owner =vote.owner
  75. };
  76. await client.GetContainer("TEAMModelOS", "School").UpsertItemAsync<ActivityData>(data, new Azure.Cosmos.PartitionKey(data.code));
  77. }
  78. else if (vote.scope == "private")
  79. {
  80. data = new ActivityData
  81. {
  82. id = vote.id,
  83. code = $"Activity-Common",
  84. type = "vote",
  85. name = vote.name,
  86. startTime = vote.startTime,
  87. endTime = vote.endTime,
  88. scode = vote.code,
  89. scope = vote.scope,
  90. progress = "going",
  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. //更新结束状态
  157. if (vote.scope == "school" || vote.scope == "teacher")
  158. {
  159. data = new ActivityData
  160. {
  161. id = vote.id,
  162. code = $"Activity-{vote.owner}",
  163. type = "vote",
  164. name = vote.name,
  165. startTime = vote.startTime,
  166. endTime = vote.endTime,
  167. scode = vote.code,
  168. scope = vote.scope,
  169. classes = vote.classes,
  170. tmdids = vote.tmdids,
  171. progress = "finish",
  172. owner = vote.owner
  173. };
  174. await client.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<ActivityData>(data,data.id, new Azure.Cosmos.PartitionKey(data.code));
  175. }
  176. else if (vote.scope == "private")
  177. {
  178. //更新结束状态
  179. data = new ActivityData
  180. {
  181. id = vote.id,
  182. code = $"Activity-Common",
  183. type = "vote",
  184. name = vote.name,
  185. startTime = vote.startTime,
  186. endTime = vote.endTime,
  187. scode = vote.code,
  188. scope = vote.scope,
  189. progress = "finish",
  190. classes = vote.classes,
  191. owner = vote.owner
  192. // tmdids = vote.tmdids
  193. };
  194. await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<ActivityData>(data,data.id, new Azure.Cosmos.PartitionKey(data.code));
  195. }
  196. break;
  197. }
  198. }
  199. }
  200. }
  201. }