TriggerExam.cs 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  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.Linq;
  7. using System.Text;
  8. using System.Text.Json;
  9. using System.Threading.Tasks;
  10. using TEAMModelOS.SDK.DI;
  11. using TEAMModelOS.SDK.Extension;
  12. using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
  13. using TEAMModelOS.SDK.Models;
  14. using TEAMModelOS.SDK.Models.Cosmos;
  15. using TEAMModelOS.SDK.Models.Cosmos.Common;
  16. namespace TEAMModelFunction
  17. {
  18. public class TriggerExam
  19. {
  20. public static async void Trigger(AzureCosmosFactory _azureCosmos, AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, DingDing _dingDing,
  21. CosmosClient client, Document input, string code, long stime, long etime, string school)
  22. {
  23. ExamInfo info = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ExamInfo>(input.Id, new Azure.Cosmos.PartitionKey($"{code}"));
  24. List<ExamClassResult> examClassResults = new List<ExamClassResult>();
  25. List<ExamSubject> examSubjects = new List<ExamSubject>();
  26. if (info.scope.Equals("teacher", StringComparison.OrdinalIgnoreCase) || info.scope.Equals("private", StringComparison.OrdinalIgnoreCase))
  27. {
  28. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryText: $"select value(c) from c where c.examId = '{info.id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"ExamClassResult-{info.creatorId}") }))
  29. {
  30. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  31. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  32. {
  33. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  34. {
  35. examClassResults.Add(obj.ToObject<ExamClassResult>());
  36. }
  37. }
  38. }
  39. }
  40. else
  41. {
  42. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryText: $"select value(c) from c where c.examId = '{info.id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"ExamClassResult-{school}") }))
  43. {
  44. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  45. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  46. {
  47. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  48. {
  49. examClassResults.Add(obj.ToObject<ExamClassResult>());
  50. }
  51. }
  52. }
  53. }
  54. List<ChangeRecord> records = await _azureStorage.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", input.Id }, { "PartitionKey", info.progress } });
  55. //处理科目信息
  56. List<string> sub = new List<string>();
  57. foreach (ExamSubject subject in info.subjects)
  58. {
  59. sub.Add(subject.id);
  60. }
  61. //ChangeRecord record = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ChangeRecord>(input.Id, new Azure.Cosmos.PartitionKey($"{info.progress}"));
  62. switch (info.progress)
  63. {
  64. case "pending":
  65. var message = new ServiceBusMessage(new { id = input.Id, progress = "going", code = code }.ToJsonString());
  66. message.ApplicationProperties.Add("name", "Exam");
  67. if (records.Count > 0)
  68. {
  69. await _serviceBus.GetServiceBusClient().cancelMessage("active-task", records[0].sequenceNumber);
  70. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", message, DateTimeOffset.FromUnixTimeMilliseconds(stime));
  71. records[0].sequenceNumber = start;
  72. await _azureStorage.SaveOrUpdate<ChangeRecord>(records[0]);
  73. //await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(record, record.id, new Azure.Cosmos.PartitionKey($"{record.code}"));
  74. }
  75. else
  76. {
  77. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", message, DateTimeOffset.FromUnixTimeMilliseconds(stime));
  78. ChangeRecord changeRecord = new ChangeRecord
  79. {
  80. RowKey = input.Id,
  81. PartitionKey = "pending",
  82. sequenceNumber = start,
  83. msgId = message.MessageId
  84. };
  85. await _azureStorage.Save<ChangeRecord>(changeRecord);
  86. //await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(changeRecord, new Azure.Cosmos.PartitionKey($"{changeRecord.code}"));
  87. }
  88. break;
  89. case "going":
  90. //ActivityData data;
  91. //if (info.scope == "school")
  92. //{
  93. // data = new ActivityData
  94. // {
  95. // id = info.id,
  96. // code = $"Activity-{info.school}",
  97. // type = "exam",
  98. // name = info.name,
  99. // startTime = info.startTime,
  100. // endTime = info.endTime,
  101. // scode = info.code,
  102. // scope = info.scope,
  103. // classes = info.classes.IsNotEmpty() ? info.classes : new List<string> { "" },
  104. // tmdids = new List<string> { "" },
  105. // progress = "going",
  106. // subjects = sub
  107. // };
  108. // await client.GetContainer("TEAMModelOS", "School").UpsertItemAsync<ActivityData>(data, new Azure.Cosmos.PartitionKey(data.code));
  109. //}
  110. //else if (info.scope == "private")
  111. //{
  112. // data = new ActivityData
  113. // {
  114. // id = info.id,
  115. // code = $"Activity-Common",
  116. // type = "exam",
  117. // name = info.name,
  118. // startTime = info.startTime,
  119. // endTime = info.endTime,
  120. // scode = info.code,
  121. // scope = info.scope,
  122. // progress = "going",
  123. // classes = info.classes.IsNotEmpty() ? info.classes : new List<string> { "" },
  124. // tmdids = new List<string> { "" },
  125. // subjects = sub
  126. // };
  127. // await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync<ActivityData>(data, new Azure.Cosmos.PartitionKey(data.code));
  128. //}
  129. (List<string> tmdids, List<Students> studentss) = await TriggerStuActivity.GetStuList(client, info.classes, info.school);
  130. List<StuActivity> stuActivities = new List<StuActivity>();
  131. List<StuActivity> tmdActivities = new List<StuActivity>();
  132. if (tmdids.IsNotEmpty())
  133. {
  134. tmdids.ForEach(x =>
  135. {
  136. tmdActivities.Add(new StuActivity
  137. {
  138. pk = "Activity",
  139. id = info.id,
  140. code = $"Activity-{x}",
  141. type = "Exam",
  142. name = info.name,
  143. startTime = info.startTime,
  144. endTime = info.endTime,
  145. scode = info.code,
  146. scope = info.scope,
  147. school = info.school,
  148. creatorId = info.creatorId,
  149. subjects = sub,
  150. blob = null
  151. });
  152. });
  153. }
  154. if (studentss.IsNotEmpty())
  155. {
  156. studentss.ForEach(x =>
  157. {
  158. stuActivities.Add(new StuActivity
  159. {
  160. pk = "Activity",
  161. id = info.id,
  162. code = $"Activity-{info.school}-{x.id}",
  163. type = "Exam",
  164. name = info.name,
  165. startTime = info.startTime,
  166. endTime = info.endTime,
  167. scode = info.code,
  168. scope = info.scope,
  169. school = info.school,
  170. creatorId = info.creatorId,
  171. subjects = sub,
  172. blob = null
  173. });
  174. });
  175. }
  176. await TriggerStuActivity.SaveStuActivity(client, stuActivities, tmdActivities);
  177. //向学生或醍摩豆账号发起通知
  178. #region
  179. Notice notice = new Notice()
  180. {
  181. sid = info.id,
  182. scode = info.code,
  183. spk = info.pk,
  184. scope = info.scope,
  185. school = info.school,
  186. stime = info.startTime,
  187. etime = info.endTime,
  188. creatorId = info.creatorId,
  189. stuids = studentss,
  190. tmdids = tmdids,
  191. type = "exam-join",//评测参加通知
  192. level = 1,
  193. data = new { }.ToJsonString()
  194. };
  195. var messageBlob = new ServiceBusMessage(notice.ToJsonString());
  196. messageBlob.ApplicationProperties.Add("name", "Notice");
  197. await _serviceBus.GetServiceBusClient().SendMessageAsync("active-task", messageBlob);
  198. #endregion
  199. if (examClassResults.Count == 0)
  200. {
  201. foreach (string cla in info.classes)
  202. {
  203. int m = 0;
  204. foreach (ExamSubject subject in info.subjects)
  205. {
  206. string classCode = "";
  207. if (string.IsNullOrEmpty(info.school) || !info.scope.Equals("school", StringComparison.OrdinalIgnoreCase))
  208. {
  209. classCode = "ExamClassResult-" + info.creatorId;
  210. }
  211. else
  212. {
  213. classCode = "ExamClassResult-" + info.school;
  214. }
  215. ExamClassResult result = new ExamClassResult
  216. {
  217. code = classCode,
  218. examId = info.id,
  219. id = Guid.NewGuid().ToString(),
  220. subjectId = subject.id,
  221. year = info.year,
  222. scope = info.scope
  223. };
  224. result.info.id = cla;
  225. List<string> ans = new List<string>();
  226. List<List<string>> anses = new List<List<string>>();
  227. List<double> ansPoint = new List<double>();
  228. List<string> ids = new List<string>();
  229. foreach (double p in info.papers[m].point)
  230. {
  231. //ans.Add(new List<string>());
  232. anses.Add(new List<string>());
  233. ansPoint.Add(-1);
  234. }
  235. var sresponse = await client.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(cla, new Azure.Cosmos.PartitionKey($"Class-{info.school}"));
  236. if (sresponse.Status == 200)
  237. {
  238. using var json = await JsonDocument.ParseAsync(sresponse.ContentStream);
  239. Class classroom = json.ToObject<Class>();
  240. //result.info.id = classroom.id;
  241. result.info.name = classroom.name;
  242. result.gradeId = classroom.gradeId;
  243. //处理班级人数
  244. await foreach (var item in client.GetContainer("TEAMModelOS", "Student").GetItemQueryStreamIterator(queryText: $"select c.id from c where c.classId = '{classroom.id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Base-{info.school}") }))
  245. {
  246. using var json_stu = await JsonDocument.ParseAsync(item.ContentStream);
  247. if (json_stu.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  248. {
  249. var accounts = json_stu.RootElement.GetProperty("Documents").EnumerateArray();
  250. while (accounts.MoveNext())
  251. {
  252. JsonElement account = accounts.Current;
  253. ids.Add(account.GetProperty("id").GetString());
  254. }
  255. }
  256. }
  257. }
  258. if (info.scope.Equals("private", StringComparison.OrdinalIgnoreCase))
  259. {
  260. var stuResponse = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemStreamAsync(cla, new Azure.Cosmos.PartitionKey($"StuList"));
  261. if (stuResponse.Status == 200)
  262. {
  263. using var json = await JsonDocument.ParseAsync(stuResponse.ContentStream);
  264. StuList stuList = json.ToObject<StuList>();
  265. //result.info.id = stuList.id;
  266. result.info.name = stuList.name;
  267. //处理发布对象为自选名单(个人)
  268. foreach (Students stus in stuList.students)
  269. {
  270. ids.Add(stus.id);
  271. }
  272. if (stuList.tmids.Count > 0)
  273. {
  274. foreach (string tid in stuList.tmids)
  275. {
  276. ids.Add(tid);
  277. }
  278. }
  279. }
  280. }
  281. else
  282. {
  283. var stuResponse = await client.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(cla, new Azure.Cosmos.PartitionKey($"StuList-{info.school}"));
  284. if (stuResponse.Status == 200)
  285. {
  286. using var json = await JsonDocument.ParseAsync(stuResponse.ContentStream);
  287. StuList stuList = json.ToObject<StuList>();
  288. //result.info.id = stuList.id;
  289. result.info.name = stuList.name;
  290. //处理发布对象为自选名单(校本)
  291. foreach (Students stus in stuList.students)
  292. {
  293. ids.Add(stus.id);
  294. }
  295. }
  296. }
  297. foreach (string stu in ids)
  298. {
  299. result.mark.Add("");
  300. result.studentIds.Add(stu);
  301. result.studentAnswers.Add(ans);
  302. result.studentScores.Add(ansPoint);
  303. result.ans.Add(anses);
  304. result.sum.Add(0);
  305. }
  306. //result.progress = info.progress;
  307. result.school = info.school;
  308. m++;
  309. await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(result, new Azure.Cosmos.PartitionKey($"{result.code}"));
  310. }
  311. }
  312. // 发送信息通知
  313. var messageEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = code }.ToJsonString());
  314. messageEnd.ApplicationProperties.Add("name", "Exam");
  315. if (records.Count > 0)
  316. {
  317. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
  318. await _serviceBus.GetServiceBusClient().cancelMessage("active-task", records[0].sequenceNumber);
  319. records[0].sequenceNumber = end;
  320. await _azureStorage.SaveOrUpdate<ChangeRecord>(records[0]);
  321. //await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(record, record.id, new Azure.Cosmos.PartitionKey($"{record.code}"));
  322. }
  323. else
  324. {
  325. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
  326. ChangeRecord changeRecord = new ChangeRecord
  327. {
  328. RowKey = input.Id,
  329. PartitionKey = "going",
  330. sequenceNumber = end,
  331. msgId = messageEnd.MessageId
  332. };
  333. await _azureStorage.Save<ChangeRecord>(changeRecord);
  334. //await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(changeRecord, new Azure.Cosmos.PartitionKey($"{changeRecord.code}"));
  335. }
  336. }
  337. else
  338. {
  339. //处理单科结算时科目与试卷信息匹配的问题
  340. int gno = 0;
  341. foreach (ExamSubject subject in info.subjects)
  342. {
  343. if (subject.classCount == info.classes.Count)
  344. {
  345. await createClassResultAsync(info, examClassResults, subject, gno, _azureCosmos, _dingDing, _azureStorage);
  346. }
  347. gno++;
  348. }
  349. }
  350. break;
  351. case "finish":
  352. int fno = 0;
  353. foreach (ExamSubject subject in info.subjects)
  354. {
  355. await createClassResultAsync(info, examClassResults, subject, fno, _azureCosmos, _dingDing, _azureStorage);
  356. fno++;
  357. }
  358. //计算单次考试简易统计信息
  359. List<ExamResult> examResults = new List<ExamResult>();
  360. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryIterator<ExamResult>(
  361. queryText: $"select value(c) from c where c.examId = '{info.id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"ExamResult-{info.id}") }))
  362. {
  363. examResults.Add(item);
  364. }
  365. //结算单科单班的标准差和平均分
  366. foreach (ExamClassResult classResult in examClassResults)
  367. {
  368. //标记单科单班总得分
  369. double subScore = 0;
  370. //标准差
  371. double sPowSum = 0;
  372. var scount = classResult.studentIds.Count;
  373. foreach (List<double> sc in classResult.studentScores)
  374. {
  375. subScore += sc.Sum();
  376. }
  377. foreach (string sid in classResult.studentIds)
  378. {
  379. double ssc = classResult.studentScores[classResult.studentIds.IndexOf(sid)].Sum();
  380. sPowSum += Math.Pow(ssc - scount > 0 ? Math.Round(subScore * 1.0 / scount, 2) : 0, 2);
  381. }
  382. classResult.standard = Math.Round(scount > 0 ? Math.Pow(sPowSum / scount, 0.5) : 0, 2);
  383. classResult.average = scount > 0 ? Math.Round(subScore / scount, 2) : 0;
  384. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(classResult, classResult.id, new Azure.Cosmos.PartitionKey($"{classResult.code}"));
  385. }
  386. //记录某次考试所有学生得分总分
  387. double score = 0;
  388. double allScore = 0;
  389. int stuCount = 0;
  390. //整体平均分
  391. double average = 0;
  392. //标准差
  393. double powSum = 0;
  394. List<string> losStu = new List<string>();
  395. //先与第一个值取并集
  396. if (examResults.Count > 0)
  397. {
  398. losStu.Union(examResults[0].lostStus);
  399. foreach (ExamResult examResult in examResults)
  400. {
  401. if (info.id == examResult.examId)
  402. {
  403. foreach (List<double> sc in examResult.studentScores)
  404. {
  405. score += sc.Sum();
  406. }
  407. stuCount = examResult.studentIds.Count;
  408. }
  409. //powSum += Math.Pow(score - examResult.studentIds.Count > 0 ? Math.Round(score * 1.0 / examResult.studentIds.Count, 2) : 0, 2);
  410. //取交集
  411. losStu = losStu.Intersect(examResult.lostStus).ToList();
  412. }
  413. }
  414. double NewsRateScore = stuCount > 0 ? Math.Round(score * 1.0 / stuCount, 2) : 0;
  415. foreach (PaperSimple simple in info.papers)
  416. {
  417. allScore += simple.point.Sum();
  418. }
  419. //计算全科标准差
  420. foreach (string id in examResults[0].studentIds)
  421. {
  422. double sc = 0;
  423. foreach (ExamResult result in examResults)
  424. {
  425. sc += result.studentScores[result.studentIds.IndexOf(id)].Sum();
  426. }
  427. powSum += Math.Pow(sc - NewsRateScore, 2);
  428. }
  429. info.standard = Math.Round(examResults[0].studentIds.Count > 0 ? Math.Pow(powSum / examResults[0].studentIds.Count, 0.5) : 0, 2);
  430. double NewsRate = allScore > 0 ? Math.Round(NewsRateScore / allScore * 100, 2) : 0;
  431. info.lostStu = losStu;
  432. //判断均分是否发生变化,便于实时的更新评测基本信息
  433. if (info.sRate != NewsRate || info.average != NewsRateScore)
  434. {
  435. info.sRate = NewsRate;
  436. info.average = NewsRateScore;
  437. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync<ExamInfo>(info, info.id, new Azure.Cosmos.PartitionKey(info.code));
  438. }
  439. break;
  440. }
  441. }
  442. //处理全部学生选题计数
  443. public static async Task examRecordCount(ExamInfo info, ExamSubject subject, DingDing _dingDing, int no, ExamResult result, List<ExamClassResult> classResults, AzureCosmosFactory _azureCosmos)
  444. {
  445. try
  446. {
  447. List<double> scores = new List<double>();
  448. foreach (List<double> sc in result.studentScores)
  449. {
  450. scores.Add(sc.Sum());
  451. }
  452. //确定高分组 最低分数
  453. scores.Sort((s1, s2) => { return s2.CompareTo(s1); });
  454. double rhwCount = Math.Floor(scores.Count * 0.27);
  455. double rhw = rhwCount > 0 ? scores[int.Parse(rhwCount.ToString("0"))] : 0;
  456. //确定低分组 最高分数
  457. //scores.Sort((s1, s2) => { return s1.CompareTo(s2); });
  458. double rhlCount = Math.Ceiling(scores.Count * 0.73);
  459. double rhl = rhlCount > 0 ? scores[int.Parse(rhlCount.ToString("0"))] : 0;
  460. //存放高分组学生ID
  461. List<string> phId = new List<string>();
  462. List<string> plId = new List<string>();
  463. List<List<List<string>>> opth = new List<List<List<string>>>();
  464. List<List<List<string>>> optl = new List<List<List<string>>>();
  465. await knowledgeCount(info, subject, _dingDing, no, classResults, rhwCount, rhw, rhlCount, rhl, _azureCosmos);
  466. await fieldCount(info, subject, _dingDing, no, classResults, rhwCount, rhw, rhlCount, rhl, _azureCosmos);
  467. int PHCount = 0;
  468. int PLCount = 0;
  469. foreach (ExamClassResult classResult in classResults)
  470. {
  471. if (classResult.subjectId.Equals(subject.id))
  472. {
  473. foreach (string id in classResult.studentIds)
  474. {
  475. int index = classResult.studentIds.IndexOf(id);
  476. if (classResult.studentScores[index].Sum() >= rhw && PHCount < rhwCount)
  477. {
  478. if (classResult.ans.Count > 0)
  479. {
  480. opth.Add(classResult.ans[index]);
  481. PHCount++;
  482. continue;
  483. }
  484. }
  485. if (classResult.studentScores[index].Sum() <= rhl && PLCount < (scores.Count - rhlCount))
  486. {
  487. if (classResult.ans.Count > 0)
  488. {
  489. optl.Add(classResult.ans[index]);
  490. PLCount++;
  491. continue;
  492. }
  493. }
  494. }
  495. }
  496. }
  497. result.phc = getMore(info, no, opth);
  498. result.plc = getMore(info, no, optl);
  499. }
  500. catch (Exception ex)
  501. {
  502. await _dingDing.SendBotMsg($"评测作答记录结算异常{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  503. }
  504. }
  505. public static async Task knowledgeCount(ExamInfo info, ExamSubject subject, DingDing _dingDing, int no, List<ExamClassResult> classResults,
  506. double rhwCount, double rhw, double rhlCount, double rhl, AzureCosmosFactory _azureCosmos)
  507. {
  508. try
  509. {
  510. int phcount = 0;
  511. int plcount = 0;
  512. //存放并去重知识点
  513. HashSet<string> kname = new HashSet<string>();
  514. info.papers[no].knowledge.ForEach(kno =>
  515. {
  516. kno.ForEach(k =>
  517. {
  518. kname.Add(k);
  519. });
  520. });
  521. List<string> knowledgeName = new List<string>();
  522. foreach (string cla in kname)
  523. {
  524. knowledgeName.Add(cla);
  525. }
  526. for (int k = 0; k < knowledgeName.Count; k++)
  527. {
  528. if (null == knowledgeName[k])
  529. {
  530. knowledgeName.Remove(knowledgeName[k]);
  531. }
  532. }
  533. foreach (ExamClassResult classResult in classResults)
  534. {
  535. if (classResult.subjectId.Equals(subject.id))
  536. {
  537. //List<int> phc = new List<int>();
  538. List<int> ph = new List<int>();
  539. List<int> pl = new List<int>();
  540. List<int> pc = new List<int>();
  541. List<double> persent = new List<double>();
  542. for (int i = 0; i < knowledgeName.Count; i++)
  543. {
  544. //初始化单个知识点得分
  545. double score = 0;
  546. double allScore = 0;
  547. int n = 0;
  548. int phCount = 0;
  549. int plCount = 0;
  550. int pCount = 0;
  551. foreach (List<string> str in info.papers[no].knowledge)
  552. {
  553. if (str.Contains(knowledgeName[i]))
  554. {
  555. var itemPersent = str.Count > 0 ? 1 / Convert.ToDouble(str.Count) : 0;
  556. allScore += info.papers[no].point[n] * itemPersent;
  557. foreach (string id in classResult.studentIds)
  558. {
  559. int index = classResult.studentIds.IndexOf(id);
  560. if (classResult.studentScores[index].Count > 0)
  561. {
  562. score += classResult.studentScores[index][n];
  563. if (classResult.studentScores[index].Sum() >= rhw && phcount < rhwCount)
  564. {
  565. if (classResult.studentScores[index][n] == 0)
  566. {
  567. phCount++;
  568. }
  569. phcount++;
  570. continue;
  571. }
  572. if (classResult.studentScores[index].Sum() <= rhl && plcount < (info.stuCount - rhlCount))
  573. {
  574. if (classResult.studentScores[index][n] == 0)
  575. {
  576. plCount++;
  577. }
  578. plcount++;
  579. continue;
  580. }
  581. if (classResult.studentScores[index][n] == 0)
  582. {
  583. pCount++;
  584. }
  585. }
  586. }
  587. }
  588. n++;
  589. }
  590. pc.Add(pCount);
  591. ph.Add(phCount);
  592. pl.Add(plCount);
  593. double per = classResult.studentIds.Count > 0 ? Math.Round(score / classResult.studentIds.Count, 2) : 0;
  594. persent.Add(allScore > 0 ? per / allScore : 0);
  595. }
  596. classResult.phc = ph;
  597. classResult.plc = pl;
  598. classResult.pc = pc;
  599. classResult.krate = persent;
  600. }
  601. await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(classResult, classResult.id, new Azure.Cosmos.PartitionKey($"{classResult.code}"));
  602. }
  603. }
  604. catch (Exception ex)
  605. {
  606. await _dingDing.SendBotMsg($"评测知识点结算异常{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  607. }
  608. }
  609. public static async Task fieldCount(ExamInfo info, ExamSubject subject, DingDing _dingDing, int no, List<ExamClassResult> classResults,
  610. double rhwCount, double rhw, double rhlCount, double rhl, AzureCosmosFactory _azureCosmos)
  611. {
  612. try
  613. {
  614. int phcount = 0;
  615. int plcount = 0;
  616. //存放并去重知识点
  617. List<int> knowledgeName = new List<int>();
  618. knowledgeName.Add(1);
  619. knowledgeName.Add(2);
  620. knowledgeName.Add(3);
  621. knowledgeName.Add(4);
  622. knowledgeName.Add(5);
  623. knowledgeName.Add(6);
  624. foreach (ExamClassResult classResult in classResults)
  625. {
  626. if (classResult.subjectId.Equals(subject.id))
  627. {
  628. //List<int> phc = new List<int>();
  629. List<int> ph = new List<int>();
  630. List<int> pl = new List<int>();
  631. List<int> pc = new List<int>();
  632. List<double> persent = new List<double>();
  633. for (int i = 0; i < knowledgeName.Count; i++)
  634. {
  635. //初始化单个知识点得分
  636. double score = 0;
  637. double allScore = 0;
  638. int n = 0;
  639. int phCount = 0;
  640. int plCount = 0;
  641. int pCount = 0;
  642. foreach (int str in info.papers[no].field)
  643. {
  644. if (str == knowledgeName[i])
  645. {
  646. var itemPersent = 1;
  647. allScore += info.papers[no].point[n] * itemPersent;
  648. foreach (string id in classResult.studentIds)
  649. {
  650. int index = classResult.studentIds.IndexOf(id);
  651. if (classResult.studentScores[index].Count > 0)
  652. {
  653. score += classResult.studentScores[index][n];
  654. if (classResult.studentScores[index].Sum() >= rhw && phcount < rhwCount)
  655. {
  656. if (classResult.studentScores[index][n] == 0)
  657. {
  658. phCount++;
  659. }
  660. phcount++;
  661. continue;
  662. }
  663. if (classResult.studentScores[index].Sum() <= rhl && plcount < (info.stuCount - rhlCount))
  664. {
  665. if (classResult.studentScores[index][n] == 0)
  666. {
  667. plCount++;
  668. }
  669. plcount++;
  670. continue;
  671. }
  672. if (classResult.studentScores[index][n] == 0)
  673. {
  674. pCount++;
  675. }
  676. }
  677. }
  678. }
  679. n++;
  680. }
  681. pc.Add(pCount);
  682. ph.Add(phCount);
  683. pl.Add(plCount);
  684. double per = classResult.studentIds.Count > 0 ? Math.Round(score / classResult.studentIds.Count, 2) : 0;
  685. persent.Add(allScore > 0 ? per / allScore : 0);
  686. }
  687. classResult.fphc = ph;
  688. classResult.fplc = pl;
  689. classResult.fpc = pc;
  690. classResult.frate = persent;
  691. }
  692. await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(classResult, classResult.id, new Azure.Cosmos.PartitionKey($"{classResult.code}"));
  693. }
  694. }
  695. catch (Exception ex)
  696. {
  697. await _dingDing.SendBotMsg($"评测认知层次结算异常{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  698. }
  699. }
  700. //处理选题计数
  701. public static List<Dictionary<string, int>> getMore(ExamInfo info, int no, List<List<List<string>>> list)
  702. {
  703. List<Dictionary<string, int>> recorde = new List<Dictionary<string, int>>();
  704. try
  705. {
  706. for (int i = 0; i < info.papers[no].answers.Count; i++)
  707. {
  708. if (info.papers[no].answers[i].Count <= 0)
  709. {
  710. recorde.Add(new Dictionary<string, int>());
  711. continue;
  712. }
  713. Dictionary<string, int> optCount = new Dictionary<string, int>();
  714. foreach (List<List<string>> stu in list)
  715. {
  716. if (stu.Count == info.papers[no].answers.Count)
  717. {
  718. var item = stu[i];
  719. foreach (string opt in item)
  720. {
  721. if (optCount.ContainsKey(opt))
  722. {
  723. optCount[opt] = optCount[opt] + 1;
  724. }
  725. else
  726. {
  727. optCount[opt] = 1;
  728. }
  729. }
  730. }
  731. }
  732. recorde.Add(optCount);
  733. }
  734. return recorde;
  735. }
  736. catch (Exception)
  737. {
  738. return recorde;
  739. }
  740. }
  741. public static async Task createClassResultAsync(ExamInfo info, List<ExamClassResult> examClassResults, ExamSubject subject, int no, AzureCosmosFactory _azureCosmos, DingDing _dingDing, AzureStorageFactory _azureStorage)
  742. {
  743. //保证试卷信息与科目信息同步
  744. ExamResult result = new ExamResult();
  745. //人数总和
  746. int Count = 0;
  747. int m = 0;
  748. double score = 0;
  749. //标准差
  750. double powSum = 0;
  751. double allScore = info.papers[no].point.Sum();
  752. List<ClassRange> classRanges = new List<ClassRange>();
  753. List<string> lostStu = new List<string>();
  754. List<double> csRate = new List<double>();
  755. List<List<List<string>>> opt = new List<List<List<string>>>();
  756. foreach (ExamClassResult classResult in examClassResults)
  757. {
  758. double classSrate = 0;
  759. if (classResult.subjectId.Equals(subject.id))
  760. {
  761. foreach (List<List<string>> op in classResult.ans)
  762. {
  763. opt.Add(op);
  764. }
  765. //记录缺考学生索引位置
  766. int index = 0;
  767. foreach (List<double> scores in classResult.studentScores)
  768. {
  769. List<double> newScores = new List<double>();
  770. int count = 0;
  771. foreach (double sc in scores)
  772. {
  773. newScores.Add(sc > -1 ? sc : 0);
  774. if (sc == -1)
  775. {
  776. count++;
  777. }
  778. }
  779. if (count == scores.Count)
  780. {
  781. lostStu.Add(classResult.studentIds[index]);
  782. //mcount++;
  783. }
  784. classSrate += newScores.Sum();
  785. score += newScores.Sum();
  786. result.studentScores.Add(newScores);
  787. index++;
  788. }
  789. //处理班级信息
  790. ClassRange range = new ClassRange();
  791. range.id = classResult.info.id;
  792. range.name = classResult.info.name;
  793. range.gradeId = classResult.gradeId;
  794. List<int> ran = new List<int>();
  795. int stuCount = classResult.studentIds.Count;
  796. Count += stuCount;
  797. if (m == 0)
  798. {
  799. ran.Add(0);
  800. ran.Add(stuCount - 1);
  801. }
  802. else
  803. {
  804. ran.Add(Count - stuCount);
  805. ran.Add(Count - 1);
  806. }
  807. m++;
  808. range.range = ran;
  809. classRanges.Add(range);
  810. //处理学生ID
  811. foreach (string id in classResult.studentIds)
  812. {
  813. result.studentIds.Add(id);
  814. }
  815. csRate.Add(result.studentIds.Count > 0 ? Math.Round(classSrate * 1.0 / classResult.studentIds.Count, 2) : 0 / allScore);
  816. //powSum += Math.Pow(classSrate - result.average, 2);
  817. //处理选项计数内容
  818. }
  819. }
  820. await examRecordCount(info, subject, _dingDing, no, result, examClassResults, _azureCosmos);
  821. result.record = getMore(info, no, opt);
  822. result.average = result.studentIds.Count > 0 ? Math.Round(score * 1.0 / result.studentIds.Count, 2) : 0;
  823. foreach (ExamClassResult classResult in examClassResults)
  824. {
  825. //double classSrate = 0;
  826. if (classResult.subjectId.Equals(subject.id))
  827. {
  828. foreach (string id in classResult.studentIds)
  829. {
  830. double sc = classResult.studentScores[classResult.studentIds.IndexOf(id)].Sum();
  831. powSum += Math.Pow(sc - result.average, 2);
  832. }
  833. }
  834. }
  835. result.standard = Math.Round(result.studentIds.Count > 0 ? Math.Pow(powSum / result.studentIds.Count, 0.5) : 0, 2);
  836. result.csRate = csRate;
  837. result.lostStus = lostStu;
  838. result.sRate = Math.Round(result.average / allScore * 100, 2);
  839. result.classes = classRanges;
  840. result.code = "ExamResult-" + info.id;
  841. result.school = info.school;
  842. result.id = subject.id;
  843. result.examId = info.id;
  844. result.subjectId = subject.id;
  845. result.year = info.year;
  846. result.paper = info.papers[no];
  847. //result.point = info.papers[j].point;
  848. result.scope = info.scope;
  849. result.name = info.name;
  850. result.time = info.startTime;
  851. await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Common").UpsertItemAsync(result, new Azure.Cosmos.PartitionKey($"ExamResult-{info.id}"));
  852. }
  853. }
  854. }