TriggerExam.cs 45 KB

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