ExamService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. using Azure.Cosmos;
  2. using DocumentFormat.OpenXml.Office2010.Excel;
  3. using MathNet.Numerics.Distributions;
  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. namespace TEAMModelOS.SDK.Models.Service
  13. {
  14. public static class ExamService
  15. {
  16. public static List<string> getClasses(List<string> cla, List<string> stus)
  17. {
  18. List<string> classes = new List<string>();
  19. try
  20. {
  21. if (cla.Count > 0)
  22. {
  23. foreach (string cl in cla)
  24. {
  25. classes.Add(cl);
  26. }
  27. }
  28. if (stus.Count > 0)
  29. {
  30. foreach (string stu in stus)
  31. {
  32. classes.Add(stu);
  33. }
  34. }
  35. return classes;
  36. }
  37. catch (Exception)
  38. {
  39. return classes;
  40. }
  41. }
  42. public static async Task deleteAsync(CosmosClient client, string id, string tId)
  43. {
  44. List<string> correctIds = new List<string>();
  45. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryStreamIterator(queryText: $"select c.id from c where c.cid = '{id}' ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"CorrectTask-{tId}") }))
  46. {
  47. using var jsonTask = await JsonDocument.ParseAsync(item.ContentStream);
  48. if (jsonTask.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  49. {
  50. var accounts = jsonTask.RootElement.GetProperty("Documents").EnumerateArray();
  51. while (accounts.MoveNext())
  52. {
  53. JsonElement account = accounts.Current;
  54. correctIds.Add(account.GetProperty("id").GetString());
  55. }
  56. }
  57. }
  58. if (correctIds.Count > 0)
  59. {
  60. await client.GetContainer(Constant.TEAMModelOS, "Teacher").DeleteItemsStreamAsync(correctIds, $"CorrectTask-{tId}");
  61. }
  62. }
  63. public static async Task<List<(string name, List<(string classId, double average)> classMore, double total,List<(string studentId, double scores, string classId)>students )>> getGradeScore(CoreAPIHttpService _coreAPIHttpService, DingDing _dingDing, CosmosClient client, List<string> classIds, string periodId, string schooCode, long stime, long etime)
  64. {
  65. List<(string name, List<(string classId,double average)> classMore, double total, List<(string studentId, double scores, string classId)> stus)> grades = new();
  66. //List<(string grade, double score)> grades = new();
  67. try
  68. {
  69. //获取该年级所有班级ID
  70. /*List<string> classIds = new();
  71. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: $"select c.id from c where c.year = {gradeId} and c.pk = 'Class' and c.periodId = '{periodId}' ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Class-{schooCode}") }))
  72. {
  73. using var jsonTask = await JsonDocument.ParseAsync(item.ContentStream);
  74. if (jsonTask.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  75. {
  76. var accounts = jsonTask.RootElement.GetProperty("Documents").EnumerateArray();
  77. while (accounts.MoveNext())
  78. {
  79. JsonElement account = accounts.Current;
  80. classIds.Add(account.GetProperty("id").GetString());
  81. }
  82. }
  83. }*/
  84. if (classIds.Count == 0)
  85. {
  86. return grades;
  87. }
  88. else
  89. {
  90. //获取该学期内学校发生的所有评测活动(不包含个人评测活动)
  91. List<ExamInfo> exams = new();
  92. School sc = new();
  93. var response = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(schooCode.ToString(), new PartitionKey($"Base"));
  94. if (response.Status == 200)
  95. {
  96. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  97. sc = json.ToObject<School>();
  98. }
  99. //var gradeNames = sc.period.Where(x => x.id.Equals(periodId.ToString()))?.FirstOrDefault().grades;
  100. //var index = gradeNames.IndexOf(gradeName.ToString());
  101. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIterator<ExamInfo>(queryText: $"select value(c) from c where c.startTime >= {stime} and c.startTime < {etime} and c.progress = 'finish' order by c.createTime desc", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Exam-{schooCode}") }))
  102. {
  103. exams.Add(item);
  104. }
  105. List<ExamInfo> newExams = new();
  106. exams = exams.Where(x => x.period.id.Equals(periodId)).ToList();
  107. foreach (ExamInfo info in exams)
  108. {
  109. bool flag = true;
  110. foreach (string id in classIds)
  111. {
  112. if (!info.classes.Contains(id))
  113. {
  114. flag = false;
  115. }
  116. }
  117. if (flag) {
  118. newExams.Add(info);
  119. }
  120. }
  121. if (newExams.Count > 3) {
  122. newExams = newExams.Take(3).ToList();
  123. }
  124. List<string> examIds = newExams.Select(x => x.id).ToList();
  125. if (examIds.Count > 0) {
  126. List<ExamClassResult> classResults = new();
  127. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIterator<ExamClassResult>(
  128. queryText: $"select value(c) from c where c.examId in ({string.Join(",", examIds.Select(x => $"'{x}'"))})",
  129. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"ExamClassResult-{schooCode}") }))
  130. {
  131. classResults.Add(item);
  132. }
  133. (List<RMember> rmembers, List<RGroupList> groups) = await GroupListService.GetMemberByListids(_coreAPIHttpService, client, _dingDing, classIds, schooCode);
  134. foreach (ExamInfo info in newExams)
  135. {
  136. double totalScore = info.papers.SelectMany(x => x.point).Sum();
  137. var classResult = classResults.Where(x => x.examId.Equals(info.id)).ToList();
  138. var classScores = classResult.GroupBy(x => x.info.id).Select(c => new { classId = c.Key, average = Math.Round(c.ToList().Sum(z => z.average) / info.subjects.Count / totalScore,2) }).ToList();
  139. List<(string className, double average)> classMore = new();
  140. foreach (var cs in classScores)
  141. {
  142. classMore.Add((cs.classId, cs.average));
  143. }
  144. var gradeScores = Math.Round(info.average / totalScore,2);
  145. List<(string sname, double scores, string classId)> stus = new();
  146. foreach (RMember member in rmembers)
  147. {
  148. double scroe = 0;
  149. foreach (ExamClassResult result in classResult)
  150. {
  151. if (result.studentIds.Contains(member.id))
  152. {
  153. int index = result.studentIds.IndexOf(member.id);
  154. scroe += result.sum[index];
  155. }
  156. }
  157. var persent = Math.Round(scroe * 1.0 / totalScore, 2);
  158. stus.Add((member.id, persent, member.classId));
  159. }
  160. grades.Add((info.name, classMore, gradeScores, stus));
  161. }
  162. }
  163. }
  164. }
  165. catch (Exception e)
  166. {
  167. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ExamService-getGradeScore()\n{e.Message}\n{e.StackTrace}", GroupNames.成都开发測試群組);
  168. }
  169. return grades;
  170. }
  171. public static async Task<string> saveMoreAsync(CosmosClient client, DingDing _dingDing, ExamLite trExam)
  172. {
  173. try
  174. {
  175. trExam.ttl = -1;
  176. trExam.code = "ExamLite-" + trExam.school;
  177. trExam.scope = "school";
  178. long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  179. trExam.createTime = now;
  180. if (trExam.publish == 1)
  181. {
  182. trExam.progress = "pending";
  183. }
  184. else
  185. {
  186. if (trExam.startTime > now)
  187. {
  188. trExam.progress = "pending";
  189. }
  190. else
  191. {
  192. trExam.progress = "going";
  193. }
  194. }
  195. if (string.IsNullOrEmpty(trExam.id))
  196. {
  197. trExam.id = Guid.NewGuid().ToString();
  198. await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(trExam, new PartitionKey($"{trExam.code}"));
  199. }
  200. else
  201. {
  202. await client.GetContainer("TEAMModelOS", "Common").UpsertItemAsync(trExam, new PartitionKey($"{trExam.code}"));
  203. }
  204. return trExam.id;
  205. }
  206. catch (Exception e)
  207. {
  208. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ExamService-saveMore\n{e.Message}\n{e.StackTrace}", GroupNames.醍摩豆服務運維群組);
  209. return "";
  210. }
  211. }
  212. /*
  213. public static async Task<string> getKnowledges(List<string> knowledges,List<ExamClassResult> answers ,string sub,List<List<string>> kones,List<double> point) {
  214. foreach (string k in knowledges)
  215. {
  216. double score = 0;
  217. double allScore = 0;
  218. int n = 0;
  219. int count = 0;
  220. foreach (ExamClassResult result in answers)
  221. {
  222. if (result.subjectId.Equals(sub))
  223. {
  224. foreach (List<string> str in kones)
  225. {
  226. if (str.Contains(k))
  227. {
  228. var itemPersent = str.Count > 0 ? 1 / Convert.ToDouble(str.Count) : 0;
  229. allScore += point.Count > 0 ? point[n] * itemPersent : 0;
  230. if (result.studentScores.Count > 0)
  231. {
  232. score += result.studentScores.Sum(r => r.Sum()) * itemPersent;
  233. }
  234. }
  235. n++;
  236. }
  237. count += result.studentIds.Count;
  238. }
  239. }
  240. double per = count > 0 ? Math.Round(score / count, 2) : 0;
  241. }
  242. }*/
  243. }
  244. }