AreaRelevantController.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. //钉钉提示信息
  25. private readonly DingDing _dingDing;
  26. private readonly Option _option;
  27. public AreaRelevantController(AzureCosmosFactory azureCosmos, DingDing dingDing, IOptionsSnapshot<Option> option)
  28. {
  29. _azureCosmos = azureCosmos;
  30. _dingDing = dingDing;
  31. _option = option?.Value;
  32. }
  33. /// <summary>
  34. /// 查询区域已经有的学校
  35. /// </summary>
  36. /// <param name="jsonElement"></param>
  37. /// <returns></returns>
  38. [ProducesDefaultResponseType]
  39. [HttpPost("get-areaschools")]
  40. public async Task<IActionResult> GetAreaSchools(JsonElement jsonElement)
  41. {
  42. try
  43. {
  44. if (!jsonElement.TryGetProperty("areaId", out JsonElement _areaId)) return BadRequest();
  45. var cosmosClient = _azureCosmos.GetCosmosClient();
  46. List<JoinAreaSchool> joinAreaSchools = new List<JoinAreaSchool>();
  47. 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}'";
  48. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: slqtxt,requestOptions:new QueryRequestOptions() { PartitionKey = new PartitionKey("Base")}))
  49. {
  50. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  51. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  52. {
  53. foreach(var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  54. {
  55. JoinAreaSchool joinAreaSchool = new JoinAreaSchool
  56. {
  57. id = obj.GetProperty("id").GetString(),
  58. name = obj.GetProperty("name").GetString(),
  59. schoolCode = obj.GetProperty("schoolCode").GetString(),
  60. province = obj.GetProperty("province").GetString(),
  61. city = obj.GetProperty("city").GetString(),
  62. dist = obj.GetProperty("dist").GetString(),
  63. picture = obj.GetProperty("picture").GetString(),
  64. period = obj.GetProperty("period").ToObject<List<Period>>().Select(x => x.name).ToList()
  65. };
  66. joinAreaSchools.Add(joinAreaSchool);
  67. }
  68. }
  69. }
  70. return Ok(new { state = 200, joinAreaSchools });
  71. }
  72. catch (Exception ex)
  73. {
  74. await _dingDing.SendBotMsg($"BI, {_option.Location} /area/get-areaschools \n {ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  75. return BadRequest();
  76. }
  77. }
  78. /// <summary>
  79. /// 学校移出区域
  80. /// </summary>
  81. /// <param name="jsonElement"></param>
  82. /// <returns></returns>
  83. [ProducesDefaultResponseType]
  84. [HttpPost("set-areashiftschool")]
  85. //[AuthToken(Roles = "assist")]
  86. public async Task<IActionResult> SetAreaShiftSchool(JsonElement jsonElement)
  87. {
  88. try
  89. {
  90. if (!jsonElement.TryGetProperty("tmdId", out JsonElement _tmdId)) return BadRequest(); //醍摩豆账户
  91. if (!jsonElement.TryGetProperty("tmdName", out JsonElement _tmdName)) return BadRequest(); //醍摩豆账号名称
  92. //if (!jsonElement.TryGetProperty("areaId", out JsonElement areaId)) return BadRequest();
  93. if (!jsonElement.TryGetProperty("schoolId", out JsonElement schoolId)) return BadRequest();
  94. //操作记录
  95. OperateLog operateLog = new OperateLog();
  96. string blobOrTable = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString();
  97. operateLog.PartitionKey = "OperateLog-BI";
  98. operateLog.RowKey = blobOrTable;
  99. operateLog.recordID = blobOrTable;
  100. operateLog.platformSource = "BI";
  101. operateLog.tmdId = $"{_tmdId}";
  102. operateLog.tmdName = $"{_tmdName}";
  103. operateLog.visitApi = "/tabledd/set-permissions";
  104. operateLog.operateTime = DateTime.Now;
  105. operateLog.operateDescribe = $"{_tmdName}【{_tmdId}】已操作学校({schoolId})移除该区域";
  106. var cosmosClient = _azureCosmos.GetCosmosClient();
  107. School tempSchool = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>($"{schoolId}", new PartitionKey("Base"));
  108. tempSchool.areaId = null;
  109. tempSchool.standard = null;
  110. School school = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<School>(tempSchool, tempSchool.id, new PartitionKey("Base"));
  111. return Ok(new { state = 200, school });
  112. }
  113. catch (Exception ex)
  114. {
  115. await _dingDing.SendBotMsg($"BI, {_option.Location} /area/set-areashiftschool \n {ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  116. return BadRequest();
  117. }
  118. }
  119. /// <summary>
  120. /// 已加入区域的学校
  121. /// </summary>
  122. public record JoinAreaSchool
  123. {
  124. public string id { get; set; }
  125. public string name { get; set; }
  126. public string schoolCode { get; set; }
  127. public string picture { get; set; }
  128. public List<string> period { get; set; }
  129. public string province { get; set; }
  130. public string city { get; set; }
  131. public string dist { get; set; }
  132. }
  133. }
  134. }