TeacherController.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. [HttpPost("get-teachers")]
  36. public async Task<IActionResult> GetTeachers(JsonElement jsonElement)
  37. {
  38. jsonElement.TryGetProperty("tmdId", out JsonElement tmdId);
  39. jsonElement.TryGetProperty("schoolCode", out JsonElement schoolCode);
  40. jsonElement.TryGetProperty("site", out JsonElement site);
  41. var cosmosClient = _azureCosmos.GetCosmosClient();
  42. if ($"{site}".Equals(BIConst.Global))
  43. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  44. List<SchoolTeacher> teachers = new List<SchoolTeacher>();
  45. string sqlTxt = "";
  46. if (!string.IsNullOrEmpty($"{tmdId}"))
  47. {
  48. sqlTxt = $"select c.id,c.name,c.code,c.picture from c where c.id='{tmdId}'";
  49. }
  50. if (!string.IsNullOrEmpty($"{schoolCode}"))
  51. {
  52. sqlTxt = $"select c.id,c.name,c.code,c.picture from c join a1 in c.schools where a1.schoolId='{schoolCode}'";
  53. }
  54. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Teacher").GetItemQueryStreamIterator(queryText: sqlTxt, requestOptions:new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  55. {
  56. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  57. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetInt16() > 0)
  58. {
  59. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  60. {
  61. SchoolTeacher schoolTeacher = new();
  62. schoolTeacher.id = obj.GetProperty("id").GetString();
  63. schoolTeacher.code = obj.GetProperty("code").GetString();
  64. schoolTeacher.name = obj.GetProperty("name").GetString();
  65. schoolTeacher.picture = obj.GetProperty("picture").GetString();
  66. teachers.Add(schoolTeacher);
  67. }
  68. }
  69. }
  70. return Ok(new { state = 200, teachers });
  71. }
  72. /// <summary>
  73. /// 通过教师Id查询详情 数据管理工具——查询工具
  74. /// </summary>
  75. /// <param name="jsonElement"></param>
  76. /// <returns></returns>
  77. [HttpPost("get-info")]
  78. public async Task<IActionResult> GetInfo(JsonElement jsonElement)
  79. {
  80. if (!jsonElement.TryGetProperty("teacherId", out JsonElement teacherId)) return BadRequest();
  81. jsonElement.TryGetProperty("code", out JsonElement code);
  82. jsonElement.TryGetProperty("site", out JsonElement site);
  83. var cosmosClient = _azureCosmos.GetCosmosClient();
  84. if ($"{site}".Equals(BIConst.Global))
  85. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  86. List<object> objs = new List<object>();
  87. 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}") }))
  88. {
  89. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  90. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  91. {
  92. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  93. {
  94. objs.Add(obj.ToObject<object>());
  95. }
  96. }
  97. }
  98. return Ok(new { state = 200, teachers = objs });
  99. }
  100. public record SchoolTeacher
  101. {
  102. public string id { get; set; }
  103. public string code { get; set; }
  104. public string picture { get; set; }
  105. public string name { get; set; }
  106. }
  107. }
  108. }