Просмотр исходного кода

BI發送訊息 由地理位置取得學校或TMID API
追加地理位置英文版JSON檔

jeff 5 месяцев назад
Родитель
Сommit
7b4de2e472

+ 290 - 115
TEAMModelBI/Controllers/BICommon/BINoticeController.cs

@@ -34,6 +34,8 @@ using Microsoft.Azure.Cosmos.Table;
 using System.Net.Http.Json;
 using NotifyData = TEAMModelOS.SDK.CoreAPIHttpService.NotifyData;
 using Microsoft.Azure.Cosmos.Linq;
+using System.Text.RegularExpressions;
+using MathNet.Numerics.Distributions;
 
 
 namespace TEAMModelBI.Controllers.BICommon
@@ -382,7 +384,7 @@ namespace TEAMModelBI.Controllers.BICommon
                         if (showSchool)
                         {
                             AreaSchoolInfo sch = new AreaSchoolInfo() { id = schId, name = schNameDic[schId], tchCnt = tchCnt };
-                            areaInfo.schools.Add(sch);
+                            areaInfo.lists.Add(sch);
                         }
                     }
                 }
@@ -409,50 +411,66 @@ namespace TEAMModelBI.Controllers.BICommon
         public async Task<IActionResult> GetGeos(JsonElement jsonElement)
         {
             regiondata regionData = GetRegionData(); //取得國省市區地理資訊架構
+            regiondata regionDataEn = GetRegionDataEn(); //取得EN版國省市區地理資訊架構
             Dictionary<string, string> geoIdNameDic = new Dictionary<string, string>(); //key: geoId val:geoName
+            Dictionary<string, string> geoCountryIdNameDicEn = new Dictionary<string, string>(); //key: geoId val:geoName
             var cosmosClient = _azureCosmos.GetCosmosClient();
-            bool showSchool = (jsonElement.TryGetProperty("showSchool", out JsonElement _showSchool)) ? _showSchool.GetBoolean() : false; //是否列出學校
+            var cosmosClientCsv2 = _azureCosmos.GetCosmosClient(name: "CoreServiceV2");
+            bool showList = (jsonElement.TryGetProperty("showList", out JsonElement _showList)) ? _showList.GetBoolean() : false; //是否列出學校或TMID
             string countryId = (jsonElement.TryGetProperty("countryId", out JsonElement _countryId)) ? _countryId.GetString() : string.Empty; //地區ID(國ID)
             string provinceId = (jsonElement.TryGetProperty("provinceId", out JsonElement _provinceId)) ? _provinceId.GetString() : string.Empty; //省ID
             string cityId = (jsonElement.TryGetProperty("cityId", out JsonElement _cityId)) ? _cityId.GetString() : string.Empty; //市ID
             string distId = (jsonElement.TryGetProperty("dist", out JsonElement _distJobj)) ? _distJobj.GetString() : string.Empty; //區ID
+            string type = (jsonElement.TryGetProperty("type", out JsonElement _type)) ? _type.GetString() : "school"; //類型 tmid、school
             string countryName = string.Empty;
             string provinceName = string.Empty;
             string cityName = string.Empty;
             string distName = string.Empty;
             //bool hasVirtual = (jsonElement.TryGetProperty("virtual", out JsonElement virtualJobj)) ? virtualJobj.GetBoolean() : true; //是否取得虛擬學校
-            bool isGlobal = (_option.Location.Contains("Global")) ? true : false;
+            bool isChina = (_option.Location.Contains("China")) ? true : false;
+            if (isChina) countryId = "CN";
             List<AreaInfo> geoInfos = new();
             //取得搜尋的國省市名稱
-            if (string.IsNullOrWhiteSpace(countryId)) return BadRequest();
-            countryName = (regionData.country.ContainsKey(countryId)) ? regionData.country[countryId].name : string.Empty;
-            if (string.IsNullOrWhiteSpace(countryName))
+            ///國字典製作
+            if (string.IsNullOrWhiteSpace(countryId))
             {
-                return BadRequest();
+                geoIdNameDic = new Dictionary<string, string>();
+                foreach (KeyValuePair<string, regionbase> item in regionData.country)
+                {
+                    geoIdNameDic.Add(item.Value.code, item.Value.name);
+                }
+                geoCountryIdNameDicEn = new Dictionary<string, string>();
+                foreach (KeyValuePair<string, regionbase> item in regionDataEn.country)
+                {
+                    geoCountryIdNameDicEn.Add(item.Value.code, item.Value.name);
+                }
             }
             else
             {
-                if (isGlobal)
-                {
-                    provinceId = string.Empty;
-                    if (countryId.Equals("TW"))
-                    {
-                        geoIdNameDic = new Dictionary<string, string>();
-                        foreach (KeyValuePair<string, regionbase> item in regionData.city[countryId][countryId.ToLower()])
-                        {
-                            geoIdNameDic.Add(item.Value.code, item.Value.name);
-                        }
-                    }
-                }
-                else
+                countryName = (regionData.country.ContainsKey(countryId)) ? regionData.country[countryId].name : string.Empty;
+            }
+            ///省字典製作
+            if (!isChina)
+            {
+                provinceId = string.Empty;
+                if (countryId.Equals("TW"))
                 {
                     geoIdNameDic = new Dictionary<string, string>();
-                    foreach (KeyValuePair<string, regionbase> item in regionData.province[countryId])
+                    foreach (KeyValuePair<string, regionbase> item in regionData.city[countryId][countryId.ToLower()])
                     {
                         geoIdNameDic.Add(item.Value.code, item.Value.name);
                     }
                 }
             }
+            else
+            {
+                geoIdNameDic = new Dictionary<string, string>();
+                foreach (KeyValuePair<string, regionbase> item in regionData.province[countryId])
+                {
+                    geoIdNameDic.Add(item.Value.code, item.Value.name);
+                }
+            }
+            ///市字典製作
             if (regionData.province.ContainsKey(countryId) && !string.IsNullOrWhiteSpace(provinceId))
             {
                 provinceName = (regionData.province[countryId].ContainsKey(provinceId)) ? regionData.province[countryId][provinceId].name : string.Empty;
@@ -462,7 +480,8 @@ namespace TEAMModelBI.Controllers.BICommon
                     geoIdNameDic.Add(item.Value.code, item.Value.name);
                 }
             }
