BlobLogController.cs 6.9 KB

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