TriggerCorrect.cs 24 KB

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