TriggerCorrect.cs 19 KB

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