From ada56ca4c799d7ad68b4b15e47589b16be095c21 Mon Sep 17 00:00:00 2001 From: cody <648753004@qq.com> Date: Thu, 26 Mar 2026 12:33:28 +0800 Subject: [PATCH] 1 --- app/Models/Company.php | 188 +++++++++++++++++++++++++++-------------- 1 file changed, 126 insertions(+), 62 deletions(-) diff --git a/app/Models/Company.php b/app/Models/Company.php index fde355a..fdb679b 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Repositories\YuanheRepository; +use Illuminate\Support\Arr; class Company extends SoftDeletesModel { @@ -163,30 +164,46 @@ class Company extends SoftDeletesModel return ['success' => false, 'message' => '用户已有公司关联', 'company' => null]; } - // 清理公司名称:去除首尾空格、换行符、制表符等异常字符 - $cleanedCompanyName = trim($user->company_name); - // 去除换行符、回车符、制表符等空白字符 - $cleanedCompanyName = preg_replace('/[\r\n\t]+/', '', $cleanedCompanyName); - // 将多个连续空格替换为单个空格 - $cleanedCompanyName = preg_replace('/\s+/', ' ', $cleanedCompanyName); - // 再次去除首尾空格 - $cleanedCompanyName = trim($cleanedCompanyName); + $cleanedCompanyName = self::normalizeCompanyName($user->company_name); // 如果清理后为空,返回错误 if (empty($cleanedCompanyName)) { return ['success' => false, 'message' => '公司名称无效', 'company' => null]; } - $YuanheRepository = new YuanheRepository(); + $result = self::getCompanyDetailByName($cleanedCompanyName); + + return self::syncUserCompanyByDetail($user, $result); + } + + public static function updateCompanyFromCache($user) + { + if (!$user || empty($user->company_name)) { + return ['success' => false, 'message' => '用户或公司名称为空', 'company' => null]; + } + + if ($user->company_id && $user->company_id > 0) { + return ['success' => false, 'message' => '用户已有公司关联', 'company' => null]; + } + + $cleanedCompanyName = self::normalizeCompanyName($user->company_name); + if (empty($cleanedCompanyName)) { + return ['success' => false, 'message' => '公司名称无效', 'company' => null]; + } + + $result = self::getCachedCompanyDetailByName($cleanedCompanyName); - // 获取公司详细信息,使用清理后的公司名称 - $result = $YuanheRepository->companyInfo(['enterpriseName' => $cleanedCompanyName]); + return self::syncUserCompanyByDetail($user, $result, '公司缓存不存在'); + } + + protected static function syncUserCompanyByDetail($user, $result, $notFoundMessage = '公司不存在') + { if (!$result) { // 标识一下未匹配到公司,后续可以根据这个字段筛选出未匹配到公司的用户 $user->company_id = 0; $user->save(); - return ['success' => false, 'message' => '公司不存在', 'company' => null]; + return ['success' => false, 'message' => $notFoundMessage, 'company' => null]; } // 如果$result['enterpriseName']存在数字,跳过 @@ -203,40 +220,7 @@ class Company extends SoftDeletesModel } $where = ['company_name' => $result['enterpriseName']]; - $data = [ - '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_address' => $result['address'], - ]; + $data = self::buildCompanyDataFromDetail($result, true); $company = Company::updateOrCreate($where, $data); @@ -264,20 +248,13 @@ class Company extends SoftDeletesModel return ['success' => false, 'message' => '公司或公司名称为空', 'company' => null]; } - // 清理公司名称 - $cleanedCompanyName = trim($company->company_name); - $cleanedCompanyName = preg_replace('/[\r\n\t]+/', '', $cleanedCompanyName); - $cleanedCompanyName = preg_replace('/\s+/', ' ', $cleanedCompanyName); - $cleanedCompanyName = trim($cleanedCompanyName); + $cleanedCompanyName = self::normalizeCompanyName($company->company_name); if (empty($cleanedCompanyName)) { return ['success' => false, 'message' => '公司名称无效', 'company' => null]; } - $YuanheRepository = new YuanheRepository(); - - // 获取公司详细信息 - $result = $YuanheRepository->companyInfo(['enterpriseName' => $cleanedCompanyName]); + $result = self::fetchCompanyDetailFromApi($cleanedCompanyName); if (!$result) { return ['success' => false, 'message' => '公司不存在', 'company' => null]; @@ -293,6 +270,95 @@ class Company extends SoftDeletesModel } // 更新公司数据(不包含地址和经纬度) + $data = self::buildCompanyDataFromDetail($result, false); + + $company->fill($data); + $company->save(); + + // 更新上市状态 + self::updateMarketStatus($company->id); + + return ['success' => true, 'message' => '更新成功', 'company' => $company]; + } + + public static function normalizeCompanyName($companyName) + { + if ($companyName === null) { + return null; + } + + $companyName = trim($companyName); + $companyName = preg_replace('/[\r\n\t]+/', '', $companyName); + $companyName = preg_replace('/\s+/', ' ', $companyName); + + return trim($companyName); + } + + protected static function getCompanyDetailByName($companyName) + { + $normalizedCompanyName = self::normalizeCompanyName($companyName); + if (empty($normalizedCompanyName)) { + return false; + } + + $cachePayload = self::getCachedCompanyDetailByName($normalizedCompanyName); + if ($cachePayload) { + return $cachePayload; + } + + return self::fetchCompanyDetailFromApi($normalizedCompanyName); + } + + protected static function getCachedCompanyDetailByName($companyName) + { + $normalizedCompanyName = self::normalizeCompanyName($companyName); + if (empty($normalizedCompanyName)) { + return false; + } + + $cache = CompanyDetailCache::query() + ->where('normalized_company_name', $normalizedCompanyName) + ->orWhere('normalized_enterprise_name', $normalizedCompanyName) + ->orderByDesc('fetched_at') + ->first(); + + if (!$cache || empty($cache->payload)) { + return false; + } + + $cache->last_matched_at = now(); + $cache->save(); + + return $cache->payload; + } + + protected static function fetchCompanyDetailFromApi($companyName) + { + $YuanheRepository = new YuanheRepository(); + $result = $YuanheRepository->companyInfo(['enterpriseName' => $companyName]); + + if (!$result) { + return false; + } + + CompanyDetailCache::updateOrCreate( + ['normalized_company_name' => self::normalizeCompanyName($companyName)], + [ + 'query_company_name' => $companyName, + 'enterprise_name' => Arr::get($result, 'enterpriseName'), + 'normalized_enterprise_name' => self::normalizeCompanyName(Arr::get($result, 'enterpriseName')), + 'credit_code' => Arr::get($result, 'creditCode'), + 'enterprise_id' => Arr::get($result, 'enterpriseId'), + 'payload' => $result, + 'fetched_at' => now(), + ] + ); + + return $result; + } + + protected static function buildCompanyDataFromDetail(array $result, $includeAddress = false) + { $data = [ 'business_scope' => $result['businessScope'], 'company_city' => $result['city'], @@ -317,19 +383,17 @@ class Company extends SoftDeletesModel 'currency_type' => $result['currencyType'], 'stock_number' => $result['stockNumber'], 'stock_type' => $result['stockType'], - 'company_tag' => implode(',', $result['tagList']), + 'company_tag' => implode(',', $result['tagList'] ?? []), 'update_date' => $result['updatedDate'] ?? null, 'project_users' => $result['projectUsers'] ?? null, 'partners' => $result['partners'] ?? null, ]; - $company->fill($data); - $company->save(); - - // 更新上市状态 - self::updateMarketStatus($company->id); + if ($includeAddress) { + $data['company_address'] = $result['address'] ?? null; + } - return ['success' => true, 'message' => '更新成功', 'company' => $company]; + return $data; } /**