BatchAreaController.cs 36 KB

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