|
@@ -1,27 +1,35 @@
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Net.Http;
|
|
|
+using System.Text.Json;
|
|
|
using System.Threading.Tasks;
|
|
|
+using Azure.Cosmos;
|
|
|
using Microsoft.Azure.Documents;
|
|
|
using Microsoft.Azure.WebJobs;
|
|
|
using Microsoft.Azure.WebJobs.Host;
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
+using TEAMModelOS.Models.CommonInfo;
|
|
|
+using TEAMModelOS.Models.SchoolInfo;
|
|
|
+using TEAMModelOS.SDK.DI;
|
|
|
+using TEAMModelOS.SDK.Extension;
|
|
|
|
|
|
namespace TEAMModelFunction
|
|
|
{
|
|
|
public class MonitorCosmosDB
|
|
|
{
|
|
|
- private readonly IHttpClientFactory _clientFactory;
|
|
|
+ private readonly IHttpClientFactory _clientFactory;
|
|
|
+ private readonly AzureCosmosFactory _azureCosmos;
|
|
|
|
|
|
- public MonitorCosmosDB( IHttpClientFactory clientFactory)
|
|
|
+ public MonitorCosmosDB( IHttpClientFactory clientFactory,AzureCosmosFactory azureCosmos)
|
|
|
{
|
|
|
- _clientFactory = clientFactory;
|
|
|
- }
|
|
|
+ _clientFactory = clientFactory;
|
|
|
+ _azureCosmos = azureCosmos;
|
|
|
+ }
|
|
|
|
|
|
[FunctionName("School")]
|
|
|
public async Task School([CosmosDBTrigger(
|
|
|
databaseName: "TEAMModelOS",
|
|
|
- collectionName: "School",
|
|
|
+ collectionName: "Common",
|
|
|
ConnectionStringSetting = "CosmosConnection",
|
|
|
LeaseCollectionName = "leases")]IReadOnlyList<Document> input, ILogger log)
|
|
|
{
|
|
@@ -30,6 +38,142 @@ namespace TEAMModelFunction
|
|
|
log.LogInformation("Documents modified " + input.Count);
|
|
|
log.LogInformation("First document Id " + input[0].Id);
|
|
|
}
|
|
|
+ //input[0]
|
|
|
+ var client = _azureCosmos.GetCosmosClient();
|
|
|
+ List<ExamInfo> exams = new List<ExamInfo>();
|
|
|
+ string pk = input[0].GetPropertyValue<string>("pk");
|
|
|
+ if (pk.Equals("Exam")) {
|
|
|
+ string code = input[0].GetPropertyValue<string>("code");
|
|
|
+ await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryText: $"select value(c) from c where c.id = '{input[0].Id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"{code}") }))
|
|
|
+ {
|
|
|
+ using var json = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
+ if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
|
|
|
+ {
|
|
|
+ foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
|
|
|
+ {
|
|
|
+
|
|
|
+ exams.Add(obj.ToObject<ExamInfo>());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (int i = 0; i < exams.Count; i++)
|
|
|
+ {
|
|
|
+ /*if (exams[i].startTime.CompareTo(DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()) < 0
|
|
|
+ && exams[i].endTime.CompareTo(DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()) > 0 )
|
|
|
+ {*/
|
|
|
+ if (exams[i].progress.Equals("going")) {
|
|
|
+
|
|
|
+ //await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(exams[i], exams[i].id.ToString(), new Azure.Cosmos.PartitionKey($"{exams[i].code}"));
|
|
|
+ for (int j = 0;i< exams[i].subjects.Count;j++) {
|
|
|
+ for (int k = 0; k < exams[i].targetClasseIds.Count; k++) {
|
|
|
+ ExamClassResult result = new ExamClassResult();
|
|
|
+ result.code = "ExamClassResult-" + exams[i].school;
|
|
|
+ result.examId = exams[i].id;
|
|
|
+ result.id = Guid.NewGuid().ToString();
|
|
|
+ result.subjectId = exams[i].subjects[j].id;
|
|
|
+ result.year = exams[i].year;
|
|
|
+ result.ttl = -1;
|
|
|
+ result.info.id = exams[i].targetClasseIds[k];
|
|
|
+ var sresponse = await client.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(exams[i].targetClasseIds[k], new Azure.Cosmos.PartitionKey($"Class-{exams[i].school}"));
|
|
|
+ if (sresponse.Status == 200)
|
|
|
+ {
|
|
|
+ using var json = await JsonDocument.ParseAsync(sresponse.ContentStream);
|
|
|
+ Classroom classroom = json.ToObject<Classroom>();
|
|
|
+ result.info.name = classroom.name;
|
|
|
+ List<string> ans = new List<string>();
|
|
|
+ List<double> ansPoint = new List<double>();
|
|
|
+ foreach (double p in exams[i].papers[j].point) {
|
|
|
+ ans.Add("");
|
|
|
+ ansPoint.Add(0);
|
|
|
+ }
|
|
|
+ foreach (StudentSimple stu in classroom.students) {
|
|
|
+ result.studentIds.Add(stu.id);
|
|
|
+ result.studentAnswers.Add(ans);
|
|
|
+ result.studentScores.Add(ansPoint);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ result.progress = exams[i].progress;
|
|
|
+ result.school = exams[i].school;
|
|
|
+ await client.GetContainer("TEAMModelOS", "School").CreateItemAsync(result, new Azure.Cosmos.PartitionKey($"{result.code}"));
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (exams[i].progress.Equals("finish"))
|
|
|
+ {
|
|
|
+ List<ExamClassResult> examClassResults = new List<ExamClassResult>();
|
|
|
+ await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryText: $"select value(c) from c where c.examId = '{exams[i].id}'"))
|
|
|
+ {
|
|
|
+ using var json = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
+ if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
|
|
|
+ {
|
|
|
+ foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
|
|
|
+ {
|
|
|
+ examClassResults.Add(obj.ToObject<ExamClassResult>());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (int j = 0; j < exams[i].subjects.Count; j++)
|
|
|
+ {
|
|
|
+ ExamResult result = new ExamResult();
|
|
|
+ result.ttl = -1;
|
|
|
+ result.pk = typeof(ExamResult).Name;
|
|
|
+ result.code = "ExamResult-" + exams[i].school;
|
|
|
+ result.school = exams[i].school;
|
|
|
+ result.id = Guid.NewGuid().ToString();
|
|
|
+ result.examId = exams[i].id;
|
|
|
+ result.subjectId = exams[i].subjects[j].id;
|
|
|
+ result.year = exams[i].year;
|
|
|
+ result.paper = exams[i].papers[j];
|
|
|
+ result.point = exams[i].papers[j].point;
|
|
|
+ result.scope = exams[i].scope;
|
|
|
+ result.name = exams[i].name;
|
|
|
+ //result.time
|
|
|
+
|
|
|
+ //人数总和
|
|
|
+ int Count = 0;
|
|
|
+ int m = 0;
|
|
|
+ List<ClassRange> classRanges = new List<ClassRange>();
|
|
|
+ foreach (ExamClassResult classResult in examClassResults)
|
|
|
+ {
|
|
|
+ //处理班级信息
|
|
|
+ ClassRange range = new ClassRange();
|
|
|
+ range.id = classResult.info.id;
|
|
|
+ range.name = classResult.info.name;
|
|
|
+ List<int> ran = new List<int>();
|
|
|
+ int stuCount = classResult.studentIds.Count;
|
|
|
+ Count += stuCount;
|
|
|
+ if (m == 0)
|
|
|
+ {
|
|
|
+ ran.Add(0);
|
|
|
+ ran.Add(stuCount - 1);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ ran.Add(Count - stuCount);
|
|
|
+ ran.Add(Count - 1);
|
|
|
+ }
|
|
|
+ m++;
|
|
|
+ range.range = ran;
|
|
|
+ classRanges.Add(range);
|
|
|
+ //处理学生ID
|
|
|
+ foreach (string id in classResult.studentIds)
|
|
|
+ {
|
|
|
+ result.studentIds.Add(id);
|
|
|
+ }
|
|
|
+ foreach (List<double> scores in classResult.studentScores)
|
|
|
+ {
|
|
|
+ result.studentScores.Add(scores);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ result.classes = classRanges;
|
|
|
+ await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Common").CreateItemAsync(result, new Azure.Cosmos.PartitionKey($"ExamResult-{result.school}"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
//_clientFactory.CreateClient().
|
|
|
}
|
|
|
}
|