소스 검색

修正消息推送取得TMID地理資訊API

jeff 4 달 전
부모
커밋
162cdf58ea
1개의 변경된 파일158개의 추가작업 그리고 33개의 파일을 삭제
  1. 158 33
      TEAMModelBI/Controllers/BICommon/BINoticeController.cs

+ 158 - 33
TEAMModelBI/Controllers/BICommon/BINoticeController.cs

@@ -574,8 +574,72 @@ namespace TEAMModelBI.Controllers.BICommon
             List<AreaInfo> geoInfos = new(); //輸出:地理位置為單位
             List<AreaInfo> tmidInfos = new(); //輸出:TMID為單位
             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
+
+            string countryName = string.Empty;
+            string provinceName = string.Empty;
+            string cityName = string.Empty;
+            string distName = string.Empty;
+
+            //[TMID地理位置欄位 ID與名稱混雜對策] 取得搜尋的國省市名稱
+            ///國字典製作
+            var geoCountryIdNameDic = new Dictionary<string, string>();
+            foreach (KeyValuePair<string, regionbase> item in regionData.country)
+            {
+                geoCountryIdNameDic.Add(item.Value.code, item.Value.name);
+            }
+            foreach (KeyValuePair<string, regionbase> item in regionDataEn.country)
+            {
+                geoCountryIdNameDicEn.Add(item.Value.code, item.Value.name);
+            }
+            if (!string.IsNullOrWhiteSpace(countryId))
+            {
+                countryName = (regionData.country.ContainsKey(countryId)) ? regionData.country[countryId].name : string.Empty;
+            }
+            ///省字典製作
+            var geoProvinceIdNameDic = new Dictionary<string, string>();
+            if (isChina)
+            {
+                foreach (KeyValuePair<string, regionbase> item in regionData.province["CN"])
+                {
+                    geoProvinceIdNameDic.Add(item.Value.code, item.Value.name);
+                }
+            }
+            if (!string.IsNullOrWhiteSpace(provinceId))
+            {
+                provinceName = (geoProvinceIdNameDic.ContainsKey(provinceId)) ? geoProvinceIdNameDic[provinceId] : string.Empty;
+            }
+            ///市字典製作
+            var geoCityIdNameDic = new Dictionary<string, string>();
+            if(isChina)
+            {
+                foreach (var dicProvince in regionData.city["CN"])
+                {
+                    var pNow = dicProvince.Value;
+                    foreach (var item in pNow)
+                    {
+                        geoCityIdNameDic.Add(item.Value.code, item.Value.name);
+                    }
+                }
+            }
+            else
+            {
+                foreach (var dicProvince in regionData.city["TW"])
+                {
+                    var pNow = dicProvince.Value;
+                    foreach (var item in pNow)
+                    {
+                        geoCityIdNameDic.Add(item.Value.code, item.Value.name);
+                    }
+                }
+            }
+            if (!string.IsNullOrWhiteSpace(cityId))
+            {
+                cityName = (geoCityIdNameDic.ContainsKey(cityId)) ? geoCityIdNameDic[cityId] : string.Empty;
+            }
+
             string sqlWhere = string.Empty;
             string groupKey = "country";
             //地理資訊取得對象:TMID
