Browse Source

[BI]資料修正API:比較IES5學校Base與BIRel中間件的scale、size,若不一致則將學校的Base的數值寫入BIRel

jeff 2 years ago
parent
commit
07e436a028
1 changed files with 92 additions and 0 deletions
  1. 92 0
      TEAMModelBI/Controllers/RepairApi/SchoolRepController.cs

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

@@ -616,5 +616,97 @@ namespace TEAMModelBI.Controllers.RepairApi
 
             return Ok(new { state = RespondCode.Ok, keys = resultKeys });
         }
+
+        /// <summary>
+        /// 比較IES5學校Base與BIRel中間件的scale、size,若不一致則將學校的Base的數值寫入BIRel
+        /// </summary>
+        /// <returns></returns>
+        [ProducesDefaultResponseType]
+        [HttpPost("set-schoolBaseToBIRel")]
+        public async Task<IActionResult> SetSchoolBaseToBIRel(JsonElement jsonElement)
+        {
+            string mode = (jsonElement.TryGetProperty("mode", out JsonElement modeJob)) ? modeJob.GetString() : "0"; // 執行模式 "0":測試 "1":執行 預設值:測試 
+            List<string> scIds = (jsonElement.TryGetProperty("scId", out JsonElement scId)) ? scId.ToObject<List<string>>() : new List<string>(); // 學校ID(array)
+            var cosmosClient = _azureCosmos.GetCosmosClient();
+            string schIdListStr = JsonSerializer.Serialize(scIds);
+            //取得IES5學校Base
+            Dictionary<string, SimpleSchoolInfo> schBaseDic = new Dictionary<string, SimpleSchoolInfo>();
+            string qry = "SELECT c.id, c.name, c.scale, c.size FROM c";
+            string where = string.Empty;
+            if (scIds.Count > 0)
+            {
+                where += (string.IsNullOrWhiteSpace(where)) ? " WHERE " : " AND ";
+                where += $"ARRAY_CONTAINS({schIdListStr}, c.id, true)";
+            }
+            qry = $"{qry}{where}";
+            await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: qry, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
+            {
+                using var json = await JsonDocument.ParseAsync(item.ContentStream);
+                if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
+                {
+                    foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
+                    {
+                        SimpleSchoolInfo schRow = obj.ToObject<SimpleSchoolInfo>();
+                        schBaseDic.Add(schRow.id, schRow);
+                    }
+                }
+            }
+            //取得IES5學校BIRel中間件
+            Dictionary<string, SimpleSchoolInfo> updSchBIRel = new Dictionary<string, SimpleSchoolInfo>(); //回傳值 更新的學校BIRel資訊
+            if (schBaseDic.Count > 0)
+            {
+                string qryBIRel = "SELECT * FROM c";
+                string whereBIRel = string.Empty;
+                if (scIds.Count > 0)
+                {
+                    whereBIRel += (string.IsNullOrWhiteSpace(whereBIRel)) ? " WHERE " : " AND ";
+                    whereBIRel += $"ARRAY_CONTAINS({schIdListStr}, c.id, true)";
+                }
+                qryBIRel = $"{qryBIRel}{whereBIRel}";
+                await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: qryBIRel, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"BIRel") }))
+                {
+                    using var json = await JsonDocument.ParseAsync(item.ContentStream);
+                    if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
+                    {
+                        foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
+                        {
+                            bool updFlg = false;
+                            BIRelation schBIRelRow = obj.ToObject<BIRelation>();
+                            if (schBaseDic.ContainsKey(schBIRelRow.id))
+                            {
+                                //size比對
+                                if (!schBIRelRow.size.Equals(schBaseDic[schBIRelRow.id].size))
+                                {
+                                    schBIRelRow.size = schBaseDic[schBIRelRow.id].size;
+                                    updFlg = true;
+                                }
+                                //scale比對
+                                if (!schBIRelRow.scale.Equals(schBaseDic[schBIRelRow.id].scale))
+                                {
+                                    schBIRelRow.scale = schBaseDic[schBIRelRow.id].scale;
+                                    updFlg = true;
+                                }
+                                //更新
+                                if (updFlg)
+                                {
+                                    if(mode.Equals("1")) await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<BIRelation>(schBIRelRow, schBIRelRow.id, new PartitionKey("BIRel"));
+                                    updSchBIRel.Add(schBIRelRow.id, schBaseDic[schBIRelRow.id]);
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+            return Ok(new { state = RespondCode.Ok, updSchBIRel });
+        }
+
+        private class SimpleSchoolInfo
+        {
+            public string id { get; set; }
+            public string name { get; set; }
+            public int scale { get; set; }
+            public int size { get; set; }
+
+        }
     }
 }