ExamService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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} 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. List<ExamClassResult> classResults = new();
  126. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIterator<ExamClassResult>(
  127. queryText: $"select value(c) from c where c.examId in ({string.Join(",", examIds.Select(x => $"'{x}'"))})",
  128. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"ExamClassResult-{schooCode}") }))
  129. {
  130. classResults.Add(item);
  131. }
  132. (List<RMember> rmembers, List<RGroupList> groups) = await GroupListService.GetMemberByListids(_coreAPIHttpService, client, _dingDing, classIds, schooCode);
  133. foreach (ExamInfo info in newExams) {
  134. double totalScore = info.papers.SelectMany(x => x.point).Sum();
  135. var classResult = classResults.Where(x => x.examId.Equals(info.id)).ToList();
  136. var classScores = classResult.GroupBy(x => x.info.id).Select(c => new { classId = c.Key, average = c.ToList().Sum(z => z.average) / info.subjects.Count / totalScore }).ToList();
  137. List<(string className, double average)> classMore = new();
  138. foreach (var cs in classScores) {
  139. classMore.Add((cs.classId, cs.average));
  140. }
  141. var gradeScores = info.average / totalScore;
  142. List<(string sname, double scores,string classId)> stus = new();
  143. foreach (RMember member in rmembers) {
  144. double scroe = 0;
  145. foreach (ExamClassResult result in classResult) {
  146. if (result.studentIds.Contains(member.id)) {
  147. int index = result.studentIds.IndexOf(member.id);
  148. scroe += result.sum[index];
  149. }
  150. }
  151. stus.Add((member.id, scroe,member.classId));
  152. }
  153. grades.Add((info.name, classMore, gradeScores, stus));
  154. }
  155. }
  156. }
  157. catch (Exception e)
  158. {
  159. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ExamService-getGradeScore()\n{e.Message}\n{e.StackTrace}", GroupNames.成都开发測試群組);
  160. }
  161. return grades;
  162. }
  163. public static async Task<string> saveMoreAsync(CosmosClient client, DingDing _dingDing, ExamLite trExam)
  164. {
  165. try
  166. {
  167. trExam.ttl = -1;
  168. trExam.code = "ExamLite-" + trExam.school;
  169. trExam.scope = "school";
  170. long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  171. trExam.createTime = now;
  172. if (trExam.publish == 1)
  173. {
  174. trExam.progress = "pending";
  175. }
  176. else
  177. {
  178. if (trExam.startTime > now)
  179. {
  180. trExam.progress = "pending";
  181. }
  182. else
  183. {
  184. trExam.progress = "going";
  185. }
  186. }
  187. if (string.IsNullOrEmpty(trExam.id))
  188. {
  189. trExam.id = Guid.NewGuid().ToString();
  190. await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(trExam, new PartitionKey($"{trExam.code}"));
  191. }
  192. else
  193. {
  194. await client.GetContainer("TEAMModelOS", "Common").UpsertItemAsync(trExam, new PartitionKey($"{trExam.code}"));
  195. }
  196. return trExam.id;
  197. }
  198. catch (Exception e)
  199. {
  200. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ExamService-saveMore\n{e.Message}\n{e.StackTrace}", GroupNames.醍摩豆服務運維群組);
  201. return "";
  202. }
  203. }
  204. /*
  205. public static async Task<string> getKnowledges(List<string> knowledges,List<ExamClassResult> answers ,string sub,List<List<string>> kones,List<double> point) {
  206. foreach (string k in knowledges)
  207. {
  208. double score = 0;
  209. double allScore = 0;
  210. int n = 0;
  211. int count = 0;
  212. foreach (ExamClassResult result in answers)
  213. {
  214. if (result.subjectId.Equals(sub))
  215. {
  216. foreach (List<string> str in kones)
  217. {
  218. if (str.Contains(k))
  219. {
  220. var itemPersent = str.Count > 0 ? 1 / Convert.ToDouble(str.Count) : 0;
  221. allScore += point.Count > 0 ? point[n] * itemPersent : 0;
  222. if (result.studentScores.Count > 0)
  223. {
  224. score += result.studentScores.Sum(r => r.Sum()) * itemPersent;
  225. }
  226. }
  227. n++;
  228. }
  229. count += result.studentIds.Count;
  230. }
  231. }
  232. double per = count > 0 ? Math.Round(score / count, 2) : 0;
  233. }
  234. }*/
  235. }
  236. }