InitialAreaController.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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.Models;
  17. using Microsoft.Azure.Cosmos;
  18. using System;
  19. using System.Collections.Generic;
  20. using TEAMModelOS.SDK.Models.Cosmos;
  21. using System.Linq;
  22. using Pipelines.Sockets.Unofficial.Arenas;
  23. using TEAMModelBI.Models;
  24. namespace TEAMModelBI.Controllers.RepairApi
  25. {
  26. [Route("initialarea")]
  27. [ApiController]
  28. public class InitialAreaController : ControllerBase
  29. {
  30. private readonly AzureCosmosFactory _azureCosmos;
  31. private readonly DingDing _dingDing;
  32. private readonly Option _option;
  33. private readonly AzureStorageFactory _azureStorage;
  34. private readonly IConfiguration _configuration;
  35. private readonly AzureServiceBusFactory _serviceBus;
  36. private readonly IHttpClientFactory _http;
  37. private readonly CoreAPIHttpService _coreAPIHttpService;
  38. private readonly IWebHostEnvironment _environment; //读取文件
  39. public InitialAreaController(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage, IOptionsSnapshot<Option> option, IConfiguration configuration, AzureServiceBusFactory serviceBus, IHttpClientFactory http, CoreAPIHttpService coreAPIHttpService, IWebHostEnvironment hostingEnvironment)
  40. {
  41. _azureCosmos = azureCosmos;
  42. _dingDing = dingDing;
  43. _azureStorage = azureStorage;
  44. _option = option?.Value;
  45. _configuration = configuration;
  46. _serviceBus = serviceBus;
  47. _http = http;
  48. _coreAPIHttpService = coreAPIHttpService;
  49. _environment = hostingEnvironment;
  50. }
  51. /// <summary>
  52. /// 创建完整版的学区初始化
  53. /// </summary>
  54. /// <param name="jsonElement"></param>
  55. /// <returns></returns>
  56. [ProducesDefaultResponseType]
  57. [HttpPost("create-full-version")]
  58. public async Task<IActionResult> CreateFullVersion(JsonElement jsonElement)
  59. {
  60. if (!jsonElement.TryGetProperty("name", out JsonElement name)) return BadRequest();
  61. jsonElement.TryGetProperty("provCode", out JsonElement provCode);
  62. jsonElement.TryGetProperty("provName", out JsonElement provName);
  63. jsonElement.TryGetProperty("cityCode", out JsonElement cityCode);
  64. jsonElement.TryGetProperty("cityName", out JsonElement cityName);
  65. jsonElement.TryGetProperty("areaAdmin", out JsonElement areadAdmin);
  66. if (!jsonElement.TryGetProperty("standard", out JsonElement standard)) return BadRequest();
  67. if (!jsonElement.TryGetProperty("standardName", out JsonElement standardName)) return BadRequest();
  68. jsonElement.TryGetProperty("institution", out JsonElement institution);
  69. jsonElement.TryGetProperty("oldStandard", out JsonElement oldStandard);
  70. var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  71. List<CopyStandard> copyStand = new();
  72. if (!string.IsNullOrEmpty($"{oldStandard}"))
  73. copyStand = oldStandard.ToObject<List<CopyStandard>>();
  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").GetItemQueryIteratorSql<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. //创建区域
  144. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync<Area>(addArea, new PartitionKey("Base-Area"));
  145. //消息分区键
  146. string partitionCode = "copyAbility-mark";
  147. #region 复制微能力点到新区
  148. List<Task<ItemResponse<Ability>>> abilities = new(); //存储区域数据
  149. List<Task<ItemResponse<AbilityTask>>> abilityTasks = new(); //存储章节
  150. Area AreaS2 = null;
  151. List<Area> areas = new();
  152. await foreach (var itemArea in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIteratorSql<Area>(queryText: $"select value(c) from c where c.standard='standard2'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-Area") }))
  153. {
  154. areas.Add(itemArea);
  155. AreaS2 = itemArea;
  156. }
  157. await foreach (var itemArea in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIteratorSql<Area>(queryText: $"select value(c) from c where c.standard='standard3'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-Area") }))
  158. {
  159. areas.Add(itemArea);
  160. }
  161. foreach (var area in areas)
  162. {
  163. //查询要复制区域的能力标准点
  164. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIteratorSql<Ability>(queryText: $"select value(c) from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Ability-{area.standard}") }))
  165. {
  166. if (!string.IsNullOrEmpty(item.blob))
  167. {
  168. item.blob = item.blob.Replace($"/{area.standard}/", $"/{standard}/");
  169. };
  170. item.standard = $"{standard}";
  171. item.code = $"Ability-{standard}";
  172. item.school = $"{standard}";
  173. //添加区能力标准点
  174. abilities.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(item, new PartitionKey($"Ability-{standard}")));
  175. }
  176. try
  177. {
  178. if (abilities.Count < 256)
  179. {
  180. await Task.WhenAll(abilities);
  181. }
  182. else
  183. {
  184. int pages = (abilities.Count + 255) / 256;
  185. for (int i = 0; i < pages; i++)
  186. {
  187. List<Task<ItemResponse<Ability>>> tempAbility = abilities.Skip((i) * 256).Take(256).ToList();
  188. await Task.WhenAll(tempAbility);
  189. }
  190. }
  191. }
  192. catch
  193. {
  194. return Ok(new { state = 200, msg = "创区成功,能力标准点复制失败,遗留数据影响!" });
  195. }
  196. try
  197. {
  198. //微能力点
  199. await foreach (var atask in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIteratorSql<AbilityTask>(queryText: $"select value(c) from c ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"AbilityTask-{area.standard}") }))
  200. {
  201. List<Tnode> tnodes = new();
  202. foreach (Tnode tnode in atask.children)
  203. {
  204. if (tnode.rnodes != null)
  205. {
  206. List<Rnode> rnodes = new List<Rnode>();
  207. foreach (Rnode rnode in tnode.rnodes)
  208. {
  209. if (!string.IsNullOrEmpty($"{rnode.link}"))
  210. {
  211. rnode.link = rnode.link.Replace($"/{area.standard}/", $"/{standard}/");
  212. }
  213. rnodes.Add(rnode);
  214. }
  215. tnode.rnodes = rnodes;
  216. }
  217. tnodes.Add(tnode);
  218. }
  219. atask.children = tnodes;
  220. atask.code = $"AbilityTask-{standard}";
  221. atask.standard = $"{standard}";
  222. atask.codeval = $"{standard}";
  223. //添加区能力标准点中的节点
  224. //abilityTasks.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(atask, new PartitionKey($"AbilityTask-{standard}")));
  225. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(atask, new PartitionKey($"AbilityTask-{standard}"));
  226. }
  227. }
  228. catch
  229. {
  230. return Ok(new { state = 200, msg = "创区成功,能力标准创建成功,微能力点复制失败,遗留数据影响!" });
  231. }
  232. //执行复制操作
  233. //BatchCopyFile batchCopyFile = new();
  234. //batchCopyFile.blobCntr = "teammodelos";
  235. //batchCopyFile.oldFileName = $"{area.standard}";
  236. //batchCopyFile.newFileName = $"{standard}";
  237. //batchCopyFile.tmdid = $"{_tmdId}";
  238. //batchCopyFile.tmdIds = new List<string> { $"{_tmdId}" };
  239. //batchCopyFile.codeKey = partitionCode;
  240. //batchCopyFile.tmdName = $"{_tmdName}";
  241. //去除新建区,复制能力点文件
  242. //var messageBatchCopyFile = new ServiceBusMessage(batchCopyFile.ToJsonString());
  243. //messageBatchCopyFile.ApplicationProperties.Add("name", "CopyStandardFile");
  244. //try
  245. //{
  246. // await _serviceBus.GetServiceBusClient().SendMessageAsync(activeTask, messageBatchCopyFile);
  247. // await serBusClient.SendMessageAsync(activeTask, messageBatchCopyFile);
  248. //}
  249. //catch (Exception)
  250. //{
  251. // return Ok(new { state = 201, msg = "能力点复制成功,复制能力点的文件失败," });
  252. //}
  253. }
  254. if (AreaS2 != null)
  255. {
  256. //新政策文件
  257. await foreach (StandardFile standardFile in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIteratorSql<StandardFile>(queryText: $"select value(c) from c where c.id='{AreaS2.id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"StandardFile") }))
  258. {
  259. if (standardFile != null)
  260. {
  261. standardFile.standard = $"{standard}";
  262. standardFile.id = areaId;
  263. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(standardFile, new PartitionKey($"StandardFile"));
  264. }
  265. }
  266. //新的区域设置
  267. await foreach (AreaSetting areaSetting in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIteratorSql<AreaSetting>(queryText: $"select value(c) from c where c.id='{AreaS2.id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("AreaSetting") }))
  268. {
  269. if (areaSetting != null)
  270. {
  271. areaSetting.accessConfig = null;
  272. areaSetting.id = areaId;
  273. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(areaSetting, new PartitionKey($"AreaSetting"));
  274. }
  275. }
  276. }
  277. else return Ok(new { state = 201, message = "未找到默认能力点!" });
  278. #endregion
  279. return Ok(new { sate = RespondCode.Ok });
  280. }
  281. }
  282. }