BlobLogController.cs 6.8 KB

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