TriggerCorrect.cs 26 KB

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