using Microsoft.IdentityModel.Tokens; using System; using System.Collections.Generic; using System.Drawing.Drawing2D; using System.IdentityModel.Tokens.Jwt; using System.IO; using System.Text; using System.Text.Json; namespace TEAMModelOS.SDK.Extension { public static class GeoRegion { //取得國省市區地理資料架構 public static regiondata GetRegionData(List region_gl, List region_cn) { regiondata region = new regiondata(); //國際 if(region_gl.Count > 0) { foreach (regionrow itemcy in region_gl) { //country string countryCode = itemcy.code; if (!region.country.ContainsKey(countryCode)) { region.country.Add(countryCode, new regionbase() { code = countryCode, name = itemcy.name }); } //province 無 //city if (itemcy.children != null) { foreach (JsonElement itemcyChild in itemcy.children) { regionrow itemct = itemcyChild.ToObject(); string provinceCode = "tw"; //台灣的省代碼用"tw" string cityCode = itemct.code; if (!region.city.ContainsKey(countryCode)) region.city.Add(countryCode, new Dictionary>()); if (!region.city[countryCode].ContainsKey(provinceCode)) region.city[countryCode].Add(provinceCode, new Dictionary()); if (!region.city[countryCode][provinceCode].ContainsKey(cityCode)) region.city[countryCode][provinceCode].Add(cityCode, new regionbase() { code = cityCode, name = itemct.name }); //dist if (itemct.children != null) { foreach (JsonElement itemctChild in itemct.children) { regionrow itemds = itemctChild.ToObject(); string distCode = itemds.code; if (!region.dist.ContainsKey(countryCode)) region.dist.Add(countryCode, new Dictionary>>()); if (!region.dist[countryCode].ContainsKey(provinceCode)) region.dist[countryCode].Add(provinceCode, new Dictionary>()); if (!region.dist[countryCode][provinceCode].ContainsKey(cityCode)) region.dist[countryCode][provinceCode].Add(cityCode, new Dictionary()); if (!region.dist[countryCode][provinceCode][cityCode].ContainsKey(distCode)) region.dist[countryCode][provinceCode][cityCode].Add(distCode, new regionbase() { code = distCode, name = itemds.name }); } } } } } } //大陸 if (region_cn.Count > 0) { //country string countryCode = "CN"; string countryName = "中国"; if (!region.country.ContainsKey(countryCode)) { region.country.Add(countryCode, new regionbase() { code = countryCode, name = countryName }); } //province foreach (regionrow itempv in region_cn) { string provinceCode = itempv.code.Replace("0000", ""); if (!region.province.ContainsKey(countryCode)) region.province.Add(countryCode, new Dictionary()); if (!region.province[countryCode].ContainsKey(provinceCode)) region.province[countryCode].Add(provinceCode, new regionbase() { code = provinceCode, name = itempv.name }); //city if (itempv.children != null) { foreach (JsonElement itempvChild in itempv.children) { regionrow itemct = itempvChild.ToObject(); string cityCode = itemct.code; if (!region.city.ContainsKey(countryCode)) region.city.Add(countryCode, new Dictionary>()); if (!region.city[countryCode].ContainsKey(provinceCode)) region.city[countryCode].Add(provinceCode, new Dictionary()); if (!region.city[countryCode][provinceCode].ContainsKey(cityCode)) region.city[countryCode][provinceCode].Add(cityCode, new regionbase() { code = cityCode, name = itemct.name }); //dist if (itemct.children != null) { foreach (JsonElement itemctChild in itemct.children) { regionrow itemds = itemctChild.ToObject(); string distCode = itemds.code; if (!region.dist.ContainsKey(countryCode)) region.dist.Add(countryCode, new Dictionary>>()); if (!region.dist[countryCode].ContainsKey(provinceCode)) region.dist[countryCode].Add(provinceCode, new Dictionary>()); if (!region.dist[countryCode][provinceCode].ContainsKey(cityCode)) region.dist[countryCode][provinceCode].Add(cityCode, new Dictionary()); if (!region.dist[countryCode][provinceCode][cityCode].ContainsKey(distCode)) region.dist[countryCode][provinceCode][cityCode].Add(distCode, new regionbase() { code = distCode, name = itemds.name }); } } } } } } return region; } //取得EN版國省市區地理資料架構 ※EN版只取國 public static regiondata GetRegionDataEn() { regiondata region = new regiondata(); //國際 var regionEn = new List(); using (StreamReader r = new StreamReader("JsonFile/Region/region_en.json")) { string json = r.ReadToEnd(); regionEn = JsonSerializer.Deserialize>(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 country { get; set; } = new(); public Dictionary> province { get; set; } = new(); public Dictionary>> city { get; set; } = new(); public Dictionary>>> dist { get; set; } = new(); } public class regionbase { public string code { get; set; } //代碼 public string name { get; set; } //名稱 } public class regionrow : regionbase { public List children { get; set; } } //地區要去除的特殊字 public static List comeRemoveStr = new List() { "省", "市", "区", "自治州", "县", "旗", "盟", "回族", "藏族", "羌族", "哈尼族", "彝族", "壮族", "苗族", "自治", "特别行政", "地區", "區", "縣" }; //大陸直轄市的省ID public static List municipalityId = new List() { "11", "12", "31", "50", "81", "82" }; } }