BatchAreaController.cs 46 KB

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