StudentController.cs 2.4 KB

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