AreaRelevantController.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using Azure.Cosmos;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Extensions.Options;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text.Json;
  9. using System.Threading.Tasks;
  10. using TEAMModelBI.Filter;
  11. using TEAMModelBI.Tool.Extension;
  12. using TEAMModelOS.Models;
  13. using TEAMModelOS.SDK.DI;
  14. using TEAMModelOS.SDK.Extension;
  15. using TEAMModelOS.SDK.Models;
  16. using TEAMModelOS.SDK.Models.Cosmos.BI;
  17. namespace TEAMModelBI.Controllers.BISchool
  18. {
  19. [Route("area")]
  20. [ApiController]
  21. public class AreaRelevantController : ControllerBase
  22. {
  23. //数据容器
  24. private readonly AzureCosmosFactory _azureCosmos;
  25. private readonly AzureStorageFactory _azureStorage;
  26. //钉钉提示信息
  27. private readonly DingDing _dingDing;
  28. private readonly Option _option;
  29. public AreaRelevantController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option)
  30. {
  31. _azureCosmos = azureCosmos;
  32. _azureStorage = azureStorage;
  33. _dingDing = dingDing;
  34. _option = option?.Value;
  35. }
  36. /// <summary>
  37. /// 查询区域已经有的学校
  38. /// </summary>
  39. /// <param name="jsonElement"></param>
  40. /// <returns></returns>
  41. [ProducesDefaultResponseType]
  42. [HttpPost("get-areaschools")]
  43. public async Task<IActionResult> GetAreaSchools(JsonElement jsonElement)
  44. {
  45. try
  46. {
  47. if (!jsonElement.TryGetProperty("areaId", out JsonElement _areaId)) return BadRequest();
  48. var cosmosClient = _azureCosmos.GetCosmosClient();
  49. List<JoinAreaSchool> joinAreaSchools = new List<JoinAreaSchool>();
  50. string slqtxt = $"SELECT c.id,c.name,c.schoolCode,c.province,c.city,c.dist,c.picture,c.period FROM c WHERE c.areaId='{_areaId}'";
  51. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: slqtxt,requestOptions:new QueryRequestOptions() { PartitionKey = new PartitionKey("Base")}))
  52. {
  53. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  54. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  55. {
  56. foreach(var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  57. {
  58. JoinAreaSchool joinAreaSchool = new JoinAreaSchool
  59. {
  60. id = obj.GetProperty("id").GetString(),
  61. name = obj.GetProperty("name").GetString(),
  62. schoolCode = obj.GetProperty("schoolCode").GetString(),
  63. province = obj.GetProperty("province").GetString(),
  64. city = obj.GetProperty("city").GetString(),
  65. dist = obj.GetProperty("dist").GetString(),
  66. picture = obj.GetProperty("picture").GetString(),
  67. period = obj.GetProperty("period").ToObject<List<Period>>().Select(x => x.name).ToList()
  68. };
  69. joinAreaSchools.Add(joinAreaSchool);
  70. }
  71. }
  72. }
  73. return Ok(new { state = 200, joinAreaSchools });
  74. }
  75. catch (Exception ex)
  76. {
  77. await _dingDing.SendBotMsg($"BI, {_option.Location} /area/get-areaschools \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  78. return BadRequest();
  79. }
  80. }
  81. /// <summary>
  82. /// 学校移出区域
  83. /// </summary>
  84. /// <param name="jsonElement"></param>
  85. /// <returns></returns>
  86. [ProducesDefaultResponseType]
  87. [AuthToken(Roles = "admin,assist")]
  88. [HttpPost("set-areashiftschool")]
  89. public async Task<IActionResult> SetAreaShiftSchool(JsonElement jsonElement)
  90. {
  91. try
  92. {
  93. //if (!jsonElement.TryGetProperty("areaId", out JsonElement areaId)) return BadRequest();
  94. if (!jsonElement.TryGetProperty("schoolId", out JsonElement schoolId)) return BadRequest();
  95. var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  96. var cosmosClient = _azureCosmos.GetCosmosClient();
  97. School tempSchool = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>($"{schoolId}", new PartitionKey("Base"));
  98. tempSchool.areaId = null;
  99. tempSchool.standard = null;
  100. School school = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<School>(tempSchool, tempSchool.id, new PartitionKey("Base"));
  101. //保存操作记录
  102. await _azureStorage.SaveBILog("school-update", $"{_tmdName}【{_tmdId}】已操作学校({schoolId})移除该区域", _dingDing, httpContext: HttpContext);
  103. return Ok(new { state = 200, school });
  104. }
  105. catch (Exception ex)
  106. {
  107. await _dingDing.SendBotMsg($"BI, {_option.Location} /area/set-areashiftschool \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  108. return BadRequest();
  109. }
  110. }
  111. /// <summary>
  112. /// 已加入区域的学校
  113. /// </summary>
  114. public record JoinAreaSchool
  115. {
  116. public string id { get; set; }
  117. public string name { get; set; }
  118. public string schoolCode { get; set; }
  119. public string picture { get; set; }
  120. public List<string> period { get; set; }
  121. public string province { get; set; }
  122. public string city { get; set; }
  123. public string dist { get; set; }
  124. }
  125. }
  126. }