在系统中自主实现全国行政区域结构化管理

2019-07-13  本文已影响0人  viayie_8691

[if !supportLists]1、  [endif]为什么要写本文?

A、在做很多系统开发的时候,区域管理之前一直是基于js加载json格式文档来实现的三级联动。但很多系统设计的时候,有些表需要一个行政区域的外键;

B、一直以来想自主实现一次行政区域的管理在系统设计级别上

C、希望此文,能给有A、B想法的朋友给予快速完成idea的参考以及给“拿来党”(注:是拿来主义的狂热粉丝)提供力所能及的帮助。

D、近一两年自己工作是做一些深度学习中图像识别的工作,觉得博文可以给予别人帮助,因为本人看过很多别人的博文,收益颇丰。从此篇开始,以后工作中遇到的问题值得分享的,都会写文分享。完成个人从“拿来主义”到“造车轮”转变的起点,更希望以此鼓励跟我一样的朋友“开始分享”!

[if !supportLists]2、  [endif]具体实现

[if !supportLists]A、 [endif]行政区域划分一手的资料在哪里?

个人查阅相关资料,觉得国务院网站应该有,但是查询后发现只有枝干性质的说明。再次查阅相关资料发现,最全最直接负责更新的国家部门是民政部,以下是民政部官方网站http://www.mca.gov.cn。其中在本文书写的时期最新的行政区域网址如下(该网址极有可能更新)http://www.mca.gov.cn/article/sj/xzqh/2019/

[if !supportLists]B、 [endif]打开A中最新的划分链接

[if !supportLists]C、 [endif]找到准确的最新文档位置后,我们需要完成数据的采集及落地的各自系统的DB中。

具体的分析如下,行政区域代码总共6位,前2位代表省级行政区域,中间两位代表市级行政区域,最后两位代表县区级行政区域划分。

[if !supportLists]D、 [endif]因为不论我们是爬取还是直接复制的数据在结构化上不统一所以需要做一定的结构化改变,具体来说主要是直辖市和港澳地区以及台湾省(这是我国不可分割的一部分,作为爱国人士,炎黄子孙所以中国的领土所以一定要包括进来)。

具体的调整如下图,将直辖市增加一行,将港澳及台湾增加两级

对比

[if !supportLists]E、  [endif]具体的数据结构和代码实现如下

数据库设计(按个人系统需要设计)

Sql语句如下

DROP TABLE IF EXISTS `area`;

CREATE TABLE `area` (

  `id` bigint(16) NOT NULL COMMENT '编号',

  `parent_id` bigint(16) DEFAULT NULL COMMENT '父级编号',

  `description` varchar(128) DEFAULT NULL COMMENT '名称',

  `rank_number` bigint(16) DEFAULT '0' COMMENT '排序',

  `display_status` varchar(16) DEFAULT NULL COMMENT '是否显示',

  `create_time` datetime DEFAULT NULL,

  `update_time` datetime DEFAULT NULL,

  `remark` varchar(128) DEFAULT NULL COMMENT '备注',

  PRIMARY KEY (`id`)

) ENGINE=MyISAM DEFAULT CHARSET=utf8;

[if !supportLists]F、  [endif]Java语言实现从文本到结构化数据

package com.awaymeet.fly.common.utils;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import org.apache.commons.lang3.StringUtils;

public class ReadText {

     public static void main(String[] args) throws IOException {

         Filef=new File("D://a.txt");//将从行政区域划分的网站上采集下来或者手动复制下来的文本放入这个文件

         FileReaderfileReader=new FileReader(f);

         BufferedReaderbufferedReader=new BufferedReader(fileReader);//读取文件BufferedReader

         StringreadLine;//读取的每一行文本内容

         StringpreCode=null;//前一行的行政区域编码

         //当前省市县的编码

         StringcurrentCodePrivince=null;

         StringcurrentCodeCity=null;

         StringcurrentCodeCounty=null;

         //之前省市县的编码

         StringpreCodePrivince=null;

         StringpreCodeCity=null;

         StringpreCodeCounty=null;

         int countPrivince=0;//统计省个数,辅助看看而已

         int countCity=0;//统计市个数,辅助看看而已

         int countCounty=0;//统计县区个数,辅助看看而已

         StringsinglePrivinceCode = null,singleCityCode= null;//用于记录读取省市变化的时候记录某一个省市

         StringprivinceSqlTxt = "",citySqlTxt= "",countySqlTxt= "";//最终的Sql语句

         while(null != (readLine=bufferedReader.readLine())){

              readLine=StringUtils.deleteWhitespace(readLine);

              StringcurrentCode=readLine.substring(0,6);

              StringcurrentArea=readLine.substring(6);

              currentCodePrivince=currentCode.substring(0,2);

              currentCodeCity=currentCode.substring(2,4);

              currentCodeCounty=currentCode.substring(4);

              if(null != preCode){

                   preCodePrivince=preCode.substring(0,2);

                   preCodeCity=preCode.substring(2,4);

                   preCodeCounty=preCode.substring(4);               

              }

              if(currentCodePrivince.equals(preCodePrivince)){

                   //System.out.println(currentCode+currentArea);

                   if(currentCodeCity.equals(preCodeCity)){

                       //System.out.println(currentCode+currentArea);

                       if(currentCodeCounty.equals(preCodeCounty)){

                            System.out.println(currentCode+currentArea);

                       }else{

                            System.out.println("父级市"+singleCityCode+"——下一个县区"+currentCode+currentArea);

                            countCounty++;

                            countySqlTxt+="INSERT

  INTO `area` VALUES ('"+currentCode+"',

  '"+singleCityCode+"', '"+currentArea+"', null, 'open', '2019-07-13

  21:50:02', '2019-07-13 21:50:02', null);";

                       }

                   }else{

                       singleCityCode=currentCode;

                       System.out.println("父级省"+singlePrivinceCode+"——下一个市"+currentCode+currentArea);

                       countCity++;

                       citySqlTxt+="INSERT

  INTO `area` VALUES ('"+currentCode+"',

  '"+singlePrivinceCode+"', '"+currentArea+"', null, 'open', '2019-07-13

  21:50:02', '2019-07-13 21:50:02', null);";

                   }

              }else{

                   singlePrivinceCode=currentCode;

                   System.out.println("下一个省"+currentCode+currentArea);

                   countPrivince++;

                   privinceSqlTxt+="INSERT

  INTO `area` VALUES ('"+currentCode+"',

  null, '"+currentArea+"', null, 'open', '2019-07-13

  21:50:02', '2019-07-13 21:50:02', null);";

              }

              preCode=currentCode;

         }

         bufferedReader.close();

         System.out.println(countPrivince);

         System.out.println(countCity);

         System.out.println(countCounty);

         FilesqlFile=new File("D://sql.sql");//将sql语句保存为文本

         FileWriterfileWriter=new FileWriter(sqlFile);

         fileWriter.write(privinceSqlTxt+citySqlTxt+countySqlTxt);

         fileWriter.close();

     }

}

[if !supportLists]G、 [endif]附件

[if !supportLists]3、  [endif]2019年5月份实现结果(前台实现完全是想怎么做就怎么做的事,我们只要完成数据结构化就okay)

[if !supportLists]4、  [endif]个人水平有限,文中如有错误或不当之处,请联系本人修改或删除,个人邮箱jaylinn@126.com,另外个人爱好技术,如有兴趣可加QQ群377147577

上一篇 下一篇

猜你喜欢

热点阅读