BatchAreaController.cs 37 KB

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