TriggerCorrect.cs 12 KB

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