BlobLogController.cs 7.0 KB

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