Browse Source

区域信息修改为数据库

hexiao 1 year ago
parent
commit
c8cd5d8c72

+ 103 - 2
application-webadmin/src/main/java/com/ankaibei/workFlow/webadmin/api/AreaApiService.java

@@ -1,17 +1,29 @@
 package com.ankaibei.workFlow.webadmin.api;
 
+import com.ankaibei.workFlow.webadmin.upms.model.SysRegion;
+import com.ankaibei.workFlow.webadmin.upms.service.SysRegionService;
 import com.ankaibei.workFlow.webadmin.utils.GaoDeWebUtil;
 import com.ankaibei.workflow.api.AreaApi;
 import com.ankaibei.workflow.vo.AreaInfo;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import io.jsonwebtoken.lang.Collections;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.BeanUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.util.ObjectUtils;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
+import java.util.ArrayList;
 import java.util.List;
+import java.util.stream.Collectors;
 
 @RestController
 @RequestMapping("/foreign/admin/area")
 public class AreaApiService implements AreaApi {
 
+    @Autowired
+    private SysRegionService sysRegionService;
 
     @Override
     @RequestMapping("/getInfoLikeKeywordsAndSubdistrict")
@@ -19,16 +31,105 @@ public class AreaApiService implements AreaApi {
         return GaoDeWebUtil.getInfoLikeKeywordsAndSubdistrict(keywords, subdistrict);
     }
 
+    /**
+     * 查询省份信息
+     */
     @Override
     @RequestMapping("/getProvinces")
     public List<AreaInfo> getProvinces() {
-        return GaoDeWebUtil.getProvinces();
+
+        QueryWrapper<SysRegion> qw = new QueryWrapper();
+        qw.eq("level", "province");
+        List<SysRegion> list = sysRegionService.list(qw);
+        if (!Collections.isEmpty(list)) {
+            return list.stream().map(o -> {
+                AreaInfo areaInfo = new AreaInfo();
+                BeanUtils.copyProperties(o, areaInfo);
+                return areaInfo;
+            }).collect(Collectors.toList());
+        }
+        return new ArrayList<>();
+//        return GaoDeWebUtil.getProvinces();
     }
 
+
+    // 根据父编码获取子集合
     @Override
     @RequestMapping("/getCity")
     public List<AreaInfo> getCity(String keywords) {
-        return GaoDeWebUtil.getCity(keywords);
+        if (StringUtils.isBlank(keywords)) {
+            return new ArrayList<>();
+        }
+        List<SysRegion> list = sysRegionService.list();
+        if (!Collections.isEmpty(list)) {
+            List<AreaInfo> collect = list.stream().filter(fo -> {
+                if (fo.getAdcode().equals(keywords) || fo.getName().contains(keywords)) {
+                    return true;
+                } else {
+                    return false;
+                }
+            }).filter(o -> o.getLevel().equals("province"))
+                    .map(o -> {
+                        AreaInfo areaInfo = new AreaInfo();
+                        BeanUtils.copyProperties(o, areaInfo);
+                        return areaInfo;
+                    }).collect(Collectors.toList());
+            List<AreaInfo> areaInfos = buildData(collect, list);
+//            return areaInfos;
+            // 构建树
+            return buildTree(areaInfos, list);
+        }
+        return new ArrayList<>();
+//        return GaoDeWebUtil.getCity(keywords);
+    }
+
+    /**
+     * 构建树
+     *
+     * @param areaInfos 满足条件的子区域集合
+     * @param list      全部行政区域
+     * @return
+     */
+    private List<AreaInfo> buildTree(List<AreaInfo> areaInfos, List<SysRegion> list) {
+        if (areaInfos.size() > 0) {
+            for (AreaInfo areaInfo : areaInfos) {
+                List<AreaInfo> collect1 = list.stream()
+                        .filter(o -> o.getParentAdcode().equals(areaInfo.getAdcode()))
+                        .map(o -> {
+                            AreaInfo ai = new AreaInfo();
+                            BeanUtils.copyProperties(o, ai);
+                            return ai;
+                        })
+                        .collect(Collectors.toList());
+                areaInfo.setDistricts(collect1);
+                if (!ObjectUtils.isEmpty(collect1) && collect1.size() > 0) {
+                     buildTree(collect1, list);
+                }
+            }
+        }
+        return areaInfos;
+    }
+
+    /**
+     * 构建数据
+     *
+     * @param collect 满足条件的父集合
+     * @param list    全部行政区域
+     * @return
+     */
+    private List<AreaInfo> buildData(List<AreaInfo> collect, List<SysRegion> list) {
+        List<AreaInfo> rlist = new ArrayList<>();
+        for (AreaInfo areaInfo : collect) {
+            List<AreaInfo> collect1 = list.stream().filter(o -> o.getParentAdcode().equals(areaInfo.getAdcode()))
+                    .map(o -> {
+                        AreaInfo ai = new AreaInfo();
+                        BeanUtils.copyProperties(o, ai);
+                        return ai;
+                    })
+                    .collect(Collectors.toList());
+            rlist.addAll(collect1);
+        }
+        return rlist;
     }
 
 }

