TriggerExam.cs 51 KB

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