-            if (isGlobal)
+            ///校 or TMID
+            if (!isChina)
             {
                 if (regionData.city.ContainsKey(countryId))
                 {
@@ -474,7 +493,8 @@ namespace TEAMModelBI.Controllers.BICommon
                         }
                     }
                 }
-            } else
+            }
+            else
             {
                 if (regionData.city.ContainsKey(countryId) &&
                     !string.IsNullOrWhiteSpace(provinceId) && regionData.city[countryId].ContainsKey(provinceId) &&
@@ -485,128 +505,271 @@ namespace TEAMModelBI.Controllers.BICommon
                 }
             }
             //SQL文製作
-            string groupKey = string.Empty;
-            string sqlWhere = string.Empty;
-            if (!string.IsNullOrWhiteSpace(countryName))
+            string groupKey = "country";
+            string countryKeyName = (type.Equals("tmid")) ? "c.country" : "c.region";
+            string sqlWhere = $" (NOT IS_NULL({countryKeyName}) AND {countryKeyName} != '') ";
+            ///國
+            if (!string.IsNullOrWhiteSpace(countryId))
             {
-                if (!string.IsNullOrWhiteSpace(sqlWhere)) sqlWhere += $" AND";
-                comeRemoveStr.ForEach(c => { countryName = countryName.Replace(c, ""); });
-                sqlWhere += $" CONTAINS(c.region, '{countryName}') ";
-                groupKey = (isGlobal) ? "city" : "province";
+                groupKey = (!isChina) ? "city" : "province";
+            }
+            if(type.Equals("tmid"))
+            {
+                if (!string.IsNullOrWhiteSpace(countryId))
+                {
+                    string tmpSql = $" {countryKeyName} = '{countryId}' ";
+                    if (!string.IsNullOrWhiteSpace(countryName))
+                    {
+                        comeRemoveStr.ForEach(c => { countryName = countryName.Replace(c, ""); });
+                        tmpSql += $" OR CONTAINS({countryKeyName}, '{countryName}') ";
+                    }
+                    if (!string.IsNullOrWhiteSpace(sqlWhere)) sqlWhere += $" AND";
+                    sqlWhere += $" ({tmpSql}) ";
+                }  
+            }
+            else
+            {
+                if (!string.IsNullOrWhiteSpace(countryName))
+                {
+                    if (!string.IsNullOrWhiteSpace(sqlWhere)) sqlWhere += $" AND";
+                    comeRemoveStr.ForEach(c => { countryName = countryName.Replace(c, ""); });
+                    sqlWhere += $" CONTAINS({countryKeyName}, '{countryName}') ";
+                }
             }
