TriggerExam.cs 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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 (string.IsNullOrEmpty(info.cn)) {
  418. if (info.scope.Equals("school"))
  419. {
  420. info.cn = info.school;
  421. }
  422. else {
  423. info.cn = info.creatorId;
  424. }
  425. }*/
  426. //判断均分是否发生变化,便于实时的更新评测基本信息
  427. if (info.sRate != NewsRate || info.average != NewsRateScore)
  428. {
  429. info.sRate = NewsRate;
  430. info.average = NewsRateScore;
  431. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync<ExamInfo>(info, info.id, new Azure.Cosmos.PartitionKey(info.code));
  432. }
  433. }
  434. catch (Exception e)
  435. {
  436. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-{info.id}-评测finish状态异常{e.Message}\n{e.StackTrace}", GroupNames.成都开发測試群組);
  437. }
  438. break;
  439. }
  440. }
  441. catch (Exception e)
  442. {
  443. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-{info.id}-评测结算异常{e.Message}\n{e.StackTrace}", GroupNames.成都开发測試群組);
  444. }
  445. }
  446. //处理全部学生选题计数
  447. public static async Task examRecordCount(ExamInfo info, ExamSubject subject, DingDing _dingDing, int no, ExamResult result, List<ExamClassResult> classResults, AzureCosmosFactory _azureCosmos)
  448. {
  449. try
  450. {
  451. List<double> scores = new List<double>();
  452. foreach (List<double> sc in result.studentScores)
  453. {
  454. scores.Add(sc.Sum());
  455. }
  456. //确定高分组 最低分数
  457. scores.Sort((s1, s2) => { return s2.CompareTo(s1); });
  458. double rhwCount = Math.Floor(scores.Count * 0.27);
  459. double rhw = rhwCount > 0 ? scores[int.Parse(rhwCount.ToString("0"))] : 0;
  460. //确定低分组 最高分数
  461. //scores.Sort((s1, s2) => { return s1.CompareTo(s2); });
  462. double rhlCount = Math.Ceiling(scores.Count * 0.73);
  463. double rhl = rhlCount > 0 ? scores[int.Parse(rhlCount.ToString("0"))] : 0;
  464. //存放高分组学生ID
  465. List<string> phId = new List<string>();
  466. List<string> plId = new List<string>();
  467. List<List<List<string>>> opth = new List<List<List<string>>>();
  468. List<List<List<string>>> optl = new List<List<List<string>>>();
  469. await knowledgeCount(info, subject, _dingDing, no, classResults, rhwCount, rhw, rhlCount, rhl, _azureCosmos);
  470. await fieldCount(info, subject, _dingDing, no, classResults, rhwCount, rhw, rhlCount, rhl, _azureCosmos);
  471. int PHCount = 0;
  472. int PLCount = 0;
  473. foreach (ExamClassResult classResult in classResults)
  474. {
  475. if (classResult.subjectId.Equals(subject.id))
  476. {
  477. foreach (string id in classResult.studentIds)
  478. {
  479. int index = classResult.studentIds.IndexOf(id);
  480. if (classResult.studentScores.Count > 0)
  481. {
  482. if (classResult.studentScores[index].Sum() >= rhw && PHCount < rhwCount)
  483. {
  484. if (classResult.ans.Count > 0)
  485. {
  486. opth.Add(classResult.ans[index]);
  487. PHCount++;
  488. continue;
  489. }
  490. }
  491. if (classResult.studentScores[index].Sum() <= rhl && PLCount < (scores.Count - rhlCount))
  492. {
  493. if (classResult.ans.Count > 0)
  494. {
  495. optl.Add(classResult.ans[index]);
  496. PLCount++;
  497. continue;
  498. }
  499. }
  500. }
  501. }
  502. }
  503. }
  504. result.phc = getMore(info, no, opth);
  505. result.plc = getMore(info, no, optl);
  506. }
  507. catch (Exception ex)
  508. {
  509. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-{info.id}-评测作答记录结算异常{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  510. }
  511. }
  512. public static async Task knowledgeCount(ExamInfo info, ExamSubject subject, DingDing _dingDing, int no, List<ExamClassResult> classResults,
  513. double rhwCount, double rhw, double rhlCount, double rhl, AzureCosmosFactory _azureCosmos)
  514. {
  515. try
  516. {
  517. int phcount = 0;
  518. int plcount = 0;
  519. //存放并去重知识点
  520. HashSet<string> kname = new HashSet<string>();
  521. if (info.papers[no].knowledge.Count > 0) {
  522. info.papers[no].knowledge.ForEach(kno =>
  523. {
  524. kno.ForEach(k =>
  525. {
  526. kname.Add(k);
  527. });
  528. });
  529. List<string> knowledgeName = new List<string>();
  530. foreach (string cla in kname)
  531. {
  532. knowledgeName.Add(cla);
  533. }
  534. for (int k = 0; k < knowledgeName.Count; k++)
  535. {
  536. if (null == knowledgeName[k])
  537. {
  538. knowledgeName.Remove(knowledgeName[k]);
  539. }
  540. }
  541. foreach (ExamClassResult classResult in classResults)
  542. {
  543. if (classResult.subjectId.Equals(subject.id))
  544. {
  545. //List<int> phc = new List<int>();
  546. List<int> ph = new List<int>();
  547. List<int> pl = new List<int>();
  548. List<int> pc = new List<int>();
  549. List<double> persent = new List<double>();
  550. for (int i = 0; i < knowledgeName.Count; i++)
  551. {
  552. //初始化单个知识点得分
  553. double score = 0;
  554. double allScore = 0;
  555. int n = 0;
  556. int phCount = 0;
  557. int plCount = 0;
  558. int pCount = 0;
  559. foreach (List<string> str in info.papers[no].knowledge)
  560. {
  561. if (str.Contains(knowledgeName[i]))
  562. {
  563. var itemPersent = str.Count > 0 ? 1 / Convert.ToDouble(str.Count) : 0;
  564. allScore += info.papers[no].point.Count > 0 ? info.papers[no].point[n] * itemPersent : 0;
  565. foreach (string id in classResult.studentIds)
  566. {
  567. int index = classResult.studentIds.IndexOf(id);
  568. if (classResult.studentScores.Count > 0)
  569. {
  570. if (classResult.studentScores[index].Count > 0)
  571. {
  572. score += classResult.studentScores[index][n] == -1 ? 0 : classResult.studentScores[index][n];
  573. if (classResult.studentScores[index].Sum() >= rhw && phcount < rhwCount)
  574. {
  575. if (classResult.studentScores[index][n] <= 0)
  576. {
  577. phCount++;
  578. }
  579. phcount++;
  580. continue;
  581. }
  582. if (classResult.studentScores[index].Sum() <= rhl && plcount < (info.stuCount - rhlCount))
  583. {
  584. if (classResult.studentScores[index][n] <= 0)
  585. {
  586. plCount++;
  587. }
  588. plcount++;
  589. continue;
  590. }
  591. if (classResult.studentScores[index][n] <= 0)
  592. {
  593. pCount++;
  594. }
  595. }
  596. }
  597. }
  598. }
  599. n++;
  600. }
  601. pc.Add(pCount);
  602. ph.Add(phCount);
  603. pl.Add(plCount);
  604. double per = classResult.studentIds.Count > 0 ? Math.Round(score / classResult.studentIds.Count, 2) : 0;
  605. persent.Add(allScore > 0 ? per / allScore : 0);
  606. }
  607. classResult.phc = ph;
  608. classResult.plc = pl;
  609. classResult.pc = pc;
  610. classResult.krate = persent;
  611. }
  612. await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(classResult, classResult.id, new Azure.Cosmos.PartitionKey($"{classResult.code}"));
  613. }
  614. }
  615. }
  616. catch (Exception ex)
  617. {
  618. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-{info.id}-评测知识点结算异常{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  619. }
  620. }
  621. public static async Task fieldCount(ExamInfo info, ExamSubject subject, DingDing _dingDing, int no, List<ExamClassResult> classResults,
  622. double rhwCount, double rhw, double rhlCount, double rhl, AzureCosmosFactory _azureCosmos)
  623. {
  624. try
  625. {
  626. int phcount = 0;
  627. int plcount = 0;
  628. //存放并去重知识点
  629. List<int> knowledgeName = new List<int>();
  630. knowledgeName.Add(1);
  631. knowledgeName.Add(2);
  632. knowledgeName.Add(3);
  633. knowledgeName.Add(4);
  634. knowledgeName.Add(5);
  635. knowledgeName.Add(6);
  636. foreach (ExamClassResult classResult in classResults)
  637. {
  638. if (classResult.subjectId.Equals(subject.id))
  639. {
  640. //List<int> phc = new List<int>();
  641. List<int> ph = new List<int>();
  642. List<int> pl = new List<int>();
  643. List<int> pc = new List<int>();
  644. List<double> persent = new List<double>();
  645. for (int i = 0; i < knowledgeName.Count; i++)
  646. {
  647. //初始化单个知识点得分
  648. double score = 0;
  649. double allScore = 0;
  650. int n = 0;
  651. int phCount = 0;
  652. int plCount = 0;
  653. int pCount = 0;
  654. if (info.papers[no].field.Count > 0) {
  655. foreach (int str in info.papers[no].field)
  656. {
  657. if (str == knowledgeName[i])
  658. {
  659. var itemPersent = 1;
  660. allScore += info.papers[no].point.Count > 0 ? info.papers[no].point[n] * itemPersent : 0;
  661. foreach (string id in classResult.studentIds)
  662. {
  663. int index = classResult.studentIds.IndexOf(id);
  664. if (classResult.studentScores.Count > 0)
  665. {
  666. if (classResult.studentScores[index].Count > 0)
  667. {
  668. score += classResult.studentScores[index][n] == -1 ? 0 : classResult.studentScores[index][n];
  669. if (classResult.studentScores[index].Sum() >= rhw && phcount < rhwCount)
  670. {
  671. if (classResult.studentScores[index][n] <= 0)
  672. {
  673. phCount++;
  674. }
  675. phcount++;
  676. continue;
  677. }
  678. if (classResult.studentScores[index].Sum() <= rhl && plcount < (info.stuCount - rhlCount))
  679. {
  680. if (classResult.studentScores[index][n] <= 0)
  681. {
  682. plCount++;
  683. }
  684. plcount++;
  685. continue;
  686. }
  687. if (classResult.studentScores[index][n] <= 0)
  688. {
  689. pCount++;
  690. }
  691. }
  692. }
  693. }
  694. }
  695. n++;
  696. }
  697. pc.Add(pCount);
  698. ph.Add(phCount);
  699. pl.Add(plCount);
  700. double per = classResult.studentIds.Count > 0 ? Math.Round(score / classResult.studentIds.Count, 2) : 0;
  701. persent.Add(allScore > 0 ? per / allScore : 0);
  702. }
  703. }
  704. classResult.fphc = ph;
  705. classResult.fplc = pl;
  706. classResult.fpc = pc;
  707. classResult.frate = persent;
  708. }
  709. await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(classResult, classResult.id, new Azure.Cosmos.PartitionKey($"{classResult.code}"));
  710. }
  711. }
  712. catch (Exception ex)
  713. {
  714. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-{info.id}-评测认知层次结算异常{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  715. }
  716. }
  717. //处理选题计数
  718. public static List<Dictionary<string, int>> getMore(ExamInfo info, int no, List<List<List<string>>> list)
  719. {
  720. List<Dictionary<string, int>> recorde = new List<Dictionary<string, int>>();
  721. try
  722. {
  723. for (int i = 0; i < info.papers[no].answers.Count; i++)
  724. {
  725. if (info.papers[no].answers[i].Count <= 0)
  726. {
  727. recorde.Add(new Dictionary<string, int>());
  728. continue;
  729. }
  730. Dictionary<string, int> optCount = new Dictionary<string, int>();
  731. foreach (List<List<string>> stu in list)
  732. {
  733. if (stu.Count == info.papers[no].answers.Count)
  734. {
  735. var item = stu[i];
  736. foreach (string opt in item)
  737. {
  738. if (optCount.ContainsKey(opt))
  739. {
  740. optCount[opt] = optCount[opt] + 1;
  741. }
  742. else
  743. {
  744. optCount[opt] = 1;
  745. }
  746. }
  747. }
  748. }
  749. recorde.Add(optCount);
  750. }
  751. return recorde;
  752. }
  753. catch (Exception)
  754. {
  755. return recorde;
  756. }
  757. }
  758. public static async Task createClassResultAsync(ExamInfo info, List<ExamClassResult> examClassResults, ExamSubject subject, int no, AzureCosmosFactory _azureCosmos, DingDing _dingDing, AzureStorageFactory _azureStorage)
  759. {
  760. //保证试卷信息与科目信息同步
  761. ExamResult result = new ExamResult();
  762. //人数总和
  763. int Count = 0;
  764. int m = 0;
  765. double score = 0;
  766. //标准差
  767. double powSum = 0;
  768. double allScore = info.papers[no].point.Sum();
  769. List<ClassRange> classRanges = new List<ClassRange>();
  770. List<string> lostStu = new List<string>();
  771. List<double> csRate = new List<double>();
  772. List<List<List<string>>> opt = new List<List<List<string>>>();
  773. foreach (ExamClassResult classResult in examClassResults)
  774. {
  775. double classSrate = 0;
  776. if (classResult.subjectId.Equals(subject.id))
  777. {
  778. foreach (List<List<string>> op in classResult.ans)
  779. {
  780. opt.Add(op);
  781. }
  782. //记录缺考学生索引位置
  783. int index = 0;
  784. foreach (List<double> scores in classResult.studentScores)
  785. {
  786. List<double> newScores = new List<double>();
  787. int count = 0;
  788. foreach (double sc in scores)
  789. {
  790. newScores.Add(sc > -1 ? sc : 0);
  791. if (sc == -1)
  792. {
  793. count++;
  794. }
  795. }
  796. if (count == scores.Count)
  797. {
  798. lostStu.Add(classResult.studentIds[index]);
  799. //mcount++;
  800. }
  801. classSrate += newScores.Sum();
  802. score += newScores.Sum();
  803. result.studentScores.Add(newScores);
  804. index++;
  805. }
  806. //处理班级信息
  807. ClassRange range = new ClassRange();
  808. range.id = classResult.info.id;
  809. range.name = classResult.info.name;
  810. range.gradeId = classResult.gradeId;
  811. List<int> ran = new List<int>();
  812. int stuCount = classResult.studentIds.Count;
  813. Count += stuCount;
  814. if (m == 0)
  815. {
  816. ran.Add(0);
  817. ran.Add(stuCount - 1);
  818. }
  819. else
  820. {
  821. ran.Add(Count - stuCount);
  822. ran.Add(Count - 1);
  823. }
  824. m++;
  825. range.range = ran;
  826. classRanges.Add(range);
  827. //处理学生ID
  828. foreach (string id in classResult.studentIds)
  829. {
  830. result.studentIds.Add(id);
  831. }
  832. if (allScore > 0)
  833. {
  834. csRate.Add(classResult.studentIds.Count > 0 ? Math.Round(classSrate * 1.0 / classResult.studentIds.Count, 2) : 0 / allScore);
  835. }
  836. else {
  837. csRate.Add(0);
  838. }
  839. //powSum += Math.Pow(classSrate - result.average, 2);
  840. //处理选项计数内容
  841. }
  842. }
  843. await examRecordCount(info, subject, _dingDing, no, result, examClassResults, _azureCosmos);
  844. result.record = getMore(info, no, opt);
  845. result.average = result.studentIds.Count > 0 ? Math.Round(score * 1.0 / result.studentIds.Count, 2) : 0;
  846. foreach (ExamClassResult classResult in examClassResults)
  847. {
  848. //double classSrate = 0;
  849. if (classResult.subjectId.Equals(subject.id))
  850. {
  851. foreach (string id in classResult.studentIds)
  852. {
  853. double sc = classResult.studentScores[classResult.studentIds.IndexOf(id)].Sum();
  854. powSum += Math.Pow(sc - result.average, 2);
  855. }
  856. }
  857. }
  858. result.standard = Math.Round(result.studentIds.Count > 0 ? Math.Pow(powSum / result.studentIds.Count, 0.5) : 0, 2);
  859. result.csRate = csRate;
  860. result.lostStus = lostStu;
  861. result.sRate = allScore > 0 ? Math.Round(result.average / allScore * 100, 2) : 0;
  862. result.classes = classRanges;
  863. result.code = "ExamResult-" + info.id;
  864. result.school = info.school;
  865. result.id = subject.id;
  866. result.examId = info.id;
  867. result.subjectId = subject.id;
  868. result.year = info.year;
  869. result.paper = info.papers[no];
  870. //result.point = info.papers[j].point;
  871. result.scope = info.scope;
  872. result.name = info.name;
  873. result.time = info.startTime;
  874. await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Common").UpsertItemAsync(result, new Azure.Cosmos.PartitionKey($"ExamResult-{info.id}"));
  875. }
  876. }
  877. }