AreaRelevantController.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 TEAMModeBI.Filter;
  11. using TEAMModelOS.Models;
  12. using TEAMModelOS.SDK.DI;
  13. using TEAMModelOS.SDK.Extension;
  14. using TEAMModelOS.SDK.Models;
  15. using TEAMModelOS.SDK.Models.Cosmos.BI;
  16. namespace TEAMModeBI.Controllers.BISchool
  17. {
  18. [Route("area")]
  19. [ApiController]
  20. public class AreaRelevantController : ControllerBase
  21. {
  22. //数据容器
  23. private readonly AzureCosmosFactory _azureCosmos;
  24. private readonly AzureStorageFactory _azureStorage;
  25. //钉钉提示信息
  26. private readonly DingDing _dingDing;
  27. private readonly Option _option;
  28. public AreaRelevantController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option)
  29. {
  30. _azureCosmos = azureCosmos;
  31. _azureStorage = azureStorage;
  32. _dingDing = dingDing;
  33. _option = option?.Value;
  34. }
  35. /// <summary>
  36. /// 查询区域已经有的学校
  37. /// </summary>
  38. /// <param name="jsonElement"></param>
  39. /// <returns></returns>
  40. [ProducesDefaultResponseType]
  41. [HttpPost("get-areaschools")]
  42. public async Task<IActionResult> GetAreaSchools(JsonElement jsonElement)
  43. {
  44. try
  45. {
  46. if (!jsonElement.TryGetProperty("areaId", out JsonElement _areaId)) return BadRequest();
  47. var cosmosClient = _azureCosmos.GetCosmosClient();
  48. List<JoinAreaSchool> joinAreaSchools = new List<JoinAreaSchool>();
  49. 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}'";
  50. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: slqtxt,requestOptions:new QueryRequestOptions() { PartitionKey = new PartitionKey("Base")}))
  51. {
  52. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  53. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  54. {
  55. foreach(var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  56. {
  57. JoinAreaSchool joinAreaSchool = new JoinAreaSchool
  58. {
  59. id = obj.GetProperty("id").GetString(),
  60. name = obj.GetProperty("name").GetString(),
  61. schoolCode = obj.GetProperty("schoolCode").GetString(),
  62. province = obj.GetProperty("province").GetString(),
  63. city = obj.GetProperty("city").GetString(),
  64. dist = obj.GetProperty("dist").GetString(),
  65. picture = obj.GetProperty("picture").GetString(),
  66. period = obj.GetProperty("period").ToObject<List<Period>>().Select(x => x.name).ToList()
  67. };
  68. joinAreaSchools.Add(joinAreaSchool);
  69. }
  70. }
  71. }
  72. return Ok(new { state = 200, joinAreaSchools });
  73. }
  74. catch (Exception ex)
  75. {
  76. await _dingDing.SendBotMsg($"BI, {_option.Location} /area/get-areaschools \n {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  77. return BadRequest();
  78. }
  79. }
  80. /// <summary>
  81. /// 学校移出区域
  82. /// </summary>
  83. /// <param name="jsonElement"></param>
  84. /// <returns></returns>
  85. [ProducesDefaultResponseType]
  86. [AuthToken(Roles = "assist")]
  87. [HttpPost("set-areashiftschool")]
  88. public async Task<IActionResult> SetAreaShiftSchool(JsonElement jsonElement)
  89. {
  90. try
  91. {
  92. if (!jsonElement.TryGetProperty("tmdId", out JsonElement _tmdId)) return BadRequest(); //醍摩豆账户
  93. if (!jsonElement.TryGetProperty("tmdName", out JsonElement _tmdName)) return BadRequest(); //醍摩豆账号名称
  94. //if (!jsonElement.TryGetProperty("areaId", out JsonElement areaId)) return BadRequest();
  95. if (!jsonElement.TryGetProperty("schoolId", out JsonElement schoolId)) return BadRequest();
  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.SaveLog("area-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}{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. }