AreaRelevantController.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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.DI.BIAzureStorage;
  11. using TEAMModelBI.Filter;
  12. using TEAMModelBI.Tool.Context;
  13. using TEAMModelBI.Tool.Extension;
  14. using TEAMModelOS.Models;
  15. using TEAMModelOS.SDK.DI;
  16. using TEAMModelOS.SDK.Extension;
  17. using TEAMModelOS.SDK.Models;
  18. using TEAMModelOS.SDK.Models.Cosmos.BI;
  19. namespace TEAMModelBI.Controllers.BISchool
  20. {
  21. [Route("area")]
  22. [ApiController]
  23. public class AreaRelevantController : ControllerBase
  24. {
  25. //数据容器
  26. private readonly AzureCosmosFactory _azureCosmos;
  27. private readonly AzureStorageFactory _azureStorage;
  28. //钉钉提示信息
  29. private readonly DingDing _dingDing;
  30. private readonly Option _option;
  31. public AreaRelevantController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option)
  32. {
  33. _azureCosmos = azureCosmos;
  34. _azureStorage = azureStorage;
  35. _dingDing = dingDing;
  36. _option = option?.Value;
  37. }
  38. /// <summary>
  39. /// 查询区域已经有的学校
  40. /// </summary>
  41. /// <param name="jsonElement"></param>
  42. /// <returns></returns>
  43. [ProducesDefaultResponseType]
  44. [HttpPost("get-areaschools")]
  45. public async Task<IActionResult> GetAreaSchools(JsonElement jsonElement)
  46. {
  47. try
  48. {
  49. jsonElement.TryGetProperty("areaId", out JsonElement _areaId);
  50. jsonElement.TryGetProperty("site", out JsonElement site);
  51. var cosmosClient = _azureCosmos.GetCosmosClient();
  52. if ($"{site}".Equals(BIConst.GlobalSite))
  53. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.GlobalSite);
  54. List<JoinAreaSchool> joinAreaSchools = new();
  55. string sqltxt = $"SELECT c.id,c.name,c.schoolCode,c.province,c.city,c.dist,c.picture,c.period,c.areaId,c.standard,c.manyAreas FROM c WHERE c.areaId='{_areaId}'";
  56. if (!string.IsNullOrEmpty($"{_areaId}"))
  57. {
  58. sqltxt = $"SELECT c.id,c.name,c.schoolCode,c.province,c.city,c.dist,c.picture,c.period,c.areaId,c.standard,c.manyAreas FROM c join m in c.manyAreas WHERE c.areaId ='{_areaId}' or m.areaId='{_areaId}'";
  59. //sqltxt = $"SELECT c.id,c.name,c.schoolCode,c.province,c.city,c.dist,c.picture,c.period FROM c join m in c.manyAreas where m.areaId='{_areaId}' or c.areaId='{_areaId}'";
  60. }
  61. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: sqltxt, requestOptions:new QueryRequestOptions() { PartitionKey = new PartitionKey("Base")}))
  62. {
  63. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  64. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  65. {
  66. foreach(var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  67. {
  68. JoinAreaSchool joinAreaSchool = new JoinAreaSchool
  69. {
  70. id = obj.GetProperty("id").GetString(),
  71. name = obj.GetProperty("name").GetString(),
  72. schoolCode = obj.GetProperty("schoolCode").GetString(),
  73. province = obj.GetProperty("province").GetString(),
  74. city = obj.GetProperty("city").GetString(),
  75. dist = obj.GetProperty("dist").GetString(),
  76. picture = obj.GetProperty("picture").GetString(),
  77. period = obj.GetProperty("period").ToObject<List<Period>>().Select(x => x.name).ToList(),
  78. areaId = obj.GetProperty("areaId").GetString(),
  79. standard = obj.GetProperty("standard").GetString()
  80. };
  81. try
  82. {
  83. joinAreaSchool.manyAreas = obj.GetProperty("manyAreas").ToObject<List<ManyArea>>();
  84. }
  85. catch { }
  86. joinAreaSchools.Add(joinAreaSchool);
  87. }
  88. }
  89. }
  90. return Ok(new { state = 200, joinAreaSchools });
  91. }
  92. catch (Exception ex)
  93. {
  94. await _dingDing.SendBotMsg($"BI, {_option.Location} /area/get-areaschools \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  95. return BadRequest();
  96. }
  97. }
  98. /// <summary>
  99. /// 学校移出区域
  100. /// </summary>
  101. /// <param name="jsonElement"></param>
  102. /// <returns></returns>
  103. [ProducesDefaultResponseType]
  104. [AuthToken(Roles = "admin,,rdc")]
  105. [HttpPost("set-areashiftschool")]
  106. public async Task<IActionResult> SetAreaShiftSchool(JsonElement jsonElement)
  107. {
  108. try
  109. {
  110. if (!jsonElement.TryGetProperty("schoolId", out JsonElement schoolId)) return BadRequest();
  111. jsonElement.TryGetProperty("areaId", out JsonElement areaId);
  112. jsonElement.TryGetProperty("standard", out JsonElement standard);
  113. jsonElement.TryGetProperty("isDefault", out JsonElement isDefault);
  114. jsonElement.TryGetProperty("site", out JsonElement site);
  115. var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  116. var cosmosClient = _azureCosmos.GetCosmosClient();
  117. var tableClient = _azureStorage.GetCloudTableClient();
  118. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  119. if ($"{site}".Equals(BIConst.GlobalSite))
  120. {
  121. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.GlobalSite);
  122. tableClient = _azureStorage.GetCloudTableClient(BIConst.GlobalSite);
  123. blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.GlobalSite);
  124. }
  125. School tempSchool = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>($"{schoolId}", new PartitionKey("Base"));
  126. if (bool.Parse($"{isDefault}") == true)
  127. {
  128. tempSchool.areaId = null;
  129. tempSchool.standard = null;
  130. }
  131. if (tempSchool.manyAreas.Count > 0)
  132. {
  133. if (bool.Parse($"{isDefault}") == true)
  134. {
  135. tempSchool.areaId = "";
  136. tempSchool.standard = "";
  137. }
  138. if (tempSchool.manyAreas != null)
  139. {
  140. if (!string.IsNullOrEmpty($"{areaId}"))
  141. {
  142. var temp = tempSchool.manyAreas.Find(ma => ma.areaId == $"{areaId}");
  143. if (temp != null)
  144. tempSchool.manyAreas.Remove(temp);
  145. }
  146. if (!string.IsNullOrEmpty($"{standard}"))
  147. {
  148. var temp = tempSchool.manyAreas.Find(ma => ma.standard == $"{standard}");
  149. if (temp != null)
  150. tempSchool.manyAreas.Remove(temp);
  151. }
  152. }
  153. }
  154. School school = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<School>(tempSchool, tempSchool.id, new PartitionKey("Base"));
  155. //保存操作记录
  156. //await _azureStorage.SaveBILog("school-update", $"{_tmdName}【{_tmdId}】已操作学校({schoolId})移除该区域", _dingDing, httpContext: HttpContext);
  157. await BIAzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "school-update", $"{_tmdName}【{_tmdId}】已操作学校({schoolId})移除该区域", _dingDing, httpContext: HttpContext);
  158. return Ok(new { state = 200, school });
  159. }
  160. catch (Exception ex)
  161. {
  162. await _dingDing.SendBotMsg($"BI, {_option.Location} /area/set-areashiftschool \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  163. return BadRequest();
  164. }
  165. }
  166. /// <summary>
  167. /// 已加入区域的学校
  168. /// </summary>
  169. public record JoinAreaSchool
  170. {
  171. public string id { get; set; }
  172. public string name { get; set; }
  173. public string schoolCode { get; set; }
  174. public string picture { get; set; }
  175. public string areaId{ get; set; }
  176. public string standard { get; set; }
  177. public List<string> period { get; set; }
  178. public string province { get; set; }
  179. public string city { get; set; }
  180. public string dist { get; set; }
  181. public List<ManyArea> manyAreas { get; set; } = new List<ManyArea>();
  182. }
  183. }
  184. }