BatchAreaController.cs 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310
  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 TEAMModelBI.Filter;
  20. using TEAMModelBI.Tool.Extension;
  21. using TEAMModelBI.Tool;
  22. using Azure.Storage.Blobs.Models;
  23. using Azure;
  24. using Azure.Storage.Blobs.Specialized;
  25. using System.Net.Http;
  26. using System.Net.Http.Json;
  27. using System.Net;
  28. using TEAMModelOS.SDK;
  29. using TEAMModelOS.SDK.Context.BI;
  30. using TEAMModelOS.SDK.DI.CoreAPI;
  31. using System.Text;
  32. using DocumentFormat.OpenXml.Bibliography;
  33. using Microsoft.Extensions.Hosting;
  34. using Microsoft.AspNetCore.Hosting;
  35. using static TEAMModelBI.Controllers.RepairApi.InitialAreaController;
  36. using TEAMModelOS.SDK.Context.Constant;
  37. namespace TEAMModelBI.Controllers.BINormal
  38. {
  39. [Route("batcharea")]
  40. [ApiController]
  41. public class BatchAreaController : ControllerBase
  42. {
  43. private readonly AzureCosmosFactory _azureCosmos;
  44. private readonly DingDing _dingDing;
  45. private readonly Option _option;
  46. private readonly AzureStorageFactory _azureStorage;
  47. private readonly IConfiguration _configuration;
  48. private readonly NotificationService _notificationService;
  49. private readonly AzureServiceBusFactory _serviceBus;
  50. private readonly IHttpClientFactory _http;
  51. private readonly CoreAPIHttpService _coreAPIHttpService;
  52. private readonly IWebHostEnvironment _environment; //读取文件
  53. public BatchAreaController(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage, IOptionsSnapshot<Option> option, IConfiguration configuration, NotificationService notificationService, AzureServiceBusFactory serviceBus, IHttpClientFactory http, CoreAPIHttpService coreAPIHttpService, IWebHostEnvironment hostingEnvironment)
  54. {
  55. _azureCosmos = azureCosmos;
  56. _dingDing = dingDing;
  57. _azureStorage = azureStorage;
  58. _option = option?.Value;
  59. _configuration = configuration;
  60. _notificationService = notificationService;
  61. _serviceBus = serviceBus;
  62. _http = http;
  63. _coreAPIHttpService = coreAPIHttpService;
  64. _environment = hostingEnvironment;
  65. }
  66. /// <summary>
  67. /// 查询所有的区域标准 //已对接
  68. /// </summary>
  69. /// <returns></returns>
  70. [ProducesDefaultResponseType]
  71. [HttpPost("get-areas")]
  72. public async Task<IActionResult> GetArea(JsonElement jsonElement)
  73. {
  74. try
  75. {
  76. jsonElement.TryGetProperty("id", out JsonElement id);
  77. jsonElement.TryGetProperty("name", out JsonElement name);
  78. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  79. int? pageSize = 100; //默认不指定返回大小
  80. string continuationToken = string.Empty; //返给前端分页token
  81. string pageToken = default;//接受前端的分页Tolen
  82. bool iscontinuation = false;//是否需要进行分页查询,默认不分页
  83. if (jsonElement.TryGetProperty("pageSize", out JsonElement jsonPageSize))
  84. {
  85. if (!jsonPageSize.ValueKind.Equals(JsonValueKind.Undefined) && !jsonPageSize.ValueKind.Equals(JsonValueKind.Null) && jsonPageSize.TryGetInt32(out int tempPageSize))
  86. {
  87. pageSize = tempPageSize;
  88. }
  89. }
  90. if (pageSize != null && pageSize.Value > 0)
  91. {
  92. iscontinuation = true;
  93. }
  94. if (jsonElement.TryGetProperty("contToken", out JsonElement ContToken))
  95. {
  96. pageToken = ContToken.GetString();
  97. }
  98. List<RecArea> areas = new();
  99. var table = _azureStorage.GetCloudTableClient().GetTableReference("IESLogin");
  100. var cosmosClient = _azureCosmos.GetCosmosClient();
  101. ////分开部署,就不需要,一站多用时,取消注释
  102. //if ($"{site}".Equals(BIConst.Global))
  103. //{
  104. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  105. // table = _azureStorage.GetCloudTableClient(BIConst.Global).GetTableReference("IESLogin");
  106. //}
  107. StringBuilder areaSql = new($"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");
  108. if (!string.IsNullOrEmpty($"{id}") && string.IsNullOrEmpty($"{name}"))
  109. areaSql.Append($" where c.id='{id}'");
  110. if (string.IsNullOrEmpty($"{id}") && !string.IsNullOrEmpty($"{name}"))
  111. areaSql.Append($" where Contains(c.name,'{name}')");
  112. await foreach (var itemArea in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryStreamIterator(queryText: areaSql.ToString(), continuationToken: pageToken, requestOptions: new QueryRequestOptions() { MaxItemCount = pageSize, PartitionKey = new PartitionKey("Base-Area") }))
  113. {
  114. using var json = await JsonDocument.ParseAsync(itemArea.ContentStream);
  115. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetInt16() > 0)
  116. {
  117. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  118. {
  119. areas.Add(obj.ToObject<RecArea>());
  120. }
  121. if (iscontinuation)
  122. {
  123. continuationToken = itemArea.GetContinuationToken();
  124. break;
  125. }
  126. }
  127. }
  128. //areas.ForEach(async area =>
  129. //{
  130. // 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");
  131. // area.aquoteRec = await table.QueryWhereString<AreaQuoteRecord>($"PartitionKey eq 'QuoteRecord' and areaId eq '{area.id}'");
  132. // //List<AreaQuoteRecord> aqr = await table.QueryWhereString<AreaQuoteRecord>($"PartitionKey eq 'QuoteRecord' and areaId eq '{area.id}'");
  133. // //aqr.Sort((x, y) => y.RowKey.CompareTo(x.RowKey));
  134. // 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") }))
  135. // {
  136. // if (string.IsNullOrEmpty(item))
  137. // area.cutArea = false;
  138. // else
  139. // area.cutArea = true;
  140. // };
  141. //});
  142. foreach (var area in areas)
  143. {
  144. 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");
  145. area.aquoteRec = await table.QueryWhereString<AreaQuoteRecord>($"PartitionKey eq 'QuoteRecord' and areaId eq '{area.id}'");
  146. //List<AreaQuoteRecord> aqr = await table.QueryWhereString<AreaQuoteRecord>($"PartitionKey eq 'QuoteRecord' and areaId eq '{area.id}'");
  147. //aqr.Sort((x, y) => y.RowKey.CompareTo(x.RowKey));
  148. 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") }))
  149. {
  150. if (string.IsNullOrEmpty(item))
  151. area.cutArea = false;
  152. else
  153. area.cutArea = true;
  154. };
  155. }
  156. return Ok(new { state = 200, areas, continuationToken });
  157. }
  158. catch (Exception ex)
  159. {
  160. await _dingDing.SendBotMsg($"BI,{_option.Location} batcharea/get-areas \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  161. return BadRequest();
  162. }
  163. }
  164. /// <summary>
  165. /// 批量创区 新接口 //已对接
  166. /// </summary>
  167. /// <param name="jsonElement"></param>
  168. /// <returns></returns>
  169. [ProducesDefaultResponseType]
  170. [AuthToken(Roles = "admin,rdc")]
  171. [HttpPost("batch-area")]
  172. public async Task<IActionResult> batchArea(JsonElement jsonElement)
  173. {
  174. try
  175. {
  176. if (!jsonElement.TryGetProperty("name", out JsonElement name)) return BadRequest();
  177. jsonElement.TryGetProperty("provCode", out JsonElement provCode);
  178. jsonElement.TryGetProperty("provName", out JsonElement provName);
  179. jsonElement.TryGetProperty("cityCode", out JsonElement cityCode);
  180. jsonElement.TryGetProperty("cityName", out JsonElement cityName);
  181. jsonElement.TryGetProperty("areaAdmin", out JsonElement areadAdmin);
  182. if (!jsonElement.TryGetProperty("standard", out JsonElement standard)) return BadRequest();
  183. if (!jsonElement.TryGetProperty("standardName", out JsonElement standardName)) return BadRequest();
  184. jsonElement.TryGetProperty("institution", out JsonElement institution);
  185. jsonElement.TryGetProperty("oldId", out JsonElement _oldId);
  186. jsonElement.TryGetProperty("oldStandard", out JsonElement oldStandard);
  187. jsonElement.TryGetProperty("oldName", out JsonElement oldName);
  188. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  189. var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  190. //操作记录实体
  191. var tempStandard = !string.IsNullOrEmpty($"{oldStandard}") && !string.IsNullOrEmpty($"{_oldId}") ? $"{oldStandard}" : "standard2";
  192. var cosmosClient = _azureCosmos.GetCosmosClient();//数据库连接
  193. var tableClient = _azureStorage.GetCloudTableClient();
  194. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  195. ServiceBusClient serBusClient;
  196. try
  197. {
  198. serBusClient = _serviceBus.GetServiceBusClient();
  199. }
  200. catch
  201. {
  202. return Ok(new { state = 403, msg = "Functionn未启动,请联系管理员" });
  203. }
  204. var activeTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");//秘钥地址
  205. //分开部署,就不需要,一站多用时,取消注释
  206. //if ($"{site}".Equals(BIConst.Global))
  207. //{
  208. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  209. // tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
  210. // blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", name: BIConst.Global);
  211. //}
  212. var table = tableClient.GetTableReference("IESLogin");
  213. //查询新的是否存在
  214. 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") }))
  215. {
  216. if (item.standard.Equals($"{standard}"))
  217. return Ok(new { state = 1, message = "新创区的standard已存在请检查" });
  218. }
  219. //区级的ID
  220. string areaId = Guid.NewGuid().ToString();
  221. Area addArea = new Area()
  222. {
  223. id = areaId,
  224. code = $"Base-Area",
  225. name = $"{name}",
  226. provCode = $"{provCode}",
  227. provName = $"{provName}",
  228. cityCode = $"{cityCode}",
  229. cityName = $"{cityName}",
  230. standard = $"{standard}",
  231. standardName = $"{standardName}",
  232. institution = $"{institution}"
  233. };
  234. #region 区级管理员
  235. var coreUser = await _coreAPIHttpService.GetUserInfo(new Dictionary<string, string> { { "key", $"{areadAdmin}" } }, _option.Location, _configuration);
  236. if (coreUser == null || coreUser.id == null)
  237. return Ok(new { state = 404, msg = "未找到改账户的管理员" });
  238. //string tmdId = !string.IsNullOrEmpty(tempTmdId) ? tempTmdId : $"{areadAdmin}";
  239. Teacher teacher = null;
  240. try
  241. {
  242. //查询该教师是否存在
  243. teacher = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<Teacher>($"{coreUser.id}", new PartitionKey("Base"));
  244. }
  245. catch
  246. {
  247. }
  248. if (teacher != null)
  249. {
  250. //教师存在,在该教师信息中添加要管理的学校信息
  251. teacher.areas.Add(new Teacher.TeacherArea { areaId = addArea.id, status = "join", name = addArea.name });
  252. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync<Teacher>(teacher, teacher.id, new PartitionKey("Base"));
  253. }
  254. else
  255. {
  256. teacher.id = coreUser.id;
  257. teacher.name = coreUser.name;
  258. teacher.picture = coreUser.picture;
  259. teacher.pk = "Base";
  260. teacher.code = "Base";
  261. teacher.size = 1;
  262. teacher.createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  263. //教师存在,在该教师信息中添加要管理的学校信息
  264. teacher.areas.Add(new Teacher.TeacherArea { areaId = addArea.id, status = "join", name = addArea.name });
  265. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").CreateItemAsync<Teacher>(teacher, new PartitionKey("Base"));
  266. }
  267. #endregion
  268. //创建区域
  269. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync<Area>(addArea, new PartitionKey("Base-Area"));
  270. //保存引用记录
  271. await table.SaveOrUpdate<AreaQuoteRecord>(new AreaQuoteRecord() { PartitionKey = "QuoteRecord", RowKey = $"{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}", areaId = $"{areaId}", quoteId = $"{_oldId}", quoteName = $"{oldName}", standard = tempStandard });
  272. //消息分区键
  273. string partitionCode = "copyAbility-mark";
  274. #region 使用Function创区
  275. //string areaCode = "copyArea-mark";
  276. //BatchCopyAreaRelevant bCopyArea = new BatchCopyAreaRelevant()
  277. //{
  278. // tmdid = $"{_tmdId}",
  279. // tmdName = $"{_tmdName}",
  280. // codeKey = areaCode,
  281. // tmdIds = new List<string> { $"{_tmdId}"},
  282. // oldStandard = $"{oldStandard}",
  283. // oldId = $"{_oldId}",
  284. // standard = $"{standard}",
  285. // areaId = areaId,
  286. // cut="",
  287. //};
  288. //var messageBatchCopyArea = new ServiceBusMessage(bCopyArea.ToJsonString());
  289. //messageBatchCopyArea.ApplicationProperties.Add("name", "CopyAreaRelevant");
  290. //var activeTaskArea = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
  291. //await _serviceBus.GetServiceBusClient().SendMessageAsync(activeTaskArea, messageBatchCopyArea);
  292. #endregion
  293. #region 旧的批量创区
  294. List<Task<ItemResponse<Ability>>> abilities = new List<Task<ItemResponse<Ability>>>(); //存储区域数据
  295. List<Task<ItemResponse<AbilityTask>>> abilityTasks = new List<Task<ItemResponse<AbilityTask>>>(); //存储章节
  296. if (!string.IsNullOrEmpty($"{oldStandard}") && !string.IsNullOrEmpty($"{_oldId}"))
  297. {
  298. //查询要复制区域的能力标准点
  299. 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}") }))
  300. {
  301. if (!string.IsNullOrEmpty(item.blob))
  302. {
  303. item.blob = item.blob.Replace($"/{oldStandard}/", $"/{standard}/");
  304. };
  305. item.standard = $"{standard}";
  306. item.code = $"Ability-{standard}";
  307. item.school = $"{standard}";
  308. //添加区能力标准点
  309. abilities.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(item, new PartitionKey($"Ability-{standard}")));
  310. }
  311. try
  312. {
  313. if (abilities.Count < 256)
  314. {
  315. await Task.WhenAll(abilities);
  316. }
  317. else
  318. {
  319. int pages = (abilities.Count + 255) / 256;
  320. for (int i = 0; i < pages; i++)
  321. {
  322. List<Task<ItemResponse<Ability>>> tempAbility = abilities.Skip((i) * 256).Take(256).ToList();
  323. await Task.WhenAll(tempAbility);
  324. }
  325. }
  326. }
  327. catch
  328. {
  329. return Ok(new { state = 200, msg = "创区成功,能力标准点复制失败,遗留数据影响!" });
  330. }
  331. try
  332. {
  333. //微能力点
  334. 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}") }))
  335. {
  336. List<Tnode> tnodes = new List<Tnode>();
  337. foreach (Tnode tnode in atask.children)
  338. {
  339. if (tnode.rnodes != null)
  340. {
  341. List<Rnode> rnodes = new List<Rnode>();
  342. foreach (Rnode rnode in tnode.rnodes)
  343. {
  344. if (!string.IsNullOrEmpty($"{rnode.link}"))
  345. {
  346. rnode.link = rnode.link.Replace($"/{oldStandard}/", $"/{standard}/");
  347. }
  348. rnodes.Add(rnode);
  349. }
  350. tnode.rnodes = rnodes;
  351. }
  352. tnodes.Add(tnode);
  353. }
  354. atask.children = tnodes;
  355. atask.code = $"AbilityTask-{standard}";
  356. atask.standard = $"{standard}";
  357. atask.codeval = $"{standard}";
  358. //添加区能力标准点中的节点
  359. //abilityTasks.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(atask, new PartitionKey($"AbilityTask-{standard}")));
  360. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(atask, new PartitionKey($"AbilityTask-{standard}"));
  361. }
  362. }
  363. catch
  364. {
  365. return Ok(new { state = 200, msg = "创区成功,能力标准创建成功,微能力点复制失败,遗留数据影响!" });
  366. }
  367. //if (abilityTasks.Count > 0)
  368. //{
  369. // for (int i = 0; i < abilityTasks.Count; i++)
  370. // {
  371. // List<Task<ItemResponse<AbilityTask>>> tempAbilityTasks = abilityTasks.Skip((i)).Take(1).ToList();
  372. // await Task.WhenAll(tempAbilityTasks);
  373. // }
  374. //}
  375. //新政策文件
  376. 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") }))
  377. {
  378. if (standardFile != null)
  379. {
  380. standardFile.standard = $"{standard}";
  381. standardFile.id = areaId;
  382. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(standardFile, new PartitionKey($"StandardFile"));
  383. }
  384. }
  385. //新的区域设置
  386. 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") }))
  387. {
  388. if (areaSetting != null)
  389. {
  390. areaSetting.accessConfig = null;
  391. areaSetting.id = areaId;
  392. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(areaSetting, new PartitionKey($"AreaSetting"));
  393. }
  394. }
  395. }
  396. else
  397. {
  398. Area area = null;
  399. 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") }))
  400. {
  401. area = tempArea;
  402. }
  403. if (area != null)
  404. {
  405. //查询要复制区域的能力标准点
  406. 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}") }))
  407. {
  408. if (!string.IsNullOrEmpty(item.blob))
  409. {
  410. item.blob = item.blob.Replace($"/{area.standard}/", $"/{standard}/");
  411. };
  412. item.standard = $"{standard}";
  413. item.code = $"Ability-{standard}";
  414. item.school = $"{standard}";
  415. //添加区能力标准点
  416. abilities.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(item, new PartitionKey($"Ability-{standard}")));
  417. }
  418. try
  419. {
  420. if (abilities.Count < 256)
  421. {
  422. await Task.WhenAll(abilities);
  423. }
  424. else
  425. {
  426. int pages = (abilities.Count + 255) / 256;
  427. for (int i = 0; i < pages; i++)
  428. {
  429. List<Task<ItemResponse<Ability>>> tempAbility = abilities.Skip((i) * 256).Take(256).ToList();
  430. await Task.WhenAll(tempAbility);
  431. }
  432. }
  433. }
  434. catch
  435. {
  436. return Ok(new { state = 200, msg = "创区成功,能力标准点复制失败,遗留数据影响!" });
  437. }
  438. try
  439. {
  440. //微能力点
  441. 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}") }))
  442. {
  443. List<Tnode> tnodes = new List<Tnode>();
  444. foreach (Tnode tnode in atask.children)
  445. {
  446. if (tnode.rnodes != null)
  447. {
  448. List<Rnode> rnodes = new List<Rnode>();
  449. foreach (Rnode rnode in tnode.rnodes)
  450. {
  451. if (!string.IsNullOrEmpty($"{rnode.link}"))
  452. {
  453. rnode.link = rnode.link.Replace($"/{area.standard}/", $"/{standard}/");
  454. }
  455. rnodes.Add(rnode);
  456. }
  457. tnode.rnodes = rnodes;
  458. }
  459. tnodes.Add(tnode);
  460. }
  461. atask.children = tnodes;
  462. atask.code = $"AbilityTask-{standard}";
  463. atask.standard = $"{standard}";
  464. atask.codeval = $"{standard}";
  465. //添加区能力标准点中的节点
  466. //abilityTasks.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(atask, new PartitionKey($"AbilityTask-{standard}")));
  467. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(atask, new PartitionKey($"AbilityTask-{standard}"));
  468. }
  469. }
  470. catch
  471. {
  472. return Ok(new { state = 200, msg = "创区成功,能力标准创建成功,微能力点复制失败,遗留数据影响!" });
  473. }
  474. //if (abilityTasks.Count > 0)
  475. //{
  476. // for (int i = 0; i < abilityTasks.Count; i++)
  477. // {
  478. // List<Task<ItemResponse<AbilityTask>>> tempAbilityTasks = abilityTasks.Skip(i).Take(1).ToList();
  479. // await Task.WhenAll(tempAbilityTasks);
  480. // }
  481. //}
  482. //新政策文件
  483. 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") }))
  484. {
  485. if (standardFile != null)
  486. {
  487. standardFile.standard = $"{standard}";
  488. standardFile.id = areaId;
  489. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(standardFile, new PartitionKey($"StandardFile"));
  490. }
  491. }
  492. //新的区域设置
  493. 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") }))
  494. {
  495. if (areaSetting != null)
  496. {
  497. areaSetting.accessConfig = null;
  498. areaSetting.id = areaId;
  499. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(areaSetting, new PartitionKey($"AreaSetting"));
  500. }
  501. }
  502. }
  503. else return Ok(new { state = 201, message = "未找到默认能力点!" });
  504. }
  505. #endregion
  506. //执行复制操作
  507. BatchCopyFile batchCopyFile = new BatchCopyFile();
  508. batchCopyFile.blobCntr = "teammodelos";
  509. batchCopyFile.oldFileName = $"{oldStandard}";
  510. batchCopyFile.newFileName = $"{standard}";
  511. batchCopyFile.tmdid = $"{_tmdId}";
  512. batchCopyFile.tmdIds = new List<string> { $"{ _tmdId}" };
  513. batchCopyFile.codeKey = partitionCode;
  514. batchCopyFile.tmdName = $"{_tmdName}";
  515. var messageBatchCopyFile = new ServiceBusMessage(batchCopyFile.ToJsonString());
  516. messageBatchCopyFile.ApplicationProperties.Add("name", "CopyStandardFile");
  517. //var activeTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
  518. try
  519. {
  520. //await _serviceBus.GetServiceBusClient().SendMessageAsync(activeTask, messageBatchCopyFile);
  521. await serBusClient.SendMessageAsync(activeTask, messageBatchCopyFile);
  522. }
  523. catch (Exception)
  524. {
  525. return Ok(new { state = 201, msg = "能力点复制成功,复制能力点的文件失败," });
  526. }
  527. //发送消息实体
  528. Notification notification = new Notification
  529. {
  530. hubName = "hita",
  531. type = "msg",
  532. from = $"BI:{_option.Location}:private",
  533. to = new List<string> { $"{ _tmdId}" },
  534. label = $"{partitionCode}_start",
  535. body = new { location = _option.Location, biz = partitionCode, tmdid = $"{_tmdId}", tmdname = $"{_tmdName}", status = 1, time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() }.ToJsonString(),
  536. expires = DateTimeOffset.UtcNow.AddDays(7).ToUnixTimeSeconds()
  537. };
  538. var notiUrl = _configuration.GetValue<string>("HaBookAuth:CoreService:sendnotification");
  539. var clientID = _configuration.GetValue<string>("HaBookAuth:CoreService:clientID");
  540. var clientSecret = _configuration.GetValue<string>("HaBookAuth:CoreService:clientSecret");
  541. var location = _option.Location;
  542. await _notificationService.SendNotification(clientID, clientSecret, location, notiUrl, notification); //站内发送消息
  543. //保存操作记录
  544. //await _azureStorage.SaveBILog("area-add", $"{_tmdName}【{_tmdId}】已操作创区功能模块:{name},当前标准【{standard}】,复制的微能力点:{tempStandard}", _dingDing, httpContext: HttpContext);
  545. await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "area-add", $"{_tmdName}【{_tmdId}】已操作创区功能模块:{name},当前标准【{standard}】,复制的微能力点:{tempStandard}", _dingDing, httpContext: HttpContext);
  546. return Ok(new { state = 200, area = addArea });
  547. }
  548. catch (Exception ex)
  549. {
  550. await _dingDing.SendBotMsg($"BI,{_option.Location} /batcharea/batch-area \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  551. return BadRequest();
  552. }
  553. }
  554. /// <summary>
  555. /// 创区后切换能力点, 先删除原来的能力点,后复制新的能力点 //已对接
  556. /// </summary>
  557. /// <param name="jsonElement"></param>
  558. /// <returns></returns>
  559. [ProducesDefaultResponseType]
  560. [AuthToken(Roles = "admin,rdc")]
  561. [HttpPost("cut-standard")]
  562. public async Task<IActionResult> CutStandard(JsonElement jsonElement)
  563. {
  564. try
  565. {
  566. if (!jsonElement.TryGetProperty("oldId", out JsonElement _oldId)) return BadRequest();
  567. if (!jsonElement.TryGetProperty("oldStandard", out JsonElement _oldStandard)) return BadRequest();
  568. if (!jsonElement.TryGetProperty("newId", out JsonElement _newId)) return BadRequest();
  569. if (!jsonElement.TryGetProperty("newStandard", out JsonElement _newStandard)) return BadRequest();
  570. jsonElement.TryGetProperty("newName", out JsonElement newName);
  571. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  572. var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  573. //操作记录实体
  574. string blobOrTable = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString();
  575. var tableClient = _azureStorage.GetCloudTableClient();
  576. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  577. var cosmosClient = _azureCosmos.GetCosmosClient();
  578. var serBusClient = _serviceBus.GetServiceBusClient();
  579. var activeTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");//秘钥地址
  580. ////分开部署,就不需要,一站多用时,取消注释
  581. //if ($"{site}".Equals(BIConst.Global))
  582. //{
  583. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  584. // tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
  585. // blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
  586. // serBusClient = _serviceBus.GetServiceBusClient(BIConst.Global); //暂未确定使用默认
  587. // //activeTask = _configuration.GetValue<string>("GlobalAzure:ServiceBus:ActiveTask"); //暂未确定使用默认
  588. //}
  589. var table = tableClient.GetTableReference("IESLogin");
  590. var responseSet = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemStreamAsync($"{_oldId}", new PartitionKey("AreaSetting"));
  591. if (responseSet.Status == 200)
  592. {
  593. using var fileJson = await JsonDocument.ParseAsync(responseSet.ContentStream);
  594. AreaSetting delSet = fileJson.ToObject<AreaSetting>();
  595. if (!string.IsNullOrEmpty(delSet.accessConfig))
  596. return Ok(new { state = 401, msg = "区域已经规定了,不能切换能能力" });
  597. }
  598. //保存引用记录
  599. //await table.SaveOrUpdate<AreaQuoteRecord>(new AreaQuoteRecord() { PartitionKey = "QuoteRecord", RowKey = $"{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}", areaId = $"{_oldId}", quoteId = $"{_newId}", quoteName = $"{newName}", standard = $"{_newStandard}" });
  600. List<string> abilityIds = new List<string>(); //册别的ID集合
  601. //查询册别信息
  602. 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}") }))
  603. {
  604. abilityIds.Add(tempAbility.id); //查询出来册别ID添加册别ID集合
  605. }
  606. //删除册别
  607. if (abilityIds.IsNotEmpty())
  608. {
  609. var sresponse = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").DeleteItemsStreamAsync(abilityIds, $"Ability-{_oldStandard}");
  610. }
  611. List<string> abilityTaskIds = new List<string>(); //章节ID集合
  612. 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}") }))
  613. {
  614. abilityTaskIds.Add(item.id); //查询出来的章节信息ID添加到战绩集合
  615. }
  616. //删除章节
  617. if (abilityTaskIds.IsNotEmpty())
  618. {
  619. var sresponse = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").DeleteItemsStreamAsync(abilityTaskIds, $"AbilityTask-{_oldStandard}");
  620. }
  621. List<Task<ItemResponse<Ability>>> abilities = new List<Task<ItemResponse<Ability>>>(); //存储册别数据
  622. List<Task<ItemResponse<AbilityTask>>> abilityTasks = new List<Task<ItemResponse<AbilityTask>>>(); //存储章节
  623. //查询要复制区域的能力标准点
  624. 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}") }))
  625. {
  626. if (!string.IsNullOrEmpty(item.blob))
  627. {
  628. item.blob = item.blob.Replace($"/{_newStandard}/", $"/{_oldStandard}/");
  629. };
  630. item.standard = $"{_oldStandard}";
  631. item.code = $"Ability-{_oldStandard}";
  632. item.school = $"{_oldStandard}";
  633. //添加区能力标准点
  634. abilities.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(item, new PartitionKey($"Ability-{_oldStandard}")));
  635. }
  636. try
  637. {
  638. if (abilities.Count < 256)
  639. {
  640. await Task.WhenAll(abilities);
  641. }
  642. else
  643. {
  644. int pages = (abilities.Count + 255) / 256;
  645. for (int i = 0; i < pages; i++)
  646. {
  647. List<Task<ItemResponse<Ability>>> tempAbility = abilities.Skip((i) * 256).Take(256).ToList();
  648. await Task.WhenAll(tempAbility);
  649. }
  650. }
  651. }
  652. catch
  653. {
  654. return Ok(new { state = 200, msg = "创区成功,能力标准点复制失败,遗留数据影响!" });
  655. }
  656. try
  657. {
  658. //微能力点
  659. 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}") }))
  660. {
  661. List<Tnode> tnodes = new();
  662. foreach (Tnode tnode in atask.children)
  663. {
  664. if (tnode.rnodes != null)
  665. {
  666. List<Rnode> rnodes = new();
  667. foreach (Rnode rnode in tnode.rnodes)
  668. {
  669. if (!string.IsNullOrEmpty($"{rnode.link}"))
  670. {
  671. rnode.link = rnode.link.Replace($"/{_newStandard}/", $"/{_oldStandard}/");
  672. }
  673. rnodes.Add(rnode);
  674. }
  675. tnode.rnodes = rnodes;
  676. }
  677. tnodes.Add(tnode);
  678. }
  679. atask.children = tnodes;
  680. atask.code = $"AbilityTask-{_oldStandard}";
  681. atask.standard = $"{_oldStandard}";
  682. atask.codeval = $"{_oldStandard}";
  683. ////添加区能力标准点中的节点
  684. //abilityTasks.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(atask, new PartitionKey($"AbilityTask-{_oldStandard}")));
  685. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(atask, new PartitionKey($"AbilityTask-{_oldStandard}"));
  686. }
  687. }
  688. catch
  689. {
  690. return Ok(new { state = 200, msg = "创区成功,能力标准创建成功,微能力点复制失败,遗留数据影响!" });
  691. }
  692. StandardFile saveFile = new();
  693. //新政策文件
  694. 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") }))
  695. {
  696. if (standardFile != null)
  697. {
  698. standardFile.standard = $"{_oldStandard}";
  699. standardFile.id = $"{_oldId}";
  700. saveFile = standardFile;
  701. }
  702. }
  703. StandardFile tempFile = new();
  704. if (saveFile.id != null)
  705. {
  706. var respFile = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemStreamAsync(saveFile.id, new PartitionKey("StandardFile"));
  707. if (respFile.Status == 200)
  708. {
  709. using var json = await JsonDocument.ParseAsync(respFile.ContentStream);
  710. tempFile = json.ToObject<StandardFile>();
  711. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").DeleteItemAsync<StandardFile>(tempFile.id, new PartitionKey("StandardFile"));
  712. }
  713. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(saveFile, new PartitionKey($"StandardFile")); // 需要删除原来的政策文件数据在进行添加
  714. }
  715. //新的区域设置
  716. AreaSetting saveSetting = new AreaSetting();
  717. 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") }))
  718. {
  719. if (areaSetting != null)
  720. {
  721. areaSetting.accessConfig = null;
  722. areaSetting.id = $"{_oldId}";
  723. saveSetting = areaSetting;
  724. }
  725. }
  726. AreaSetting tempSetting = new();
  727. if (saveSetting.id != null)
  728. {
  729. var respSetting = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemStreamAsync(saveSetting.id, new PartitionKey("AreaSetting"));
  730. if (respSetting.Status == 200)
  731. {
  732. using var json = await JsonDocument.ParseAsync(respSetting.ContentStream);
  733. tempSetting = json.ToObject<AreaSetting>();
  734. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").DeleteItemAsync<AreaSetting>(tempFile.id, new PartitionKey("AreaSetting"));
  735. }
  736. tempSetting = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(saveSetting, new PartitionKey($"AreaSetting")); //需要删除原来的区域设置数据在进行添加
  737. }
  738. //发送消息分区键
  739. string partitionCode = "DelBeforeCopyAbility-mark";
  740. //v2通知
  741. Teacher targetTeacher = await cosmosClient.GetContainer(Constant.TEAMModelOS, Constant.Teacher).ReadItemAsync<Teacher>($"{_tmdId}", new PartitionKey($"Base"));
  742. _coreAPIHttpService.PushNotify(new List<IdNameCode> { new IdNameCode { id = targetTeacher.id, name = targetTeacher.name, code = targetTeacher.lang } }, "copy-file_area", Constant.NotifyType_IES5_Management, new Dictionary<string, object> { { "tmdname", $"{_tmdName}" } }, _option.Location, _configuration, _dingDing, _environment.ContentRootPath);
  743. //执行复制操作
  744. BatchCopyFile batchCopyFile = new();
  745. batchCopyFile.blobCntr = "teammodelos";
  746. batchCopyFile.oldFileName = $"{_newStandard}";
  747. batchCopyFile.newFileName = $"{_oldStandard}";
  748. batchCopyFile.tmdid = $"{_tmdId}";
  749. batchCopyFile.tmdIds = new List<string> { $"{ _tmdId}" };
  750. batchCopyFile.codeKey = partitionCode;
  751. batchCopyFile.tmdName = $"{_tmdName}";
  752. var messageBatchCopyFile = new ServiceBusMessage(batchCopyFile.ToJsonString());
  753. messageBatchCopyFile.ApplicationProperties.Add("name", "CopyStandardFile");
  754. try
  755. {
  756. //await _serviceBus.GetServiceBusClient().SendMessageAsync(activeTask, messageBatchCopyFile); //先执行删除操作,在执行复制 单一
  757. await serBusClient.SendMessageAsync(activeTask, messageBatchCopyFile); //先执行删除操作,在执行复制
  758. }
  759. catch (Exception)
  760. {
  761. return Ok(new { state = 201 ,msg = "能力点复制成功,复制能力点的文件失败," });
  762. }
  763. //发送消息实体
  764. Notification notification = new()
  765. {
  766. hubName = "hita",
  767. type = "msg",
  768. from = $"BI:{_option.Location}:private",
  769. to = new List<string> { $"{ _tmdId}" },
  770. label = $"{partitionCode}_start",
  771. body = new { location = _option.Location, biz = partitionCode, tmdid = $"{_tmdId}", tmdname = $"{_tmdName}", status = 1, time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() }.ToJsonString(),
  772. expires = DateTimeOffset.UtcNow.AddDays(7).ToUnixTimeSeconds()
  773. };
  774. var url = _configuration.GetValue<string>("HaBookAuth:CoreService:sendnotification");
  775. var clientID = _configuration.GetValue<string>("HaBookAuth:CoreService:clientID");
  776. var clientSecret = _configuration.GetValue<string>("HaBookAuth:CoreService:clientSecret");
  777. var location = _option.Location;
  778. await _notificationService.SendNotification(clientID, clientSecret, location, url, notification); //发送站内发送消息
  779. //保存操作记录
  780. await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "area-cut", $"{_tmdName}【{_tmdId}】已操作【{_oldStandard}】切换至{_newStandard}微能力点,复制标准:{_newStandard}", _dingDing, httpContext: HttpContext);
  781. return Ok(new { state = 200 });
  782. }
  783. catch (Exception ex)
  784. {
  785. await _dingDing.SendBotMsg($"BI,{_option.Location} batcharea/cut-standard \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  786. return BadRequest();
  787. }
  788. }
  789. /// <summary>
  790. /// 删除区域和区域的册别、能力点、区域文件、区域设置
  791. /// </summary>
  792. /// <param name="jsonElement"></param>
  793. /// <returns></returns>
  794. [ProducesDefaultResponseType]
  795. [HttpPost("del-area")]
  796. public async Task<IActionResult> DelArea(JsonElement jsonElement)
  797. {
  798. if (!jsonElement.TryGetProperty("areaId", out JsonElement areaId)) return BadRequest();
  799. if (!jsonElement.TryGetProperty("standard", out JsonElement standard)) return BadRequest();
  800. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  801. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "teammodelos");
  802. var cosmosClient = _azureCosmos.GetCosmosClient();
  803. ////分开部署,就不需要,一站多用时,取消注释
  804. //if ($"{site}".Equals(BIConst.Global))
  805. //{
  806. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  807. // blobClient = _azureStorage.GetBlobContainerClient(containerName: "teammodelos", BIConst.Global);
  808. //}
  809. var response = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemStreamAsync($"{areaId}", new PartitionKey("Base-Area"));
  810. if (response.Status == 200)
  811. {
  812. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  813. Area area = json.ToObject<Area>();
  814. if (area.standard == $"{standard}")
  815. {
  816. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").DeleteItemAsync<Area>(area.id, new PartitionKey("Base-Area")); //删除区
  817. List<Teacher> teachers = new();
  818. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<Teacher>(queryText: $"SELECT distinct value(c) FROM c join a in c.areas join s in c.schools where a.areaId='{area.id}' or s.areaId='{area.id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  819. {
  820. teachers.Add(item);
  821. }
  822. foreach (var item in teachers)
  823. {
  824. var tempArea = item.areas.Find(f => f.areaId.Equals(area.id));
  825. if (tempArea != null)
  826. {
  827. item.areas.Remove(tempArea);
  828. }
  829. item.schools.ForEach(fe => { if (fe.areaId.Equals(area.id)) fe.areaId = ""; });
  830. await cosmosClient.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<Teacher>(item, item.id, new PartitionKey("Base"));
  831. }
  832. List<string> scIds = await CommonFind.FindScIds(cosmosClient, $"select c.id from c where c.areaId='{area.id}' and c.standard ='{area.standard}'", "Base");
  833. foreach (var item in scIds)
  834. {
  835. School school = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(item, new PartitionKey("Base"));
  836. school.areaId = "";
  837. school.standard = "";
  838. school = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<School>(school, school.id, new PartitionKey("Base"));
  839. }
  840. List<string> abilityIds = new(); //册别的ID集合
  841. List<string> abilityTaskIds = new(); //章节ID集合
  842. //查询册别信息
  843. 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}") }))
  844. {
  845. abilityIds.Add(tempAbility.id); //查询出来册别ID添加册别ID集合
  846. }
  847. //删除册别
  848. if (abilityIds.IsNotEmpty())
  849. {
  850. var sresponse = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").DeleteItemsStreamAsync(abilityIds, $"Ability-{area.standard}");
  851. }
  852. 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}") }))
  853. {
  854. abilityTaskIds.Add(item.id); //查询出来的章节信息ID添加到战绩集合
  855. }
  856. //删除章节
  857. if (abilityTaskIds.IsNotEmpty())
  858. {
  859. var sresponse = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").DeleteItemsStreamAsync(abilityTaskIds, $"AbilityTask-{standard}");
  860. }
  861. //区域政策文件
  862. StandardFile delFile = new();
  863. var responseFile = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemStreamAsync(area.id, new PartitionKey("StandardFile"));
  864. if(responseFile.Status == 200)
  865. {
  866. using var fileJson = await JsonDocument.ParseAsync(responseFile.ContentStream);
  867. delFile = fileJson.ToObject<StandardFile>();
  868. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").DeleteItemAsync<StandardFile>(delFile.id, new PartitionKey("StandardFile"));
  869. }
  870. //区域设置
  871. AreaSetting delSet = new();
  872. var responseSet = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemStreamAsync(area.id, new PartitionKey("AreaSetting"));
  873. if (responseSet.Status == 200)
  874. {
  875. using var fileJson = await JsonDocument.ParseAsync(responseSet.ContentStream);
  876. delSet = fileJson.ToObject<AreaSetting>();
  877. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").DeleteItemAsync<AreaSetting>(delSet.id, new PartitionKey("AreaSetting"));
  878. }
  879. List<object> fileobj = new();
  880. //先删除原有的文件
  881. List<Task<Response<bool>>> DelList = new();
  882. await foreach (BlobItem blobItem in blobClient.GetBlobsAsync(BlobTraits.None, BlobStates.None, $"yxpt/{area.standard}/"))
  883. {
  884. fileobj.Add(blobItem.Name);
  885. DelList.Add(blobClient.GetBlobBaseClient(blobItem.Name).DeleteIfExistsAsync());
  886. }
  887. if (DelList.Count <= 256)
  888. {
  889. await Task.WhenAll(DelList);
  890. }
  891. else
  892. {
  893. int pages = (DelList.Count + 255) / 256;
  894. for (int i = 0; i < pages; i++)
  895. {
  896. List<Task<Response<bool>>> delList = DelList.Skip((i) * 256).Take(256).ToList();
  897. await Task.WhenAll(delList);
  898. }
  899. }
  900. return Ok(new { state = 200,area, scIds, abilityIds, abilityTaskIds, delFile, delSet, fileobj });
  901. }
  902. else return Ok(new { state = 400, msg = "id和standard不匹配!" });
  903. }
  904. else return Ok(new { state = 404,msg="依据Id未找到该区!" });
  905. }
  906. /// <summary>
  907. /// 同步两个区到新的区中
  908. /// </summary>
  909. /// <param name="jsonElement"></param>
  910. /// <returns></returns>
  911. [ProducesDefaultResponseType]
  912. [AuthToken(Roles = "admin")]
  913. [HttpPost("cut-full-statndard")]
  914. public async Task<IActionResult> CutFullStandard(JsonElement jsonElement)
  915. {
  916. string _oldId = "bde5c011-2ae4-461a-b46c-5483ba72ae45";
  917. string _oldStandard = "standard27";
  918. string standardFileId = "02944f32-f534-3397-ea56-e6f1fc6c3714";
  919. string standard = "standard2";
  920. List<CopyStandard> copyStand = new()
  921. {
  922. new CopyStandard() { id = standardFileId, standard = standard },
  923. new CopyStandard() { id = "99a4a33b-e21b-44ac-80a1-b31dc40496e0", standard = "standard3" }
  924. };
  925. var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  926. var tableClient = _azureStorage.GetCloudTableClient();
  927. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  928. var cosmosClient = _azureCosmos.GetCosmosClient();
  929. var serBusClient = _serviceBus.GetServiceBusClient();
  930. var activeTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");//秘钥地址
  931. var table = tableClient.GetTableReference("IESLogin");
  932. var responseSet = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemStreamAsync($"{_oldId}", new PartitionKey("AreaSetting"));
  933. if (responseSet.Status == 200)
  934. {
  935. using var fileJson = await JsonDocument.ParseAsync(responseSet.ContentStream);
  936. AreaSetting delSet = fileJson.ToObject<AreaSetting>();
  937. if (!string.IsNullOrEmpty(delSet.accessConfig))
  938. return Ok(new { state = 401, msg = "区域已经规定了,不能切换能能力" });
  939. }
  940. List<string> abilityIds = new List<string>(); //册别的ID集合
  941. //查询册别信息
  942. 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}") }))
  943. {
  944. abilityIds.Add(tempAbility.id); //查询出来册别ID添加册别ID集合
  945. }
  946. //删除册别
  947. if (abilityIds.IsNotEmpty())
  948. {
  949. var sresponse = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").DeleteItemsStreamAsync(abilityIds, $"Ability-{_oldStandard}");
  950. }
  951. List<string> abilityTaskIds = new List<string>(); //章节ID集合
  952. 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}") }))
  953. {
  954. abilityTaskIds.Add(item.id); //查询出来的章节信息ID添加到战绩集合
  955. }
  956. //删除章节
  957. if (abilityTaskIds.IsNotEmpty())
  958. {
  959. var sresponse = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").DeleteItemsStreamAsync(abilityTaskIds, $"AbilityTask-{_oldStandard}");
  960. }
  961. List<Task<ItemResponse<Ability>>> abilities = new(); //存储册别数据
  962. List<Task<ItemResponse<AbilityTask>>> abilityTasks = new(); //存储章节
  963. List<string> repeatAbilityId = new();
  964. List<string> repeatAbilityTaskId = new();
  965. foreach (var newstand in copyStand)
  966. {
  967. try
  968. {
  969. //查询要复制区域的能力标准点
  970. 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-{newstand.standard}") }))
  971. {
  972. if (!string.IsNullOrEmpty(item.blob))
  973. item.blob = item.blob.Replace($"/{newstand.standard}/", $"/{_oldStandard}/");
  974. item.standard = $"{_oldStandard}";
  975. item.code = $"Ability-{_oldStandard}";
  976. item.school = $"{_oldStandard}";
  977. //添加区能力标准点
  978. //abilities.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(item, new PartitionKey($"Ability-{_oldStandard}")));
  979. //await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(item, new PartitionKey($"Ability-{_oldStandard}"));
  980. var respond = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemStreamAsync(item.id, new PartitionKey(item.code));
  981. if (respond.Status != 200)
  982. abilities.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(item, new PartitionKey($"Ability-{_oldStandard}")));
  983. else
  984. repeatAbilityId.Add(item.id);
  985. }
  986. }
  987. catch
  988. {
  989. return Ok(new { state = 200, msg = "创区成功,能力标准点复制失败,遗留数据影响!" });
  990. }
  991. try
  992. {
  993. if (abilities.Count < 256)
  994. {
  995. await Task.WhenAll(abilities);
  996. }
  997. else
  998. {
  999. int pages = (abilities.Count + 255) / 256;
  1000. for (int i = 0; i < pages; i++)
  1001. {
  1002. List<Task<ItemResponse<Ability>>> tempAbility = abilities.Skip((i) * 256).Take(256).ToList();
  1003. await Task.WhenAll(tempAbility);
  1004. }
  1005. }
  1006. }
  1007. catch
  1008. {
  1009. return Ok(new { state = 200, msg = "创区成功,能力标准点复制失败,遗留数据影响!" });
  1010. }
  1011. try
  1012. {
  1013. //微能力点
  1014. 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-{newstand.standard}") }))
  1015. {
  1016. List<Tnode> tnodes = new();
  1017. foreach (Tnode tnode in atask.children)
  1018. {
  1019. if (tnode.rnodes != null)
  1020. {
  1021. List<Rnode> rnodes = new();
  1022. foreach (Rnode rnode in tnode.rnodes)
  1023. {
  1024. if (!string.IsNullOrEmpty($"{rnode.link}"))
  1025. {
  1026. rnode.link = rnode.link.Replace($"/{newstand.standard}/", $"/{_oldStandard}/");
  1027. }
  1028. rnodes.Add(rnode);
  1029. }
  1030. tnode.rnodes = rnodes;
  1031. }
  1032. tnodes.Add(tnode);
  1033. }
  1034. atask.children = tnodes;
  1035. atask.code = $"AbilityTask-{_oldStandard}";
  1036. atask.standard = $"{_oldStandard}";
  1037. atask.codeval = $"{_oldStandard}";
  1038. var respond = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemStreamAsync(atask.id, new PartitionKey(atask.code));
  1039. if (respond.Status != 200)
  1040. ////添加区能力标准点中的节点
  1041. //abilityTasks.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(atask, new PartitionKey($"AbilityTask-{_oldStandard}")));
  1042. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(atask, new PartitionKey($"AbilityTask-{_oldStandard}"));
  1043. else
  1044. repeatAbilityTaskId.Add(atask.id);
  1045. }
  1046. }
  1047. catch
  1048. {
  1049. return Ok(new { state = 200, msg = "创区成功,能力标准创建成功,微能力点复制失败,遗留数据影响!" });
  1050. }
  1051. //发送消息分区键
  1052. string partitionCode = "DelBeforeCopyAbility-mark";
  1053. //执行复制操作
  1054. BatchCopyFile batchCopyFile = new();
  1055. batchCopyFile.blobCntr = "teammodelos";
  1056. batchCopyFile.oldFileName = $"{newstand.standard}";
  1057. batchCopyFile.newFileName = $"{_oldStandard}";
  1058. batchCopyFile.tmdid = $"{_tmdId}";
  1059. batchCopyFile.tmdIds = new List<string> { $"{_tmdId}" };
  1060. batchCopyFile.codeKey = partitionCode;
  1061. batchCopyFile.tmdName = $"{_tmdName}";
  1062. var messageBatchCopyFile = new ServiceBusMessage(batchCopyFile.ToJsonString());
  1063. messageBatchCopyFile.ApplicationProperties.Add("name", "CopyStandardFile");
  1064. try
  1065. {
  1066. //await _serviceBus.GetServiceBusClient().SendMessageAsync(activeTask, messageBatchCopyFile); //先执行删除操作,在执行复制 单一
  1067. await serBusClient.SendMessageAsync(activeTask, messageBatchCopyFile); //先执行删除操作,在执行复制
  1068. }
  1069. catch (Exception)
  1070. {
  1071. return Ok(new { state = 201, msg = "能力点复制成功,复制能力点的文件失败," });
  1072. }
  1073. //发送消息实体
  1074. Notification notification = new()
  1075. {
  1076. hubName = "hita",
  1077. type = "msg",
  1078. from = $"BI:{_option.Location}:private",
  1079. to = new List<string> { $"{_tmdId}" },
  1080. label = $"{partitionCode}_start",
  1081. body = new { location = _option.Location, biz = partitionCode, tmdid = $"{_tmdId}", tmdname = $"{_tmdName}", status = 1, time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() }.ToJsonString(),
  1082. expires = DateTimeOffset.UtcNow.AddDays(7).ToUnixTimeSeconds()
  1083. };
  1084. var url = _configuration.GetValue<string>("HaBookAuth:CoreService:sendnotification");
  1085. var clientID = _configuration.GetValue<string>("HaBookAuth:CoreService:clientID");
  1086. var clientSecret = _configuration.GetValue<string>("HaBookAuth:CoreService:clientSecret");
  1087. var location = _option.Location;
  1088. await _notificationService.SendNotification(clientID, clientSecret, location, url, notification); //发送站内发送消息
  1089. }
  1090. StandardFile saveFile = new();
  1091. //新政策文件
  1092. await foreach (StandardFile standardFile in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<StandardFile>(queryText: $"select value(c) from c where c.id='{standardFileId}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"StandardFile") }))
  1093. {
  1094. if (standardFile != null)
  1095. {
  1096. standardFile.standard = $"{_oldStandard}";
  1097. standardFile.id = $"{_oldId}";
  1098. saveFile = standardFile;
  1099. }
  1100. }
  1101. StandardFile tempFile = new();
  1102. if (saveFile.id != null)
  1103. {
  1104. var respFile = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemStreamAsync(saveFile.id, new PartitionKey("StandardFile"));
  1105. if (respFile.Status == 200)
  1106. {
  1107. using var json = await JsonDocument.ParseAsync(respFile.ContentStream);
  1108. tempFile = json.ToObject<StandardFile>();
  1109. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").DeleteItemAsync<StandardFile>(tempFile.id, new PartitionKey("StandardFile"));
  1110. }
  1111. tempFile = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(saveFile, new PartitionKey($"StandardFile")); // 需要删除原来的政策文件数据在进行添加
  1112. }
  1113. //if (tempFile.id != null)
  1114. //{
  1115. // if (tempFile.id.Equals(saveFile.id))
  1116. // await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReplaceItemAsync<StandardFile>(saveFile, saveFile.id, new PartitionKey("StandardFile")); //直接替换以前的数据
  1117. // else
  1118. // await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(saveFile, new PartitionKey($"StandardFile")); // 需要删除原来的政策文件数据在进行添加
  1119. //}
  1120. //新的区域设置
  1121. AreaSetting saveSetting = new();
  1122. await foreach (AreaSetting areaSetting in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<AreaSetting>(queryText: $"select value(c) from c where c.id='{standardFileId}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("AreaSetting") }))
  1123. {
  1124. if (areaSetting != null)
  1125. {
  1126. areaSetting.accessConfig = null;
  1127. areaSetting.id = $"{_oldId}";
  1128. saveSetting = areaSetting;
  1129. }
  1130. }
  1131. AreaSetting tempSetting = new();
  1132. if (saveSetting.id != null)
  1133. {
  1134. var respSetting = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemStreamAsync(saveSetting.id, new PartitionKey("AreaSetting"));
  1135. if (respSetting.Status == 200)
  1136. {
  1137. using var json = await JsonDocument.ParseAsync(respSetting.ContentStream);
  1138. tempSetting = json.ToObject<AreaSetting>();
  1139. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").DeleteItemAsync<AreaSetting>(tempFile.id, new PartitionKey("AreaSetting"));
  1140. }
  1141. tempSetting = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(saveSetting, new PartitionKey($"AreaSetting")); //需要删除原来的区域设置数据在进行添加
  1142. }
  1143. //if (tempSetting.id != null)
  1144. //{
  1145. // if (tempSetting.id.Equals(saveSetting.id))
  1146. // await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReplaceItemAsync<AreaSetting>(saveSetting, saveSetting.id, new PartitionKey($"AreaSetting")); //直接替换以前的数据
  1147. // else
  1148. // await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(saveSetting, new PartitionKey($"AreaSetting")); //需要删除原来的区域设置数据在进行添加
  1149. //}
  1150. return Ok(new { state = RespondCode.Ok, repeatAbilityId, repeatAbilityTaskId });
  1151. }
  1152. /// <summary>
  1153. /// 区域列表
  1154. /// </summary>
  1155. public record RecArea
  1156. {
  1157. public string id { get; set; }
  1158. public string code { get; set; }
  1159. public string pk { get; set; }
  1160. public string name { get; set; }
  1161. public string provCode { get; set; }
  1162. public string provName { get; set; }
  1163. public string cityCode { get; set; }
  1164. public string cityName { get; set; }
  1165. public string standard { get; set; }
  1166. public string standardName { get; set; }
  1167. public string institution { get; set; }
  1168. public int schoolCount { get; set; }
  1169. public bool cutArea { get; set; } = false;
  1170. public List<AreaQuoteRecord> aquoteRec { get; set; } = new List<AreaQuoteRecord>();
  1171. }
  1172. }
  1173. }