ExamService.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using Azure.Cosmos;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.Json;
  7. using System.Threading.Tasks;
  8. using TEAMModelOS.SDK.DI;
  9. namespace TEAMModelOS.SDK.Models.Service
  10. {
  11. public static class ExamService
  12. {
  13. public static List<string> getClasses(List<string> cla, List<string> stus)
  14. {
  15. List<string> classes = new List<string>();
  16. try
  17. {
  18. if (cla.Count > 0)
  19. {
  20. foreach (string cl in cla)
  21. {
  22. classes.Add(cl);
  23. }
  24. }
  25. if (stus.Count > 0)
  26. {
  27. foreach (string stu in stus)
  28. {
  29. classes.Add(stu);
  30. }
  31. }
  32. return classes;
  33. }
  34. catch (Exception)
  35. {
  36. return classes;
  37. }
  38. }
  39. public static async Task deleteAsync(CosmosClient client, string id, string tId)
  40. {
  41. List<string> correctIds = new List<string>();
  42. 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}") }))
  43. {
  44. using var jsonTask = await JsonDocument.ParseAsync(item.ContentStream);
  45. if (jsonTask.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  46. {
  47. var accounts = jsonTask.RootElement.GetProperty("Documents").EnumerateArray();
  48. while (accounts.MoveNext())
  49. {
  50. JsonElement account = accounts.Current;
  51. correctIds.Add(account.GetProperty("id").GetString());
  52. }
  53. }
  54. }
  55. if (correctIds.Count > 0)
  56. {
  57. await client.GetContainer(Constant.TEAMModelOS, "Teacher").DeleteItemsStreamAsync(correctIds, $"CorrectTask-{tId}");
  58. }
  59. }
  60. public static async Task<string> saveMoreAsync(CosmosClient client, DingDing _dingDing, ExamLite trExam)
  61. {
  62. try
  63. {
  64. trExam.ttl = -1;
  65. trExam.code = "ExamLite-" + trExam.school;
  66. trExam.scope = "school";
  67. long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  68. trExam.createTime = now;
  69. if (trExam.publish == 1)
  70. {
  71. trExam.progress = "pending";
  72. }
  73. else
  74. {
  75. if (trExam.startTime > now)
  76. {
  77. trExam.progress = "pending";
  78. }
  79. else
  80. {
  81. trExam.progress = "going";
  82. }
  83. }
  84. if (string.IsNullOrEmpty(trExam.id))
  85. {
  86. trExam.id = Guid.NewGuid().ToString();
  87. await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(trExam, new PartitionKey($"{trExam.code}"));
  88. }
  89. else
  90. {
  91. await client.GetContainer("TEAMModelOS", "Common").UpsertItemAsync(trExam, new PartitionKey($"{trExam.code}"));
  92. }
  93. return trExam.id;
  94. }
  95. catch (Exception e)
  96. {
  97. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ExamService-saveMore\n{e.Message}\n{e.StackTrace}", GroupNames.醍摩豆服務運維群組);
  98. return "";
  99. }
  100. }
  101. /*
  102. public static async Task<string> getKnowledges(List<string> knowledges,List<ExamClassResult> answers ,string sub,List<List<string>> kones,List<double> point) {
  103. foreach (string k in knowledges)
  104. {
  105. double score = 0;
  106. double allScore = 0;
  107. int n = 0;
  108. int count = 0;
  109. foreach (ExamClassResult result in answers)
  110. {
  111. if (result.subjectId.Equals(sub))
  112. {
  113. foreach (List<string> str in kones)
  114. {
  115. if (str.Contains(k))
  116. {
  117. var itemPersent = str.Count > 0 ? 1 / Convert.ToDouble(str.Count) : 0;
  118. allScore += point.Count > 0 ? point[n] * itemPersent : 0;
  119. if (result.studentScores.Count > 0)
  120. {
  121. score += result.studentScores.Sum(r => r.Sum()) * itemPersent;
  122. }
  123. }
  124. n++;
  125. }
  126. count += result.studentIds.Count;
  127. }
  128. }
  129. double per = count > 0 ? Math.Round(score / count, 2) : 0;
  130. }
  131. }*/
  132. }
  133. }