JointlyController.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.Extensions.Options;
  4. using System.Collections.Generic;
  5. using System.Text.Json;
  6. using System.Threading.Tasks;
  7. using TEAMModelOS.Models;
  8. using TEAMModelOS.SDK.DI;
  9. using TEAMModelOS.SDK.Extension;
  10. using Microsoft.Azure.Cosmos;
  11. namespace TEAMModelBI.Controllers.BICommon
  12. {
  13. [Route("jointly")]
  14. [ApiController]
  15. public class JointlyController : ControllerBase
  16. {
  17. private readonly AzureCosmosFactory _azureCosmos;
  18. private readonly AzureStorageFactory _azureStorage;
  19. private readonly DingDing _dingDing;
  20. private readonly Option _option;
  21. public readonly List<string> _containers = new() { "Teacher", "School", "Student", "Normal", "Common" };
  22. public JointlyController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option)
  23. {
  24. _azureCosmos = azureCosmos;
  25. _dingDing = dingDing;
  26. _azureStorage = azureStorage;
  27. _option = option?.Value;
  28. }
  29. /// <summary>
  30. /// 容器合一通过ID查询详情 //已对接
  31. /// </summary>
  32. /// <param name="jsonEmelent"></param>
  33. /// <returns></returns>
  34. [ProducesDefaultResponseType]
  35. [HttpPost("get-info")]
  36. public async Task<IActionResult> GetInfo(JsonElement jsonEmelent)
  37. {
  38. if (!jsonEmelent.TryGetProperty("id", out JsonElement id)) return BadRequest();
  39. if (!jsonEmelent.TryGetProperty("type", out JsonElement type)) return BadRequest();
  40. jsonEmelent.TryGetProperty("code", out JsonElement code);
  41. var cosmosClient = _azureCosmos.GetCosmosClient();
  42. List<object> objs = new();
  43. List<int> types = type.ToObject<List<int>>();
  44. if (types.Count > 0)
  45. {
  46. foreach (var temp in types)
  47. {
  48. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", _containers[temp]).GetItemQueryStreamIteratorSql(queryText: $"select value(c) from c where c.id='{id}'", requestOptions: string.IsNullOrEmpty($"{code}") ? new QueryRequestOptions() { } : new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}") }))
  49. {
  50. using var json = await JsonDocument.ParseAsync(item.Content);
  51. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  52. {
  53. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  54. {
  55. objs.Add(obj.ToObject<object>());
  56. }
  57. }
  58. }
  59. }
  60. }
  61. else
  62. {
  63. foreach (var contaoner in _containers)
  64. {
  65. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", contaoner).GetItemQueryStreamIteratorSql(queryText: $"select value(c) from c where c.id='{id}'", requestOptions: string.IsNullOrEmpty($"{code}") ? new QueryRequestOptions() { } : new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}") }))
  66. {
  67. using var json = await JsonDocument.ParseAsync(item.Content);
  68. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  69. {
  70. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  71. {
  72. objs.Add(obj.ToObject<object>());
  73. }
  74. }
  75. }
  76. }
  77. }
  78. return Ok(new { state = 200, infos = objs });
  79. }
  80. }
  81. }