TriggerVote.cs 12 KB

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