-            if (!string.IsNullOrWhiteSpace(provinceName))
+            
+            ///省
+            if(!string.IsNullOrWhiteSpace(provinceId))
             {
-                if (!string.IsNullOrWhiteSpace(sqlWhere)) sqlWhere += $" AND";
-                comeRemoveStr.ForEach(c => { provinceName = provinceName.Replace(c, ""); });
-                sqlWhere += $" CONTAINS(c.province, '{provinceName}') ";
                 groupKey = "city";
             }
-            if (!string.IsNullOrWhiteSpace(cityName))
+            if (type.Equals("tmid"))
+            {
+                if (!string.IsNullOrWhiteSpace(provinceId))
+                {
+                    string tmpSql = $" c.province = '{provinceId}' ";
+                    if (!string.IsNullOrWhiteSpace(provinceName))
+                    {
+                        comeRemoveStr.ForEach(c => { provinceName = provinceName.Replace(c, ""); });
+                        tmpSql += $" OR CONTAINS(c.province, '{provinceName}') ";
+                    }
+                    if (!string.IsNullOrWhiteSpace(sqlWhere)) sqlWhere += $" AND";
+                    sqlWhere += $" ({tmpSql}) ";
+                }
+            }
+            else
+            {
+                if (!string.IsNullOrWhiteSpace(provinceName))
+                {
+                    if (!string.IsNullOrWhiteSpace(sqlWhere)) sqlWhere += $" AND";
+                    comeRemoveStr.ForEach(c => { provinceName = provinceName.Replace(c, ""); });
+                    sqlWhere += $" CONTAINS(c.province, '{provinceName}') ";
+                }
+            }
+                
+            ///市
+            if (!string.IsNullOrWhiteSpace(cityId))
             {
-                if (!string.IsNullOrWhiteSpace(sqlWhere)) sqlWhere += $" AND";
-                comeRemoveStr.ForEach(c => { cityName = cityName.Replace(c, ""); });
-                sqlWhere += $" CONTAINS(c.city, '{cityName}') ";
                 groupKey = "name";
             }
