Browse Source

创建完整的初始化学区,未完成

Li 2 years ago
parent
commit
ae18011d36
1 changed files with 169 additions and 0 deletions
  1. 169 0
      TEAMModelBI/Controllers/RepairApi/InitialAreaController.cs

+ 169 - 0
TEAMModelBI/Controllers/RepairApi/InitialAreaController.cs

@@ -0,0 +1,169 @@
+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.DI.CoreAPI;
+using TEAMModelOS.SDK.Models;
+using Azure.Cosmos;
+using System;
+using System.Collections.Generic;
+
+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 NotificationService _notificationService;
+        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, NotificationService notificationService, AzureServiceBusFactory serviceBus, IHttpClientFactory http, CoreAPIHttpService coreAPIHttpService, IWebHostEnvironment hostingEnvironment)
+        {
+            _azureCosmos = azureCosmos;
+            _dingDing = dingDing;
+            _azureStorage = azureStorage;
+            _option = option?.Value;
+            _configuration = configuration;
+            _notificationService = notificationService;
+            _serviceBus = serviceBus;
+            _http = http;
+            _coreAPIHttpService = coreAPIHttpService;
+            _environment = hostingEnvironment;
+        }
+
+        /// <summary>
+        /// 创建完整版的学区初始化
+        /// </summary>
+        /// <param name="jsonElement"></param>
+        /// <returns></returns>
+        [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("oldId", out JsonElement _oldId);
+            jsonElement.TryGetProperty("oldStandard", out JsonElement oldStandard);
+            jsonElement.TryGetProperty("oldName", out JsonElement oldName);
+            //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
+
+            var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
+
+            //操作记录实体
+            var tempStandard = !string.IsNullOrEmpty($"{oldStandard}") && !string.IsNullOrEmpty($"{_oldId}") ? $"{oldStandard}" : "standard2";
+
+            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").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()
+            {
+                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
+
+
+            return Ok(new { sate = RespondCode.Ok });
+        }
+
+
+
+
+
+    }
+}