123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- using Microsoft.Azure.Cosmos;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Newtonsoft.Json;
- using StackExchange.Redis;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Net.Http.Json;
- using System.Text;
- using System.Text.Json;
- using System.Threading.Tasks;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK.Extension;
- using TEAMModelOS.SDK.Models;
- using TEAMModelOS.SDK.Models.Cosmos;
- using TEAMModelOS.SDK.Models.Cosmos.Common;
- using Azure.Storage.Blobs.Models;
- using Microsoft.AspNetCore.Authorization;
- namespace TEAMModelOS.Controllers.XTest
- {
- [Route("business-test")]
- [ApiController]
- public class BusinessController : ControllerBase
- {
- private readonly AzureCosmosFactory _azureCosmos;
- private readonly AzureStorageFactory _azureStorage;
- public BusinessController(AzureCosmosFactory azureCosmos, AzureRedisFactory azureRedis, AzureStorageFactory azureStorage, DingDing dingDing)
- {
- _azureCosmos = azureCosmos;
- _azureStorage = azureStorage;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- //[ProducesDefaultResponseType]
- //[HttpPost("get-schools")]
- //public async Task<IActionResult> GetSchools(JsonElement request) {
- // var client = _azureCosmos.GetCosmosClient();
- // string sql = $"select c.id as code,c.name,c.picture from c ";
- // List<dynamic> dynamics = new List<dynamic>();
- // await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIteratorSql<dynamic>(queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey("Base") }))
- // {
- // dynamics.Add(item);
- // }
- // return Ok(new { schools= dynamics,status=200});
- //}
- /// <summary>
- ///
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("get-school-info")]
- public async Task<IActionResult> GetSchoolInfo(JsonElement request)
- {
-
- if (!request.TryGetProperty("id", out JsonElement _code))
- {
- return BadRequest();
- }
- var client = _azureCosmos.GetCosmosClient();
- try
- {
- // string sql = $"select c.id ,c.code ,c.name from c ";
- School school = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>($"{_code}", partitionKey: new PartitionKey("Base"));
- //List<dynamic> knowledges = new List<dynamic>();
- //var sbjs= school.period.SelectMany(x => x.subjects);
- //List<Subject> subjects = new List<Subject>();
- //if (sbjs != null) {
- // subjects.AddRange(sbjs);
- //}
- //foreach (var sub in subjects) {
- // await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIteratorSql<dynamic>(requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Knowledge-{school.id}-{sub.id}") }))
- // {
- // knowledges.Add(item);
- // }
- //}
- //List<dynamic> exams = new List<dynamic>();
- //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIteratorSql<dynamic>(queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Exam-{school.id}") }))
- //{
- // exams.Add(item);
- //}
- //List<dynamic> votes = new List<dynamic>();
- //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIteratorSql<dynamic>(queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Vote-{school.id}") }))
- //{
- // votes.Add(item);
- //}
- //List<dynamic> surveys = new List<dynamic>();
- //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIteratorSql<dynamic>(queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Survey-{school.id}") }))
- //{
- // surveys.Add(item);
- //}
- //List<dynamic> courses = new List<dynamic>();
- //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIteratorSql<dynamic>(queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Course-{school.id}") }))
- //{
- // courses.Add(item);
- //}
- //List<dynamic> classes = new List<dynamic>();
- //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIteratorSql<dynamic>( requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Class-{school.id}") }))
- //{
- // classes.Add(item);
- //}
- //List<dynamic> stulist = new List<dynamic>();
- //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIteratorSql<dynamic>( requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"StuList-{school.id}") }))
- //{
- // stulist.Add(item);
- //}
- //List<dynamic> rooms = new List<dynamic>();
- //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIteratorSql<dynamic>( requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Room-{school.id}") }))
- //{
- // rooms.Add(item);
- //}
- //List<dynamic> volumes = new List<dynamic>();
- //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIteratorSql<dynamic>( requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Volume-{school.id}") }))
- //{
- // volumes.Add(item);
- //}
- return Ok(new {id= school.id,name=school.name, picture=school.picture });
- }
- catch (CosmosException ex)
- {
- return Ok(new { status = ex.StatusCode });
- }
- catch (Exception _) {
- return Ok(new { status = 500 });
- }
- }
- [ProducesDefaultResponseType]
- [HttpPost("get-student-info")]
- public async Task<IActionResult> GetStudentInfo(JsonElement request) {
- if (!request.TryGetProperty("id", out JsonElement _id))
- {
- return BadRequest();
- }
- if (!request.TryGetProperty("code", out JsonElement _code))
- {
- return BadRequest();
- }
- var client = _azureCosmos.GetCosmosClient();
- try
- {
- Student student = await client.GetContainer(Constant.TEAMModelOS, "Student").ReadItemAsync<Student>($"{_id}", partitionKey: new PartitionKey($"Base-{_code}"));
-
- return Ok(new { id = student.id, name = student.name, picture = student.picture });
- }
- catch (CosmosException ex)
- {
- return Ok(new { status = ex.StatusCode });
- }
- catch (Exception _)
- {
- return Ok(new { status = 500 });
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("get-teacher-info")]
- public async Task<IActionResult> GetTeacherInfo(JsonElement request)
- {
- if (!request.TryGetProperty("id", out JsonElement _code))
- {
- return BadRequest();
- }
- var client = _azureCosmos.GetCosmosClient();
- try
- {
- //string sql = $"select c.id ,c.code ,c.name from c ";
- Teacher teacher = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<Teacher>($"{_code}", partitionKey: new PartitionKey("Base"));
- //List<dynamic> exams = new List<dynamic>();
- //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIteratorSql<dynamic>(queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Exam-{teacher.id}") }))
- //{
- // exams.Add(item);
- //}
- //List<dynamic> votes = new List<dynamic>();
- //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIteratorSql<dynamic>(queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Vote-{teacher.id}") }))
- //{
- // votes.Add(item);
- //}
- //List<dynamic> surveys = new List<dynamic>();
- //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIteratorSql<dynamic>(queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Survey-{teacher.id}") }))
- //{
- // surveys.Add(item);
- //}
- //List<dynamic> courses = new List<dynamic>();
- //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIteratorSql<dynamic>(queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Course-{teacher.id}") }))
- //{
- // courses.Add(item);
- //}
- //List<dynamic> stulist = new List<dynamic>();
- //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIteratorSql<dynamic>(requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"StuList-{teacher.id}") }))
- //{
- // stulist.Add(item);
- //}
- //List<dynamic> volumes = new List<dynamic>();
- //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIteratorSql<dynamic>(requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Volume-{teacher.id}") }))
- //{
- // volumes.Add(item);
- //}
- return Ok(new { id = teacher.id, name = teacher.name, picture = teacher.picture });
- }
- catch (CosmosException ex)
- {
- return Ok(new { status = ex.StatusCode });
- }
- catch (Exception _)
- {
- return Ok(new { status = 500 });
- }
- }
- //取的特定Blob容器的使用量
- [HttpPost("get-blob-list-size")]
- [Authorize(Roles = "IES")]
- public async Task<IActionResult> GetBlobListSize(JsonElement json)
- {
- if (!json.TryGetProperty("container", out JsonElement containerName))
- {
- return Ok(new { msg = "参数错误" });
- }
- //文件夹(关联id)导向的文件夹 exam homework art records survey vote item
- string[] prefixDirId = new string[] { "exam", "homework", "art", "records", "survey", "vote", "item" };
- HashSet<string> ids = new HashSet<string>();
- Dictionary<string, List<KeyValuePair<string, long?>>> recordUrls = new Dictionary<string, List<KeyValuePair<string, long?>>>(); //單位:bytes
- foreach (string prefix in prefixDirId)
- {
- await foreach (BlobItem blobItem in _azureStorage.GetBlobContainerClient(containerName.ToString()).GetBlobsAsync(BlobTraits.None, BlobStates.None, prefix: prefix))
- {
- var path = blobItem.Name.Split("/");
- if (path.Length > 2)
- {
- string id = path[0];
- ids.Add(id);
- if (recordUrls.ContainsKey(id))
- {
- recordUrls[id].Add(new KeyValuePair<string, long?>(blobItem.Name, blobItem.Properties.ContentLength));
- }
- else
- {
- recordUrls[id] = new List<KeyValuePair<string, long?>> { new KeyValuePair<string, long?>(blobItem.Name, blobItem.Properties.ContentLength) };
- }
- }
- }
- }
- //資料整理
- Dictionary<string, long> result = new Dictionary<string, long>();
- foreach (KeyValuePair<string, List<KeyValuePair<string, long?>>> rec in recordUrls)
- {
- foreach (KeyValuePair<string, long?> recVal in rec.Value)
- {
- if (result.ContainsKey(rec.Key))
- {
- result[rec.Key] += (long)Convert.ToDouble(recVal.Value);
- }
- else
- {
- result.Add(rec.Key, (long)Convert.ToDouble(recVal.Value));
- }
- }
- }
- return Ok(new { result });
- }
- }
- }
|