JointlyController.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using Azure.Cosmos;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Extensions.Options;
  5. using System.Collections.Generic;
  6. using System.Text.Json;
  7. using System.Threading.Tasks;
  8. using TEAMModelOS.Models;
  9. using TEAMModelOS.SDK.DI;
  10. using TEAMModelOS.SDK.Extension;
  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 List<string> { "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 List<object>();
  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]).GetItemQueryStreamIterator(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.ContentStream);
  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).GetItemQueryStreamIterator(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.ContentStream);
  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. //if (type.GetInt32() != 0)
  79. //{
  80. // await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", _containers[type.GetInt32()]).GetItemQueryStreamIterator(queryText: $"select value(c) from c where c.id='{id}'", requestOptions: string.IsNullOrEmpty($"{code}") ? new QueryRequestOptions() { } : new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}") }))
  81. // {
  82. // using var json = await JsonDocument.ParseAsync(item.ContentStream);
  83. // if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  84. // {
  85. // foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  86. // {
  87. // objs.Add(obj.ToObject<object>());
  88. // }
  89. // }
  90. // }
  91. //}
  92. //else
  93. //{
  94. // foreach (var contaoner in _containers)
  95. // {
  96. // await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", contaoner).GetItemQueryStreamIterator(queryText: $"select value(c) from c where c.id='{id}'", requestOptions: string.IsNullOrEmpty($"{code}") ? new QueryRequestOptions() { } : new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}") }))
  97. // {
  98. // using var json = await JsonDocument.ParseAsync(item.ContentStream);
  99. // if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  100. // {
  101. // foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  102. // {
  103. // objs.Add(obj.ToObject<object>());
  104. // }
  105. // }
  106. // }
  107. // }
  108. //}
  109. return Ok(new { state = 200, infos = objs });
  110. }
  111. }
  112. }