TriggerVote.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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}");
  29. _azureRedis.GetRedisClient(8).KeyDelete($"Vote:Count:{vote.id}");
  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}");
  127. //获取投票活动的选项及投票数
  128. var counts = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Vote:Count:{vote.id}");
  129. List<dynamic> countcds = new List<dynamic>();
  130. if (counts != null && counts.Length > 0)
  131. {
  132. foreach (var count in counts)
  133. {
  134. countcds.Add(new { code = count.Element.ToString(), count = (int)count.Score });
  135. }
  136. }
  137. List<Task<string>> tasks = new List<Task<string>>();
  138. List<VoteRecord> recordsBlob = new List<VoteRecord>();
  139. foreach (var rcd in records)
  140. {
  141. var value = rcd.Value.ToString().ToObject<VoteRecord>();
  142. recordsBlob.Add(value);
  143. }
  144. //分组每个人的
  145. var gp = recordsBlob.GroupBy(x => x.userid).Select(x => new { key = x.Key, list = x.ToList() });
  146. foreach (var g in gp)
  147. {
  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. tasks.Add(_azureStorage.UploadFileByContainer(vote.owner, new { options = countcds, records = recordsBlob }.ToJsonString(), "vote", $"{vote.id}/record.json"));
  153. //处理投票者的记录
  154. await Task.WhenAll(tasks);
  155. //
  156. if (string.IsNullOrEmpty(vote.recordUrl))
  157. {
  158. vote.recordUrl = url;
  159. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync<Vote>(vote, vote.id, new Azure.Cosmos.PartitionKey(vote.code));
  160. }
  161. //更新结束状态
  162. if (vote.scope == "school")
  163. {
  164. data = new ActivityData
  165. {
  166. id = vote.id,
  167. code = $"Activity-{vote.owner}",
  168. type = "vote",
  169. name = vote.name,
  170. startTime = vote.startTime,
  171. endTime = vote.endTime,
  172. scode = vote.code,
  173. scope = vote.scope,
  174. progress = "finish",
  175. classes = vote.classes.IsNotEmpty() ? vote.classes : new List<string> { "" },
  176. tmdids = vote.tmdids.IsNotEmpty() ? vote.tmdids : new List<string> { "" },
  177. owner = vote.owner,
  178. subjects = new List<string> { "" }
  179. };
  180. await client.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<ActivityData>(data, data.id, new Azure.Cosmos.PartitionKey(data.code));
  181. }
  182. else if (vote.scope == "private")
  183. {
  184. //更新结束状态
  185. data = new ActivityData
  186. {
  187. id = vote.id,
  188. code = $"Activity-Common",
  189. type = "vote",
  190. name = vote.name,
  191. startTime = vote.startTime,
  192. endTime = vote.endTime,
  193. scode = vote.code,
  194. scope = vote.scope,
  195. progress = "finish",
  196. classes = vote.classes.IsNotEmpty() ? vote.classes : new List<string> { "" },
  197. owner = vote.owner,
  198. tmdids = new List<string> { "" },
  199. subjects = new List<string> { "" }
  200. };
  201. await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<ActivityData>(data, data.id, new Azure.Cosmos.PartitionKey(data.code));
  202. }
  203. break;
  204. }
  205. }
  206. }
  207. }
  208. }