12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295 |
- <?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('enter_time','asc')
- ->select('id as customer_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();
- 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;
- }
- }
|