-            Dictionary<string, string> schGeoDic = new Dictionary<string, string>(); //key: schoolId val:統計的geoId
-            Dictionary<string, string> schNameDic = new Dictionary<string, string>(); //key: schoolId val:學校名稱
-            Dictionary<string, int> schTeacherCntDic = new Dictionary<string, int>(); //key: schoolId val:學校教師數
-            if (!string.IsNullOrWhiteSpace(sqlWhere))
+            if (type.Equals("tmid"))
             {
-                //取得學校ID列表
-                List<string> teacherCodes = new List<string>();
-                sqlWhere = $" WHERE {sqlWhere}";
-                string sqlSch = $"SELECT c.id, c.name, c.region, c.province, c.city, c.dist FROM c {sqlWhere} ";
-                ///實體校
-                await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, Constant.School).GetItemQueryIteratorSql<JsonElement>(queryText: sqlSch, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Base") }))
+                if (!string.IsNullOrWhiteSpace(cityId))
                 {
-                    string schId = item.GetProperty("id").ToString();
-                    string schName = item.GetProperty("name").ToString();
-                    string geoName = item.GetProperty(groupKey).ToString();
-                    if (groupKey.Equals("city") || groupKey.Equals("province"))
+                    string tmpSql = (!isChina) ? $" c.province = '{cityId}' " : $" c.city = '{cityId}' ";
+                    if (!string.IsNullOrWhiteSpace(cityName))
                     {
-                        if (geoName.Equals("荆州市")) geoName = "荆州市"; //例外處理
-                        else comeRemoveStr.ForEach(c => { geoName = geoName.Replace(c, ""); }); //常規處理
-
+                        comeRemoveStr.ForEach(c => { cityName = cityName.Replace(c, ""); });
+                        tmpSql += (!isChina) ? $" OR CONTAINS(c.province, '{cityName}') " : $" OR CONTAINS(c.city, '{cityName}') ";
                     }
-                    string geoId = (!string.IsNullOrWhiteSpace(geoName)) ? geoIdNameDic.FirstOrDefault(g => g.Value.Contains(geoName)).Key : string.Empty;
-                    if (!schGeoDic.ContainsKey(schId))
+                    if (!string.IsNullOrWhiteSpace(sqlWhere)) sqlWhere += $" AND";
+                    sqlWhere += $" ({tmpSql}) ";
+                }  
+            }
+            else
+            {
+                if (!string.IsNullOrWhiteSpace(cityName))
+                {
+                    if (!string.IsNullOrWhiteSpace(sqlWhere)) sqlWhere += $" AND";
+                    comeRemoveStr.ForEach(c => { cityName = cityName.Replace(c, ""); });
+                    sqlWhere += $" CONTAINS(c.city, '{cityName}') ";
+                }
+            }
+
+            //結果取得
+            ///TMID類型
+            if (type.Equals("tmid"))
+            {
+                sqlWhere = $" WHERE {sqlWhere}";
+                string sqlTmid = $"SELECT c.id, c.name, {countryKeyName}, c.province, c.city, c.dist FROM c {sqlWhere} ";
+                await foreach (var item in cosmosClientCsv2.GetContainer("Core", "ID2").GetItemQueryIteratorSql<JsonElement>(queryText: sqlTmid, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"base-ex") }))
+                {
+                    string tmId = item.GetProperty("id").ToString();
+                    string tmName = item.GetProperty("name").ToString();
+                    string geoId = (!isChina && groupKey.Equals("city")) ? item.GetProperty("province").ToString() : item.GetProperty(groupKey).ToString();
+                    string _geoIdEn = GetDictionaryKeyByValue(geoCountryIdNameDicEn, geoId); 
+                    string geostringType = (!Regex.IsMatch(geoId, "^[a-zA-Z0-9]+$") || !string.IsNullOrWhiteSpace(_geoIdEn)) ? "lang" : "numeric";
+                    if (!groupKey.Equals("name") && geostringType.Equals("lang")) //ID是地名而非ID
                     {
-                        schGeoDic.Add(schId, geoId);
-                        schNameDic.Add(schId, schName);
+                        //地名前置處理
+                        if (groupKey.Equals("city") || groupKey.Equals("province"))
+                        {
+                            if (geoId.Equals("荆州市")) geoId = "荆州市"; //例外處理
+                            else comeRemoveStr.ForEach(c => { geoId = geoId.Replace(c, ""); }); //常規處理
+                        }
+                        //國家名為英文=>取得對應的國ID對策
+                        if (groupKey.Equals("country") && !string.IsNullOrWhiteSpace(_geoIdEn)) 
+                        {
+                            geoId = _geoIdEn;
+                        }
+                        //置換geoId
+                        else
+                        {
+                            geoId = geoIdNameDic.FirstOrDefault(g => g.Value.Contains(geoId)).Key;
+                        }
                     }
-                    if (!teacherCodes.Contains($"Teacher-{schId}"))
+                    if (!string.IsNullOrWhiteSpace(geoId))
                     {
-                        teacherCodes.Add($"Teacher-{schId}");
+                        AreaInfo geoInfo = geoInfos.Where(g => g.id.Equals(geoId)).FirstOrDefault();
+                        if (geoInfo == null)
+                        {
+                            AreaInfo geoInfoCrt = new AreaInfo();
+                            geoInfoCrt.id = (groupKey.Equals("country") || groupKey.Equals("province") || groupKey.Equals("city")) ? geoId : tmId;
+                            geoInfoCrt.name = (_option.Location.Contains("Global")) ? "無法取得名稱" : "无法取得名称";
+                            if (groupKey.Equals("country") || groupKey.Equals("province") || groupKey.Equals("city"))
+                            {
+                                if (!string.IsNullOrWhiteSpace(geoId) && geoIdNameDic.ContainsKey(geoId))
+                                {
+                                    geoInfoCrt.name = geoIdNameDic[geoId];
+                                }
+                            }
+                            else
+                            {
+                                geoInfoCrt.name = tmName;
+                            }
+
+                            geoInfos.Add(geoInfoCrt);
+                            geoInfo = geoInfos.Where(g => g.id.Equals(geoInfoCrt.id)).FirstOrDefault();
+                        }
+                        geoInfo.tchCnt++;
+                        if (showList && (groupKey.Equals("country") || groupKey.Equals("province") || groupKey.Equals("city")))
+                        {
+                            if(!geoInfo.lists.Select(g => g.id).ToList().Contains(tmId))
+                            {
+                                AreaSchoolInfo listRow = new AreaSchoolInfo() { id = tmId, name = tmName, tchCnt = 1 };
+                                geoInfo.lists.Add(listRow);
+                            }
+                        }
                     }
                 }
