TriggerCorrect.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. qu = marker.qu,
  96. //模块术
  97. model = sub.model
  98. //type = correct.isArb
  99. };
  100. if (correct.isArb == 1)
  101. {
  102. task.type = 3;
  103. }
  104. else if (correct.isErr == 1)
  105. {
  106. task.type = 2;
  107. }
  108. else {
  109. task.type = 1;
  110. }
  111. await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync<CorrectTask>(task,new Azure.Cosmos.PartitionKey(task.code));
  112. }
  113. }
  114. //生成异常卷处理人员
  115. //生成仲裁人员
  116. //调用本次考试所涉及的所有已经作答的数据 并生成阅卷池,存入redis
  117. //评测id
  118. string eid = correct.id;
  119. //评测的分区键
  120. string ecode = correct.scode;
  121. //评测科目
  122. string subjectId = sub.id;
  123. //生成临时作答数据存放到redis
  124. var redisClient = _azureRedis.GetRedisClient(8);
  125. ExamInfo info = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ExamInfo>(eid, new Azure.Cosmos.PartitionKey(ecode));
  126. List<ExamClassResult> classResults = new List<ExamClassResult>();
  127. //获取原题配分
  128. int paperIndex = 0;
  129. foreach (ExamSubject subject in info.subjects) {
  130. if (subject.id.Equals(subjectId))
  131. {
  132. break;
  133. }
  134. else {
  135. paperIndex++;
  136. }
  137. }
  138. List<double> paperPoint = info.papers[paperIndex].point;
  139. if (info.scope.Equals("school"))
  140. {
  141. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryIterator<ExamClassResult>(
  142. queryText: $"select value(c) from c where c.examId = '{eid}' and c.subjectId = '{subjectId}'",
  143. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"ExamClassResult-{info.school}") }))
  144. {
  145. classResults.Add(item);
  146. }
  147. }
  148. else
  149. {
  150. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryIterator<ExamClassResult>(
  151. queryText: $"select value(c) from c where c.examId = '{eid}' and c.subjectId = '{subjectId}'",
  152. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"ExamClassResult-{info.creatorId}") }))
  153. {
  154. classResults.Add(item);
  155. }
  156. }
  157. List<Task<ItemResponse<Scoring>>> tasks = new List<Task<ItemResponse<Scoring>>>();
  158. //初始化老师阅卷记录
  159. //List<string> tmds = new List<string>();
  160. List<string> marks = new List<string>();
  161. for (int i = 0; i < correct.num; i++) {
  162. marks.Add("");
  163. }
  164. //初始化阅卷试题部分数据
  165. /*List<Item> items = new List<Item>();
  166. foreach (double psc in paperPoint) {
  167. Item item = new Item
  168. {
  169. ssc = psc,
  170. scores = infos
  171. };
  172. items.Add(item);
  173. }*/
  174. foreach (ExamClassResult examClass in classResults)
  175. {
  176. foreach (string stuId in examClass.studentIds)
  177. {
  178. int index = examClass.studentIds.IndexOf(stuId);
  179. if (index > -1) {
  180. List<double> scc = examClass.studentScores[index];
  181. List<Item> items = new List<Item>();
  182. int itemIndex = 0;
  183. foreach (double psc in scc)
  184. {
  185. List<Info> infos = new List<Info>();
  186. /* for (int i = 0; i < correct.num; i++)
  187. {
  188. Info fo = new Info
  189. {
  190. sc = psc,
  191. index = itemIndex
  192. };
  193. infos.Add(fo);
  194. }*/
  195. Item item = new Item
  196. {
  197. ssc = paperPoint[itemIndex],
  198. scores = infos
  199. };
  200. itemIndex++;
  201. items.Add(item);
  202. }
  203. Scoring sc = new Scoring
  204. {
  205. id = Guid.NewGuid().ToString(),
  206. code = "Scoring-" + info.school,
  207. blob = examClass.studentAnswers[index].Count > 0 ? examClass.studentAnswers[index][0] : "",
  208. stuId =stuId,
  209. examId = eid,
  210. subjectId = subjectId,
  211. scores = examClass.studentScores[index],
  212. //tIds = tmds,
  213. marks = marks,
  214. items = items,
  215. model = sub.model
  216. };
  217. tasks.Add(client.GetContainer("TEAMModelOS", "Teacher").CreateItemAsync<Scoring>(sc, new Azure.Cosmos.PartitionKey(sc.code)));
  218. }
  219. //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()));
  220. }
  221. }
  222. await Task.WhenAll(tasks);
  223. }
  224. }
  225. var messageCorrectEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = tdata.code }.ToJsonString());
  226. messageCorrectEnd.ApplicationProperties.Add("name", "Correct");
  227. if (correctRecords.Count > 0)
  228. {
  229. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageCorrectEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.etime));
  230. await _serviceBus.GetServiceBusClient().cancelMessage(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), correctRecords[0].sequenceNumber);
  231. correctRecords[0].sequenceNumber = end;
  232. await _azureStorage.SaveOrUpdate<ChangeRecord>(correctRecords[0]);
  233. }
  234. else
  235. {
  236. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageCorrectEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.etime));
  237. ChangeRecord changeRecord = new ChangeRecord
  238. {
  239. RowKey = input.Id,
  240. PartitionKey = "going",
  241. sequenceNumber = end,
  242. msgId = messageCorrectEnd.MessageId
  243. };
  244. await _azureStorage.Save<ChangeRecord>(changeRecord);
  245. }
  246. break;
  247. case "finish":
  248. ///暂不处理
  249. break;
  250. }
  251. }
  252. }
  253. }
  254. }