TriggerExam.cs 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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. subScore += sc.Sum();
  338. }
  339. foreach (string sid in classResult.studentIds)
  340. {
  341. double ssc = classResult.studentScores[classResult.studentIds.IndexOf(sid)].Sum();
  342. sPowSum += Math.Pow(ssc - scount > 0 ? Math.Round(subScore * 1.0 / scount, 2) : 0, 2);
  343. }
  344. classResult.standard = Math.Round(scount > 0 ? Math.Pow(sPowSum / scount, 0.5) : 0, 2);
  345. classResult.average = scount > 0 ? Math.Round(subScore / scount, 2) : 0;
  346. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(classResult, classResult.id, new Azure.Cosmos.PartitionKey($"{classResult.code}"));
  347. }
  348. //记录某次考试所有学生得分总分
  349. double score = 0;
  350. double allScore = 0;
  351. int stuCount = 0;
  352. //整体平均分
  353. double average = 0;
  354. //标准差
  355. double powSum = 0;
  356. List<string> losStu = new List<string>();
  357. //先与第一个值取并集
  358. if (examResults.Count > 0)
  359. {
  360. losStu.Union(examResults[0].lostStus);
  361. foreach (ExamResult examResult in examResults)
  362. {
  363. if (info.id == examResult.examId)
  364. {
  365. foreach (List<double> sc in examResult.studentScores)
  366. {
  367. score += sc.Sum();
  368. }
  369. stuCount = examResult.studentIds.Count;
  370. }
  371. //powSum += Math.Pow(score - examResult.studentIds.Count > 0 ? Math.Round(score * 1.0 / examResult.studentIds.Count, 2) : 0, 2);
  372. //取交集
  373. losStu = losStu.Intersect(examResult.lostStus).ToList();
  374. }
  375. }
  376. double NewsRateScore = stuCount > 0 ? Math.Round(score * 1.0 / stuCount, 2) : 0;
  377. foreach (PaperSimple simple in info.papers)
  378. {
  379. allScore += simple.point.Sum();
  380. }
  381. //计算全科标准差
  382. foreach (string id in examResults[0].studentIds)
  383. {
  384. double sc = 0;
  385. foreach (ExamResult result in examResults)
  386. {
  387. sc += result.studentScores[result.studentIds.IndexOf(id)].Sum();
  388. }
  389. powSum += Math.Pow(sc - NewsRateScore, 2);
  390. }
  391. info.standard = Math.Round(examResults[0].studentIds.Count > 0 ? Math.Pow(powSum / examResults[0].studentIds.Count, 0.5) : 0, 2);
  392. double NewsRate = allScore > 0 ? Math.Round(NewsRateScore / allScore * 100, 2) : 0;
  393. info.lostStu = losStu;
  394. //判断均分是否发生变化,便于实时的更新评测基本信息
  395. if (info.sRate != NewsRate || info.average != NewsRateScore)
  396. {
  397. info.sRate = NewsRate;
  398. info.average = NewsRateScore;
  399. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync<ExamInfo>(info, info.id, new Azure.Cosmos.PartitionKey(info.code));
  400. }
  401. break;
  402. }
  403. }
  404. //处理全部学生选题计数
  405. public static async Task examRecordCount(ExamInfo info, ExamSubject subject, DingDing _dingDing, int no, ExamResult result, List<ExamClassResult> classResults, AzureCosmosFactory _azureCosmos)
  406. {
  407. try
  408. {
  409. List<double> scores = new List<double>();
  410. foreach (List<double> sc in result.studentScores)
  411. {
  412. scores.Add(sc.Sum());
  413. }
  414. //确定高分组 最低分数
  415. scores.Sort((s1, s2) => { return s2.CompareTo(s1); });
  416. double rhwCount = Math.Floor(scores.Count * 0.27);
  417. double rhw = rhwCount > 0 ? scores[int.Parse(rhwCount.ToString("0"))] : 0;
  418. //确定低分组 最高分数
  419. //scores.Sort((s1, s2) => { return s1.CompareTo(s2); });
  420. double rhlCount = Math.Ceiling(scores.Count * 0.73);
  421. double rhl = rhlCount > 0 ? scores[int.Parse(rhlCount.ToString("0"))] : 0;
  422. //存放高分组学生ID
  423. List<string> phId = new List<string>();
  424. List<string> plId = new List<string>();
  425. List<List<List<string>>> opth = new List<List<List<string>>>();
  426. List<List<List<string>>> optl = new List<List<List<string>>>();
  427. await knowledgeCount(info, subject, _dingDing, no, classResults, rhwCount, rhw, rhlCount, rhl, _azureCosmos);
  428. await fieldCount(info, subject, _dingDing, no, classResults, rhwCount, rhw, rhlCount, rhl, _azureCosmos);
  429. int PHCount = 0;
  430. int PLCount = 0;
  431. foreach (ExamClassResult classResult in classResults)
  432. {
  433. if (classResult.subjectId.Equals(subject.id))
  434. {
  435. foreach (string id in classResult.studentIds)
  436. {
  437. int index = classResult.studentIds.IndexOf(id);
  438. if (classResult.studentScores[index].Sum() >= rhw && PHCount < rhwCount)
  439. {
  440. if (classResult.ans.Count > 0)
  441. {
  442. opth.Add(classResult.ans[index]);
  443. PHCount++;
  444. continue;
  445. }
  446. }
  447. if (classResult.studentScores[index].Sum() <= rhl && PLCount < (scores.Count - rhlCount))
  448. {
  449. if (classResult.ans.Count > 0)
  450. {
  451. optl.Add(classResult.ans[index]);
  452. PLCount++;
  453. continue;
  454. }
  455. }
  456. }
  457. }
  458. }
  459. result.phc = getMore(info, no, opth);
  460. result.plc = getMore(info, no, optl);
  461. }
  462. catch (Exception ex)
  463. {
  464. await _dingDing.SendBotMsg($"评测作答记录结算异常{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  465. }
  466. }
  467. public static async Task knowledgeCount(ExamInfo info, ExamSubject subject, DingDing _dingDing, int no, List<ExamClassResult> classResults,
  468. double rhwCount, double rhw, double rhlCount, double rhl, AzureCosmosFactory _azureCosmos)
  469. {
  470. try
  471. {
  472. int phcount = 0;
  473. int plcount = 0;
  474. //存放并去重知识点
  475. HashSet<string> kname = new HashSet<string>();
  476. info.papers[no].knowledge.ForEach(kno =>
  477. {
  478. kno.ForEach(k =>
  479. {
  480. kname.Add(k);
  481. });
  482. });
  483. List<string> knowledgeName = new List<string>();
  484. foreach (string cla in kname)
  485. {
  486. knowledgeName.Add(cla);
  487. }
  488. for (int k = 0; k < knowledgeName.Count; k++)
  489. {
  490. if (null == knowledgeName[k])
  491. {
  492. knowledgeName.Remove(knowledgeName[k]);
  493. }
  494. }
  495. foreach (ExamClassResult classResult in classResults)
  496. {
  497. if (classResult.subjectId.Equals(subject.id))
  498. {
  499. //List<int> phc = new List<int>();
  500. List<int> ph = new List<int>();
  501. List<int> pl = new List<int>();
  502. List<int> pc = new List<int>();
  503. List<double> persent = new List<double>();
  504. for (int i = 0; i < knowledgeName.Count; i++)
  505. {
  506. //初始化单个知识点得分
  507. double score = 0;
  508. double allScore = 0;
  509. int n = 0;
  510. int phCount = 0;
  511. int plCount = 0;
  512. int pCount = 0;
  513. foreach (List<string> str in info.papers[no].knowledge)
  514. {
  515. if (str.Contains(knowledgeName[i]))
  516. {
  517. var itemPersent = str.Count > 0 ? 1 / Convert.ToDouble(str.Count) : 0;
  518. allScore += info.papers[no].point[n] * itemPersent;
  519. foreach (string id in classResult.studentIds)
  520. {
  521. int index = classResult.studentIds.IndexOf(id);
  522. if (classResult.studentScores[index].Count > 0)
  523. {
  524. score += classResult.studentScores[index][n];
  525. if (classResult.studentScores[index].Sum() >= rhw && phcount < rhwCount)
  526. {
  527. if (classResult.studentScores[index][n] == 0)
  528. {
  529. phCount++;
  530. }
  531. phcount++;
  532. continue;
  533. }
  534. if (classResult.studentScores[index].Sum() <= rhl && plcount < (info.stuCount - rhlCount))
  535. {
  536. if (classResult.studentScores[index][n] == 0)
  537. {
  538. plCount++;
  539. }
  540. plcount++;
  541. continue;
  542. }
  543. if (classResult.studentScores[index][n] == 0)
  544. {
  545. pCount++;
  546. }
  547. }
  548. }
  549. }
  550. n++;
  551. }
  552. pc.Add(pCount);
  553. ph.Add(phCount);
  554. pl.Add(plCount);
  555. double per = classResult.studentIds.Count > 0 ? Math.Round(score / classResult.studentIds.Count, 2) : 0;
  556. persent.Add(allScore > 0 ? per / allScore : 0);
  557. }
  558. classResult.phc = ph;
  559. classResult.plc = pl;
  560. classResult.pc = pc;
  561. classResult.krate = persent;
  562. }
  563. await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(classResult, classResult.id, new Azure.Cosmos.PartitionKey($"{classResult.code}"));
  564. }
  565. }
  566. catch (Exception ex)
  567. {
  568. await _dingDing.SendBotMsg($"评测知识点结算异常{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  569. }
  570. }
  571. public static async Task fieldCount(ExamInfo info, ExamSubject subject, DingDing _dingDing, int no, List<ExamClassResult> classResults,
  572. double rhwCount, double rhw, double rhlCount, double rhl, AzureCosmosFactory _azureCosmos)
  573. {
  574. try
  575. {
  576. int phcount = 0;
  577. int plcount = 0;
  578. //存放并去重知识点
  579. List<int> knowledgeName = new List<int>();
  580. knowledgeName.Add(1);
  581. knowledgeName.Add(2);
  582. knowledgeName.Add(3);
  583. knowledgeName.Add(4);
  584. knowledgeName.Add(5);
  585. knowledgeName.Add(6);
  586. foreach (ExamClassResult classResult in classResults)
  587. {
  588. if (classResult.subjectId.Equals(subject.id))
  589. {
  590. //List<int> phc = new List<int>();
  591. List<int> ph = new List<int>();
  592. List<int> pl = new List<int>();
  593. List<int> pc = new List<int>();
  594. List<double> persent = new List<double>();
  595. for (int i = 0; i < knowledgeName.Count; i++)
  596. {
  597. //初始化单个知识点得分
  598. double score = 0;
  599. double allScore = 0;
  600. int n = 0;
  601. int phCount = 0;
  602. int plCount = 0;
  603. int pCount = 0;
  604. foreach (int str in info.papers[no].field)
  605. {
  606. if (str == knowledgeName[i])
  607. {
  608. var itemPersent = 1;
  609. allScore += info.papers[no].point[n] * itemPersent;
  610. foreach (string id in classResult.studentIds)
  611. {
  612. int index = classResult.studentIds.IndexOf(id);
  613. if (classResult.studentScores[index].Count > 0)
  614. {
  615. score += classResult.studentScores[index][n];
  616. if (classResult.studentScores[index].Sum() >= rhw && phcount < rhwCount)
  617. {
  618. if (classResult.studentScores[index][n] == 0)
  619. {
  620. phCount++;
  621. }
  622. phcount++;
  623. continue;
  624. }
  625. if (classResult.studentScores[index].Sum() <= rhl && plcount < (info.stuCount - rhlCount))
  626. {
  627. if (classResult.studentScores[index][n] == 0)
  628. {
  629. plCount++;
  630. }
  631. plcount++;
  632. continue;
  633. }
  634. if (classResult.studentScores[index][n] == 0)
  635. {
  636. pCount++;
  637. }
  638. }
  639. }
  640. }
  641. n++;
  642. }
  643. pc.Add(pCount);
  644. ph.Add(phCount);
  645. pl.Add(plCount);
  646. double per = classResult.studentIds.Count > 0 ? Math.Round(score / classResult.studentIds.Count, 2) : 0;
  647. persent.Add(allScore > 0 ? per / allScore : 0);
  648. }
  649. classResult.fphc = ph;
  650. classResult.fplc = pl;
  651. classResult.fpc = pc;
  652. classResult.frate = persent;
  653. }
  654. await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(classResult, classResult.id, new Azure.Cosmos.PartitionKey($"{classResult.code}"));
  655. }
  656. }
  657. catch (Exception ex)
  658. {
  659. await _dingDing.SendBotMsg($"评测认知层次结算异常{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  660. }
  661. }
  662. //处理选题计数
  663. public static List<Dictionary<string, int>> getMore(ExamInfo info, int no, List<List<List<string>>> list)
  664. {
  665. List<Dictionary<string, int>> recorde = new List<Dictionary<string, int>>();
  666. try
  667. {
  668. for (int i = 0; i < info.papers[no].answers.Count; i++)
  669. {
  670. if (info.papers[no].answers[i].Count <= 0)
  671. {
  672. recorde.Add(new Dictionary<string, int>());
  673. continue;
  674. }
  675. Dictionary<string, int> optCount = new Dictionary<string, int>();
  676. foreach (List<List<string>> stu in list)
  677. {
  678. if (stu.Count == info.papers[no].answers.Count)
  679. {
  680. var item = stu[i];
  681. foreach (string opt in item)
  682. {
  683. if (optCount.ContainsKey(opt))
  684. {
  685. optCount[opt] = optCount[opt] + 1;
  686. }
  687. else
  688. {
  689. optCount[opt] = 1;
  690. }
  691. }
  692. }
  693. }
  694. recorde.Add(optCount);
  695. }
  696. return recorde;
  697. }
  698. catch (Exception)
  699. {
  700. return recorde;
  701. }
  702. }
  703. public static async Task createClassResultAsync(ExamInfo info, List<ExamClassResult> examClassResults, ExamSubject subject, int no, AzureCosmosFactory _azureCosmos, DingDing _dingDing, AzureStorageFactory _azureStorage)
  704. {
  705. //保证试卷信息与科目信息同步
  706. ExamResult result = new ExamResult();
  707. //人数总和
  708. int Count = 0;
  709. int m = 0;
  710. double score = 0;
  711. //标准差
  712. double powSum = 0;
  713. double allScore = info.papers[no].point.Sum();
  714. List<ClassRange> classRanges = new List<ClassRange>();
  715. List<string> lostStu = new List<string>();
  716. List<double> csRate = new List<double>();
  717. List<List<List<string>>> opt = new List<List<List<string>>>();
  718. foreach (ExamClassResult classResult in examClassResults)
  719. {
  720. double classSrate = 0;
  721. if (classResult.subjectId.Equals(subject.id))
  722. {
  723. foreach (List<List<string>> op in classResult.ans)
  724. {
  725. opt.Add(op);
  726. }
  727. //记录缺考学生索引位置
  728. int index = 0;
  729. foreach (List<double> scores in classResult.studentScores)
  730. {
  731. List<double> newScores = new List<double>();
  732. int count = 0;
  733. foreach (double sc in scores)
  734. {
  735. newScores.Add(sc > -1 ? sc : 0);
  736. if (sc == -1)
  737. {
  738. count++;
  739. }
  740. }
  741. if (count == scores.Count)
  742. {
  743. lostStu.Add(classResult.studentIds[index]);
  744. //mcount++;
  745. }
  746. classSrate += newScores.Sum();
  747. score += newScores.Sum();
  748. result.studentScores.Add(newScores);
  749. index++;
  750. }
  751. //处理班级信息
  752. ClassRange range = new ClassRange();
  753. range.id = classResult.info.id;
  754. range.name = classResult.info.name;
  755. range.gradeId = classResult.gradeId;
  756. List<int> ran = new List<int>();
  757. int stuCount = classResult.studentIds.Count;
  758. Count += stuCount;
  759. if (m == 0)
  760. {
  761. ran.Add(0);
  762. ran.Add(stuCount - 1);
  763. }
  764. else
  765. {
  766. ran.Add(Count - stuCount);
  767. ran.Add(Count - 1);
  768. }
  769. m++;
  770. range.range = ran;
  771. classRanges.Add(range);
  772. //处理学生ID
  773. foreach (string id in classResult.studentIds)
  774. {
  775. result.studentIds.Add(id);
  776. }
  777. csRate.Add(result.studentIds.Count > 0 ? Math.Round(classSrate * 1.0 / classResult.studentIds.Count, 2) : 0 / allScore);
  778. //powSum += Math.Pow(classSrate - result.average, 2);
  779. //处理选项计数内容
  780. }
  781. }
  782. await examRecordCount(info, subject, _dingDing, no, result, examClassResults, _azureCosmos);
  783. result.record = getMore(info, no, opt);
  784. result.average = result.studentIds.Count > 0 ? Math.Round(score * 1.0 / result.studentIds.Count, 2) : 0;
  785. foreach (ExamClassResult classResult in examClassResults)
  786. {
  787. //double classSrate = 0;
  788. if (classResult.subjectId.Equals(subject.id))
  789. {
  790. foreach (string id in classResult.studentIds)
  791. {
  792. double sc = classResult.studentScores[classResult.studentIds.IndexOf(id)].Sum();
  793. powSum += Math.Pow(sc - result.average, 2);
  794. }
  795. }
  796. }
  797. result.standard = Math.Round(result.studentIds.Count > 0 ? Math.Pow(powSum / result.studentIds.Count, 0.5) : 0, 2);
  798. result.csRate = csRate;
  799. result.lostStus = lostStu;
  800. result.sRate = Math.Round(result.average / allScore * 100, 2);
  801. result.classes = classRanges;
  802. result.code = "ExamResult-" + info.id;
  803. result.school = info.school;
  804. result.id = subject.id;
  805. result.examId = info.id;
  806. result.subjectId = subject.id;
  807. result.year = info.year;
  808. result.paper = info.papers[no];
  809. //result.point = info.papers[j].point;
  810. result.scope = info.scope;
  811. result.name = info.name;
  812. result.time = info.startTime;
  813. await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Common").UpsertItemAsync(result, new Azure.Cosmos.PartitionKey($"ExamResult-{info.id}"));
  814. }
  815. }
  816. }