123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- using Azure.Cosmos;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Options;
- using StackExchange.Redis;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.Json;
- using System.Threading.Tasks;
- using TEAMModelBI.Tool;
- using TEAMModelOS.Models;
- using TEAMModelOS.SDK.Context.BI;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK.Extension;
- namespace TEAMModelBI.Controllers.Census
- {
- [Route("bloblog")]
- [ApiController]
- public class BlobLogController : ControllerBase
- {
- private readonly AzureCosmosFactory _azureCosmos;
- private readonly AzureStorageFactory _azureStorage;
- private readonly DingDing _dingDing;
- private readonly Option _option;
- private readonly AzureRedisFactory _azureRedis;
- public BlobLogController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureFactory, DingDing dingDing, IOptionsSnapshot<Option> option, AzureRedisFactory azureRedis)
- {
- _azureCosmos = azureCosmos;
- _azureStorage = azureFactory;
- _dingDing = dingDing;
- _option = option?.Value;
- _azureRedis = azureRedis;
- }
- /// <summary>
- /// 空间统计数量统计 已对接
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("get-area")]
- public async Task<IActionResult> GetArea(JsonElement jsonElement)
- {
- jsonElement.TryGetProperty("site", out JsonElement site);
- jsonElement.TryGetProperty("areaId", out JsonElement areaId);
- var cosmosClient = _azureCosmos.GetCosmosClient();
- if ($"{site}".Equals(BIConst.Global))
- cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
- StringBuilder sqlSize = new($"select value(sum(c.size)) from c ");
- long useSize = 0; //使用大小
- List<RecBlobFile> blobFiles = new();
- if (!string.IsNullOrEmpty($"{areaId}"))
- {
- string sqlTxt = $"select c.id from c where c.areaId='{areaId}'";
- sqlSize.Append($" where c.areaId = '{areaId}'");
- List<string> schools = await CommonFind.FindSchoolIds(cosmosClient, sqlTxt.ToString(), "Base");
- foreach (var id in schools)
- {
- await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<RecBlobFile>(queryText: "SELECT c.id,c.code,c.name,c.size,c.type FROM c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Bloblog-{id}") }))
- {
- blobFiles.Add(item);
- }
- List<string> tecId = await CommonFind.FindRolesId(cosmosClient, schools);
- List<RecBlobFile> tecBlob = await GetBlobTeache(cosmosClient, tecId);
- if (tecBlob.Count > 0)
- {
- blobFiles.Concat(tecBlob);
- }
- await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<RecBlobFile>(queryText: $"SELECT c.id,c.code,c.name,c.size,c.type FROM c where c.pk='Bloblog'", requestOptions: new QueryRequestOptions() { }))
- {
- blobFiles.Add(item);
- }
- long blobsize = 0;
- RedisValue value = _azureRedis.GetRedisClient(8).HashGet($"Blob:Record", $"{id}");
- if (!value.IsNullOrEmpty)
- {
- JsonElement record = value.ToString().ToObject<JsonElement>();
- if (record.TryGetInt64(out blobsize))
- {
- useSize += blobsize;
- }
- }
- }
- }
- else
- {
- await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<RecBlobFile>(queryText: "SELECT c.id,c.code,c.name,c.size,c.type FROM c where c.pk='Bloblog'", requestOptions: new QueryRequestOptions() { }))
- {
- blobFiles.Add(item);
- }
- await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<RecBlobFile>(queryText: "SELECT c.id,c.code,c.name,c.size,c.type FROM c where c.pk='Bloblog'", requestOptions: new QueryRequestOptions() { }))
- {
- blobFiles.Add(item);
- }
- List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"select c.id from c", "Base");
- foreach (var id in schoolIds)
- {
- long blobsize = 0;
- RedisValue value = _azureRedis.GetRedisClient(8).HashGet($"Blob:Record", $"{id}");
- if (!value.IsNullOrEmpty)
- {
- JsonElement record = value.ToString().ToObject<JsonElement>();
- if (record.TryGetInt64(out blobsize))
- {
- useSize += blobsize;
- }
- }
- }
- }
- var typeCount = blobFiles.GroupBy(m => new { m.type }).Select(y => new { key = y.Key.type, value = y.Count() }).ToList();
- var areaSize = await CommonFind.GetSqlValueCount(cosmosClient, "School", sqlSize.ToString(), "Base");
- return Ok(new { state = 200, areaSize, useSize, recCount= blobFiles.Count, typeCount });
- }
- /// <summary>
- /// 依据教师获取教师个人的记录
- /// </summary>
- /// <param name="cosmosClient"></param>
- /// <param name="teacherIds"></param>
- /// <returns></returns>
- public async static Task<List<RecBlobFile>> GetBlobTeache(CosmosClient cosmosClient, List<string> teacherIds)
- {
- List<RecBlobFile> blobFiles = new();
- if (teacherIds.Count > 0)
- {
- foreach (var teacherId in teacherIds)
- {
- await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<RecBlobFile>(queryText: $"SELECT c.id,c.code,c.name,c.size,c.type FROM c where c.pk='Bloblog'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Bloblog-{teacherId}") }))
- {
- blobFiles.Add(item);
- }
- }
- }
- return blobFiles;
- }
- /// <summary>
- /// 空间内容信息
- /// </summary>
- public record RecBlobFile
- {
- public string id { get; set; }
- public string code { get; set; }
- public string name { get; set; }
- public int size { get; set; }
- public string type { get; set; }
- }
- }
- }
|