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

262 lines
9.6 KiB

5 months ago
<?php
namespace App\Console\Commands;
4 months ago
use App\Models\Company;
5 months ago
use App\Models\User;
use App\Repositories\MeetRepository;
4 months ago
use App\Repositories\YuanheRepository;
5 months ago
use Illuminate\Console\Command;
class UpdateCompany extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
2 weeks ago
protected $signature = 'update_company {--user_id=} {--address=0} {--market=0}';
5 months ago
/**
* The console command description.
*
* @var string
*/
4 weeks ago
protected $description = '批量更新公司信息';
5 months ago
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
4 months ago
$user_id = $this->option('user_id');
1 week ago
$updateLocal = (int) $this->option('address');
$updateMarket = (int) $this->option('market');
4 months ago
// 更新公司信息
$this->compnay($user_id);
4 weeks ago
// 更新经纬度信息(可选)
if ($updateLocal) {
$this->local($user_id);
}
2 weeks ago
// 更新上市状态(可选)
if ($updateMarket) {
$this->updateMarketStatus();
}
4 months ago
return $this->info('全部更新完成');
}
/**
* 更新公司信息
*/
public function compnay($user_id = null)
{
if ($user_id) {
// 强制单个更新
3 weeks ago
$users = User::whereHas('courseSigns', function ($query) {
$query->where('status', 1);
})->where('id', $user_id)->get();
4 months ago
} else {
3 weeks ago
// 批量更新(只更新有报名审核通过的用户)
$users = User::whereHas('courseSigns', function ($query) {
$query->where('status', 1);
})->whereNotNull('company_name')->get();
4 months ago
}
4 weeks ago
$total = $users->count();
if ($total == 0) {
return $this->info('没有需要更新的用户');
}
$this->info("开始更新公司信息,共 {$total} 个用户");
$bar = $this->output->createProgressBar($total);
$bar->start();
4 months ago
$YuanheRepository = new YuanheRepository();
foreach ($users as $user) {
4 months ago
// 获取公司详细信息
4 months ago
$result = $YuanheRepository->companyInfo(['enterpriseName' => $user->company_name]);
4 months ago
if (!$result) {
4 weeks ago
$bar->setMessage($user->company_name . ' 公司不存在', 'status');
$bar->advance();
4 months ago
continue;
}
1 month ago
// 如果$result['enterpriseName']存在数字,跳过
if (preg_match('/\d/', $result['enterpriseName'])) {
4 weeks ago
$bar->setMessage($user->company_name . ' 公司名称包含数字,跳过', 'status');
$bar->advance();
1 month ago
continue;
}
3 weeks ago
if ($result['status'] == '未注册') {
$bar->setMessage($user->company_name . ' 公司未注册,跳过', 'status');
$bar->advance();
continue;
}
4 months ago
$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']),
4 months ago
'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']),
4 weeks ago
// 更新日期
4 weeks ago
'update_date' => $result['updatedDate'] ?? null,
4 weeks ago
// 管理平台
4 weeks ago
'project_users' => $result['projectUsers'] ?? null,
3 weeks ago
// 股东信息
'partners' => $result['partners'] ?? null,
4 months ago
];
$company = Company::updateOrCreate($where, $data);
// 更新用户关联
$user->company_id = $company->id;
$user->save();
2 weeks ago
// 更新上市状态
$this->updateMarketStatus($company->id);
4 weeks ago
$bar->setMessage($result['enterpriseName'] . ' 更新成功', 'status');
$bar->advance();
4 months ago
}
4 weeks ago
$bar->finish();
$this->newLine();
4 months ago
return $this->info('公司信息-全部更新完成');
5 months ago
}
4 months ago
/**
* 更新经纬度信息
*/
public function local($user_id = null)
{
if ($user_id) {
// 强制单个更新
$user = User::find($user_id);
if (empty($user->company_id)) {
4 weeks ago
return $this->error('用户没有关联公司');
4 months ago
}
$companys = Company::where('id', $user->company_id)->get();
} else {
// 批量更新
$companys = Company::whereNull('company_longitude')
->whereNotNUll('company_address')
->where('company_address', '!=', '')
->get();
}
4 weeks ago
$total = $companys->count();
if ($total == 0) {
return $this->info('没有需要更新经纬度的公司');
}
$this->info("开始更新经纬度信息,共 {$total} 个公司");
$bar = $this->output->createProgressBar($total);
$bar->start();
4 months ago
// 每3个数据分一个chunk 。接口限制了一秒只能3次请求
4 weeks ago
$companysChunk = $companys->chunk(3);
foreach ($companysChunk as $companyChunk) {
foreach ($companyChunk as $item) {
4 months ago
$local = Company::addressTolocation($item->company_address);
$item->company_longitude = $local['lng'];
$item->company_latitude = $local['lat'];
$item->save();
4 weeks ago
$bar->setMessage($item->company_name . " 经纬度({$local['lng']}, {$local['lat']})更新成功", 'status');
$bar->advance();
4 months ago
}
sleep(1);
}
4 weeks ago
$bar->finish();
$this->newLine();
return $this->info('经纬度信息-全部更新完成');
4 months ago
}
5 months ago
2 weeks ago
/**
* 根据 company_tag 更新上市状态
* 判断是否包含上市代码标签,如 688001.SH、000001.SZ、830001.BJ 等
*/
2 weeks ago
public function updateMarketStatus($companyId = null)
2 weeks ago
{
$this->info('开始更新上市状态...');
// 获取所有有 company_tag 的公司
2 weeks ago
if ($companyId) {
$companies = Company::where('id', $companyId)->get();
} else {
$companies = Company::whereNotNull('company_tag')
->where('company_tag', '!=', '')
->get();
}
2 weeks ago
$total = $companies->count();
if ($total == 0) {
return $this->info('没有需要更新上市状态的公司');
}
$bar = $this->output->createProgressBar($total);
$bar->start();
2 weeks ago
// 上市代码正则:匹配全球各地上市公司股票代码后缀
1 week ago
// 支持的后缀:.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
// 简化匹配:只要字符串中包含分隔符(.)+指定后缀,就算上市(只匹配.开头的,不匹配-开头的)
2 weeks ago
// 支持格式688001.SH、AAPG.O、TSLA.US、华兴源创688001.SH
// 后缀按长度从长到短排序,避免短后缀误匹配
1 week ago
$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';
2 weeks ago
$updatedCount = 0;
foreach ($companies as $company) {
$hasStockCode = preg_match($stockCodePattern, $company->company_tag);
5 days ago
// 检查是否包含"新三板"
$hasXinsanban = strpos($company->company_tag, '新三板') !== false;
// 如果匹配到股票代码或包含"新三板",则标记为上市
$newMarketStatus = ($hasStockCode || $hasXinsanban) ? 1 : 0;
2 weeks ago
// 只有状态变化才更新
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} 个公司");
}
5 months ago
}