TriggerCorrect.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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.Json;
  7. using System.Threading.Tasks;
  8. using TEAMModelOS.SDK.DI;
  9. using TEAMModelOS.SDK.Extension;
  10. using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
  11. using TEAMModelOS.SDK.Models;
  12. using TEAMModelOS.SDK.Models.Cosmos;
  13. namespace TEAMModelFunction
  14. {
  15. public static class TriggerCorrect
  16. {
  17. public static async void Trigger(AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, DingDing _dingDing,
  18. CosmosClient client, Document input, TriggerData tdata, AzureRedisFactory _azureRedis)
  19. {
  20. if ((tdata.status != null && tdata.status.Value == 404) || tdata.ttl > 0)
  21. {
  22. return;
  23. }
  24. var adid = tdata.id;
  25. var adcode = "";
  26. string blobcntr = null;
  27. if (tdata.scope == "school")
  28. {
  29. adcode = $"Activity-{tdata.school}";
  30. blobcntr = tdata.school;
  31. }
  32. else {
  33. return;
  34. }
  35. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}阅卷配置【{tdata.name}-{tdata.id}-ttl={tdata.ttl}】正在执行", GroupNames.成都开发測試群組);
  36. Correct correct = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Correct>(input.Id, new Azure.Cosmos.PartitionKey($"{tdata.code}"));
  37. List<ChangeRecord> correctRecords = await _azureStorage.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", input.Id }, { "PartitionKey", correct.progress } });
  38. if (correct != null) {
  39. switch (correct.progress) {
  40. case "pending":
  41. var messageCorrect = new ServiceBusMessage(new { id = input.Id, progress = "going", code = tdata.code }.ToJsonString());
  42. messageCorrect.ApplicationProperties.Add("name", "Correct");
  43. if (correctRecords.Count > 0)
  44. {
  45. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageCorrect, DateTimeOffset.FromUnixTimeMilliseconds(tdata.stime));
  46. await _serviceBus.GetServiceBusClient().cancelMessage(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), correctRecords[0].sequenceNumber);
  47. correctRecords[0].sequenceNumber = start;
  48. await _azureStorage.SaveOrUpdate<ChangeRecord>(correctRecords[0]);
  49. }
  50. else
  51. {
  52. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageCorrect, DateTimeOffset.FromUnixTimeMilliseconds(tdata.stime));
  53. ChangeRecord changeRecord = new ChangeRecord
  54. {
  55. RowKey = input.Id,
  56. PartitionKey = "pending",
  57. sequenceNumber = start,
  58. msgId = messageCorrect.MessageId
  59. };
  60. await _azureStorage.Save<ChangeRecord>(changeRecord);
  61. }
  62. break;
  63. case "going":
  64. if (correct.subs.IsNotEmpty()) {
  65. foreach (var sub in correct.subs) {
  66. ///生成阅卷教师的阅卷任务列表
  67. if (sub.markers.IsNotEmpty()) {
  68. foreach (var marker in sub.markers) {
  69. CorrectTask task = new CorrectTask
  70. {
  71. ttl = -1,
  72. pk= "CorrectTask",
  73. code = "CorrectTask-" + marker.id,
  74. //评测id 或者阅卷配置id
  75. id = correct.id,
  76. //科目
  77. subject=sub.id,
  78. //科目名称
  79. subjectName=sub.name,
  80. //评测code
  81. ecode=correct.scode,
  82. //阅卷配置code
  83. scode=correct.code,
  84. //任务名称
  85. name=correct.name,
  86. //开始时间
  87. startTime=correct.startTime,
  88. //结束时间
  89. endTime=correct.endTime,
  90. //批改数量
  91. count=marker.count
  92. };
  93. await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync<CorrectTask>(task,new Azure.Cosmos.PartitionKey(task.code));
  94. }
  95. }
  96. //生成异常卷处理人员
  97. //生成仲裁人员
  98. //调用本次考试所涉及的所有已经作答的数据 并生成阅卷池,存入redis
  99. //评测id
  100. string eid = correct.id;
  101. //评测的分区键
  102. string ecode = correct.scode;
  103. //评测科目
  104. string subjectId = sub.id;
  105. //生成临时作答数据存放到redis
  106. var redisClient = _azureRedis.GetRedisClient(8);
  107. ExamInfo info = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ExamInfo>(eid, new Azure.Cosmos.PartitionKey(ecode));
  108. List<ExamClassResult> classResults = new List<ExamClassResult>();
  109. if (info.scope.Equals("school"))
  110. {
  111. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryIterator<ExamClassResult>(
  112. queryText: $"select value(c) from c where c.examId = '{eid}' and c.subjectId = '{subjectId}'",
  113. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"ExamClassResult-{info.school}") }))
  114. {
  115. classResults.Add(item);
  116. }
  117. }
  118. else
  119. {
  120. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryIterator<ExamClassResult>(
  121. queryText: $"select value(c) from c where c.examId = '{eid}' and c.subjectId = '{subjectId}'",
  122. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"ExamClassResult-{info.creatorId}") }))
  123. {
  124. classResults.Add(item);
  125. }
  126. }
  127. List<Task<bool>> tasks = new List<Task<bool>>();
  128. foreach (ExamClassResult examClass in classResults)
  129. {
  130. foreach (string stuId in examClass.studentIds)
  131. {
  132. int index = examClass.studentIds.IndexOf(stuId);
  133. tasks.Add(redisClient.HashSetAsync($"Exam:Scoring:{eid}-{subjectId}", stuId, new { tmdId = "" , ans = examClass.studentAnswers[index][0], score = examClass.studentScores[index] }.ToJsonString()));
  134. }
  135. }
  136. await Task.WhenAll(tasks);
  137. }
  138. }
  139. var messageCorrectEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = tdata.code }.ToJsonString());
  140. messageCorrectEnd.ApplicationProperties.Add("name", "Correct");
  141. if (correctRecords.Count > 0)
  142. {
  143. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageCorrectEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.etime));
  144. await _serviceBus.GetServiceBusClient().cancelMessage(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), correctRecords[0].sequenceNumber);
  145. correctRecords[0].sequenceNumber = end;
  146. await _azureStorage.SaveOrUpdate<ChangeRecord>(correctRecords[0]);
  147. }
  148. else
  149. {
  150. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageCorrectEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.etime));
  151. ChangeRecord changeRecord = new ChangeRecord
  152. {
  153. RowKey = input.Id,
  154. PartitionKey = "going",
  155. sequenceNumber = end,
  156. msgId = messageCorrectEnd.MessageId
  157. };
  158. await _azureStorage.Save<ChangeRecord>(changeRecord);
  159. }
  160. break;
  161. case "finish":
  162. ///暂不处理
  163. break;
  164. }
  165. }
  166. }
  167. }
  168. }