123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- 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);
- //var keys = server.Keys(pattern: "Vote*", database: db.Database);
- //foreach (var key in keys) {
- // Console.WriteLine(key);
- //}
- // db.HashScanAsync()
- 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("fixExamActivity")]
- 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<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
- });
- });
- }
- 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
- });
- });
- }
- await TriggerStuActivity.SaveStuActivity(client, stuActivities, tmdActivities);
- }
- }
- return new OkObjectResult(new { });
- }
- }
- }
|