TriggerCorrect.cs 27 KB

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