123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- using Microsoft.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;
- using TEAMModelOS.SDK.Models.Cosmos.BI;
- using TEAMModelOS.SDK.Models.Service.BI;
- 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);
- jsonElement.TryGetProperty("isJoin", out JsonElement isJoin);
- 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; //使用大小
- int recCount = 0;
- Dictionary<string, int> typeCnt = new();
- List<string> schoolIds = new();
- if (!string.IsNullOrEmpty($"{areaId}"))
- {
- string sqlTxt = $"select value(c.id) from c where c.areaId='{areaId}'";
- sqlSize.Append($" where c.areaId = '{areaId}'");
- schoolIds = await CommonFind.FindScIds(cosmosClient, sqlTxt.ToString(), "Base");
- foreach (var item in StaticValue.fileType)
- {
- int fileCnt = 0;
- if (schoolIds.Count > 0)
- {
- string inScStr = BICommonWay.ManyScSql(" REPLACE(c.code, 'Bloblog-', '')", schoolIds);
- string typeSql = $"select value(count(c.id)) from c where c.pk='Bloblog' and {inScStr} and c.type='{item}'";
- fileCnt = await CommonFind.GetSqlValueCount(cosmosClient, new List<string>() { "School", "Teacher" }, typeSql);
- }
- recCount += fileCnt;
- typeCnt.Add(item, fileCnt);
- }
- foreach (var scId in schoolIds)
- {
- long blobsize = 0;
- RedisValue value = _azureRedis.GetRedisClient(8).HashGet($"Blob:Record", $"{scId}");
- if (!value.IsNullOrEmpty)
- {
- JsonElement record = value.ToString().ToObject<JsonElement>();
- if (record.TryGetInt64(out blobsize))
- {
- useSize += blobsize;
- }
- }
- }
- }
- else
- {
- StringBuilder scSql = new("select value(c.id) from c");
- if ($"{isJoin}".Equals("noJoin"))
- {
- scSql.Append($" where c.areaId = null or c.areaId = ''");
- }
- schoolIds = await CommonFind.GetValueSingle(cosmosClient, "School",scSql.ToString(), "Base");
- foreach (var item in StaticValue.fileType)
- {
- string typeSql = $"select value(count(c.id)) from c where c.pk='Bloblog' and c.type='{item}'";
- int fileCnt = await CommonFind.GetSqlValueCount(cosmosClient, new List<string>() { "School", "Teacher" }, typeSql);
- typeCnt.Add(item, fileCnt);
- recCount += fileCnt;
- }
- 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 areaSize = await CommonFind.GetSqlValueCount(cosmosClient, "School", sqlSize.ToString(), "Base");
- return Ok(new { state = 200, areaSize, useSize, typeCount = typeCnt.ToList(), recCount });
- }
- /// <summary>
- /// 依据教师获取教师个人的记录
- /// </summary>
- /// <param name="cosmosClient"></param>
- /// <param name="teacherIds"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- 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").GetItemQueryIteratorSql<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; }
- }
- }
- }
|