TriggerExam.cs 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  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. School sc = await client.GetContainer("TEAMModelOS", "School").ReadItemAsync<School>(info.school, new Azure.Cosmos.PartitionKey("Base"));
  207. foreach (Period period in sc.period)
  208. {
  209. if (period.id.Equals(classroom.periodId))
  210. {
  211. foreach (Semester semester in period.semesters)
  212. {
  213. if (semester.start == 1)
  214. {
  215. int year = DateTimeOffset.UtcNow.Year;
  216. int month = DateTimeOffset.UtcNow.Month;
  217. int day = DateTimeOffset.UtcNow.Day;
  218. int time = month > semester.month ? 0 : 1;
  219. int eyear = year - time;
  220. result.gradeId = (eyear - classroom.year).ToString();
  221. }
  222. }
  223. }
  224. }
  225. //result.info.id = classroom.id;
  226. result.info.name = classroom.name;
  227. //result.gradeId = classroom.year.ToString();
  228. //处理班级人数
  229. 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}") }))
  230. {
  231. using var json_stu = await JsonDocument.ParseAsync(item.ContentStream);
  232. if (json_stu.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  233. {
  234. var accounts = json_stu.RootElement.GetProperty("Documents").EnumerateArray();
  235. while (accounts.MoveNext())
  236. {
  237. JsonElement account = accounts.Current;
  238. ids.Add(account.GetProperty("id").GetString());
  239. }
  240. }
  241. }
  242. }
  243. if (info.scope.Equals("private", StringComparison.OrdinalIgnoreCase))
  244. {
  245. var stuResponse = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemStreamAsync(cla, new Azure.Cosmos.PartitionKey($"StuList"));
  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. if (stuList.tmids.Count > 0)
  258. {
  259. foreach (string tid in stuList.tmids)
  260. {
  261. ids.Add(tid);
  262. }
  263. }
  264. }
  265. }
  266. else
  267. {
  268. var stuResponse = await client.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(cla, new Azure.Cosmos.PartitionKey($"StuList-{info.school}"));
  269. if (stuResponse.Status == 200)
  270. {
  271. using var json = await JsonDocument.ParseAsync(stuResponse.ContentStream);
  272. StuList stuList = json.ToObject<StuList>();
  273. //result.info.id = stuList.id;
  274. result.info.name = stuList.name;
  275. //处理发布对象为自选名单(校本)
  276. foreach (Students stus in stuList.students)
  277. {
  278. ids.Add(stus.id);
  279. }
  280. }
  281. }
  282. foreach (string stu in ids)
  283. {
  284. result.mark.Add("");
  285. result.studentIds.Add(stu);
  286. result.studentAnswers.Add(ans);
  287. result.studentScores.Add(ansPoint);
  288. result.ans.Add(anses);
  289. result.sum.Add(0);
  290. }
  291. //result.progress = info.progress;
  292. result.school = info.school;
  293. m++;
  294. await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(result, new Azure.Cosmos.PartitionKey($"{result.code}"));
  295. }
  296. }
  297. // 发送信息通知
  298. var messageEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = code }.ToJsonString());
  299. messageEnd.ApplicationProperties.Add("name", "Exam");
  300. if (records.Count > 0)
  301. {
  302. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
  303. await _serviceBus.GetServiceBusClient().cancelMessage(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), records[0].sequenceNumber);
  304. records[0].sequenceNumber = end;
  305. await _azureStorage.SaveOrUpdate<ChangeRecord>(records[0]);
  306. //await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(record, record.id, new Azure.Cosmos.PartitionKey($"{record.code}"));
  307. }
  308. else
  309. {
  310. long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
  311. ChangeRecord changeRecord = new ChangeRecord
  312. {
  313. RowKey = input.Id,
  314. PartitionKey = "going",
  315. sequenceNumber = end,
  316. msgId = messageEnd.MessageId
  317. };
  318. await _azureStorage.Save<ChangeRecord>(changeRecord);
  319. //await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(changeRecord, new Azure.Cosmos.PartitionKey($"{changeRecord.code}"));
  320. }
  321. }
  322. else
  323. {
  324. //处理单科结算时科目与试卷信息匹配的问题
  325. int gno = 0;
  326. foreach (ExamSubject subject in info.subjects)
  327. {
  328. if (subject.classCount == info.classes.Count)
  329. {
  330. await createClassResultAsync(info, examClassResults, subject, gno, _azureCosmos, _dingDing, _azureStorage);
  331. }
  332. gno++;
  333. }
  334. }
  335. }
  336. catch (Exception e)
  337. {
  338. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-{info.id}-评测going状态异常{e.Message}\n{e.StackTrace}", GroupNames.成都开发測試群組);
  339. }
  340. break;
  341. case "finish":
  342. int fno = 0;
  343. try
  344. {
  345. foreach (ExamSubject subject in info.subjects)
  346. {
  347. await createClassResultAsync(info, examClassResults, subject, fno, _azureCosmos, _dingDing, _azureStorage);
  348. fno++;
  349. }
  350. //计算单次考试简易统计信息
  351. List<ExamResult> examResults = new List<ExamResult>();
  352. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryIterator<ExamResult>(
  353. queryText: $"select value(c) from c where c.examId = '{info.id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"ExamResult-{info.id}") }))
  354. {
  355. examResults.Add(item);
  356. }
  357. List<Task<ItemResponse<ExamClassResult>>> tasks = new List<Task<ItemResponse<ExamClassResult>>>();
  358. //结算单科单班的标准差和平均分
  359. foreach (ExamClassResult classResult in examClassResults)
  360. {
  361. //标记单科单班总得分
  362. double subScore = 0;
  363. //标准差
  364. double sPowSum = 0;
  365. var scount = classResult.studentIds.Count;
  366. foreach (List<double> sc in classResult.studentScores)
  367. {
  368. List<double> newSc = new List<double>();
  369. foreach (double ssc in sc)
  370. {
  371. if (ssc == -1)
  372. {
  373. newSc.Add(0);
  374. }
  375. else
  376. {
  377. newSc.Add(ssc);
  378. }
  379. }
  380. subScore += newSc.Sum();
  381. }
  382. foreach (string sid in classResult.studentIds)
  383. {
  384. double ssc = classResult.studentScores[classResult.studentIds.IndexOf(sid)].Sum();
  385. sPowSum += Math.Pow(ssc - scount > 0 ? Math.Round(subScore * 1.0 / scount, 2) : 0, 2);
  386. }
  387. classResult.standard = Math.Round(scount > 0 ? Math.Pow(sPowSum / scount, 0.5) : 0, 2);
  388. classResult.average = scount > 0 ? Math.Round(subScore / scount, 2) : 0;
  389. classResult.progress = true;
  390. tasks.Add(client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(classResult, classResult.id, new Azure.Cosmos.PartitionKey($"{classResult.code}")));
  391. }
  392. await Task.WhenAll(tasks);
  393. //记录某次考试所有学生得分总分
  394. double score = 0;
  395. double allScore = 0;
  396. int stuCount = 0;
  397. //标准差
  398. double powSum = 0;
  399. List<string> losStu = new List<string>();
  400. //先与第一个值取并集
  401. if (examResults.Count > 0)
  402. {
  403. losStu = losStu.Union(examResults[0].lostStus).ToList();
  404. foreach (ExamResult examResult in examResults)
  405. {
  406. if (info.id == examResult.examId)
  407. {
  408. foreach (List<double> sc in examResult.studentScores)
  409. {
  410. score += sc.Sum();
  411. }
  412. stuCount = examResult.studentIds.Count;
  413. }
  414. //powSum += Math.Pow(score - examResult.studentIds.Count > 0 ? Math.Round(score * 1.0 / examResult.studentIds.Count, 2) : 0, 2);
  415. //取交集
  416. losStu = losStu.Intersect(examResult.lostStus).ToList();
  417. }
  418. }
  419. double NewsRateScore = stuCount > 0 ? Math.Round(score * 1.0 / stuCount, 2) : 0;
  420. foreach (PaperSimple simple in info.papers)
  421. {
  422. allScore += simple.point.Sum();
  423. }
  424. //计算全科标准差
  425. foreach (string id in examResults[0].studentIds)
  426. {
  427. double sc = 0;
  428. foreach (ExamResult result in examResults)
  429. {
  430. sc += result.studentScores[result.studentIds.IndexOf(id)].Sum();
  431. }
  432. powSum += Math.Pow(sc - NewsRateScore, 2);
  433. }
  434. info.standard = Math.Round(examResults[0].studentIds.Count > 0 ? Math.Pow(powSum / examResults[0].studentIds.Count, 0.5) : 0, 2);
  435. double NewsRate = allScore > 0 ? Math.Round(NewsRateScore / allScore * 100, 2) : 0;
  436. info.lostStu = losStu;
  437. /*//补充历史数据的容器名称
  438. if (string.IsNullOrEmpty(info.cn)) {
  439. if (info.scope.Equals("school"))
  440. {
  441. info.cn = info.school;
  442. }
  443. else {
  444. info.cn = info.creatorId;
  445. }
  446. }*/
  447. //判断均分是否发生变化,便于实时的更新评测基本信息
  448. if (info.sRate != NewsRate || info.average != NewsRateScore)
  449. {
  450. info.sRate = NewsRate;
  451. info.average = NewsRateScore;
  452. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync<ExamInfo>(info, info.id, new Azure.Cosmos.PartitionKey(info.code));
  453. }
  454. }
  455. catch (Exception e)
  456. {
  457. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-{info.id}-评测finish状态异常{e.Message}\n{e.StackTrace}", GroupNames.成都开发測試群組);
  458. }
  459. break;
  460. }
  461. }
  462. catch (Exception e)
  463. {
  464. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-{info.id}-评测结算异常{e.Message}\n{e.StackTrace}", GroupNames.成都开发測試群組);
  465. }
  466. }
  467. //处理全部学生选题计数
  468. public static async Task examRecordCount(ExamInfo info, ExamSubject subject, DingDing _dingDing, int no, ExamResult result, List<ExamClassResult> classResults, AzureCosmosFactory _azureCosmos)
  469. {
  470. try
  471. {
  472. List<double> scores = new List<double>();
  473. foreach (List<double> sc in result.studentScores)
  474. {
  475. scores.Add(sc.Sum());
  476. }
  477. //确定高分组 最低分数
  478. scores.Sort((s1, s2) => { return s2.CompareTo(s1); });
  479. double rhwCount = Math.Floor(scores.Count * 0.27);
  480. double rhw = rhwCount > 0 ? scores[int.Parse(rhwCount.ToString("0"))] : 0;
  481. //确定低分组 最高分数
  482. //scores.Sort((s1, s2) => { return s1.CompareTo(s2); });
  483. double rhlCount = Math.Ceiling(scores.Count * 0.73);
  484. double rhl = rhlCount > 0 ? scores[int.Parse(rhlCount.ToString("0"))] : 0;
  485. //存放高分组学生ID
  486. List<string> phId = new List<string>();
  487. List<string> plId = new List<string>();
  488. List<List<List<string>>> opth = new List<List<List<string>>>();
  489. List<List<List<string>>> optl = new List<List<List<string>>>();
  490. await knowledgeCount(info, subject, _dingDing, no, classResults, rhwCount, rhw, rhlCount, rhl, _azureCosmos);
  491. await fieldCount(info, subject, _dingDing, no, classResults, rhwCount, rhw, rhlCount, rhl, _azureCosmos);
  492. int PHCount = 0;
  493. int PLCount = 0;
  494. foreach (ExamClassResult classResult in classResults)
  495. {
  496. if (classResult.subjectId.Equals(subject.id))
  497. {
  498. foreach (string id in classResult.studentIds)
  499. {
  500. int index = classResult.studentIds.IndexOf(id);
  501. if (classResult.studentScores.Count > 0)
  502. {
  503. if (classResult.studentScores[index].Sum() >= rhw && PHCount < rhwCount)
  504. {
  505. if (classResult.ans.Count > 0)
  506. {
  507. opth.Add(classResult.ans[index]);
  508. PHCount++;
  509. continue;
  510. }
  511. }
  512. if (classResult.studentScores[index].Sum() <= rhl && PLCount < (scores.Count - rhlCount))
  513. {
  514. if (classResult.ans.Count > 0)
  515. {
  516. optl.Add(classResult.ans[index]);
  517. PLCount++;
  518. continue;
  519. }
  520. }
  521. }
  522. }
  523. }
  524. }
  525. result.phc = getMore(info, no, opth);
  526. result.plc = getMore(info, no, optl);
  527. }
  528. catch (Exception ex)
  529. {
  530. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-{info.id}-评测作答记录结算异常{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  531. }
  532. }
  533. public static async Task knowledgeCount(ExamInfo info, ExamSubject subject, DingDing _dingDing, int no, List<ExamClassResult> classResults,
  534. double rhwCount, double rhw, double rhlCount, double rhl, AzureCosmosFactory _azureCosmos)
  535. {
  536. try
  537. {
  538. int phcount = 0;
  539. int plcount = 0;
  540. //存放并去重知识点
  541. HashSet<string> kname = new HashSet<string>();
  542. if (info.papers[no].knowledge.Count > 0)
  543. {
  544. info.papers[no].knowledge.ForEach(kno =>
  545. {
  546. kno.ForEach(k =>
  547. {
  548. kname.Add(k);
  549. });
  550. });
  551. List<string> knowledgeName = new List<string>();
  552. foreach (string cla in kname)
  553. {
  554. knowledgeName.Add(cla);
  555. }
  556. for (int k = 0; k < knowledgeName.Count; k++)
  557. {
  558. if (null == knowledgeName[k])
  559. {
  560. knowledgeName.Remove(knowledgeName[k]);
  561. }
  562. }
  563. foreach (ExamClassResult classResult in classResults)
  564. {
  565. if (classResult.subjectId.Equals(subject.id))
  566. {
  567. //List<int> phc = new List<int>();
  568. List<int> ph = new List<int>();
  569. List<int> pl = new List<int>();
  570. List<int> pc = new List<int>();
  571. List<double> persent = new List<double>();
  572. for (int i = 0; i < knowledgeName.Count; i++)
  573. {
  574. //初始化单个知识点得分
  575. double score = 0;
  576. double allScore = 0;
  577. int n = 0;
  578. int phCount = 0;
  579. int plCount = 0;
  580. int pCount = 0;
  581. foreach (List<string> str in info.papers[no].knowledge)
  582. {
  583. if (str.Contains(knowledgeName[i]))
  584. {
  585. var itemPersent = str.Count > 0 ? 1 / Convert.ToDouble(str.Count) : 0;
  586. allScore += info.papers[no].point.Count > 0 ? info.papers[no].point[n] * itemPersent : 0;
  587. foreach (string id in classResult.studentIds)
  588. {
  589. int index = classResult.studentIds.IndexOf(id);
  590. if (classResult.studentScores.Count > 0)
  591. {
  592. if (classResult.studentScores[index].Count > 0)
  593. {
  594. score += classResult.studentScores[index][n] == -1 ? 0 : classResult.studentScores[index][n];
  595. if (classResult.studentScores[index].Sum() >= rhw && phcount < rhwCount)
  596. {
  597. if (classResult.studentScores[index][n] <= 0)
  598. {
  599. phCount++;
  600. }
  601. phcount++;
  602. continue;
  603. }
  604. if (classResult.studentScores[index].Sum() <= rhl && plcount < (info.stuCount - rhlCount))
  605. {
  606. if (classResult.studentScores[index][n] <= 0)
  607. {
  608. plCount++;
  609. }
  610. plcount++;
  611. continue;
  612. }
  613. if (classResult.studentScores[index][n] <= 0)
  614. {
  615. pCount++;
  616. }
  617. }
  618. }
  619. }
  620. }
  621. n++;
  622. }
  623. pc.Add(pCount);
  624. ph.Add(phCount);
  625. pl.Add(plCount);
  626. double per = classResult.studentIds.Count > 0 ? Math.Round(score / classResult.studentIds.Count, 2) : 0;
  627. persent.Add(allScore > 0 ? per / allScore : 0);
  628. }
  629. classResult.phc = ph;
  630. classResult.plc = pl;
  631. classResult.pc = pc;
  632. classResult.krate = persent;
  633. }
  634. //await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(classResult, classResult.id, new Azure.Cosmos.PartitionKey($"{classResult.code}"));
  635. }
  636. }
  637. }
  638. catch (Exception ex)
  639. {
  640. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-{info.id}-评测知识点结算异常{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  641. }
  642. }
  643. public static async Task fieldCount(ExamInfo info, ExamSubject subject, DingDing _dingDing, int no, List<ExamClassResult> classResults,
  644. double rhwCount, double rhw, double rhlCount, double rhl, AzureCosmosFactory _azureCosmos)
  645. {
  646. try
  647. {
  648. int phcount = 0;
  649. int plcount = 0;
  650. //存放并去重知识点
  651. List<int> knowledgeName = new List<int>();
  652. knowledgeName.Add(1);
  653. knowledgeName.Add(2);
  654. knowledgeName.Add(3);
  655. knowledgeName.Add(4);
  656. knowledgeName.Add(5);
  657. knowledgeName.Add(6);
  658. foreach (ExamClassResult classResult in classResults)
  659. {
  660. if (classResult.subjectId.Equals(subject.id))
  661. {
  662. //List<int> phc = new List<int>();
  663. List<int> ph = new List<int>();
  664. List<int> pl = new List<int>();
  665. List<int> pc = new List<int>();
  666. List<double> persent = new List<double>();
  667. for (int i = 0; i < knowledgeName.Count; i++)
  668. {
  669. //初始化单个知识点得分
  670. double score = 0;
  671. double allScore = 0;
  672. int n = 0;
  673. int phCount = 0;
  674. int plCount = 0;
  675. int pCount = 0;
  676. if (info.papers[no].field.Count > 0)
  677. {
  678. foreach (int str in info.papers[no].field)
  679. {
  680. if (str == knowledgeName[i])
  681. {
  682. var itemPersent = 1;
  683. allScore += info.papers[no].point.Count > 0 ? info.papers[no].point[n] * itemPersent : 0;
  684. foreach (string id in classResult.studentIds)
  685. {
  686. int index = classResult.studentIds.IndexOf(id);
  687. if (classResult.studentScores.Count > 0)
  688. {
  689. if (classResult.studentScores[index].Count > 0)
  690. {
  691. score += classResult.studentScores[index][n] == -1 ? 0 : classResult.studentScores[index][n];
  692. if (classResult.studentScores[index].Sum() >= rhw && phcount < rhwCount)
  693. {
  694. if (classResult.studentScores[index][n] <= 0)
  695. {
  696. phCount++;
  697. }
  698. phcount++;
  699. continue;
  700. }
  701. if (classResult.studentScores[index].Sum() <= rhl && plcount < (info.stuCount - rhlCount))
  702. {
  703. if (classResult.studentScores[index][n] <= 0)
  704. {
  705. plCount++;
  706. }
  707. plcount++;
  708. continue;
  709. }
  710. if (classResult.studentScores[index][n] <= 0)
  711. {
  712. pCount++;
  713. }
  714. }
  715. }
  716. }
  717. }
  718. n++;
  719. }
  720. pc.Add(pCount);
  721. ph.Add(phCount);
  722. pl.Add(plCount);
  723. double per = classResult.studentIds.Count > 0 ? Math.Round(score / classResult.studentIds.Count, 2) : 0;
  724. persent.Add(allScore > 0 ? per / allScore : 0);
  725. }
  726. }
  727. classResult.fphc = ph;
  728. classResult.fplc = pl;
  729. classResult.fpc = pc;
  730. classResult.frate = persent;
  731. }
  732. //await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(classResult, classResult.id, new Azure.Cosmos.PartitionKey($"{classResult.code}"));
  733. }
  734. }
  735. catch (Exception ex)
  736. {
  737. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-{info.id}-评测认知层次结算异常{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  738. }
  739. }
  740. //处理选题计数
  741. public static List<Dictionary<string, int>> getMore(ExamInfo info, int no, List<List<List<string>>> list)
  742. {
  743. List<Dictionary<string, int>> recorde = new List<Dictionary<string, int>>();
  744. try
  745. {
  746. for (int i = 0; i < info.papers[no].answers.Count; i++)
  747. {
  748. if (info.papers[no].answers[i].Count <= 0)
  749. {
  750. recorde.Add(new Dictionary<string, int>());
  751. continue;
  752. }
  753. Dictionary<string, int> optCount = new Dictionary<string, int>();
  754. foreach (List<List<string>> stu in list)
  755. {
  756. if (stu.Count == info.papers[no].answers.Count)
  757. {
  758. var item = stu[i];
  759. foreach (string opt in item)
  760. {
  761. if (optCount.ContainsKey(opt))
  762. {
  763. optCount[opt] = optCount[opt] + 1;
  764. }
  765. else
  766. {
  767. optCount[opt] = 1;
  768. }
  769. }
  770. }
  771. }
  772. recorde.Add(optCount);
  773. }
  774. return recorde;
  775. }
  776. catch (Exception)
  777. {
  778. return recorde;
  779. }
  780. }
  781. public static async Task createClassResultAsync(ExamInfo info, List<ExamClassResult> examClassResults, ExamSubject subject, int no, AzureCosmosFactory _azureCosmos, DingDing _dingDing, AzureStorageFactory _azureStorage)
  782. {
  783. //保证试卷信息与科目信息同步
  784. ExamResult result = new ExamResult();
  785. //人数总和
  786. int Count = 0;
  787. int m = 0;
  788. double score = 0;
  789. //标准差
  790. double powSum = 0;
  791. double allScore = info.papers[no].point.Sum();
  792. List<ClassRange> classRanges = new List<ClassRange>();
  793. List<string> lostStu = new List<string>();
  794. List<double> csRate = new List<double>();
  795. List<List<List<string>>> opt = new List<List<List<string>>>();
  796. foreach (ExamClassResult classResult in examClassResults)
  797. {
  798. double classSrate = 0;
  799. if (classResult.subjectId.Equals(subject.id))
  800. {
  801. foreach (List<List<string>> op in classResult.ans)
  802. {
  803. opt.Add(op);
  804. }
  805. //记录缺考学生索引位置
  806. int index = 0;
  807. foreach (List<double> scores in classResult.studentScores)
  808. {
  809. List<double> newScores = new List<double>();
  810. int count = 0;
  811. foreach (double sc in scores)
  812. {
  813. newScores.Add(sc > -1 ? sc : 0);
  814. if (sc == -1)
  815. {
  816. count++;
  817. }
  818. }
  819. if (count == scores.Count)
  820. {
  821. lostStu.Add(classResult.studentIds[index]);
  822. //mcount++;
  823. }
  824. classSrate += newScores.Sum();
  825. score += newScores.Sum();
  826. result.studentScores.Add(newScores);
  827. index++;
  828. }
  829. //处理班级信息
  830. ClassRange range = new ClassRange();
  831. range.id = classResult.info.id;
  832. range.name = classResult.info.name;
  833. range.gradeId = classResult.gradeId;
  834. List<int> ran = new List<int>();
  835. int stuCount = classResult.studentIds.Count;
  836. Count += stuCount;
  837. if (m == 0)
  838. {
  839. ran.Add(0);
  840. ran.Add(stuCount - 1);
  841. }
  842. else
  843. {
  844. ran.Add(Count - stuCount);
  845. ran.Add(Count - 1);
  846. }
  847. m++;
  848. range.range = ran;
  849. classRanges.Add(range);
  850. //处理学生ID
  851. foreach (string id in classResult.studentIds)
  852. {
  853. result.studentIds.Add(id);
  854. }
  855. if (allScore > 0)
  856. {
  857. csRate.Add(classResult.studentIds.Count > 0 ? Math.Round(classSrate * 1.0 / classResult.studentIds.Count, 2) : 0 / allScore);
  858. }
  859. else
  860. {
  861. csRate.Add(0);
  862. }
  863. //powSum += Math.Pow(classSrate - result.average, 2);
  864. //处理选项计数内容
  865. }
  866. }
  867. await examRecordCount(info, subject, _dingDing, no, result, examClassResults, _azureCosmos);
  868. result.record = getMore(info, no, opt);
  869. result.average = result.studentIds.Count > 0 ? Math.Round(score * 1.0 / result.studentIds.Count, 2) : 0;
  870. foreach (ExamClassResult classResult in examClassResults)
  871. {
  872. //double classSrate = 0;
  873. if (classResult.subjectId.Equals(subject.id))
  874. {
  875. foreach (string id in classResult.studentIds)
  876. {
  877. double sc = classResult.studentScores[classResult.studentIds.IndexOf(id)].Sum();
  878. powSum += Math.Pow(sc - result.average, 2);
  879. }
  880. }
  881. }
  882. result.standard = Math.Round(result.studentIds.Count > 0 ? Math.Pow(powSum / result.studentIds.Count, 0.5) : 0, 2);
  883. result.csRate = csRate;
  884. result.lostStus = lostStu;
  885. result.sRate = allScore > 0 ? Math.Round(result.average / allScore * 100, 2) : 0;
  886. result.classes = classRanges;
  887. result.code = "ExamResult-" + info.id;
  888. result.school = info.school;
  889. result.id = subject.id;
  890. result.examId = info.id;
  891. result.subjectId = subject.id;
  892. result.year = info.year;
  893. result.paper = info.papers[no];
  894. //result.point = info.papers[j].point;
  895. result.scope = info.scope;
  896. result.name = info.name;
  897. result.time = info.startTime;
  898. await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Common").UpsertItemAsync(result, new Azure.Cosmos.PartitionKey($"ExamResult-{info.id}"));
  899. }
  900. }
  901. }