BatchAreaController.cs 44 KB

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