-                ///虛擬校
-                await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, Constant.School).GetItemQueryIteratorSql<JsonElement>(queryText: sqlSch, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"VirtualBase") }))
+                if (groupKey.Equals("name")) groupKey = "tmid";
+            }
+            ///學校類型
+            else if (type.Equals("school"))
+            {
+                Dictionary<string, string> schGeoDic = new Dictionary<string, string>(); //key: schoolId val:統計的geoId
+                Dictionary<string, string> schNameDic = new Dictionary<string, string>(); //key: schoolId val:學校名稱
+                Dictionary<string, int> schTeacherCntDic = new Dictionary<string, int>(); //key: schoolId val:學校教師數
+                if (!string.IsNullOrWhiteSpace(sqlWhere))
                 {
-                    string schId = item.GetProperty("id").ToString();
-                    string schName = item.GetProperty("name").ToString();
-                    string geoName = item.GetProperty(groupKey).ToString();
-                    if (groupKey.Equals("city") || groupKey.Equals("province"))
+                    //取得學校ID列表
+                    List<string> teacherCodes = new List<string>();
+                    sqlWhere = $" WHERE {sqlWhere}";
+                    string sqlSch = $"SELECT c.id, c.name, c.region as country, c.province, c.city, c.dist FROM c {sqlWhere} ";
+                    ///實體校
+                    await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, Constant.School).GetItemQueryIteratorSql<JsonElement>(queryText: sqlSch, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Base") }))
                     {
-                        if (geoName.Equals("荆州市")) geoName = "荆州市"; //例外處理
-                        else comeRemoveStr.ForEach(c => { geoName = geoName.Replace(c, ""); }); //常規處理
+                        string schId = item.GetProperty("id").ToString();
+                        string schName = item.GetProperty("name").ToString();
+                        string geoName = item.GetProperty(groupKey).ToString();
+                        if (groupKey.Equals("city") || groupKey.Equals("province"))
+                        {
+                            if (geoName.Equals("荆州市")) geoName = "荆州市"; //例外處理
+                            else comeRemoveStr.ForEach(c => { geoName = geoName.Replace(c, ""); }); //常規處理
+
+                        }
+                        string geoId = (!string.IsNullOrWhiteSpace(geoName)) ? geoIdNameDic.FirstOrDefault(g => g.Value.Contains(geoName)).Key : string.Empty;
+                        if (!schGeoDic.ContainsKey(schId))
+                        {
+                            schGeoDic.Add(schId, geoId);
+                            schNameDic.Add(schId, schName);
+                        }
+                        if (!teacherCodes.Contains($"Teacher-{schId}"))
+                        {
+                            teacherCodes.Add($"Teacher-{schId}");
+                        }
                     }
-                    string geoId = (!string.IsNullOrWhiteSpace(geoName)) ? geoIdNameDic.FirstOrDefault(g => g.Value.Contains(geoName)).Key : string.Empty;
-                    if (!schGeoDic.ContainsKey(schId))
+                    ///虛擬校
+                    await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, Constant.School).GetItemQueryIteratorSql<JsonElement>(queryText: sqlSch, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"VirtualBase") }))
                     {
-                        schGeoDic.Add(schId, geoId);
-                        schNameDic.Add(schId, schName);
+                        string schId = item.GetProperty("id").ToString();
+                        string schName = item.GetProperty("name").ToString();
+                        string geoName = item.GetProperty(groupKey).ToString();
+
+                        string geoNameCountry = item.GetProperty("country").ToString();
+
+                        if (groupKey.Equals("city") || groupKey.Equals("province"))
+                        {
+                            if (geoName.Equals("荆州市")) geoName = "荆州市"; //例外處理
+                            else comeRemoveStr.ForEach(c => { geoName = geoName.Replace(c, ""); }); //常規處理
+                        }
+                        string geoId = (!string.IsNullOrWhiteSpace(geoName)) ? geoIdNameDic.FirstOrDefault(g => g.Value.Contains(geoName)).Key : string.Empty;
+                        if (!schGeoDic.ContainsKey(schId))
+                        {
+                            schGeoDic.Add(schId, geoId);
+                            schNameDic.Add(schId, schName);
+                        }
                     }
