Browse Source

新接口的数据接口。

Li 2 years ago
parent
commit
60d9838e03

+ 6 - 1
TEAMModelBI/Controllers/BISchool/BatchSchoolController.cs

@@ -53,8 +53,9 @@ namespace TEAMModelBI.Controllers.BISchool
         //读取配置信息
         private readonly IConfiguration _configuration;
         private readonly CoreAPIHttpService _coreAPIHttpService;
+        private readonly HttpTrigger _httpTrigger;
 
-        public BatchSchoolController(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage, IOptionsSnapshot<Option> option, IWebHostEnvironment hostingEnvironment, IConfiguration configuration, IHttpClientFactory http, CoreAPIHttpService coreAPIHttpService)
+        public BatchSchoolController(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage, IOptionsSnapshot<Option> option, IWebHostEnvironment hostingEnvironment, IConfiguration configuration, IHttpClientFactory http, CoreAPIHttpService coreAPIHttpService, HttpTrigger httpTrigger)
         {
             _azureCosmos = azureCosmos;
             _dingDing = dingDing;
@@ -64,6 +65,7 @@ namespace TEAMModelBI.Controllers.BISchool
             _configuration = configuration;
             _http = http;
             _coreAPIHttpService = coreAPIHttpService;
+            _httpTrigger = httpTrigger;
         }
 
         /// <summary>
@@ -218,8 +220,11 @@ namespace TEAMModelBI.Controllers.BISchool
                             stringBuilder.Append($"创建学校:{upSchool.name}【{upSchool.id}】");
                             noticeDD.Append($"{upSchool.name}【{upSchool.id}】 \r 学校管理员信息:");
                             upSc.Add(upSchool.id);
+
                             //创建学校
                             await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync<School>(upSchool, new PartitionKey(upSchool.code));
+                            //创建学校信息中间件
+                            _ = _httpTrigger.RequestHttpTrigger(new { school = $"{upSchool}" }, _option.Location, "set-sc-birelation");
 
                             var content = new StringContent(bischool.admin.ToArray().ToJsonString(), Encoding.UTF8, "application/json");
                             string json = await _coreAPIHttpService.GetUserInfos(content);

+ 9 - 77
TEAMModelBI/Controllers/Census/SchoolController.cs

@@ -684,93 +684,25 @@ namespace TEAMModelBI.Controllers.Census
         }
 
         /// <summary>
-        /// 学校信息
+        /// 学校信息中间件查询接口
         /// </summary>
         /// <param name="jsonElement"></param>
         /// <returns></returns>
         [ProducesDefaultResponseType]
         [HttpPost("get-scinfos")]
