InitialAreaController.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.Extensions.Configuration;
  4. using Microsoft.Extensions.Options;
  5. using System.Net.Http;
  6. using TEAMModelOS.SDK.DI;
  7. using TEAMModelOS.SDK;
  8. using TEAMModelOS.Models;
  9. using System.Text.Json;
  10. using System.Threading.Tasks;
  11. using TEAMModelOS.SDK.Context.Constant;
  12. using Azure.Messaging.ServiceBus;
  13. using TEAMModelBI.Tool.Extension;
  14. using TEAMModelOS.SDK.Extension;
  15. using Microsoft.AspNetCore.Hosting;
  16. using TEAMModelOS.SDK.DI.CoreAPI;
  17. using TEAMModelOS.SDK.Models;
  18. using Azure.Cosmos;
  19. using System;
  20. using System.Collections.Generic;
  21. namespace TEAMModelBI.Controllers.RepairApi
  22. {
  23. [Route("initialarea")]
  24. [ApiController]
  25. public class InitialAreaController : ControllerBase
  26. {
  27. private readonly AzureCosmosFactory _azureCosmos;
  28. private readonly DingDing _dingDing;
  29. private readonly Option _option;
  30. private readonly AzureStorageFactory _azureStorage;
  31. private readonly IConfiguration _configuration;
  32. private readonly NotificationService _notificationService;
  33. private readonly AzureServiceBusFactory _serviceBus;
  34. private readonly IHttpClientFactory _http;
  35. private readonly CoreAPIHttpService _coreAPIHttpService;
  36. private readonly IWebHostEnvironment _environment; //读取文件
  37. public InitialAreaController(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage, IOptionsSnapshot<Option> option, IConfiguration configuration, NotificationService notificationService, AzureServiceBusFactory serviceBus, IHttpClientFactory http, CoreAPIHttpService coreAPIHttpService, IWebHostEnvironment hostingEnvironment)
  38. {
  39. _azureCosmos = azureCosmos;
  40. _dingDing = dingDing;
  41. _azureStorage = azureStorage;
  42. _option = option?.Value;
  43. _configuration = configuration;
  44. _notificationService = notificationService;
  45. _serviceBus = serviceBus;
  46. _http = http;
  47. _coreAPIHttpService = coreAPIHttpService;
  48. _environment = hostingEnvironment;
  49. }
  50. /// <summary>
  51. /// 创建完整版的学区初始化
  52. /// </summary>
  53. /// <param name="jsonElement"></param>
  54. /// <returns></returns>
  55. [HttpPost("create-full-version")]
  56. public async Task<IActionResult> CreateFullVersion(JsonElement jsonElement)
  57. {
  58. if (!jsonElement.TryGetProperty("name", out JsonElement name)) return BadRequest();
  59. jsonElement.TryGetProperty("provCode", out JsonElement provCode);
  60. jsonElement.TryGetProperty("provName", out JsonElement provName);
  61. jsonElement.TryGetProperty("cityCode", out JsonElement cityCode);
  62. jsonElement.TryGetProperty("cityName", out JsonElement cityName);
  63. jsonElement.TryGetProperty("areaAdmin", out JsonElement areadAdmin);
  64. if (!jsonElement.TryGetProperty("standard", out JsonElement standard)) return BadRequest();
  65. if (!jsonElement.TryGetProperty("standardName", out JsonElement standardName)) return BadRequest();
  66. jsonElement.TryGetProperty("institution", out JsonElement institution);
  67. jsonElement.TryGetProperty("oldId", out JsonElement _oldId);
  68. jsonElement.TryGetProperty("oldStandard", out JsonElement oldStandard);
  69. jsonElement.TryGetProperty("oldName", out JsonElement oldName);
  70. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  71. var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  72. //操作记录实体
  73. var tempStandard = !string.IsNullOrEmpty($"{oldStandard}") && !string.IsNullOrEmpty($"{_oldId}") ? $"{oldStandard}" : "standard2";
  74. var cosmosClient = _azureCosmos.GetCosmosClient(); //数据库连接
  75. var tableClient = _azureStorage.GetCloudTableClient();
  76. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  77. ServiceBusClient serBusClient;
  78. try
  79. {
  80. serBusClient = _serviceBus.GetServiceBusClient();
  81. }
  82. catch
  83. {
  84. return Ok(new { state = 403, msg = "Functionn未启动,请联系管理员" });
  85. }
  86. var activeTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
  87. var table = tableClient.GetTableReference("IESLogin");
  88. //查询新的是否存在
  89. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<Area>(queryText: $"select value(c) from c where c.standard='{standard}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-Area") }))
  90. {
  91. if (item.standard.Equals($"{standard}"))
  92. return Ok(new { state = 1, message = "新创区的standard已存在请检查" });
  93. }
  94. //区级的ID
  95. string areaId = Guid.NewGuid().ToString();
  96. Area addArea = new()
  97. {
  98. id = areaId,
  99. code = $"Base-Area",
  100. name = $"{name}",
  101. provCode = $"{provCode}",
  102. provName = $"{provName}",
  103. cityCode = $"{cityCode}",
  104. cityName = $"{cityName}",
  105. standard = $"{standard}",
  106. standardName = $"{standardName}",
  107. institution = $"{institution}"
  108. };
  109. #region 区级管理员
  110. var coreUser = await _coreAPIHttpService.GetUserInfo(new Dictionary<string, string> { { "key", $"{areadAdmin}" } }, _option.Location, _configuration);
  111. if (coreUser == null || coreUser.id == null)
  112. return Ok(new { state = 404, msg = "未找到改账户的管理员" });
  113. //string tmdId = !string.IsNullOrEmpty(tempTmdId) ? tempTmdId : $"{areadAdmin}";
  114. Teacher teacher = null;
  115. try
  116. {
  117. //查询该教师是否存在
  118. teacher = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<Teacher>($"{coreUser.id}", new PartitionKey("Base"));
  119. }
  120. catch
  121. {
  122. }
  123. if (teacher != null)
  124. {
  125. //教师存在,在该教师信息中添加要管理的学校信息
  126. teacher.areas.Add(new Teacher.TeacherArea { areaId = addArea.id, status = "join", name = addArea.name });
  127. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync<Teacher>(teacher, teacher.id, new PartitionKey("Base"));
  128. }
  129. else
  130. {
  131. teacher.id = coreUser.id;
  132. teacher.name = coreUser.name;
  133. teacher.picture = coreUser.picture;
  134. teacher.pk = "Base";
  135. teacher.code = "Base";
  136. teacher.size = 1;
  137. teacher.createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  138. //教师存在,在该教师信息中添加要管理的学校信息
  139. teacher.areas.Add(new Teacher.TeacherArea { areaId = addArea.id, status = "join", name = addArea.name });
  140. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").CreateItemAsync<Teacher>(teacher, new PartitionKey("Base"));
  141. }
  142. #endregion
  143. return Ok(new { sate = RespondCode.Ok });
  144. }
  145. }
  146. }