TriggerVote.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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(AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, DingDing _dingDing,
  21. CosmosClient client, Document input, TriggerData tdata, AzureRedisFactory _azureRedis)
  22. {
  23. if ((tdata.status != null && tdata.status.Value == 404)||tdata.ttl>0)
  24. {
  25. return;
  26. }
  27. var adid = tdata.id;
  28. var adcode = "";
  29. if (tdata.scope == "school")
  30. {
  31. adcode = $"Activity-{tdata.school}";
  32. }
  33. else if (tdata.scope == "private")
  34. {
  35. adcode = $"Activity-{tdata.creatorId}";
  36. }
  37. ActivityData data = null;
  38. try
  39. {
  40. if (tdata.scope == "school")
  41. {
  42. data = await client.GetContainer("TEAMModelOS", "School").ReadItemAsync<ActivityData>(adid, new Azure.Cosmos.PartitionKey($"{adcode}"));
  43. }
  44. else if (tdata.scope == "private")
  45. {
  46. data = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemAsync<ActivityData>(adid, new Azure.Cosmos.PartitionKey($"{adcode}"));
  47. }
  48. }
  49. catch
  50. {
  51. data = null;
  52. }
  53. await _dingDing.SendBotMsg($"投票活动【{tdata.name}-{tdata.id}-ttl={tdata.ttl}】正在操作", GroupNames.成都开发測試群組);
  54. Vote vote = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Vote>(input.Id, new Azure.Cosmos.PartitionKey($"{tdata.code}"));
  55. List<ChangeRecord> voteRecords = await _azureStorage.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", input.Id }, { "PartitionKey", vote.progress } });
  56. if (vote != null) {
  57. switch (vote.progress)
  58. {
  59. case "pending":
  60. var messageVote = new ServiceBusMessage(new { id = input.Id, progress = "going", code = tdata.code }.ToJsonString());
  61. messageVote.ApplicationProperties.Add("name", "Vote");
  62. if (voteRecords.Count > 0)
  63. {
  64. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVote, DateTimeOffset.FromUnixTimeMilliseconds(tdata.stime));
  65. await _serviceBus.GetServiceBusClient().cancelMessage("active-task", voteRecords[0].sequenceNumber);
  66. voteRecords[0].sequenceNumber = start;
  67. await _azureStorage.SaveOrUpdate<ChangeRecord>(voteRecords[0]);
  68. }
  69. else
  70. {
  71. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVote, DateTimeOffset.FromUnixTimeMilliseconds(tdata.stime));
  72. ChangeRecord changeRecord = new ChangeRecord
  73. {
  74. RowKey = input.Id,
  75. PartitionKey = "pending",
  76. sequenceNumber = start,
  77. msgId = messageVote.MessageId
  78. };
  79. await _azureStorage.Save<ChangeRecord>(changeRecord);
  80. }
  81. break;
  82. case "going":
  83. if (vote.scope == "school")
  84. {
  85. data = new ActivityData
  86. {
  87. id = vote.id,
  88. code = $"Activity-{vote.school}",
  89. type = "vote",
  90. name = vote.name,
  91. startTime = vote.startTime,
  92. endTime = vote.endTime,
  93. scode = vote.code,
  94. scope = vote.scope,
  95. classes = vote.classes.IsNotEmpty() ? vote.classes : new List<string> { "" },
  96. tmdids = vote.tmdids.IsNotEmpty() ? vote.tmdids : new List<string> { "" },
  97. progress = "going",
  98. subjects = new List<string> { "" }
  99. };
  100. await client.GetContainer("TEAMModelOS", "School").UpsertItemAsync<ActivityData>(data, new Azure.Cosmos.PartitionKey(data.code));
  101. }
  102. else if (vote.scope == "private")
  103. {
  104. data = new ActivityData
  105. {
  106. id = vote.id,
  107. code = $"Activity-Common",
  108. type = "vote",
  109. name = vote.name,
  110. startTime = vote.startTime,
  111. endTime = vote.endTime,
  112. scode = vote.code,
  113. scope = vote.scope,
  114. progress = "going",
  115. classes = vote.classes.IsNotEmpty() ? vote.classes : new List<string> { "" },
  116. tmdids = new List<string> { "" },
  117. subjects = new List<string> { "" }
  118. };
  119. await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync<ActivityData>(data, new Azure.Cosmos.PartitionKey(data.code));
  120. }
  121. var messageVoteEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = tdata.code }.ToJsonString());
  122. messageVoteEnd.ApplicationProperties.Add("name", "Vote");
  123. if (voteRecords.Count > 0)
  124. {
  125. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVoteEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.etime));
  126. await _serviceBus.GetServiceBusClient().cancelMessage("active-task", voteRecords[0].sequenceNumber);
  127. voteRecords[0].sequenceNumber = end;
  128. await _azureStorage.SaveOrUpdate<ChangeRecord>(voteRecords[0]);
  129. }
  130. else
  131. {
  132. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVoteEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.etime));
  133. ChangeRecord changeRecord = new ChangeRecord
  134. {
  135. RowKey = input.Id,
  136. PartitionKey = "going",
  137. sequenceNumber = end,
  138. msgId = messageVoteEnd.MessageId
  139. };
  140. await _azureStorage.Save<ChangeRecord>(changeRecord);
  141. }
  142. await _dingDing.SendBotMsg($"投票活动{tdata.id}将于:{tdata.etime}完成并结算!", GroupNames.成都开发測試群組);
  143. break;
  144. case "finish":
  145. await _dingDing.SendBotMsg($"投票活动{tdata.id}开始结算{tdata.etime}!", GroupNames.成都开发測試群組);
  146. //获取投票活动的所有投票记录
  147. var records = await _azureRedis.GetRedisClient(8).HashGetAllAsync($"Vote:Record:{vote.id}");
  148. //获取投票活动的选项及投票数
  149. var counts = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Vote:Count:{vote.id}");
  150. List<dynamic> countcds = new List<dynamic>();
  151. if (counts != null && counts.Length > 0)
  152. {
  153. foreach (var count in counts)
  154. {
  155. countcds.Add(new { code = count.Element.ToString(), count = (int)count.Score });
  156. }
  157. }
  158. List<Task<string>> tasks = new List<Task<string>>();
  159. List<VoteRecord> recordsBlob = new List<VoteRecord>();
  160. foreach (var rcd in records)
  161. {
  162. var value = rcd.Value.ToString().ToObject<VoteRecord>();
  163. recordsBlob.Add(value);
  164. }
  165. //分组每个人的
  166. var gp = recordsBlob.GroupBy(x => x.userid).Select(x => new { key = x.Key, list = x.ToList() });
  167. foreach (var g in gp)
  168. {
  169. tasks.Add(_azureStorage.UploadFileByContainer(vote.blobcntr, g.list.ToJsonString(), "vote", $"{vote.id}/urecord/{g.key}.json"));
  170. }
  171. //处理活动方的记录,
  172. string url = $"/vote/{vote.id}/record.json";
  173. tasks.Add(_azureStorage.UploadFileByContainer(vote.blobcntr, new { options = countcds, records = recordsBlob }.ToJsonString(), "vote", $"{vote.id}/record.json"));
  174. //处理投票者的记录
  175. if (string.IsNullOrEmpty(vote.recordUrl))
  176. {
  177. vote.recordUrl = url;
  178. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync<Vote>(vote, vote.id, new Azure.Cosmos.PartitionKey(vote.code));
  179. }
  180. else {
  181. //异动,且已经有结算记录则不必再继续。
  182. _azureRedis.GetRedisClient(8).KeyDelete($"Vote:Record:{vote.id}");
  183. _azureRedis.GetRedisClient(8).KeyDelete($"Vote:Count:{vote.id}");
  184. break;
  185. }
  186. await Task.WhenAll(tasks);
  187. data.progress = "finish";
  188. //更新结束状态
  189. if (vote.scope == "school")
  190. {
  191. await client.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<ActivityData>(data, data.id, new Azure.Cosmos.PartitionKey(data.code));
  192. }
  193. else if (vote.scope == "private")
  194. {
  195. await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<ActivityData>(data, data.id, new Azure.Cosmos.PartitionKey(data.code));
  196. }
  197. //_azureRedis.GetRedisClient(8).KeyDelete($"Vote:Record:{vote.id}");
  198. //_azureRedis.GetRedisClient(8).KeyDelete($"Vote:Count:{vote.id}");
  199. break;
  200. }
  201. }
  202. }
  203. }
  204. }