BlobLogController.cs 6.6 KB

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