InitialAreaController.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 Azure.Cosmos;
  18. using System;
  19. using System.Collections.Generic;
  20. using TEAMModelOS.SDK.Models.Cosmos.Common;
  21. using System.Linq;
  22. using Pipelines.Sockets.Unofficial.Arenas;
  23. using TEAMModelOS.SDK.Models.Cosmos.BI;
  24. using TEAMModelBI.Filter;
  25. using HTEXLib.COMM.Helpers;
  26. using TEAMModelBI.Models;
  27. namespace TEAMModelBI.Controllers.RepairApi
  28. {
  29. [Route("initialarea")]
  30. [ApiController]
  31. public class InitialAreaController : ControllerBase
  32. {
  33. private readonly AzureCosmosFactory _azureCosmos;
  34. private readonly DingDing _dingDing;
  35. private readonly Option _option;
  36. private readonly AzureStorageFactory _azureStorage;
  37. private readonly IConfiguration _configuration;
  38. private readonly AzureServiceBusFactory _serviceBus;
  39. private readonly IHttpClientFactory _http;
  40. private readonly CoreAPIHttpService _coreAPIHttpService;
  41. private readonly IWebHostEnvironment _environment; //读取文件
  42. public InitialAreaController(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage, IOptionsSnapshot<Option> option, IConfiguration configuration, AzureServiceBusFactory serviceBus, IHttpClientFactory http, CoreAPIHttpService coreAPIHttpService, IWebHostEnvironment hostingEnvironment)
  43. {
  44. _azureCosmos = azureCosmos;
  45. _dingDing = dingDing;
  46. _azureStorage = azureStorage;
  47. _option = option?.Value;
  48. _configuration = configuration;
  49. _serviceBus = serviceBus;
  50. _http = http;
  51. _coreAPIHttpService = coreAPIHttpService;
  52. _environment = hostingEnvironment;
  53. }
  54. /// <summary>
  55. /// 创建完整版的学区初始化
  56. /// </summary>
  57. /// <param name="jsonElement"></param>
  58. /// <returns></returns>
  59. [ProducesDefaultResponseType]
  60. [HttpPost("create-full-version")]
  61. public async Task<IActionResult> CreateFullVersion(JsonElement jsonElement)
  62. {
  63. if (!jsonElement.TryGetProperty("name", out JsonElement name)) return BadRequest();
  64. jsonElement.TryGetProperty("provCode", out JsonElement provCode);
  65. jsonElement.TryGetProperty("provName", out JsonElement provName);
  66. jsonElement.TryGetProperty("cityCode", out JsonElement cityCode);
  67. jsonElement.TryGetProperty("cityName", out JsonElement cityName);
  68. jsonElement.TryGetProperty("areaAdmin", out JsonElement areadAdmin);
  69. if (!jsonElement.TryGetProperty("standard", out JsonElement standard)) return BadRequest();
  70. if (!jsonElement.TryGetProperty("standardName", out JsonElement standardName)) return BadRequest();
  71. jsonElement.TryGetProperty("institution", out JsonElement institution);
  72. jsonElement.TryGetProperty("oldStandard", out JsonElement oldStandard);
  73. var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  74. List<CopyStandard> copyStand = new();
  75. if (!string.IsNullOrEmpty($"{oldStandard}"))
  76. copyStand = oldStandard.ToObject<List<CopyStandard>>();
  77. var cosmosClient = _azureCosmos.GetCosmosClient(); //数据库连接
  78. var tableClient = _azureStorage.GetCloudTableClient();
  79. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  80. ServiceBusClient serBusClient;
  81. try
  82. {
  83. serBusClient = _serviceBus.GetServiceBusClient();
  84. }
  85. catch
  86. {
  87. return Ok(new { state = 403, msg = "Functionn未启动,请联系管理员" });
  88. }
  89. var activeTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
  90. var table = tableClient.GetTableReference("IESLogin");
  91. //查询新的是否存在
  92. 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") }))
  93. {
  94. if (item.standard.Equals($"{standard}"))
  95. return Ok(new { state = 1, message = "新创区的standard已存在请检查" });
  96. }
  97. //区级的ID
  98. string areaId = Guid.NewGuid().ToString();
  99. Area addArea = new()
  100. {
  101. id = areaId,
  102. code = $"Base-Area",
  103. name = $"{name}",
  104. provCode = $"{provCode}",
  105. provName = $"{provName}",
  106. cityCode = $"{cityCode}",
  107. cityName = $"{cityName}",
  108. standard = $"{standard}",
  109. standardName = $"{standardName}",
  110. institution = $"{institution}"
  111. };
  112. #region 区级管理员
  113. var coreUser = await _coreAPIHttpService.GetUserInfo(new Dictionary<string, string> { { "key", $"{areadAdmin}" } }, _option.Location, _configuration);
  114. if (coreUser == null || coreUser.id == null)
  115. return Ok(new { state = 404, msg = "未找到改账户的管理员" });
  116. //string tmdId = !string.IsNullOrEmpty(tempTmdId) ? tempTmdId : $"{areadAdmin}";
  117. Teacher teacher = null;
  118. try
  119. {
  120. //查询该教师是否存在
  121. teacher = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<Teacher>($"{coreUser.id}", new PartitionKey("Base"));
  122. }
  123. catch
  124. {
  125. }
  126. if (teacher != null)
  127. {
  128. //教师存在,在该教师信息中添加要管理的学校信息
  129. teacher.areas.Add(new Teacher.TeacherArea { areaId = addArea.id, status = "join", name = addArea.name });
  130. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync<Teacher>(teacher, teacher.id, new PartitionKey("Base"));
  131. }
  132. else
  133. {
  134. teacher.id = coreUser.id;
  135. teacher.name = coreUser.name;
  136. teacher.picture = coreUser.picture;
  137. teacher.pk = "Base";
  138. teacher.code = "Base";
  139. teacher.size = 1;
  140. teacher.createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  141. //教师存在,在该教师信息中添加要管理的学校信息
  142. teacher.areas.Add(new Teacher.TeacherArea { areaId = addArea.id, status = "join", name = addArea.name });
  143. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").CreateItemAsync<Teacher>(teacher, new PartitionKey("Base"));
  144. }
  145. #endregion
  146. //创建区域
  147. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync<Area>(addArea, new PartitionKey("Base-Area"));
  148. //消息分区键
  149. string partitionCode = "copyAbility-mark";
  150. #region 复制微能力点到新区
  151. List<Task<ItemResponse<Ability>>> abilities = new(); //存储区域数据
  152. List<Task<ItemResponse<AbilityTask>>> abilityTasks = new(); //存储章节
  153. Area AreaS2 = null;
  154. List<Area> areas = new();
  155. await foreach (var itemArea in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<Area>(queryText: $"select value(c) from c where c.standard='standard2'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-Area") }))
  156. {
  157. areas.Add(itemArea);
  158. AreaS2 = itemArea;
  159. }
  160. await foreach (var itemArea in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<Area>(queryText: $"select value(c) from c where c.standard='standard3'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-Area") }))
  161. {
  162. areas.Add(itemArea);
  163. }
  164. foreach (var area in areas)
  165. {
  166. //查询要复制区域的能力标准点
  167. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<Ability>(queryText: $"select value(c) from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Ability-{area.standard}") }))
  168. {
  169. if (!string.IsNullOrEmpty(item.blob))
  170. {
  171. item.blob = item.blob.Replace($"/{area.standard}/", $"/{standard}/");
  172. };
  173. item.standard = $"{standard}";
  174. item.code = $"Ability-{standard}";
  175. item.school = $"{standard}";
  176. //添加区能力标准点
  177. abilities.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(item, new PartitionKey($"Ability-{standard}")));
  178. }
  179. try
  180. {
  181. if (abilities.Count < 256)
  182. {
  183. await Task.WhenAll(abilities);
  184. }
  185. else
  186. {
  187. int pages = (abilities.Count + 255) / 256;
  188. for (int i = 0; i < pages; i++)
  189. {
  190. List<Task<ItemResponse<Ability>>> tempAbility = abilities.Skip((i) * 256).Take(256).ToList();
  191. await Task.WhenAll(tempAbility);
  192. }
  193. }
  194. }
  195. catch
  196. {
  197. return Ok(new { state = 200, msg = "创区成功,能力标准点复制失败,遗留数据影响!" });
  198. }
  199. try
  200. {
  201. //微能力点
  202. await foreach (var atask in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<AbilityTask>(queryText: $"select value(c) from c ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"AbilityTask-{area.standard}") }))
  203. {
  204. List<Tnode> tnodes = new();
  205. foreach (Tnode tnode in atask.children)
  206. {
  207. if (tnode.rnodes != null)
  208. {
  209. List<Rnode> rnodes = new List<Rnode>();
  210. foreach (Rnode rnode in tnode.rnodes)
  211. {
  212. if (!string.IsNullOrEmpty($"{rnode.link}"))
  213. {
  214. rnode.link = rnode.link.Replace($"/{area.standard}/", $"/{standard}/");
  215. }
  216. rnodes.Add(rnode);
  217. }
  218. tnode.rnodes = rnodes;
  219. }
  220. tnodes.Add(tnode);
  221. }
  222. atask.children = tnodes;
  223. atask.code = $"AbilityTask-{standard}";
  224. atask.standard = $"{standard}";
  225. atask.codeval = $"{standard}";
  226. //添加区能力标准点中的节点
  227. //abilityTasks.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(atask, new PartitionKey($"AbilityTask-{standard}")));
  228. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(atask, new PartitionKey($"AbilityTask-{standard}"));
  229. }
  230. }
  231. catch
  232. {
  233. return Ok(new { state = 200, msg = "创区成功,能力标准创建成功,微能力点复制失败,遗留数据影响!" });
  234. }
  235. //执行复制操作
  236. //BatchCopyFile batchCopyFile = new();
  237. //batchCopyFile.blobCntr = "teammodelos";
  238. //batchCopyFile.oldFileName = $"{area.standard}";
  239. //batchCopyFile.newFileName = $"{standard}";
  240. //batchCopyFile.tmdid = $"{_tmdId}";
  241. //batchCopyFile.tmdIds = new List<string> { $"{_tmdId}" };
  242. //batchCopyFile.codeKey = partitionCode;
  243. //batchCopyFile.tmdName = $"{_tmdName}";
  244. //去除新建区,复制能力点文件
  245. //var messageBatchCopyFile = new ServiceBusMessage(batchCopyFile.ToJsonString());
  246. //messageBatchCopyFile.ApplicationProperties.Add("name", "CopyStandardFile");
  247. //try
  248. //{
  249. // await _serviceBus.GetServiceBusClient().SendMessageAsync(activeTask, messageBatchCopyFile);
  250. // await serBusClient.SendMessageAsync(activeTask, messageBatchCopyFile);
  251. //}
  252. //catch (Exception)
  253. //{
  254. // return Ok(new { state = 201, msg = "能力点复制成功,复制能力点的文件失败," });
  255. //}
  256. }
  257. if (AreaS2 != null)
  258. {
  259. //新政策文件
  260. await foreach (StandardFile standardFile in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<StandardFile>(queryText: $"select value(c) from c where c.id='{AreaS2.id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"StandardFile") }))
  261. {
  262. if (standardFile != null)
  263. {
  264. standardFile.standard = $"{standard}";
  265. standardFile.id = areaId;
  266. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(standardFile, new PartitionKey($"StandardFile"));
  267. }
  268. }
  269. //新的区域设置
  270. await foreach (AreaSetting areaSetting in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<AreaSetting>(queryText: $"select value(c) from c where c.id='{AreaS2.id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("AreaSetting") }))
  271. {
  272. if (areaSetting != null)
  273. {
  274. areaSetting.accessConfig = null;
  275. areaSetting.id = areaId;
  276. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(areaSetting, new PartitionKey($"AreaSetting"));
  277. }
  278. }
  279. }
  280. else return Ok(new { state = 201, message = "未找到默认能力点!" });
  281. #endregion
  282. return Ok(new { sate = RespondCode.Ok });
  283. }
  284. }
  285. }