ExamTrigger.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. string classCode = "";
  71. if (string.IsNullOrEmpty(info.school) || !info.scope.Equals("school",StringComparison.OrdinalIgnoreCase))
  72. {
  73. classCode = "ExamClassResult-" + info.creatorId;
  74. }
  75. else {
  76. classCode = "ExamClassResult-" + info.school;
  77. }
  78. ExamClassResult result = new ExamClassResult
  79. {
  80. code = classCode,
  81. examId = info.id,
  82. id = Guid.NewGuid().ToString(),
  83. subjectId = subject.id,
  84. year = info.year,
  85. scope = info.scope
  86. };
  87. result.info.id = cla;
  88. if (info.scope.Equals("private", StringComparison.OrdinalIgnoreCase))
  89. {
  90. var sresponse = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemStreamAsync(cla, new Azure.Cosmos.PartitionKey($"Class-{info.creatorId}"));
  91. if (sresponse.Status == 200)
  92. {
  93. using var json = await JsonDocument.ParseAsync(sresponse.ContentStream);
  94. Class classroom = json.ToObject<Class>();
  95. result.info.name = classroom.name;
  96. result.gradeId = classroom.gradeId;
  97. List<string> ans = new List<string>();
  98. List<double> ansPoint = new List<double>();
  99. foreach (double p in info.papers[m].point)
  100. {
  101. //ans.Add(new List<string>());
  102. ansPoint.Add(-1);
  103. }
  104. foreach (StudentSimple stu in classroom.students)
  105. {
  106. result.studentIds.Add(stu.id);
  107. result.studentAnswers.Add(ans);
  108. result.studentScores.Add(ansPoint);
  109. result.sum.Add(0);
  110. }
  111. }
  112. }
  113. else {
  114. var sresponse = await client.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(cla, new Azure.Cosmos.PartitionKey($"Class-{info.school}"));
  115. if (sresponse.Status == 200)
  116. {
  117. using var json = await JsonDocument.ParseAsync(sresponse.ContentStream);
  118. Class classroom = json.ToObject<Class>();
  119. result.info.name = classroom.name;
  120. result.gradeId = classroom.gradeId;
  121. List<string> ans = new List<string>();
  122. List<double> ansPoint = new List<double>();
  123. foreach (double p in info.papers[m].point)
  124. {
  125. //ans.Add(new List<string>());
  126. ansPoint.Add(-1);
  127. }
  128. foreach (StudentSimple stu in classroom.students)
  129. {
  130. result.studentIds.Add(stu.id);
  131. result.studentAnswers.Add(ans);
  132. result.studentScores.Add(ansPoint);
  133. result.sum.Add(0);
  134. }
  135. }
  136. }
  137. //result.progress = info.progress;
  138. result.school = info.school;
  139. m++;
  140. await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(result, new Azure.Cosmos.PartitionKey($"{result.code}"));
  141. }
  142. }
  143. // 发送信息通知
  144. var messageEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = code }.ToJsonString());
  145. messageEnd.ApplicationProperties.Add("name", "Exam");
  146. if (records.Count > 0)
  147. {
  148. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
  149. await _serviceBus.GetServiceBusClient().cancelMessage("active-task", records[0].sequenceNumber);
  150. records[0].sequenceNumber = end;
  151. await _azureStorage.SaveOrUpdate<ChangeRecord>(records[0]);
  152. //await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(record, record.id, new Azure.Cosmos.PartitionKey($"{record.code}"));
  153. }
  154. else
  155. {
  156. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
  157. ChangeRecord changeRecord = new ChangeRecord
  158. {
  159. RowKey = input.Id,
  160. PartitionKey = "going",
  161. sequenceNumber = end,
  162. msgId = messageEnd.MessageId
  163. };
  164. await _azureStorage.Save<ChangeRecord>(changeRecord);
  165. //await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(changeRecord, new Azure.Cosmos.PartitionKey($"{changeRecord.code}"));
  166. }
  167. }
  168. else
  169. {
  170. //处理单科结算时科目与试卷信息匹配的问题
  171. int gno = 0;
  172. foreach (ExamSubject subject in info.subjects)
  173. {
  174. if (subject.classCount == info.targetClassIds.Count)
  175. {
  176. await createClassResultAsync(info, examClassResults, subject, gno,_azureCosmos);
  177. }
  178. gno++;
  179. }
  180. }
  181. break;
  182. case "finish":
  183. int fno = 0;
  184. foreach (ExamSubject subject in info.subjects)
  185. {
  186. await createClassResultAsync(info, examClassResults, subject, fno, _azureCosmos);
  187. fno++;
  188. }
  189. break;
  190. }
  191. }
  192. public static async Task createClassResultAsync(ExamInfo info, List<ExamClassResult> examClassResults, ExamSubject subject, int no, AzureCosmosFactory _azureCosmos)
  193. {
  194. //保证试卷信息与科目信息同步
  195. ExamResult result = new ExamResult();
  196. //人数总和
  197. int Count = 0;
  198. int m = 0;
  199. List<ClassRange> classRanges = new List<ClassRange>();
  200. foreach (ExamClassResult classResult in examClassResults)
  201. {
  202. if (classResult.subjectId.Equals(subject.id))
  203. {
  204. foreach (List<double> scores in classResult.studentScores)
  205. {
  206. result.studentScores.Add(scores);
  207. }
  208. //处理班级信息
  209. ClassRange range = new ClassRange();
  210. range.id = classResult.info.id;
  211. range.name = classResult.info.name;
  212. range.gradeId = classResult.gradeId;
  213. List<int> ran = new List<int>();
  214. int stuCount = classResult.studentIds.Count;
  215. Count += stuCount;
  216. if (m == 0)
  217. {
  218. ran.Add(0);
  219. ran.Add(stuCount - 1);
  220. }
  221. else
  222. {
  223. ran.Add(Count - stuCount);
  224. ran.Add(Count - 1);
  225. }
  226. m++;
  227. range.range = ran;
  228. classRanges.Add(range);
  229. //处理学生ID
  230. foreach (string id in classResult.studentIds)
  231. {
  232. result.studentIds.Add(id);
  233. }
  234. }
  235. }
  236. result.classes = classRanges;
  237. result.code = "ExamResult-" + info.id;
  238. result.school = info.school;
  239. result.id = subject.id;
  240. result.examId = info.id;
  241. result.subjectId = subject.id;
  242. result.year = info.year;
  243. result.paper = info.papers[no];
  244. //result.point = info.papers[j].point;
  245. result.scope = info.scope;
  246. result.name = info.name;
  247. result.time = info.startTime;
  248. await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Common").UpsertItemAsync(result, new Azure.Cosmos.PartitionKey($"ExamResult-{info.id}"));
  249. }
  250. }
  251. }