TriggerCorrect.cs 27 KB

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