TriggerExam.cs 45 KB

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