BatchAreaController.cs 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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(Constant.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(Constant.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. //分区键
  184. string partitionCode = "copyAbility-mark";
  185. if (!string.IsNullOrEmpty($"{oldStandard}") && !string.IsNullOrEmpty($"{_oldId}"))
  186. {
  187. //查询要复制区域的能力标准点
  188. 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}") }))
  189. {
  190. if (!string.IsNullOrEmpty(item.blob))
  191. {
  192. item.blob = item.blob.Replace($"/{oldStandard}/", $"/{standard}/");
  193. };
  194. item.standard = $"{standard}";
  195. item.code = $"Ability-{standard}";
  196. item.school = $"{standard}";
  197. //添加区能力标准点
  198. abilities.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(item, new PartitionKey($"Ability-{standard}")));
  199. }
  200. if (abilities.Count < 256)
  201. {
  202. await Task.WhenAll(abilities);
  203. }
  204. else
  205. {
  206. int pages = (abilities.Count + 255) / 256;
  207. for (int i = 0; i < pages; i++)
  208. {
  209. List<Task<ItemResponse<Ability>>> tempAbility = abilities.Skip((i) * 256).Take(256).ToList();
  210. await Task.WhenAll(tempAbility);
  211. }
  212. }
  213. //微能力点
  214. 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}") }))
  215. {
  216. List<Tnode> tnodes = new List<Tnode>();
  217. foreach (Tnode tnode in atask.children)
  218. {
  219. if (tnode.rnodes != null)
  220. {
  221. List<Rnode> rnodes = new List<Rnode>();
  222. foreach (Rnode rnode in tnode.rnodes)
  223. {
  224. if (!string.IsNullOrEmpty($"{rnode.link}"))
  225. {
  226. rnode.link = rnode.link.Replace($"/{oldStandard}/", $"/{standard}/");
  227. }
  228. rnodes.Add(rnode);
  229. }
  230. tnode.rnodes = rnodes;
  231. }
  232. tnodes.Add(tnode);
  233. }
  234. atask.children = tnodes;
  235. atask.code = $"AbilityTask-{standard}";
  236. atask.standard = $"{standard}";
  237. atask.codeval = $"{standard}";
  238. //添加区能力标准点中的节点
  239. //abilityTasks.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(atask, new PartitionKey($"AbilityTask-{standard}")));
  240. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(atask, new PartitionKey($"AbilityTask-{standard}"));
  241. }
  242. //for (int i = 0; i < abilityTasks.Count; i++)
  243. //{
  244. // List<Task<ItemResponse<AbilityTask>>> tempAbilityTasks = abilityTasks.Skip(i).Take(1).ToList();
  245. // await Task.WhenAll(tempAbilityTasks);
  246. //}
  247. //新政策文件
  248. 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") }))
  249. {
  250. standardFile.standard = $"{standard}";
  251. standardFile.id = areaId;
  252. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(standardFile, new PartitionKey($"StandardFile"));
  253. }
  254. //新的区域设置
  255. 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") }))
  256. {
  257. areaSetting.accessConfig = null;
  258. areaSetting.id = areaId;
  259. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(areaSetting, new PartitionKey($"AreaSetting"));
  260. }
  261. //执行复制操作
  262. BatchCopyFile batchCopyFile = new BatchCopyFile();
  263. batchCopyFile.blobCntr = "teammodelos";
  264. batchCopyFile.oldFileName = $"{oldStandard}";
  265. batchCopyFile.newFileName = $"{standard}";
  266. batchCopyFile.tmdid = $"{_tmdId}";
  267. batchCopyFile.tmdIds = new List<string> { $"{ _tmdId}" };
  268. batchCopyFile.codeKey = partitionCode;
  269. batchCopyFile.tmdName = $"{_tmdName}";
  270. var messageBatchCopyFile = new ServiceBusMessage(batchCopyFile.ToJsonString());
  271. messageBatchCopyFile.ApplicationProperties.Add("name", "CopyStandardFile");
  272. var activeTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
  273. await _serviceBus.GetServiceBusClient().SendMessageAsync(activeTask, messageBatchCopyFile);
  274. //发送消息实体
  275. Notification notification = new Notification
  276. {
  277. hubName = "hita",
  278. type = "msg",
  279. from = $"ies5:{_option.Location}:private",
  280. to = new List<string> { $"{ _tmdId}" },
  281. label = $"{partitionCode}_start",
  282. body = new { location = _option.Location, biz = partitionCode, tmdid = $"{_tmdId}", tmdname = $"{_tmdName}", status = 1, time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() }.ToJsonString(),
  283. expires = DateTimeOffset.UtcNow.AddDays(7).ToUnixTimeSeconds()
  284. };
  285. var url = _configuration.GetValue<string>("HaBookAuth:CoreService:sendnotification");
  286. var clientID = _configuration.GetValue<string>("HaBookAuth:CoreService:clientID");
  287. var clientSecret = _configuration.GetValue<string>("HaBookAuth:CoreService:clientSecret");
  288. var location = _option.Location;
  289. await _notificationService.SendNotification(clientID, clientSecret, location, url, notification); //站内发送消息
  290. return Ok(new { state = 200, area = addArea });
  291. }
  292. else
  293. {
  294. Area area = null;
  295. 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") }))
  296. {
  297. area = tempArea;
  298. }
  299. if (area != null)
  300. {
  301. //查询要复制区域的能力标准点
  302. 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}") }))
  303. {
  304. if (!string.IsNullOrEmpty(item.blob))
  305. {
  306. item.blob = item.blob.Replace($"/{area.standard}/", $"/{standard}/");
  307. };
  308. item.standard = $"{standard}";
  309. item.code = $"Ability-{standard}";
  310. item.school = $"{standard}";
  311. //添加区能力标准点
  312. abilities.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(item, new PartitionKey($"Ability-{standard}")));
  313. }
  314. if (abilities.Count < 256)
  315. {
  316. await Task.WhenAll(abilities);
  317. }
  318. else
  319. {
  320. int pages = (abilities.Count + 255) / 256;
  321. for (int i = 0; i < pages; i++)
  322. {
  323. List<Task<ItemResponse<Ability>>> tempAbility = abilities.Skip((i) * 256).Take(256).ToList();
  324. await Task.WhenAll(tempAbility);
  325. }
  326. }
  327. //微能力点
  328. 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}") }))
  329. {
  330. List<Tnode> tnodes = new List<Tnode>();
  331. foreach (Tnode tnode in atask.children)
  332. {
  333. if (tnode.rnodes != null)
  334. {
  335. List<Rnode> rnodes = new List<Rnode>();
  336. foreach (Rnode rnode in tnode.rnodes)
  337. {
  338. if (!string.IsNullOrEmpty($"{rnode.link}"))
  339. {
  340. rnode.link = rnode.link.Replace($"/{area.standard}/", $"/{standard}/");
  341. }
  342. rnodes.Add(rnode);
  343. }
  344. tnode.rnodes = rnodes;
  345. }
  346. tnodes.Add(tnode);
  347. }
  348. atask.children = tnodes;
  349. atask.code = $"AbilityTask-{standard}";
  350. atask.standard = $"{standard}";
  351. atask.codeval = $"{standard}";
  352. ////添加区能力标准点中的节点
  353. //abilityTasks.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(atask, new PartitionKey($"AbilityTask-{standard}")));
  354. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(atask, new PartitionKey($"AbilityTask-{standard}"));
  355. }
  356. //for (int i = 0; i < abilityTasks.Count; i++)
  357. //{
  358. // List<Task<ItemResponse<AbilityTask>>> tempAbilityTasks = abilityTasks.Skip(i).Take(1).ToList();
  359. // await Task.WhenAll(tempAbilityTasks);
  360. //}
  361. //新政策文件
  362. 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") }))
  363. {
  364. standardFile.standard = $"{standard}";
  365. standardFile.id = areaId;
  366. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(standardFile, new PartitionKey($"StandardFile"));
  367. }
  368. //新的区域设置
  369. 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") }))
  370. {
  371. areaSetting.accessConfig = null;
  372. areaSetting.id = areaId;
  373. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(areaSetting, new PartitionKey($"AreaSetting"));
  374. }
  375. //执行复制操作
  376. BatchCopyFile batchCopyFile = new BatchCopyFile();
  377. batchCopyFile.blobCntr = "teammodelos";
  378. batchCopyFile.oldFileName = $"{area.standard}";
  379. batchCopyFile.newFileName = $"{standard}";
  380. batchCopyFile.tmdid = $"{_tmdId}";
  381. batchCopyFile.tmdIds = new List<string> { $"{ _tmdId}" };
  382. batchCopyFile.codeKey = partitionCode;
  383. batchCopyFile.tmdName = $"{_tmdName}";
  384. var messageBatchCopyFile = new ServiceBusMessage(batchCopyFile.ToJsonString());
  385. messageBatchCopyFile.ApplicationProperties.Add("name", "CopyStandardFile");
  386. var activeTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
  387. await _serviceBus.GetServiceBusClient().SendMessageAsync(activeTask, messageBatchCopyFile); //执行复制操作
  388. //发送消息实体
  389. Notification notification = new Notification
  390. {
  391. hubName = "hita",
  392. type = "msg",
  393. from = $"ies5:{_option.Location}:private",
  394. to = new List<string> { $"{ _tmdId}" },
  395. label = $"{partitionCode}_start",
  396. body = new { location = _option.Location, biz = partitionCode, tmdid = $"{_tmdId}", tmdname = $"{_tmdName}", status = 1, time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() }.ToJsonString(),
  397. expires = DateTimeOffset.UtcNow.AddDays(7).ToUnixTimeSeconds()
  398. };
  399. var url = _configuration.GetValue<string>("HaBookAuth:CoreService:sendnotification");
  400. var clientID = _configuration.GetValue<string>("HaBookAuth:CoreService:clientID");
  401. var clientSecret = _configuration.GetValue<string>("HaBookAuth:CoreService:clientSecret");
  402. var location = _option.Location;
  403. await _notificationService.SendNotification(clientID, clientSecret, location, url, notification); //发送站内发送消息
  404. return Ok(new { state = 200, area });
  405. }
  406. else return Ok(new { state = 201, message = "未找到默认能力点!" });
  407. }
  408. }
  409. catch (Exception ex)
  410. {
  411. await _dingDing.SendBotMsg($"BI,{_option.Location} /batcharea/batch-area \n {ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  412. return BadRequest();
  413. }
  414. }
  415. /// <summary>
  416. /// 创区后切换能力点, 先删除原来的能力点,后复制新的能力点
  417. /// </summary>
  418. /// <param name="jsonElement"></param>
  419. /// <returns></returns>
  420. [ProducesDefaultResponseType]
  421. [HttpPost("cut-standard")]
  422. public async Task<IActionResult> CutStandard(JsonElement jsonElement)
  423. {
  424. try
  425. {
  426. if (!jsonElement.TryGetProperty("oldId", out JsonElement _oldId)) return BadRequest();
  427. if (!jsonElement.TryGetProperty("oldStandard", out JsonElement _oldStandard)) return BadRequest();
  428. if (!jsonElement.TryGetProperty("newId", out JsonElement _newId)) return BadRequest();
  429. if (!jsonElement.TryGetProperty("newStandard", out JsonElement _newStandard)) return BadRequest();
  430. if (!jsonElement.TryGetProperty("tmdId", out JsonElement _tmdId)) return BadRequest();
  431. if (!jsonElement.TryGetProperty("tmdName", out JsonElement _tmdName)) return BadRequest();
  432. //保存操作记录
  433. BIOperateLog bIOperateLog = new BIOperateLog
  434. {
  435. PartitionKey = "BILogging",
  436. RowKey = "BIOperate",
  437. tmdId = $"{_tmdId}",
  438. tmdName = $"{_tmdName}",
  439. operateDescribe = $"{_tmdName}操作切换能力点",
  440. visitApi = "dd/cut-standard",
  441. operateTime = DateTime.Now
  442. };
  443. //await _azureStorage.GetBlobServiceClient("BIOperateLog")
  444. await _azureStorage.Save<BIOperateLog>(bIOperateLog);
  445. var cosmosClient = _azureCosmos.GetCosmosClient();
  446. List<string> abilityIds = new List<string>(); //册别的ID集合
  447. //查询册别信息
  448. 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}") }))
  449. {
  450. abilityIds.Add(tempAbility.id); //查询出来册别ID添加册别ID集合
  451. }
  452. //删除册别
  453. if (abilityIds.IsNotEmpty())
  454. {
  455. //var sresponse = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").DeleteItemsStreamAsync(abilityIds, $"Ability-{_oldStandard}");
  456. }
  457. List<string> abilityTaskIds = new List<string>(); //章节ID集合
  458. foreach (var abilityId in abilityIds)
  459. {
  460. 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}") }))
  461. {
  462. abilityTaskIds.Add(item.id); //查询出来的章节信息ID添加到战绩集合
  463. }
  464. }
  465. //删除章节
  466. if (abilityTaskIds.IsNotEmpty())
  467. {
  468. //var sresponse = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").DeleteItemsStreamAsync(abilityTaskIds, $"AbilityTask-{_oldStandard}");
  469. }
  470. List<Task<ItemResponse<Ability>>> abilities = new List<Task<ItemResponse<Ability>>>(); //存储册别数据
  471. List<Task<ItemResponse<AbilityTask>>> abilityTasks = new List<Task<ItemResponse<AbilityTask>>>(); //存储章节
  472. List<Ability> abilities1 = new List<Ability>(); //存储数据 测试使用,后期删除
  473. List<AbilityTask> abilityTasks1 = new List<AbilityTask>(); //存储章节 测试使用,后期删除
  474. List<StandardFile> standardFiles = new List<StandardFile>(); // 显示新的政策文件 //测试使用,后期删除
  475. List<AreaSetting> areaSettings = new List<AreaSetting>(); // 显示新的区域配置 //测试使用,后期删除
  476. //查询要复制区域的能力标准点
  477. 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}") }))
  478. {
  479. if (!string.IsNullOrEmpty(item.blob))
  480. {
  481. item.blob = item.blob.Replace($"/{_newStandard}/", $"/{_oldStandard}/");
  482. };
  483. item.standard = $"{_oldStandard}";
  484. item.code = $"Ability-{_oldStandard}";
  485. item.school = $"{_oldStandard}";
  486. abilities1.Add(item); //显示新的册别信息集合 //测试使用,后期删除
  487. ////添加区能力标准点
  488. //abilities.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(item, new PartitionKey($"Ability-{_oldStandard}")));
  489. }
  490. //if (abilities.Count < 256)
  491. //{
  492. // await Task.WhenAll(abilities);
  493. //}
  494. //else
  495. //{
  496. // int pages = (abilities.Count + 255) / 256;
  497. // for (int i = 0; i < pages; i++)
  498. // {
  499. // List<Task<ItemResponse<Ability>>> tempAbility = abilities.Skip((i) * 256).Take(256).ToList();
  500. // await Task.WhenAll(tempAbility);
  501. // }
  502. //}
  503. //微能力点
  504. 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}") }))
  505. {
  506. List<Tnode> tnodes = new List<Tnode>();
  507. foreach (Tnode tnode in atask.children)
  508. {
  509. if (tnode.rnodes != null)
  510. {
  511. List<Rnode> rnodes = new List<Rnode>();
  512. foreach (Rnode rnode in tnode.rnodes)
  513. {
  514. if (!string.IsNullOrEmpty($"{rnode.link}"))
  515. {
  516. rnode.link = rnode.link.Replace($"/{_newStandard}/", $"/{_oldStandard}/");
  517. }
  518. rnodes.Add(rnode);
  519. }
  520. tnode.rnodes = rnodes;
  521. }
  522. tnodes.Add(tnode);
  523. }
  524. atask.children = tnodes;
  525. atask.code = $"AbilityTask-{_oldStandard}";
  526. atask.standard = $"{_oldStandard}";
  527. atask.codeval = $"{_oldStandard}";
  528. abilityTasks1.Add(atask); //显示新的章节信息集合 //测试使用,后期删除
  529. ////添加区能力标准点中的节点
  530. //abilityTasks.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(atask, new PartitionKey($"AbilityTask-{_oldStandard}")));
  531. //await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(atask, new PartitionKey($"AbilityTask-{_oldStandard}"));
  532. }
  533. //if (abilityTasks.Count <= 256)
  534. //{
  535. // await Task.WhenAll(abilityTasks);
  536. //}
  537. //else
  538. //{
  539. // int pages = (abilityTasks.Count + 255) / 256;
  540. // for (int i = 0; i < pages; i++)
  541. // {
  542. // List<Task<ItemResponse<AbilityTask>>> itemAbilityTask = abilityTasks.Skip((i) * 256).Take(256).ToList();
  543. // await Task.WhenAll(itemAbilityTask);
  544. // }
  545. //}
  546. //新政策文件
  547. 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") }))
  548. {
  549. standardFile.standard = $"{_oldStandard}";
  550. standardFile.id = $"{_oldId}";
  551. //await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(standardFile, new PartitionKey($"StandardFile")); // 需要删除原来的政策文件数据在进行添加
  552. //await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReplaceItemAsync<StandardFile>(standardFile, standardFile.id, new PartitionKey("StandardFile")); //直接替换以前的数据
  553. standardFiles.Add(standardFile);// 显示新的政策文件集合 //测试使用,后期删除
  554. }
  555. //新的区域设置
  556. 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") }))
  557. {
  558. areaSetting.accessConfig = null;
  559. areaSetting.id = $"{_oldId}";
  560. //await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(areaSetting, new PartitionKey($"AreaSetting")); //需要删除原来的区域设置数据在进行添加
  561. //await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReplaceItemAsync<AreaSetting>(areaSetting, areaSetting.id, new PartitionKey($"AreaSetting")); //直接替换以前的数据
  562. areaSettings.Add(areaSetting); // 显示新的区域配置集合 //测试使用,后期删除
  563. }
  564. //分区键
  565. string partitionCode = "DelBeforeCopyAbility-mark";
  566. //执行复制操作
  567. BatchCopyFile batchCopyFile = new BatchCopyFile();
  568. batchCopyFile.blobCntr = "teammodelos";
  569. batchCopyFile.oldFileName = $"{_newStandard}";
  570. batchCopyFile.newFileName = $"{_oldStandard}";
  571. batchCopyFile.tmdid = $"{_tmdId}";
  572. batchCopyFile.tmdIds = new List<string> { $"{ _tmdId}" };
  573. batchCopyFile.codeKey = partitionCode;
  574. batchCopyFile.tmdName = $"{_tmdName}";
  575. var messageBatchCopyFile = new ServiceBusMessage(batchCopyFile.ToJsonString());
  576. messageBatchCopyFile.ApplicationProperties.Add("name", "CopyStandardFile"); //Function暂时还未写
  577. var activeTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
  578. //await _serviceBus.GetServiceBusClient().SendMessageAsync(activeTask, messageBatchCopyFile); //先执行删除操作,在执行复制
  579. //发送消息实体
  580. Notification notification = new Notification
  581. {
  582. hubName = "hita",
  583. type = "msg",
  584. from = $"ies5:{_option.Location}:private",
  585. to = new List<string> { $"{ _tmdId}" },
  586. label = $"{partitionCode}_start",
  587. body = new { location = _option.Location, biz = partitionCode, tmdid = $"{_tmdId}", tmdname = $"{_tmdName}", status = 1, time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() }.ToJsonString(),
  588. expires = DateTimeOffset.UtcNow.AddDays(7).ToUnixTimeSeconds()
  589. };
  590. var url = _configuration.GetValue<string>("HaBookAuth:CoreService:sendnotification");
  591. var clientID = _configuration.GetValue<string>("HaBookAuth:CoreService:clientID");
  592. var clientSecret = _configuration.GetValue<string>("HaBookAuth:CoreService:clientSecret");
  593. var location = _option.Location;
  594. await _notificationService.SendNotification(clientID, clientSecret, location, url, notification); //发送站内发送消息
  595. return Ok(new { state = 200, abilityIds, abilityTaskIds, abilities1, abilityTasks1, standardFiles, areaSettings });
  596. }
  597. catch (Exception ex)
  598. {
  599. await _dingDing.SendBotMsg($"BI,{_option.Location} batcharea/cutstandard \r {ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  600. return BadRequest();
  601. }
  602. }
  603. #region 测试复制文件 流程通,后期删除
  604. /// <summary>
  605. /// 测试复制Blob中的文件 批量创区接口测试完成后删除
  606. /// </summary>
  607. /// <param name="jsonElement"></param>
  608. /// <returns></returns>
  609. [ProducesDefaultResponseType]
  610. [HttpPost("copybolb")]
  611. public async Task<IActionResult> TestCopyBolb(JsonElement jsonElement)
  612. {
  613. if (!jsonElement.TryGetProperty("cntr", out JsonElement _cntr)) return BadRequest();
  614. if (!jsonElement.TryGetProperty("oldurl", out JsonElement _oldurl)) return BadRequest();
  615. if (!jsonElement.TryGetProperty("newurl", out JsonElement _newurl)) return BadRequest();
  616. var cosmosClient = _azureCosmos.GetCosmosClient();
  617. int index = $"{_oldurl}".IndexOf("/", 0);
  618. string oldUrl = index == 0 ? oldUrl = $"{_oldurl}".Substring(1) : oldUrl = $"{_oldurl}";
  619. var bcc = _azureStorage.GetBlobContainerClient($"{_cntr}"); //获取地址
  620. var urlSaS = _azureStorage.GetBlobSAS($"{_cntr}", oldUrl, BlobSasPermissions.Read | BlobSasPermissions.List); // 取得容器sas 和有效期
  621. //BlobBaseClient blob = new BlobBaseClient(new Uri(bcc.Uri+$"{setemp[0]}/{_newFile}")); //新的地址
  622. ////var s = blob.SyncCopyFromUri(new Uri(url)); //执行复制操作
  623. var response = bcc.GetBlobClient($"{_newurl}").SyncCopyFromUri(new Uri(urlSaS));
  624. if (response.Value.CopyStatus.Equals(1))
  625. return Ok(new { state = 200, message ="复制成功"});
  626. else return Ok(new { response.Value.CopyStatus });
  627. //var response = await bcc.GetBlobClient($"{_newurl}").StartCopyFromUriAsync(new Uri(urlSaS));
  628. //return Ok(new { state = 200 , response.Value,response.HasCompleted,response.HasValue,response.Id});
  629. }
  630. /// <summary>
  631. /// 批量复制文件接口 批量创区接口测试完成后删除
  632. /// </summary>
  633. /// <param name="jsonElement"></param>
  634. /// <returns></returns>
  635. [ProducesDefaultResponseType]
  636. [HttpPost("copybolbFile")]
  637. public async Task<IActionResult> TestCopyBolbFile(JsonElement jsonElement)
  638. {
  639. if (!jsonElement.TryGetProperty("cntr", out JsonElement _cntr)) return BadRequest();
  640. if (!jsonElement.TryGetProperty("oldstandard", out JsonElement _oldstandard)) return BadRequest();
  641. if (!jsonElement.TryGetProperty("newstandard", out JsonElement _newstandard)) return BadRequest();
  642. List<string> errmess = new List<string>();
  643. List<string> fils = new List<string>();
  644. List<Task<Response<BlobCopyInfo>>> file_list = new List<Task<Response<BlobCopyInfo>>>();
  645. var client = _azureStorage.GetBlobContainerClient($"{_cntr}");//获取地址
  646. await foreach (BlobItem item in client.GetBlobsAsync(BlobTraits.None, BlobStates.None, $"yxpt/{_oldstandard}/"))
  647. {
  648. fils.Add(item.Name);
  649. string oldurl1 = $"{item.Name}".Replace($"/{_oldstandard}/", $"/{_newstandard}/");
  650. var urlSaS = _azureStorage.GetBlobSAS($"{_cntr}", item.Name, BlobSasPermissions.Read | BlobSasPermissions.List); // 取得容器sas 和有效期
  651. client.GetBlobClient(oldurl1).SyncCopyFromUri(new Uri(urlSaS));
  652. //file_list.Add(blobcc.GetBlobClient($"{oldurl1}").SyncCopyFromUriAsync(new Uri(urlSaS)));
  653. }
  654. if (file_list.Count <= 256)
  655. {
  656. await Task.WhenAll(file_list);
  657. }
  658. else
  659. {
  660. int pages = (file_list.Count + 255) / 256;
  661. for (int i = 0; i < pages; i++)
  662. {
  663. List<Task<Response<BlobCopyInfo>>> file_lists = file_list.Skip((i) * 256).Take(256).ToList();
  664. await Task.WhenAll(file_lists);
  665. }
  666. }
  667. return Ok(new { fils.Count, fils });
  668. }
  669. /// <summary>
  670. /// 批量删除在复制文件接口 正式完成后删除该接口
  671. /// </summary>
  672. /// <param name="jsonElement"></param>
  673. /// <returns></returns>
  674. [ProducesDefaultResponseType]
  675. [HttpPost("copyafterdel")]
  676. public async Task<IActionResult> CopyAfterDel(JsonElement jsonElement)
  677. {
  678. if (!jsonElement.TryGetProperty("cntr", out JsonElement _cntr)) return BadRequest();
  679. if (!jsonElement.TryGetProperty("delstandard", out JsonElement _delstandard)) return BadRequest();
  680. if (!jsonElement.TryGetProperty("copystandard", out JsonElement _copystandard)) return BadRequest();
  681. var blobClient = _azureStorage.GetBlobContainerClient($"{_cntr}"); //获取地址
  682. //先删除原有的文件
  683. List<Task<Response<bool>>> DelList = new List<Task<Response<bool>>>();
  684. await foreach (BlobItem blobItem in blobClient.GetBlobsAsync(BlobTraits.None, BlobStates.None, $"yxpt/{_delstandard}/"))
  685. {
  686. //await blobClient.GetBlobClient(blobItem.Name).DeleteIfExistsAsync();
  687. DelList.Add(blobClient.GetBlobBaseClient(blobItem.Name).DeleteIfExistsAsync());
  688. }
  689. if (DelList.Count <= 256)
  690. {
  691. await Task.WhenAll(DelList);
  692. }
  693. else
  694. {
  695. int pages = (DelList.Count + 255) / 256;
  696. for (int i = 0; i < pages; i++)
  697. {
  698. List<Task<Response<bool>>> delList = DelList.Skip((i) * 256).Take(256).ToList();
  699. await Task.WhenAll(delList);
  700. }
  701. }
  702. //在复制新的文件
  703. List<Task<Response<BlobCopyInfo>>> CopyList = new List<Task<Response<BlobCopyInfo>>>();
  704. await foreach (BlobItem blobItem in blobClient.GetBlobsAsync(BlobTraits.None, BlobStates.None, $"yxpt/{_copystandard}/"))
  705. {
  706. string oldurl = $"{blobItem.Name}".Replace($"/{_copystandard}/", $"/{_delstandard}/"); //替换旧文件
  707. var copyUrlSas = _azureStorage.GetBlobSAS($"{_cntr}", blobItem.Name, BlobSasPermissions.Read | BlobSasPermissions.List);
  708. //await blobClient.GetBlobClient(oldurl).SyncCopyFromUriAsync(new Uri(copyUrlSas));
  709. CopyList.Add(blobClient.GetBlobClient(oldurl).SyncCopyFromUriAsync(new Uri(copyUrlSas)));
  710. }
  711. if (CopyList.Count <= 256)
  712. {
  713. await Task.WhenAll(CopyList);
  714. }
  715. else
  716. {
  717. int pages = (CopyList.Count + 255) / 256;
  718. for (int i = 0; i < pages; i++)
  719. {
  720. List<Task<Response<BlobCopyInfo>>> copyList = CopyList.Skip((i) * 256).Take(256).ToList();
  721. await Task.WhenAll(copyList);
  722. }
  723. }
  724. return Ok(new { state = 200 });
  725. }
  726. /// <summary>
  727. /// 批量复制文件夹方法 批量创区接口测试完成后删除
  728. /// </summary>
  729. /// <param name="_cntr"></param>
  730. /// <param name="_oldstandard"></param>
  731. /// <param name="_newstandard"></param>
  732. /// <returns></returns>
  733. public async Task<IActionResult> BarchCopyFile(string _cntr,string _oldstandard,string _newstandard)
  734. {
  735. List<string> errmess = new List<string>();
  736. List<Task<Response<BlobCopyInfo>>> file_list = new List<Task<Response<BlobCopyInfo>>>();
  737. var client = _azureStorage.GetBlobContainerClient($"{_cntr}"); //获取地址
  738. await foreach (BlobItem item in client.GetBlobsAsync(BlobTraits.None, BlobStates.None, $"yxpt/{_oldstandard}/"))
  739. {
  740. string oldurl1 = $"{item.Name}".Replace($"/{_oldstandard}/", $"/{_newstandard}/");
  741. //yxpt/standard6/jyzx/002ea332-0d1f-717c-0589-5f54c3c5ef4a/3 各班分数等级占比分析.mp4
  742. var urlSaS = _azureStorage.GetBlobSAS($"{_cntr}", item.Name, BlobSasPermissions.Read | BlobSasPermissions.List); // 取得容器sas 和有效期
  743. //client.GetBlobClient($"{newurl}").SyncCopyFromUri(new Uri(urlSaS));
  744. file_list.Add(client.GetBlobClient($"{oldurl1}").SyncCopyFromUriAsync(new Uri(urlSaS)));
  745. }
  746. if (file_list.Count <= 256)
  747. {
  748. await Task.WhenAll(file_list);
  749. }
  750. else
  751. {
  752. int pages = (file_list.Count + 255) / 256;
  753. for (int i = 0; i < pages; i++)
  754. {
  755. List<Task<Response<BlobCopyInfo>>> file_lists = file_list.Skip((i) * 256).Take(256).ToList();
  756. await Task.WhenAll(file_lists);
  757. }
  758. }
  759. if (errmess.Count > 0) return Ok(new { state = 201, errmess });
  760. else return Ok(new { state = 200, message = "全部复制成功" });
  761. }
  762. #endregion
  763. }
  764. }