-        public async Task<IActionResult> GetScInfos(JsonElement jsonElement)
+        public async Task<IActionResult> GetScInfos(JsonElement jsonElement) 
         {
-            try
-            {
-                var cosmosClient = _azureCosmos.GetCosmosClient();
-                List<string> scId = await CommonFind.FindScIds(cosmosClient, "select value(c.id) from c ", "Base");
-                List<BIRelation> scInfos = new();
-                foreach (var itemId in scId)
-                {
-                    BIRelation bIRelation = null;
-                    var resRel = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(itemId, new PartitionKey("BIRel"));
-                    if (resRel.Status == 200)
-                    {
-                        using var fileJson = await JsonDocument.ParseAsync(resRel.ContentStream);
-                        bIRelation = fileJson.ToObject<BIRelation>();
-                    }
-                    else
-                    {
-                        ScBaseInfo scBaseInfo = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<ScBaseInfo>(itemId, new PartitionKey("Base"));
-                        string areaName = null;
-                        if (!string.IsNullOrEmpty(scBaseInfo.areaId))
-                        {
-                            await foreach (var itemName in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<string>(queryText: $"select value(c.name) from c where c.pk='Area' and c.id='{scBaseInfo.areaId}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base-Area") }))
-                            {
-                                areaName = itemName;
-                            }
-                        }
-
-                        bIRelation = new BIRelation()
-                        {
-                            id = scBaseInfo.id,
-                            name = scBaseInfo.name,
-                            picture = scBaseInfo.picture,
-                            size = scBaseInfo.size,
-                            createDate = scBaseInfo.createTime,
-                            region = scBaseInfo.region,
-                            province = scBaseInfo.province,
-                            city = scBaseInfo.city,
-                            dist = scBaseInfo.dist,
-                            address = scBaseInfo.address,
-                            areaId = scBaseInfo.areaId,
-                            scale = scBaseInfo.scale,
-                            areaName = areaName,
-                            upDate = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
-                        };
-
-                        var response = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(itemId, new PartitionKey("ProductSum"));
-                        if (response.Status == 200)
-                        {
-                            using var json = await JsonDocument.ParseAsync(response.ContentStream);
-                            if (json.RootElement.TryGetProperty("serial", out JsonElement serial) && !serial.ValueKind.Equals(JsonValueKind.Null))
-                            {
-                                List<string> serials = serial.ToObject<List<SchoolProductSumData>>().Select(x => x.prodCode).ToList();
-                                bIRelation.serial = serials;
-                            }
-                            if (json.RootElement.TryGetProperty("service", out JsonElement service) && !service.ValueKind.Equals(JsonValueKind.Null))
-                            {
-                                List<string> services = service.ToObject<List<SchoolProductSumData>>().Select(x => x.prodCode).ToList();
-                                bIRelation.service = services;
-                            }
-                            if (json.RootElement.TryGetProperty("hard", out JsonElement hard) && !hard.ValueKind.Equals(JsonValueKind.Null))
-                            {
-                                List<string> hards = hard.ToObject<List<SchoolProductSumDataHard>>().Select(x => x.prodCode).ToList();
-                                bIRelation.hard = hards;
-                            }
-                        }
-                        bIRelation = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync<BIRelation>(bIRelation, new PartitionKey("BIRel"));
-                    }
-
-                    scInfos.Add(bIRelation);
-                }
+            var cosmosClient = _azureCosmos.GetCosmosClient();
+            List<string> scId = await CommonFind.FindScIds(cosmosClient, "select value(c.id) from c ", "Base");
 
-                return Ok(new { state = RespondCode.Ok, cnt = scId.Count, scInfos });
-            }
-            catch (Exception ex)
+            List<BIRelation> scInfos = new();
+            await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<BIRelation>(queryText:$"select value(c) from c",requestOptions:new QueryRequestOptions() { PartitionKey = new PartitionKey("BIRel") }))
             {
-                await _dingDing.SendBotMsg($"BI,{_option.Location},/school/get-scinfos \n{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
-                return BadRequest();
+                scInfos.Add(item);
             }
-        }
 
+            return Ok(new { state = RespondCode.Ok, allCnt = scId.Count, scInfos });
+        }
 
         /// <summary>
         /// 依据Id查询School容器  数据管理工具——查询工具

+ 94 - 0
TEAMModelBI/Controllers/RepairApi/SchoolRepController.cs

@@ -21,6 +21,9 @@ using Azure.Cosmos;
 using System.Linq;
 using TEAMModelOS.SDK.Context.Constant;
 using Pipelines.Sockets.Unofficial.Arenas;
+using TEAMModelBI.Tool;
+using TEAMModelOS.SDK.Models.Cosmos.BI.BISchool;
+using TEAMModelOS.SDK.Models.Cosmos.BI;
 
 namespace TEAMModelBI.Controllers.RepairApi
 {
@@ -301,5 +304,96 @@ namespace TEAMModelBI.Controllers.RepairApi
             return Ok(new { state = RespondCode.Ok, school });
         }
 
