TriggerCorrect.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. namespace TEAMModelFunction
  17. {
  18. public static class TriggerCorrect
  19. {
  20. public static async void Trigger(AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, DingDing _dingDing,
  21. CosmosClient client, Document input, TriggerData tdata, AzureRedisFactory _azureRedis)
  22. {
  23. if ((tdata.status != null && tdata.status.Value == 404) || tdata.ttl > 0)
  24. {
  25. return;
  26. }
  27. var adid = tdata.id;
  28. var adcode = "";
  29. string blobcntr = null;
  30. if (tdata.scope.Equals("school"))
  31. {
  32. adcode = $"Activity-{tdata.school}";
  33. blobcntr = tdata.school;
  34. }
  35. else
  36. {
  37. return;
  38. }
  39. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}阅卷配置【{tdata.name}-{tdata.id}-ttl={tdata.ttl}】正在执行", GroupNames.成都开发測試群組);
  40. Correct correct = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<Correct>(input.Id, new Azure.Cosmos.PartitionKey($"{tdata.code}"));
  41. List<ChangeRecord> correctRecords = await _azureStorage.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", input.Id }, { "PartitionKey", correct.progress } });
  42. if (correct != null)
  43. {
  44. switch (correct.progress)
  45. {
  46. case "pending":
  47. var messageCorrect = new ServiceBusMessage(new { id = input.Id, progress = "going", code = tdata.code }.ToJsonString());
  48. messageCorrect.ApplicationProperties.Add("name", "Correct");
  49. if (correctRecords.Count > 0)
  50. {
  51. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageCorrect, DateTimeOffset.FromUnixTimeMilliseconds(tdata.stime));
  52. await _serviceBus.GetServiceBusClient().cancelMessage(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), correctRecords[0].sequenceNumber);
  53. correctRecords[0].sequenceNumber = start;
  54. await _azureStorage.SaveOrUpdate<ChangeRecord>(correctRecords[0]);
  55. }
  56. else
  57. {
  58. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageCorrect, DateTimeOffset.FromUnixTimeMilliseconds(tdata.stime));
  59. ChangeRecord changeRecord = new ChangeRecord
  60. {
  61. RowKey = input.Id,
  62. PartitionKey = "pending",
  63. sequenceNumber = start,
  64. msgId = messageCorrect.MessageId
  65. };
  66. await _azureStorage.Save<ChangeRecord>(changeRecord);
  67. }
  68. break;
  69. case "going":
  70. if (correct.subs.IsNotEmpty())
  71. {
  72. foreach (var sub in correct.subs)
  73. {
  74. ///生成阅卷教师的阅卷任务列表
  75. if (sub.markers.IsNotEmpty())
  76. {
  77. foreach (var marker in sub.markers)
  78. {
  79. CorrectTask task = new CorrectTask
  80. {
  81. ttl = -1,
  82. pk = "CorrectTask",
  83. code = "CorrectTask-" + marker.id,
  84. id = Guid.NewGuid().ToString(),
  85. //评测id 或者阅卷配置id
  86. cid = correct.id,
  87. //科目
  88. subject = sub.id,
  89. //科目名称
  90. subjectName = sub.name,
  91. //评测code
  92. ecode = correct.scode,
  93. //阅卷配置code
  94. scode = correct.code,
  95. //任务名称
  96. name = correct.name,
  97. //开始时间
  98. startTime = correct.startTime,
  99. //结束时间
  100. endTime = correct.endTime,
  101. //批改数量
  102. count = marker.count,
  103. //按题阅卷时,题号
  104. qu = marker.qu,
  105. //模块数
  106. model = sub.model,
  107. type = 1,
  108. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
  109. };
  110. await client.GetContainer(Constant.TEAMModelOS, "Teacher").UpsertItemAsync<CorrectTask>(task, new Azure.Cosmos.PartitionKey(task.code));
  111. }
  112. }
  113. //生成异常卷处理人员
  114. if (sub.err.IsNotEmpty())
  115. {
  116. foreach (var tId in sub.err)
  117. {
  118. CorrectTask task = new CorrectTask
  119. {
  120. ttl = -1,
  121. pk = "CorrectTask",
  122. code = "CorrectTask-" + tId,
  123. id = Guid.NewGuid().ToString(),
  124. //评测id 或者阅卷配置id
  125. cid = correct.id,
  126. //科目
  127. subject = sub.id,
  128. //科目名称
  129. subjectName = sub.name,
  130. //评测code
  131. ecode = correct.scode,
  132. //阅卷配置code
  133. scode = correct.code,
  134. //任务名称
  135. name = correct.name,
  136. //开始时间
  137. startTime = correct.startTime,
  138. //结束时间
  139. endTime = correct.endTime,
  140. //模块数
  141. model = sub.model,
  142. type = 2,
  143. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
  144. };
  145. await client.GetContainer(Constant.TEAMModelOS, "Teacher").UpsertItemAsync<CorrectTask>(task, new Azure.Cosmos.PartitionKey(task.code));
  146. }
  147. }
  148. //生成仲裁人员
  149. if (sub.arb.IsNotEmpty())
  150. {
  151. foreach (var tId in sub.arb)
  152. {
  153. CorrectTask task = new CorrectTask
  154. {
  155. ttl = -1,
  156. pk = "CorrectTask",
  157. code = "CorrectTask-" + tId,
  158. id = Guid.NewGuid().ToString(),
  159. //评测id 或者阅卷配置id
  160. cid = correct.id,
  161. //科目
  162. subject = sub.id,
  163. //科目名称
  164. subjectName = sub.name,
  165. //评测code
  166. ecode = correct.scode,
  167. //阅卷配置code
  168. scode = correct.code,
  169. //任务名称
  170. name = correct.name,
  171. //开始时间
  172. startTime = correct.startTime,
  173. //结束时间
  174. endTime = correct.endTime,
  175. //模块数
  176. model = sub.model,
  177. type = 3,
  178. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
  179. };
  180. await client.GetContainer(Constant.TEAMModelOS, "Teacher").UpsertItemAsync<CorrectTask>(task, new Azure.Cosmos.PartitionKey(task.code));
  181. }
  182. }
  183. //调用本次考试所涉及的所有已经作答的数据 并生成阅卷池,存入redis
  184. //评测id
  185. string eid = correct.id;
  186. //评测的分区键
  187. string ecode = correct.scode;
  188. //评测科目
  189. string subjectId = sub.id;
  190. //生成临时作答数据存放到redis
  191. var redisClient = _azureRedis.GetRedisClient(8);
  192. ExamInfo info = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<ExamInfo>(eid, new Azure.Cosmos.PartitionKey(ecode));
  193. List<ExamClassResult> classResults = new List<ExamClassResult>();
  194. //获取原题配分
  195. int paperIndex = 0;
  196. foreach (ExamSubject subject in info.subjects)
  197. {
  198. if (subject.id.Equals(subjectId))
  199. {
  200. break;
  201. }
  202. else
  203. {
  204. paperIndex++;
  205. }
  206. }
  207. List<double> paperPoint = info.papers[paperIndex].point;
  208. if (info.scope.Equals("school"))
  209. {
  210. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIterator<ExamClassResult>(
  211. queryText: $"select value(c) from c where c.examId = '{eid}' and c.subjectId = '{subjectId}'",
  212. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"ExamClassResult-{info.school}") }))
  213. {
  214. classResults.Add(item);
  215. }
  216. }
  217. else
  218. {
  219. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIterator<ExamClassResult>(
  220. queryText: $"select value(c) from c where c.examId = '{eid}' and c.subjectId = '{subjectId}'",
  221. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"ExamClassResult-{info.creatorId}") }))
  222. {
  223. classResults.Add(item);
  224. }
  225. }
  226. List<Task<ItemResponse<Scoring>>> tasks = new List<Task<ItemResponse<Scoring>>>();
  227. //初始化老师阅卷记录
  228. //List<string> tmds = new List<string>();
  229. /* List<string> marks = new List<string>();
  230. for (int i = 0; i < correct.num; i++)
  231. {
  232. marks.Add("");
  233. }*/
  234. foreach (ExamClassResult examClass in classResults)
  235. {
  236. foreach (string stuId in examClass.studentIds)
  237. {
  238. int index = examClass.studentIds.IndexOf(stuId);
  239. if (index > -1)
  240. {
  241. List<double> scc = examClass.studentScores[index];
  242. List<Item> items = new List<Item>();
  243. List<Qs> qss = new List<Qs>();
  244. int itemIndex = 0;
  245. foreach (double psc in scc)
  246. {
  247. //初始化异常卷信息,初始化仲裁卷信息
  248. Qs qs = new Qs();
  249. List<Info> infos = new List<Info>();
  250. Item item = new Item
  251. {
  252. ssc = paperPoint[itemIndex],
  253. scores = infos
  254. };
  255. itemIndex++;
  256. items.Add(item);
  257. qss.Add(qs);
  258. }
  259. Scoring sc = new Scoring
  260. {
  261. id = Guid.NewGuid().ToString(),
  262. code = "Scoring-" + info.school,
  263. blob = examClass.studentAnswers[index].Count > 0 ? examClass.studentAnswers[index][0] : "",
  264. stuId = stuId,
  265. examId = eid,
  266. subjectId = subjectId,
  267. scores = examClass.studentScores[index],
  268. count = correct.num,
  269. //marks = marks,
  270. items = items,
  271. qs = qss,
  272. //endTime = correct.endTime,
  273. model = sub.model
  274. };
  275. tasks.Add(client.GetContainer(Constant.TEAMModelOS, "Teacher").CreateItemAsync<Scoring>(sc, new Azure.Cosmos.PartitionKey(sc.code)));
  276. }
  277. //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()));
  278. }
  279. }
  280. await Task.WhenAll(tasks);
  281. }
  282. }
  283. var messageCorrectEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = tdata.code }.ToJsonString());
  284. messageCorrectEnd.ApplicationProperties.Add("name", "Correct");
  285. if (correctRecords.Count > 0)
  286. {
  287. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageCorrectEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.etime));
  288. await _serviceBus.GetServiceBusClient().cancelMessage(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), correctRecords[0].sequenceNumber);
  289. correctRecords[0].sequenceNumber = end;
  290. await _azureStorage.SaveOrUpdate<ChangeRecord>(correctRecords[0]);
  291. }
  292. else
  293. {
  294. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageCorrectEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.etime));
  295. ChangeRecord changeRecord = new ChangeRecord
  296. {
  297. RowKey = input.Id,
  298. PartitionKey = "going",
  299. sequenceNumber = end,
  300. msgId = messageCorrectEnd.MessageId
  301. };
  302. await _azureStorage.Save<ChangeRecord>(changeRecord);
  303. }
  304. break;
  305. case "finish":
  306. ///暂不处理
  307. break;
  308. }
  309. }
  310. }
  311. }
  312. }