StudentController.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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.Context.BI;
  10. using TEAMModelOS.SDK.DI;
  11. using TEAMModelOS.SDK.Models;
  12. namespace TEAMModelBI.Controllers.BIStudent
  13. {
  14. [Route("student")]
  15. [ApiController]
  16. public class StudentController : ControllerBase
  17. {
  18. private readonly AzureCosmosFactory _azureCosmos;
  19. private readonly AzureStorageFactory _azureStorage;
  20. private readonly DingDing _dingDing;
  21. private readonly Option _option;
  22. public StudentController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option)
  23. {
  24. _azureCosmos = azureCosmos;
  25. _azureStorage = azureStorage;
  26. _dingDing = dingDing;
  27. _option = option?.Value;
  28. }
  29. /// <summary>
  30. /// 依据学生Id查询详情 数据管理工具——查询工具
  31. /// </summary>
  32. /// <param name="jsonElement"></param>
  33. /// <returns></returns>
  34. [HttpPost("get-info")]
  35. public async Task<IActionResult> GetInfo(JsonElement jsonElement)
  36. {
  37. if (!jsonElement.TryGetProperty("studentId", out JsonElement studentId)) return BadRequest();
  38. jsonElement.TryGetProperty("code", out JsonElement code);
  39. jsonElement.TryGetProperty("site", out JsonElement site);
  40. var cosmosClient = _azureCosmos.GetCosmosClient();
  41. if ($"{site}".Equals(BIConst.Global))
  42. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  43. List<object> objs = new List<object>();
  44. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Student").GetItemQueryStreamIterator(queryText: $"select value(c) from c where c.id='{studentId}'", requestOptions: string.IsNullOrEmpty($"{code}") ? new QueryRequestOptions() { } : new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}") }))
  45. {
  46. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  47. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetInt16() > 0)
  48. {
  49. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  50. {
  51. objs.Add(obj);
  52. }
  53. }
  54. }
  55. return Ok(new { state = 200, students = objs });
  56. }
  57. }
  58. }