+
+
+        /// <summary>
+        /// 学校信息中间件查询和修复接口
+        /// </summary>
+        /// <param name="jsonElement"></param>
+        /// <returns></returns>
+        [ProducesDefaultResponseType]
+        [HttpPost("set-allscinfos")]
+        public async Task<IActionResult> SetAllScInfos(JsonElement jsonElement)
+        {
+            try
+            {
+                var cosmosClient = _azureCosmos.GetCosmosClient();
+                List<string> scId = await CommonFind.FindScIds(cosmosClient, "select value(c.id) from c ", "Base");
+                List<BIRelation> scInfos = new();
+                foreach (var itemId in scId)
+                {
+                    BIRelation bIRelation = null;
+                    var resRel = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(itemId, new PartitionKey("BIRel"));
+                    if (resRel.Status == 200)
+                    {
+                        continue;
+                        //using var fileJson = await JsonDocument.ParseAsync(resRel.ContentStream);
+                        //bIRelation = fileJson.ToObject<BIRelation>();
+                    }
+                    else
+                    {
+                        ScBaseInfo scBaseInfo = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<ScBaseInfo>(itemId, new PartitionKey("Base"));
+                        string areaName = null;
+                        if (!string.IsNullOrEmpty(scBaseInfo.areaId))
+                        {
+                            await foreach (var itemName in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<string>(queryText: $"select value(c.name) from c where c.pk='Area' and c.id='{scBaseInfo.areaId}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base-Area") }))
+                            {
+                                areaName = itemName;
+                            }
+                        }
+
+                        bIRelation = new BIRelation()
+                        {
+                            id = scBaseInfo.id,
+                            name = scBaseInfo.name,
+                            picture = scBaseInfo.picture,
+                            size = scBaseInfo.size,
+                            createDate = scBaseInfo.createTime,
+                            region = scBaseInfo.region,
+                            province = scBaseInfo.province,
+                            city = scBaseInfo.city,
+                            dist = scBaseInfo.dist,
+                            address = scBaseInfo.address,
+                            areaId = scBaseInfo.areaId,
+                            scale = scBaseInfo.scale,
+                            areaName = areaName,
+                            upDate = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
+                        };
+
+                        var response = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(itemId, new PartitionKey("ProductSum"));
+                        if (response.Status == 200)
+                        {
+                            using var json = await JsonDocument.ParseAsync(response.ContentStream);
+                            if (json.RootElement.TryGetProperty("serial", out JsonElement serial) && !serial.ValueKind.Equals(JsonValueKind.Null))
+                            {
+                                List<string> serials = serial.ToObject<List<SchoolProductSumData>>().Select(x => x.prodCode).ToList();
+                                bIRelation.serial = serials;
+                            }
+                            if (json.RootElement.TryGetProperty("service", out JsonElement service) && !service.ValueKind.Equals(JsonValueKind.Null))
+                            {
+                                List<string> services = service.ToObject<List<SchoolProductSumData>>().Select(x => x.prodCode).ToList();
+                                bIRelation.service = services;
+                            }
+                            if (json.RootElement.TryGetProperty("hard", out JsonElement hard) && !hard.ValueKind.Equals(JsonValueKind.Null))
+                            {
+                                List<string> hards = hard.ToObject<List<SchoolProductSumDataHard>>().Select(x => x.prodCode).ToList();
+                                bIRelation.hard = hards;
+                            }
+                        }
+                        bIRelation = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync<BIRelation>(bIRelation, new PartitionKey("BIRel"));
+                    }
+
+                    scInfos.Add(bIRelation);
+                }
+
+                return Ok(new { state = RespondCode.Ok, cnt = scId.Count, scInfos });
+            }
+            catch (Exception ex)
+            {
+                await _dingDing.SendBotMsg($"BI,{_option.Location},/sccholrep/set-allscinfos \n{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
+                return BadRequest();
+            }
+        }
+
     }
 }

+ 70 - 1
TEAMModelOS.FunctionV4/HttpTrigger/BIHttpTrigger.cs

@@ -8,13 +8,16 @@ using System.Text.Json;
 using System.Threading.Tasks;
 using Azure.Cosmos;
 using Azure.Storage.Blobs;
