TeacherController.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.Extensions.Options;
  4. using Azure.Cosmos;
  5. using System.Text.Json;
  6. using System.Threading.Tasks;
  7. using System.Collections.Generic;
  8. using TEAMModelOS.Models;
  9. using TEAMModelOS.SDK.DI;
  10. using TEAMModelOS.SDK.Extension;
  11. using TEAMModelOS.SDK.Models;
  12. using TEAMModelOS.SDK.Context.BI;
  13. namespace TEAMModelBI.Controllers.BITeacher
  14. {
  15. [Route("teacher")]
  16. [ApiController]
  17. public class TeacherController : ControllerBase
  18. {
  19. private readonly AzureCosmosFactory _azureCosmos;
  20. private readonly AzureStorageFactory _azureStorage;
  21. private readonly DingDing _dingDing;
  22. private readonly Option _option;
  23. public TeacherController(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. /// 查询学校教师列表
  32. /// </summary>
  33. /// <param name="jsonElement"></param>
  34. /// <returns></returns>
  35. [ProducesDefaultResponseType]
  36. [HttpPost("get-teachers")]
  37. public async Task<IActionResult> GetTeachers(JsonElement jsonElement)
  38. {
  39. jsonElement.TryGetProperty("tmdId", out JsonElement tmdId);
  40. jsonElement.TryGetProperty("schoolCode", out JsonElement schoolCode);
  41. jsonElement.TryGetProperty("site", out JsonElement site);
  42. var cosmosClient = _azureCosmos.GetCosmosClient();
  43. if ($"{site}".Equals(BIConst.Global))
  44. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  45. List<SchoolTeacher> teachers = new();
  46. string sqlTxt = "";
  47. if (!string.IsNullOrEmpty($"{tmdId}"))
  48. {
  49. sqlTxt = $"select c.id,c.name,c.code,c.picture from c where c.id='{tmdId}'";
  50. }
  51. if (!string.IsNullOrEmpty($"{schoolCode}"))
  52. {
  53. sqlTxt = $"select c.id,c.name,c.code,c.picture from c join a1 in c.schools where a1.schoolId='{schoolCode}'";
  54. }
  55. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Teacher").GetItemQueryStreamIterator(queryText: sqlTxt, requestOptions:new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  56. {
  57. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  58. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetInt16() > 0)
  59. {
  60. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  61. {
  62. SchoolTeacher schoolTeacher = new();
  63. schoolTeacher.id = obj.GetProperty("id").GetString();
  64. schoolTeacher.code = obj.GetProperty("code").GetString();
  65. schoolTeacher.name = obj.GetProperty("name").GetString();
  66. schoolTeacher.picture = obj.GetProperty("picture").GetString();
  67. teachers.Add(schoolTeacher);
  68. }
  69. }
  70. }
  71. return Ok(new { state = 200, teachers });
  72. }
  73. /// <summary>
  74. /// 通过教师Id查询详情 数据管理工具——查询工具
  75. /// </summary>
  76. /// <param name="jsonElement"></param>
  77. /// <returns></returns>
  78. [ProducesDefaultResponseType]
  79. [HttpPost("get-info")]
  80. public async Task<IActionResult> GetInfo(JsonElement jsonElement)
  81. {
  82. if (!jsonElement.TryGetProperty("teacherId", out JsonElement teacherId)) return BadRequest();
  83. jsonElement.TryGetProperty("code", out JsonElement code);
  84. jsonElement.TryGetProperty("site", out JsonElement site);
  85. var cosmosClient = _azureCosmos.GetCosmosClient();
  86. if ($"{site}".Equals(BIConst.Global))
  87. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  88. List<object> objs = new();
  89. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Teacher").GetItemQueryStreamIterator(queryText: $"select value(c) from c where c.id='{teacherId}'", requestOptions: string.IsNullOrEmpty($"{code}") ? new QueryRequestOptions() { } : new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}") }))
  90. {
  91. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  92. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  93. {
  94. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  95. {
  96. objs.Add(obj.ToObject<object>());
  97. }
  98. }
  99. }
  100. return Ok(new { state = 200, teachers = objs });
  101. }
  102. public record SchoolTeacher
  103. {
  104. public string id { get; set; }
  105. public string code { get; set; }
  106. public string picture { get; set; }
  107. public string name { get; set; }
  108. }
  109. }
  110. }