ExamService.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using Azure.Cosmos;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Text.Json;
  6. using System.Threading.Tasks;
  7. using TEAMModelOS.SDK.DI;
  8. namespace TEAMModelOS.SDK.Models.Service
  9. {
  10. public static class ExamService
  11. {
  12. public static List<string> getClasses(List<string> cla, List<string> stus)
  13. {
  14. List<string> classes = new List<string>();
  15. try
  16. {
  17. if (cla.Count > 0)
  18. {
  19. foreach (string cl in cla)
  20. {
  21. classes.Add(cl);
  22. }
  23. }
  24. if (stus.Count > 0)
  25. {
  26. foreach (string stu in stus)
  27. {
  28. classes.Add(stu);
  29. }
  30. }
  31. return classes;
  32. }
  33. catch (Exception)
  34. {
  35. return classes;
  36. }
  37. }
  38. public static async Task deleteAsync(CosmosClient client, string id, string tId)
  39. {
  40. List<string> correctIds = new List<string>();
  41. 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}") }))
  42. {
  43. using var jsonTask = await JsonDocument.ParseAsync(item.ContentStream);
  44. if (jsonTask.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  45. {
  46. var accounts = jsonTask.RootElement.GetProperty("Documents").EnumerateArray();
  47. while (accounts.MoveNext())
  48. {
  49. JsonElement account = accounts.Current;
  50. correctIds.Add(account.GetProperty("id").GetString());
  51. }
  52. }
  53. }
  54. if (correctIds.Count > 0)
  55. {
  56. await client.GetContainer(Constant.TEAMModelOS, "Teacher").DeleteItemsStreamAsync(correctIds, $"CorrectTask-{tId}");
  57. }
  58. }
  59. public static async Task<string> saveMoreAsync(CosmosClient client, DingDing _dingDing, ExamLite trExam)
  60. {
  61. try
  62. {
  63. trExam.ttl = -1;
  64. trExam.code = "ExamLite-" + trExam.school;
  65. trExam.scope = "school";
  66. long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  67. trExam.createTime = now;
  68. if (trExam.publish == 1)
  69. {
  70. trExam.progress = "pending";
  71. }
  72. else
  73. {
  74. if (trExam.startTime > now)
  75. {
  76. trExam.progress = "pending";
  77. }
  78. else
  79. {
  80. trExam.progress = "going";
  81. }
  82. }
  83. if (string.IsNullOrEmpty(trExam.id))
  84. {
  85. trExam.id = Guid.NewGuid().ToString();
  86. await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(trExam, new PartitionKey($"{trExam.code}"));
  87. }
  88. else
  89. {
  90. await client.GetContainer("TEAMModelOS", "Common").UpsertItemAsync(trExam, new PartitionKey($"{trExam.code}"));
  91. }
  92. return trExam.id;
  93. }
  94. catch (Exception e)
  95. {
  96. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ExamService-saveMore\n{e.Message}{e.StackTrace}", GroupNames.醍摩豆服務運維群組);
  97. return "";
  98. }
  99. }
  100. }
  101. }