123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442 |
- using Azure.Cosmos;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Newtonsoft.Json;
- using OS.Funct;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text.Json;
- using System.Threading.Tasks;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK.Extension;
- using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
- using TEAMModelOS.SDK.Models;
- using TEAMModelOS.SDK.Models.Cosmos.Common;
- namespace TEAMModelOS.Controllers.XTest
- {
- [Route("test")]
- [ApiController]
- public class TestController :ControllerBase
- {
- private readonly AzureStorageFactory _azureStorage;
- private readonly AzureRedisFactory _azureRedis;
- private readonly AzureCosmosFactory _azureCosmos;
- public TestController(AzureCosmosFactory azureCosmos, AzureRedisFactory azureRedis, AzureStorageFactory azureStorage) {
- _azureCosmos = azureCosmos;
- _azureRedis = azureRedis;
- _azureStorage = azureStorage;
- }
- /// <summary>
- /// 测试blob多线程写入同一个文件
- /// </summary>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpGet("multiple-blob")]
- public async Task<IActionResult> MultipleBlob() {
- await _azureStorage.GetBlobContainerClient("hbcn").List("other");
- var dn=await _azureStorage.GetBlobContainerClient("hbcn").GetBlobClient("survey/2e44ee33-ba65-34b6-7e0f-b7e627c70a54/record.json").DownloadAsync();
- var jsonc = await JsonDocument.ParseAsync( dn.Value.Content);
- var Recordc = jsonc.RootElement.ToObject<JsonElement>();
- return Ok();
- }
- /// <summary>
- /// 测试redis通配符
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpGet("test-redis")]
- public async Task<IActionResult> TestRedis( ) {
-
- var db = _azureRedis.GetRedisClient(8);
- await db.StringSetAsync("1111", "111111");
- return Ok() ;
- }
- /// <summary>
- /// 删除
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- //[AuthToken(Roles = "teacher")]
- [HttpPost("delete")]
- public async Task<IActionResult> Delete(JsonElement request)
- {
- try
- {
-
- if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
- if (!request.TryGetProperty("scope", out JsonElement scope)) return BadRequest();
- var client = _azureCosmos.GetCosmosClient();
- List<Task> tasksFiles = new List<Task>();
- var query = $"select c.id from c ";
- if (scope.ToString().Equals("school"))
- {
- var ids = client.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIterator(queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}") });
- await foreach (var id in ids)
- {
- using var json = await JsonDocument.ParseAsync(id.ContentStream);
- var jsonList = json.RootElement.GetProperty("Documents").EnumerateArray();
- //批量删除
- foreach (var obj in jsonList)
- {
- tasksFiles.Add(client.GetContainer("TEAMModelOS", "School").DeleteItemStreamAsync(obj.GetProperty("id").ToString(), new PartitionKey($"{code}")));
- }
- }
- }
- else if (scope.ToString().Equals("teacher")) {
- var ids = client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryStreamIterator(queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}") });
- await foreach (var id in ids)
- {
- using var json = await JsonDocument.ParseAsync(id.ContentStream);
- var jsonList = json.RootElement.GetProperty("Documents").EnumerateArray();
- //批量删除
- foreach (var obj in jsonList)
- {
- //tasksFiles.Add(client.GetContainer("TEAMModelOS", "Teacher").DeleteItemStreamAsync(obj.GetProperty("id").ToString(), new PartitionKey($"{code}")));
- }
- }
- }
- await Task.WhenAll(tasksFiles);
- return Ok(new { code = 1 });
- }
- catch (Exception e)
- {
- return BadRequest(e.StackTrace);
- }
- }
- [ProducesDefaultResponseType]
- //[AuthToken(Roles = "teacher")]
- [HttpPost("fix-stu-course")]
- public async Task<IActionResult> fixStuCourse(JsonElement request) {
- string originCode = request.GetProperty("originCode").GetString();
- List<Course> courses = new List<Course>();
- var client = _azureCosmos.GetCosmosClient();
- var query = $"select * from c ";
- await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<Course>(queryText: query,
- requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Course-{originCode}") }))
- {
- courses.Add(item);
- }
- await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<Course>(queryText: query,
- requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Course-{originCode}") }))
- {
- courses.Add(item);
- }
- //2.获取课程的id 并尝试添加或移除对应的学生课程记录StuCourse。
- foreach (var course in courses)
- {
- if (course.schedule.IsNotEmpty())
- {
- foreach (var sc in course.schedule)
- {
- if (!string.IsNullOrEmpty(sc.stulist))
- {
- (List<string> tmdids, List<Students> studentss) = await TriggerStuActivity.GetStuList(client, new List<string>() { sc.stulist }, course.school);
- foreach (var addStu in studentss)
- {
- var stuCourse = new StuCourse
- {
- id = course.id,
- scode = course.code,
- name = course.name,
- code = $"StuCourse-{course.school}-{addStu.id}",
- scope = course.scope,
- school = course.school,
- creatorId = course.creatorId,
- pk = "StuCourse"
- };
- await client.GetContainer("TEAMModelOS", "Student").UpsertItemAsync(stuCourse, new PartitionKey(stuCourse.code));
- }
- foreach (var addTmd in tmdids)
- {
- var tmdCourse = new StuCourse
- {
- id = course.id,
- scode = course.code,
- name = course.name,
- code = $"StuCourse-{addTmd}",
- scope = course.scope,
- //school = courseChange.school,
- creatorId = course.creatorId,
- pk = "StuCourse"
- };
- await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync(tmdCourse, new PartitionKey(tmdCourse.code));
- }
- }
- }
- }
- }
- return new OkObjectResult(new { });
- }
- [ProducesDefaultResponseType]
- //[AuthToken(Roles = "teacher")]
- [HttpPost("fix-exam-activity")]
- public async Task<IActionResult> ExamActivity(JsonElement request)
- {
-
- var datas = request.ToObject<List<string>>();
- 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>());
- }
- }
- }
- foreach (var info in exams)
- {
- if (!info.classes.IsNotEmpty())
- {
- continue;
- }
- List<string> sub = new List<string>();
- foreach (ExamSubject subject in info.subjects)
- {
- sub.Add(subject.id);
- }
- (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,
- blob = null,
- owner = info.owner
- });
- });
- }
- 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,
- blob = null,
- owner = info.owner
- });
- });
- }
- await TriggerStuActivity.SaveStuActivity(client, stuActivities, tmdActivities);
- }
- }
- return new OkObjectResult(new { });
- }
- [ProducesDefaultResponseType]
- //[AuthToken(Roles = "teacher")]
- [HttpPost("fix-vote-activity")]
- public async Task<IActionResult> VoteActivity(JsonElement request)
- {
- var datas = request.ToObject<List<string>>();
- var client = _azureCosmos.GetCosmosClient();
- var query = $"select * from c ";
- foreach (string data in datas)
- {
- List<Vote> votes = new List<Vote>();
- await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(
- queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Vote-{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())
- {
- votes.Add(obj.ToObject<Vote>());
- }
- }
- }
- foreach (var info in votes)
- {
- if (!info.classes.IsNotEmpty())
- {
- continue;
- }
- (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 = "vote",
- name = info.name,
- startTime = info.startTime,
- endTime = info.endTime,
- scode = info.code,
- scope = info.scope,
- school = info.school,
- creatorId = info.creatorId,
- subjects = new List<string>() { "" },
- blob = null,
- owner = info.owner
- });
- });
- }
- if (studentss.IsNotEmpty())
- {
- studentss.ForEach(x => {
- stuActivities.Add(new StuActivity
- {
- pk = "Activity",
- id = info.id,
- code = $"Activity-{info.school}-{x.id}",
- type = "vote",
- name = info.name,
- startTime = info.startTime,
- endTime = info.endTime,
- scode = info.code,
- scope = info.scope,
- school = info.school,
- creatorId = info.creatorId,
- subjects = new List<string>() { "" },
- blob = null,
- owner = info.owner
- });
- });
- }
- await TriggerStuActivity.SaveStuActivity(client, stuActivities, tmdActivities);
- }
- }
- return new OkObjectResult(new { });
- }
- [ProducesDefaultResponseType]
- //[AuthToken(Roles = "teacher")]
- [HttpPost("fix-survey-activity")]
- public async Task<IActionResult> SurveyActivity(JsonElement request)
- {
- var datas = request.ToObject<List<string>>();
- var client = _azureCosmos.GetCosmosClient();
- var query = $"select * from c ";
- foreach (string data in datas)
- {
- List<Survey> surveys = new List<Survey>();
- await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(
- queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Survey-{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())
- {
- surveys.Add(obj.ToObject<Survey>());
- }
- }
- }
- foreach (var info in surveys)
- {
- if (!info.classes.IsNotEmpty())
- {
- continue;
- }
- (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 = "survey",
- name = info.name,
- startTime = info.startTime,
- endTime = info.endTime,
- scode = info.code,
- scope = info.scope,
- school = info.school,
- creatorId = info.creatorId,
- subjects = new List<string>() { "" },
- blob = info.blob
- });
- });
- }
- if (studentss.IsNotEmpty())
- {
- studentss.ForEach(x => {
- stuActivities.Add(new StuActivity
- {
- pk = "Activity",
- id = info.id,
- code = $"Activity-{info.school}-{x.id}",
- type = "survey",
- name = info.name,
- startTime = info.startTime,
- endTime = info.endTime,
- scode = info.code,
- scope = info.scope,
- school = info.school,
- creatorId = info.creatorId,
- subjects = new List<string>() { "" },
- blob = info.blob
- });
- });
- }
- await TriggerStuActivity.SaveStuActivity(client, stuActivities, tmdActivities);
- }
- }
- return new OkObjectResult(new { });
- }
- }
- }
|