BlobLogController.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using Azure.Cosmos;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Extensions.Options;
  5. using StackExchange.Redis;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Text.Json;
  10. using System.Threading.Tasks;
  11. using TEAMModelBI.Tool;
  12. using TEAMModelOS.Models;
  13. using TEAMModelOS.SDK.DI;
  14. using TEAMModelOS.SDK.Extension;
  15. namespace TEAMModelBI.Controllers.Census
  16. {
  17. [Route("bloblog")]
  18. [ApiController]
  19. public class BlobLogController : ControllerBase
  20. {
  21. private readonly AzureCosmosFactory _azureCosmos;
  22. private readonly AzureStorageFactory _azureStorage;
  23. private readonly DingDing _dingDing;
  24. private readonly Option _option;
  25. private readonly AzureRedisFactory _azureRedis;
  26. public BlobLogController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureFactory, DingDing dingDing, IOptionsSnapshot<Option> option, AzureRedisFactory azureRedis)
  27. {
  28. _azureCosmos = azureCosmos;
  29. _azureStorage = azureFactory;
  30. _dingDing = dingDing;
  31. _option = option?.Value;
  32. _azureRedis = azureRedis;
  33. }
  34. /// <summary>
  35. /// 空间统计数量统计
  36. /// </summary>
  37. /// <param name="jsonElement"></param>
  38. /// <returns></returns>
  39. [ProducesDefaultResponseType]
  40. [HttpPost("get-area")]
  41. public async Task<IActionResult> GetArea(JsonElement jsonElement)
  42. {
  43. jsonElement.TryGetProperty("areaId", out JsonElement areaId);
  44. var cosmosClient = _azureCosmos.GetCosmosClient();
  45. StringBuilder sqlSize = new($"select sum(c.size) as totals from c ");
  46. long useSize = 0; //使用大小
  47. List<RecBlobFile> blobFiles = new();
  48. if (!string.IsNullOrEmpty($"{areaId}"))
  49. {
  50. string sqlTxt = $"select c.id from c where c.areaId='{areaId}'";
  51. sqlSize.Append($" where c.areaId = '{areaId}'");
  52. List<string> schools = await CommonFind.FindSchoolIds(cosmosClient, sqlTxt.ToString(), "Base");
  53. foreach (var id in schools)
  54. {
  55. 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}") }))
  56. {
  57. blobFiles.Add(item);
  58. }
  59. List<string> tecId = await CommonFind.FindRolesId(cosmosClient, schools);
  60. List<RecBlobFile> tecBlob = await GetBlobTeache(cosmosClient, tecId);
  61. if (tecBlob.Count > 0)
  62. {
  63. blobFiles.Concat(tecBlob);
  64. }
  65. 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() { }))
  66. {
  67. blobFiles.Add(item);
  68. }
  69. long blobsize = 0;
  70. RedisValue value = _azureRedis.GetRedisClient(8).HashGet($"Blob:Record", $"{id}");
  71. if (!value.IsNullOrEmpty)
  72. {
  73. JsonElement record = value.ToString().ToObject<JsonElement>();
  74. if (record.TryGetInt64(out blobsize))
  75. {
  76. useSize += blobsize;
  77. }
  78. }
  79. }
  80. }
  81. else
  82. {
  83. 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() { }))
  84. {
  85. blobFiles.Add(item);
  86. }
  87. 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() { }))
  88. {
  89. blobFiles.Add(item);
  90. }
  91. List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"select c.id from c", "Base");
  92. foreach (var id in schoolIds)
  93. {
  94. long blobsize = 0;
  95. RedisValue value = _azureRedis.GetRedisClient(8).HashGet($"Blob:Record", $"{id}");
  96. if (!value.IsNullOrEmpty)
  97. {
  98. JsonElement record = value.ToString().ToObject<JsonElement>();
  99. if (record.TryGetInt64(out blobsize))
  100. {
  101. useSize += blobsize;
  102. }
  103. }
  104. }
  105. }
  106. var typeCount = blobFiles.GroupBy(m => new { m.type }).Select(y => new { key = y.Key.type, value = y.Count() }).ToList();
  107. var areaSize = await CommonFind.FindTotals(cosmosClient, sqlSize.ToString(), "School", "Base");
  108. return Ok(new { state = 200, areaSize, useSize, recCount= blobFiles.Count, typeCount });
  109. }
  110. /// <summary>
  111. /// 依据教师获取教师个人的记录
  112. /// </summary>
  113. /// <param name="cosmosClient"></param>
  114. /// <param name="teacherIds"></param>
  115. /// <returns></returns>
  116. public async static Task<List<RecBlobFile>> GetBlobTeache(CosmosClient cosmosClient, List<string> teacherIds)
  117. {
  118. List<RecBlobFile> blobFiles = new();
  119. if (teacherIds.Count > 0)
  120. {
  121. foreach (var teacherId in teacherIds)
  122. {
  123. 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}") }))
  124. {
  125. blobFiles.Add(item);
  126. }
  127. }
  128. }
  129. return blobFiles;
  130. }
  131. /// <summary>
  132. /// 空间内容信息
  133. /// </summary>
  134. public record RecBlobFile
  135. {
  136. public string id { get; set; }
  137. public string code { get; set; }
  138. public string name { get; set; }
  139. public int size { get; set; }
  140. public string type { get; set; }
  141. }
  142. }
  143. }