+using DocumentFormat.OpenXml.Bibliography;
 using Microsoft.AspNetCore.Http;
 using Microsoft.Azure.Functions.Worker;
 using Microsoft.Azure.Functions.Worker.Http;
 using StackExchange.Redis;
 using TEAMModelOS.SDK.DI;
 using TEAMModelOS.SDK.Extension;
+using TEAMModelOS.SDK.Models;
 using TEAMModelOS.SDK.Models.Cosmos.BI;
+using TEAMModelOS.SDK.Models.Cosmos.BI.BISchool;
 using TEAMModelOS.SDK.Models.Service.BIStatsWay;
 
 namespace TEAMModelOS.FunctionV4.HttpTrigger
@@ -36,6 +39,72 @@ namespace TEAMModelOS.FunctionV4.HttpTrigger
             _httpClient = httpClient;
         }
 
+        [Function("set-sc-birelation")]
+        public async Task<HttpResponseData> upSchoolBIRelation([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestData req)
+        {
+            var response = req.CreateResponse(HttpStatusCode.OK);
+            dynamic jsondata = new ExpandoObject();
+            try
+            {
+                var cosmosClient = _azureCosmos.GetCosmosClient();
+                var tableClient = _azureStorage.GetCloudTableClient();
+                var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
+
+                string data = await new StreamReader(req.Body).ReadToEndAsync();
+                var json = JsonDocument.Parse(data).RootElement;
+                School school = new();
+                jsondata = json;
+                if (json.TryGetProperty("school", out JsonElement _school))
+                {
+                    school = _school.ToObject<School>();
+                }
+                string type = "";
+
+                BIRelation bIRelation = new();
+                var resBiRel =  await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(school.id, new PartitionKey("BIRel"));
+                if (resBiRel.Status == 200)
+                {
+                    using var fileJson = await JsonDocument.ParseAsync(resBiRel.ContentStream);
+                    bIRelation = fileJson.ToObject<BIRelation>();
+
+                }
+                else
+                    bIRelation.id = school.name;
+
+                bIRelation.name = school.name;
+                bIRelation.picture = school.picture;
+                bIRelation.region = school.region;
+                bIRelation.province = school.province;
+                bIRelation.city = school.city;
+                bIRelation.dist = school.dist;
+                bIRelation.address = school.address;
+                bIRelation.areaId = school.areaId;
+                bIRelation.size = school.size;
+                bIRelation.scale = school.scale;
+                bIRelation.upDate = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
+
+                if (resBiRel.Status == 200)
+                {
+                    await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<BIRelation>(bIRelation, bIRelation.id, new PartitionKey("BIRel"));
+                    type = "update";
+                }
+                else
+                {
+                    await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync<BIRelation>(bIRelation, new PartitionKey("BIRel"));
+                    type = "add";
+                }
+
+                //保存操作记录
+                await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, $"birelation-{type}", $"触发更新学校信息表中间件", _dingDing);
+            }
+            catch (Exception ex)
+            {
+                await _dingDing.SendBotMsg($"stats-sc-info,{ex.Message}\n{ex.StackTrace}\n{jsondata.ToJsonString()}", GroupNames.成都开发測試群組);
+            }
+
+            return response;
+        }
+
         /// <summary>
         /// 统计学校的信息
         /// </summary>
@@ -164,7 +233,7 @@ namespace TEAMModelOS.FunctionV4.HttpTrigger
             }
             catch (Exception ex)
             {
-                await _dingDing.SendBotMsg($"stats-sc-info,{ex.Message}\n{ex.StackTrace}\n{jsondata.ToJsonString()}", GroupNames.成都开发測試群組);
+                await _dingDing.SendBotMsg($"set-scstats-type,{ex.Message}\n{ex.StackTrace}\n{jsondata.ToJsonString()}", GroupNames.成都开发測試群組);
             }
 
             return response;