Browse Source

area

develop
李可松 5 years ago
parent
commit
efcd9c85ec
  1. 29
      app/Http/Controllers/Api/TestController.php
  2. 3211
      area.txt

29
app/Http/Controllers/Api/TestController.php

@ -5,6 +5,7 @@ namespace App\Http\Controllers\Api;
use App\Models\Order;
use App\Models\OrderProductItem;
use App\Models\Product;
use Illuminate\Support\Facades\DB;
/**
* 仅用于测试
@ -13,7 +14,7 @@ use App\Models\Product;
*/
class TestController
{
public function index()
/*public function index()
{
//test
$ids = Order::orderBy('id')->get(['id AS order_id', 'product_ids', 'agent_id', 'agent_product_id']);
@ -30,13 +31,13 @@ class TestController
}
}
return '';
}
}*/
public function area()
public function index()
{
$handle = fopen(Env::get('root_path').'2020-11行政区划(民政部).txt', 'r');
$handle = fopen(base_path('area.txt'), 'r');
if ($handle) {
Db::execute('TRUNCATE `aqs_area`;');
DB::statement('TRUNCATE `areas`;');
while (($line = fgets($handle, 4096)) !== false) {
$line = trim($line);
@ -45,8 +46,8 @@ class TestController
$front4 = substr($line, 0, 4); //区划码前4位
//判断是否是直辖市,是直辖市则再插入一次上条记录作为二级联动
if( isset($last4, $arr, $parent_arr[$front2]) && $last4=='0000' && $last2!='00' ) {
$insert_id = Db::name('area')->insertGetId(['area_code'=>$arr[0], 'parent_id'=>$parent_arr[$front2], 'name'=>$arr[1]]);
if (isset($last4, $arr, $parent_arr[$front2]) && $last4 == '0000' && $last2 != '00') {
$insert_id = DB::table('areas')->insertGetId(['code' => $arr[0], 'pid' => $parent_arr[$front2], 'name' => $arr[1]]);
$parent_arr[$front4] = $insert_id;
//对直辖市不做省直辖县处理,如:重庆市
$parent_arr[$front2] = $insert_id;
@ -55,17 +56,21 @@ class TestController
$last4 = substr($line, 2, 4); //区划码后4位
$arr = preg_split('/\s+/', $line);
if( $last4 == '0000' ) {
$insert_id = Db::name('area')->insertGetId(['area_code'=>$arr[0], 'parent_id'=>0, 'name'=>$arr[1]]);
if (count($arr) !== 2) continue;
if ($last4 == '0000') {
$insert_id = DB::table('areas')->insertGetId(['code' => $arr[0], 'pid' => 0, 'name' => $arr[1]]);
$parent_arr[$front2] = $insert_id;
} else if( $last2 == '00' ) {
$insert_id = Db::name('area')->insertGetId(['area_code'=>$arr[0], 'parent_id'=>$parent_arr[$front2], 'name'=>$arr[1]]);
} else if ($last2 == '00') {
$insert_id = DB::table('areas')->insertGetId(['code' => $arr[0], 'pid' => $parent_arr[$front2], 'name' => $arr[1]]);
$parent_arr[$front4] = $insert_id;
} else {
//考虑到省直辖县级市情况,如:海南琼海市、湖北仙桃市等,但重庆市的省辖县除外(已在上面判断直辖市逻辑中做处理)
$parent_id = $parent_arr[$front4] ?? $parent_arr[$front2];
Db::name('area')->insertGetId(['area_code'=>$arr[0], 'parent_id'=>$parent_id, 'name'=>$arr[1]]);
DB::table('areas')->insertGetId(['code' => $arr[0], 'pid' => $parent_id, 'name' => $arr[1]]);
}
}
} else {
return 'open file fail!';
}
return 'success';
}}

3211
area.txt
File diff suppressed because it is too large
View File

Loading…
Cancel
Save