@@ -588,22 +652,41 @@ namespace TEAMModelBI.Controllers.BICommon
                 if (!string.IsNullOrWhiteSpace(countryId))
                 {
                     groupKey = (!isChina) ? "city" : "province";
-                    sqlWhere += $" AND c.country = '{countryId}' ";
+                    string tmpSql = $" c.country = '{countryId}' ";
+                    if (!string.IsNullOrWhiteSpace(countryName))
+                    {
+                        comeRemoveStr.ForEach(c => { countryName = countryName.Replace(c, ""); });
+                        tmpSql += $" OR CONTAINS(c.country, '{countryName}') ";
+                    }
+                    sqlWhere += $" AND ({tmpSql}) ";
                 }
                 ///省
                 if (!string.IsNullOrWhiteSpace(provinceId))
                 {
                     groupKey = "city";
-                    sqlWhere += $" AND c.province = '{provinceId}' ";
+                    string tmpSql = $" c.province = '{provinceId}' ";
+                    if (!string.IsNullOrWhiteSpace(provinceName))
+                    {
+                        comeRemoveStr.ForEach(c => { provinceName = provinceName.Replace(c, ""); });
+                        tmpSql += $" OR CONTAINS(c.province, '{provinceName}') ";
+                    }
+                    sqlWhere += $" AND ({tmpSql}) ";
                 }
                 ///市
                 if (!string.IsNullOrWhiteSpace(cityId))
                 {
                     groupKey = "name";
-                    sqlWhere += $" AND c.city = '{cityId}' ";
+                    string tmpSql = (!isChina) ? $" c.province = '{cityId}' " : $" c.city = '{cityId}' ";
+                    if (!string.IsNullOrWhiteSpace(cityName))
+                    {
+                        comeRemoveStr.ForEach(c => { cityName = cityName.Replace(c, ""); });
+                        tmpSql += (!isChina) ? $" OR CONTAINS(c.province, '{cityName}') " : $" OR CONTAINS(c.city, '{cityName}') ";
+                    }
+                    sqlWhere += $" AND ({tmpSql}) ";
                 }
                 if (!string.IsNullOrWhiteSpace(sqlWhere))
                 {
+                    Regex regex = new Regex("^[a-zA-Z0-9]{2}$");
                     string sqlTmidEx = $"SELECT * FROM c WHERE {sqlWhere}";
                     await foreach (var item in cosmosClientCsv2.GetContainer("Core", "ID2").GetItemQueryIteratorSql<JsonElement>(queryText: sqlTmidEx, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"base-ex") }))
                     {
@@ -612,47 +695,75 @@ namespace TEAMModelBI.Controllers.BICommon
                         ///國
                         if(!string.IsNullOrWhiteSpace(tmidExInfoTmp.country))
                         {
-                            var countryInfo = regionData.country.FirstOrDefault(c => c.Key.Equals(tmidExInfoTmp.country));
-                            if (!countryInfo.Equals(default(KeyValuePair<string, regionbase>)))
+                            string countryIdNow = (regex.IsMatch(tmidExInfoTmp.country)) ? tmidExInfoTmp.country : string.Empty;
+                            if (string.IsNullOrWhiteSpace(countryIdNow)) //國欄位填入國名對策
+                            {
+                                var geoCountry = geoCountryIdNameDic.FirstOrDefault(x => x.Value.Contains(tmidExInfoTmp.country));
+                                if (!geoCountry.Equals(default(KeyValuePair<string, string>))) {
+                                    countryIdNow = geoCountry.Key;
+                                }
+                            }
+                            if (string.IsNullOrWhiteSpace(countryIdNow)) //國欄位填入國名對策(英文)
+                            {
+                                var geoCountryEn = geoCountryIdNameDicEn.FirstOrDefault(x => x.Value.Equals(tmidExInfoTmp.country));
+                                if (!geoCountryEn.Equals(default(KeyValuePair<string, string>))) {
+                                    countryIdNow = geoCountryEn.Key;
+                                }
+                            }
+                            if(!string.IsNullOrWhiteSpace(countryIdNow) && !tmidExInfoTmp.country.Equals(countryIdNow)) tmidExInfoTmp.country = countryIdNow;
+                            if (geoCountryIdNameDic.TryGetValue(countryIdNow, out var countryInfo))
                             {
-                                tmidExInfoTmp.countryName = countryInfo.Value.name;
+                                tmidExInfoTmp.countryName = countryInfo;
                             }
                         }
                         ///省
-                        if (!string.IsNullOrWhiteSpace(tmidExInfoTmp.province) && regionData.province.ContainsKey(tmidExInfoTmp.country))
+                        if (!string.IsNullOrWhiteSpace(tmidExInfoTmp.province))
                         {
-                            var provinceDic = regionData.province[tmidExInfoTmp.country];
-                            var provinceInfo = provinceDic.FirstOrDefault(p => p.Key.Equals(tmidExInfoTmp.province));
-                            if (!provinceInfo.Equals(default(KeyValuePair<string, regionbase>)))
+                            if (tmidExInfoTmp.country.Equals("TW")) //TW的省欄位填的是市資料
                             {
-                                tmidExInfoTmp.provinceName = provinceInfo.Value.name;
+                                tmidExInfoTmp.city = tmidExInfoTmp.province;
+                                tmidExInfoTmp.province = string.Empty;
+                            }
+                            else
+                            {
+                                string provinceIdNow = (regex.IsMatch(tmidExInfoTmp.province)) ? tmidExInfoTmp.province : string.Empty;
+                                if (string.IsNullOrWhiteSpace(provinceIdNow)) //省欄位填入省名對策
+                                {
+                                    var geoProvince = geoProvinceIdNameDic.FirstOrDefault(x => x.Value.Equals(tmidExInfoTmp.province));
+                                    if (!geoProvince.Equals(default(KeyValuePair<string, string>)))
+                                    {
+                                        provinceIdNow = geoProvince.Key;
+                                    }
+                                }
+                                if (!string.IsNullOrWhiteSpace(provinceIdNow) && !tmidExInfoTmp.province.Equals(provinceIdNow)) tmidExInfoTmp.province = provinceIdNow;
+                                //var provinceDic = regionData.province[tmidExInfoTmp.country];
+                                if (geoProvinceIdNameDic.TryGetValue(tmidExInfoTmp.province, out var provinceInfo))
+                                {
+                                    tmidExInfoTmp.provinceName = provinceInfo;
+                                }
                             }
                         }
                         ///市
-                        if (!string.IsNullOrWhiteSpace(tmidExInfoTmp.city) && regionData.city.ContainsKey(tmidExInfoTmp.country))
+                        if (!string.IsNullOrWhiteSpace(tmidExInfoTmp.city))
                         {
-                            if (regionData.city[tmidExInfoTmp.country].ContainsKey("tw"))
+                            string cityIdNow = (regex.IsMatch(tmidExInfoTmp.city)) ? tmidExInfoTmp.city : string.Empty;
+                            if (string.IsNullOrWhiteSpace(cityIdNow)) //市欄位填入市名對策
                             {
-                                var cityDic = regionData.city[tmidExInfoTmp.country]["tw"];
-                                var cityInfo = cityDic.FirstOrDefault(c => c.Key.Equals(tmidExInfoTmp.city));
-                                if (!cityInfo.Equals(default(KeyValuePair<string, regionbase>)))
+                                var geoCity = geoCityIdNameDic.FirstOrDefault(x => x.Value.Equals(tmidExInfoTmp.city));
+                                if (!geoCity.Equals(default(KeyValuePair<string, string>)))
                                 {
-                                    tmidExInfoTmp.cityName = cityInfo.Value.name;
+                                    cityIdNow = geoCity.Key;
                                 }
                             }
-                            else if(!string.IsNullOrWhiteSpace(tmidExInfoTmp.province) && regionData.city[tmidExInfoTmp.country].ContainsKey(tmidExInfoTmp.province))
+                            if (!string.IsNullOrWhiteSpace(cityIdNow) && !tmidExInfoTmp.city.Equals(cityIdNow)) tmidExInfoTmp.city = cityIdNow;
+                            if(geoCityIdNameDic.TryGetValue(tmidExInfoTmp.city, out var cityInfo))
                             {
-                                var cityDic = regionData.city[tmidExInfoTmp.country][tmidExInfoTmp.province];
-                                var cityInfo = cityDic.FirstOrDefault(c => c.Key.Equals(tmidExInfoTmp.city));
-                                if (!cityInfo.Equals(default(KeyValuePair<string, regionbase>)))
-                                {
-                                    tmidExInfoTmp.cityName = cityInfo.Value.name;
-                                }
+                                tmidExInfoTmp.cityName = cityInfo;
                             }
                         }
                         //要取得的學校ID
-                        if (string.IsNullOrWhiteSpace(tmidExInfoTmp.schoolCode) && !schIds.Contains(tmidExInfoTmp.schoolCode)) schIds.Add(tmidExInfoTmp.schoolCode);
-                        if (string.IsNullOrWhiteSpace(tmidExInfoTmp.schoolCodeW) && !schIds.Contains(tmidExInfoTmp.schoolCodeW)) schIds.Add(tmidExInfoTmp.schoolCodeW);
+                        if (!string.IsNullOrWhiteSpace(tmidExInfoTmp.schoolCode) && !schIds.Contains(tmidExInfoTmp.schoolCode)) schIds.Add(tmidExInfoTmp.schoolCode);
+                        if (!string.IsNullOrWhiteSpace(tmidExInfoTmp.schoolCodeW) && !schIds.Contains(tmidExInfoTmp.schoolCodeW)) schIds.Add(tmidExInfoTmp.schoolCodeW);
                         //輸出項生成
                         tmidExInfos.Add(tmidExInfoTmp);
                     }