-                }
 
-                //取得學校教師數
-                string sqlTch = $"SELECT REPLACE(c.code, 'Teacher-', '') as schoolId, COUNT(c.id) AS tchCnt FROM c WHERE c.pk = 'Teacher' AND c.status = 'join' AND ARRAY_CONTAINS({JsonSerializer.Serialize(teacherCodes)}, c.code) GROUP BY c.code";
-                await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, Constant.School).GetItemQueryIteratorSql<JsonElement>(queryText: sqlTch, requestOptions: null))
-                {
-                    string schId = item.GetProperty("schoolId").ToString();
-                    int tchCnt = item.GetProperty("tchCnt").GetInt32();
-                    schTeacherCntDic.Add(schId, tchCnt);
+                    //取得學校教師數
+                    string sqlTch = $"SELECT REPLACE(c.code, 'Teacher-', '') as schoolId, COUNT(c.id) AS tchCnt FROM c WHERE c.pk = 'Teacher' AND c.status = 'join' AND ARRAY_CONTAINS({JsonSerializer.Serialize(teacherCodes)}, c.code) GROUP BY c.code";
+                    await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, Constant.School).GetItemQueryIteratorSql<JsonElement>(queryText: sqlTch, requestOptions: null))
+                    {
+                        string schId = item.GetProperty("schoolId").ToString();
+                        int tchCnt = item.GetProperty("tchCnt").GetInt32();
+                        schTeacherCntDic.Add(schId, tchCnt);
+                    }
                 }
-            }
 
-            //資料整理1 有教師則記入
-            foreach (KeyValuePair<string, int> item in schTeacherCntDic)
-            {
-                string schId = item.Key;
-                int tchCnt = item.Value;
-                string geoId = schGeoDic[schId];
-                AreaInfo geoInfo = geoInfos.Where(g => g.id.Equals(geoId)).FirstOrDefault();
-                if (geoInfo == null)
+                //資料整理1 有教師則記入
+                foreach (KeyValuePair<string, int> item in schTeacherCntDic)
                 {
-                    AreaInfo geoInfoCrt = new AreaInfo();
-                    geoInfoCrt.id = (groupKey.Equals("city") || groupKey.Equals("province")) ? geoId : schId;
-                    geoInfoCrt.name = (_option.Location.Contains("Global")) ? "無法取得名稱" : "无法取得名称";
-                    if(groupKey.Equals("city") || groupKey.Equals("province"))
+                    string schId = item.Key;
+                    int tchCnt = item.Value;
+                    string geoId = schGeoDic[schId];
+                    AreaInfo geoInfo = geoInfos.Where(g => g.id.Equals(geoId)).FirstOrDefault();
+                    if (geoInfo == null)
                     {
-                        if(!string.IsNullOrWhiteSpace(geoId) && geoIdNameDic.ContainsKey(geoId))
+                        AreaInfo geoInfoCrt = new AreaInfo();
+                        geoInfoCrt.id = (groupKey.Equals("country") || groupKey.Equals("province") || groupKey.Equals("city")) ? geoId : schId;
+                        geoInfoCrt.name = (_option.Location.Contains("Global")) ? "無法取得名稱" : "无法取得名称";
+                        if(groupKey.Equals("country") || groupKey.Equals("province") || groupKey.Equals("city"))
                         {
-                            geoInfoCrt.name = geoIdNameDic[geoId];
+                            if(!string.IsNullOrWhiteSpace(geoId) && geoIdNameDic.ContainsKey(geoId))
+                            {
+                                geoInfoCrt.name = geoIdNameDic[geoId];
+                            }
                         }
+                        else if(schNameDic.ContainsKey(schId))
+                        {
+                            geoInfoCrt.name = schNameDic[schId];
+                        }
+                        geoInfos.Add(geoInfoCrt);
+                        geoInfo = geoInfos.Where(g => g.id.Equals(geoInfoCrt.id)).FirstOrDefault();
                     }
-                    else if(schNameDic.ContainsKey(schId))
+                    geoInfo.scCnt++;
+                    geoInfo.tchCnt += tchCnt;
+                    if (showList && (groupKey.Equals("country") || groupKey.Equals("province") || groupKey.Equals("city")))
                     {
-                        geoInfoCrt.name = schNameDic[schId];
+                        AreaSchoolInfo sch = new AreaSchoolInfo() { id = schId, name = schNameDic[schId], tchCnt = tchCnt };
+                        geoInfo.lists.Add(sch);
                     }
-                    geoInfos.Add(geoInfoCrt);
-                    geoInfo = geoInfos.Where(g => g.id.Equals(geoInfoCrt.id)).FirstOrDefault();
-                }
-                geoInfo.scCnt++;
-                geoInfo.tchCnt += tchCnt;
-                if (showSchool && (groupKey.Equals("city") || groupKey.Equals("province")))
-                {
-                    AreaSchoolInfo sch = new AreaSchoolInfo() { id = schId, name = schNameDic[schId], tchCnt = tchCnt };
-                    geoInfo.schools.Add(sch);
                 }
+                //資料整理2 移除無學校學區
+                var result = geoInfos.Where(a => a.scCnt > 0).ToList();
+                if (groupKey.Equals("name")) groupKey = "school";
             }
