BatchAreaController.cs 37 KB

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