@@ -713,15 +824,23 @@ namespace TEAMModelBI.Controllers.BICommon
                         if (!string.IsNullOrWhiteSpace(schId))
                         {
                             AreaSchoolInfo schInGeo = geoInfo.lists.FirstOrDefault(s => s.id.Equals(schId));
-                            if(schInGeo == null)
+                            if (schInGeo == null)
                             {
                                 geoInfo.lists.Add(new AreaSchoolInfo() { id = schId, name = schName });
                                 schInGeo = geoInfo.lists.FirstOrDefault(s => s.id.Equals(schId));
                             }
                             schInGeo.tchCnt++;
+                            geoInfo.scCnt++;
+                            geoInfo.tchCnt++;
                         }
                     }
                 }
+                ///移除無校列
+                geoInfos.RemoveAll(g => g.lists.Count.Equals(0));
+                if (!showList)
+                {
+                    geoInfos.ForEach(g => g.lists.Clear());
+                }
             }
             //地理資訊取得對象:學校
             else if (type.Equals("school"))
@@ -777,8 +896,10 @@ namespace TEAMModelBI.Controllers.BICommon
                             case "province":
                                 geoId = Convert.ToString(item.GetProperty("provinceId"));
                                 var provinceDic = regionData.province[countryId];
-                                var provinceInfo = provinceDic.FirstOrDefault(p => p.Key.Equals(geoId));
-                                geoName = (!provinceInfo.Equals(default(KeyValuePair<string, regionbase>))) ? provinceInfo.Value.name : string.Empty;
+                                if(provinceDic.TryGetValue(geoId, out var provinceInfo))
+                                {
+                                    geoName = provinceInfo.name;
+                                }
                                 if (!string.IsNullOrWhiteSpace(geoName)) comeRemoveStr.ForEach(c => { geoName = ReplaceLastMatch(geoName, c, string.Empty); }); //字串替換
                                 break;
                             case "city":
