package com.boman.web.core.controller;

import com.alibaba.fastjson.JSONObject;
import com.boman.common.core.utils.obj.ObjectUtils;
import com.boman.domain.Czrk;
import com.boman.domain.CzrkJzdz;
import com.boman.domain.SysDept;
import com.boman.domain.VaccineInfoOperation;
import com.boman.domain.dto.AjaxResult;
import com.boman.web.core.domain.SysRegion;
import com.boman.web.core.mapper.*;
import com.boman.web.core.service.vaccineInfo.IVaccineInfoService;
import com.boman.web.core.service.vaccineInfo.impl.VaccineInfoServiceImpl;
import com.boman.web.core.utils.IdUtils;
import com.google.common.base.Strings;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author shiqian
 * @date 2021年09月08日 10:40
 **/
@RestController
@RequestMapping("/test")
public class TestController {
    @Resource
    private VaccineInfoMapper mapper;

    @Autowired
    private SysRegionMapper sysRegionMapper;

    @Autowired
    private CzrkMapper czrkMapper;

    @Autowired
    private CzrkJzdzMapper czrkJzdzMapper;


    /**
     * 对外处理已确认但是丢失了乡镇信息的接口
     */
    //@PostMapping("/lost")
    public AjaxResult lost()
    {
        List<Czrk> czrks = czrkMapper.selectLost();
        if (czrks != null && czrks.size() > 0){
            for (Czrk czrk : czrks) {
                if (czrk != null){
                    String nowIn = czrk.getNowIn();
                    if (com.boman.common.core.utils.StringUtils.isNotBlank(nowIn)){
                        czrk.setProvince("安徽省");
                        czrk.setProvinceId("12");
                        czrk.setCity("安庆市");
                        czrk.setCityId("340800000000");
                        czrk.setRegionId("340882000000");
                        czrk.setRegion("潜山市");
                        //判断有没有详细地址
                        if (nowIn.contains("梅城镇")){
                            czrk.setVillageTowns("梅城镇");
                            czrk.setVillageTownsId("340882100000");
                        }else {
                            Map<String, String> addressResolutionMap = addressResolutionMap(nowIn);
                            System.out.println(addressResolutionMap);
                            if (addressResolutionMap != null){
                                String town = addressResolutionMap.get("town");
                                //去china_area表中查询对应数据
                                List<SysRegion> sysRegions = sysRegionMapper.selectSysRegionByNameLike(town);
                                if (sysRegions.size() == 1) {
                                    SysRegion sysRegion = sysRegions.get(0);
                                    if (sysRegion != null) {
                                        czrk.setVillageTowns(sysRegion.getName());
                                        czrk.setVillageTownsId(sysRegion.getAreaId());
                                    }
                                } else if (sysRegions.size() > 1) {
                                    for (SysRegion sysRegion : sysRegions) {
                                        if (sysRegion != null) {
                                            String pid = sysRegion.getPid();
                                            String regionId = czrk.getRegionId();
                                            if (regionId != null && regionId.equals(pid)) {
                                                //说明是这个镇
                                                czrk.setVillageTowns(sysRegion.getName());
                                                czrk.setVillageTownsId(sysRegion.getAreaId());
                                            }
                                        }
                                    }
                                }
                                String village = addressResolutionMap.get("village");
                                if (StringUtils.isNotBlank(village)){
                                    //去china_area表中查询对应数据
                                    List<SysRegion> sysRegionsVillage = sysRegionMapper.selectSysRegionByNameLike(village);
                                    if (sysRegionsVillage.size() == 1) {
                                        SysRegion sysRegion = sysRegionsVillage.get(0);
                                        if (sysRegion != null) {
                                            czrk.setVillage(sysRegion.getName());
                                            czrk.setVillageId(sysRegion.getAreaId());
                                        }
                                    } else if (sysRegionsVillage.size() > 1) {
                                        for (SysRegion sysRegion : sysRegionsVillage) {
                                            if (sysRegion != null) {
                                                String pid = sysRegion.getPid();
                                                String villageTownsId = czrk.getVillageTownsId();
                                                if (villageTownsId != null && villageTownsId.equals(pid)) {
                                                    //说明是这个镇下面的村
                                                    czrk.setVillage(sysRegion.getName());
                                                    czrk.setVillageId(sysRegion.getAreaId());
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        czrkMapper.updateCzrk(czrk);
                        System.out.println("更新了:"+czrk.getIdCard());
                    }
                }
            }
        }
        return AjaxResult.success();
    }

    @GetMapping("/handleData")
    public AjaxResult handleData() {
        List<VaccineInfoOperation> list = mapper.handleData();
        for (VaccineInfoOperation vaccineInfoOperation : list) {
            if (Objects.isNull(vaccineInfoOperation.getAge()) && ObjectUtils.isEmpty(vaccineInfoOperation.getBirthday())) {
                continue;
            }

            mapper.updateAgeBirthday(vaccineInfoOperation);
        }

        return new AjaxResult();
    }

    @GetMapping("/setHouseType")
    public AjaxResult setHouseType() {
        List<JSONObject> list = mapper.getHouseType();
        for (JSONObject json : list) {
            String idCard = json.getString("idCard");
            String yhzgx = json.getString("yhzgx");// 与户主关系
            String code = json.getString("code"); // 户号

            String houseType = "", yhzgx1 = "";
            if (StringUtils.isNotEmpty(yhzgx)) {
                yhzgx = yhzgx.trim();
                if ("户主".equals(yhzgx)) {
                    houseType = "是";
                    yhzgx1 = "户主";
                } else {
                    houseType = "否";
                    yhzgx1 = yhzgx;
                }
            }

            mapper.setHouseType(idCard, code, yhzgx1, houseType);
        }

        return new AjaxResult();
    }

    /**
     * 处理常驻人口数据
     *
     * @return
     */
    //@PostMapping("/setCzrk")
    public AjaxResult setCzrk() {
        //587731
        for (int i = 0; i < 59; i++) {
            int startPage = i * 10000;
            int endPage = 10000;
            List<VaccineInfoOperation> vaccineInfoOperations = mapper.selectVaccineInfoListByPage(startPage, endPage);
            if (vaccineInfoOperations.size() > 0) {
                for (VaccineInfoOperation vaccineInfoOperation : vaccineInfoOperations) {
                    if (vaccineInfoOperation != null) {
                        Czrk czrk = new Czrk();
                        String userName = vaccineInfoOperation.getUserName();
                        if (!userName.contains("测试") && userName.length() < 6) {
                            czrk.setUserName(userName);
                            czrk.setGender(vaccineInfoOperation.getGender());
                            czrk.setAge(vaccineInfoOperation.getAge());
                            czrk.setBirthday(vaccineInfoOperation.getBirthday());
                            czrk.setDeptId(vaccineInfoOperation.getDeptId());
                            czrk.setIdCard(vaccineInfoOperation.getIdCard());
                            czrk.setPhoneNum(vaccineInfoOperation.getPhoneNum());
                            czrk.setCode(vaccineInfoOperation.getCode());
                            czrk.setHouseType(vaccineInfoOperation.getHouseType());
                            czrk.setRlr(vaccineInfoOperation.getRlr());
                            czrk.setYhzgx(vaccineInfoOperation.getYhzgx());
                            czrk.setWorkUnit(vaccineInfoOperation.getWorkUnit());
                            //户籍地镇
                            String villageTowns = vaccineInfoOperation.getVillageTowns();
                            if (StringUtils.isNotBlank(villageTowns)) {
                                czrk.setIsRl("是");
                                List<SysRegion> sysRegions = sysRegionMapper.selectSysRegionByName(villageTowns);
                                if (sysRegions.size() > 0) {
                                    for (SysRegion sysRegion : sysRegions) {
                                        if (sysRegion != null) {
                                            String pid = sysRegion.getPid();
                                            if (pid != null) {
                                                if ("340882000000".equals(pid)) {
                                                    czrk.setVillageTownsId(sysRegion.getAreaId());
                                                    czrk.setVillageTowns(villageTowns);
                                                }
                                            }

                                        }
                                    }
                                }
                            }
                            String villager = vaccineInfoOperation.getVillage();
                            if (StringUtils.isNotBlank(villager)) {
                                List<SysRegion> sysRegions = sysRegionMapper.selectSysRegionByName(villager);
                                if (sysRegions.size() > 1) {
                                    for (SysRegion sysRegion : sysRegions) {
                                        if (sysRegion != null) {
                                            String villageTownsId = czrk.getVillageTownsId();
                                            if (villageTownsId != null && villageTownsId.equals(sysRegion.getPid())) {
                                                czrk.setVillageId(sysRegion.getAreaId());
                                                czrk.setVillage(villager);
                                            }
                                        }
                                    }
                                } else if (sysRegions.size() == 1) {
                                    SysRegion sysRegion = sysRegions.get(0);
                                    if (sysRegion != null) {
                                        czrk.setVillageId(sysRegion.getAreaId());
                                        czrk.setVillage(villager);
                                    }
                                }
                            }
                            czrk.setIsDel(vaccineInfoOperation.getIsDel());
                            czrk.setDeleteReason(vaccineInfoOperation.getDeleteReason());
                            czrk.setNowIn(vaccineInfoOperation.getDomicile());
                            czrk.setRemark(vaccineInfoOperation.getRemark());
                            czrk.setCreateBy("admin");
                            czrkMapper.insertCzrk(czrk);

                            CzrkJzdz czrkJzdz = new CzrkJzdz();
                            czrkJzdz.setCzrkId(czrk.getId());
                            czrkJzdz.setIdCard(vaccineInfoOperation.getIdCard());
                            czrkJzdz.setUserName(vaccineInfoOperation.getUserName());
                            czrkJzdz.setDeptId(vaccineInfoOperation.getDeptId());
                            //现在居住地省
                            String province = vaccineInfoOperation.getProvince();
                            if (StringUtils.isNotBlank(province)) {
                                List<SysRegion> sysRegions = sysRegionMapper.selectSysRegionByName(province);
                                if (sysRegions.size() > 0) {
                                    SysRegion sysRegion = sysRegions.get(0);
                                    if (sysRegion != null) {
                                        czrkJzdz.setProvinceId(sysRegion.getAreaId().toString());
                                        czrkJzdz.setProvince(province);
                                    }

                                }
                            }
                            //现在居住地市
                            String city = vaccineInfoOperation.getCity();
                            if (StringUtils.isNotBlank(city)) {
                                List<SysRegion> sysRegions = sysRegionMapper.selectSysRegionByName(city);
                                if (sysRegions.size() > 0) {
                                    SysRegion sysRegion = sysRegions.get(0);
                                    if (sysRegion != null) {
                                        czrkJzdz.setCityId(sysRegion.getAreaId().toString());
                                        czrkJzdz.setCity(city);
                                    }
                                }
                            }

                            //现在居住地区
                            String region = vaccineInfoOperation.getRegion();
                            if (StringUtils.isNotBlank(region)) {
                                List<SysRegion> sysRegions = sysRegionMapper.selectSysRegionByName(region);
                                //如果有多个区,名字相同的,切没有市的信息,则不赋值
                                if (sysRegions.size() > 1) {
                                    for (SysRegion sysRegion : sysRegions) {
                                        if (sysRegion != null) {
                                            String cityId = czrkJzdz.getCityId();
                                            if (StringUtils.isNotBlank(cityId)){
                                                if (cityId.equals(sysRegion.getPid())) {
                                                    czrkJzdz.setRegionId(sysRegion.getAreaId().toString());
                                                    czrkJzdz.setRegion(region);
                                                }
                                            }
                                        }
                                    }
                                } else if (sysRegions.size() == 1) {
                                    SysRegion sysRegion = sysRegions.get(0);
                                    if (sysRegion != null) {
                                        czrkJzdz.setRegionId(sysRegion.getAreaId().toString());
                                        czrkJzdz.setRegion(region);
                                    }
                                }
                            }

                            //居住地的镇
                            String residenceTown = vaccineInfoOperation.getResidenceTown();
                            if (StringUtils.isNotBlank(residenceTown)) {
                                List<SysRegion> sysRegions = sysRegionMapper.selectSysRegionByName(residenceTown);
                                if (sysRegions.size() > 1) {
                                    for (SysRegion sysRegion : sysRegions) {
                                        if (sysRegion != null) {
                                            Long regionId = Long.parseLong(czrkJzdz.getRegionId());
                                            if ( regionId.equals(Long.parseLong(sysRegion.getPid()))) {
                                                czrkJzdz.setTownId(sysRegion.getAreaId().toString());
                                                czrkJzdz.setTown(residenceTown);
                                            }
                                        }
                                    }
                                } else if (sysRegions.size() == 1) {
                                    SysRegion sysRegion = sysRegions.get(0);
                                    if (sysRegion != null) {
                                        czrkJzdz.setTownId(sysRegion.getAreaId().toString());
                                        czrkJzdz.setTown(residenceTown);
                                    }
                                }
                            }

                            //居住地的村
                            String residenceVillage = vaccineInfoOperation.getResidenceVillage();
                            if (StringUtils.isNotBlank(residenceVillage)) {
                                List<SysRegion> sysRegions = sysRegionMapper.selectSysRegionByName(residenceVillage);
                                if (sysRegions.size() > 1) {
                                    for (SysRegion sysRegion : sysRegions) {
                                        if (sysRegion != null) {
                                            String townId = czrkJzdz.getTownId();
                                            if (townId != null && townId.equals(sysRegion.getPid())) {
                                                czrkJzdz.setVillageId(sysRegion.getAreaId().toString());
                                                czrkJzdz.setVillage(residenceVillage);
                                            }
                                        }
                                    }
                                } else if (sysRegions.size() == 1) {
                                    SysRegion sysRegion = sysRegions.get(0);
                                    if (sysRegion != null) {
                                        czrkJzdz.setVillageId(String.valueOf(sysRegions.get(0).getAreaId()));
                                        czrkJzdz.setVillage(residenceVillage);
                                    }
                                }
                            }
                            czrkJzdz.setNowIn(vaccineInfoOperation.getNowIn());
                            czrkJzdz.setCreateBy("admin");
                            czrkJzdzMapper.insertCzrkJzdz(czrkJzdz);
                        }
                    }
                }
            }
            System.out.println("处理完成" + startPage + "数据");
        }
        return AjaxResult.success("全部完成");
    }


    /**
     * 处理常驻人口数据村信息
     *
     * @return
     */
    //@PostMapping("/setCzrkTown")
    public AjaxResult setCzrkTown() {
        for (int i = 0; i < 46; i++) {
            int startPage = i * 10000;
            int endPage = 10000;
            List<Czrk> czrks = czrkMapper.selectCzrkListByPage(startPage, endPage);
            if (czrks.size() > 0) {
                for (Czrk czrk : czrks) {
                    if (czrk != null) {
                        String nowIn = czrk.getNowIn();
                        Long deptId = czrk.getDeptId();
                        //有居住地址
                        if (StringUtils.isNotBlank(nowIn)) {
                            //提取出村的名称
                            String village = addressResolution(nowIn);
                            if (StringUtils.isNotBlank(village)) {
                                //去china_area表中查询对应数据
                                List<SysRegion> sysRegions = sysRegionMapper.selectSysRegionByNameLike(village);
                                if (sysRegions.size() == 1) {
                                    SysRegion sysRegion = sysRegions.get(0);
                                    if (sysRegion != null) {
                                        czrk.setVillage(sysRegion.getName());
                                        czrk.setVillageId(sysRegion.getAreaId());
                                    }
                                } else if (sysRegions.size() > 1) {
                                    for (SysRegion sysRegion : sysRegions) {
                                        if (sysRegion != null) {
                                            String pid = sysRegion.getPid();
                                            String villageTownsId = czrk.getVillageTownsId();
                                            if (villageTownsId != null && String.valueOf(villageTownsId).equals(pid)) {
                                                //说明是这个镇下面的村
                                                czrk.setVillage(sysRegion.getName());
                                                czrk.setVillageId(sysRegion.getAreaId());
                                            }
                                        }
                                    }
                                }
                            }
                        } else {
                            //没有居住地址看看有没有deptId
                            if (deptId != null) {
                                //去sys_dept表中找到对应的数据
                                SysDept sysDept = czrkMapper.selectDeptNameById(deptId);
                                if (sysDept != null) {
                                    String ancestors = sysDept.getAncestors();
                                    String[] split = ancestors.split(",");
                                    if (split.length == 3) {
                                        //说明是组,则找上级村id
                                        Long parentId = sysDept.getParentId();
                                        if (parentId != null) {
                                            //获取到村名称
                                            SysDept sysDeptParent = czrkMapper.selectDeptNameById(parentId);
                                            if (sysDeptParent != null) {
                                                //村名称
                                                String deptName = sysDeptParent.getDeptName();
                                                if (StringUtils.isNotBlank(deptName)) {
                                                    //去china_area表中查询对应数据
                                                    List<SysRegion> sysRegions = sysRegionMapper.selectSysRegionByNameLike(deptName);
                                                    if (sysRegions.size() == 1) {
                                                        SysRegion sysRegion = sysRegions.get(0);
                                                        if (sysRegion != null) {
                                                            czrk.setVillage(sysRegion.getName());
                                                            czrk.setVillageId(sysRegion.getAreaId());
                                                        }
                                                    } else if (sysRegions.size() > 1) {
                                                        for (SysRegion sysRegion : sysRegions) {
                                                            if (sysRegion != null) {
                                                                String pid = sysRegion.getPid();
                                                                String villageTownsId = czrk.getVillageTownsId();
                                                                if (villageTownsId != null && villageTownsId.equals(pid)) {
                                                                    //说明是这个镇下面的村
                                                                    czrk.setVillage(sysRegion.getName());
                                                                    czrk.setVillageId(sysRegion.getAreaId());
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    if (split.length == 2) {
                                        //说明是村
                                        String deptName = sysDept.getDeptName();
                                        //村名称
                                        if (StringUtils.isNotBlank(deptName)) {
                                            //去china_area表中查询对应数据
                                            List<SysRegion> sysRegions = sysRegionMapper.selectSysRegionByNameLike(deptName);
                                            if (sysRegions.size() == 1) {
                                                SysRegion sysRegion = sysRegions.get(0);
                                                if (sysRegion != null) {
                                                    czrk.setVillage(sysRegion.getName());
                                                    czrk.setVillageId(sysRegion.getAreaId());
                                                }
                                            } else if (sysRegions.size() > 1) {
                                                for (SysRegion sysRegion : sysRegions) {
                                                    if (sysRegion != null) {
                                                        String pid = sysRegion.getPid();
                                                        String villageTownsId = czrk.getVillageTownsId();
                                                        if (villageTownsId != null && villageTownsId.equals(pid)) {
                                                            //说明是这个镇下面的村
                                                            czrk.setVillage(sysRegion.getName());
                                                            czrk.setVillageId(sysRegion.getAreaId());
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        //村名称
                        String village = czrk.getVillage();
                        String villageId = czrk.getVillageId();
                        String idCard = czrk.getIdCard();
                        if (StringUtils.isNotBlank(village)) {
                            Czrk czrkNew = new Czrk();
                            czrkNew.setId(czrk.getId());
                            czrkNew.setVillageId(villageId);
                            czrkNew.setVillage(village);
                            czrkMapper.updateCzrk(czrkNew);
                            System.out.println("更新编号" + czrk.getId() + village);
                        }
                        CzrkJzdz czrkJzdz = new CzrkJzdz();

                        if (StringUtils.isNotBlank(village) && StringUtils.isNotBlank(idCard)) {
                            //更新czrk_jzdz
                            czrkJzdz.setVillage(village);
                            czrkJzdz.setVillageId(String.valueOf(villageId));
                        } else {
                            System.out.println(czrk.getId() + "村名称或者身份证号码不存在,没有更新czrk_jzdz");
                        }
                        if (StringUtils.isNotBlank(idCard)) {
                            czrkJzdz.setTownId(String.valueOf(czrk.getVillageTownsId()));
                            czrkJzdz.setTown(czrk.getVillageTowns());
                            czrkJzdz.setIdCard(idCard);
                            czrkJzdzMapper.updateByIdCard(czrkJzdz);
                        }
                    }
                }
            }
            System.out.println("更新完成");
        }
        return AjaxResult.success();
    }


    /**
     * 处理常驻人口居住地址的数据
     *
     * @return
     */
    @PostMapping("/setCzrkJzdz")
    public AjaxResult setCzrkJzdz() {
        for (int i = 0; i < 10; i++) {
            int startPage = 0;
            int endPage = 10000;
            List<CzrkJzdz> czrkJzdzs = czrkJzdzMapper.selectCzrkJzdzListPage(startPage, endPage);
            if (czrkJzdzs.size() > 0) {
                for (CzrkJzdz czrkJzdz : czrkJzdzs) {
                    if (czrkJzdz != null) {
                        String nowIn = czrkJzdz.getNowIn();
                        if (StringUtils.isNotBlank(nowIn)) {
                            Map<String, String> stringStringMap = addressResolutionMap(nowIn);
                            if (stringStringMap != null && stringStringMap.size() > 0) {
                                //镇
                                String town = stringStringMap.get("town");
                                if (StringUtils.isNotBlank(town)) {
                                    //去查询对应镇的id
                                    List<SysRegion> sysRegions = sysRegionMapper.selectSysRegionByNameLike(town);
                                    if (sysRegions.size() == 1) {
                                        SysRegion sysRegion = sysRegions.get(0);
                                        if (sysRegion != null) {
                                            String name = sysRegion.getName();
                                            String areaId = sysRegion.getAreaId();
                                            czrkJzdz.setTown(name);
                                            czrkJzdz.setTownId(String.valueOf(areaId));
                                        }
                                    }
                                    if (sysRegions.size() > 1) {
                                        for (SysRegion sysRegion : sysRegions) {
                                            String pid = sysRegion.getPid();
                                            if ("340882000000".equals(pid)) {
                                                String name = sysRegion.getName();
                                                String areaId = sysRegion.getAreaId();
                                                czrkJzdz.setTown(name);
                                                czrkJzdz.setTownId(areaId);
                                            }
                                        }
                                    }
                                }
                                //村
                                String village = stringStringMap.get("village");
                                if (StringUtils.isNotBlank(village)) {
                                    village = village.replace("-", "");
                                    List<SysRegion> sysRegions = sysRegionMapper.selectSysRegionByNameLike(village);
                                    if (sysRegions.size() == 1) {
                                        SysRegion sysRegion = sysRegions.get(0);
                                        if (sysRegion != null) {
                                            String name = sysRegion.getName();
                                            String areaId = sysRegion.getAreaId();
                                            czrkJzdz.setVillage(name);
                                            czrkJzdz.setVillageId(areaId);
                                        }
                                    }
                                    if (sysRegions.size() > 1) {
                                        for (SysRegion sysRegion : sysRegions) {
                                            String pid = sysRegion.getPid();
                                            if (String.valueOf(czrkJzdz.getTownId()).equals(pid)) {
                                                String name = sysRegion.getName();
                                                String areaId = sysRegion.getAreaId();
                                                czrkJzdz.setVillage(name);
                                                czrkJzdz.setVillageId(areaId);
                                            }
                                        }
                                    }
                                }
                                if (StringUtils.isNotBlank(town)) {
                                    CzrkJzdz cx = new CzrkJzdz();
                                    cx.setCzrkId(czrkJzdz.getCzrkId());
                                    cx.setTownId(czrkJzdz.getTownId());
                                    cx.setTown(czrkJzdz.getTown());
                                    if (czrkJzdz.getVillage() != null) {
                                        cx.setVillageId(czrkJzdz.getVillageId());
                                        cx.setVillage(czrkJzdz.getVillage());
                                    }
                                    czrkJzdzMapper.updateByCzrkId(cx);
                                    System.out.println("更新" + czrkJzdz.getCzrkId());
                                }
                            }
                        }
                    }
                }
            }
        }
        return AjaxResult.success();
    }

    /**
     * 解析地址
     *
     * @param address
     * @return
     * @author lin
     */
    public static String addressResolution(String address) {
        String regex = "(?<town>[^区]+区|.+镇|.+乡)?(?<village>.[^村]+村|.+居委会|.+乡)";
        Matcher m = Pattern.compile(regex).matcher(address);
        String province = null, city = null, county = null, town = null, village = null;
        List<Map<String, String>> table = new ArrayList<Map<String, String>>();
        Map<String, String> row = null;
        String result = null;
        while (m.find()) {
/*            row=new LinkedHashMap<String,String>();
            town=m.group("town");
            row.put("town", town==null?"":town.trim());
            village=m.group("village");
            row.put("village", village==null?"":village.trim());
            table.add(row);*/
            village = m.group("village");
            result = village == null ? "" : village.trim();
        }
        return result;
    }

    public static Map<String, String> addressResolutionMap(String address) {
        String regex = "(?<town>[^区]+区|.+镇|.+乡)?(?<village>.[^村]+村|.+村委会|.+乡)";
        Matcher m = Pattern.compile(regex).matcher(address);
        String province = null, city = null, county = null, town = null, village = null;
        List<Map<String, String>> table = new ArrayList<Map<String, String>>();
        Map<String, String> row = null;
        String result = null;
        if (m.find()) {
            row = new LinkedHashMap<String, String>();
            town = m.group("town");
            row.put("town", town == null ? "" : town.trim());
            village = m.group("village");
            row.put("village", village == null ? "" : village.trim());
            table.add(row);
        }
        return row;
    }

}
