VoteTrigger.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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.Text;
  7. using System.Text.Json;
  8. using System.Threading.Tasks;
  9. using TEAMModelOS.SDK.DI;
  10. using TEAMModelOS.SDK.Extension;
  11. using TEAMModelOS.SDK.Models;
  12. using TEAMModelOS.SDK.Models.Cosmos.Student;
  13. namespace TEAMModelFunction
  14. {
  15. public static class VoteTrigger
  16. {
  17. public static async void Trigger(AzureCosmosFactory _azureCosmos, AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, DingDing _dingDing,
  18. CosmosClient client, Document input, string code, long stime, long etime, string school)
  19. {
  20. Vote vote = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Vote>(input.Id, new Azure.Cosmos.PartitionKey($"{code}"));
  21. List<ChangeRecord> voteRecords = await _azureStorage.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", input.Id }, { "PartitionKey", vote.progress } });
  22. //ChangeRecord voteRecord = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ChangeRecord>(input.Id, new Azure.Cosmos.PartitionKey($"{vote.progress}"));
  23. switch (vote.progress)
  24. {
  25. case "pending":
  26. var messageVote = new ServiceBusMessage(new { id = input.Id, progress = "going", code = code }.ToJsonString());
  27. messageVote.ApplicationProperties.Add("name", "Vote");
  28. if (voteRecords.Count > 0)
  29. {
  30. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVote, DateTimeOffset.FromUnixTimeMilliseconds(stime));
  31. await _serviceBus.GetServiceBusClient().cancelMessage("active-task", voteRecords[0].sequenceNumber);
  32. voteRecords[0].sequenceNumber = start;
  33. await _azureStorage.SaveOrUpdate<ChangeRecord>(voteRecords[0]);
  34. //await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(voteRecord, voteRecord.id, new Azure.Cosmos.PartitionKey($"{voteRecord.code}"));
  35. }
  36. else
  37. {
  38. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVote, DateTimeOffset.FromUnixTimeMilliseconds(stime));
  39. ChangeRecord changeRecord = new ChangeRecord
  40. {
  41. RowKey = input.Id,
  42. PartitionKey = "pending",
  43. sequenceNumber = start,
  44. msgId = messageVote.MessageId
  45. };
  46. await _azureStorage.Save<ChangeRecord>(changeRecord);
  47. //await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(changeRecord, new Azure.Cosmos.PartitionKey($"{changeRecord.code}"));
  48. }
  49. break;
  50. case "going":
  51. var tcode = code.Replace("Vote-", "");
  52. //处理教师可投票信息初始化
  53. if (vote.teachers != null && vote.scope == "school")
  54. {
  55. List<CommonData> teacherDatas = new List<CommonData>();
  56. vote.teachers.ForEach(x => {
  57. teacherDatas.Add(new CommonData
  58. {
  59. id = vote.id,
  60. code = $"Common-{x}",
  61. type = "vote",
  62. name = vote.name,
  63. startTime = vote.startTime,
  64. endTime = vote.endTime,
  65. scode = vote.code,
  66. // spcode = tcode
  67. });
  68. });
  69. List<Task<ItemResponse<CommonData>>> tasks = new List<Task<ItemResponse<CommonData>>>();
  70. foreach (var data in teacherDatas) {
  71. tasks.Add(client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync<CommonData>(data, new Azure.Cosmos.PartitionKey(data.code)));
  72. }
  73. var response= await Task.WhenAll(tasks);
  74. }
  75. //处理学校教室或私人教师可投票信息初始化
  76. if (vote.classes != null) {
  77. List<Task<ItemResponse<CommonData>>> tasks = new List<Task<ItemResponse<CommonData>>>();
  78. //学校教室
  79. if (vote.scope == "school")
  80. {
  81. List<CommonData> classDatas = new List<CommonData>();
  82. foreach (var clazz in vote.classes) {
  83. var response = await client.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(clazz, new Azure.Cosmos.PartitionKey($"Class-{tcode}"));
  84. if (response.Status == 200) {
  85. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  86. Class clss = json.ToObject<Class>();
  87. if (clss.students != null && clss.students.Count > 0) {
  88. clss.students.ForEach(x => {
  89. classDatas.Add(new CommonData
  90. {
  91. id = vote.id,
  92. code = $"Common-{x.id}",
  93. type = "vote",
  94. name = vote.name,
  95. startTime = vote.startTime,
  96. endTime = vote.endTime,
  97. scode = vote.code,
  98. // spcode = x.scode
  99. });
  100. });
  101. }
  102. }
  103. }
  104. foreach (var data in classDatas)
  105. {
  106. tasks.Add(client.GetContainer("TEAMModelOS", "Student").UpsertItemAsync<CommonData>(data, new Azure.Cosmos.PartitionKey(data.code)));
  107. }
  108. }
  109. //私人教室
  110. else if (vote.scope == "private") {
  111. foreach (var clazz in vote.classes)
  112. {
  113. var response = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemStreamAsync(clazz, new Azure.Cosmos.PartitionKey($"Class-{tcode}"));
  114. if (response.Status == 200)
  115. {
  116. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  117. Class clss = json.ToObject<Class>();
  118. //处理私人教室的学生数据来源问题
  119. //从不同学校的学生账号
  120. if (clss.source == 1) {
  121. List<CommonData> classDatas = new List<CommonData>();
  122. if (clss.students != null && clss.students.Count > 0)
  123. {
  124. clss.students.ForEach(x => {
  125. classDatas.Add(new CommonData
  126. {
  127. id = vote.id,
  128. code = $"Common-{x.id}",
  129. type = "vote",
  130. name = vote.name,
  131. startTime = vote.startTime,
  132. endTime = vote.endTime,
  133. scode = vote.code,
  134. // spcode = x.scode
  135. });
  136. });
  137. foreach (var data in classDatas)
  138. {
  139. tasks.Add(client.GetContainer("TEAMModelOS", "Student").UpsertItemAsync<CommonData>(data, new Azure.Cosmos.PartitionKey(data.code)));
  140. }
  141. }
  142. }
  143. //从扫码加入的醍摩豆ID
  144. if (clss.source == 2) {
  145. List<CommonData> classDatas = new List<CommonData>();
  146. if (clss.students != null && clss.students.Count > 0)
  147. {
  148. clss.students.ForEach(x => {
  149. classDatas.Add(new CommonData
  150. {
  151. id = vote.id,
  152. code = $"Common-{x.id}",
  153. type = "vote",
  154. name = vote.name,
  155. startTime = vote.startTime,
  156. endTime = vote.endTime,
  157. scode = vote.code,
  158. //spcode = x.scode
  159. }) ;
  160. });
  161. foreach (var data in classDatas)
  162. {
  163. tasks.Add(client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync<CommonData>(data, new Azure.Cosmos.PartitionKey(data.code)));
  164. }
  165. }
  166. }
  167. }
  168. }
  169. }
  170. var task = await Task.WhenAll(tasks);
  171. }
  172. var messageVoteEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = code }.ToJsonString());
  173. messageVoteEnd.ApplicationProperties.Add("name", "Vote");
  174. if (voteRecords.Count > 0)
  175. {
  176. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVoteEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
  177. await _serviceBus.GetServiceBusClient().cancelMessage("active-task", voteRecords[0].sequenceNumber);
  178. voteRecords[0].sequenceNumber = end;
  179. await _azureStorage.SaveOrUpdate<ChangeRecord>(voteRecords[0]);
  180. //await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(voteRecord, voteRecord.id, new Azure.Cosmos.PartitionKey($"{voteRecord.code}"));
  181. }
  182. else
  183. {
  184. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVoteEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
  185. ChangeRecord changeRecord = new ChangeRecord
  186. {
  187. RowKey = input.Id,
  188. PartitionKey = "going",
  189. sequenceNumber = end,
  190. msgId = messageVoteEnd.MessageId
  191. };
  192. await _azureStorage.Save<ChangeRecord>(changeRecord);
  193. //await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(changeRecord, new Azure.Cosmos.PartitionKey($"{changeRecord.code}"));
  194. }
  195. break;
  196. case "finish":
  197. break;
  198. }
  199. }
  200. }
  201. }