using System; using System.IO; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.Azure.WebJobs; using Microsoft.Azure.WebJobs.Extensions.Http; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using TEAMModelOS.SDK.DI; using Azure.Cosmos; using System.Text.Json; using System.Collections.Generic; using TEAMModelOS.SDK.Models; using TEAMModelOS.SDK.Extension; using TEAMModelOS.SDK.Helper.Common.CollectionHelper; using TEAMModelOS.SDK.Models.Cosmos; using TEAMModelOS.SDK.Models.Cosmos.Common; namespace TEAMModelFunction { public class ActivityHttpTrigger { private readonly AzureCosmosFactory _azureCosmos; private readonly DingDing _dingDing; private readonly AzureStorageFactory _azureStorage; private readonly AzureRedisFactory _azureRedis; public ActivityHttpTrigger(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage , AzureRedisFactory azureRedis) { _azureCosmos = azureCosmos; _dingDing = dingDing; _azureStorage = azureStorage; _azureRedis = azureRedis; } [FunctionName("fix-exam-activity")] public async Task ExamActivity([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); List datas = JsonConvert.DeserializeObject>(requestBody); var client = _azureCosmos.GetCosmosClient(); var query = $"select * from c "; foreach (string data in datas) { List exams = new List(); await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator( queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Exam-{data}") })) { 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()); } } } log.LogInformation($"{exams.ToJsonString()}"); foreach (var info in exams) { List sub = new List(); foreach (ExamSubject subject in info.subjects) { sub.Add(subject.id); } //ActivityData dataa; //if (info.scope == "school") //{ // dataa = new ActivityData // { // id = info.id, // code = $"Activity-{info.school}", // type = "exam", // name = info.name, // startTime = info.startTime, // endTime = info.endTime, // scode = info.code, // scope = info.scope, // classes = info.classes.IsNotEmpty() ? info.classes : new List { "" }, // tmdids = new List { "" }, // progress = "going", // subjects = sub // }; // await client.GetContainer("TEAMModelOS", "School").UpsertItemAsync(dataa, new Azure.Cosmos.PartitionKey(dataa.code)); //} //else if (info.scope == "private") //{ // dataa = new ActivityData // { // id = info.id, // code = $"Activity-Common", // type = "exam", // name = info.name, // startTime = info.startTime, // endTime = info.endTime, // scode = info.code, // scope = info.scope, // progress = "going", // classes = info.classes.IsNotEmpty() ? info.classes : new List { "" }, // tmdids = new List { "" }, // subjects = sub // }; // await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync(dataa, new Azure.Cosmos.PartitionKey(dataa.code)); //} (List tmdids, List studentss) = await TriggerStuActivity.GetStuList(client, info.classes, info.school); List stuActivities = new List(); List tmdActivities = new List(); if (tmdids.IsNotEmpty()) { tmdids.ForEach(x => { tmdActivities.Add(new StuActivity { pk = "Activity", id = info.id, code = $"Activity-{x}", type = "exam", name = info.name, startTime = info.startTime, endTime = info.endTime, scode = info.code, scope = info.scope, school = info.school, creatorId = info.creatorId, subjects = sub }); }); } if (studentss.IsNotEmpty()) { studentss.ForEach(x => { stuActivities.Add(new StuActivity { pk = "Activity", id = info.id, code = $"Activity-{info.school}-{x.id}", type = "exam", name = info.name, startTime = info.startTime, endTime = info.endTime, scode = info.code, scope = info.scope, school = info.school, creatorId = info.creatorId, subjects = sub }); }); } await TriggerStuActivity.SaveStuActivity(client, stuActivities, tmdActivities); } } return new OkObjectResult(new { }); } [FunctionName("fix-vote-activity")] public async Task VoteActivity( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request.1"); string name = req.Query["name"]; string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); dynamic data = JsonConvert.DeserializeObject(requestBody); log.LogInformation($"{data}"); name = name ?? data?.name; string responseMessage = string.IsNullOrEmpty(name) ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response." : $"Hello, {name}. This HTTP triggered function executed successfully."; return new OkObjectResult(responseMessage); } [FunctionName("fix-survey-activity")] public async Task SurveyActivity( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request.1"); string name = req.Query["name"]; string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); dynamic data = JsonConvert.DeserializeObject(requestBody); log.LogInformation($"{data}"); name = name ?? data?.name; string responseMessage = string.IsNullOrEmpty(name) ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response." : $"Hello, {name}. This HTTP triggered function executed successfully."; return new OkObjectResult(responseMessage); } } }