From 38b8f457f7f6d093ed052dda759b6c6476e61f20 Mon Sep 17 00:00:00 2001 From: cody <648753004@qq.com> Date: Thu, 25 Dec 2025 13:49:53 +0800 Subject: [PATCH] update --- app/Console/Commands/UpdateCompany.php | 176 +------------------------ app/Jobs/UpdateCompanyJob.php | 130 +----------------- app/Models/Company.php | 142 ++++++++++++++++++++ 3 files changed, 152 insertions(+), 296 deletions(-) diff --git a/app/Console/Commands/UpdateCompany.php b/app/Console/Commands/UpdateCompany.php index a2eeb35..f225b49 100755 --- a/app/Console/Commands/UpdateCompany.php +++ b/app/Console/Commands/UpdateCompany.php @@ -4,8 +4,6 @@ namespace App\Console\Commands; use App\Models\Company; use App\Models\User; -use App\Repositories\MeetRepository; -use App\Repositories\YuanheRepository; use Illuminate\Console\Command; @@ -72,181 +70,21 @@ class UpdateCompany extends Command $bar = $this->output->createProgressBar($total); $bar->start(); - $YuanheRepository = new YuanheRepository(); foreach ($users as $user) { - // 获取公司详细信息 - $result = $YuanheRepository->companyInfo(['enterpriseName' => $user->company_name]); - if (!$result) { - // 标识一下为匹配到公司,后续可以根据这个字段筛选出未匹配到公司的用户 - $user->company_id = 0; - $user->save(); - $bar->setMessage($user->company_name . ' 公司不存在', 'status'); - $bar->advance(); - continue; - } - // 如果$result['enterpriseName']存在数字,跳过 - if (preg_match('/\d/', $result['enterpriseName'])) { - $user->company_id = 0; - $user->save(); - $bar->setMessage($user->company_name . ' 公司名称包含数字,跳过', 'status'); - $bar->advance(); - continue; - } - if ($result['status'] == '未注册') { - $user->company_id = 0; - $user->save(); - $bar->setMessage($user->company_name . ' 公司未注册,跳过', 'status'); - $bar->advance(); - continue; - } - $where = ['company_name' => $result['enterpriseName']]; - $data = [ - 'company_address' => $result['address'], - 'business_scope' => $result['businessScope'], - 'company_city' => $result['city'], - 'contact_mail' => $result['contactMail'], - 'contact_phone' => $result['contactPhone'], - 'company_area' => $result['country'], - 'credit_code' => $result['creditCode'], - 'enterprise_id' => $result['enterpriseId'], - 'company_name' => $result['enterpriseName'], - 'is_abroad' => $result['isAbroad'], - 'company_market' => $result['isOnStock'], - 'is_yh_invested' => $result['isYhInvested'], - 'logo' => $result['logo'], - 'company_legal_representative' => $result['operName'], - 'company_province' => $result['province'], - 'company_industry' => combineKeyValue($result['qccIndustry']), - 'regist_amount' => $result['registAmount'], - 'regist_capi_type' => $result['registCapiType'], - 'company_date' => $result['startDate'], - 'status' => $result['status'], - 'stock_date' => $result['stockDate'], - 'currency_type' => $result['currencyType'], - 'stock_number' => $result['stockNumber'], - 'stock_type' => $result['stockType'], - 'company_tag' => implode(',', $result['tagList']), - // 更新日期 - 'update_date' => $result['updatedDate'] ?? null, - // 管理平台 - 'project_users' => $result['projectUsers'] ?? null, - // 股东信息 - 'partners' => $result['partners'] ?? null, - ]; - $company = Company::updateOrCreate($where, $data); - // 更新用户关联 - $user->company_id = $company->id; - $user->save(); - // 更新上市状态 - $this->updateMarketStatus($company->id); - // 更新位置 - $this->local($company->id); - $bar->setMessage($result['enterpriseName'] . ' 更新成功', 'status'); - $bar->advance(); - } - - $bar->finish(); - $this->newLine(); - return $this->info('公司信息-全部更新完成'); - } + // 调用模型方法更新公司信息 + $result = Company::updateCompanyFromUser($user); - /** - * 更新经纬度信息 - */ - public function local($companyId = null) - { - $companys = Company::whereNull('company_longitude') - ->whereNotNull('company_address') - ->where('company_address', '!=', '') - ->where(function ($query) use ($companyId) { - if ($companyId) { - $query->where('id', $companyId); - } - })->get(); - - $total = $companys->count(); - if ($total == 0) { - return $this->info('没有需要更新经纬度的公司'); - } - - $this->info("开始更新经纬度信息,共 {$total} 个公司"); - $bar = $this->output->createProgressBar($total); - $bar->start(); - - // 每3个数据分一个chunk 。接口限制了一秒只能3次请求 - $companysChunk = $companys->chunk(3); - foreach ($companysChunk as $companyChunk) { - foreach ($companyChunk as $item) { - $local = Company::addressTolocation($item->company_address); - $item->company_longitude = $local['lng']; - $item->company_latitude = $local['lat']; - $item->save(); - $bar->setMessage($item->company_name . " 经纬度({$local['lng']}, {$local['lat']})更新成功", 'status'); - $bar->advance(); + if ($result['success']) { + $bar->setMessage($result['company']->company_name . ' 更新成功', 'status'); + } else { + $bar->setMessage($user->company_name . ' ' . $result['message'], 'status'); } - sleep(1); - } - - $bar->finish(); - $this->newLine(); - return $this->info('经纬度信息-全部更新完成'); - } - - /** - * 根据 company_tag 更新上市状态 - * 判断是否包含上市代码标签,如 688001.SH、000001.SZ、830001.BJ 等 - */ - public function updateMarketStatus($companyId = null) - { - $this->info('开始更新上市状态...'); - - // 获取所有有 company_tag 的公司 - $companies = Company::whereNotNull('company_tag') - ->where(function ($query) use ($companyId) { - if ($companyId) { - $query->where('id', $companyId); - } - })->where('company_tag', '!=', '') - ->get(); - - $total = $companies->count(); - if ($total == 0) { - return $this->info('没有需要更新上市状态的公司'); - } - - $bar = $this->output->createProgressBar($total); - $bar->start(); - - // 上市代码正则:匹配全球各地上市公司股票代码后缀 - // 支持的后缀:.SH,.SZ,.BJ,.TW,.HK,.SG,.US,.DE,.FR,.JP,.KR,.N,.O,.A,.PK,.Q,.TO,.AX,.L,.WS,.U,.PR,.B,.DB,.UN,.RT,.WT,.E,.C,.D,.F,.G,.H,.I,.J,.K,.L,.M,.N,.O,.P,.V,.Y,.Z - // 简化匹配:只要字符串中包含分隔符(.)+指定后缀,就算上市(只匹配.开头的,不匹配-开头的) - // 支持格式:688001.SH、AAPG.O、TSLA.US、华兴源创(688001.SH)等 - // 后缀按长度从长到短排序,避免短后缀误匹配 - $stockCodePattern = '/\.(SWR|SW|WR|SS|RS|SB|PK|TO|AX|WS|PR|DB|UN|RT|WT|SH|SZ|BJ|TW|HK|SG|US|DE|FR|JP|KR|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|U|V|W|X|Y|Z)(?![A-Za-z0-9])/i'; - - $updatedCount = 0; - foreach ($companies as $company) { - $hasStockCode = preg_match($stockCodePattern, $company->company_tag); - // 检查是否包含"新三板" - $hasXinsanban = strpos($company->company_tag, '新三板') !== false; - // 如果匹配到股票代码或包含"新三板",则标记为上市 - $newMarketStatus = ($hasStockCode || $hasXinsanban) ? 1 : 0; - - // 只有状态变化才更新 - if ($company->company_market != $newMarketStatus) { - $company->company_market = $newMarketStatus; - $company->save(); - $updatedCount++; - $statusText = $newMarketStatus ? '上市' : '非上市'; - $bar->setMessage("{$company->company_name} => {$statusText}", 'status'); - } - $bar->advance(); } $bar->finish(); $this->newLine(); - return $this->info("上市状态更新完成,共更新 {$updatedCount} 个公司"); + return $this->info('公司信息-全部更新完成'); } } diff --git a/app/Jobs/UpdateCompanyJob.php b/app/Jobs/UpdateCompanyJob.php index f599c4c..32efa41 100644 --- a/app/Jobs/UpdateCompanyJob.php +++ b/app/Jobs/UpdateCompanyJob.php @@ -7,7 +7,6 @@ namespace App\Jobs; use App\Models\Company; use App\Models\User; -use App\Repositories\YuanheRepository; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; @@ -42,135 +41,12 @@ class UpdateCompanyJob implements ShouldQueue { $user = User::find($this->userId); - if (!$user || empty($user->company_name)) { + if (!$user) { return; } - // 如果已经有公司关联,跳过 - if ($user->company_id) { - return; - } - - $YuanheRepository = new YuanheRepository(); - - // 获取公司详细信息 - $result = $YuanheRepository->companyInfo(['enterpriseName' => $user->company_name]); - - if (!$result) { - // 标识一下未匹配到公司,后续可以根据这个字段筛选出未匹配到公司的用户 - $user->company_id = 0; - $user->save(); - return; - } - - // 如果$result['enterpriseName']存在数字,跳过 - if (preg_match('/\d/', $result['enterpriseName'])) { - $user->company_id = 0; - $user->save(); - return; - } - - if ($result['status'] == '未注册') { - $user->company_id = 0; - $user->save(); - return; - } - - $where = ['company_name' => $result['enterpriseName']]; - $data = [ - 'company_address' => $result['address'], - 'business_scope' => $result['businessScope'], - 'company_city' => $result['city'], - 'contact_mail' => $result['contactMail'], - 'contact_phone' => $result['contactPhone'], - 'company_area' => $result['country'], - 'credit_code' => $result['creditCode'], - 'enterprise_id' => $result['enterpriseId'], - 'company_name' => $result['enterpriseName'], - 'is_abroad' => $result['isAbroad'], - 'company_market' => $result['isOnStock'], - 'is_yh_invested' => $result['isYhInvested'], - 'logo' => $result['logo'], - 'company_legal_representative' => $result['operName'], - 'company_province' => $result['province'], - 'company_industry' => combineKeyValue($result['qccIndustry']), - 'regist_amount' => $result['registAmount'], - 'regist_capi_type' => $result['registCapiType'], - 'company_date' => $result['startDate'], - 'status' => $result['status'], - 'stock_date' => $result['stockDate'], - 'currency_type' => $result['currencyType'], - 'stock_number' => $result['stockNumber'], - 'stock_type' => $result['stockType'], - 'company_tag' => implode(',', $result['tagList']), - // 更新日期 - 'update_date' => $result['updatedDate'] ?? null, - // 管理平台 - 'project_users' => $result['projectUsers'] ?? null, - // 股东信息 - 'partners' => $result['partners'] ?? null, - ]; - - $company = Company::updateOrCreate($where, $data); - - // 更新用户关联 - $user->company_id = $company->id; - $user->save(); - - // 更新上市状态 - $this->updateMarketStatus($company->id); - - // 更新位置 - $this->updateLocation($company->id); - } - - /** - * 更新经纬度信息 - */ - private function updateLocation($companyId) - { - $company = Company::find($companyId); - - if (!$company || empty($company->company_address) || $company->company_longitude) { - return; - } - - $local = Company::addressTolocation($company->company_address); - $company->company_longitude = $local['lng']; - $company->company_latitude = $local['lat']; - $company->save(); - } - - /** - * 根据 company_tag 更新上市状态 - * 判断是否包含上市代码标签,如 688001.SH、000001.SZ、830001.BJ 等 - */ - private function updateMarketStatus($companyId) - { - $company = Company::find($companyId); - - if (!$company || empty($company->company_tag)) { - return; - } - - // 上市代码正则:匹配全球各地上市公司股票代码后缀 - // 支持的后缀:.SH,.SZ,.BJ,.TW,.HK,.SG,.US,.DE,.FR,.JP,.KR,.N,.O,.A,.PK,.Q,.TO,.AX,.L,.WS,.U,.PR,.B,.DB,.UN,.RT,.WT,.E,.C,.D,.F,.G,.H,.I,.J,.K,.L,.M,.N,.O,.P,.V,.Y,.Z - // 简化匹配:只要字符串中包含分隔符(.)+指定后缀,就算上市(只匹配.开头的,不匹配-开头的) - // 支持格式:688001.SH、AAPG.O、TSLA.US、华兴源创(688001.SH)等 - // 后缀按长度从长到短排序,避免短后缀误匹配 - $stockCodePattern = '/\.(SWR|SW|WR|SS|RS|SB|PK|TO|AX|WS|PR|DB|UN|RT|WT|SH|SZ|BJ|TW|HK|SG|US|DE|FR|JP|KR|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|U|V|W|X|Y|Z)(?![A-Za-z0-9])/i'; - - $hasStockCode = preg_match($stockCodePattern, $company->company_tag); - // 检查是否包含"新三板" - $hasXinsanban = strpos($company->company_tag, '新三板') !== false; - // 如果匹配到股票代码或包含"新三板",则标记为上市 - $newMarketStatus = ($hasStockCode || $hasXinsanban) ? 1 : 0; - - // 只有状态变化才更新 - if ($company->company_market != $newMarketStatus) { - $company->company_market = $newMarketStatus; - $company->save(); - } + // 调用模型方法更新公司信息 + Company::updateCompanyFromUser($user); } } diff --git a/app/Models/Company.php b/app/Models/Company.php index 0d67a9f..745ecac 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -2,6 +2,8 @@ namespace App\Models; +use App\Repositories\YuanheRepository; + class Company extends SoftDeletesModel { @@ -160,5 +162,145 @@ class Company extends SoftDeletesModel } } + /** + * 根据用户信息更新公司信息 + * @param User $user 用户对象 + * @return array 返回结果 ['success' => bool, 'message' => string, 'company' => Company|null] + */ + public static function updateCompanyFromUser($user) + { + if (!$user || empty($user->company_name)) { + return ['success' => false, 'message' => '用户或公司名称为空', 'company' => null]; + } + + // 如果已经有公司关联,跳过 + if ($user->company_id) { + return ['success' => false, 'message' => '用户已有公司关联', 'company' => null]; + } + + $YuanheRepository = new YuanheRepository(); + + // 获取公司详细信息 + $result = $YuanheRepository->companyInfo(['enterpriseName' => $user->company_name]); + + if (!$result) { + // 标识一下未匹配到公司,后续可以根据这个字段筛选出未匹配到公司的用户 + $user->company_id = 0; + $user->save(); + return ['success' => false, 'message' => '公司不存在', 'company' => null]; + } + + // 如果$result['enterpriseName']存在数字,跳过 + if (preg_match('/\d/', $result['enterpriseName'])) { + $user->company_id = 0; + $user->save(); + return ['success' => false, 'message' => '公司名称包含数字,跳过', 'company' => null]; + } + + if ($result['status'] == '未注册') { + $user->company_id = 0; + $user->save(); + return ['success' => false, 'message' => '公司未注册,跳过', 'company' => null]; + } + + $where = ['company_name' => $result['enterpriseName']]; + $data = [ + 'company_address' => $result['address'], + 'business_scope' => $result['businessScope'], + 'company_city' => $result['city'], + 'contact_mail' => $result['contactMail'], + 'contact_phone' => $result['contactPhone'], + 'company_area' => $result['country'], + 'credit_code' => $result['creditCode'], + 'enterprise_id' => $result['enterpriseId'], + 'company_name' => $result['enterpriseName'], + 'is_abroad' => $result['isAbroad'], + 'company_market' => $result['isOnStock'], + 'is_yh_invested' => $result['isYhInvested'], + 'logo' => $result['logo'], + 'company_legal_representative' => $result['operName'], + 'company_province' => $result['province'], + 'company_industry' => combineKeyValue($result['qccIndustry']), + 'regist_amount' => $result['registAmount'], + 'regist_capi_type' => $result['registCapiType'], + 'company_date' => $result['startDate'], + 'status' => $result['status'], + 'stock_date' => $result['stockDate'], + 'currency_type' => $result['currencyType'], + 'stock_number' => $result['stockNumber'], + 'stock_type' => $result['stockType'], + 'company_tag' => implode(',', $result['tagList']), + // 更新日期 + 'update_date' => $result['updatedDate'] ?? null, + // 管理平台 + 'project_users' => $result['projectUsers'] ?? null, + // 股东信息 + 'partners' => $result['partners'] ?? null, + ]; + + $company = Company::updateOrCreate($where, $data); + + // 更新用户关联 + $user->company_id = $company->id; + $user->save(); + + // 更新上市状态 + self::updateMarketStatus($company->id); + + // 更新位置 + self::updateLocation($company->id); + + return ['success' => true, 'message' => '更新成功', 'company' => $company]; + } + + /** + * 更新经纬度信息 + * @param int $companyId 公司ID + * @return bool + */ + public static function updateLocation($companyId) + { + $company = Company::find($companyId); + if (!$company || empty($company->company_address) || $company->company_longitude) { + return false; + } + + $local = Company::addressTolocation($company->company_address); + $company->company_longitude = $local['lng']; + $company->company_latitude = $local['lat']; + $company->save(); + return true; + } + + /** + * 根据 company_tag 更新上市状态 + * 判断是否包含上市代码标签,如 688001.SH、000001.SZ、830001.BJ 等 + * @param int $companyId 公司ID + * @return bool 是否更新成功 + */ + public static function updateMarketStatus($companyId) + { + $company = Company::find($companyId); + if (!$company || empty($company->company_tag)) { + return false; + } + + // 上市代码正则:匹配全球各地上市公司股票代码后缀 + $stockCodePattern = '/\.(SWR|SW|WR|SS|RS|SB|PK|TO|AX|WS|PR|DB|UN|RT|WT|SH|SZ|BJ|TW|HK|SG|US|DE|FR|JP|KR|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|U|V|W|X|Y|Z)(?![A-Za-z0-9])/i'; + + $hasStockCode = preg_match($stockCodePattern, $company->company_tag); + // 检查是否包含"新三板" + $hasXinsanban = strpos($company->company_tag, '新三板') !== false; + // 如果匹配到股票代码或包含"新三板",则标记为上市 + $newMarketStatus = ($hasStockCode || $hasXinsanban) ? 1 : 0; + + // 只有状态变化才更新 + if ($company->company_market != $newMarketStatus) { + $company->company_market = $newMarketStatus; + $company->save(); + return true; + } + return false; + } }