BatchAreaController.cs 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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. try
  454. {
  455. //await _serviceBus.GetServiceBusClient().SendMessageAsync(activeTask, messageBatchCopyFile);
  456. await serBusClient.SendMessageAsync(activeTask, messageBatchCopyFile);
  457. }
  458. catch (Exception)
  459. {
  460. return Ok(new { state = 201, msg = "能力点复制成功,复制能力点的文件失败," });
  461. }
  462. //发送消息实体
  463. Notification notification = new Notification
  464. {
  465. hubName = "hita",
  466. type = "msg",
  467. from = $"BI:{_option.Location}:private",
  468. to = new List<string> { $"{ _tmdId}" },
  469. label = $"{partitionCode}_start",
  470. body = new { location = _option.Location, biz = partitionCode, tmdid = $"{_tmdId}", tmdname = $"{_tmdName}", status = 1, time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() }.ToJsonString(),
  471. expires = DateTimeOffset.UtcNow.AddDays(7).ToUnixTimeSeconds()
  472. };
  473. var url = _configuration.GetValue<string>("HaBookAuth:CoreService:sendnotification");
  474. var clientID = _configuration.GetValue<string>("HaBookAuth:CoreService:clientID");
  475. var clientSecret = _configuration.GetValue<string>("HaBookAuth:CoreService:clientSecret");
  476. var location = _option.Location;
  477. await _notificationService.SendNotification(clientID, clientSecret, location, url, notification); //站内发送消息
  478. //保存操作记录
  479. //await _azureStorage.SaveBILog("area-add", $"{_tmdName}【{_tmdId}】已操作创区功能模块:{name},当前标准【{standard}】,复制的微能力点:{tempStandard}", _dingDing, httpContext: HttpContext);
  480. await BIAzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "area-add", $"{_tmdName}【{_tmdId}】已操作创区功能模块:{name},当前标准【{standard}】,复制的微能力点:{tempStandard}", _dingDing, httpContext: HttpContext);
  481. return Ok(new { state = 200, area = addArea });
  482. }
  483. catch (Exception ex)
  484. {
  485. await _dingDing.SendBotMsg($"BI,{_option.Location} /batcharea/batch-area \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  486. return BadRequest();
  487. }
  488. }
  489. /// <summary>
  490. /// 创区后切换能力点, 先删除原来的能力点,后复制新的能力点
  491. /// </summary>
  492. /// <param name="jsonElement"></param>
  493. /// <returns></returns>
  494. [ProducesDefaultResponseType]
  495. [AuthToken(Roles = "admin,rdc")]
  496. [HttpPost("cut-standard")]
  497. public async Task<IActionResult> CutStandard(JsonElement jsonElement)
  498. {
  499. try
  500. {
  501. if (!jsonElement.TryGetProperty("oldId", out JsonElement _oldId)) return BadRequest();
  502. if (!jsonElement.TryGetProperty("oldStandard", out JsonElement _oldStandard)) return BadRequest();
  503. if (!jsonElement.TryGetProperty("newId", out JsonElement _newId)) return BadRequest();
  504. if (!jsonElement.TryGetProperty("newStandard", out JsonElement _newStandard)) return BadRequest();
  505. jsonElement.TryGetProperty("newName", out JsonElement newName);
  506. jsonElement.TryGetProperty("site", out JsonElement site);
  507. var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  508. //操作记录实体
  509. string blobOrTable = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString();
  510. var tableClient = _azureStorage.GetCloudTableClient();
  511. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  512. var cosmosClient = _azureCosmos.GetCosmosClient();
  513. var serBusClient = _serviceBus.GetServiceBusClient();
  514. var activeTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");//秘钥地址
  515. if ($"{site}".Equals(BIConst.GlobalSite))
  516. {
  517. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.GlobalSite);
  518. tableClient = _azureStorage.GetCloudTableClient(BIConst.GlobalSite);
  519. blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.GlobalSite);
  520. serBusClient = _serviceBus.GetServiceBusClient(BIConst.GlobalSite); //暂未确定使用默认
  521. //activeTask = _configuration.GetValue<string>("GlobalAzure:ServiceBus:ActiveTask"); //暂未确定使用默认
  522. }
  523. var table = tableClient.GetTableReference("IESLogin");
  524. //保存引用记录
  525. //await table.SaveOrUpdate<AreaQuoteRecord>(new AreaQuoteRecord() { PartitionKey = "QuoteRecord", RowKey = $"{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}", areaId = $"{_oldId}", quoteId = $"{_newId}", quoteName = $"{newName}", standard = $"{_newStandard}" });
  526. List<string> abilityIds = new List<string>(); //册别的ID集合
  527. //查询册别信息
  528. 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}") }))
  529. {
  530. abilityIds.Add(tempAbility.id); //查询出来册别ID添加册别ID集合
  531. }
  532. //删除册别
  533. if (abilityIds.IsNotEmpty())
  534. {
  535. var sresponse = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").DeleteItemsStreamAsync(abilityIds, $"Ability-{_oldStandard}");
  536. }
  537. List<string> abilityTaskIds = new List<string>(); //章节ID集合
  538. 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}") }))
  539. {
  540. abilityTaskIds.Add(item.id); //查询出来的章节信息ID添加到战绩集合
  541. }
  542. //删除章节
  543. if (abilityTaskIds.IsNotEmpty())
  544. {
  545. var sresponse = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").DeleteItemsStreamAsync(abilityTaskIds, $"AbilityTask-{_oldStandard}");
  546. }
  547. List<Task<ItemResponse<Ability>>> abilities = new List<Task<ItemResponse<Ability>>>(); //存储册别数据
  548. List<Task<ItemResponse<AbilityTask>>> abilityTasks = new List<Task<ItemResponse<AbilityTask>>>(); //存储章节
  549. //查询要复制区域的能力标准点
  550. 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}") }))
  551. {
  552. if (!string.IsNullOrEmpty(item.blob))
  553. {
  554. item.blob = item.blob.Replace($"/{_newStandard}/", $"/{_oldStandard}/");
  555. };
  556. item.standard = $"{_oldStandard}";
  557. item.code = $"Ability-{_oldStandard}";
  558. item.school = $"{_oldStandard}";
  559. //添加区能力标准点
  560. abilities.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(item, new PartitionKey($"Ability-{_oldStandard}")));
  561. }
  562. try
  563. {
  564. if (abilities.Count < 256)
  565. {
  566. await Task.WhenAll(abilities);
  567. }
  568. else
  569. {
  570. int pages = (abilities.Count + 255) / 256;
  571. for (int i = 0; i < pages; i++)
  572. {
  573. List<Task<ItemResponse<Ability>>> tempAbility = abilities.Skip((i) * 256).Take(256).ToList();
  574. await Task.WhenAll(tempAbility);
  575. }
  576. }
  577. }
  578. catch
  579. {
  580. return Ok(new { state = 200, msg = "创区成功,能力标准点复制失败,遗留数据影响!" });
  581. }
  582. try
  583. {
  584. //微能力点
  585. 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}") }))
  586. {
  587. List<Tnode> tnodes = new();
  588. foreach (Tnode tnode in atask.children)
  589. {
  590. if (tnode.rnodes != null)
  591. {
  592. List<Rnode> rnodes = new();
  593. foreach (Rnode rnode in tnode.rnodes)
  594. {
  595. if (!string.IsNullOrEmpty($"{rnode.link}"))
  596. {
  597. rnode.link = rnode.link.Replace($"/{_newStandard}/", $"/{_oldStandard}/");
  598. }
  599. rnodes.Add(rnode);
  600. }
  601. tnode.rnodes = rnodes;
  602. }
  603. tnodes.Add(tnode);
  604. }
  605. atask.children = tnodes;
  606. atask.code = $"AbilityTask-{_oldStandard}";
  607. atask.standard = $"{_oldStandard}";
  608. atask.codeval = $"{_oldStandard}";
  609. ////添加区能力标准点中的节点
  610. //abilityTasks.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(atask, new PartitionKey($"AbilityTask-{_oldStandard}")));
  611. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(atask, new PartitionKey($"AbilityTask-{_oldStandard}"));
  612. }
  613. }
  614. catch
  615. {
  616. return Ok(new { state = 200, msg = "创区成功,能力标准创建成功,微能力点复制失败,遗留数据影响!" });
  617. }
  618. StandardFile saveFile = new();
  619. //新政策文件
  620. 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") }))
  621. {
  622. if (standardFile != null)
  623. {
  624. standardFile.standard = $"{_oldStandard}";
  625. standardFile.id = $"{_oldId}";
  626. saveFile = standardFile;
  627. }
  628. }
  629. StandardFile tempFile = new();
  630. try
  631. {
  632. tempFile = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemAsync<StandardFile>(saveFile.id, new PartitionKey("StandardFile"));
  633. }
  634. catch
  635. {
  636. }
  637. if (tempFile.id != null)
  638. {
  639. if (tempFile.id.Equals(saveFile.id))
  640. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReplaceItemAsync<StandardFile>(saveFile, saveFile.id, new PartitionKey("StandardFile")); //直接替换以前的数据
  641. else
  642. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(saveFile, new PartitionKey($"StandardFile")); // 需要删除原来的政策文件数据在进行添加
  643. }
  644. //新的区域设置
  645. AreaSetting saveSetting = new AreaSetting();
  646. 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") }))
  647. {
  648. if (areaSetting != null)
  649. {
  650. areaSetting.accessConfig = null;
  651. areaSetting.id = $"{_oldId}";
  652. saveSetting = areaSetting;
  653. }
  654. }
  655. AreaSetting tempSetting = new();
  656. try
  657. {
  658. tempSetting = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemAsync<AreaSetting>(saveSetting.id, new PartitionKey("StandardFile"));
  659. }
  660. catch
  661. {
  662. }
  663. if (tempSetting.id != null)
  664. {
  665. if (tempSetting.id.Equals(saveSetting.id))
  666. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReplaceItemAsync<AreaSetting>(saveSetting, saveSetting.id, new PartitionKey($"AreaSetting")); //直接替换以前的数据
  667. else
  668. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(saveSetting, new PartitionKey($"AreaSetting")); //需要删除原来的区域设置数据在进行添加
  669. }
  670. //发送消息分区键
  671. string partitionCode = "DelBeforeCopyAbility-mark";
  672. //执行复制操作
  673. BatchCopyFile batchCopyFile = new BatchCopyFile();
  674. batchCopyFile.blobCntr = "teammodelos";
  675. batchCopyFile.oldFileName = $"{_newStandard}";
  676. batchCopyFile.newFileName = $"{_oldStandard}";
  677. batchCopyFile.tmdid = $"{_tmdId}";
  678. batchCopyFile.tmdIds = new List<string> { $"{ _tmdId}" };
  679. batchCopyFile.codeKey = partitionCode;
  680. batchCopyFile.tmdName = $"{_tmdName}";
  681. var messageBatchCopyFile = new ServiceBusMessage(batchCopyFile.ToJsonString());
  682. messageBatchCopyFile.ApplicationProperties.Add("name", "CopyStandardFile"); //Function暂时还未写
  683. try
  684. {
  685. //await _serviceBus.GetServiceBusClient().SendMessageAsync(activeTask, messageBatchCopyFile); //先执行删除操作,在执行复制 单一
  686. await serBusClient.SendMessageAsync(activeTask, messageBatchCopyFile); //先执行删除操作,在执行复制
  687. }
  688. catch (Exception)
  689. {
  690. return Ok(new { state = 201 ,msg = "能力点复制成功,复制能力点的文件失败," });
  691. }
  692. //发送消息实体
  693. Notification notification = new Notification
  694. {
  695. hubName = "hita",
  696. type = "msg",
  697. from = $"BI:{_option.Location}:private",
  698. to = new List<string> { $"{ _tmdId}" },
  699. label = $"{partitionCode}_start",
  700. body = new { location = _option.Location, biz = partitionCode, tmdid = $"{_tmdId}", tmdname = $"{_tmdName}", status = 1, time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() }.ToJsonString(),
  701. expires = DateTimeOffset.UtcNow.AddDays(7).ToUnixTimeSeconds()
  702. };
  703. var url = _configuration.GetValue<string>("HaBookAuth:CoreService:sendnotification");
  704. var clientID = _configuration.GetValue<string>("HaBookAuth:CoreService:clientID");
  705. var clientSecret = _configuration.GetValue<string>("HaBookAuth:CoreService:clientSecret");
  706. var location = _option.Location;
  707. await _notificationService.SendNotification(clientID, clientSecret, location, url, notification); //发送站内发送消息
  708. //保存操作记录
  709. //await _azureStorage.SaveBILog("area-cut", $"{_tmdName}【{_tmdId}】已操作【{_oldStandard}】切换至{_newStandard}微能力点,复制标准:{_newStandard}", _dingDing, httpContext: HttpContext);
  710. await BIAzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "area-cut", $"{_tmdName}【{_tmdId}】已操作【{_oldStandard}】切换至{_newStandard}微能力点,复制标准:{_newStandard}", _dingDing, httpContext: HttpContext);
  711. return Ok(new { state = 200 });
  712. }
  713. catch (Exception ex)
  714. {
  715. await _dingDing.SendBotMsg($"BI,{_option.Location} batcharea/cut-standard \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  716. return BadRequest();
  717. }
  718. }
  719. /// <summary>
  720. /// 区域列表
  721. /// </summary>
  722. public record RecArea
  723. {
  724. public string id { get; set; }
  725. public string code { get; set; }
  726. public string pk { get; set; }
  727. public string name { get; set; }
  728. public string provCode { get; set; }
  729. public string provName { get; set; }
  730. public string cityCode { get; set; }
  731. public string cityName { get; set; }
  732. public string standard { get; set; }
  733. public string standardName { get; set; }
  734. public string institution { get; set; }
  735. public int schoolCount { get; set; }
  736. public List<AreaQuoteRecord> aquoteRec { get; set; } = new List<AreaQuoteRecord>();
  737. }
  738. }
  739. }