TriggerExam.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 TriggerExam
  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. if (info.scope.Equals("teacher", StringComparison.OrdinalIgnoreCase) || info.scope.Equals("private", StringComparison.OrdinalIgnoreCase))
  23. {
  24. 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-{info.creatorId}") }))
  25. {
  26. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  27. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  28. {
  29. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  30. {
  31. examClassResults.Add(obj.ToObject<ExamClassResult>());
  32. }
  33. }
  34. }
  35. }
  36. else {
  37. 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}") }))
  38. {
  39. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  40. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  41. {
  42. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  43. {
  44. examClassResults.Add(obj.ToObject<ExamClassResult>());
  45. }
  46. }
  47. }
  48. }
  49. List<ChangeRecord> records = await _azureStorage.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", input.Id }, { "PartitionKey", info.progress } });
  50. //ChangeRecord record = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ChangeRecord>(input.Id, new Azure.Cosmos.PartitionKey($"{info.progress}"));
  51. switch (info.progress)
  52. {
  53. case "pending":
  54. var message = new ServiceBusMessage(new { id = input.Id, progress = "going", code = code }.ToJsonString());
  55. message.ApplicationProperties.Add("name", "Exam");
  56. if (records.Count > 0)
  57. {
  58. await _serviceBus.GetServiceBusClient().cancelMessage("active-task", records[0].sequenceNumber);
  59. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", message, DateTimeOffset.FromUnixTimeMilliseconds(stime));
  60. records[0].sequenceNumber = start;
  61. await _azureStorage.SaveOrUpdate<ChangeRecord>(records[0]);
  62. //await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(record, record.id, new Azure.Cosmos.PartitionKey($"{record.code}"));
  63. }
  64. else
  65. {
  66. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", message, DateTimeOffset.FromUnixTimeMilliseconds(stime));
  67. ChangeRecord changeRecord = new ChangeRecord
  68. {
  69. RowKey = input.Id,
  70. PartitionKey = "pending",
  71. sequenceNumber = start,
  72. msgId = message.MessageId
  73. };
  74. await _azureStorage.Save<ChangeRecord>(changeRecord);
  75. //await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(changeRecord, new Azure.Cosmos.PartitionKey($"{changeRecord.code}"));
  76. }
  77. break;
  78. case "going":
  79. if (examClassResults.Count == 0)
  80. {
  81. foreach (string cla in info.targetClassIds)
  82. {
  83. int m = 0;
  84. foreach (ExamSubject subject in info.subjects)
  85. {
  86. string classCode = "";
  87. if (string.IsNullOrEmpty(info.school) || !info.scope.Equals("school",StringComparison.OrdinalIgnoreCase))
  88. {
  89. classCode = "ExamClassResult-" + info.creatorId;
  90. }
  91. else {
  92. classCode = "ExamClassResult-" + info.school;
  93. }
  94. ExamClassResult result = new ExamClassResult
  95. {
  96. code = classCode,
  97. examId = info.id,
  98. id = Guid.NewGuid().ToString(),
  99. subjectId = subject.id,
  100. year = info.year,
  101. scope = info.scope
  102. };
  103. result.info.id = cla;
  104. if (info.scope.Equals("private", StringComparison.OrdinalIgnoreCase))
  105. {
  106. var sresponse = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemStreamAsync(cla, new Azure.Cosmos.PartitionKey($"Class-{info.creatorId}"));
  107. if (sresponse.Status == 200)
  108. {
  109. using var json = await JsonDocument.ParseAsync(sresponse.ContentStream);
  110. Class classroom = json.ToObject<Class>();
  111. result.info.name = classroom.name;
  112. result.gradeId = classroom.gradeId;
  113. List<string> ans = new List<string>();
  114. List<double> ansPoint = new List<double>();
  115. foreach (double p in info.papers[m].point)
  116. {
  117. //ans.Add(new List<string>());
  118. ansPoint.Add(-1);
  119. }
  120. foreach (StudentSimple stu in classroom.students)
  121. {
  122. result.studentIds.Add(stu.id);
  123. result.studentAnswers.Add(ans);
  124. result.studentScores.Add(ansPoint);
  125. result.sum.Add(0);
  126. }
  127. }
  128. }
  129. else {
  130. var sresponse = await client.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(cla, new Azure.Cosmos.PartitionKey($"Class-{info.school}"));
  131. if (sresponse.Status == 200)
  132. {
  133. using var json = await JsonDocument.ParseAsync(sresponse.ContentStream);
  134. Class classroom = json.ToObject<Class>();
  135. result.info.name = classroom.name;
  136. result.gradeId = classroom.gradeId;
  137. List<string> ans = new List<string>();
  138. List<double> ansPoint = new List<double>();
  139. foreach (double p in info.papers[m].point)
  140. {
  141. //ans.Add(new List<string>());
  142. ansPoint.Add(-1);
  143. }
  144. foreach (StudentSimple stu in classroom.students)
  145. {
  146. result.studentIds.Add(stu.id);
  147. result.studentAnswers.Add(ans);
  148. result.studentScores.Add(ansPoint);
  149. result.sum.Add(0);
  150. }
  151. }
  152. }
  153. //result.progress = info.progress;
  154. result.school = info.school;
  155. m++;
  156. await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(result, new Azure.Cosmos.PartitionKey($"{result.code}"));
  157. }
  158. }
  159. // 发送信息通知
  160. var messageEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = code }.ToJsonString());
  161. messageEnd.ApplicationProperties.Add("name", "Exam");
  162. if (records.Count > 0)
  163. {
  164. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
  165. await _serviceBus.GetServiceBusClient().cancelMessage("active-task", records[0].sequenceNumber);
  166. records[0].sequenceNumber = end;
  167. await _azureStorage.SaveOrUpdate<ChangeRecord>(records[0]);
  168. //await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(record, record.id, new Azure.Cosmos.PartitionKey($"{record.code}"));
  169. }
  170. else
  171. {
  172. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
  173. ChangeRecord changeRecord = new ChangeRecord
  174. {
  175. RowKey = input.Id,
  176. PartitionKey = "going",
  177. sequenceNumber = end,
  178. msgId = messageEnd.MessageId
  179. };
  180. await _azureStorage.Save<ChangeRecord>(changeRecord);
  181. //await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(changeRecord, new Azure.Cosmos.PartitionKey($"{changeRecord.code}"));
  182. }
  183. }
  184. else
  185. {
  186. //处理单科结算时科目与试卷信息匹配的问题
  187. int gno = 0;
  188. foreach (ExamSubject subject in info.subjects)
  189. {
  190. if (subject.classCount == info.targetClassIds.Count)
  191. {
  192. await createClassResultAsync(info, examClassResults, subject, gno,_azureCosmos);
  193. }
  194. gno++;
  195. }
  196. }
  197. break;
  198. case "finish":
  199. int fno = 0;
  200. foreach (ExamSubject subject in info.subjects)
  201. {
  202. await createClassResultAsync(info, examClassResults, subject, fno, _azureCosmos);
  203. fno++;
  204. }
  205. break;
  206. }
  207. }
  208. public static async Task createClassResultAsync(ExamInfo info, List<ExamClassResult> examClassResults, ExamSubject subject, int no, AzureCosmosFactory _azureCosmos)
  209. {
  210. //保证试卷信息与科目信息同步
  211. ExamResult result = new ExamResult();
  212. //人数总和
  213. int Count = 0;
  214. int m = 0;
  215. List<ClassRange> classRanges = new List<ClassRange>();
  216. foreach (ExamClassResult classResult in examClassResults)
  217. {
  218. if (classResult.subjectId.Equals(subject.id))
  219. {
  220. foreach (List<double> scores in classResult.studentScores)
  221. {
  222. List<double> newScores = new List<double>();
  223. foreach (double sc in scores) {
  224. newScores.Add(sc > -1 ? sc : 0);
  225. }
  226. result.studentScores.Add(newScores);
  227. }
  228. //处理班级信息
  229. ClassRange range = new ClassRange();
  230. range.id = classResult.info.id;
  231. range.name = classResult.info.name;
  232. range.gradeId = classResult.gradeId;
  233. List<int> ran = new List<int>();
  234. int stuCount = classResult.studentIds.Count;
  235. Count += stuCount;
  236. if (m == 0)
  237. {
  238. ran.Add(0);
  239. ran.Add(stuCount - 1);
  240. }
  241. else
  242. {
  243. ran.Add(Count - stuCount);
  244. ran.Add(Count - 1);
  245. }
  246. m++;
  247. range.range = ran;
  248. classRanges.Add(range);
  249. //处理学生ID
  250. foreach (string id in classResult.studentIds)
  251. {
  252. result.studentIds.Add(id);
  253. }
  254. }
  255. }
  256. result.classes = classRanges;
  257. result.code = "ExamResult-" + info.id;
  258. result.school = info.school;
  259. result.id = subject.id;
  260. result.examId = info.id;
  261. result.subjectId = subject.id;
  262. result.year = info.year;
  263. result.paper = info.papers[no];
  264. //result.point = info.papers[j].point;
  265. result.scope = info.scope;
  266. result.name = info.name;
  267. result.time = info.startTime;
  268. await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Common").UpsertItemAsync(result, new Azure.Cosmos.PartitionKey($"ExamResult-{info.id}"));
  269. }
  270. }
  271. }