TriggerCorrect.cs 15 KB

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