TriggerVote.cs 11 KB

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