+ 20 - 0
application-webadmin/src/main/java/com/ankaibei/workFlow/webadmin/upms/controller/TestController.java

@@ -0,0 +1,20 @@
+package com.ankaibei.workFlow.webadmin.upms.controller;
+
+import com.ankaibei.workFlow.common.core.object.ResponseResult;
+import com.ankaibei.workFlow.webadmin.utils.GaoDeWebUtil;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@RequestMapping("/foreign/test")
+@RestController
+public class TestController {
+
+
+    @GetMapping("/init")
+    public ResponseResult<String>  init(){
+        GaoDeWebUtil.initRegion();
+        return ResponseResult.success("ok");
+    }
+
+}

+ 8 - 0
application-webadmin/src/main/java/com/ankaibei/workFlow/webadmin/upms/dao/SysRegionMapper.java

@@ -0,0 +1,8 @@
+package com.ankaibei.workFlow.webadmin.upms.dao;
+
+import com.ankaibei.workFlow.common.core.base.dao.BaseDaoMapper;
+import com.ankaibei.workFlow.webadmin.upms.model.SysRegion;
+
+public interface SysRegionMapper extends BaseDaoMapper<SysRegion> {
+
+}

+ 11 - 0
application-webadmin/src/main/java/com/ankaibei/workFlow/webadmin/upms/dao/mapper/SysRegionMapper.xml

@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.ankaibei.workFlow.webadmin.upms.dao.SysRegionMapper">
+    <resultMap id="BaseResultMap" type="com.ankaibei.workFlow.webadmin.upms.model.SysRegion">
+        <id column="adcode" jdbcType="VARCHAR" property="adcode"/>
+        <id column="name" jdbcType="VARCHAR" property="name"/>
+        <id column="center" jdbcType="VARCHAR" property="center"/>
+        <id column="level" jdbcType="VARCHAR" property="level"/>
+        <id column="parent_adcode" jdbcType="VARCHAR" property="parentAdcode"/>
+    </resultMap>
+</mapper>

+ 32 - 0
application-webadmin/src/main/java/com/ankaibei/workFlow/webadmin/upms/model/SysRegion.java

@@ -0,0 +1,32 @@
+package com.ankaibei.workFlow.webadmin.upms.model;
+
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableName;
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+@Data
+@Builder
+@AllArgsConstructor
+@NoArgsConstructor
+@TableName(value = "work_sys_region")
+public class SysRegion {
+
+    @TableField(value = "adcode")
+    private String adcode;
+
+    @TableField(value = "name")
+    private String name;
+
+    @TableField(value = "center")
+    private String center;
+
+    @TableField(value = "level")
+    private String level;
+
+    @TableField(value = "parent_adcode")
+    private String parentAdcode;
+
+}

+ 12 - 0
application-webadmin/src/main/java/com/ankaibei/workFlow/webadmin/upms/service/SysRegionService.java

@@ -0,0 +1,12 @@
+package com.ankaibei.workFlow.webadmin.upms.service;
+
+import com.ankaibei.workFlow.common.core.base.service.IBaseService;
+import com.ankaibei.workFlow.webadmin.upms.model.SysRegion;
+import com.ankaibei.workflow.vo.AreaInfo;
+
+import java.util.List;
+
+public interface SysRegionService extends IBaseService<SysRegion, Long> {
+
+    boolean insertAll(List<AreaInfo> list);
+}