@@ -788,14 +909,18 @@ namespace TEAMModelBI.Controllers.BICommon
                                     if (regionData.city[countryId].ContainsKey("tw"))
                                     {
                                         var cityDic = regionData.city[countryId]["tw"];
-                                        var cityInfo = cityDic.FirstOrDefault(c => c.Key.Equals(geoId));
-                                        if (!string.IsNullOrWhiteSpace(geoName)) geoName = (!cityInfo.Equals(default(KeyValuePair<string, regionbase>))) ? cityInfo.Value.name : string.Empty;
+                                        if(cityDic.TryGetValue(geoId, out var cityInfo))
+                                        {
+                                            geoName = cityInfo.name;
+                                        }
                                     }
                                     else if (!string.IsNullOrWhiteSpace(provinceId) && regionData.city[countryId].ContainsKey(provinceId))
                                     {
                                         var cityDic = regionData.city[countryId][provinceId];
-                                        var cityInfo = cityDic.FirstOrDefault(c => c.Key.Equals(geoId));
-                                        if (!string.IsNullOrWhiteSpace(geoName)) geoName = (!cityInfo.Equals(default(KeyValuePair<string, regionbase>))) ? cityInfo.Value.name : string.Empty;
+                                        if (cityDic.TryGetValue(geoId, out var cityInfo))
+                                        {
+                                            geoName = cityInfo.name;
+                                        }
                                     }
                                 }
                                 comeRemoveStr.ForEach(c => { geoName = ReplaceLastMatch(geoName, c, string.Empty); }); //字串替換