ExamTrigger.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. namespace TEAMModelFunction
  13. {
  14. public class ExamTrigger
  15. {
  16. public static async void Trigger(AzureCosmosFactory _azureCosmos, AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, DingDing _dingDing,
  17. CosmosClient client, Document input ,string code,long stime,long etime, string school)
  18. {
  19. ExamInfo info = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ExamInfo>(input.Id, new Azure.Cosmos.PartitionKey($"{code}"));
  20. List<ExamClassResult> examClassResults = new List<ExamClassResult>();
  21. List<ExamSubject> examSubjects = new List<ExamSubject>();
  22. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryText: $"select value(c) from c where c.examId = '{info.id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"ExamClassResult-{school}") }))
  23. {
  24. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  25. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  26. {
  27. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  28. {
  29. examClassResults.Add(obj.ToObject<ExamClassResult>());
  30. }
  31. }
  32. }
  33. List<ChangeRecord> records = await _azureStorage.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", input.Id }, { "PartitionKey", info.progress } });
  34. //ChangeRecord record = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ChangeRecord>(input.Id, new Azure.Cosmos.PartitionKey($"{info.progress}"));
  35. switch (info.progress)
  36. {
  37. case "pending":
  38. var message = new ServiceBusMessage(new { id = input.Id, progress = "going", code = code }.ToJsonString());
  39. message.ApplicationProperties.Add("name", "Exam");
  40. if (records.Count > 0)
  41. {
  42. await _serviceBus.GetServiceBusClient().cancelMessage("active-task", records[0].sequenceNumber);
  43. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", message, DateTimeOffset.FromUnixTimeMilliseconds(stime));
  44. records[0].sequenceNumber = start;
  45. await _azureStorage.SaveOrUpdate<ChangeRecord>(records[0]);
  46. //await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(record, record.id, new Azure.Cosmos.PartitionKey($"{record.code}"));
  47. }
  48. else
  49. {
  50. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", message, DateTimeOffset.FromUnixTimeMilliseconds(stime));
  51. ChangeRecord changeRecord = new ChangeRecord
  52. {
  53. RowKey = input.Id,
  54. PartitionKey = "pending",
  55. sequenceNumber = start,
  56. msgId = message.MessageId
  57. };
  58. await _azureStorage.Save<ChangeRecord>(changeRecord);
  59. //await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(changeRecord, new Azure.Cosmos.PartitionKey($"{changeRecord.code}"));
  60. }
  61. break;
  62. case "going":
  63. if (examClassResults.Count == 0)
  64. {
  65. foreach (string cla in info.targetClassIds)
  66. {
  67. int m = 0;
  68. foreach (ExamSubject subject in info.subjects)
  69. {
  70. ExamClassResult result = new ExamClassResult
  71. {
  72. code = "ExamClassResult-" + info.school,
  73. examId = info.id,
  74. id = Guid.NewGuid().ToString(),
  75. subjectId = subject.id,
  76. year = info.year,
  77. scope = info.scope
  78. };
  79. result.info.id = cla;
  80. var sresponse = await client.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(cla, new Azure.Cosmos.PartitionKey($"Class-{info.school}"));
  81. if (sresponse.Status == 200)
  82. {
  83. using var json = await JsonDocument.ParseAsync(sresponse.ContentStream);
  84. Class classroom = json.ToObject<Class>();
  85. result.info.name = classroom.name;
  86. List<string> ans = new List<string>();
  87. List<double> ansPoint = new List<double>();
  88. foreach (double p in info.papers[m].point)
  89. {
  90. //ans.Add(new List<string>());
  91. ansPoint.Add(-1);
  92. }
  93. foreach (StudentSimple stu in classroom.students)
  94. {
  95. result.studentIds.Add(stu.id);
  96. result.studentAnswers.Add(ans);
  97. result.studentScores.Add(ansPoint);
  98. result.sum.Add(0);
  99. }
  100. }
  101. //result.progress = info.progress;
  102. result.school = info.school;
  103. m++;
  104. await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(result, new Azure.Cosmos.PartitionKey($"{result.code}"));
  105. }
  106. }
  107. // 发送信息通知
  108. var messageEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = code }.ToJsonString());
  109. messageEnd.ApplicationProperties.Add("name", "Exam");
  110. if (records.Count > 0)
  111. {
  112. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
  113. await _serviceBus.GetServiceBusClient().cancelMessage("active-task", records[0].sequenceNumber);
  114. records[0].sequenceNumber = end;
  115. await _azureStorage.SaveOrUpdate<ChangeRecord>(records[0]);
  116. //await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(record, record.id, new Azure.Cosmos.PartitionKey($"{record.code}"));
  117. }
  118. else
  119. {
  120. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
  121. ChangeRecord changeRecord = new ChangeRecord
  122. {
  123. RowKey = input.Id,
  124. PartitionKey = "going",
  125. sequenceNumber = end,
  126. msgId = messageEnd.MessageId
  127. };
  128. await _azureStorage.Save<ChangeRecord>(changeRecord);
  129. //await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(changeRecord, new Azure.Cosmos.PartitionKey($"{changeRecord.code}"));
  130. }
  131. }
  132. else
  133. {
  134. //处理单科结算时科目与试卷信息匹配的问题
  135. int gno = 0;
  136. foreach (ExamSubject subject in info.subjects)
  137. {
  138. if (subject.classCount == info.targetClassIds.Count)
  139. {
  140. await createClassResultAsync(info, examClassResults, subject, gno,_azureCosmos);
  141. }
  142. gno++;
  143. }
  144. }
  145. break;
  146. case "finish":
  147. int fno = 0;
  148. foreach (ExamSubject subject in info.subjects)
  149. {
  150. await createClassResultAsync(info, examClassResults, subject, fno, _azureCosmos);
  151. fno++;
  152. }
  153. break;
  154. }
  155. }
  156. public static async Task createClassResultAsync(ExamInfo info, List<ExamClassResult> examClassResults, ExamSubject subject, int no, AzureCosmosFactory _azureCosmos)
  157. {
  158. //保证试卷信息与科目信息同步
  159. ExamResult result = new ExamResult();
  160. //人数总和
  161. int Count = 0;
  162. int m = 0;
  163. List<ClassRange> classRanges = new List<ClassRange>();
  164. foreach (ExamClassResult classResult in examClassResults)
  165. {
  166. if (classResult.subjectId.Equals(subject.id))
  167. {
  168. foreach (List<double> scores in classResult.studentScores)
  169. {
  170. result.studentScores.Add(scores);
  171. }
  172. //处理班级信息
  173. ClassRange range = new ClassRange();
  174. range.id = classResult.info.id;
  175. range.name = classResult.info.name;
  176. List<int> ran = new List<int>();
  177. int stuCount = classResult.studentIds.Count;
  178. Count += stuCount;
  179. if (m == 0)
  180. {
  181. ran.Add(0);
  182. ran.Add(stuCount - 1);
  183. }
  184. else
  185. {
  186. ran.Add(Count - stuCount);
  187. ran.Add(Count - 1);
  188. }
  189. m++;
  190. range.range = ran;
  191. classRanges.Add(range);
  192. //处理学生ID
  193. foreach (string id in classResult.studentIds)
  194. {
  195. result.studentIds.Add(id);
  196. }
  197. }
  198. }
  199. result.classes = classRanges;
  200. result.code = "ExamResult-" + info.id;
  201. result.school = info.school;
  202. result.id = subject.id;
  203. result.examId = info.id;
  204. result.subjectId = subject.id;
  205. result.year = info.year;
  206. result.paper = info.papers[no];
  207. //result.point = info.papers[j].point;
  208. result.scope = info.scope;
  209. result.name = info.name;
  210. result.time = info.startTime;
  211. await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Common").UpsertItemAsync(result, new Azure.Cosmos.PartitionKey($"ExamResult-{info.id}"));
  212. }
  213. }
  214. }