TriggerVote.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. ActivityData data;
  60. if (vote.scope == "school" || vote.scope == "teacher")
  61. {
  62. data = new ActivityData
  63. {
  64. id = vote.id,
  65. code = $"Activity-{vote.owner}",
  66. type = "vote",
  67. name = vote.name,
  68. startTime = vote.startTime,
  69. endTime = vote.endTime,
  70. scode = vote.code,
  71. scope = vote.scope,
  72. classes = vote.classes,
  73. tmdids = vote.tmdids,
  74. progress= "going",
  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. progress = "going",
  92. classes = vote.classes,
  93. owner = vote.owner
  94. // tmdids = vote.tmdids
  95. };
  96. await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync<ActivityData>(data, new Azure.Cosmos.PartitionKey(data.code));
  97. }
  98. var messageVoteEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = code }.ToJsonString());
  99. messageVoteEnd.ApplicationProperties.Add("name", "Vote");
  100. if (voteRecords.Count > 0)
  101. {
  102. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVoteEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
  103. await _serviceBus.GetServiceBusClient().cancelMessage("active-task", voteRecords[0].sequenceNumber);
  104. voteRecords[0].sequenceNumber = end;
  105. await _azureStorage.SaveOrUpdate<ChangeRecord>(voteRecords[0]);
  106. }
  107. else
  108. {
  109. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVoteEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
  110. ChangeRecord changeRecord = new ChangeRecord
  111. {
  112. RowKey = input.Id,
  113. PartitionKey = "going",
  114. sequenceNumber = end,
  115. msgId = messageVoteEnd.MessageId
  116. };
  117. await _azureStorage.Save<ChangeRecord>(changeRecord);
  118. }
  119. break;
  120. case "finish":
  121. //获取投票活动的所有投票记录
  122. var records= await _azureRedis.GetRedisClient(8).HashGetAllAsync($"Vote:Record:{vote.id}_{vote.code}");
  123. //获取投票活动的选项及投票数
  124. var counts= _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Vote:Count:{vote.id}_{vote.code}");
  125. if (counts != null && counts.Length > 0) {
  126. foreach (var count in counts) {
  127. vote.options.ForEach(x => {
  128. //重新赋值
  129. if (x.code.Equals(count.Element.ToString())) {
  130. x.count = (int)count.Score;
  131. }
  132. });
  133. }
  134. }
  135. List<Task<string>> tasks = new List<Task<string>>();
  136. List<dynamic> recordsBlob = new List<dynamic>();
  137. foreach (var rcd in records) {
  138. var key = rcd.Name.ToString().Split("-")[0];
  139. var value = rcd.Value.ToString().ToObject<JsonElement>();
  140. recordsBlob.Add(new { key,value});
  141. tasks.Add(_azureStorage.UploadFileByContainer(vote.owner, value.ToJsonString(), "vote", $"{vote.id}/{key}.json"));
  142. }
  143. //处理活动方的记录
  144. string url = $"vote/{vote.id}/index.json";
  145. vote.recordUrl = url;
  146. tasks.Add(_azureStorage.UploadFileByContainer(vote.owner, recordsBlob.ToJsonString(), "vote", $"{vote.id}/index.json"));
  147. //处理投票者的记录
  148. await Task.WhenAll(tasks);
  149. if (vote.scope == "school" || vote.scope == "teacher")
  150. {
  151. await client.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<Vote>(vote,vote.id, new Azure.Cosmos.PartitionKey(vote.code));
  152. }
  153. else if (vote.scope == "private")
  154. {
  155. await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<Vote>(vote, vote.id, new Azure.Cosmos.PartitionKey(vote.code));
  156. }
  157. //更新结束状态
  158. if (vote.scope == "school" || vote.scope == "teacher")
  159. {
  160. data = new ActivityData
  161. {
  162. id = vote.id,
  163. code = $"Activity-{vote.owner}",
  164. type = "vote",
  165. name = vote.name,
  166. startTime = vote.startTime,
  167. endTime = vote.endTime,
  168. scode = vote.code,
  169. scope = vote.scope,
  170. classes = vote.classes,
  171. tmdids = vote.tmdids,
  172. progress = "finish",
  173. owner = vote.owner
  174. };
  175. await client.GetContainer("TEAMModelOS", "School").UpsertItemAsync<ActivityData>(data, new Azure.Cosmos.PartitionKey(data.code));
  176. }
  177. else if (vote.scope == "private")
  178. {
  179. //更新结束状态
  180. data = new ActivityData
  181. {
  182. id = vote.id,
  183. code = $"Activity-Common",
  184. type = "vote",
  185. name = vote.name,
  186. startTime = vote.startTime,
  187. endTime = vote.endTime,
  188. scode = vote.code,
  189. scope = vote.scope,
  190. progress = "finish",
  191. classes = vote.classes,
  192. owner = vote.owner
  193. // tmdids = vote.tmdids
  194. };
  195. await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync<ActivityData>(data, new Azure.Cosmos.PartitionKey(data.code));
  196. }
  197. break;
  198. }
  199. }
  200. }
  201. }
  202. }