ExamService.cs 13 KB

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