123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 |
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.Options;
- using System.Net.Http;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK;
- using TEAMModelOS.Models;
- using System.Text.Json;
- using System.Threading.Tasks;
- using TEAMModelOS.SDK.Context.Constant;
- using Azure.Messaging.ServiceBus;
- using TEAMModelBI.Tool.Extension;
- using TEAMModelOS.SDK.Extension;
- using Microsoft.AspNetCore.Hosting;
- using TEAMModelOS.SDK.Models;
- using Microsoft.Azure.Cosmos;
- using System;
- using System.Collections.Generic;
- using TEAMModelOS.SDK.Models.Cosmos;
- using System.Linq;
- using Pipelines.Sockets.Unofficial.Arenas;
- using TEAMModelBI.Models;
- namespace TEAMModelBI.Controllers.RepairApi
- {
- [Route("initialarea")]
- [ApiController]
- public class InitialAreaController : ControllerBase
- {
- private readonly AzureCosmosFactory _azureCosmos;
- private readonly DingDing _dingDing;
- private readonly Option _option;
- private readonly AzureStorageFactory _azureStorage;
- private readonly IConfiguration _configuration;
- private readonly AzureServiceBusFactory _serviceBus;
- private readonly IHttpClientFactory _http;
- private readonly CoreAPIHttpService _coreAPIHttpService;
- private readonly IWebHostEnvironment _environment; //读取文件
- public InitialAreaController(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage, IOptionsSnapshot<Option> option, IConfiguration configuration, AzureServiceBusFactory serviceBus, IHttpClientFactory http, CoreAPIHttpService coreAPIHttpService, IWebHostEnvironment hostingEnvironment)
- {
- _azureCosmos = azureCosmos;
- _dingDing = dingDing;
- _azureStorage = azureStorage;
- _option = option?.Value;
- _configuration = configuration;
- _serviceBus = serviceBus;
- _http = http;
- _coreAPIHttpService = coreAPIHttpService;
- _environment = hostingEnvironment;
- }
- /// <summary>
- /// 创建完整版的学区初始化
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("create-full-version")]
- public async Task<IActionResult> CreateFullVersion(JsonElement jsonElement)
- {
- 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);
- jsonElement.TryGetProperty("areaAdmin", out JsonElement areadAdmin);
- if (!jsonElement.TryGetProperty("standard", out JsonElement standard)) return BadRequest();
- if (!jsonElement.TryGetProperty("standardName", out JsonElement standardName)) return BadRequest();
- jsonElement.TryGetProperty("institution", out JsonElement institution);
- jsonElement.TryGetProperty("oldStandard", out JsonElement oldStandard);
- var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
- List<CopyStandard> copyStand = new();
- if (!string.IsNullOrEmpty($"{oldStandard}"))
- copyStand = oldStandard.ToObject<List<CopyStandard>>();
- var cosmosClient = _azureCosmos.GetCosmosClient(); //数据库连接
- var tableClient = _azureStorage.GetCloudTableClient();
- var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
- ServiceBusClient serBusClient;
- try
- {
- serBusClient = _serviceBus.GetServiceBusClient();
- }
- catch
- {
- return Ok(new { state = 403, msg = "Functionn未启动,请联系管理员" });
- }
- var activeTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
- var table = tableClient.GetTableReference("IESLogin");
- //查询新的是否存在
- await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIteratorSql<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()
- {
- id = areaId,
- code = $"Base-Area",
- name = $"{name}",
- provCode = $"{provCode}",
- provName = $"{provName}",
- cityCode = $"{cityCode}",
- cityName = $"{cityName}",
- standard = $"{standard}",
- standardName = $"{standardName}",
- institution = $"{institution}"
- };
- #region 区级管理员
- var coreUser = await _coreAPIHttpService.GetUserInfo(new Dictionary<string, string> { { "key", $"{areadAdmin}" } }, _option.Location, _configuration);
- if (coreUser == null || coreUser.id == null)
- return Ok(new { state = 404, msg = "未找到改账户的管理员" });
- //string tmdId = !string.IsNullOrEmpty(tempTmdId) ? tempTmdId : $"{areadAdmin}";
- Teacher teacher = null;
- try
- {
- //查询该教师是否存在
- teacher = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<Teacher>($"{coreUser.id}", new PartitionKey("Base"));
- }
- catch
- {
- }
- if (teacher != null)
- {
- //教师存在,在该教师信息中添加要管理的学校信息
- teacher.areas.Add(new Teacher.TeacherArea { areaId = addArea.id, status = "join", name = addArea.name });
- await cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync<Teacher>(teacher, teacher.id, new PartitionKey("Base"));
- }
- else
- {
- teacher.id = coreUser.id;
- teacher.name = coreUser.name;
- teacher.picture = coreUser.picture;
- teacher.pk = "Base";
- teacher.code = "Base";
- teacher.size = 1;
- teacher.createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
- //教师存在,在该教师信息中添加要管理的学校信息
- teacher.areas.Add(new Teacher.TeacherArea { areaId = addArea.id, status = "join", name = addArea.name });
- await cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").CreateItemAsync<Teacher>(teacher, new PartitionKey("Base"));
- }
- #endregion
- //创建区域
- await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync<Area>(addArea, new PartitionKey("Base-Area"));
- //消息分区键
- string partitionCode = "copyAbility-mark";
- #region 复制微能力点到新区
- List<Task<ItemResponse<Ability>>> abilities = new(); //存储区域数据
- List<Task<ItemResponse<AbilityTask>>> abilityTasks = new(); //存储章节
- Area AreaS2 = null;
- List<Area> areas = new();
- await foreach (var itemArea in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIteratorSql<Area>(queryText: $"select value(c) from c where c.standard='standard2'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-Area") }))
- {
- areas.Add(itemArea);
- AreaS2 = itemArea;
- }
- await foreach (var itemArea in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIteratorSql<Area>(queryText: $"select value(c) from c where c.standard='standard3'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-Area") }))
- {
- areas.Add(itemArea);
- }
- foreach (var area in areas)
- {
- //查询要复制区域的能力标准点
- await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIteratorSql<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}")));
- }
- try
- {
- 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);
- }
- }
- }
- catch
- {
- return Ok(new { state = 200, msg = "创区成功,能力标准点复制失败,遗留数据影响!" });
- }
- try
- {
- //微能力点
- await foreach (var atask in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIteratorSql<AbilityTask>(queryText: $"select value(c) from c ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"AbilityTask-{area.standard}") }))
- {
- List<Tnode> tnodes = new();
- 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}"));
- }
- }
- catch
- {
- return Ok(new { state = 200, msg = "创区成功,能力标准创建成功,微能力点复制失败,遗留数据影响!" });
- }
- //执行复制操作
- //BatchCopyFile batchCopyFile = new();
- //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", "CopyStandardFile");
- //try
- //{
- // await _serviceBus.GetServiceBusClient().SendMessageAsync(activeTask, messageBatchCopyFile);
- // await serBusClient.SendMessageAsync(activeTask, messageBatchCopyFile);
- //}
- //catch (Exception)
- //{
- // return Ok(new { state = 201, msg = "能力点复制成功,复制能力点的文件失败," });
- //}
-
- }
- if (AreaS2 != null)
- {
- //新政策文件
- await foreach (StandardFile standardFile in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIteratorSql<StandardFile>(queryText: $"select value(c) from c where c.id='{AreaS2.id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"StandardFile") }))
- {
- if (standardFile != null)
- {
- 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").GetItemQueryIteratorSql<AreaSetting>(queryText: $"select value(c) from c where c.id='{AreaS2.id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("AreaSetting") }))
- {
- if (areaSetting != null)
- {
- areaSetting.accessConfig = null;
- areaSetting.id = areaId;
- await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync(areaSetting, new PartitionKey($"AreaSetting"));
- }
- }
- }
- else return Ok(new { state = 201, message = "未找到默认能力点!" });
- #endregion
- return Ok(new { sate = RespondCode.Ok });
- }
- }
- }
|