TriggerExam.cs 44 KB

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