BatchAreaController.cs 43 KB

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