TriggerVote.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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.Linq;
  7. using System.Text;
  8. using System.Text.Json;
  9. using System.Threading.Tasks;
  10. using TEAMModelOS.SDK.DI;
  11. using TEAMModelOS.SDK.Extension;
  12. using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
  13. using TEAMModelOS.SDK.Models;
  14. using TEAMModelOS.SDK.Models.Cosmos;
  15. using TEAMModelOS.SDK.Models.Cosmos.Common.Inner;
  16. namespace TEAMModelFunction
  17. {
  18. public static class TriggerVote
  19. {
  20. public static async void Trigger(AzureCosmosFactory _azureCosmos, AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, DingDing _dingDing,
  21. CosmosClient client, Document input, string code, long stime, long etime, string school, AzureRedisFactory _azureRedis)
  22. {
  23. Vote vote = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Vote>(input.Id, new Azure.Cosmos.PartitionKey($"{code}"));
  24. List<ChangeRecord> voteRecords = await _azureStorage.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", input.Id }, { "PartitionKey", vote.progress } });
  25. if (vote.ttl >= 1)
  26. {
  27. //TODO 处理TTL删除业务
  28. _azureRedis.GetRedisClient(8).KeyDelete($"Vote:Record:{vote.id}_{vote.code}");
  29. _azureRedis.GetRedisClient(8).KeyDelete($"Vote:Count:{vote.id}_{vote.code}");
  30. return;
  31. }
  32. else {
  33. switch (vote.progress)
  34. {
  35. case "pending":
  36. var messageVote = new ServiceBusMessage(new { id = input.Id, progress = "going", code = code }.ToJsonString());
  37. messageVote.ApplicationProperties.Add("name", "Vote");
  38. if (voteRecords.Count > 0)
  39. {
  40. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVote, DateTimeOffset.FromUnixTimeMilliseconds(stime));
  41. await _serviceBus.GetServiceBusClient().cancelMessage("active-task", voteRecords[0].sequenceNumber);
  42. voteRecords[0].sequenceNumber = start;
  43. await _azureStorage.SaveOrUpdate<ChangeRecord>(voteRecords[0]);
  44. //await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(voteRecord, voteRecord.id, new Azure.Cosmos.PartitionKey($"{voteRecord.code}"));
  45. }
  46. else
  47. {
  48. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVote, DateTimeOffset.FromUnixTimeMilliseconds(stime));
  49. ChangeRecord changeRecord = new ChangeRecord
  50. {
  51. RowKey = input.Id,
  52. PartitionKey = "pending",
  53. sequenceNumber = start,
  54. msgId = messageVote.MessageId
  55. };
  56. await _azureStorage.Save<ChangeRecord>(changeRecord);
  57. //await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(changeRecord, new Azure.Cosmos.PartitionKey($"{changeRecord.code}"));
  58. }
  59. break;
  60. case "going":
  61. ActivityData data;
  62. if (vote.scope == "school" )
  63. {
  64. data = new ActivityData
  65. {
  66. id = vote.id,
  67. code = $"Activity-{vote.owner}",
  68. type = "vote",
  69. name = vote.name,
  70. startTime = vote.startTime,
  71. endTime = vote.endTime,
  72. scode = vote.code,
  73. scope = vote.scope,
  74. classes = vote.classes.IsNotEmpty() ? vote.classes : new List<string> { "" },
  75. tmdids = vote.tmdids.IsNotEmpty() ? vote.tmdids : new List<string> { "" },
  76. progress= "going",
  77. owner =vote.owner,
  78. subjects = new List<string> { "" }
  79. };
  80. await client.GetContainer("TEAMModelOS", "School").UpsertItemAsync<ActivityData>(data, new Azure.Cosmos.PartitionKey(data.code));
  81. }
  82. else if (vote.scope == "private")
  83. {
  84. data = new ActivityData
  85. {
  86. id = vote.id,
  87. code = $"Activity-Common",
  88. type = "vote",
  89. name = vote.name,
  90. startTime = vote.startTime,
  91. endTime = vote.endTime,
  92. scode = vote.code,
  93. scope = vote.scope,
  94. progress = "going",
  95. classes = vote.classes.IsNotEmpty() ? vote.classes : new List<string> { "" },
  96. owner = vote.owner,
  97. tmdids = new List<string> { "" },
  98. subjects= new List<string> { ""}
  99. };
  100. await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync<ActivityData>(data, new Azure.Cosmos.PartitionKey(data.code));
  101. }
  102. var messageVoteEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = code }.ToJsonString());
  103. messageVoteEnd.ApplicationProperties.Add("name", "Vote");
  104. if (voteRecords.Count > 0)
  105. {
  106. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVoteEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
  107. await _serviceBus.GetServiceBusClient().cancelMessage("active-task", voteRecords[0].sequenceNumber);
  108. voteRecords[0].sequenceNumber = end;
  109. await _azureStorage.SaveOrUpdate<ChangeRecord>(voteRecords[0]);
  110. }
  111. else
  112. {
  113. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVoteEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
  114. ChangeRecord changeRecord = new ChangeRecord
  115. {
  116. RowKey = input.Id,
  117. PartitionKey = "going",
  118. sequenceNumber = end,
  119. msgId = messageVoteEnd.MessageId
  120. };
  121. await _azureStorage.Save<ChangeRecord>(changeRecord);
  122. }
  123. break;
  124. case "finish":
  125. //获取投票活动的所有投票记录
  126. var records= await _azureRedis.GetRedisClient(8).HashGetAllAsync($"Vote:Record:{vote.id}_{vote.code}");
  127. //获取投票活动的选项及投票数
  128. var counts= _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Vote:Count:{vote.id}_{vote.code}");
  129. if (counts != null && counts.Length > 0) {
  130. foreach (var count in counts) {
  131. vote.options.ForEach(x => {
  132. //重新赋值
  133. if (x.code.Equals(count.Element.ToString())) {
  134. x.count = (int)count.Score;
  135. }
  136. });
  137. }
  138. }
  139. List<Task<string>> tasks = new List<Task<string>>();
  140. List<VoteRecord> recordsBlob = new List<VoteRecord>();
  141. foreach (var rcd in records) {
  142. var value = rcd.Value.ToString().ToObject<VoteRecord>();
  143. recordsBlob.Add(value);
  144. }
  145. //分组每个人的
  146. var gp= recordsBlob.GroupBy(x => x.userid).Select(x=>new { key=x.Key,list=x.ToList()});
  147. foreach (var g in gp) {
  148. tasks.Add(_azureStorage.UploadFileByContainer(vote.owner, g.list.ToJsonString(), "vote", $"{vote.id}/urecord/{g.key}.json"));
  149. }
  150. //处理活动方的记录
  151. string url = $"vote/{vote.id}/record.json";
  152. vote.recordUrl = url;
  153. tasks.Add(_azureStorage.UploadFileByContainer(vote.owner, recordsBlob.ToJsonString(), "vote", $"{vote.id}/record.json"));
  154. //处理投票者的记录
  155. await Task.WhenAll(tasks);
  156. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync<Vote>(vote, vote.id, new Azure.Cosmos.PartitionKey(vote.code));
  157. //更新结束状态
  158. if (vote.scope == "school")
  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. progress = "finish",
  171. classes = vote.classes.IsNotEmpty() ? vote.classes : new List<string> { "" },
  172. tmdids = vote.tmdids.IsNotEmpty() ? vote.tmdids : new List<string> { "" },
  173. owner = vote.owner,
  174. subjects = new List<string> { "" }
  175. };
  176. await client.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<ActivityData>(data,data.id, new Azure.Cosmos.PartitionKey(data.code));
  177. }
  178. else if (vote.scope == "private")
  179. {
  180. //更新结束状态
  181. data = new ActivityData
  182. {
  183. id = vote.id,
  184. code = $"Activity-Common",
  185. type = "vote",
  186. name = vote.name,
  187. startTime = vote.startTime,
  188. endTime = vote.endTime,
  189. scode = vote.code,
  190. scope = vote.scope,
  191. progress = "finish",
  192. classes = vote.classes.IsNotEmpty() ? vote.classes : new List<string> { "" },
  193. owner = vote.owner,
  194. tmdids = new List<string> { "" },
  195. subjects = new List<string> { "" }
  196. };
  197. await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<ActivityData>(data,data.id, new Azure.Cosmos.PartitionKey(data.code));
  198. }
  199. break;
  200. }
  201. }
  202. }
  203. }
  204. }