| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300 | 
							- <?php
 
- namespace App\Console\Commands;
 
- use App\Model\AreaMap;
 
- use App\Model\BasicType;
 
- use App\Model\BasicTypeAllUse;
 
- use App\Model\Customer;
 
- use App\Model\CustomerInfo;
 
- use App\Model\CustomerReport;
 
- use App\Model\CustomerReportDepart;
 
- use App\Model\FollowUpRecord;
 
- use App\Model\SalesOrder;
 
- use App\Model\SeeRange;
 
- use Illuminate\Console\Command;
 
- use Illuminate\Support\Facades\DB;
 
- class CustomerWriteReport extends Command
 
- {
 
-     /**
 
-      * The name and signature of the console command.
 
-      *
 
-      * @var string
 
-      */
 
-     protected $signature = 'command:customer_write_report';
 
-     /**
 
-      * The console command description.
 
-      *
 
-      * @var string
 
-      */
 
-     protected $description = 'Command description';
 
-     /**
 
-      * Create a new command instance.
 
-      *
 
-      * @return void
 
-      */
 
-     public function __construct()
 
-     {
 
-         parent::__construct();
 
-     }
 
-     /**
 
-      * Execute the console command.
 
-      *
 
-      * @return mixed
 
-      */
 
-     public function handle()
 
-     {
 
-         echo "执行任务--------start---------------\n";
 
-         try {
 
-             $this->customerWrite();
 
-         }catch (\Throwable $exception){
 
-             echo "发生异常:" . $exception->getFile() . "|" . $exception->getMessage() . "|" . $exception->getLine() . "\n";
 
-         }
 
-         echo "执行任务--------end---------------\n";
 
-     }
 
-     private function customerWrite(){
 
-         //今天到上月今天的时间戳
 
-         $nowStamp = time();
 
-         $lastMonth = date("Y-m-d 00:00:00", strtotime("-1 month"));
 
-         $lastMonthStamp = strtotime($lastMonth);
 
-         list($provinceMap, $cityMap, $countyMap) = $this->constructAddress();
 
- //        dd($provinceMap, $cityMap, $countyMap);
 
- //        dd(count($provinceMap), count($cityMap), count($countyMap));
 
- //        $this->forInsertArea($provinceMap, $cityMap, $countyMap);dd(122);
 
-         Customer::where("del_time",0)
 
-             ->where('enter_time','>=', $lastMonthStamp)
 
-             ->where('enter_time','<=', $nowStamp)
 
-             ->orderBy('id')
 
-             ->select('id','title','model_type','consulting_product_new','enter_time','address2','top_depart_id','customer_from','car_type')
 
-             ->chunkById(200, function ($data) use($provinceMap, $cityMap, $countyMap,$nowStamp){
 
-                 // 开启事务
 
-                 DB::transaction(function () use ($data, $provinceMap, $cityMap, $countyMap, $nowStamp) {
 
-                     $dataArray = $data->toArray();
 
-                     foreach ($dataArray as $key => $value){
 
-                         $dataArray[$key]['customer_id'] = $value['id'];
 
-                         unset($dataArray[$key]['id']);
 
-                     }
 
-                     list($main, $main_depart) = $this->processingData($dataArray, $provinceMap, $cityMap, $countyMap, $nowStamp);
 
-                     if (! empty($main)) {
 
-                         $customer_ids = array_column($dataArray, 'customer_id');
 
-                         // 删除旧数据
 
-                         CustomerReport::where('del_time', 0)
 
-                             ->whereIn('customer_id', $customer_ids)
 
-                             ->update(['del_time' => $nowStamp]);
 
-                         CustomerReportDepart::where('del_time', 0)
 
-                             ->whereIn('customer_id', $customer_ids)
 
-                             ->update(['del_time' => $nowStamp]);
 
-                         // 写入新数据
 
-                         CustomerReport::insert($main);
 
-                         CustomerReportDepart::insert($main_depart);
 
-                         echo '200条写入中' . PHP_EOL;
 
-                     } else {
 
-                         echo '暂无数据写入' . PHP_EOL;
 
-                     }
 
-                 });
 
-             });
 
-     }
 
-     private function processingData($data,$provinceMap,$cityMap,$countyMap,$time){
 
-         $return = [];
 
-         $customer_id = array_column($data,'customer_id');
 
-         $range = SeeRange::where('del_time',0)
 
-             ->whereIn('data_id', $customer_id)
 
-             ->where('data_type',SeeRange::type_one)
 
-             ->where('type',SeeRange::data_three)
 
-             ->select('data_id as customer_id','param_id as top_depart_id')
 
-             ->get()->toArray();
 
-         $map = [];
 
-         foreach ($range as $value){
 
-             $map[$value['customer_id']][] = $value['top_depart_id'];
 
-         }
 
-         $isset_map = SalesOrder::where('del_time', 0)
 
-             ->whereIn('customer_id', $customer_id)
 
-             ->pluck('customer_id')
 
-             ->unique()
 
-             ->flip()
 
-             ->map(fn() => 1)
 
-             ->toArray();
 
-         // 最新的跟进记录
 
-         $latestRecordsArray = $detail_insert = [];
 
-         $latestRecords = FollowUpRecord::whereIn('data_id', $customer_id)
 
-             ->where('type', FollowUpRecord::type_one)
 
-             ->where('del_time',0)
 
-             ->select('data_id','follow_type','crt_id')
 
-             ->orderBy('id','desc')
 
-             ->get()->toArray();
 
-         foreach ($latestRecords as $value){
 
-             if(! isset($latestRecordsArray[$value['data_id']])){
 
-                 $latestRecordsArray[$value['data_id']] = [
 
-                     'follow_type' => $value['follow_type'],
 
-                     'num' => 1,
 
-                 ];
 
-             }else{
 
-                 $latestRecordsArray[$value['data_id']]['num'] += 1;
 
-             }
 
-             $detail_insert[] = [
 
-                 'customer_id' => $value['data_id'],
 
-                 'top_depart_id' => 0,
 
-                 'crt_time' => $time,
 
-                 'type' => CustomerReportDepart::type_three,
 
-                 'man_id' => $value['crt_id'],
 
-             ];
 
-         }
 
-         $customer_info = CustomerInfo::where('del_time',0)
 
-             ->whereIn('customer_id',$customer_id)
 
-             ->whereIn('type',[CustomerInfo::type_one, CustomerInfo::type_two])
 
-             ->select('type','contact_type','contact_info','customer_id','data_id')
 
-             ->get()->toArray();
 
-         $customer_info_map = $customer_info_map2 = [];
 
-         foreach ($customer_info as $value){
 
-             if($value['type'] == CustomerInfo::type_one){
 
-                 if(! $value['contact_info']) continue;
 
-                 if(! empty($customer_info_map[$value['customer_id']])) continue;
 
-                 $customer_info_map[$value['customer_id']] = $value['contact_info'];
 
-             }else{
 
-                 if(! $value['data_id']) continue;
 
-                 if(! empty($customer_info_map2[$value['customer_id']]) && in_array($value['data_id'], $customer_info_map2[$value['customer_id']])) continue;
 
-                 $customer_info_map2[$value['customer_id']][] = $value['data_id'];
 
-             }
 
-         }
 
-         $array = array_unique(array_merge_recursive(array_column($data,'customer_from'),array_column($data,'car_type')));
 
-         $basic_map = BasicType::whereIn('id',$array)
 
-             ->pluck('title','id')
 
-             ->toArray();
 
-         $basic_all_map = BasicTypeAllUse::where('type', BasicTypeAllUse::type_one)
 
-             ->whereIn('id',array_unique(array_column($data,'consulting_product_new')))
 
-             ->pluck('title','id')
 
-             ->toArray();
 
-         foreach ($data as $key => $value){
 
-             $province = $province_code = $city = $city_code = "";
 
-             $is_success = 0;
 
-             if(! empty($value['address2'])){
 
-                 $tmp = $this->parseAddress($value['address2'], $provinceMap, $cityMap,$countyMap);
 
-                 if(empty($tmp['province']['label']) || empty($tmp['city']['label'])){
 
- //                    $tmp['origin'] = $value['address2'];
 
- //                    $a = [
 
- //                        "provice" => $tmp['province']['label'],
 
- //                        "provice_code" => $tmp['province']['value'],
 
- //                        "city" => $tmp['city']['label'],
 
- //                        "city_code" => $tmp['city']['value'],
 
- //                        "origin" => $value['address2']
 
- //                    ];
 
- //                    $return[] = $a;
 
-                 }else{
 
-                     $province = $tmp['province']['label'];
 
-                     $province_code = $tmp['province']['value'];
 
-                     $city = $tmp['city']['label'];
 
-                     $city_code = $tmp['city']['value'];
 
-                     $is_success = 1;
 
-                 }
 
-             }
 
-             $data[$key]['province'] = $province;
 
-             $data[$key]['province_code'] = $province_code;
 
-             $data[$key]['city'] = $city;
 
-             $data[$key]['city_code'] = $city_code;
 
-             $data[$key]['is_success'] = $is_success;
 
-             $belong_depart_id = [$value['top_depart_id']];
 
-             if(isset($map[$value['customer_id']])) $belong_depart_id = array_unique($map[$value['customer_id']]);
 
-             foreach ($belong_depart_id as $b){
 
-                 $detail_insert[] = [
 
-                     'customer_id' => $value['customer_id'],
 
-                     'top_depart_id' => $b,
 
-                     'crt_time' => $time,
 
-                     'type' => CustomerReportDepart::type_one,
 
-                     'man_id' => 0,
 
-                 ];
 
-             }
 
-             $customer_info2 = $customer_info_map2[$value['customer_id']] ?? [];
 
-             if(! empty($customer_info2)){
 
-                 foreach ($customer_info2 as $v_i){
 
-                     $detail_insert[] = [
 
-                         'customer_id' => $value['customer_id'],
 
-                         'top_depart_id' => 0,
 
-                         'crt_time' => $time,
 
-                         'type' => CustomerReportDepart::type_two,
 
-                         'man_id' => $v_i,
 
-                     ];
 
-                 }
 
-             }
 
-             $type = -1;
 
-             $r_array = $latestRecordsArray[$value['customer_id']] ?? [];
 
-             $num = $r_array['num'] ?? 0;
 
-             if(isset($isset_map[$value['customer_id']])){
 
-                 $type = 3;
 
-             }else{
 
-                 if(! empty($r_array)) $type = $r_array['follow_type'];
 
-             }
 
-             $data[$key]['type'] = $type;
 
-             $data[$key]['follow_num'] = $num;
 
-             $data[$key]['contact_info'] = $customer_info_map[$value['customer_id']] ?? "";
 
-             $data[$key]['customer_from'] = $basic_map[$value['customer_from']] ?? "";
 
-             $data[$key]['car_type'] = $customer_info_map[$value['car_type']] ?? "";
 
-             $data[$key]['consulting_product_new_title'] = $basic_all_map[$value['consulting_product_new']] ?? "";
 
-             $data[$key]['crt_time'] = $time;
 
-         }
 
-         return [$data, $detail_insert];
 
-     }
 
-     private function forInsertArea($provinceMap, $cityMap, $countyMap){
 
-         $insert = [];
 
-         foreach ($provinceMap as $key => $value){
 
-             $insert[] = [
 
-                 'key' => $key,
 
-                 'label' => $value['label'],
 
-                 'value' => $value['value'],
 
-                 'province' => "",
 
-                 'type' => 1,
 
-             ];
 
-         }
 
-         foreach ($cityMap as $key => $value){
 
-             $insert[] = [
 
-                 'key' => $key,
 
-                 'label' => $value['label'],
 
-                 'value' => $value['value'],
 
-                 'province' => $value['province'],
 
-                 'type' => 2,
 
-             ];
 
-         }
 
-         foreach ($countyMap as $key => $value){
 
-             $insert[] = [
 
-                 'key' => $key,
 
-                 'label' => $value['city_label'],
 
-                 'value' => $value['city_value'],
 
-                 'province' => $value['province_value'],
 
-                 'type' => 3,
 
-             ];
 
-         }
 
-         AreaMap::where('del_time',0)
 
-             ->where('is_create_by_human',0)
 
-             ->update(['del_time' => time()]);
 
-         AreaMap::insert($insert);
 
-     }
 
-     private function constructAddress($type = 0) {
 
-         if(! empty($type)){
 
-             $result = AreaMap::where('del_time',0)
 
-                 ->select('key','label','value','province','type')
 
-                 ->get()->toArray();
 
-             $provinceMap = [];
 
-             $cityMap = [];
 
-             $countyMap = []; // 新增区县映射表
 
-             foreach ($result as $value){
 
-                 if($value['type'] == 1){
 
-                     $provinceMap[$value['key']] = [
 
-                         'label' => $value['label'],
 
-                         'value' => $value['value'],
 
-                     ];
 
-                 }elseif($value['type'] == 2){
 
-                     $cityMap[$value['key']] = [
 
-                         'label' => $value['label'],
 
-                         'value' => $value['value'],
 
-                         'province' => $value['province'],
 
-                     ];
 
-                 }else{
 
-                     $countyMap[$value['key']] = [
 
-                         'city_label' => $value['label'],
 
-                         'city_value' => $value['value'],
 
-                         'province_value' => $value['value'],
 
-                         'type' => 3,
 
-                     ];
 
-                 }
 
-             }
 
-         }else{
 
-             $addressData = config('address3');
 
-             if(is_string($addressData)) $addressData = json_decode($addressData, true);
 
-             $provinceMap = [];
 
-             $cityMap = [];
 
-             $countyMap = []; // 新增区县映射表
 
-             // 添加直辖市特殊处理
 
-             $municipalities = [
 
-                 '110000' => ['北京', '北京市'],
 
-                 '120000' => ['天津', '天津市'],
 
-                 '310000' => ['上海', '上海市'],
 
-                 '500000' => ['重庆', '重庆市']
 
-             ];
 
-             // 添加省级简称映射
 
-             $provinceAbbr = [
 
-                 '内蒙古' => '内蒙古自治区',
 
-                 '新疆' => '新疆维吾尔自治区',
 
-                 '广西' => '广西壮族自治区',
 
-                 '宁夏' => '宁夏回族自治区',
 
-                 '西藏' => '西藏自治区',
 
-             ];
 
-             foreach ($addressData as $province) {
 
-                 $pValue = $province['value'];
 
-                 $pLabel = $province['label'];
 
-                 // 添加省份全称和简称
 
-                 $shortProvince = preg_replace('/(省|市|自治区|特别行政区)$/u', '', $pLabel);
 
-                 $provinceMap[$pLabel] = ['label' => $pLabel, 'value' => $pValue];
 
-                 $provinceMap[$shortProvince] = ['label' => $pLabel, 'value' => $pValue];
 
-                 $provinceMap[$pValue] = ['label' => $pLabel, 'value' => $pValue]; // 新增:通过编码直接访问
 
-                 // 添加省级简称
 
-                 if (isset($provinceAbbr[$shortProvince])) {
 
-                     $provinceMap[$shortProvince] = ['label' => $pLabel, 'value' => $pValue];
 
-                 }
 
-                 // 处理直辖市
 
-                 if (isset($municipalities[$pValue])) {
 
-                     $cityLabel = $municipalities[$pValue][1];
 
-                     $cityMap[$cityLabel] = $cityMap[$municipalities[$pValue][0]] = [
 
-                         'label' => $cityLabel,
 
-                         'value' => $pValue,
 
-                         'province' => $pValue
 
-                     ];
 
-                     // 添加直辖市区县映射
 
-                     foreach ($province['children'][0]['children'] as $district) {
 
-                         $dLabel = $district['label'];
 
-                         $countyMap[$dLabel] = [
 
-                             'city_label' => $cityLabel,
 
-                             'city_value' => $pValue,
 
-                             'province_value' => $pValue
 
-                         ];
 
-                         // 添加区县简称(去掉"区"字)
 
-                         $shortDistrict = preg_replace('/区$/', '', $dLabel);
 
-                         if ($shortDistrict !== $dLabel) {
 
-                             $countyMap[$shortDistrict] = $countyMap[$dLabel];
 
-                         }
 
-                     }
 
-                     continue;
 
-                 }
 
-                 // 处理普通城市和区县
 
-                 foreach ($province['children'] as $city) {
 
-                     $cValue = $city['value'];
 
-                     $cLabel = $city['label'];
 
-                     // 添加城市全称和简称
 
-                     $shortCity = preg_replace('/(市|地区|盟)$/u', '', $cLabel);
 
-                     $cityMap[$cLabel] = [
 
-                         'label' => $cLabel,
 
-                         'value' => $cValue,
 
-                         'province' => $pValue
 
-                     ];
 
-                     $cityMap[$shortCity] = [
 
-                         'label' => $cLabel,
 
-                         'value' => $cValue,
 
-                         'province' => $pValue
 
-                     ];
 
-                     // 添加自治州特殊处理(如恩施)
 
-                     $specialMapping = [
 
-                         '延边朝鲜族自治州' => '延边',
 
-                         '恩施土家族苗族自治州' => '恩施',
 
-                         '湘西土家族苗族自治州' => '湘西',
 
-                         '阿坝藏族羌族自治州' => '阿坝',
 
-                         '凉山彝族自治州' => '凉山',
 
-                         '甘孜藏族自治州' => '甘孜',
 
-                         '黔东南苗族侗族自治州' => '黔东南',
 
-                         '黔南布依族苗族自治州' => '黔南',
 
-                         '黔西南布依族苗族自治州' => '黔西南',
 
-                         '楚雄彝族自治州' => '楚雄',
 
-                         '红河哈尼族彝族自治州' => '红河',
 
-                         '文山壮族苗族自治州' => '文山',
 
-                         '西双版纳傣族自治州' => '西双版纳',
 
-                         '大理白族自治州' => '大理',
 
-                         '德宏傣族景颇族自治州' => '德宏',
 
-                         '怒江傈僳族自治州' => '怒江',
 
-                         '迪庆藏族自治州' => '迪庆',
 
-                         '临夏回族自治州' => '临夏',
 
-                         '甘南藏族自治州' => '甘南',
 
-                         '海南藏族自治州' => '海南',
 
-                         '海北藏族自治州' => '海北',
 
-                         '海西蒙古族藏族自治州' => '海西',
 
-                         '黄南藏族自治州' => '黄南',
 
-                         '果洛藏族自治州' => '果洛',
 
-                         '玉树藏族自治州' => '玉树',
 
-                         '伊犁哈萨克自治州' => '伊犁',
 
-                         '博尔塔拉蒙古自治州' => '博尔塔拉',
 
-                         '昌吉回族自治州' => '昌吉',
 
- //                        '巴音郭楞蒙古自治州' => '巴州',
 
-                         '巴音郭楞蒙古自治州' => '巴音郭楞',
 
-                         '克孜勒苏柯尔克孜自治州' => '克孜勒苏',
 
-                     ];
 
- //                    $specialMapping = array_flip($specialMapping);
 
-                     if (isset($specialMapping[$shortCity])) {
 
-                         $cityMap[$specialMapping[$shortCity]] = [
 
-                             'label' => $shortCity,
 
-                             'value' => $cValue,
 
-                             'province' => $pValue
 
-                         ];
 
-                     }
 
- //                    if (strpos($cLabel, '自治州') !== false) {
 
- ////                    dump($province['children']);
 
- //                        $autonomousShort = str_replace(['自治州', '土家族', '苗族', '壮族'], '', $cLabel);
 
- //                        $cityMap[$autonomousShort] = [
 
- //                            'label' => $cLabel,
 
- //                            'value' => $cValue,
 
- //                            'province' => $pValue
 
- //                        ];
 
- //                    }
 
-                     // 添加城市常见简称(特殊处理)
 
-                     $specialAbbr = [
 
-                         '成都' => '成都市',
 
-                         '杭州' => '杭州市',
 
-                         '广州' => '广州市',
 
-                         '深圳' => '深圳市',
 
-                         '武汉' => '武汉市',
 
-                         '南京' => '南京市',
 
-                         '苏州' => '苏州市',
 
-                         '宁波' => '宁波市',
 
-                         '青岛' => '青岛市',
 
-                     ];
 
-                     if (isset($specialAbbr[$shortCity])) {
 
-                         $cityMap[$shortCity] = [
 
-                             'label' => $specialAbbr[$shortCity],
 
-                             'value' => $cValue,
 
-                             'province' => $pValue
 
-                         ];
 
-                     }
 
-                     // 添加区县映射
 
-                     if (isset($city['children'])) {
 
-                         foreach ($city['children'] as $county) {
 
-                             $countyLabel = $county['label'];
 
-                             $countyMap[$countyLabel] = [
 
-                                 'city_label' => $cLabel,
 
-                                 'city_value' => $cValue,
 
-                                 'province_value' => $pValue
 
-                             ];
 
-                             // 添加区县简称(去掉"区"/"县"字)
 
-                             $shortCounty = preg_replace('/(区|县|市|自治县|黎族自治县|黎族苗族自治县)$/u', '', $countyLabel);
 
-                             if ($shortCounty !== $countyLabel) {
 
-                                 $countyMap[$shortCounty] = $countyMap[$countyLabel];
 
-                             }
 
-                         }
 
-                     }
 
-                 }
 
-             }
 
-         }
 
- //dd($cityMap);
 
-         return [$provinceMap, $cityMap, $countyMap]; // 返回三个映射表
 
-     }
 
-     function parseAddress($str, $provinceMap, $cityMap, $countyMap) {
 
-         static $cache = [];
 
-         if (isset($cache[$str])) return $cache[$str];
 
-         $str = str_replace(['·', ' ', '-', '-'], '', trim($str));
 
-         // 0. 处理空字符串
 
-         if (empty($str)) {
 
-             $result = [
 
-                 'province' => ['label' => '', 'value' => ''],
 
-                 'city' => ['label' => '', 'value' => '']
 
-             ];
 
-             $cache[$str] = $result;
 
-             return $result;
 
-         }
 
-         // 1. 处理直辖市简称
 
-         $municipalityShort = ['京', '沪', '津', '渝'];
 
-         $municipalityMap = [
 
-             '京' => '北京市',
 
-             '沪' => '上海市',
 
-             '津' => '天津市',
 
-             '渝' => '重庆市'
 
-         ];
 
-         if (in_array(mb_substr($str, 0, 1), $municipalityShort)) {
 
-             $cityName = $municipalityMap[mb_substr($str, 0, 1)];
 
-             $city = $cityMap[$cityName] ?? null;
 
-             if ($city) {
 
-                 $provinceInfo = $provinceMap[$city['province']] ?? null;
 
-                 $result = [
 
-                     'province' => $provinceInfo ?: ['label' => $cityName, 'value' => $city['province']],
 
-                     'city' => ['label' => $city['label'], 'value' => $city['value']]
 
-                 ];
 
-                 $cache[$str] = $result;
 
-                 return $result;
 
-             }
 
-         }
 
-         // 2. 完整字符串匹配城市
 
-         if (isset($cityMap[$str])) {
 
-             $city = $cityMap[$str];
 
-             $provinceInfo = $provinceMap[$city['province']] ?? null;
 
-             $result = [
 
-                 'province' => $provinceInfo ?: ['label' => '', 'value' => $city['province']],
 
-                 'city' => ['label' => $city['label'], 'value' => $city['value']]
 
-             ];
 
-             $cache[$str] = $result;
 
-             return $result;
 
-         }
 
-         // 3. 匹配区县(新增)
 
-         if (isset($countyMap[$str])) {
 
-             $county = $countyMap[$str];
 
-             $cityInfo = [
 
-                 'label' => $county['city_label'],
 
-                 'value' => $county['city_value']
 
-             ];
 
-             $provinceInfo = $provinceMap[$county['province_value']] ?? null;
 
-             $result = [
 
-                 'province' => $provinceInfo ?: ['label' => '', 'value' => $county['province_value']],
 
-                 'city' => $cityInfo
 
-             ];
 
-             $cache[$str] = $result;
 
-             return $result;
 
-         }
 
-         // 4. 优先匹配3字省份(如内蒙古)
 
-         if (mb_strlen($str) >= 3) {
 
-             $prefix = mb_substr($str, 0, 3);
 
-             if (isset($provinceMap[$prefix])) {
 
-                 return $this->handleMatchedProvince($prefix, mb_substr($str, 3), $provinceMap, $cityMap, $countyMap, $cache, $str);
 
-             }
 
-         }
 
-         // 5. 匹配2字省份
 
-         if (mb_strlen($str) >= 2) {
 
-             $prefix = mb_substr($str, 0, 2);
 
-             if (isset($provinceMap[$prefix])) {
 
-                 return $this->handleMatchedProvince($prefix, mb_substr($str, 2), $provinceMap, $cityMap, $countyMap, $cache, $str);
 
-             }
 
-         }
 
-         // 6. 城市关键词扫描(加强版)
 
-         $cityKeys = array_keys($cityMap);
 
-         usort($cityKeys, fn($a, $b) => mb_strlen($b) - mb_strlen($a));
 
-         foreach ($cityKeys as $key) {
 
-             if (mb_strpos($str, $key) !== false) {
 
-                 $city = $cityMap[$key];
 
-                 $provinceInfo = $provinceMap[$city['province']] ?? null;
 
-                 $result = [
 
-                     'province' => $provinceInfo ?: ['label' => '', 'value' => $city['province']],
 
-                     'city' => ['label' => $city['label'], 'value' => $city['value']]
 
-                 ];
 
-                 $cache[$str] = $result;
 
-                 return $result;
 
-             }
 
-         }
 
-         // 7. 区县关键词扫描(新增)
 
-         $countyKeys = array_keys($countyMap);
 
-         usort($countyKeys, fn($a, $b) => mb_strlen($b) - mb_strlen($a));
 
-         foreach ($countyKeys as $key) {
 
-             if (mb_strpos($str, $key) !== false) {
 
-                 $county = $countyMap[$key];
 
-                 $provinceInfo = $provinceMap[$county['province_value']] ?? null;
 
-                 $result = [
 
-                     'province' => $provinceInfo ?: ['label' => '', 'value' => $county['province_value']],
 
-                     'city' => ['label' => $county['city_label'], 'value' => $county['city_value']]
 
-                 ];
 
-                 $cache[$str] = $result;
 
-                 return $result;
 
-             }
 
-         }
 
-         // 8. 最终匹配失败
 
-         $result = [
 
-             'province' => ['label' => '', 'value' => ''],
 
-             'city' => ['label' => '', 'value' => '']
 
-         ];
 
-         $cache[$str] = $result;
 
-         return $result;
 
-     }
 
-     // 优化省份匹配后的处理
 
-     function handleMatchedProvince($provinceKey, $remainder, $provinceMap, $cityMap, $countyMap, &$cache, $originalStr) {
 
-         $province = $provinceMap[$provinceKey];
 
-         // 0. 处理空余部分
 
-         if (empty($remainder)) {
 
-             $result = [
 
-                 'province' => ['label' => $province['label'], 'value' => $province['value']],
 
-                 'city' => ['label' => '', 'value' => '']
 
-             ];
 
-             $cache[$originalStr] = $result;
 
-             return $result;
 
-         }
 
-         // 1. 尝试完整匹配城市
 
-         if (isset($cityMap[$remainder])) {
 
-             $city = $cityMap[$remainder];
 
-             $result = [
 
-                 'province' => ['label' => $province['label'], 'value' => $province['value']],
 
-                 'city' => ['label' => $city['label'], 'value' => $city['value']]
 
-             ];
 
-             $cache[$originalStr] = $result;
 
-             return $result;
 
-         }
 
-         // 2. 尝试匹配区县(新增)
 
-         if (isset($countyMap[$remainder])) {
 
-             $county = $countyMap[$remainder];
 
-             $result = [
 
-                 'province' => ['label' => $province['label'], 'value' => $province['value']],
 
-                 'city' => ['label' => $county['city_label'], 'value' => $county['city_value']]
 
-             ];
 
-             $cache[$originalStr] = $result;
 
-             return $result;
 
-         }
 
-         // 3. 城市关键词扫描
 
-         $cityKeys = array_keys($cityMap);
 
-         usort($cityKeys, fn($a, $b) => mb_strlen($b) - mb_strlen($a));
 
-         foreach ($cityKeys as $key) {
 
-             if (mb_strpos($remainder, $key) !== false) {
 
-                 $city = $cityMap[$key];
 
-                 $result = [
 
-                     'province' => ['label' => $province['label'], 'value' => $province['value']],
 
-                     'city' => ['label' => $city['label'], 'value' => $city['value']]
 
-                 ];
 
-                 $cache[$originalStr] = $result;
 
-                 return $result;
 
-             }
 
-         }
 
-         // 4. 区县关键词扫描(新增)
 
-         $countyKeys = array_keys($countyMap);
 
-         usort($countyKeys, fn($a, $b) => mb_strlen($b) - mb_strlen($a));
 
-         foreach ($countyKeys as $key) {
 
-             if (mb_strpos($remainder, $key) !== false) {
 
-                 $county = $countyMap[$key];
 
-                 $result = [
 
-                     'province' => ['label' => $province['label'], 'value' => $province['value']],
 
-                     'city' => ['label' => $county['city_label'], 'value' => $county['city_value']]
 
-                 ];
 
-                 $cache[$originalStr] = $result;
 
-                 return $result;
 
-             }
 
-         }
 
-         // 5. 仅返回省份信息
 
-         $result = [
 
-             'province' => ['label' => $province['label'], 'value' => $province['value']],
 
-             'city' => ['label' => '', 'value' => '']
 
-         ];
 
-         $cache[$originalStr] = $result;
 
-         return $result;
 
-     }
 
-     //带转换数据
 
-     private function constructAddress3() {
 
-         $special = ["710000", "810000", "820000"];
 
-         $address2Data = config("address");
 
-         $extra_data = [];
 
-         foreach ($address2Data as $value){
 
-             if(in_array($value['value'], $special)){
 
-                 $extra_data[] = $value;
 
-             }
 
-         }
 
-         // 1. 加载新数据结构
 
-         $address2Data = config("address2");
 
-         $address2Data = json_decode($address2Data, true);
 
-         // 2. 转换数据结构为旧格式
 
-         $addressData = [];
 
-         foreach ($address2Data as $province) {
 
-             $provinceEntry = [
 
-                 'value' => $province['code'],
 
-                 'label' => $province['name'],
 
-                 'children' => []
 
-             ];
 
-             if(! isset($province['city'])) continue;
 
-             foreach ($province['city'] as $city) {
 
-                 $cityEntry = [
 
-                     'value' => $city['code'],
 
-                     'label' => $city['name'],
 
-                     'children' => []
 
-                 ];
 
-                 foreach ($city['area'] as $area) {
 
-                     $cityEntry['children'][] = [
 
-                         'value' => $area['code'],
 
-                         'label' => $area['name']
 
-                     ];
 
-                 }
 
-                 $provinceEntry['children'][] = $cityEntry;
 
-             }
 
-             $addressData[] = $provinceEntry;
 
-         }
 
-         $addressData = array_merge_recursive($addressData, $extra_data);
 
-         echo json_encode($addressData);die;
 
-         $provinceMap = [];
 
-         $cityMap = [];
 
-         $countyMap = []; // 新增区县映射表
 
-         // 添加直辖市特殊处理
 
-         $municipalities = [
 
-             '110000' => ['北京', '北京市'],
 
-             '120000' => ['天津', '天津市'],
 
-             '310000' => ['上海', '上海市'],
 
-             '500000' => ['重庆', '重庆市']
 
-         ];
 
-         // 添加省级简称映射
 
-         $provinceAbbr = [
 
-             '内蒙古' => '内蒙古自治区',
 
-             '新疆' => '新疆维吾尔自治区',
 
-             '广西' => '广西壮族自治区',
 
-             '宁夏' => '宁夏回族自治区',
 
-             '西藏' => '西藏自治区'
 
-         ];
 
-         foreach ($addressData as $province) {
 
-             $pValue = $province['value'];
 
-             $pLabel = $province['label'];
 
-             // 添加省份全称和简称
 
-             $shortProvince = preg_replace('/(省|市|自治区|特别行政区)$/u', '', $pLabel);
 
-             $provinceMap[$pLabel] = ['label' => $pLabel, 'value' => $pValue];
 
-             $provinceMap[$shortProvince] = ['label' => $pLabel, 'value' => $pValue];
 
-             $provinceMap[$pValue] = ['label' => $pLabel, 'value' => $pValue]; // 新增:通过编码直接访问
 
-             // 添加省级简称
 
-             if (isset($provinceAbbr[$shortProvince])) {
 
-                 $provinceMap[$shortProvince] = ['label' => $pLabel, 'value' => $pValue];
 
-             }
 
-             // 处理直辖市
 
-             if (isset($municipalities[$pValue])) {
 
-                 $cityLabel = $municipalities[$pValue][1];
 
-                 $cityMap[$cityLabel] = $cityMap[$municipalities[$pValue][0]] = [
 
-                     'label' => $cityLabel,
 
-                     'value' => $pValue,
 
-                     'province' => $pValue
 
-                 ];
 
-                 // 添加直辖市区县映射
 
-                 foreach ($province['children'][0]['children'] as $district) {
 
-                     $dLabel = $district['label'];
 
-                     $countyMap[$dLabel] = [
 
-                         'city_label' => $cityLabel,
 
-                         'city_value' => $pValue,
 
-                         'province_value' => $pValue
 
-                     ];
 
-                     // 添加区县简称(去掉"区"字)
 
-                     $shortDistrict = preg_replace('/区$/', '', $dLabel);
 
-                     if ($shortDistrict !== $dLabel) {
 
-                         $countyMap[$shortDistrict] = $countyMap[$dLabel];
 
-                     }
 
-                 }
 
-                 continue;
 
-             }
 
-             // 处理普通城市和区县
 
-             foreach ($province['children'] as $city) {
 
-                 $cValue = $city['value'];
 
-                 $cLabel = $city['label'];
 
-                 // 添加城市全称和简称
 
-                 $shortCity = preg_replace('/市$/', '', $cLabel);
 
-                 $cityMap[$cLabel] = [
 
-                     'label' => $cLabel,
 
-                     'value' => $cValue,
 
-                     'province' => $pValue
 
-                 ];
 
-                 $cityMap[$shortCity] = [
 
-                     'label' => $cLabel,
 
-                     'value' => $cValue,
 
-                     'province' => $pValue
 
-                 ];
 
-                 // 添加自治州特殊处理(如恩施)
 
-                 if (strpos($cLabel, '自治州') !== false) {
 
-                     $autonomousShort = str_replace(['自治州', '土家族', '苗族', '壮族'], '', $cLabel);
 
-                     $cityMap[$autonomousShort] = [
 
-                         'label' => $cLabel,
 
-                         'value' => $cValue,
 
-                         'province' => $pValue
 
-                     ];
 
-                 }
 
-                 // 添加城市常见简称(特殊处理)
 
-                 $specialAbbr = [
 
-                     '成都' => '成都市',
 
-                     '杭州' => '杭州市',
 
-                     '广州' => '广州市',
 
-                     '深圳' => '深圳市',
 
-                     '武汉' => '武汉市',
 
-                     '南京' => '南京市',
 
-                     '苏州' => '苏州市',
 
-                     '宁波' => '宁波市',
 
-                     '青岛' => '青岛市'
 
-                 ];
 
-                 if (isset($specialAbbr[$shortCity])) {
 
-                     $cityMap[$shortCity] = [
 
-                         'label' => $specialAbbr[$shortCity],
 
-                         'value' => $cValue,
 
-                         'province' => $pValue
 
-                     ];
 
-                 }
 
-                 // 添加区县映射
 
-                 if (isset($city['children'])) {
 
-                     foreach ($city['children'] as $county) {
 
-                         $countyLabel = $county['label'];
 
-                         $countyMap[$countyLabel] = [
 
-                             'city_label' => $cLabel,
 
-                             'city_value' => $cValue,
 
-                             'province_value' => $pValue
 
-                         ];
 
-                         // 添加区县简称(去掉"区"/"县"字)
 
-                         $shortCounty = preg_replace('/(区|县|市)$/u', '', $countyLabel);
 
-                         if ($shortCounty !== $countyLabel) {
 
-                             $countyMap[$shortCounty] = $countyMap[$countyLabel];
 
-                         }
 
-                     }
 
-                 }
 
-             }
 
-         }
 
-         return [$provinceMap, $cityMap, $countyMap]; // 返回三个映射表
 
-     }
 
-     private function constructAddress2(){
 
-         $addressData = config('address');
 
-         $provinceMap = [];
 
-         $cityMap = [];
 
-         // 添加直辖市特殊处理
 
-         $municipalities = [
 
-             '110000' => ['北京', '北京市'],
 
-             '120000' => ['天津', '天津市'],
 
-             '310000' => ['上海', '上海市'],
 
-             '500000' => ['重庆', '重庆市']
 
-         ];
 
-         // 添加省级简称映射
 
-         $provinceAbbr = [
 
-             '内蒙古' => '内蒙古自治区',
 
-             '新疆' => '新疆维吾尔自治区',
 
-             '广西' => '广西壮族自治区',
 
-             '宁夏' => '宁夏回族自治区',
 
-             '西藏' => '西藏自治区'
 
-         ];
 
-         foreach ($addressData as $province) {
 
-             $pValue = $province['value'];
 
-             $pLabel = $province['label'];
 
-             // 添加省份全称和简称
 
-             $shortProvince = preg_replace('/(省|市|自治区|特别行政区)$/u', '', $pLabel);
 
-             $provinceMap[$pLabel] = ['label' => $pLabel, 'value' => $pValue];
 
-             $provinceMap[$shortProvince] = ['label' => $pLabel, 'value' => $pValue];
 
-             // 添加省级简称
 
-             if (isset($provinceAbbr[$shortProvince])) {
 
-                 $provinceMap[$shortProvince] = ['label' => $pLabel, 'value' => $pValue];
 
-             }
 
-             // 处理直辖市
 
-             if (isset($municipalities[$pValue])) {
 
-                 $cityLabel = $municipalities[$pValue][1];
 
-                 $cityMap[$cityLabel] = $cityMap[$municipalities[$pValue][0]] = [
 
-                     'label' => $cityLabel,
 
-                     'value' => $pValue,
 
-                     'province' => $pValue
 
-                 ];
 
-                 continue;
 
-             }
 
-             // 处理普通城市
 
-             foreach ($province['children'] as $city) {
 
-                 $cValue = $city['value'];
 
-                 $cLabel = $city['label'];
 
-                 // 添加城市全称和简称
 
-                 $shortCity = preg_replace('/市$/', '', $cLabel);
 
-                 $cityMap[$cLabel] = [
 
-                     'label' => $cLabel,
 
-                     'value' => $cValue,
 
-                     'province' => $pValue
 
-                 ];
 
-                 $cityMap[$shortCity] = [
 
-                     'label' => $cLabel,
 
-                     'value' => $cValue,
 
-                     'province' => $pValue
 
-                 ];
 
-                 // 添加城市常见简称(特殊处理)
 
-                 $specialAbbr = [
 
-                     '成都' => '成都市',
 
-                     '杭州' => '杭州市',
 
-                     '广州' => '广州市',
 
-                     '深圳' => '深圳市',
 
-                     '武汉' => '武汉市',
 
-                     '南京' => '南京市'
 
-                 ];
 
-                 if (isset($specialAbbr[$shortCity])) {
 
-                     $cityMap[$shortCity] = [
 
-                         'label' => $specialAbbr[$shortCity],
 
-                         'value' => $cValue,
 
-                         'province' => $pValue
 
-                     ];
 
-                 }
 
-             }
 
-         }
 
-         return [$provinceMap, $cityMap];
 
-     }
 
-     function parseAddress2($str, $provinceMap, $cityMap) {
 
-         static $cache = [];
 
-         if (isset($cache[$str])) return $cache[$str];
 
-         $str = str_replace(['·', ' ', '-'], '', trim($str));
 
-         // 1. 处理直辖市简称
 
-         $municipalityShort = ['京', '沪', '津', '渝'];
 
-         $municipalityMap = [
 
-             '京' => '北京市',
 
-             '沪' => '上海市',
 
-             '津' => '天津市',
 
-             '渝' => '重庆市'
 
-         ];
 
-         if (in_array(mb_substr($str, 0, 1), $municipalityShort)) {
 
-             $cityName = $municipalityMap[mb_substr($str, 0, 1)];
 
-             $city = $cityMap[$cityName] ?? null;
 
-             if ($city) {
 
-                 $result = [
 
-                     'province' => ['label' => $city['label'], 'value' => $city['province']],
 
-                     'city' => ['label' => $city['label'], 'value' => $city['value']]
 
-                 ];
 
-                 $cache[$str] = $result;
 
-                 return $result;
 
-             }
 
-         }
 
-         // 2. 完整字符串匹配城市(加强版)
 
-         if (isset($cityMap[$str])) {
 
-             $city = $cityMap[$str];
 
-             $province = $provinceMap[$city['province']] ?? null;
 
-             $result = [
 
-                 'province' => $province ? ['label' => $province['label'], 'value' => $province['value']] : ['label' => '', 'value' => ''],
 
-                 'city' => ['label' => $city['label'], 'value' => $city['value']]
 
-             ];
 
-             $cache[$str] = $result;
 
-             return $result;
 
-         }
 
-         // 3. 优先匹配3字省份(如内蒙古)
 
-         if (mb_strlen($str) >= 3) {
 
-             $prefix = mb_substr($str, 0, 3);
 
-             if (isset($provinceMap[$prefix])) {
 
-                 return $this->handleMatchedProvince($prefix, mb_substr($str, 3), $provinceMap, $cityMap, $cache, $str);
 
-             }
 
-         }
 
-         // 4. 匹配2字省份
 
-         if (mb_strlen($str) >= 2) {
 
-             $prefix = mb_substr($str, 0, 2);
 
-             if (isset($provinceMap[$prefix])) {
 
-                 return $this->handleMatchedProvince($prefix, mb_substr($str, 2), $provinceMap, $cityMap, $cache, $str);
 
-             }
 
-         }
 
-         // 5. 城市关键词扫描(加强版)
 
-         $cityKeys = array_keys($cityMap);
 
-         usort($cityKeys, fn($a, $b) => mb_strlen($b) - mb_strlen($a));
 
-         foreach ($cityKeys as $key) {
 
-             if (mb_strpos($str, $key) !== false) {
 
-                 $city = $cityMap[$key];
 
-                 $province = $provinceMap[$city['province']] ?? null;
 
-                 // 特殊处理:当匹配到城市时,尝试找回省份
 
-                 if (!$province) {
 
-                     $province = $provinceMap[$city['province']] ?? ['label' => '', 'value' => ''];
 
-                 }
 
-                 $result = [
 
-                     'province' => ['label' => $province['label'], 'value' => $province['value']],
 
-                     'city' => ['label' => $city['label'], 'value' => $city['value']]
 
-                 ];
 
-                 $cache[$str] = $result;
 
-                 return $result;
 
-             }
 
-         }
 
-         // 6. 最终匹配失败
 
-         $result = [
 
-             'province' => ['label' => '', 'value' => ''],
 
-             'city' => ['label' => '', 'value' => '']
 
-         ];
 
-         $cache[$str] = $result;
 
-         return $result;
 
-     }
 
-     // 优化省份匹配后的处理
 
-     function handleMatchedProvince2($provinceKey, $remainder, $provinceMap, $cityMap, &$cache, $originalStr) {
 
-         $province = $provinceMap[$provinceKey];
 
-         // 直辖市特殊处理
 
-         $municipalities = ['110000', '120000', '310000', '500000'];
 
-         if (in_array($province['value'], $municipalities)) {
 
-             $city = $cityMap[$province['label']] ?? null;
 
-             if ($city) {
 
-                 $result = [
 
-                     'province' => ['label' => $province['label'], 'value' => $province['value']],
 
-                     'city' => ['label' => $city['label'], 'value' => $city['value']]
 
-                 ];
 
-                 $cache[$originalStr] = $result;
 
-                 return $result;
 
-             }
 
-         }
 
-         // 尝试匹配剩余部分
 
-         if ($remainder !== '') {
 
-             // 先尝试完整匹配
 
-             if (isset($cityMap[$remainder])) {
 
-                 $city = $cityMap[$remainder];
 
-                 $result = [
 
-                     'province' => ['label' => $province['label'], 'value' => $province['value']],
 
-                     'city' => ['label' => $city['label'], 'value' => $city['value']]
 
-                 ];
 
-                 $cache[$originalStr] = $result;
 
-                 return $result;
 
-             }
 
-             // 再尝试关键词扫描
 
-             $cityKeys = array_keys($cityMap);
 
-             usort($cityKeys, fn($a, $b) => mb_strlen($b) - mb_strlen($a));
 
-             foreach ($cityKeys as $key) {
 
-                 if (mb_strpos($remainder, $key) !== false) {
 
-                     $city = $cityMap[$key];
 
-                     $result = [
 
-                         'province' => ['label' => $province['label'], 'value' => $province['value']],
 
-                         'city' => ['label' => $city['label'], 'value' => $city['value']]
 
-                     ];
 
-                     $cache[$originalStr] = $result;
 
-                     return $result;
 
-                 }
 
-             }
 
-         }
 
-         // 仅返回省份信息
 
-         $result = [
 
-             'province' => ['label' => $province['label'], 'value' => $province['value']],
 
-             'city' => ['label' => '', 'value' => '']
 
-         ];
 
-         $cache[$originalStr] = $result;
 
-         return $result;
 
-     }
 
-     private function constructAddress1(){
 
-         $addressData = config('address');
 
-         // 构建映射表
 
-         $provinceMap = [];
 
-         $cityMap = [];
 
-         // 直辖市特殊处理(直接映射到市)
 
-         $municipalities = [
 
-             '110000' => ['北京', '北京市'],
 
-             '120000' => ['天津', '天津市'],
 
-             '310000' => ['上海', '上海市'],
 
-             '500000' => ['重庆', '重庆市']
 
-         ];
 
-         foreach ($addressData as $province) {
 
-             $pValue = $province['value'];
 
-             $pLabel = $province['label'];
 
-             // 生成省份简称
 
-             $shortProvince = preg_replace('/(省|市|自治区|特别行政区)$/u', '', $pLabel);
 
-             $provinceMap[$pLabel] = ['label' => $pLabel, 'value' => $pValue];
 
-             $provinceMap[$shortProvince] = ['label' => $pLabel, 'value' => $pValue];
 
-             // 处理直辖市(省市同名)
 
-             if (isset($municipalities[$pValue])) {
 
-                 $cityLabel = $municipalities[$pValue][1];
 
-                 $cityMap[$cityLabel] = $cityMap[$municipalities[$pValue][0]] = [
 
-                     'label' => $cityLabel,
 
-                     'value' => $pValue, // 直辖市使用省级代码
 
-                     'province' => $pValue
 
-                 ];
 
-                 continue;
 
-             }
 
-             // 处理普通城市
 
-             foreach ($province['children'] as $city) {
 
-                 $cValue = $city['value'];
 
-                 $cLabel = $city['label'];
 
-                 // 生成城市简称
 
-                 $shortCity = preg_replace('/市$/', '', $cLabel);
 
-                 $cityMap[$cLabel] = [
 
-                     'label' => $cLabel,
 
-                     'value' => $cValue,
 
-                     'province' => $pValue
 
-                 ];
 
-                 if ($shortCity !== $cLabel) {
 
-                     $cityMap[$shortCity] = [
 
-                         'label' => $cLabel,
 
-                         'value' => $cValue,
 
-                         'province' => $pValue
 
-                     ];
 
-                 }
 
-             }
 
-         }
 
-         return [$provinceMap, $cityMap];
 
-     }
 
-     // 解析函数
 
-     function parseAddress1($str, $provinceMap, $cityMap) {
 
-         static $cache = []; // 结果缓存
 
-         if (isset($cache[$str])) return $cache[$str];
 
-         $str = str_replace(['·', ' '], '', trim($str));
 
-         // 1. 处理带分隔符的格式 (广东-佛山)
 
-         if (preg_match('/[-\s]/', $str)) {
 
-             $parts = preg_split('/[-\s]+/', $str);
 
-             foreach ($parts as $part) {
 
-                 if ($result = $this->parseAddress($part, $provinceMap, $cityMap)) {
 
-                     $cache[$str] = $result;
 
-                     return $result;
 
-                 }
 
-             }
 
-         }
 
-         // 2. 完整字符串匹配城市
 
-         if (isset($cityMap[$str])) {
 
-             $city = $cityMap[$str];
 
-             $province = $provinceMap[$city['province']] ?? null;
 
-             $result = [
 
-                 'province' => $province ? ['label' => $province['label'], 'value' => $province['value']] : ['label' => '', 'value' => ''],
 
-                 'city' => ['label' => $city['label'], 'value' => $city['value']]
 
-             ];
 
-             $cache[$str] = $result;
 
-             return $result;
 
-         }
 
-         // 3. 优先匹配3字省份 (内蒙古)
 
-         if (mb_strlen($str) >= 3) {
 
-             $prefix = mb_substr($str, 0, 3);
 
-             if (isset($provinceMap[$prefix])) {
 
-                 return $this->handleMatchedProvince($prefix, mb_substr($str, 3), $provinceMap, $cityMap, $cache, $str);
 
-             }
 
-         }
 
-         // 4. 匹配2字省份 (江苏)
 
-         if (mb_strlen($str) >= 2) {
 
-             $prefix = mb_substr($str, 0, 2);
 
-             if (isset($provinceMap[$prefix])) {
 
-                 return $this->handleMatchedProvince($prefix, mb_substr($str, 2), $provinceMap, $cityMap, $cache, $str);
 
-             }
 
-         }
 
-         // 5. 城市关键词扫描 (解决"成都双流"类问题)
 
-         $cityKeys = array_keys($cityMap);
 
-         usort($cityKeys, fn($a, $b) => mb_strlen($b) - mb_strlen($a));
 
-         foreach ($cityKeys as $key) {
 
-             if (mb_strpos($str, $key) !== false) {
 
-                 $city = $cityMap[$key];
 
-                 $province = $provinceMap[$city['province']] ?? null;
 
-                 $result = [
 
-                     'province' => $province ? ['label' => $province['label'], 'value' => $province['value']] : ['label' => '', 'value' => ''],
 
-                     'city' => ['label' => $city['label'], 'value' => $city['value']]
 
-                 ];
 
-                 $cache[$str] = $result;
 
-                 return $result;
 
-             }
 
-         }
 
-         // 6. 最终匹配失败
 
-         $result = [
 
-             'province' => ['label' => '', 'value' => ''],
 
-             'city' => ['label' => '', 'value' => '']
 
-         ];
 
-         $cache[$str] = $result;
 
-         return $result;
 
-     }
 
-     // 省份匹配后的处理
 
-     function handleMatchedProvince1($provinceKey, $remainder, $provinceMap, $cityMap, &$cache, $originalStr) {
 
-         $province = $provinceMap[$provinceKey];
 
-         // 直辖市特殊处理
 
-         if (isset($provinceMap[$province['label']])) {
 
-             $city = $cityMap[$province['label']] ?? null;
 
-             if ($city) {
 
-                 $result = [
 
-                     'province' => ['label' => $province['label'], 'value' => $province['value']],
 
-                     'city' => ['label' => $city['label'], 'value' => $city['value']]
 
-                 ];
 
-                 $cache[$originalStr] = $result;
 
-                 return $result;
 
-             }
 
-         }
 
-         // 尝试匹配剩余部分
 
-         if ($remainder !== '' && isset($cityMap[$remainder])) {
 
-             $city = $cityMap[$remainder];
 
-             $result = [
 
-                 'province' => ['label' => $province['label'], 'value' => $province['value']],
 
-                 'city' => ['label' => $city['label'], 'value' => $city['value']]
 
-             ];
 
-             $cache[$originalStr] = $result;
 
-             return $result;
 
-         }
 
-         // 仅返回省份信息
 
-         $result = [
 
-             'province' => ['label' => $province['label'], 'value' => $province['value']],
 
-             'city' => ['label' => '', 'value' => '']
 
-         ];
 
-         $cache[$originalStr] = $result;
 
-         return $result;
 
-     }
 
- }
 
 
  |