BlobLogController.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. using TEAMModelOS.SDK.Models.Cosmos.BI;
  17. using TEAMModelOS.SDK.Models.Service.BI;
  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("site", out JsonElement site);
  47. jsonElement.TryGetProperty("areaId", out JsonElement areaId);
  48. var cosmosClient = _azureCosmos.GetCosmosClient();
  49. if ($"{site}".Equals(BIConst.Global))
  50. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  51. StringBuilder sqlSize = new($"select value(sum(c.size)) from c ");
  52. long useSize = 0; //使用大小
  53. Dictionary<string, int> typeCnt = new();
  54. List<string> schoolIds = new();
  55. if (!string.IsNullOrEmpty($"{areaId}"))
  56. {
  57. string sqlTxt = $"select c.id from c where c.areaId='{areaId}'";
  58. sqlSize.Append($" where c.areaId = '{areaId}'");
  59. schoolIds = await CommonFind.FindSchoolIds(cosmosClient, sqlTxt.ToString(), "Base");
  60. foreach (var item in StaticValue.fileType)
  61. {
  62. int fileCnt = 0;
  63. if (schoolIds.Count > 0)
  64. {
  65. string inScStr = BICommonWay.ManyScSql(" REPLACE(c.code, 'Bloblog-', '')", schoolIds);
  66. string typeSql = $"select value(count(c.id)) from c where c.pk='Bloblog' and {inScStr} and c.type='{item}'";
  67. fileCnt = await CommonFind.GetSqlValueCount(cosmosClient, new List<string>() { "School", "Teacher" }, typeSql);
  68. }
  69. typeCnt.Add(item, fileCnt);
  70. }
  71. foreach (var scId in schoolIds)
  72. {
  73. long blobsize = 0;
  74. RedisValue value = _azureRedis.GetRedisClient(8).HashGet($"Blob:Record", $"{scId}");
  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. foreach (var item in StaticValue.fileType)
  88. {
  89. string typeSql = $"select value(count(c.id)) from c where c.pk='Bloblog' and c.type='{item}'";
  90. int fileCnt = await CommonFind.GetSqlValueCount(cosmosClient, new List<string>() { "School", "Teacher" }, typeSql);
  91. typeCnt.Add(item, fileCnt);
  92. }
  93. schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"select c.id from c", "Base");
  94. foreach (var id in schoolIds)
  95. {
  96. long blobsize = 0;
  97. RedisValue value = _azureRedis.GetRedisClient(8).HashGet($"Blob:Record", $"{id}");
  98. if (!value.IsNullOrEmpty)
  99. {
  100. JsonElement record = value.ToString().ToObject<JsonElement>();
  101. if (record.TryGetInt64(out blobsize))
  102. {
  103. useSize += blobsize;
  104. }
  105. }
  106. }
  107. }
  108. var areaSize = await CommonFind.GetSqlValueCount(cosmosClient, "School", sqlSize.ToString(), "Base");
  109. return Ok(new { state = 200, areaSize, useSize, typeCount = typeCnt.ToList() });
  110. }
  111. /// <summary>
  112. /// 依据教师获取教师个人的记录
  113. /// </summary>
  114. /// <param name="cosmosClient"></param>
  115. /// <param name="teacherIds"></param>
  116. /// <returns></returns>
  117. [ProducesDefaultResponseType]
  118. public async static Task<List<RecBlobFile>> GetBlobTeache(CosmosClient cosmosClient, List<string> teacherIds)
  119. {
  120. List<RecBlobFile> blobFiles = new();
  121. if (teacherIds.Count > 0)
  122. {
  123. foreach (var teacherId in teacherIds)
  124. {
  125. 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}") }))
  126. {
  127. blobFiles.Add(item);
  128. }
  129. }
  130. }
  131. return blobFiles;
  132. }
  133. /// <summary>
  134. /// 空间内容信息
  135. /// </summary>
  136. public record RecBlobFile
  137. {
  138. public string id { get; set; }
  139. public string code { get; set; }
  140. public string name { get; set; }
  141. public int size { get; set; }
  142. public string type { get; set; }
  143. }
  144. }
  145. }