+ 57 - 0
application-webadmin/src/main/java/com/ankaibei/workFlow/webadmin/upms/service/impl/SysRegionServiceImpl.java

@@ -0,0 +1,57 @@
+package com.ankaibei.workFlow.webadmin.upms.service.impl;
+
+import com.ankaibei.workFlow.common.core.base.dao.BaseDaoMapper;
+import com.ankaibei.workFlow.common.core.base.service.BaseService;
+import com.ankaibei.workFlow.webadmin.upms.dao.SysRegionMapper;
+import com.ankaibei.workFlow.webadmin.upms.model.SysRegion;
+import com.ankaibei.workFlow.webadmin.upms.service.SysRegionService;
+import com.ankaibei.workflow.vo.AreaInfo;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.BeanUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.stereotype.Service;
+import org.springframework.util.ObjectUtils;
+
+import java.util.List;
+
+
+@Slf4j
+@Service
+public class SysRegionServiceImpl extends BaseService<SysRegion, Long> implements SysRegionService {
+
+    @Autowired
+    private SysRegionMapper sysRegionMapper;
+
+    @Override
+    protected BaseDaoMapper<SysRegion> mapper() {
+        return sysRegionMapper;
+    }
+
+
+    @Override
+    @Async
+    public boolean insertAll(List<AreaInfo> list) {
+        recursiveInsertion(list,"0");
+        return true;
+    }
+
+    private void   recursiveInsertion(List<AreaInfo> list,String parentAdcode){
+        if(!ObjectUtils.isEmpty(list) && list.size() >0){
+            for (AreaInfo areaInfo : list) {
+                if(areaInfo.getLevel().equals("street")){
+                     return;
+                }
+                SysRegion sysRegion = new SysRegion();
+                BeanUtils.copyProperties(areaInfo,sysRegion);
+                sysRegion.setParentAdcode(parentAdcode);
+                log.info("sysRegion",sysRegion);
+                sysRegionMapper.insert(sysRegion);
+                if(!ObjectUtils.isEmpty(areaInfo.getDistricts())
+                        && areaInfo.getDistricts().size()>0){
+                    recursiveInsertion(areaInfo.getDistricts(),areaInfo.getAdcode());
+                }
+            }
+        }
+    }
+}

+ 13 - 0
application-webadmin/src/main/java/com/ankaibei/workFlow/webadmin/utils/GaoDeWebUtil.java

@@ -4,6 +4,7 @@ import cn.hutool.http.HttpRequest;
 import cn.hutool.http.HttpUtil;
 import com.alibaba.fastjson.JSON;
 import com.ankaibei.workFlow.webadmin.config.ApplicationConfig;
+import com.ankaibei.workFlow.webadmin.upms.service.SysRegionService;
 import com.ankaibei.workflow.vo.AreaInfo;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -21,6 +22,10 @@ public class GaoDeWebUtil {
     @Autowired
     private ApplicationConfig applicationConfig;
 
+    @Autowired
+    private SysRegionService sysRegionService;
+
+    public  static   SysRegionService   regionService;
     public static String key = "1038f05a58704762e009c8e726226c26";
 
     public final static String areaPath = "https://restapi.amap.com/v3/config/district?keywords=%s&subdistrict=%d&key=%s";
@@ -28,6 +33,7 @@ public class GaoDeWebUtil {
     @PostConstruct
     private void init() {
         GaoDeWebUtil.key = applicationConfig.getGdKey();
+        GaoDeWebUtil.regionService = sysRegionService;
     }
 
 
@@ -100,6 +106,13 @@ public class GaoDeWebUtil {
         return null;
     }
 
+    // 初始化行政区域   - 每次调用前,先手动清空work_sys_region 表数据
+    public static void initRegion(){
+        List<AreaInfo> list = getInfoLikeKeywordsAndSubdistrict("中华人民共和国", 3);
+//        System.out.println(JSON.toJSONString(list));
+        regionService.insertAll(list);
+    }
+
     public static void main(String[] args) {
         List<AreaInfo> list = getInfoLikeKeywordsAndSubdistrict("中华人民共和国", 3);
 //        List<AreaInfo> list =  getProvinces();