TriggerCorrect.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. {
  36. return;
  37. }
  38. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}阅卷配置【{tdata.name}-{tdata.id}-ttl={tdata.ttl}】正在执行", GroupNames.成都开发測試群組);
  39. Correct correct = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Correct>(input.Id, new Azure.Cosmos.PartitionKey($"{tdata.code}"));
  40. List<ChangeRecord> correctRecords = await _azureStorage.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", input.Id }, { "PartitionKey", correct.progress } });
  41. if (correct != null)
  42. {
  43. switch (correct.progress)
  44. {
  45. case "pending":
  46. var messageCorrect = new ServiceBusMessage(new { id = input.Id, progress = "going", code = tdata.code }.ToJsonString());
  47. messageCorrect.ApplicationProperties.Add("name", "Correct");
  48. if (correctRecords.Count > 0)
  49. {
  50. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageCorrect, DateTimeOffset.FromUnixTimeMilliseconds(tdata.stime));
  51. await _serviceBus.GetServiceBusClient().cancelMessage(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), correctRecords[0].sequenceNumber);
  52. correctRecords[0].sequenceNumber = start;
  53. await _azureStorage.SaveOrUpdate<ChangeRecord>(correctRecords[0]);
  54. }
  55. else
  56. {
  57. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageCorrect, DateTimeOffset.FromUnixTimeMilliseconds(tdata.stime));
  58. ChangeRecord changeRecord = new ChangeRecord
  59. {
  60. RowKey = input.Id,
  61. PartitionKey = "pending",
  62. sequenceNumber = start,
  63. msgId = messageCorrect.MessageId
  64. };
  65. await _azureStorage.Save<ChangeRecord>(changeRecord);
  66. }
  67. break;
  68. case "going":
  69. if (correct.subs.IsNotEmpty())
  70. {
  71. foreach (var sub in correct.subs)
  72. {
  73. ///生成阅卷教师的阅卷任务列表
  74. if (sub.markers.IsNotEmpty())
  75. {
  76. foreach (var marker in sub.markers)
  77. {
  78. CorrectTask task = new CorrectTask
  79. {
  80. ttl = -1,
  81. pk = "CorrectTask",
  82. code = "CorrectTask-" + marker.id,
  83. //评测id 或者阅卷配置id
  84. id = correct.id,
  85. //科目
  86. subject = sub.id,
  87. //科目名称
  88. subjectName = sub.name,
  89. //评测code
  90. ecode = correct.scode,
  91. //阅卷配置code
  92. scode = correct.code,
  93. //任务名称
  94. name = correct.name,
  95. //开始时间
  96. startTime = correct.startTime,
  97. //结束时间
  98. endTime = correct.endTime,
  99. //批改数量
  100. count = marker.count,
  101. //按题阅卷时,题号
  102. qu = marker.qu,
  103. //模块术
  104. model = sub.model,
  105. type = 1
  106. };
  107. await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync<CorrectTask>(task, new Azure.Cosmos.PartitionKey(task.code));
  108. }
  109. }
  110. //生成异常卷处理人员
  111. if (sub.err.IsNotEmpty())
  112. {
  113. foreach (var tId in sub.err)
  114. {
  115. CorrectTask task = new CorrectTask
  116. {
  117. ttl = -1,
  118. pk = "CorrectTask",
  119. code = "CorrectTask-" + tId,
  120. //评测id 或者阅卷配置id
  121. id = correct.id,
  122. //科目
  123. subject = sub.id,
  124. //科目名称
  125. subjectName = sub.name,
  126. //评测code
  127. ecode = correct.scode,
  128. //阅卷配置code
  129. scode = correct.code,
  130. //任务名称
  131. name = correct.name,
  132. //开始时间
  133. startTime = correct.startTime,
  134. //结束时间
  135. endTime = correct.endTime,
  136. //模块术
  137. model = sub.model,
  138. type = 2
  139. };
  140. await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync<CorrectTask>(task, new Azure.Cosmos.PartitionKey(task.code));
  141. }
  142. }
  143. //生成仲裁人员
  144. if (sub.arb.IsNotEmpty())
  145. {
  146. foreach (var tId in sub.arb)
  147. {
  148. CorrectTask task = new CorrectTask
  149. {
  150. ttl = -1,
  151. pk = "CorrectTask",
  152. code = "CorrectTask-" + tId,
  153. //评测id 或者阅卷配置id
  154. id = correct.id,
  155. //科目
  156. subject = sub.id,
  157. //科目名称
  158. subjectName = sub.name,
  159. //评测code
  160. ecode = correct.scode,
  161. //阅卷配置code
  162. scode = correct.code,
  163. //任务名称
  164. name = correct.name,
  165. //开始时间
  166. startTime = correct.startTime,
  167. //结束时间
  168. endTime = correct.endTime,
  169. //模块术
  170. model = sub.model,
  171. type = 3
  172. };
  173. await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync<CorrectTask>(task, new Azure.Cosmos.PartitionKey(task.code));
  174. }
  175. }
  176. //调用本次考试所涉及的所有已经作答的数据 并生成阅卷池,存入redis
  177. //评测id
  178. string eid = correct.id;
  179. //评测的分区键
  180. string ecode = correct.scode;
  181. //评测科目
  182. string subjectId = sub.id;
  183. //生成临时作答数据存放到redis
  184. var redisClient = _azureRedis.GetRedisClient(8);
  185. ExamInfo info = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ExamInfo>(eid, new Azure.Cosmos.PartitionKey(ecode));
  186. List<ExamClassResult> classResults = new List<ExamClassResult>();
  187. //获取原题配分
  188. int paperIndex = 0;
  189. foreach (ExamSubject subject in info.subjects)
  190. {
  191. if (subject.id.Equals(subjectId))
  192. {
  193. break;
  194. }
  195. else
  196. {
  197. paperIndex++;
  198. }
  199. }
  200. List<double> paperPoint = info.papers[paperIndex].point;
  201. if (info.scope.Equals("school"))
  202. {
  203. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryIterator<ExamClassResult>(
  204. queryText: $"select value(c) from c where c.examId = '{eid}' and c.subjectId = '{subjectId}'",
  205. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"ExamClassResult-{info.school}") }))
  206. {
  207. classResults.Add(item);
  208. }
  209. }
  210. else
  211. {
  212. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryIterator<ExamClassResult>(
  213. queryText: $"select value(c) from c where c.examId = '{eid}' and c.subjectId = '{subjectId}'",
  214. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"ExamClassResult-{info.creatorId}") }))
  215. {
  216. classResults.Add(item);
  217. }
  218. }
  219. List<Task<ItemResponse<Scoring>>> tasks = new List<Task<ItemResponse<Scoring>>>();
  220. //初始化老师阅卷记录
  221. //List<string> tmds = new List<string>();
  222. List<string> marks = new List<string>();
  223. for (int i = 0; i < correct.num; i++)
  224. {
  225. marks.Add("");
  226. }
  227. foreach (ExamClassResult examClass in classResults)
  228. {
  229. foreach (string stuId in examClass.studentIds)
  230. {
  231. int index = examClass.studentIds.IndexOf(stuId);
  232. if (index > -1)
  233. {
  234. List<double> scc = examClass.studentScores[index];
  235. List<Item> items = new List<Item>();
  236. List<Qs> qss = new List<Qs>();
  237. int itemIndex = 0;
  238. foreach (double psc in scc)
  239. {
  240. //初始化异常卷信息,初始化仲裁卷信息
  241. Qs qs = new Qs();
  242. List<Info> infos = new List<Info>();
  243. Item item = new Item
  244. {
  245. ssc = paperPoint[itemIndex],
  246. scores = infos
  247. };
  248. itemIndex++;
  249. items.Add(item);
  250. qss.Add(qs);
  251. }
  252. Scoring sc = new Scoring
  253. {
  254. id = Guid.NewGuid().ToString(),
  255. code = "Scoring-" + info.school,
  256. blob = examClass.studentAnswers[index].Count > 0 ? examClass.studentAnswers[index][0] : "",
  257. stuId = stuId,
  258. examId = eid,
  259. subjectId = subjectId,
  260. scores = examClass.studentScores[index],
  261. marks = marks,
  262. items = items,
  263. qs = qss,
  264. //endTime = correct.endTime,
  265. model = sub.model
  266. };
  267. tasks.Add(client.GetContainer("TEAMModelOS", "Teacher").CreateItemAsync<Scoring>(sc, new Azure.Cosmos.PartitionKey(sc.code)));
  268. }
  269. //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()));
  270. }
  271. }
  272. await Task.WhenAll(tasks);
  273. }
  274. }
  275. var messageCorrectEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = tdata.code }.ToJsonString());
  276. messageCorrectEnd.ApplicationProperties.Add("name", "Correct");
  277. if (correctRecords.Count > 0)
  278. {
  279. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageCorrectEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.etime));
  280. await _serviceBus.GetServiceBusClient().cancelMessage(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), correctRecords[0].sequenceNumber);
  281. correctRecords[0].sequenceNumber = end;
  282. await _azureStorage.SaveOrUpdate<ChangeRecord>(correctRecords[0]);
  283. }
  284. else
  285. {
  286. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageCorrectEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.etime));
  287. ChangeRecord changeRecord = new ChangeRecord
  288. {
  289. RowKey = input.Id,
  290. PartitionKey = "going",
  291. sequenceNumber = end,
  292. msgId = messageCorrectEnd.MessageId
  293. };
  294. await _azureStorage.Save<ChangeRecord>(changeRecord);
  295. }
  296. break;
  297. case "finish":
  298. ///暂不处理
  299. break;
  300. }
  301. }
  302. }
  303. }
  304. }