فهرست منبع

TMID 取得IOT統計資料API 國省市區名稱取得邏輯修正

jeff 1 سال پیش
والد
کامیت
720643ca7c
2فایلهای تغییر یافته به همراه152 افزوده شده و 3 حذف شده
  1. 21 3
      TEAMModelBI/Controllers/BITmid/TmidController.cs
  2. 131 0
      TEAMModelBI/Tool/Extension/GeoRegion.cs

+ 21 - 3
TEAMModelBI/Controllers/BITmid/TmidController.cs

@@ -16,6 +16,7 @@ using TEAMModelOS.SDK.DI;
 using TEAMModelOS.SDK.Extension;
 using TEAMModelOS.SDK.Models;
 using TEAMModelOS.SDK.Services;
+using static TEAMModelBI.Models.Extension.GeoRegion;
 
 namespace TEAMModelBI.Controllers.BITmid
 {
@@ -163,6 +164,7 @@ namespace TEAMModelBI.Controllers.BITmid
                     }
                 }
                 //取得TMID進階資料
+                regiondata regionData = GetRegionData(); //取得國省市區地理資訊架構
                 query = new QueryDefinition(@"SELECT c.id, c.name, c.mobile, c.mail, c.country, c.province, c.city, c.schoolCode, c.schoolCodeW, c.unitType, c.unitName, c.jobTitle FROM c WHERE (ARRAY_CONTAINS(@key, c.id) OR ARRAY_CONTAINS(@key, c.mobile))")
                     .WithParameter("@key", tmids);
                 await foreach (var item in cosmosClientCsv2
@@ -184,9 +186,24 @@ namespace TEAMModelBI.Controllers.BITmid
                             if (string.IsNullOrWhiteSpace(tmidStics.name)) tmidStics.name = doc.GetProperty("name").GetString();
                             if (string.IsNullOrWhiteSpace(tmidStics.mobile)) tmidStics.mobile = GenDataMask(doc.GetProperty("mobile").GetString(), "mobile");
                             if (string.IsNullOrWhiteSpace(tmidStics.mail)) tmidStics.mail = GenDataMask(doc.GetProperty("mail").GetString(), "mail");
-                            tmidStics.country = doc.GetProperty("country").GetString();
-                            tmidStics.province = doc.GetProperty("province").GetString();
-                            tmidStics.city = doc.GetProperty("city").GetString();
+                            string country = doc.GetProperty("country").GetString();
+                            tmidStics.country = (regionData.country.ContainsKey(country)) ? regionData.country[country].name : country;
+                            tmidStics.country = tmidStics.country.Replace("地區", "").Replace("地区", "");
+                            string province = doc.GetProperty("province").GetString();
+                            string city = doc.GetProperty("city").GetString();
+                            string district = string.Empty;
+                            if (country.Equals("TW"))
+                            {
+                                district = city;
+                                city = province;
+                                province = string.Empty;
+                            }
+                            tmidStics.province = (regionData.country.ContainsKey(country) && regionData.province.ContainsKey(country) && regionData.province[country].ContainsKey(province)) ? regionData.province[country][province].name : province;
+                            if (country.Equals("TW"))
+                                tmidStics.city = (regionData.city.ContainsKey(country) && regionData.city[country]["tw"].ContainsKey(city)) ? regionData.city[country]["tw"][city].name : city;
+                            else if(country.Equals("CN"))
+                                tmidStics.city = (regionData.city.ContainsKey(country) && regionData.city[country].ContainsKey(province) && regionData.city[country][province].ContainsKey(city)) ? regionData.city[country][province][city].name : city;
+                            tmidStics.dist = (!string.IsNullOrWhiteSpace(district) && country.Equals("TW") && regionData.city[country]["tw"].ContainsKey(city) && regionData.dist[country]["tw"][city].ContainsKey(district)) ? regionData.dist[country]["tw"][city][district].name : district;
                             tmidStics.schoolCode = (doc.TryGetProperty("schoolCode", out JsonElement schCode)) ? schCode.GetString() : string.Empty;
                             tmidStics.schoolCodeW = (doc.TryGetProperty("schoolCodeW", out JsonElement schCodeW)) ? schCodeW.GetString() : string.Empty;
                             tmidStics.unitType = (doc.TryGetProperty("unitType", out JsonElement unitType)) ? unitType.GetString() : string.Empty;
@@ -657,6 +674,7 @@ namespace TEAMModelBI.Controllers.BITmid
             public string country { get; set; }
             public string province { get; set; }
             public string city { get; set; }
+            public string dist { get; set; }
             public string schoolCode { get; set; }
             public string schoolCodeW { get; set; }
             public string lang { get; set; }

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

@@ -0,0 +1,131 @@
+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;
+using TEAMModelOS.SDK.Extension;
+using TEAMModelOS.SDK.Models.Cosmos.BI;
+
+namespace TEAMModelBI.Models.Extension
+{
+    public static class GeoRegion
+    {
+        //取得國省市區地理資料架構
+        public static regiondata GetRegionData()
+        {
+            regiondata region = new regiondata();
+            //國際
+            var regionTw = new List<regionrow>();
+            using (StreamReader r = new StreamReader("JsonFile/Region/region_gl.json"))
+            {
+                string json = r.ReadToEnd();
+                regionTw = JsonSerializer.Deserialize<List<regionrow>>(json);
+                foreach (regionrow itemcy in regionTw)
+                {
+                    //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<regionrow>();
+                            string provinceCode = "tw"; //台灣的省代碼用"tw"
+                            string cityCode = itemct.code;
+                            if (!region.city.ContainsKey(countryCode)) region.city.Add(countryCode, new Dictionary<string, Dictionary<string, regionbase>>());
+                            if (!region.city[countryCode].ContainsKey(provinceCode)) region.city[countryCode].Add(provinceCode, new Dictionary<string, regionbase>());
+                            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<regionrow>();
+                                    string distCode = itemds.code;
+                                    if (!region.dist.ContainsKey(countryCode)) region.dist.Add(countryCode, new Dictionary<string, Dictionary<string, Dictionary<string, regionbase>>>());
+                                    if (!region.dist[countryCode].ContainsKey(provinceCode)) region.dist[countryCode].Add(provinceCode, new Dictionary<string, Dictionary<string, regionbase>>());
+                                    if (!region.dist[countryCode][provinceCode].ContainsKey(cityCode)) region.dist[countryCode][provinceCode].Add(cityCode, new Dictionary<string, regionbase>());
+                                    if (!region.dist[countryCode][provinceCode][cityCode].ContainsKey(distCode)) region.dist[countryCode][provinceCode][cityCode].Add(distCode, new regionbase() { code = distCode, name = itemds.name });
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+            //大陸
+            var regionCn = new List<regionrow>();
+            using (StreamReader r = new StreamReader("JsonFile/Region/region_cn.json"))
+            {
+                string json = r.ReadToEnd();
+                regionCn = JsonSerializer.Deserialize<List<regionrow>>(json);
+                //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 regionCn)
+                {
+                    string provinceCode = itempv.code.Replace("0000", "");
+                    if (!region.province.ContainsKey(countryCode)) region.province.Add(countryCode, new Dictionary<string, regionbase>());
+                    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<regionrow>();
+                            string cityCode = itemct.code;
+                            if (!region.city.ContainsKey(countryCode)) region.city.Add(countryCode, new Dictionary<string, Dictionary<string, regionbase>>());
+                            if (!region.city[countryCode].ContainsKey(provinceCode)) region.city[countryCode].Add(provinceCode, new Dictionary<string, regionbase>());
+                            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<regionrow>();
+                                    string distCode = itemds.code;
+                                    if (!region.dist.ContainsKey(countryCode)) region.dist.Add(countryCode, new Dictionary<string, Dictionary<string, Dictionary<string, regionbase>>>());
+                                    if (!region.dist[countryCode].ContainsKey(provinceCode)) region.dist[countryCode].Add(provinceCode, new Dictionary<string, Dictionary<string, regionbase>>());
+                                    if (!region.dist[countryCode][provinceCode].ContainsKey(cityCode)) region.dist[countryCode][provinceCode].Add(cityCode, new Dictionary<string, regionbase>());
+                                    if (!region.dist[countryCode][provinceCode][cityCode].ContainsKey(distCode)) region.dist[countryCode][provinceCode][cityCode].Add(distCode, new regionbase() { code = distCode, name = itemds.name });
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+
+            return region;
+        }
+
+        public class regiondata
+        {
+            public Dictionary<string, regionbase> country { get; set; } = new();
+            public Dictionary<string, Dictionary<string, regionbase>> province { get; set; } = new();
+            public Dictionary<string, Dictionary<string, Dictionary<string, regionbase>>> city { get; set; } = new();
+            public Dictionary<string, Dictionary<string, Dictionary<string, Dictionary<string, regionbase>>>> dist { get; set; } = new();
+        }
+        public class regionbase
+        {
+            public string code { get; set; } //代碼
+            public string name { get; set; } //名稱
+        }
+        public class regionrow : regionbase
+        {
+            public List<object> children { get; set; }
+        }
+
+    }
+}