TriggerCorrect.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. using HTEXLib.COMM.Helpers;
  16. using Azure;
  17. namespace TEAMModelOS.FunctionV4
  18. {
  19. public static class TriggerCorrect
  20. {
  21. public static async Task Trigger(AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, DingDing _dingDing,
  22. CosmosClient client, JsonElement input, TriggerData tdata, AzureRedisFactory _azureRedis)
  23. {
  24. if ((tdata.status != null && tdata.status.Value == 404) || tdata.ttl > 0)
  25. {
  26. return;
  27. }
  28. var adid = tdata.id;
  29. var adcode = "";
  30. string blobcntr = null;
  31. if (tdata.scope.Equals("school"))
  32. {
  33. adcode = $"Activity-{tdata.school}";
  34. blobcntr = tdata.school;
  35. }
  36. else
  37. {
  38. return;
  39. }
  40. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}阅卷配置【{tdata.name}-{tdata.id}-ttl={tdata.ttl}】正在执行", GroupNames.成都开发測試群組);
  41. try {
  42. Correct correct = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<Correct>(tdata.id, new Azure.Cosmos.PartitionKey($"{tdata.code}"));
  43. if (correct != null)
  44. {
  45. string PartitionKey = string.Format("{0}{1}{2}", correct.code, "-", correct.progress);
  46. List<ChangeRecord> correctRecords = await _azureStorage.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", tdata.id }, { "PartitionKey", PartitionKey } });
  47. switch (correct.progress)
  48. {
  49. case "pending":
  50. var messageCorrect = new ServiceBusMessage(new { id = tdata.id, progress = "going", code = tdata.code }.ToJsonString());
  51. messageCorrect.ApplicationProperties.Add("name", "Correct");
  52. if (correctRecords.Count > 0)
  53. {
  54. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageCorrect, DateTimeOffset.FromUnixTimeMilliseconds(tdata.startTime));
  55. await _serviceBus.GetServiceBusClient().cancelMessage(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), correctRecords[0].sequenceNumber);
  56. correctRecords[0].sequenceNumber = start;
  57. await _azureStorage.SaveOrUpdate<ChangeRecord>(correctRecords[0]);
  58. }
  59. else
  60. {
  61. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageCorrect, DateTimeOffset.FromUnixTimeMilliseconds(tdata.startTime));
  62. ChangeRecord changeRecord = new ChangeRecord
  63. {
  64. RowKey = tdata.id,
  65. PartitionKey = PartitionKey,
  66. sequenceNumber = start,
  67. msgId = messageCorrect.MessageId
  68. };
  69. await _azureStorage.Save<ChangeRecord>(changeRecord);
  70. }
  71. break;
  72. case "going":
  73. //评测id
  74. string eid = correct.id;
  75. //评测的分区键
  76. string ecode = correct.scode;
  77. ExamInfo info = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<ExamInfo>(tdata.id, new Azure.Cosmos.PartitionKey($"{ecode}"));
  78. if (correct.subs.IsNotEmpty())
  79. {
  80. foreach (var sub in correct.subs)
  81. {
  82. ///生成阅卷教师的阅卷任务列表
  83. if (sub.markers.IsNotEmpty())
  84. {
  85. foreach (var marker in sub.markers)
  86. {
  87. CorrectTask task = new CorrectTask
  88. {
  89. ttl = -1,
  90. pk = "CorrectTask",
  91. code = "CorrectTask-" + marker.id,
  92. id = Guid.NewGuid().ToString(),
  93. //评测id 或者阅卷配置id
  94. cid = correct.id,
  95. //科目
  96. subject = sub.id,
  97. //科目名称
  98. subjectName = sub.name,
  99. //评测code
  100. ecode = correct.scode,
  101. //阅卷配置code
  102. scode = correct.code,
  103. //任务名称
  104. name = correct.name,
  105. progress = "going",
  106. //开始时间
  107. startTime = correct.startTime,
  108. //结束时间
  109. endTime = correct.endTime,
  110. //批改数量
  111. count = marker.count,
  112. //按题阅卷时,题号
  113. qu = marker.qu,
  114. //模块数
  115. model = sub.model,
  116. type = 1,
  117. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  118. source = info.source
  119. };
  120. await client.GetContainer(Constant.TEAMModelOS, "Teacher").UpsertItemAsync<CorrectTask>(task, new Azure.Cosmos.PartitionKey(task.code));
  121. }
  122. }
  123. //生成异常卷处理人员
  124. if (sub.err.IsNotEmpty())
  125. {
  126. foreach (var tId in sub.err)
  127. {
  128. CorrectTask task = new CorrectTask
  129. {
  130. ttl = -1,
  131. pk = "CorrectTask",
  132. code = "CorrectTask-" + tId,
  133. id = Guid.NewGuid().ToString(),
  134. //评测id 或者阅卷配置id
  135. cid = correct.id,
  136. //科目
  137. subject = sub.id,
  138. //科目名称
  139. subjectName = sub.name,
  140. //评测code
  141. ecode = correct.scode,
  142. //阅卷配置code
  143. scode = correct.code,
  144. progress = "going",
  145. //任务名称
  146. name = correct.name,
  147. //开始时间
  148. startTime = correct.startTime,
  149. //结束时间
  150. endTime = correct.endTime,
  151. //模块数
  152. model = sub.model,
  153. type = 2,
  154. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
  155. };
  156. await client.GetContainer(Constant.TEAMModelOS, "Teacher").UpsertItemAsync<CorrectTask>(task, new Azure.Cosmos.PartitionKey(task.code));
  157. }
  158. }
  159. //生成仲裁人员
  160. if (sub.arb.IsNotEmpty())
  161. {
  162. foreach (var tId in sub.arb)
  163. {
  164. CorrectTask task = new CorrectTask
  165. {
  166. ttl = -1,
  167. pk = "CorrectTask",
  168. code = "CorrectTask-" + tId,
  169. id = Guid.NewGuid().ToString(),
  170. //评测id 或者阅卷配置id
  171. cid = correct.id,
  172. //科目
  173. subject = sub.id,
  174. //科目名称
  175. subjectName = sub.name,
  176. progress = "going",
  177. //评测code
  178. ecode = correct.scode,
  179. //阅卷配置code
  180. scode = correct.code,
  181. //任务名称
  182. name = correct.name,
  183. //开始时间
  184. startTime = correct.startTime,
  185. //结束时间
  186. endTime = correct.endTime,
  187. //模块数
  188. model = sub.model,
  189. type = 3,
  190. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
  191. };
  192. await client.GetContainer(Constant.TEAMModelOS, "Teacher").UpsertItemAsync<CorrectTask>(task, new Azure.Cosmos.PartitionKey(task.code));
  193. }
  194. }
  195. //评测科目
  196. string subjectId = sub.id;
  197. //生成临时作答数据存放到redis
  198. //var redisClient = _azureRedis.GetRedisClient(8);
  199. //ExamInfo info = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<ExamInfo>(eid, new Azure.Cosmos.PartitionKey(ecode));
  200. List<ExamClassResult> classResults = new List<ExamClassResult>();
  201. //获取原题配分
  202. int paperIndex = 0;
  203. foreach (ExamSubject subject in info.subjects)
  204. {
  205. if (subject.id.Equals(subjectId))
  206. {
  207. break;
  208. }
  209. else
  210. {
  211. paperIndex++;
  212. }
  213. }
  214. List<double> paperPoint = info.papers[paperIndex].point;
  215. List<List<string>> ans = info.papers[paperIndex].answers;
  216. if (info.scope.Equals("school"))
  217. {
  218. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIterator<ExamClassResult>(
  219. queryText: $"select value(c) from c where c.examId = '{eid}' and c.subjectId = '{subjectId}'",
  220. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"ExamClassResult-{info.school}") }))
  221. {
  222. classResults.Add(item);
  223. }
  224. }
  225. else
  226. {
  227. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIterator<ExamClassResult>(
  228. queryText: $"select value(c) from c where c.examId = '{eid}' and c.subjectId = '{subjectId}'",
  229. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"ExamClassResult-{info.creatorId}") }))
  230. {
  231. classResults.Add(item);
  232. }
  233. }
  234. List<Task<ItemResponse<SDK.Models.Cosmos.Common.Scoring>>> tasks = new List<Task<ItemResponse<SDK.Models.Cosmos.Common.Scoring>>>();
  235. //初始化老师阅卷记录
  236. //List<string> tmds = new List<string>();
  237. /* List<string> marks = new List<string>();
  238. for (int i = 0; i < correct.num; i++)
  239. {
  240. marks.Add("");
  241. }*/
  242. foreach (ExamClassResult examClass in classResults)
  243. {
  244. foreach (string stuId in examClass.studentIds)
  245. {
  246. int index = examClass.studentIds.IndexOf(stuId);
  247. if (index > -1)
  248. {
  249. List<double> scc = examClass.studentScores[index];
  250. List<Item> items = new List<Item>();
  251. List<Qs> qss = new List<Qs>();
  252. int itemIndex = 0;
  253. foreach (double psc in scc)
  254. {
  255. //初始化异常卷信息,初始化仲裁卷信息
  256. Qs qs = new Qs();
  257. List<Info> infos = new List<Info>();
  258. Item item = new Item
  259. {
  260. ssc = paperPoint[itemIndex],
  261. scores = infos
  262. };
  263. itemIndex++;
  264. items.Add(item);
  265. qss.Add(qs);
  266. }
  267. //处理学生未作答 生成阅卷数据时 客观题分数为-1的情况
  268. List<double> scores = new List<double>();
  269. int n = 0;
  270. foreach (List<string> answer in ans)
  271. {
  272. var scs = examClass.studentScores[index][n];
  273. if (answer.Count > 0)
  274. {
  275. if (scs == -1)
  276. {
  277. scores.Add(0);
  278. }
  279. else
  280. {
  281. scores.Add(scs);
  282. }
  283. }
  284. else
  285. {
  286. scores.Add(scs);
  287. }
  288. n++;
  289. }
  290. SDK.Models.Cosmos.Common.Scoring sc = new SDK.Models.Cosmos.Common.Scoring
  291. {
  292. id = Guid.NewGuid().ToString(),
  293. code = "Scoring-" + info.school,
  294. blob = examClass.studentAnswers[index].Count > 0 ? examClass.studentAnswers[index][0] : "",
  295. stuId = stuId,
  296. examId = eid,
  297. subjectId = subjectId,
  298. scores = scores,
  299. count = correct.num,
  300. //marks = marks,
  301. items = items,
  302. qs = qss,
  303. //endTime = correct.endTime,
  304. model = sub.model
  305. };
  306. tasks.Add(client.GetContainer(Constant.TEAMModelOS, "Teacher").CreateItemAsync<SDK.Models.Cosmos.Common.Scoring>(sc, new Azure.Cosmos.PartitionKey(sc.code)));
  307. }
  308. //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()));
  309. }
  310. }
  311. await Task.WhenAll(tasks);
  312. }
  313. }
  314. var messageCorrectEnd = new ServiceBusMessage(new { id = tdata.id, progress = "finish", code = tdata.code }.ToJsonString());
  315. messageCorrectEnd.ApplicationProperties.Add("name", "Correct");
  316. if (correctRecords.Count > 0)
  317. {
  318. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageCorrectEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.endTime));
  319. await _serviceBus.GetServiceBusClient().cancelMessage(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), correctRecords[0].sequenceNumber);
  320. correctRecords[0].sequenceNumber = end;
  321. await _azureStorage.SaveOrUpdate<ChangeRecord>(correctRecords[0]);
  322. }
  323. else
  324. {
  325. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageCorrectEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.endTime));
  326. ChangeRecord changeRecord = new ChangeRecord
  327. {
  328. RowKey = tdata.id,
  329. PartitionKey = PartitionKey,
  330. sequenceNumber = end,
  331. msgId = messageCorrectEnd.MessageId
  332. };
  333. await _azureStorage.Save<ChangeRecord>(changeRecord);
  334. }
  335. break;
  336. case "finish":
  337. foreach (var sub in correct.subs)
  338. {
  339. List<string> ids = new List<string>();
  340. ///阅卷教师的阅卷任务列表
  341. if (sub.markers.IsNotEmpty())
  342. {
  343. foreach (var marker in sub.markers)
  344. {
  345. ids.Add(marker.id);
  346. //await client.GetContainer(Constant.TEAMModelOS, "Teacher").UpsertItemAsync<CorrectTask>(task, new Azure.Cosmos.PartitionKey(task.code),new ItemRequestOptions().PostTriggers);
  347. }
  348. }
  349. //异常卷处理人员
  350. if (sub.err.IsNotEmpty())
  351. {
  352. foreach (var tId in sub.err)
  353. {
  354. if (!ids.Contains(tId))
  355. {
  356. ids.Add(tId);
  357. }
  358. }
  359. }
  360. //仲裁人员
  361. if (sub.arb.IsNotEmpty())
  362. {
  363. foreach (var tId in sub.arb)
  364. {
  365. if (!ids.Contains(tId))
  366. {
  367. ids.Add(tId);
  368. }
  369. }
  370. }
  371. List<CorrectTask> corrects = new List<CorrectTask>();
  372. foreach (string id in ids)
  373. {
  374. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<CorrectTask>(
  375. queryText: $"select value(c) from c where c.cid = '{correct.id}'",
  376. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"CorrectTask-{id}") }))
  377. {
  378. corrects.Add(item);
  379. }
  380. }
  381. List<Task<ItemResponse<CorrectTask>>> tasks = new List<Task<ItemResponse<CorrectTask>>>();
  382. foreach (CorrectTask task in corrects)
  383. {
  384. task.progress = "finish";
  385. tasks.Add(client.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync(task, task.id, new Azure.Cosmos.PartitionKey(task.code)));
  386. }
  387. await Task.WhenAll(tasks);
  388. }
  389. break;
  390. }
  391. }
  392. }
  393. catch (CosmosException e)
  394. {
  395. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-CosmosDB异常{e.Message}\n{e.Status}", GroupNames.成都开发測試群組);
  396. }
  397. catch (Exception e)
  398. {
  399. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-阅卷异常{e.Message}\n{e.StackTrace}", GroupNames.成都开发測試群組);
  400. }
  401. }
  402. }
  403. }