TriggerCorrect.cs 19 KB

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