BatchAreaController.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.Extensions.Configuration;
  4. using Microsoft.Extensions.Options;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using TEAMModelOS.Models;
  10. using TEAMModelOS.SDK.DI;
  11. using TEAMModelOS.SDK.Models;
  12. using Azure.Cosmos;
  13. using System.Text.Json;
  14. using HTEXLib.COMM.Helpers;
  15. using TEAMModelOS.SDK.Models.Cosmos.Common;
  16. using TEAMModelOS.SDK.Models.Cosmos.BI;
  17. using Azure.Messaging.ServiceBus;
  18. using TEAMModelOS.SDK.Extension;
  19. using TEAMModelOS.SDK.Models.Service;
  20. using TEAMModelBI.Filter;
  21. using TEAMModelBI.Tool.Extension;
  22. namespace TEAMModelBI.Controllers.BISchool
  23. {
  24. [Route("batcharea")]
  25. [ApiController]
  26. public class BatchAreaController : ControllerBase
  27. {
  28. private readonly AzureCosmosFactory _azureCosmos;
  29. private readonly DingDing _dingDing;
  30. private readonly Option _option;
  31. private readonly AzureStorageFactory _azureStorage;
  32. private readonly IConfiguration _configuration;
  33. private readonly NotificationService _notificationService;
  34. private readonly AzureServiceBusFactory _serviceBus;
  35. public BatchAreaController(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage, IOptionsSnapshot<Option> option, IConfiguration configuration, NotificationService notificationService, AzureServiceBusFactory serviceBus)
  36. {
  37. _azureCosmos = azureCosmos;
  38. _dingDing = dingDing;
  39. _azureStorage = azureStorage;
  40. _option = option?.Value;
  41. _configuration = configuration;
  42. _notificationService = notificationService;
  43. _serviceBus = serviceBus;
  44. }
  45. /// <summary>
  46. /// 查询所有的区域标准
  47. /// </summary>
  48. /// <returns></returns>
  49. [ProducesDefaultResponseType]
  50. [HttpPost("get-areas")]
  51. public async Task<IActionResult> GetArea()
  52. {
  53. try
  54. {
  55. List<Area> areas = new List<Area>();
  56. var azureClient = _azureCosmos.GetCosmosClient();
  57. await foreach (var item in azureClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<Area>(queryText: $"select * from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base-Area") }))
  58. {
  59. areas.Add(item);
  60. }
  61. return Ok(new { state = 200, areas });
  62. }
  63. catch (Exception ex)
  64. {
  65. await _dingDing.SendBotMsg($"BI,{_option.Location} batcharea/get-areas \n {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  66. return BadRequest();
  67. }
  68. }
  69. /// <summary>
  70. /// 批量创区
  71. /// </summary>
  72. /// <param name="areas"></param>
  73. /// <returns></returns>
  74. [ProducesDefaultResponseType]
  75. [HttpPost("upd-area")]
  76. public async Task<IActionResult> batchCreateArea(List<Area> areas)
  77. {
  78. try
  79. {
  80. List<Area> standards = new List<Area>();
  81. bool isCreate = true;
  82. if (areas.Count > 0)
  83. {
  84. foreach (Area itemarea in areas)
  85. {
  86. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<Area>(queryText: $"select value(c) from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-Area") }))
  87. {
  88. if (item.standard.Equals(itemarea.standard))
  89. {
  90. standards.Add(itemarea);
  91. isCreate = false;
  92. }
  93. }
  94. if (isCreate == true)
  95. {
  96. Area addArea = new Area()
  97. {
  98. id = Guid.NewGuid().ToString(),
  99. code = $"Base-Area",
  100. name = itemarea.name,
  101. provCode = itemarea.provCode,
  102. provName = itemarea.provName,
  103. cityCode = itemarea.cityCode,
  104. cityName = itemarea.cityName,
  105. standard = itemarea.standard,
  106. standardName = itemarea.standardName,
  107. institution = itemarea.institution
  108. };
  109. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync<Area>(addArea, new PartitionKey("Base-Area"));
  110. }
  111. }
  112. }
  113. else return Ok(new { state = 1, message = "区域参数为空" });
  114. if (standards.Count > 0)
  115. return Ok(new { state = 201, message = "已有部分区域批量创建成功;标准项已重复!请检查标准项!", standards = standards });
  116. else return Ok(new { state = 200, message = "批量创区全部完成", });
  117. }
  118. catch (Exception ex)
  119. {
  120. await _dingDing.SendBotMsg($"BI,{_option.Location} batcharea/batch-createarea \n {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  121. return BadRequest();
  122. }
  123. }
  124. /// <summary>
  125. /// 批量创区 新接口
  126. /// </summary>
  127. /// <param name="jsonElement"></param>
  128. /// <returns></returns>
  129. [ProducesDefaultResponseType]
  130. [AuthToken(Roles = "assist")]
  131. [HttpPost("batch-area")]
  132. public async Task<IActionResult> batchArea(JsonElement jsonElement)
  133. {
  134. try
  135. {
  136. if (!jsonElement.TryGetProperty("name", out JsonElement name)) return BadRequest();
  137. jsonElement.TryGetProperty("provCode", out JsonElement provCode);
  138. jsonElement.TryGetProperty("provName", out JsonElement provName);
  139. jsonElement.TryGetProperty("cityCode", out JsonElement cityCode);
  140. jsonElement.TryGetProperty("cityName", out JsonElement cityName);
  141. if (!jsonElement.TryGetProperty("standard", out JsonElement standard)) return BadRequest();
  142. if (!jsonElement.TryGetProperty("standardName", out JsonElement standardName)) return BadRequest();
  143. jsonElement.TryGetProperty("institution", out JsonElement institution);
  144. jsonElement.TryGetProperty("oldId", out JsonElement _oldId);
  145. jsonElement.TryGetProperty("oldStandard", out JsonElement oldStandard);
  146. var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  147. //操作记录实体
  148. var tempStandard = !string.IsNullOrEmpty($"{oldStandard}") && !string.IsNullOrEmpty($"{_oldId}") ? $"{oldStandard}" : "standard2";
  149. var cosmosClient = _azureCosmos.GetCosmosClient();//数据库连接
  150. //查询新的是否存在
  151. 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") }))
  152. {
  153. if (item.standard.Equals($"{standard}"))
  154. {
  155. return Ok(new { state = 1, message = "新创区的standard已存在请检查" });
  156. }
  157. }
  158. //区级的ID
  159. string areaId = Guid.NewGuid().ToString();
  160. Area addArea = new Area()
  161. {
  162. id = areaId,
  163. code = $"Base-Area",
  164. name = $"{name}",
  165. provCode = $"{provCode}",
  166. provName = $"{provName}",
  167. cityCode = $"{cityCode}",
  168. cityName = $"{cityName}",
  169. standard = $"{standard}",
  170. standardName = $"{standardName}",
  171. institution = $"{institution}"
  172. };
  173. //创建区域
  174. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync<Area>(addArea, new PartitionKey("Base-Area"));
  175. //消息分区键
  176. string partitionCode = "copyAbility-mark";
  177. #region 使用Function创区
  178. //string areaCode = "copyArea-mark";
  179. //BatchCopyAreaRelevant bCopyArea = new BatchCopyAreaRelevant()
  180. //{
  181. // tmdid = $"{_tmdId}",
  182. // tmdName = $"{_tmdName}",
  183. // codeKey = areaCode,
  184. // tmdIds = new List<string> { $"{_tmdId}"},
  185. // oldStandard = $"{oldStandard}",
  186. // oldId = $"{_oldId}",
  187. // standard = $"{standard}",
  188. // areaId = areaId,
  189. // cut="",
  190. //};
  191. //var messageBatchCopyArea = new ServiceBusMessage(bCopyArea.ToJsonString());
  192. //messageBatchCopyArea.ApplicationProperties.Add("name", "CopyAreaRelevant");
  193. //var activeTaskArea = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
  194. //await _serviceBus.GetServiceBusClient().SendMessageAsync(activeTaskArea, messageBatchCopyArea);
  195. #endregion
  196. #region 旧的批量创区
  197. List<Task<ItemResponse<Ability>>> abilities = new List<Task<ItemResponse<Ability>>>(); //存储区域数据
  198. List<Task<ItemResponse<AbilityTask>>> abilityTasks = new List<Task<ItemResponse<AbilityTask>>>(); //存储章节
  199. if (!string.IsNullOrEmpty($"{oldStandard}") && !string.IsNullOrEmpty($"{_oldId}"))
  200. {
  201. //查询要复制区域的能力标准点
  202. 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-{oldStandard}") }))
  203. {
  204. if (!string.IsNullOrEmpty(item.blob))
  205. {
  206. item.blob = item.blob.Replace($"/{oldStandard}/", $"/{standard}/");
  207. };
  208. item.standard = $"{standard}";
  209. item.code = $"Ability-{standard}";
  210. item.school = $"{standard}";
  211. //添加区能力标准点
  212. abilities.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(item, new PartitionKey($"Ability-{standard}")));
  213. }
  214. if (abilities.Count < 256)
  215. {
  216. await Task.WhenAll(abilities);
  217. }
  218. else
  219. {
  220. int pages = (abilities.Count + 255) / 256;
  221. for (int i = 0; i < pages; i++)
  222. {
  223. List<Task<ItemResponse<Ability>>> tempAbility = abilities.Skip((i) * 256).Take(256).ToList();
  224. await Task.WhenAll(tempAbility);
  225. }
  226. }
  227. //微能力点
  228. 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-{oldStandard}") }))
  229. {
  230. List<Tnode> tnodes = new List<Tnode>();
  231. foreach (Tnode tnode in atask.children)
  232. {
  233. if (tnode.rnodes != null)
  234. {
  235. List<Rnode> rnodes = new List<Rnode>();
  236. foreach (Rnode rnode in tnode.rnodes)
  237. {
  238. if (!string.IsNullOrEmpty($"{rnode.link}"))
  239. {
  240. rnode.link = rnode.link.Replace($"/{oldStandard}/", $"/{standard}/");
  241. }
  242. rnodes.Add(rnode);
  243. }
  244. tnode.rnodes = rnodes;
  245. }
  246. tnodes.Add(tnode);
  247. }
  248. atask.children = tnodes;
  249. atask.code = $"AbilityTask-{standard}";
  250. atask.standard = $"{standard}";
  251. atask.codeval = $"{standard}";
  252. //添加区能力标准点中的节点
  253. //abilityTasks.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(atask, new PartitionKey($"AbilityTask-{standard}")));
  254. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(atask, new PartitionKey($"AbilityTask-{standard}"));
  255. }
  256. //if (abilityTasks.Count > 0)
  257. //{
  258. // for (int i = 0; i < abilityTasks.Count; i++)
  259. // {
  260. // List<Task<ItemResponse<AbilityTask>>> tempAbilityTasks = abilityTasks.Skip((i)).Take(1).ToList();
  261. // await Task.WhenAll(tempAbilityTasks);
  262. // }
  263. //}
  264. //新政策文件
  265. await foreach (StandardFile standardFile in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<StandardFile>(queryText: $"select value(c) from c where c.id='{_oldId}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"StandardFile") }))
  266. {
  267. if (standardFile != null)
  268. {
  269. standardFile.standard = $"{standard}";
  270. standardFile.id = areaId;
  271. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(standardFile, new PartitionKey($"StandardFile"));
  272. }
  273. }
  274. //新的区域设置
  275. await foreach (AreaSetting areaSetting in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<AreaSetting>(queryText: $"select value(c) from c where c.id='{_oldId}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("AreaSetting") }))
  276. {
  277. if (areaSetting != null)
  278. {
  279. areaSetting.accessConfig = null;
  280. areaSetting.id = areaId;
  281. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(areaSetting, new PartitionKey($"AreaSetting"));
  282. }
  283. }
  284. }
  285. else
  286. {
  287. Area area = null;
  288. await foreach (var tempArea 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") }))
  289. {
  290. area = tempArea;
  291. }
  292. if (area != null)
  293. {
  294. //查询要复制区域的能力标准点
  295. 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}") }))
  296. {
  297. if (!string.IsNullOrEmpty(item.blob))
  298. {
  299. item.blob = item.blob.Replace($"/{area.standard}/", $"/{standard}/");
  300. };
  301. item.standard = $"{standard}";
  302. item.code = $"Ability-{standard}";
  303. item.school = $"{standard}";
  304. //添加区能力标准点
  305. abilities.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(item, new PartitionKey($"Ability-{standard}")));
  306. }
  307. if (abilities.Count < 256)
  308. {
  309. await Task.WhenAll(abilities);
  310. }
  311. else
  312. {
  313. int pages = (abilities.Count + 255) / 256;
  314. for (int i = 0; i < pages; i++)
  315. {
  316. List<Task<ItemResponse<Ability>>> tempAbility = abilities.Skip((i) * 256).Take(256).ToList();
  317. await Task.WhenAll(tempAbility);
  318. }
  319. }
  320. //微能力点
  321. 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}") }))
  322. {
  323. List<Tnode> tnodes = new List<Tnode>();
  324. foreach (Tnode tnode in atask.children)
  325. {
  326. if (tnode.rnodes != null)
  327. {
  328. List<Rnode> rnodes = new List<Rnode>();
  329. foreach (Rnode rnode in tnode.rnodes)
  330. {
  331. if (!string.IsNullOrEmpty($"{rnode.link}"))
  332. {
  333. rnode.link = rnode.link.Replace($"/{area.standard}/", $"/{standard}/");
  334. }
  335. rnodes.Add(rnode);
  336. }
  337. tnode.rnodes = rnodes;
  338. }
  339. tnodes.Add(tnode);
  340. }
  341. atask.children = tnodes;
  342. atask.code = $"AbilityTask-{standard}";
  343. atask.standard = $"{standard}";
  344. atask.codeval = $"{standard}";
  345. //添加区能力标准点中的节点
  346. //abilityTasks.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(atask, new PartitionKey($"AbilityTask-{standard}")));
  347. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(atask, new PartitionKey($"AbilityTask-{standard}"));
  348. }
  349. //if (abilityTasks.Count > 0)
  350. //{
  351. // for (int i = 0; i < abilityTasks.Count; i++)
  352. // {
  353. // List<Task<ItemResponse<AbilityTask>>> tempAbilityTasks = abilityTasks.Skip(i).Take(1).ToList();
  354. // await Task.WhenAll(tempAbilityTasks);
  355. // }
  356. //}
  357. //新政策文件
  358. await foreach (StandardFile standardFile in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<StandardFile>(queryText: $"select value(c) from c where c.id='{area.id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"StandardFile") }))
  359. {
  360. if (standardFile != null)
  361. {
  362. standardFile.standard = $"{standard}";
  363. standardFile.id = areaId;
  364. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(standardFile, new PartitionKey($"StandardFile"));
  365. }
  366. }
  367. //新的区域设置
  368. await foreach (AreaSetting areaSetting in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<AreaSetting>(queryText: $"select value(c) from c where c.id='{area.id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("AreaSetting") }))
  369. {
  370. if (areaSetting != null)
  371. {
  372. areaSetting.accessConfig = null;
  373. areaSetting.id = areaId;
  374. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(areaSetting, new PartitionKey($"AreaSetting"));
  375. }
  376. }
  377. }
  378. else return Ok(new { state = 201, message = "未找到默认能力点!" });
  379. }
  380. #endregion
  381. //执行复制操作
  382. BatchCopyFile batchCopyFile = new BatchCopyFile();
  383. batchCopyFile.blobCntr = "teammodelos";
  384. batchCopyFile.oldFileName = $"{oldStandard}";
  385. batchCopyFile.newFileName = $"{standard}";
  386. batchCopyFile.tmdid = $"{_tmdId}";
  387. batchCopyFile.tmdIds = new List<string> { $"{ _tmdId}" };
  388. batchCopyFile.codeKey = partitionCode;
  389. batchCopyFile.tmdName = $"{_tmdName}";
  390. var messageBatchCopyFile = new ServiceBusMessage(batchCopyFile.ToJsonString());
  391. messageBatchCopyFile.ApplicationProperties.Add("name", "CopyStandardFile");
  392. var activeTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
  393. await _serviceBus.GetServiceBusClient().SendMessageAsync(activeTask, messageBatchCopyFile);
  394. //发送消息实体
  395. Notification notification = new Notification
  396. {
  397. hubName = "hita",
  398. type = "msg",
  399. from = $"ies5:{_option.Location}:private",
  400. to = new List<string> { $"{ _tmdId}" },
  401. label = $"{partitionCode}_start",
  402. body = new { location = _option.Location, biz = partitionCode, tmdid = $"{_tmdId}", tmdname = $"{_tmdName}", status = 1, time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() }.ToJsonString(),
  403. expires = DateTimeOffset.UtcNow.AddDays(7).ToUnixTimeSeconds()
  404. };
  405. var url = _configuration.GetValue<string>("HaBookAuth:CoreService:sendnotification");
  406. var clientID = _configuration.GetValue<string>("HaBookAuth:CoreService:clientID");
  407. var clientSecret = _configuration.GetValue<string>("HaBookAuth:CoreService:clientSecret");
  408. var location = _option.Location;
  409. await _notificationService.SendNotification(clientID, clientSecret, location, url, notification); //站内发送消息
  410. //保存操作记录
  411. await _azureStorage.SaveLog("area-add", $"{_tmdName}【{_tmdId}】已操作创区功能模块:{name},当前标准【{standard}】,复制的微能力点:{tempStandard}", _dingDing, httpContext: HttpContext);
  412. return Ok(new { state = 200, area = addArea });
  413. }
  414. catch (Exception ex)
  415. {
  416. await _dingDing.SendBotMsg($"BI,{_option.Location} /batcharea/batch-area \n {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  417. return BadRequest();
  418. }
  419. }
  420. /// <summary>
  421. /// 创区后切换能力点, 先删除原来的能力点,后复制新的能力点
  422. /// </summary>
  423. /// <param name="jsonElement"></param>
  424. /// <returns></returns>
  425. [ProducesDefaultResponseType]
  426. [AuthToken(Roles = "assist")]
  427. [HttpPost("cut-standard")]
  428. public async Task<IActionResult> CutStandard(JsonElement jsonElement)
  429. {
  430. try
  431. {
  432. if (!jsonElement.TryGetProperty("oldId", out JsonElement _oldId)) return BadRequest();
  433. if (!jsonElement.TryGetProperty("oldStandard", out JsonElement _oldStandard)) return BadRequest();
  434. if (!jsonElement.TryGetProperty("newId", out JsonElement _newId)) return BadRequest();
  435. if (!jsonElement.TryGetProperty("newStandard", out JsonElement _newStandard)) return BadRequest();
  436. var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  437. //操作记录实体
  438. string blobOrTable = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString();
  439. var cosmosClient = _azureCosmos.GetCosmosClient();
  440. List<string> abilityIds = new List<string>(); //册别的ID集合
  441. //查询册别信息
  442. await foreach (var tempAbility in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<Ability>(queryText: $"select value(c) from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Ability-{_oldStandard}") }))
  443. {
  444. abilityIds.Add(tempAbility.id); //查询出来册别ID添加册别ID集合
  445. }
  446. //删除册别
  447. if (abilityIds.IsNotEmpty())
  448. {
  449. var sresponse = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").DeleteItemsStreamAsync(abilityIds, $"Ability-{_oldStandard}");
  450. }
  451. List<string> abilityTaskIds = new List<string>(); //章节ID集合
  452. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<AbilityTask>(queryText: $"select value(c) from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"AbilityTask-{_oldStandard}") }))
  453. {
  454. abilityTaskIds.Add(item.id); //查询出来的章节信息ID添加到战绩集合
  455. }
  456. //删除章节
  457. if (abilityTaskIds.IsNotEmpty())
  458. {
  459. var sresponse = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").DeleteItemsStreamAsync(abilityTaskIds, $"AbilityTask-{_oldStandard}");
  460. }
  461. List<Task<ItemResponse<Ability>>> abilities = new List<Task<ItemResponse<Ability>>>(); //存储册别数据
  462. List<Task<ItemResponse<AbilityTask>>> abilityTasks = new List<Task<ItemResponse<AbilityTask>>>(); //存储章节
  463. //查询要复制区域的能力标准点
  464. 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-{_newStandard}") }))
  465. {
  466. if (!string.IsNullOrEmpty(item.blob))
  467. {
  468. item.blob = item.blob.Replace($"/{_newStandard}/", $"/{_oldStandard}/");
  469. };
  470. item.standard = $"{_oldStandard}";
  471. item.code = $"Ability-{_oldStandard}";
  472. item.school = $"{_oldStandard}";
  473. //添加区能力标准点
  474. abilities.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(item, new PartitionKey($"Ability-{_oldStandard}")));
  475. }
  476. if (abilities.Count < 256)
  477. {
  478. await Task.WhenAll(abilities);
  479. }
  480. else
  481. {
  482. int pages = (abilities.Count + 255) / 256;
  483. for (int i = 0; i < pages; i++)
  484. {
  485. List<Task<ItemResponse<Ability>>> tempAbility = abilities.Skip((i) * 256).Take(256).ToList();
  486. await Task.WhenAll(tempAbility);
  487. }
  488. }
  489. //微能力点
  490. 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-{_newStandard}") }))
  491. {
  492. List<Tnode> tnodes = new List<Tnode>();
  493. foreach (Tnode tnode in atask.children)
  494. {
  495. if (tnode.rnodes != null)
  496. {
  497. List<Rnode> rnodes = new List<Rnode>();
  498. foreach (Rnode rnode in tnode.rnodes)
  499. {
  500. if (!string.IsNullOrEmpty($"{rnode.link}"))
  501. {
  502. rnode.link = rnode.link.Replace($"/{_newStandard}/", $"/{_oldStandard}/");
  503. }
  504. rnodes.Add(rnode);
  505. }
  506. tnode.rnodes = rnodes;
  507. }
  508. tnodes.Add(tnode);
  509. }
  510. atask.children = tnodes;
  511. atask.code = $"AbilityTask-{_oldStandard}";
  512. atask.standard = $"{_oldStandard}";
  513. atask.codeval = $"{_oldStandard}";
  514. ////添加区能力标准点中的节点
  515. //abilityTasks.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(atask, new PartitionKey($"AbilityTask-{_oldStandard}")));
  516. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(atask, new PartitionKey($"AbilityTask-{_oldStandard}"));
  517. }
  518. StandardFile saveFile = new StandardFile();
  519. //新政策文件
  520. await foreach (StandardFile standardFile in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<StandardFile>(queryText: $"select value(c) from c where c.id='{_newId}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"StandardFile") }))
  521. {
  522. if (standardFile != null)
  523. {
  524. standardFile.standard = $"{_oldStandard}";
  525. standardFile.id = $"{_oldId}";
  526. saveFile = standardFile;
  527. }
  528. }
  529. StandardFile tempFile = new();
  530. try
  531. {
  532. tempFile = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemAsync<StandardFile>(saveFile.id, new PartitionKey("StandardFile"));
  533. }
  534. catch
  535. {
  536. }
  537. if (tempFile.id != null)
  538. {
  539. if (tempFile.id.Equals(saveFile.id))
  540. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReplaceItemAsync<StandardFile>(saveFile, saveFile.id, new PartitionKey("StandardFile")); //直接替换以前的数据
  541. else
  542. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(saveFile, new PartitionKey($"StandardFile")); // 需要删除原来的政策文件数据在进行添加
  543. }
  544. //新的区域设置
  545. AreaSetting saveSetting = new AreaSetting();
  546. await foreach (AreaSetting areaSetting in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<AreaSetting>(queryText: $"select value(c) from c where c.id='{_newId}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("AreaSetting") }))
  547. {
  548. if (areaSetting != null)
  549. {
  550. areaSetting.accessConfig = null;
  551. areaSetting.id = $"{_oldId}";
  552. saveSetting = areaSetting;
  553. }
  554. }
  555. AreaSetting tempSetting = new();
  556. try
  557. {
  558. tempSetting = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemAsync<AreaSetting>(saveSetting.id, new PartitionKey("StandardFile"));
  559. }
  560. catch
  561. {
  562. }
  563. if (tempSetting.id != null)
  564. {
  565. if (tempSetting.id.Equals(saveSetting.id))
  566. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReplaceItemAsync<AreaSetting>(saveSetting, saveSetting.id, new PartitionKey($"AreaSetting")); //直接替换以前的数据
  567. else
  568. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(saveSetting, new PartitionKey($"AreaSetting")); //需要删除原来的区域设置数据在进行添加
  569. }
  570. //发送消息分区键
  571. string partitionCode = "DelBeforeCopyAbility-mark";
  572. //执行复制操作
  573. BatchCopyFile batchCopyFile = new BatchCopyFile();
  574. batchCopyFile.blobCntr = "teammodelos";
  575. batchCopyFile.oldFileName = $"{_newStandard}";
  576. batchCopyFile.newFileName = $"{_oldStandard}";
  577. batchCopyFile.tmdid = $"{_tmdId}";
  578. batchCopyFile.tmdIds = new List<string> { $"{ _tmdId}" };
  579. batchCopyFile.codeKey = partitionCode;
  580. batchCopyFile.tmdName = $"{_tmdName}";
  581. var messageBatchCopyFile = new ServiceBusMessage(batchCopyFile.ToJsonString());
  582. messageBatchCopyFile.ApplicationProperties.Add("name", "CopyStandardFile"); //Function暂时还未写
  583. var activeTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
  584. await _serviceBus.GetServiceBusClient().SendMessageAsync(activeTask, messageBatchCopyFile); //先执行删除操作,在执行复制
  585. //发送消息实体
  586. Notification notification = new Notification
  587. {
  588. hubName = "hita",
  589. type = "msg",
  590. from = $"BI:{_option.Location}:private",
  591. to = new List<string> { $"{ _tmdId}" },
  592. label = $"{partitionCode}_start",
  593. body = new { location = _option.Location, biz = partitionCode, tmdid = $"{_tmdId}", tmdname = $"{_tmdName}", status = 1, time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() }.ToJsonString(),
  594. expires = DateTimeOffset.UtcNow.AddDays(7).ToUnixTimeSeconds()
  595. };
  596. var url = _configuration.GetValue<string>("HaBookAuth:CoreService:sendnotification");
  597. var clientID = _configuration.GetValue<string>("HaBookAuth:CoreService:clientID");
  598. var clientSecret = _configuration.GetValue<string>("HaBookAuth:CoreService:clientSecret");
  599. var location = _option.Location;
  600. await _notificationService.SendNotification(clientID, clientSecret, location, url, notification); //发送站内发送消息
  601. //保存操作记录
  602. await _azureStorage.SaveLog("standard-cut", $"{_tmdName}【{_tmdId}】已操作【{_oldStandard}】切换至{_newStandard}微能力点,复制标准:{_newStandard}", _dingDing, httpContext: HttpContext);
  603. return Ok(new { state = 200 });
  604. }
  605. catch (Exception ex)
  606. {
  607. await _dingDing.SendBotMsg($"BI,{_option.Location} batcharea/cut-standard \n {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  608. return BadRequest();
  609. }
  610. }
  611. }
  612. }