123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850 |
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.Options;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using TEAMModelOS.Models;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK.Models;
- using Azure.Cosmos;
- using DingTalk.Api;
- using DingTalk.Api.Request;
- using DingTalk.Api.Response;
- using System.Text.Json;
- using HTEXLib.COMM.Helpers;
- using TEAMModelOS.Services.Common;
- using TEAMModelOS.SDK.Models.Cosmos.Common;
- using Azure.Storage.Blobs.Models;
- using Azure.Storage.Blobs;
- using System.Text;
- using Azure.Storage.Sas;
- using Azure.Storage.Blobs.Specialized;
- using Azure;
- using TEAMModelOS.SDK.Models.Cosmos.BI;
- using Azure.Messaging.ServiceBus;
- using TEAMModelOS.SDK.Extension;
- using TEAMModelOS.SDK.Models.Service;
- namespace TEAMModeBI.Controllers.BISchool
- {
- [Route("batcharea")]
- [ApiController]
- public class BatchAreaController : ControllerBase
- {
- private readonly AzureCosmosFactory _azureCosmos;
- private readonly DingDing _dingDing;
- private readonly Option _option;
- private readonly AzureStorageFactory _azureStorage;
- private readonly IConfiguration _configuration;
- private readonly NotificationService _notificationService;
- private readonly AzureServiceBusFactory _serviceBus;
- public BatchAreaController(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage, IOptionsSnapshot<Option> option, IConfiguration configuration,NotificationService notificationService, AzureServiceBusFactory serviceBus)
- {
- _azureCosmos = azureCosmos;
- _dingDing = dingDing;
- _azureStorage = azureStorage;
- _option = option?.Value;
- _configuration = configuration;
- _notificationService = notificationService;
- _serviceBus = serviceBus;
- }
- /// <summary>
- /// 查询所有的区域标准
- /// </summary>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("get-areas")]
- public async Task<IActionResult> GetArea()
- {
- try
- {
- List<Area> areas = new List<Area>();
- var azureClient = _azureCosmos.GetCosmosClient();
- await foreach (var item in azureClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<Area>(queryText: $"select * from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base-Area") }))
- {
- areas.Add(item);
- }
- return Ok(new { state = 200, areas });
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"BI,{_option.Location} batcharea/get-areas \n {ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
- return BadRequest();
- }
- }
- /// <summary>
- /// 批量创区
- /// </summary>
- /// <param name="areas"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("upd-area")]
- public async Task<IActionResult> batchCreateArea(List<Area> areas)
- {
- try
- {
- List<Area> standards = new List<Area>();
- bool isCreate = true;
- if (areas.Count > 0)
- {
- foreach (Area itemarea in areas)
- {
- await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<Area>(queryText: $"select value(c) from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-Area") }))
- {
- if (item.standard.Equals(itemarea.standard))
- {
- standards.Add(itemarea);
- isCreate = false;
- }
- }
- if (isCreate == true)
- {
- Area addArea = new Area()
- {
- id = Guid.NewGuid().ToString(),
- code = $"Base-Area",
- name = itemarea.name,
- provCode = itemarea.provCode,
- provName = itemarea.provName,
- cityCode = itemarea.cityCode,
- cityName = itemarea.cityName,
- standard = itemarea.standard,
- standardName = itemarea.standardName,
- institution = itemarea.institution
- };
- await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync<Area>(addArea, new PartitionKey("Base-Area"));
- }
- }
- }
- else return Ok(new { sate = 1 ,message="区域参数为空"});
- if (standards.Count > 0)
- return Ok(new { state = 201, message = "已有部分区域批量创建成功;标准项已重复!请检查标准项!", standards = standards });
- else return Ok(new { state = 200, message = "批量创区全部完成", });
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"BI,{_option.Location} batcharea/batch-createarea \n {ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
- return BadRequest();
- }
- }
- /// <summary>
- /// 批量创区 新接口
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("batch-area")]
- public async Task<IActionResult> batchArea(JsonElement jsonElement)
- {
- try
- {
- if (!jsonElement.TryGetProperty("name", out JsonElement name)) return BadRequest();
- jsonElement.TryGetProperty("provCode", out JsonElement provCode);
- jsonElement.TryGetProperty("provName", out JsonElement provName);
- jsonElement.TryGetProperty("cityCode", out JsonElement cityCode);
- jsonElement.TryGetProperty("cityName", out JsonElement cityName);
- if (!jsonElement.TryGetProperty("standard", out JsonElement standard)) return BadRequest();
- if (!jsonElement.TryGetProperty("standardName", out JsonElement standardName)) return BadRequest();
- jsonElement.TryGetProperty("institution", out JsonElement institution);
- if (!jsonElement.TryGetProperty("tmdId", out JsonElement _tmdId)) return BadRequest();
- if (!jsonElement.TryGetProperty("tmdName", out JsonElement _tmdName)) return BadRequest();
- jsonElement.TryGetProperty("oldId", out JsonElement _oldId);
- jsonElement.TryGetProperty("oldStandard", out JsonElement oldStandard);
- //操作记录实体
- var tempStandard = !string.IsNullOrEmpty($"{oldStandard}") && !string.IsNullOrEmpty($"{_oldId}") ? $"{oldStandard}" : "standard2";
- string blobOrTable = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString();
- OperateLog bIOperateLog = new OperateLog
- {
- PartitionKey = "OperateLog-BI",
- RowKey = blobOrTable,
- recordID = blobOrTable,
- platformSource = "BI",
- tmdId = $"{_tmdId}",
- tmdName = $"{_tmdName}",
- operateDescribe = $"{_tmdName}【{_tmdId}】已操作创区功能模块:{name},当前标准【{standard}】,复制的微能力点:{tempStandard}",
- visitApi = "batcharea/batch-area",
- operateTime = DateTime.Now
- };
- //保存操作记录
- await _azureStorage.Save<OperateLog>(bIOperateLog);
- var cosmosClient = _azureCosmos.GetCosmosClient();//数据库连接
- //查询新的是否存在
- 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") }))
- {
- if (item.standard.Equals($"{standard}"))
- {
- return Ok(new { state = 1, message = "新创区的standard已存在请检查" });
- }
- }
- //区级的ID
- string areaId = Guid.NewGuid().ToString();
- Area addArea = new Area()
- {
- id = areaId,
- code = $"Base-Area",
- name = $"{name}",
- provCode = $"{provCode}",
- provName = $"{provName}",
- cityCode = $"{cityCode}",
- cityName = $"{cityName}",
- standard = $"{standard}",
- standardName = $"{standardName}",
- institution = $"{institution}"
- };
- //创建区域
- await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync<Area>(addArea, new PartitionKey("Base-Area"));
-
- List<Task<ItemResponse<Ability>>> abilities = new List<Task<ItemResponse<Ability>>>(); //存储数据
- List<Task<ItemResponse<AbilityTask>>> abilityTasks = new List<Task<ItemResponse<AbilityTask>>>(); //存储章节
- //分区键
- string partitionCode = "copyAbility-mark";
- if (!string.IsNullOrEmpty($"{oldStandard}") && !string.IsNullOrEmpty($"{_oldId}"))
- {
- //查询要复制区域的能力标准点
- 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}") }))
- {
- if (!string.IsNullOrEmpty(item.blob))
- {
- item.blob = item.blob.Replace($"/{oldStandard}/", $"/{standard}/");
- };
- item.standard = $"{standard}";
- item.code = $"Ability-{standard}";
- item.school = $"{standard}";
- //添加区能力标准点
- abilities.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(item, new PartitionKey($"Ability-{standard}")));
- }
- if (abilities.Count < 256)
- {
- await Task.WhenAll(abilities);
- }
- else
- {
- int pages = (abilities.Count + 255) / 256;
- for (int i = 0; i < pages; i++)
- {
- List<Task<ItemResponse<Ability>>> tempAbility = abilities.Skip((i) * 256).Take(256).ToList();
- await Task.WhenAll(tempAbility);
- }
- }
- //微能力点
- 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}") }))
- {
- List<Tnode> tnodes = new List<Tnode>();
- foreach (Tnode tnode in atask.children)
- {
- if (tnode.rnodes != null)
- {
- List<Rnode> rnodes = new List<Rnode>();
- foreach (Rnode rnode in tnode.rnodes)
- {
- if (!string.IsNullOrEmpty($"{rnode.link}"))
- {
- rnode.link = rnode.link.Replace($"/{oldStandard}/", $"/{standard}/");
- }
- rnodes.Add(rnode);
- }
- tnode.rnodes = rnodes;
- }
- tnodes.Add(tnode);
- }
- atask.children = tnodes;
- atask.code = $"AbilityTask-{standard}";
- atask.standard = $"{standard}";
- atask.codeval = $"{standard}";
- //添加区能力标准点中的节点
- //abilityTasks.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(atask, new PartitionKey($"AbilityTask-{standard}")));
- await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(atask, new PartitionKey($"AbilityTask-{standard}"));
- }
- //for (int i = 0; i < abilityTasks.Count; i++)
- //{
- // List<Task<ItemResponse<AbilityTask>>> tempAbilityTasks = abilityTasks.Skip(i).Take(1).ToList();
- // await Task.WhenAll(tempAbilityTasks);
- //}
- //新政策文件
- 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") }))
- {
- standardFile.standard = $"{standard}";
- standardFile.id = areaId;
- await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(standardFile, new PartitionKey($"StandardFile"));
- }
- //新的区域设置
- 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") }))
- {
- areaSetting.accessConfig = null;
- areaSetting.id = areaId;
- await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(areaSetting, new PartitionKey($"AreaSetting"));
- }
- //执行复制操作
- BatchCopyFile batchCopyFile = new BatchCopyFile();
- batchCopyFile.blobCntr = "teammodelos";
- batchCopyFile.oldFileName = $"{oldStandard}";
- batchCopyFile.newFileName = $"{standard}";
- batchCopyFile.tmdid = $"{_tmdId}";
- batchCopyFile.tmdIds = new List<string> { $"{ _tmdId}" };
- batchCopyFile.codeKey = partitionCode;
- batchCopyFile.tmdName = $"{_tmdName}";
- var messageBatchCopyFile = new ServiceBusMessage(batchCopyFile.ToJsonString());
- messageBatchCopyFile.ApplicationProperties.Add("name", "AsbCopyStandardFile");
- var activeTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
- await _serviceBus.GetServiceBusClient().SendMessageAsync(activeTask, messageBatchCopyFile);
- //发送消息实体
- Notification notification = new Notification
- {
- hubName = "hita",
- type = "msg",
- from = $"ies5:{_option.Location}:private",
- to = new List<string> { $"{ _tmdId}" },
- label = $"{partitionCode}_start",
- body = new { location = _option.Location, biz = partitionCode, tmdid = $"{_tmdId}", tmdname = $"{_tmdName}", status = 1, time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() }.ToJsonString(),
- expires = DateTimeOffset.UtcNow.AddDays(7).ToUnixTimeSeconds()
- };
- var url = _configuration.GetValue<string>("HaBookAuth:CoreService:sendnotification");
- var clientID = _configuration.GetValue<string>("HaBookAuth:CoreService:clientID");
- var clientSecret = _configuration.GetValue<string>("HaBookAuth:CoreService:clientSecret");
- var location = _option.Location;
- await _notificationService.SendNotification(clientID, clientSecret, location, url, notification); //站内发送消息
- return Ok(new { state = 200, area = addArea });
- }
- else
- {
- Area area = null;
- 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") }))
- {
- area = tempArea;
- }
- if (area != null)
- {
- //查询要复制区域的能力标准点
- 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}") }))
- {
- if (!string.IsNullOrEmpty(item.blob))
- {
- item.blob = item.blob.Replace($"/{area.standard}/", $"/{standard}/");
- };
- item.standard = $"{standard}";
- item.code = $"Ability-{standard}";
- item.school = $"{standard}";
- //添加区能力标准点
- abilities.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(item, new PartitionKey($"Ability-{standard}")));
- }
- if (abilities.Count < 256)
- {
- await Task.WhenAll(abilities);
- }
- else
- {
- int pages = (abilities.Count + 255) / 256;
- for (int i = 0; i < pages; i++)
- {
- List<Task<ItemResponse<Ability>>> tempAbility = abilities.Skip((i) * 256).Take(256).ToList();
- await Task.WhenAll(tempAbility);
- }
- }
- //微能力点
- 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}") }))
- {
- List<Tnode> tnodes = new List<Tnode>();
- foreach (Tnode tnode in atask.children)
- {
- if (tnode.rnodes != null)
- {
- List<Rnode> rnodes = new List<Rnode>();
- foreach (Rnode rnode in tnode.rnodes)
- {
- if (!string.IsNullOrEmpty($"{rnode.link}"))
- {
- rnode.link = rnode.link.Replace($"/{area.standard}/", $"/{standard}/");
- }
- rnodes.Add(rnode);
- }
- tnode.rnodes = rnodes;
- }
- tnodes.Add(tnode);
- }
- atask.children = tnodes;
- atask.code = $"AbilityTask-{standard}";
- atask.standard = $"{standard}";
- atask.codeval = $"{standard}";
- ////添加区能力标准点中的节点
- //abilityTasks.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(atask, new PartitionKey($"AbilityTask-{standard}")));
- await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(atask, new PartitionKey($"AbilityTask-{standard}"));
- }
- //for (int i = 0; i < abilityTasks.Count; i++)
- //{
- // List<Task<ItemResponse<AbilityTask>>> tempAbilityTasks = abilityTasks.Skip(i).Take(1).ToList();
- // await Task.WhenAll(tempAbilityTasks);
- //}
- //新政策文件
- 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") }))
- {
- standardFile.standard = $"{standard}";
- standardFile.id = areaId;
- await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(standardFile, new PartitionKey($"StandardFile"));
- }
- //新的区域设置
- 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") }))
- {
- areaSetting.accessConfig = null;
- areaSetting.id = areaId;
- await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(areaSetting, new PartitionKey($"AreaSetting"));
- }
- //执行复制操作
- BatchCopyFile batchCopyFile = new BatchCopyFile();
- batchCopyFile.blobCntr = "teammodelos";
- batchCopyFile.oldFileName = $"{area.standard}";
- batchCopyFile.newFileName = $"{standard}";
- batchCopyFile.tmdid = $"{_tmdId}";
- batchCopyFile.tmdIds = new List<string> { $"{ _tmdId}" };
- batchCopyFile.codeKey = partitionCode;
- batchCopyFile.tmdName = $"{_tmdName}";
- var messageBatchCopyFile = new ServiceBusMessage(batchCopyFile.ToJsonString());
- messageBatchCopyFile.ApplicationProperties.Add("name", "AsbCopyStandardFile");
- var activeTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
- await _serviceBus.GetServiceBusClient().SendMessageAsync(activeTask, messageBatchCopyFile); //执行复制操作
- //发送消息实体
- Notification notification = new Notification
- {
- hubName = "hita",
- type = "msg",
- from = $"ies5:{_option.Location}:private",
- to = new List<string> { $"{ _tmdId}" },
- label = $"{partitionCode}_start",
- body = new { location = _option.Location, biz = partitionCode, tmdid = $"{_tmdId}", tmdname = $"{_tmdName}", status = 1, time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() }.ToJsonString(),
- expires = DateTimeOffset.UtcNow.AddDays(7).ToUnixTimeSeconds()
- };
- var url = _configuration.GetValue<string>("HaBookAuth:CoreService:sendnotification");
- var clientID = _configuration.GetValue<string>("HaBookAuth:CoreService:clientID");
- var clientSecret = _configuration.GetValue<string>("HaBookAuth:CoreService:clientSecret");
- var location = _option.Location;
- await _notificationService.SendNotification(clientID, clientSecret, location, url, notification); //发送站内发送消息
- return Ok(new { state = 200, area });
- }
- else return Ok(new { state = 201, message = "未找到默认能力点!" });
- }
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"BI,{_option.Location} /batcharea/batch-area \n {ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
- return BadRequest();
- }
- }
- /// <summary>
- /// 创区后切换能力点, 先删除原来的能力点,后复制新的能力点
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("cut-standard")]
- public async Task<IActionResult> CutStandard(JsonElement jsonElement)
- {
- try
- {
- if (!jsonElement.TryGetProperty("oldId", out JsonElement _oldId)) return BadRequest();
- if (!jsonElement.TryGetProperty("oldStandard", out JsonElement _oldStandard)) return BadRequest();
- if (!jsonElement.TryGetProperty("newId", out JsonElement _newId)) return BadRequest();
- if (!jsonElement.TryGetProperty("newStandard", out JsonElement _newStandard)) return BadRequest();
- if (!jsonElement.TryGetProperty("tmdId", out JsonElement _tmdId)) return BadRequest();
- if (!jsonElement.TryGetProperty("tmdName", out JsonElement _tmdName)) return BadRequest();
- //操作记录实体
- string blobOrTable = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString();
- OperateLog bIOperateLog = new OperateLog
- {
- PartitionKey = "OperateLog-BI",
- RowKey = blobOrTable,
- recordID = blobOrTable,
- platformSource = "BI",
- tmdId = $"{_tmdId}",
- tmdName = $"{_tmdName}",
- operateDescribe = $"{_tmdName}【{_tmdId}】已操作【{_oldStandard}】切换至{_newStandard}微能力点,复制标准:{_newStandard}",
- visitApi = "batcharea/cut-standard",
- operateTime = DateTime.Now
- };
- //保存操作记录
- await _azureStorage.Save<OperateLog>(bIOperateLog);
- var cosmosClient = _azureCosmos.GetCosmosClient();
- List<string> abilityIds = new List<string>(); //册别的ID集合
- //查询册别信息
- 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}") }))
- {
- abilityIds.Add(tempAbility.id); //查询出来册别ID添加册别ID集合
- }
- //删除册别
- if (abilityIds.IsNotEmpty())
- {
- var sresponse = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").DeleteItemsStreamAsync(abilityIds, $"Ability-{_oldStandard}");
- }
- List<string> abilityTaskIds = new List<string>(); //章节ID集合
- foreach (var abilityId in abilityIds)
- {
- await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<AbilityTask>(queryText: $"select value(c) from c where c.abilityId='{abilityId}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"AbilityTask-{_oldStandard}") }))
- {
- abilityTaskIds.Add(item.id); //查询出来的章节信息ID添加到战绩集合
- }
- }
- //删除章节
- if (abilityTaskIds.IsNotEmpty())
- {
- var sresponse = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").DeleteItemsStreamAsync(abilityTaskIds, $"AbilityTask-{_oldStandard}");
- }
- List<Task<ItemResponse<Ability>>> abilities = new List<Task<ItemResponse<Ability>>>(); //存储册别数据
- List<Task<ItemResponse<AbilityTask>>> abilityTasks = new List<Task<ItemResponse<AbilityTask>>>(); //存储章节
- //查询要复制区域的能力标准点
- 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}") }))
- {
- if (!string.IsNullOrEmpty(item.blob))
- {
- item.blob = item.blob.Replace($"/{_newStandard}/", $"/{_oldStandard}/");
- };
- item.standard = $"{_oldStandard}";
- item.code = $"Ability-{_oldStandard}";
- item.school = $"{_oldStandard}";
- //添加区能力标准点
- abilities.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(item, new PartitionKey($"Ability-{_oldStandard}")));
- }
- if (abilities.Count < 256)
- {
- await Task.WhenAll(abilities);
- }
- else
- {
- int pages = (abilities.Count + 255) / 256;
- for (int i = 0; i < pages; i++)
- {
- List<Task<ItemResponse<Ability>>> tempAbility = abilities.Skip((i) * 256).Take(256).ToList();
- await Task.WhenAll(tempAbility);
- }
- }
- //微能力点
- 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}") }))
- {
- List<Tnode> tnodes = new List<Tnode>();
- foreach (Tnode tnode in atask.children)
- {
- if (tnode.rnodes != null)
- {
- List<Rnode> rnodes = new List<Rnode>();
- foreach (Rnode rnode in tnode.rnodes)
- {
- if (!string.IsNullOrEmpty($"{rnode.link}"))
- {
- rnode.link = rnode.link.Replace($"/{_newStandard}/", $"/{_oldStandard}/");
- }
- rnodes.Add(rnode);
- }
- tnode.rnodes = rnodes;
- }
- tnodes.Add(tnode);
- }
- atask.children = tnodes;
- atask.code = $"AbilityTask-{_oldStandard}";
- atask.standard = $"{_oldStandard}";
- atask.codeval = $"{_oldStandard}";
- ////添加区能力标准点中的节点
- //abilityTasks.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(atask, new PartitionKey($"AbilityTask-{_oldStandard}")));
- await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(atask, new PartitionKey($"AbilityTask-{_oldStandard}"));
- }
- //新政策文件
- 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") }))
- {
- standardFile.standard = $"{_oldStandard}";
- standardFile.id = $"{_oldId}";
- //await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(standardFile, new PartitionKey($"StandardFile")); // 需要删除原来的政策文件数据在进行添加
- await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReplaceItemAsync<StandardFile>(standardFile, standardFile.id, new PartitionKey("StandardFile")); //直接替换以前的数据
- }
- //新的区域设置
- 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") }))
- {
- areaSetting.accessConfig = null;
- areaSetting.id = $"{_oldId}";
- //await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(areaSetting, new PartitionKey($"AreaSetting")); //需要删除原来的区域设置数据在进行添加
- await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReplaceItemAsync<AreaSetting>(areaSetting, areaSetting.id, new PartitionKey($"AreaSetting")); //直接替换以前的数据
- }
- //发送消息分区键
- string partitionCode = "DelBeforeCopyAbility-mark";
- //执行复制操作
- BatchCopyFile batchCopyFile = new BatchCopyFile();
- batchCopyFile.blobCntr = "teammodelos";
- batchCopyFile.oldFileName = $"{_newStandard}";
- batchCopyFile.newFileName = $"{_oldStandard}";
- batchCopyFile.tmdid = $"{_tmdId}";
- batchCopyFile.tmdIds = new List<string> { $"{ _tmdId}" };
- batchCopyFile.codeKey = partitionCode;
- batchCopyFile.tmdName = $"{_tmdName}";
- var messageBatchCopyFile = new ServiceBusMessage(batchCopyFile.ToJsonString());
- messageBatchCopyFile.ApplicationProperties.Add("name", "AsbCopyStandardFile"); //Function暂时还未写
- var activeTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
- await _serviceBus.GetServiceBusClient().SendMessageAsync(activeTask, messageBatchCopyFile); //先执行删除操作,在执行复制
- //发送消息实体
- Notification notification = new Notification
- {
- hubName = "hita",
- type = "msg",
- from = $"ies5:{_option.Location}:private",
- to = new List<string> { $"{ _tmdId}" },
- label = $"{partitionCode}_start",
- body = new { location = _option.Location, biz = partitionCode, tmdid = $"{_tmdId}", tmdname = $"{_tmdName}", status = 1, time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() }.ToJsonString(),
- expires = DateTimeOffset.UtcNow.AddDays(7).ToUnixTimeSeconds()
- };
- var url = _configuration.GetValue<string>("HaBookAuth:CoreService:sendnotification");
- var clientID = _configuration.GetValue<string>("HaBookAuth:CoreService:clientID");
- var clientSecret = _configuration.GetValue<string>("HaBookAuth:CoreService:clientSecret");
- var location = _option.Location;
- await _notificationService.SendNotification(clientID, clientSecret, location, url, notification); //发送站内发送消息
- return Ok(new { state = 200 });
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"BI,{_option.Location} batcharea/cutstandard \n {ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
- return BadRequest();
- }
- }
- #region 测试复制文件 流程通,后期删除
- /// <summary>
- /// 测试复制Blob中的文件 批量创区接口测试完成后删除
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("copybolb")]
- public async Task<IActionResult> TestCopyBolb(JsonElement jsonElement)
- {
- if (!jsonElement.TryGetProperty("cntr", out JsonElement _cntr)) return BadRequest();
- if (!jsonElement.TryGetProperty("oldurl", out JsonElement _oldurl)) return BadRequest();
- if (!jsonElement.TryGetProperty("newurl", out JsonElement _newurl)) return BadRequest();
- var cosmosClient = _azureCosmos.GetCosmosClient();
- int index = $"{_oldurl}".IndexOf("/", 0);
- string oldUrl = index == 0 ? oldUrl = $"{_oldurl}".Substring(1) : oldUrl = $"{_oldurl}";
- var bcc = _azureStorage.GetBlobContainerClient($"{_cntr}"); //获取地址
- var urlSaS = _azureStorage.GetBlobSAS($"{_cntr}", oldUrl, BlobSasPermissions.Read | BlobSasPermissions.List); // 取得容器sas 和有效期
- //BlobBaseClient blob = new BlobBaseClient(new Uri(bcc.Uri+$"{setemp[0]}/{_newFile}")); //新的地址
- ////var s = blob.SyncCopyFromUri(new Uri(url)); //执行复制操作
- var response = bcc.GetBlobClient($"{_newurl}").SyncCopyFromUri(new Uri(urlSaS));
- if (response.Value.CopyStatus.Equals(1))
- return Ok(new { state = 200, message ="复制成功"});
- else return Ok(new { response.Value.CopyStatus });
- //var response = await bcc.GetBlobClient($"{_newurl}").StartCopyFromUriAsync(new Uri(urlSaS));
- //return Ok(new { state = 200 , response.Value,response.HasCompleted,response.HasValue,response.Id});
- }
- /// <summary>
- /// 批量复制文件接口 批量创区接口测试完成后删除
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("copybolbFile")]
- public async Task<IActionResult> TestCopyBolbFile(JsonElement jsonElement)
- {
- if (!jsonElement.TryGetProperty("cntr", out JsonElement _cntr)) return BadRequest();
- if (!jsonElement.TryGetProperty("oldstandard", out JsonElement _oldstandard)) return BadRequest();
- if (!jsonElement.TryGetProperty("newstandard", out JsonElement _newstandard)) return BadRequest();
- List<string> errmess = new List<string>();
- List<string> fils = new List<string>();
- List<Task<Response<BlobCopyInfo>>> file_list = new List<Task<Response<BlobCopyInfo>>>();
- var client = _azureStorage.GetBlobContainerClient($"{_cntr}");//获取地址
- await foreach (BlobItem item in client.GetBlobsAsync(BlobTraits.None, BlobStates.None, $"yxpt/{_oldstandard}/"))
- {
- fils.Add(item.Name);
- string oldurl1 = $"{item.Name}".Replace($"/{_oldstandard}/", $"/{_newstandard}/");
- var urlSaS = _azureStorage.GetBlobSAS($"{_cntr}", item.Name, BlobSasPermissions.Read | BlobSasPermissions.List); // 取得容器sas 和有效期
- client.GetBlobClient(oldurl1).SyncCopyFromUri(new Uri(urlSaS));
- //file_list.Add(blobcc.GetBlobClient($"{oldurl1}").SyncCopyFromUriAsync(new Uri(urlSaS)));
- }
- if (file_list.Count <= 256)
- {
- await Task.WhenAll(file_list);
- }
- else
- {
- int pages = (file_list.Count + 255) / 256;
- for (int i = 0; i < pages; i++)
- {
- List<Task<Response<BlobCopyInfo>>> file_lists = file_list.Skip((i) * 256).Take(256).ToList();
- await Task.WhenAll(file_lists);
- }
- }
- return Ok(new { fils.Count, fils });
- }
- /// <summary>
- /// 批量删除在复制文件接口 正式完成后删除该接口
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("copyafterdel")]
- public async Task<IActionResult> CopyAfterDel(JsonElement jsonElement)
- {
- if (!jsonElement.TryGetProperty("cntr", out JsonElement _cntr)) return BadRequest();
- if (!jsonElement.TryGetProperty("delstandard", out JsonElement _delstandard)) return BadRequest();
- if (!jsonElement.TryGetProperty("copystandard", out JsonElement _copystandard)) return BadRequest();
- var blobClient = _azureStorage.GetBlobContainerClient($"{_cntr}"); //获取地址
- //先删除原有的文件
- List<Task<Response<bool>>> DelList = new List<Task<Response<bool>>>();
- await foreach (BlobItem blobItem in blobClient.GetBlobsAsync(BlobTraits.None, BlobStates.None, $"yxpt/{_delstandard}/"))
- {
- //await blobClient.GetBlobClient(blobItem.Name).DeleteIfExistsAsync();
- DelList.Add(blobClient.GetBlobBaseClient(blobItem.Name).DeleteIfExistsAsync());
- }
- if (DelList.Count <= 256)
- {
- await Task.WhenAll(DelList);
- }
- else
- {
- int pages = (DelList.Count + 255) / 256;
- for (int i = 0; i < pages; i++)
- {
- List<Task<Response<bool>>> delList = DelList.Skip((i) * 256).Take(256).ToList();
- await Task.WhenAll(delList);
- }
- }
- //在复制新的文件
- List<Task<Response<BlobCopyInfo>>> CopyList = new List<Task<Response<BlobCopyInfo>>>();
- await foreach (BlobItem blobItem in blobClient.GetBlobsAsync(BlobTraits.None, BlobStates.None, $"yxpt/{_copystandard}/"))
- {
- string oldurl = $"{blobItem.Name}".Replace($"/{_copystandard}/", $"/{_delstandard}/"); //替换旧文件
- var copyUrlSas = _azureStorage.GetBlobSAS($"{_cntr}", blobItem.Name, BlobSasPermissions.Read | BlobSasPermissions.List);
- //await blobClient.GetBlobClient(oldurl).SyncCopyFromUriAsync(new Uri(copyUrlSas));
- CopyList.Add(blobClient.GetBlobClient(oldurl).SyncCopyFromUriAsync(new Uri(copyUrlSas)));
- }
- if (CopyList.Count <= 256)
- {
- await Task.WhenAll(CopyList);
- }
- else
- {
- int pages = (CopyList.Count + 255) / 256;
- for (int i = 0; i < pages; i++)
- {
- List<Task<Response<BlobCopyInfo>>> copyList = CopyList.Skip((i) * 256).Take(256).ToList();
- await Task.WhenAll(copyList);
- }
- }
- return Ok(new { state = 200 });
- }
- /// <summary>
- /// 批量复制文件夹方法 批量创区接口测试完成后删除
- /// </summary>
- /// <param name="_cntr"></param>
- /// <param name="_oldstandard"></param>
- /// <param name="_newstandard"></param>
- /// <returns></returns>
- public async Task<IActionResult> BarchCopyFile(string _cntr,string _oldstandard,string _newstandard)
- {
- List<string> errmess = new List<string>();
- List<Task<Response<BlobCopyInfo>>> file_list = new List<Task<Response<BlobCopyInfo>>>();
- var client = _azureStorage.GetBlobContainerClient($"{_cntr}"); //获取地址
- await foreach (BlobItem item in client.GetBlobsAsync(BlobTraits.None, BlobStates.None, $"yxpt/{_oldstandard}/"))
- {
- string oldurl1 = $"{item.Name}".Replace($"/{_oldstandard}/", $"/{_newstandard}/");
- //yxpt/standard6/jyzx/002ea332-0d1f-717c-0589-5f54c3c5ef4a/3 各班分数等级占比分析.mp4
- var urlSaS = _azureStorage.GetBlobSAS($"{_cntr}", item.Name, BlobSasPermissions.Read | BlobSasPermissions.List); // 取得容器sas 和有效期
- //client.GetBlobClient($"{newurl}").SyncCopyFromUri(new Uri(urlSaS));
- file_list.Add(client.GetBlobClient($"{oldurl1}").SyncCopyFromUriAsync(new Uri(urlSaS)));
- }
- if (file_list.Count <= 256)
- {
- await Task.WhenAll(file_list);
- }
- else
- {
- int pages = (file_list.Count + 255) / 256;
- for (int i = 0; i < pages; i++)
- {
- List<Task<Response<BlobCopyInfo>>> file_lists = file_list.Skip((i) * 256).Take(256).ToList();
- await Task.WhenAll(file_lists);
- }
- }
- if (errmess.Count > 0) return Ok(new { state = 201, errmess });
- else return Ok(new { state = 200, message = "全部复制成功" });
- }
- #endregion
- }
- }
|