123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- using Azure.Cosmos;
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Text.Json;
- using System.Threading.Tasks;
- using TEAMModelOS.SDK.DI;
- namespace TEAMModelOS.SDK.Models.Service
- {
- public static class ExamService
- {
- public static List<string> getClasses(List<string> cla, List<string> stus)
- {
- List<string> classes = new List<string>();
- try
- {
- if (cla.Count > 0)
- {
- foreach (string cl in cla)
- {
- classes.Add(cl);
- }
- }
- if (stus.Count > 0)
- {
- foreach (string stu in stus)
- {
- classes.Add(stu);
- }
- }
- return classes;
- }
- catch (Exception)
- {
- return classes;
- }
- }
- public static async Task deleteAsync(CosmosClient client, string id, string tId)
- {
- List<string> correctIds = new List<string>();
- 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}") }))
- {
- using var jsonTask = await JsonDocument.ParseAsync(item.ContentStream);
- if (jsonTask.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
- {
- var accounts = jsonTask.RootElement.GetProperty("Documents").EnumerateArray();
- while (accounts.MoveNext())
- {
- JsonElement account = accounts.Current;
- correctIds.Add(account.GetProperty("id").GetString());
- }
- }
- }
- if (correctIds.Count > 0)
- {
- await client.GetContainer(Constant.TEAMModelOS, "Teacher").DeleteItemsStreamAsync(correctIds, $"CorrectTask-{tId}");
- }
- }
- public static async Task<string> saveMoreAsync(CosmosClient client, DingDing _dingDing, ExamLite trExam)
- {
- try
- {
- trExam.ttl = -1;
- trExam.code = "ExamLite-" + trExam.school;
- trExam.scope = "school";
- long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
- trExam.createTime = now;
- if (trExam.publish == 1)
- {
- trExam.progress = "pending";
- }
- else
- {
- if (trExam.startTime > now)
- {
- trExam.progress = "pending";
- }
- else
- {
- trExam.progress = "going";
- }
- }
- if (string.IsNullOrEmpty(trExam.id))
- {
- trExam.id = Guid.NewGuid().ToString();
- await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(trExam, new PartitionKey($"{trExam.code}"));
- }
- else
- {
- await client.GetContainer("TEAMModelOS", "Common").UpsertItemAsync(trExam, new PartitionKey($"{trExam.code}"));
- }
- return trExam.id;
- }
- catch (Exception e)
- {
- await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ExamService-saveMore\n{e.Message}{e.StackTrace}", GroupNames.醍摩豆服務運維群組);
- return "";
- }
- }
- }
- }
|