BatchAreaController.cs 41 KB

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