-            //資料整理2 移除無學校學區
-            var result = geoInfos.Where(a => a.scCnt > 0).ToList();
-            if (groupKey.Equals("name")) groupKey = "school";
-            return Ok(new { state = 200, dataType = groupKey, data = result });
+
+            return Ok(new { state = 200, dataType = groupKey, data = geoInfos });
         }
 
         /// <summary>
@@ -734,7 +897,7 @@ namespace TEAMModelBI.Controllers.BICommon
                 if (showSchool)
                 {
                     AreaSchoolInfo sch = new AreaSchoolInfo() { id = schId, name = schNameDic[schId], tchCnt = tchCnt };
-                    unitInfo.schools.Add(sch);
+                    unitInfo.lists.Add(sch);
                 }
             }
             if (!string.IsNullOrWhiteSpace(type))
@@ -1390,6 +1553,18 @@ namespace TEAMModelBI.Controllers.BICommon
             return responseMessage;
         }
 
+        private string GetDictionaryKeyByValue(Dictionary<string, string> dic, string value)
+        {
+            string result = string.Empty;
+            foreach(KeyValuePair<string, string> d in dic)
+            {
+                if(d.Value.Equals(value))
+                {
+                    result = d.Key;
+                }
+            }
+            return result;
+        }
         /// <summary>
         /// 取得TMID資訊 接收參數class
         /// </summary>
@@ -1413,13 +1588,13 @@ namespace TEAMModelBI.Controllers.BICommon
                 public List<string> id { get; set; } = new(); //產品類型
             }
         }
-        public record Geo //TMID用
+        public record Geo
         {
             public string countryId { get; set; }
             public string provinceId { get; set; }
             public string cityId { get; set; }
             public string distId { get; set; }
-            //public string type { get; set; } //tmid、school
+            public string type { get; set; } //tmid、school
         }
 
         /// <summary>
@@ -1460,7 +1635,7 @@ namespace TEAMModelBI.Controllers.BICommon
             public string name { get; set; }
             public int scCnt { get; set; } = 0;
             public int tchCnt { get; set; } = 0;
-            public List<AreaSchoolInfo> schools { get; set; } = new();
+            public List<AreaSchoolInfo> lists { get; set; } = new();
         }
         /// <summary>
         /// 學區附屬學校

Разница между файлами не показана из-за своего большого размера
+ 1880 - 0
TEAMModelBI/JsonFile/Region/region_en.json


+ 23 - 0
TEAMModelBI/Tool/Extension/GeoRegion.cs

@@ -110,6 +110,29 @@ namespace TEAMModelBI.Models.Extension
             return region;
         }
 
+        //取得EN版國省市區地理資料架構 ※EN版只取國
+        public static regiondata GetRegionDataEn()
+        {
+            regiondata region = new regiondata();
+            //國際
+            var regionEn = new List<regionrow>();
+            using (StreamReader r = new StreamReader("JsonFile/Region/region_en.json"))
+            {
+                string json = r.ReadToEnd();
+                regionEn = JsonSerializer.Deserialize<List<regionrow>>(json);
+                foreach (regionrow itemcy in regionEn)
+                {
+                    //country
+                    string countryCode = itemcy.code;
+                    if (!region.country.ContainsKey(countryCode))
+                    {
+                        region.country.Add(countryCode, new regionbase() { code = countryCode, name = itemcy.name });
+                    }
+                }
+            }
+            return region;
+        }
+
         public class regiondata
         {
             public Dictionary<string, regionbase> country { get; set; } = new();