Ejemplo n.º 1
0
        public void District_Test()
        {
            var client = new RegionMapClient();
            var result = client.GetMapDistrict();

            Assert.NotNull(result);
            Assert.Equal(1, result.Status);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     按照国际区域编码标准导入中国城市与区域
        /// </summary>
        public void Init()
        {
            if (!Exists())
            {
                var regionIdList = new List <long>();
                var crileList    = new List <string>();
                var client       = new RegionMapClient();
                var mapDistrict  = client.GetMapDistrict();
                if (mapDistrict != null && mapDistrict.Status == 1)
                {
                    #region // 添加国家

                    var country = mapDistrict.Districts[0];
                    var region  = new Region(100000, 0)
                    {
                        Country  = Country.China,
                        Name     = country.Name,
                        Center   = country.Center,
                        Level    = RegionLevel.Country,
                        FullName = country.Name
                    };
                    //  Add(region);

                    #endregion // 添加国家

                    foreach (var provinceItem in country.Districts)
                    {
                        var list = new List <Region>();
                        if (provinceItem.AdCode.IsNullOrEmpty())
                        {
                            throw new ValidException("省份Id获取失败");
                        }

                        if (provinceItem.Level == "province")
                        {
                            var provinceRegion = new Region(provinceItem.AdCode.ConvertToLong(0), 0)
                            {
                                Country  = Country.China,
                                Name     = FormatRegionName(provinceItem.Name),
                                Center   = provinceItem.Center,
                                Level    = RegionLevel.Province,
                                FullName = provinceItem.Name
                            };
                            provinceRegion.ProvinceId = provinceRegion.RegionId;
                            list.Add(provinceRegion); // 添加省份

                            // 添加城市
                            foreach (var cityItem in provinceItem.Districts)
                            {
                                if (cityItem.AdCode.IsNullOrEmpty())
                                {
                                    throw new ValidException("城市Id获取失败");
                                }

                                if (cityItem.Level == "city")
                                {
                                    var cityRegion = new Region(cityItem.AdCode.ConvertToLong(0),
                                                                provinceRegion.RegionId)
                                    {
                                        Country    = Country.China,
                                        Name       = FormatRegionName(cityItem.Name),
                                        Center     = cityItem.Center,
                                        Level      = RegionLevel.City,
                                        FullName   = provinceItem.Name + " " + cityItem.Name,
                                        ProvinceId = provinceRegion.RegionId,
                                        ParentId   = provinceRegion.RegionId
                                    };
                                    cityRegion.CityId = cityRegion.RegionId;
                                    list.Add(cityRegion);
                                    regionIdList.Add(cityRegion.RegionId);

                                    // 添加区域 区县
                                    var i = 60;
                                    foreach (var countyItem in cityItem.Districts)
                                    {
                                        if (countyItem.AdCode.IsNullOrEmpty())
                                        {
                                            throw new ValidException("区域Id获取失败");
                                        }

                                        var countryId = countyItem.AdCode.ConvertToLong(0);
                                        if (regionIdList.Contains(countryId))
                                        {
                                            countryId = (countryId + i.ToString()).ConvertToLong(0);
                                            i++;
                                        }

                                        regionIdList.Add(countryId);
                                        var countyRegion = new Region(countryId,
                                                                      cityRegion.RegionId)
                                        {
                                            Country    = Country.China,
                                            Name       = FormatRegionName(countyItem.Name),
                                            Center     = countyItem.Center,
                                            Level      = RegionLevel.County,
                                            FullName   = provinceItem.Name + " " + cityItem.Name + " " + countyItem.Name,
                                            ProvinceId = provinceRegion.RegionId,
                                            CityId     = cityRegion.RegionId,
                                            ParentId   = cityRegion.RegionId
                                        };
                                        list.Add(countyRegion);
                                    }
                                }
                            }
                        }

                        //批量添加
                        AddMany(list);
                    }
                }
            }
        }