123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- 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<IActionResult> 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<string> datas = JsonConvert.DeserializeObject<List<string>>(requestBody);
- var client = _azureCosmos.GetCosmosClient();
-
- var query = $"select * from c ";
- foreach (string data in datas) {
- List<ExamInfo> exams = new List<ExamInfo>();
- 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<ExamInfo>());
- }
- }
- }
- log.LogInformation($"{exams.ToJsonString()}");
- foreach (var info in exams) {
- List<string> sub = new List<string>();
- 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<string> { "" },
- // tmdids = new List<string> { "" },
- // progress = "going",
- // subjects = sub
- // };
- // await client.GetContainer("TEAMModelOS", "School").UpsertItemAsync<ActivityData>(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<string> { "" },
- // tmdids = new List<string> { "" },
- // subjects = sub
- // };
- // await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync<ActivityData>(dataa, new Azure.Cosmos.PartitionKey(dataa.code));
- //}
- (List<string> tmdids, List<Students> studentss) = await TriggerStuActivity.GetStuList(client, info.classes, info.school);
- List<StuActivity> stuActivities = new List<StuActivity>();
- List<StuActivity> tmdActivities = new List<StuActivity>();
- 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<IActionResult> 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<IActionResult> 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);
- }
- }
- }
|