You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
7.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace app\base\logic;
class District extends Base
{
/**
* 获取树状三级行政区划
* @date 2022-08-11
*/
public function listDistrictTree(){
$district_model = new \app\base\model\district\District();
// 查询有效三级行政区划
$district_list = $district_model -> listDistrict();
// 更改为树状数组
$district_list = listToTree($district_list);
return $district_list;
}
/**
* 更新全部省市区三级行政区划
* @date 2021-02-04
*/
function createDistrict()
{
exit; //一般不会更新,需要更新时注释掉该行代码 需要把三个表备份一下,截断后重新写入新数据
$map_param = get_tencent_map_config(2);
$district_model = new \app\base\model\district\District();
$tencent_map_class = new \tencent\map\District($map_param);
$res = $tencent_map_class->getDistrict();
$data = $res['result']; //全部省市区三级行政区划
//更新三级行政区划
$city_id = 100;//二级行政区域ID由101起始
$area_id = 1000;//三级行政区域ID由1001起始
foreach ($data[0] as $k1 => $v1) {
$province[] = [ //省级数据
'id' => $k1 + 1,
'level' => 1,
'pid' => '',
'adcode' => $v1['id'],
'brief_name' => $v1['name'],
'name' => $v1['fullname'],
'lat' => $v1['location']['lat'],
'lng' => $v1['location']['lng'],
'pinyin' => implode(' ', $v1['pinyin']),
'create_time' => time(),
'update_time' => time()
];
$city_data = array_slice($data[1], $v1['cidx'][0], $v1['cidx'][1] - $v1['cidx'][0] + 1); //截取对应的二级行政区域数据
//判断该省是否为直辖市、香港、澳门
$is_directly_city = true; //一级是否是直辖市、香港、澳门,即省市区只有二级行政区域没有三级行政区域数据(二级延用一级,三级延用二级)
$has_directly_area = false; //一级是否含有直辖县第3、4位为90的为省直辖县二级多一个"省直辖县级行政区划",该地区所有直辖县移至该二级三级)
foreach ($city_data as $k => $v) {
if (!empty($v['cidx'])) {
$is_directly_city = false;
}
if (substr($v['id'], 2, 2) == '90') {
$has_directly_area = true;
}
}
if ($is_directly_city) { //针对于直辖市、香港、澳门
$city[] = [
'id' => ++$city_id,
'level' => 2,
'pid' => $k1 + 1,
'adcode' => $v1['id'],
'brief_name' => $v1['name'],
'name' => $v1['fullname'],
'lat' => $v1['location']['lat'],
'lng' => $v1['location']['lng'],
'pinyin' => implode(' ', $v1['pinyin']),
'create_time' => time(),
'update_time' => time(),
];
foreach ($city_data as $k2 => $v2) {
$area[] = [
'id' => ++$area_id,
'level' => 3,
'pid' => $city_id,
'adcode' => $v2['id'],
'brief_name' => $v2['name'],
'name' => $v2['fullname'],
'lat' => $v2['location']['lat'],
'lng' => $v2['location']['lng'],
'pinyin' => implode(' ', $v2['pinyin']),
'create_time' => time(),
'update_time' => time(),
];
}
} else {
$directly_area_id = 0;
foreach ($city_data as $k2 => $v2) {
if (substr($v2['id'], 2, 2) == '90') {
if (!$directly_area_id) {
$city[] = [
'id' => ++$city_id,
'level' => 2,
'pid' => $k1 + 1,
'adcode' => '',
'brief_name' => '省直辖县',
'name' => '省直辖县级行政区划',
'lat' => '',
'lng' => '',
'pinyin' => 'sheng zhi xia xian',
'create_time' => time(),
'update_time' => time(),
];
$directly_area_id = $city_id;
}
$area[] = [
'id' => ++$area_id,
'level' => 3,
'pid' => $directly_area_id,
'adcode' => $v2['id'],
'brief_name' => $v2['name'],
'name' => $v2['fullname'],
'lat' => $v2['location']['lat'],
'lng' => $v2['location']['lng'],
'pinyin' => implode(' ', $v2['pinyin']),
'create_time' => time(),
'update_time' => time(),
];
} else {
$city[] = [
'id' => ++$city_id,
'level' => 2,
'pid' => $k1 + 1,
'adcode' => $v2['id'],
'brief_name' => $v2['name'],
'name' => $v2['fullname'],
'lat' => $v2['location']['lat'],
'lng' => $v2['location']['lng'],
'pinyin' => implode(' ', $v2['pinyin']),
'create_time' => time(),
'update_time' => time(),
];
$area_data = array_slice($data[2], $v2['cidx'][0], $v2['cidx'][1] - $v2['cidx'][0] + 1); //截取对应的三级行政区域数据
foreach ($area_data as $k3 => $v3) {
$area[] = [
'id' => ++$area_id,
'level' => 3,
'pid' => $city_id,
'adcode' => $v3['id'],
'brief_name' => '',
'name' => $v3['fullname'],
'lat' => $v3['location']['lat'],
'lng' => $v3['location']['lng'],
'pinyin' => '',
'create_time' => time(),
'update_time' => time(),
];
}
}
}
}
}
//合并省市区数据
$district_data = array_merge($province, $city, $area);
$district_model->insertAll($district_data);
echo 'success';
}
}