BatchAreaController.cs 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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 Azure.Storage.Blobs.Models;
  24. using Azure;
  25. using Azure.Storage.Blobs.Specialized;
  26. using System.Net.Http;
  27. using System.Net.Http.Json;
  28. using System.Net;
  29. using TEAMModelOS.SDK;
  30. using TEAMModelOS.SDK.Context.BI;
  31. namespace TEAMModelBI.Controllers.BISchool
  32. {
  33. [Route("batcharea")]
  34. [ApiController]
  35. public class BatchAreaController : ControllerBase
  36. {
  37. private readonly AzureCosmosFactory _azureCosmos;
  38. private readonly DingDing _dingDing;
  39. private readonly Option _option;
  40. private readonly AzureStorageFactory _azureStorage;
  41. private readonly IConfiguration _configuration;
  42. private readonly NotificationService _notificationService;
  43. private readonly AzureServiceBusFactory _serviceBus;
  44. private readonly IHttpClientFactory _http;
  45. private readonly CoreAPIHttpService _coreAPIHttpService;
  46. public BatchAreaController(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage, IOptionsSnapshot<Option> option, IConfiguration configuration, NotificationService notificationService, AzureServiceBusFactory serviceBus, IHttpClientFactory http, CoreAPIHttpService coreAPIHttpService)
  47. {
  48. _azureCosmos = azureCosmos;
  49. _dingDing = dingDing;
  50. _azureStorage = azureStorage;
  51. _option = option?.Value;
  52. _configuration = configuration;
  53. _notificationService = notificationService;
  54. _serviceBus = serviceBus;
  55. _http = http;
  56. _coreAPIHttpService = coreAPIHttpService;
  57. }
  58. /// <summary>
  59. /// 查询所有的区域标准 //已对接
  60. /// </summary>
  61. /// <returns></returns>
  62. [ProducesDefaultResponseType]
  63. [HttpPost("get-areas")]
  64. public async Task<IActionResult> GetArea(JsonElement jsonElement)
  65. {
  66. try
  67. {
  68. jsonElement.TryGetProperty("site", out JsonElement site);
  69. int? pageSize = null; //默认不指定返回大小
  70. string continuationToken = string.Empty; //返给前端分页token
  71. string pageToken = default;//接受前端的分页Tolen
  72. bool iscontinuation = false;//是否需要进行分页查询,默认不分页
  73. if (jsonElement.TryGetProperty("pageSize", out JsonElement jsonPageSize))
  74. {
  75. if (!jsonPageSize.ValueKind.Equals(JsonValueKind.Undefined) && !jsonPageSize.ValueKind.Equals(JsonValueKind.Null) && jsonPageSize.TryGetInt32(out int tempPageSize))
  76. {
  77. pageSize = tempPageSize;
  78. }
  79. }
  80. if (pageSize != null && pageSize.Value > 0)
  81. {
  82. iscontinuation = true;
  83. }
  84. if (jsonElement.TryGetProperty("contToken", out JsonElement ContToken))
  85. {
  86. pageToken = ContToken.GetString();
  87. }
  88. List<RecArea> areas = new();
  89. var table = _azureStorage.GetCloudTableClient().GetTableReference("IESLogin");
  90. var cosmosClient = _azureCosmos.GetCosmosClient();
  91. if ($"{site}".Equals(BIConst.Global))
  92. {
  93. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  94. table = _azureStorage.GetCloudTableClient(BIConst.Global).GetTableReference("IESLogin");
  95. }
  96. string areaSql = $"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";
  97. await foreach (var itemArea in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryStreamIterator(queryText: areaSql, continuationToken: pageToken, requestOptions: new QueryRequestOptions() { MaxItemCount = pageSize, PartitionKey = new PartitionKey("Base-Area") }))
  98. {
  99. using var json = await JsonDocument.ParseAsync(itemArea.ContentStream);
  100. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetInt16() > 0)
  101. {
  102. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  103. {
  104. areas.Add(obj.ToObject<RecArea>());
  105. }
  106. if (iscontinuation)
  107. {
  108. continuationToken = itemArea.GetContinuationToken();
  109. break;
  110. }
  111. }
  112. }
  113. areas.ForEach(async area =>
  114. {
  115. area.schoolCount = await CommonFind.GetSqlValueCount(cosmosClient, "School", $"select value(count(c.id)) from c where c.areaId='{area.id}' and c.standard='{area.standard}'", "Base");
  116. area.aquoteRec = await table.QueryWhereString<AreaQuoteRecord>($"PartitionKey eq 'QuoteRecord' and areaId eq '{area.id}'");
  117. //List<AreaQuoteRecord> aqr = await table.QueryWhereString<AreaQuoteRecord>($"PartitionKey eq 'QuoteRecord' and areaId eq '{area.id}'");
  118. //aqr.Sort((x, y) => y.RowKey.CompareTo(x.RowKey));
  119. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<string>(queryText: $"select value(c.accessConfig) from c where c.id='{area.id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("AreaSetting") }))
  120. {
  121. if (string.IsNullOrEmpty(item))
  122. area.cutArea = false;
  123. else
  124. area.cutArea = true;
  125. };
  126. });
  127. //await foreach (var item in azureClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<RecArea>(queryText: areaSql,requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base-Area") }))
  128. //{
  129. // areas.Add(item);
  130. //}
  131. //foreach (var recArea in areas)
  132. //{
  133. // recArea.schoolCount = await CommonFind.GetSqlValueCount(cosmosClient, "School", $"select value(count(c.id)) from c where c.areaId='{recArea.id}' and c.standard='{recArea.standard}'", "Base");
  134. // List<AreaQuoteRecord> aqr = await table.QueryWhereString<AreaQuoteRecord>($"PartitionKey eq 'QuoteRecord' and areaId eq '{recArea.id}'");
  135. // aqr.Sort((x, y) => y.RowKey.CompareTo(x.RowKey));
  136. // recArea.aquoteRec = aqr;
  137. // await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<string>(queryText: $"select value(c.accessConfig) from c where c.id='{recArea.id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("AreaSetting") }))
  138. // {
  139. // if (string.IsNullOrEmpty(item))
  140. // recArea.cutArea = false;
  141. // else
  142. // recArea.cutArea = true;
  143. // };
  144. //}
  145. return Ok(new { state = 200, areas, continuationToken });
  146. }
  147. catch (Exception ex)
  148. {
  149. await _dingDing.SendBotMsg($"BI,{_option.Location} batcharea/get-areas \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  150. return BadRequest();
  151. }
  152. }
  153. /// <summary>
  154. /// 批量创区 新接口 //已对接
  155. /// </summary>
  156. /// <param name="jsonElement"></param>
  157. /// <returns></returns>
  158. [ProducesDefaultResponseType]
  159. [AuthToken(Roles = "admin,rdc")]
  160. [HttpPost("batch-area")]
  161. public async Task<IActionResult> batchArea(JsonElement jsonElement)
  162. {
  163. try
  164. {
  165. if (!jsonElement.TryGetProperty("name", out JsonElement name)) return BadRequest();
  166. jsonElement.TryGetProperty("provCode", out JsonElement provCode);
  167. jsonElement.TryGetProperty("provName", out JsonElement provName);
  168. jsonElement.TryGetProperty("cityCode", out JsonElement cityCode);
  169. jsonElement.TryGetProperty("cityName", out JsonElement cityName);
  170. jsonElement.TryGetProperty("areaAdmin", out JsonElement areadAdmin);
  171. if (!jsonElement.TryGetProperty("standard", out JsonElement standard)) return BadRequest();
  172. if (!jsonElement.TryGetProperty("standardName", out JsonElement standardName)) return BadRequest();
  173. jsonElement.TryGetProperty("institution", out JsonElement institution);
  174. jsonElement.TryGetProperty("oldId", out JsonElement _oldId);
  175. jsonElement.TryGetProperty("oldStandard", out JsonElement oldStandard);
  176. jsonElement.TryGetProperty("oldName", out JsonElement oldName);
  177. jsonElement.TryGetProperty("site", out JsonElement site);
  178. var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  179. //操作记录实体
  180. var tempStandard = !string.IsNullOrEmpty($"{oldStandard}") && !string.IsNullOrEmpty($"{_oldId}") ? $"{oldStandard}" : "standard2";
  181. //var table = _azureStorage.GetCloudTableClient().GetTableReference("IESLogin");
  182. var cosmosClient = _azureCosmos.GetCosmosClient();//数据库连接
  183. var tableClient = _azureStorage.GetCloudTableClient();
  184. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  185. ServiceBusClient serBusClient;
  186. try
  187. {
  188. serBusClient = _serviceBus.GetServiceBusClient();
  189. }
  190. catch
  191. {
  192. return Ok(new { state = 403, msg = "Functionn未启动,请联系管理员" });
  193. }
  194. var activeTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");//秘钥地址
  195. if ($"{site}".Equals(BIConst.Global))
  196. {
  197. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  198. tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
  199. blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", name: BIConst.Global);
  200. //serBusClient = _serviceBus.GetServiceBusClient(BIConst.Global); //暂未确定使用默认
  201. //activeTask = _configuration.GetValue<string>("GlobalAzure:ServiceBus:ActiveTask"); //暂未确定使用默认
  202. }
  203. var table = tableClient.GetTableReference("IESLogin");
  204. //查询新的是否存在
  205. 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") }))
  206. {
  207. if (item.standard.Equals($"{standard}"))
  208. return Ok(new { state = 1, message = "新创区的standard已存在请检查" });
  209. }
  210. //区级的ID
  211. string areaId = Guid.NewGuid().ToString();
  212. Area addArea = new Area()
  213. {
  214. id = areaId,
  215. code = $"Base-Area",
  216. name = $"{name}",
  217. provCode = $"{provCode}",
  218. provName = $"{provName}",
  219. cityCode = $"{cityCode}",
  220. cityName = $"{cityName}",
  221. standard = $"{standard}",
  222. standardName = $"{standardName}",
  223. institution = $"{institution}"
  224. };
  225. #region 区级管理员
  226. var coreUser = await _coreAPIHttpService.GetUserInfo(new Dictionary<string, string> { { "key", $"{areadAdmin}" } }, _option.Location, _configuration);
  227. if (coreUser == null || coreUser.id == null)
  228. return Ok(new { state = 404, msg = "未找到改账户的管理员" });
  229. //string tmdId = !string.IsNullOrEmpty(tempTmdId) ? tempTmdId : $"{areadAdmin}";
  230. Teacher teacher = null;
  231. try
  232. {
  233. //查询该教师是否存在
  234. teacher = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<Teacher>($"{coreUser.id}", new PartitionKey("Base"));
  235. }
  236. catch
  237. {
  238. }
  239. if (teacher != null)
  240. {
  241. //教师存在,在该教师信息中添加要管理的学校信息
  242. teacher.areas.Add(new Teacher.TeacherArea { areaId = addArea.id, status = "join", name = addArea.name });
  243. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync<Teacher>(teacher, teacher.id, new PartitionKey("Base"));
  244. }
  245. else
  246. {
  247. teacher.id = coreUser.id;
  248. teacher.name = coreUser.name;
  249. teacher.picture = coreUser.picture;
  250. teacher.pk = "Base";
  251. teacher.code = "Base";
  252. teacher.size = 1;
  253. teacher.createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  254. //教师存在,在该教师信息中添加要管理的学校信息
  255. teacher.areas.Add(new Teacher.TeacherArea { areaId = addArea.id, status = "join", name = addArea.name });
  256. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").CreateItemAsync<Teacher>(teacher, new PartitionKey("Base"));
  257. }
  258. #endregion
  259. //创建区域
  260. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync<Area>(addArea, new PartitionKey("Base-Area"));
  261. //保存引用记录
  262. await table.SaveOrUpdate<AreaQuoteRecord>(new AreaQuoteRecord() { PartitionKey = "QuoteRecord", RowKey = $"{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}", areaId = $"{areaId}", quoteId = $"{_oldId}", quoteName = $"{oldName}", standard = tempStandard });
  263. //消息分区键
  264. string partitionCode = "copyAbility-mark";
  265. #region 使用Function创区
  266. //string areaCode = "copyArea-mark";
  267. //BatchCopyAreaRelevant bCopyArea = new BatchCopyAreaRelevant()
  268. //{
  269. // tmdid = $"{_tmdId}",
  270. // tmdName = $"{_tmdName}",
  271. // codeKey = areaCode,
  272. // tmdIds = new List<string> { $"{_tmdId}"},
  273. // oldStandard = $"{oldStandard}",
  274. // oldId = $"{_oldId}",
  275. // standard = $"{standard}",
  276. // areaId = areaId,
  277. // cut="",
  278. //};
  279. //var messageBatchCopyArea = new ServiceBusMessage(bCopyArea.ToJsonString());
  280. //messageBatchCopyArea.ApplicationProperties.Add("name", "CopyAreaRelevant");
  281. //var activeTaskArea = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
  282. //await _serviceBus.GetServiceBusClient().SendMessageAsync(activeTaskArea, messageBatchCopyArea);
  283. #endregion
  284. #region 旧的批量创区
  285. List<Task<ItemResponse<Ability>>> abilities = new List<Task<ItemResponse<Ability>>>(); //存储区域数据
  286. List<Task<ItemResponse<AbilityTask>>> abilityTasks = new List<Task<ItemResponse<AbilityTask>>>(); //存储章节
  287. if (!string.IsNullOrEmpty($"{oldStandard}") && !string.IsNullOrEmpty($"{_oldId}"))
  288. {
  289. //查询要复制区域的能力标准点
  290. 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}") }))
  291. {
  292. if (!string.IsNullOrEmpty(item.blob))
  293. {
  294. item.blob = item.blob.Replace($"/{oldStandard}/", $"/{standard}/");
  295. };
  296. item.standard = $"{standard}";
  297. item.code = $"Ability-{standard}";
  298. item.school = $"{standard}";
  299. //添加区能力标准点
  300. abilities.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(item, new PartitionKey($"Ability-{standard}")));
  301. }
  302. try
  303. {
  304. if (abilities.Count < 256)
  305. {
  306. await Task.WhenAll(abilities);
  307. }
  308. else
  309. {
  310. int pages = (abilities.Count + 255) / 256;
  311. for (int i = 0; i < pages; i++)
  312. {
  313. List<Task<ItemResponse<Ability>>> tempAbility = abilities.Skip((i) * 256).Take(256).ToList();
  314. await Task.WhenAll(tempAbility);
  315. }
  316. }
  317. }
  318. catch
  319. {
  320. return Ok(new { state = 200, msg = "创区成功,能力标准点复制失败,遗留数据影响!" });
  321. }
  322. try
  323. {
  324. //微能力点
  325. 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}") }))
  326. {
  327. List<Tnode> tnodes = new List<Tnode>();
  328. foreach (Tnode tnode in atask.children)
  329. {
  330. if (tnode.rnodes != null)
  331. {
  332. List<Rnode> rnodes = new List<Rnode>();
  333. foreach (Rnode rnode in tnode.rnodes)
  334. {
  335. if (!string.IsNullOrEmpty($"{rnode.link}"))
  336. {
  337. rnode.link = rnode.link.Replace($"/{oldStandard}/", $"/{standard}/");
  338. }
  339. rnodes.Add(rnode);
  340. }
  341. tnode.rnodes = rnodes;
  342. }
  343. tnodes.Add(tnode);
  344. }
  345. atask.children = tnodes;
  346. atask.code = $"AbilityTask-{standard}";
  347. atask.standard = $"{standard}";
  348. atask.codeval = $"{standard}";
  349. //添加区能力标准点中的节点
  350. //abilityTasks.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(atask, new PartitionKey($"AbilityTask-{standard}")));
  351. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(atask, new PartitionKey($"AbilityTask-{standard}"));
  352. }
  353. }
  354. catch
  355. {
  356. return Ok(new { state = 200, msg = "创区成功,能力标准创建成功,微能力点复制失败,遗留数据影响!" });
  357. }
  358. //if (abilityTasks.Count > 0)
  359. //{
  360. // for (int i = 0; i < abilityTasks.Count; i++)
  361. // {
  362. // List<Task<ItemResponse<AbilityTask>>> tempAbilityTasks = abilityTasks.Skip((i)).Take(1).ToList();
  363. // await Task.WhenAll(tempAbilityTasks);
  364. // }
  365. //}
  366. //新政策文件
  367. 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") }))
  368. {
  369. if (standardFile != null)
  370. {
  371. standardFile.standard = $"{standard}";
  372. standardFile.id = areaId;
  373. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(standardFile, new PartitionKey($"StandardFile"));
  374. }
  375. }
  376. //新的区域设置
  377. 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") }))
  378. {
  379. if (areaSetting != null)
  380. {
  381. areaSetting.accessConfig = null;
  382. areaSetting.id = areaId;
  383. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(areaSetting, new PartitionKey($"AreaSetting"));
  384. }
  385. }
  386. }
  387. else
  388. {
  389. Area area = null;
  390. 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") }))
  391. {
  392. area = tempArea;
  393. }
  394. if (area != null)
  395. {
  396. //查询要复制区域的能力标准点
  397. 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}") }))
  398. {
  399. if (!string.IsNullOrEmpty(item.blob))
  400. {
  401. item.blob = item.blob.Replace($"/{area.standard}/", $"/{standard}/");
  402. };
  403. item.standard = $"{standard}";
  404. item.code = $"Ability-{standard}";
  405. item.school = $"{standard}";
  406. //添加区能力标准点
  407. abilities.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(item, new PartitionKey($"Ability-{standard}")));
  408. }
  409. try
  410. {
  411. if (abilities.Count < 256)
  412. {
  413. await Task.WhenAll(abilities);
  414. }
  415. else
  416. {
  417. int pages = (abilities.Count + 255) / 256;
  418. for (int i = 0; i < pages; i++)
  419. {
  420. List<Task<ItemResponse<Ability>>> tempAbility = abilities.Skip((i) * 256).Take(256).ToList();
  421. await Task.WhenAll(tempAbility);
  422. }
  423. }
  424. }
  425. catch
  426. {
  427. return Ok(new { state = 200, msg = "创区成功,能力标准点复制失败,遗留数据影响!" });
  428. }
  429. try
  430. {
  431. //微能力点
  432. 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}") }))
  433. {
  434. List<Tnode> tnodes = new List<Tnode>();
  435. foreach (Tnode tnode in atask.children)
  436. {
  437. if (tnode.rnodes != null)
  438. {
  439. List<Rnode> rnodes = new List<Rnode>();
  440. foreach (Rnode rnode in tnode.rnodes)
  441. {
  442. if (!string.IsNullOrEmpty($"{rnode.link}"))
  443. {
  444. rnode.link = rnode.link.Replace($"/{area.standard}/", $"/{standard}/");
  445. }
  446. rnodes.Add(rnode);
  447. }
  448. tnode.rnodes = rnodes;
  449. }
  450. tnodes.Add(tnode);
  451. }
  452. atask.children = tnodes;
  453. atask.code = $"AbilityTask-{standard}";
  454. atask.standard = $"{standard}";
  455. atask.codeval = $"{standard}";
  456. //添加区能力标准点中的节点
  457. //abilityTasks.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(atask, new PartitionKey($"AbilityTask-{standard}")));
  458. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(atask, new PartitionKey($"AbilityTask-{standard}"));
  459. }
  460. }
  461. catch
  462. {
  463. return Ok(new { state = 200, msg = "创区成功,能力标准创建成功,微能力点复制失败,遗留数据影响!" });
  464. }
  465. //if (abilityTasks.Count > 0)
  466. //{
  467. // for (int i = 0; i < abilityTasks.Count; i++)
  468. // {
  469. // List<Task<ItemResponse<AbilityTask>>> tempAbilityTasks = abilityTasks.Skip(i).Take(1).ToList();
  470. // await Task.WhenAll(tempAbilityTasks);
  471. // }
  472. //}
  473. //新政策文件
  474. 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") }))
  475. {
  476. if (standardFile != null)
  477. {
  478. standardFile.standard = $"{standard}";
  479. standardFile.id = areaId;
  480. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(standardFile, new PartitionKey($"StandardFile"));
  481. }
  482. }
  483. //新的区域设置
  484. 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") }))
  485. {
  486. if (areaSetting != null)
  487. {
  488. areaSetting.accessConfig = null;
  489. areaSetting.id = areaId;
  490. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(areaSetting, new PartitionKey($"AreaSetting"));
  491. }
  492. }
  493. }
  494. else return Ok(new { state = 201, message = "未找到默认能力点!" });
  495. }
  496. #endregion
  497. //执行复制操作
  498. BatchCopyFile batchCopyFile = new BatchCopyFile();
  499. batchCopyFile.blobCntr = "teammodelos";
  500. batchCopyFile.oldFileName = $"{oldStandard}";
  501. batchCopyFile.newFileName = $"{standard}";
  502. batchCopyFile.tmdid = $"{_tmdId}";
  503. batchCopyFile.tmdIds = new List<string> { $"{ _tmdId}" };
  504. batchCopyFile.codeKey = partitionCode;
  505. batchCopyFile.tmdName = $"{_tmdName}";
  506. var messageBatchCopyFile = new ServiceBusMessage(batchCopyFile.ToJsonString());
  507. messageBatchCopyFile.ApplicationProperties.Add("name", "CopyStandardFile");
  508. //var activeTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
  509. try
  510. {
  511. //await _serviceBus.GetServiceBusClient().SendMessageAsync(activeTask, messageBatchCopyFile);
  512. await serBusClient.SendMessageAsync(activeTask, messageBatchCopyFile);
  513. }
  514. catch (Exception)
  515. {
  516. return Ok(new { state = 201, msg = "能力点复制成功,复制能力点的文件失败," });
  517. }
  518. //发送消息实体
  519. Notification notification = new Notification
  520. {
  521. hubName = "hita",
  522. type = "msg",
  523. from = $"BI:{_option.Location}:private",
  524. to = new List<string> { $"{ _tmdId}" },
  525. label = $"{partitionCode}_start",
  526. body = new { location = _option.Location, biz = partitionCode, tmdid = $"{_tmdId}", tmdname = $"{_tmdName}", status = 1, time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() }.ToJsonString(),
  527. expires = DateTimeOffset.UtcNow.AddDays(7).ToUnixTimeSeconds()
  528. };
  529. var notiUrl = _configuration.GetValue<string>("HaBookAuth:CoreService:sendnotification");
  530. var clientID = _configuration.GetValue<string>("HaBookAuth:CoreService:clientID");
  531. var clientSecret = _configuration.GetValue<string>("HaBookAuth:CoreService:clientSecret");
  532. var location = _option.Location;
  533. await _notificationService.SendNotification(clientID, clientSecret, location, notiUrl, notification); //站内发送消息
  534. //保存操作记录
  535. //await _azureStorage.SaveBILog("area-add", $"{_tmdName}【{_tmdId}】已操作创区功能模块:{name},当前标准【{standard}】,复制的微能力点:{tempStandard}", _dingDing, httpContext: HttpContext);
  536. await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "area-add", $"{_tmdName}【{_tmdId}】已操作创区功能模块:{name},当前标准【{standard}】,复制的微能力点:{tempStandard}", _dingDing, httpContext: HttpContext);
  537. return Ok(new { state = 200, area = addArea });
  538. }
  539. catch (Exception ex)
  540. {
  541. await _dingDing.SendBotMsg($"BI,{_option.Location} /batcharea/batch-area \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  542. return BadRequest();
  543. }
  544. }
  545. /// <summary>
  546. /// 创区后切换能力点, 先删除原来的能力点,后复制新的能力点 //已对接
  547. /// </summary>
  548. /// <param name="jsonElement"></param>
  549. /// <returns></returns>
  550. [ProducesDefaultResponseType]
  551. [AuthToken(Roles = "admin,rdc")]
  552. [HttpPost("cut-standard")]
  553. public async Task<IActionResult> CutStandard(JsonElement jsonElement)
  554. {
  555. try
  556. {
  557. if (!jsonElement.TryGetProperty("oldId", out JsonElement _oldId)) return BadRequest();
  558. if (!jsonElement.TryGetProperty("oldStandard", out JsonElement _oldStandard)) return BadRequest();
  559. if (!jsonElement.TryGetProperty("newId", out JsonElement _newId)) return BadRequest();
  560. if (!jsonElement.TryGetProperty("newStandard", out JsonElement _newStandard)) return BadRequest();
  561. jsonElement.TryGetProperty("newName", out JsonElement newName);
  562. jsonElement.TryGetProperty("site", out JsonElement site);
  563. var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  564. //操作记录实体
  565. string blobOrTable = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString();
  566. var tableClient = _azureStorage.GetCloudTableClient();
  567. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  568. var cosmosClient = _azureCosmos.GetCosmosClient();
  569. var serBusClient = _serviceBus.GetServiceBusClient();
  570. var activeTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");//秘钥地址
  571. if ($"{site}".Equals(BIConst.Global))
  572. {
  573. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  574. tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
  575. blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
  576. serBusClient = _serviceBus.GetServiceBusClient(BIConst.Global); //暂未确定使用默认
  577. //activeTask = _configuration.GetValue<string>("GlobalAzure:ServiceBus:ActiveTask"); //暂未确定使用默认
  578. }
  579. var table = tableClient.GetTableReference("IESLogin");
  580. var responseSet = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemStreamAsync($"{_oldId}", new PartitionKey("AreaSetting"));
  581. if (responseSet.Status == 200)
  582. {
  583. using var fileJson = await JsonDocument.ParseAsync(responseSet.ContentStream);
  584. AreaSetting delSet = fileJson.ToObject<AreaSetting>();
  585. if (!string.IsNullOrEmpty(delSet.accessConfig))
  586. return Ok(new { state = 401, msg = "区域已经规定了,不能切换能能力" });
  587. }
  588. //保存引用记录
  589. //await table.SaveOrUpdate<AreaQuoteRecord>(new AreaQuoteRecord() { PartitionKey = "QuoteRecord", RowKey = $"{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}", areaId = $"{_oldId}", quoteId = $"{_newId}", quoteName = $"{newName}", standard = $"{_newStandard}" });
  590. List<string> abilityIds = new List<string>(); //册别的ID集合
  591. //查询册别信息
  592. 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}") }))
  593. {
  594. abilityIds.Add(tempAbility.id); //查询出来册别ID添加册别ID集合
  595. }
  596. //删除册别
  597. if (abilityIds.IsNotEmpty())
  598. {
  599. var sresponse = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").DeleteItemsStreamAsync(abilityIds, $"Ability-{_oldStandard}");
  600. }
  601. List<string> abilityTaskIds = new List<string>(); //章节ID集合
  602. 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}") }))
  603. {
  604. abilityTaskIds.Add(item.id); //查询出来的章节信息ID添加到战绩集合
  605. }
  606. //删除章节
  607. if (abilityTaskIds.IsNotEmpty())
  608. {
  609. var sresponse = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").DeleteItemsStreamAsync(abilityTaskIds, $"AbilityTask-{_oldStandard}");
  610. }
  611. List<Task<ItemResponse<Ability>>> abilities = new List<Task<ItemResponse<Ability>>>(); //存储册别数据
  612. List<Task<ItemResponse<AbilityTask>>> abilityTasks = new List<Task<ItemResponse<AbilityTask>>>(); //存储章节
  613. //查询要复制区域的能力标准点
  614. 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}") }))
  615. {
  616. if (!string.IsNullOrEmpty(item.blob))
  617. {
  618. item.blob = item.blob.Replace($"/{_newStandard}/", $"/{_oldStandard}/");
  619. };
  620. item.standard = $"{_oldStandard}";
  621. item.code = $"Ability-{_oldStandard}";
  622. item.school = $"{_oldStandard}";
  623. //添加区能力标准点
  624. abilities.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(item, new PartitionKey($"Ability-{_oldStandard}")));
  625. }
  626. try
  627. {
  628. if (abilities.Count < 256)
  629. {
  630. await Task.WhenAll(abilities);
  631. }
  632. else
  633. {
  634. int pages = (abilities.Count + 255) / 256;
  635. for (int i = 0; i < pages; i++)
  636. {
  637. List<Task<ItemResponse<Ability>>> tempAbility = abilities.Skip((i) * 256).Take(256).ToList();
  638. await Task.WhenAll(tempAbility);
  639. }
  640. }
  641. }
  642. catch
  643. {
  644. return Ok(new { state = 200, msg = "创区成功,能力标准点复制失败,遗留数据影响!" });
  645. }
  646. try
  647. {
  648. //微能力点
  649. 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}") }))
  650. {
  651. List<Tnode> tnodes = new();
  652. foreach (Tnode tnode in atask.children)
  653. {
  654. if (tnode.rnodes != null)
  655. {
  656. List<Rnode> rnodes = new();
  657. foreach (Rnode rnode in tnode.rnodes)
  658. {
  659. if (!string.IsNullOrEmpty($"{rnode.link}"))
  660. {
  661. rnode.link = rnode.link.Replace($"/{_newStandard}/", $"/{_oldStandard}/");
  662. }
  663. rnodes.Add(rnode);
  664. }
  665. tnode.rnodes = rnodes;
  666. }
  667. tnodes.Add(tnode);
  668. }
  669. atask.children = tnodes;
  670. atask.code = $"AbilityTask-{_oldStandard}";
  671. atask.standard = $"{_oldStandard}";
  672. atask.codeval = $"{_oldStandard}";
  673. ////添加区能力标准点中的节点
  674. //abilityTasks.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(atask, new PartitionKey($"AbilityTask-{_oldStandard}")));
  675. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(atask, new PartitionKey($"AbilityTask-{_oldStandard}"));
  676. }
  677. }
  678. catch
  679. {
  680. return Ok(new { state = 200, msg = "创区成功,能力标准创建成功,微能力点复制失败,遗留数据影响!" });
  681. }
  682. StandardFile saveFile = new();
  683. //新政策文件
  684. 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") }))
  685. {
  686. if (standardFile != null)
  687. {
  688. standardFile.standard = $"{_oldStandard}";
  689. standardFile.id = $"{_oldId}";
  690. saveFile = standardFile;
  691. }
  692. }
  693. StandardFile tempFile = new();
  694. try
  695. {
  696. tempFile = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemAsync<StandardFile>(saveFile.id, new PartitionKey("StandardFile"));
  697. }
  698. catch
  699. {
  700. }
  701. if (tempFile.id != null)
  702. {
  703. if (tempFile.id.Equals(saveFile.id))
  704. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReplaceItemAsync<StandardFile>(saveFile, saveFile.id, new PartitionKey("StandardFile")); //直接替换以前的数据
  705. else
  706. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(saveFile, new PartitionKey($"StandardFile")); // 需要删除原来的政策文件数据在进行添加
  707. }
  708. //新的区域设置
  709. AreaSetting saveSetting = new AreaSetting();
  710. 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") }))
  711. {
  712. if (areaSetting != null)
  713. {
  714. areaSetting.accessConfig = null;
  715. areaSetting.id = $"{_oldId}";
  716. saveSetting = areaSetting;
  717. }
  718. }
  719. AreaSetting tempSetting = new();
  720. try
  721. {
  722. tempSetting = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemAsync<AreaSetting>(saveSetting.id, new PartitionKey("StandardFile"));
  723. }
  724. catch
  725. {
  726. }
  727. if (tempSetting.id != null)
  728. {
  729. if (tempSetting.id.Equals(saveSetting.id))
  730. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReplaceItemAsync<AreaSetting>(saveSetting, saveSetting.id, new PartitionKey($"AreaSetting")); //直接替换以前的数据
  731. else
  732. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(saveSetting, new PartitionKey($"AreaSetting")); //需要删除原来的区域设置数据在进行添加
  733. }
  734. //发送消息分区键
  735. string partitionCode = "DelBeforeCopyAbility-mark";
  736. //执行复制操作
  737. BatchCopyFile batchCopyFile = new BatchCopyFile();
  738. batchCopyFile.blobCntr = "teammodelos";
  739. batchCopyFile.oldFileName = $"{_newStandard}";
  740. batchCopyFile.newFileName = $"{_oldStandard}";
  741. batchCopyFile.tmdid = $"{_tmdId}";
  742. batchCopyFile.tmdIds = new List<string> { $"{ _tmdId}" };
  743. batchCopyFile.codeKey = partitionCode;
  744. batchCopyFile.tmdName = $"{_tmdName}";
  745. var messageBatchCopyFile = new ServiceBusMessage(batchCopyFile.ToJsonString());
  746. messageBatchCopyFile.ApplicationProperties.Add("name", "CopyStandardFile");
  747. try
  748. {
  749. //await _serviceBus.GetServiceBusClient().SendMessageAsync(activeTask, messageBatchCopyFile); //先执行删除操作,在执行复制 单一
  750. await serBusClient.SendMessageAsync(activeTask, messageBatchCopyFile); //先执行删除操作,在执行复制
  751. }
  752. catch (Exception)
  753. {
  754. return Ok(new { state = 201 ,msg = "能力点复制成功,复制能力点的文件失败," });
  755. }
  756. //发送消息实体
  757. Notification notification = new Notification
  758. {
  759. hubName = "hita",
  760. type = "msg",
  761. from = $"BI:{_option.Location}:private",
  762. to = new List<string> { $"{ _tmdId}" },
  763. label = $"{partitionCode}_start",
  764. body = new { location = _option.Location, biz = partitionCode, tmdid = $"{_tmdId}", tmdname = $"{_tmdName}", status = 1, time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() }.ToJsonString(),
  765. expires = DateTimeOffset.UtcNow.AddDays(7).ToUnixTimeSeconds()
  766. };
  767. var url = _configuration.GetValue<string>("HaBookAuth:CoreService:sendnotification");
  768. var clientID = _configuration.GetValue<string>("HaBookAuth:CoreService:clientID");
  769. var clientSecret = _configuration.GetValue<string>("HaBookAuth:CoreService:clientSecret");
  770. var location = _option.Location;
  771. await _notificationService.SendNotification(clientID, clientSecret, location, url, notification); //发送站内发送消息
  772. //保存操作记录
  773. //await _azureStorage.SaveBILog("area-cut", $"{_tmdName}【{_tmdId}】已操作【{_oldStandard}】切换至{_newStandard}微能力点,复制标准:{_newStandard}", _dingDing, httpContext: HttpContext);
  774. await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "area-cut", $"{_tmdName}【{_tmdId}】已操作【{_oldStandard}】切换至{_newStandard}微能力点,复制标准:{_newStandard}", _dingDing, httpContext: HttpContext);
  775. return Ok(new { state = 200 });
  776. }
  777. catch (Exception ex)
  778. {
  779. await _dingDing.SendBotMsg($"BI,{_option.Location} batcharea/cut-standard \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  780. return BadRequest();
  781. }
  782. }
  783. /// <summary>
  784. /// 删除区域和区域的册别、能力点、区域文件、区域设置
  785. /// </summary>
  786. /// <param name="jsonElement"></param>
  787. /// <returns></returns>
  788. [HttpPost("del-area")]
  789. public async Task<IActionResult> DelArea(JsonElement jsonElement)
  790. {
  791. if (!jsonElement.TryGetProperty("areaId", out JsonElement areaId)) return BadRequest();
  792. if (!jsonElement.TryGetProperty("standard", out JsonElement standard)) return BadRequest();
  793. jsonElement.TryGetProperty("site", out JsonElement site);
  794. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "teammodelos");
  795. var cosmosClient = _azureCosmos.GetCosmosClient();
  796. if ($"{site}".Equals(BIConst.Global))
  797. {
  798. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  799. blobClient = _azureStorage.GetBlobContainerClient(containerName: "teammodelos", BIConst.Global);
  800. }
  801. var response = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemStreamAsync($"{areaId}", new PartitionKey("Base-Area"));
  802. if (response.Status == 200)
  803. {
  804. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  805. Area area = json.ToObject<Area>();
  806. if (area.standard == $"{standard}")
  807. {
  808. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").DeleteItemAsync<Area>(area.id, new PartitionKey("Base-Area")); //删除区
  809. List<string> scIds = await CommonFind.FindSchoolIds(cosmosClient, $"select c.id from c where c.areaId='{area.id}' and c.standard ='{area.standard}'", "Base");
  810. foreach (var item in scIds)
  811. {
  812. School school = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(item, new PartitionKey("Base"));
  813. school.areaId = "";
  814. school.standard = "";
  815. school = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<School>(school, school.id, new PartitionKey("Base"));
  816. }
  817. List<string> abilityIds = new(); //册别的ID集合
  818. List<string> abilityTaskIds = new(); //章节ID集合
  819. //查询册别信息
  820. 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-{area.standard}") }))
  821. {
  822. abilityIds.Add(tempAbility.id); //查询出来册别ID添加册别ID集合
  823. }
  824. //删除册别
  825. if (abilityIds.IsNotEmpty())
  826. {
  827. var sresponse = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").DeleteItemsStreamAsync(abilityIds, $"Ability-{area.standard}");
  828. }
  829. 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-{area.standard}") }))
  830. {
  831. abilityTaskIds.Add(item.id); //查询出来的章节信息ID添加到战绩集合
  832. }
  833. //删除章节
  834. if (abilityTaskIds.IsNotEmpty())
  835. {
  836. var sresponse = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").DeleteItemsStreamAsync(abilityTaskIds, $"AbilityTask-{standard}");
  837. }
  838. //区域政策文件
  839. StandardFile delFile = new();
  840. var responseFile = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemStreamAsync(area.id, new PartitionKey("StandardFile"));
  841. if(responseFile.Status == 200)
  842. {
  843. using var fileJson = await JsonDocument.ParseAsync(responseFile.ContentStream);
  844. delFile = fileJson.ToObject<StandardFile>();
  845. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").DeleteItemAsync<StandardFile>(delFile.id, new PartitionKey("StandardFile"));
  846. }
  847. //区域设置
  848. AreaSetting delSet = new();
  849. var responseSet = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemStreamAsync(area.id, new PartitionKey("AreaSetting"));
  850. if (responseSet.Status == 200)
  851. {
  852. using var fileJson = await JsonDocument.ParseAsync(responseSet.ContentStream);
  853. delSet = fileJson.ToObject<AreaSetting>();
  854. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").DeleteItemAsync<AreaSetting>(delSet.id, new PartitionKey("AreaSetting"));
  855. }
  856. List<object> fileobj = new();
  857. //先删除原有的文件
  858. List<Task<Response<bool>>> DelList = new List<Task<Response<bool>>>();
  859. await foreach (BlobItem blobItem in blobClient.GetBlobsAsync(BlobTraits.None, BlobStates.None, $"yxpt/{area.standard}/"))
  860. {
  861. fileobj.Add(blobItem.Name);
  862. DelList.Add(blobClient.GetBlobBaseClient(blobItem.Name).DeleteIfExistsAsync());
  863. }
  864. if (DelList.Count <= 256)
  865. {
  866. await Task.WhenAll(DelList);
  867. }
  868. else
  869. {
  870. int pages = (DelList.Count + 255) / 256;
  871. for (int i = 0; i < pages; i++)
  872. {
  873. List<Task<Response<bool>>> delList = DelList.Skip((i) * 256).Take(256).ToList();
  874. await Task.WhenAll(delList);
  875. }
  876. }
  877. return Ok(new { state = 200,area, scIds, abilityIds, abilityTaskIds, delFile, delSet, fileobj });
  878. }
  879. else return Ok(new { state = 400, msg = "id和standard不匹配!" });
  880. }
  881. else return Ok(new { state = 404,msg="依据Id未找到该区!" });
  882. }
  883. /// <summary>
  884. /// 区域列表
  885. /// </summary>
  886. public record RecArea
  887. {
  888. public string id { get; set; }
  889. public string code { get; set; }
  890. public string pk { get; set; }
  891. public string name { get; set; }
  892. public string provCode { get; set; }
  893. public string provName { get; set; }
  894. public string cityCode { get; set; }
  895. public string cityName { get; set; }
  896. public string standard { get; set; }
  897. public string standardName { get; set; }
  898. public string institution { get; set; }
  899. public int schoolCount { get; set; }
  900. public bool cutArea { get; set; } = false;
  901. public List<AreaQuoteRecord> aquoteRec { get; set; } = new List<AreaQuoteRecord>();
  902. }
  903. }
  904. }