TriggerExam.cs 51 KB

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