BIOpenApiController.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using Azure.Cosmos;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Extensions.Options;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Text;
  8. using System.Text.Json;
  9. using System.Threading.Tasks;
  10. using TEAMModelOS.Models;
  11. using TEAMModelOS.SDK.DI;
  12. using TEAMModelOS.SDK.Models.Cosmos.BI;
  13. namespace TEAMModelBI.Controllers.BINormal
  14. {
  15. [Route("openapi")]
  16. [ApiController]
  17. public class BIOpenApiController : ControllerBase
  18. {
  19. public readonly AzureCosmosFactory _azureCosmos;
  20. public readonly AzureStorageFactory _azureStorage;
  21. public readonly DingDing _dingDing;
  22. public readonly Option _option;
  23. public BIOpenApiController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option)
  24. {
  25. _azureCosmos = azureCosmos;
  26. _azureStorage = azureStorage;
  27. _dingDing = dingDing;
  28. _option = option?.Value;
  29. }
  30. /// <summary>
  31. /// 查询所有开放API列表
  32. /// </summary>
  33. /// <param name="jsonElement"></param>
  34. /// <returns></returns>
  35. [ProducesDefaultResponseType]
  36. [HttpPost("get-infos")]
  37. public async Task<IActionResult> GetOpenApi(JsonElement jsonElement)
  38. {
  39. jsonElement.TryGetProperty("id", out JsonElement id);
  40. var cosmosClient = _azureCosmos.GetCosmosClient();
  41. StringBuilder sqlTxt = new("select c.id,c.code,c.pk,c.name,c.rw,c.method,c.url,c.descr,c.type,c.descrUrl,c.createTime from c where c.pk='Api' ");
  42. if (!string.IsNullOrEmpty($"{id}"))
  43. {
  44. sqlTxt.Append($" and c.id='{id}'");
  45. }
  46. List<ReadApi> openApis = new();
  47. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Normal").GetItemQueryStreamIterator(queryText:sqlTxt.ToString(),requestOptions:new QueryRequestOptions() { PartitionKey = new PartitionKey("Api")}))
  48. {
  49. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  50. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt32() > 0)
  51. {
  52. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  53. {
  54. ReadApi readApi = new()
  55. {
  56. id = obj.GetProperty("id").GetString(),
  57. pk = obj.GetProperty("pk").GetString(),
  58. code = obj.GetProperty("code").GetString(),
  59. name = obj.GetProperty("name").GetString(),
  60. rw = obj.GetProperty("rw").GetString(),
  61. method= obj.GetProperty("method").GetString(),
  62. url = obj.GetProperty("url").GetString(),
  63. descr = obj.GetProperty("descr").GetString(),
  64. type = obj.GetProperty("type").GetInt32(),
  65. descrUrl=obj.GetProperty("descrUrl").GetString(),
  66. createTime = obj.GetProperty("createTime").GetInt64()
  67. };
  68. openApis.Add(readApi);
  69. }
  70. }
  71. }
  72. return Ok(new { state = 200, openApis });
  73. }
  74. /// <summary>
  75. /// 新增和修改Api信息
  76. /// </summary>
  77. /// <param name="openApi"></param>
  78. /// <returns></returns>
  79. [ProducesDefaultResponseType]
  80. [HttpPost("set-api")]
  81. public async Task<IActionResult> SetOpenApi(BIOpenApi openApi)
  82. {
  83. var cosmosClient = _azureCosmos.GetCosmosClient();
  84. if (string.IsNullOrEmpty(openApi.id))
  85. {
  86. openApi.id = Guid.NewGuid().ToString();
  87. openApi.code = "Api";
  88. openApi.pk = "Api";
  89. openApi.type = string.IsNullOrEmpty($"{openApi.type}") ? openApi.type : 1;
  90. openApi.createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  91. openApi = await cosmosClient.GetContainer("TEAMModelOS", "Normal").CreateItemAsync<BIOpenApi>(openApi, new PartitionKey("Api"));
  92. }
  93. else
  94. {
  95. var response = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemStreamAsync(openApi.id, new PartitionKey("Api"));
  96. if (response.Status == 200)
  97. {
  98. openApi.ttl = -1;
  99. openApi.pk = "Api";
  100. openApi.code = "Api";
  101. openApi = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<BIOpenApi>(openApi, openApi.id, new PartitionKey("Api"));
  102. }
  103. else return Ok(new { state = 404, msg = "未找到该id相关的Api信息" });
  104. }
  105. return Ok(new { state = 200 , openApi });
  106. }
  107. public record ReadApi
  108. {
  109. public string id { get; set; }
  110. public string code { get; set; }
  111. public string pk { get; set; }
  112. public string name { get; set; }
  113. public string rw { get; set; }
  114. public string method { get; set; }
  115. public string url { get; set; }
  116. public string descr { get; set; }
  117. public int type { get; set; }
  118. public string descrUrl { get; set; }
  119. public long createTime { get; set; }
  120. }
  121. }
  122. }