BatchAreaController.cs 75 KB

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