|
|
|
|
@ -176,11 +176,15 @@ class Company extends SoftDeletesModel
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据用户信息更新公司信息
|
|
|
|
|
* 根据用户信息更新/同步公司信息(统一方法)
|
|
|
|
|
* @param User $user 用户对象
|
|
|
|
|
* @param bool $skipIfHasCompany 如果已有公司关联(company_id > 0)是否跳过,默认true
|
|
|
|
|
* @param bool $updateAddress 是否更新地址,默认true
|
|
|
|
|
* @param bool $updateLocation 是否更新经纬度,默认true
|
|
|
|
|
* @param bool $setCompanyIdOnFail 失败时是否设置company_id=0,默认true
|
|
|
|
|
* @return array 返回结果 ['success' => bool, 'message' => string, 'company' => Company|null]
|
|
|
|
|
*/
|
|
|
|
|
public static function updateCompanyFromUser($user)
|
|
|
|
|
public static function updateCompanyFromUser($user, $skipIfHasCompany = true, $updateAddress = true, $updateLocation = true, $setCompanyIdOnFail = true)
|
|
|
|
|
{
|
|
|
|
|
if (!$user || empty($user->company_name)) {
|
|
|
|
|
return ['success' => false, 'message' => '用户或公司名称为空', 'company' => null];
|
|
|
|
|
@ -188,7 +192,7 @@ class Company extends SoftDeletesModel
|
|
|
|
|
|
|
|
|
|
// 如果已经有有效的公司关联(company_id > 0),跳过
|
|
|
|
|
// 允许处理 company_id = -1(待更新)或 null(初始状态)的情况
|
|
|
|
|
if ($user->company_id && $user->company_id > 0) {
|
|
|
|
|
if ($skipIfHasCompany && $user->company_id && $user->company_id > 0) {
|
|
|
|
|
return ['success' => false, 'message' => '用户已有公司关联', 'company' => null];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -213,27 +217,32 @@ class Company extends SoftDeletesModel
|
|
|
|
|
|
|
|
|
|
if (!$result) {
|
|
|
|
|
// 标识一下未匹配到公司,后续可以根据这个字段筛选出未匹配到公司的用户
|
|
|
|
|
$user->company_id = 0;
|
|
|
|
|
$user->save();
|
|
|
|
|
if ($setCompanyIdOnFail) {
|
|
|
|
|
$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();
|
|
|
|
|
if ($setCompanyIdOnFail) {
|
|
|
|
|
$user->company_id = 0;
|
|
|
|
|
$user->save();
|
|
|
|
|
}
|
|
|
|
|
return ['success' => false, 'message' => '公司名称包含数字,跳过', 'company' => null];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($result['status'] == '未注册') {
|
|
|
|
|
$user->company_id = 0;
|
|
|
|
|
$user->save();
|
|
|
|
|
if ($setCompanyIdOnFail) {
|
|
|
|
|
$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'],
|
|
|
|
|
@ -266,6 +275,11 @@ class Company extends SoftDeletesModel
|
|
|
|
|
'partners' => $result['partners'] ?? null,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// 根据参数决定是否更新地址
|
|
|
|
|
if ($updateAddress) {
|
|
|
|
|
$data['company_address'] = $result['address'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$company = Company::updateOrCreate($where, $data);
|
|
|
|
|
|
|
|
|
|
// 更新用户关联
|
|
|
|
|
@ -275,12 +289,25 @@ class Company extends SoftDeletesModel
|
|
|
|
|
// 更新上市状态
|
|
|
|
|
self::updateMarketStatus($company->id);
|
|
|
|
|
|
|
|
|
|
// 更新位置
|
|
|
|
|
self::updateLocation($company->id);
|
|
|
|
|
// 根据参数决定是否更新位置(经纬度)
|
|
|
|
|
if ($updateLocation) {
|
|
|
|
|
self::updateLocation($company->id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ['success' => true, 'message' => '更新成功', 'company' => $company];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 全量同步公司信息(不包含地址和经纬度)
|
|
|
|
|
* @param User $user 用户对象
|
|
|
|
|
* @return array 返回结果 ['success' => bool, 'message' => string, 'company' => Company|null]
|
|
|
|
|
*/
|
|
|
|
|
public static function syncCompanyFromUser($user)
|
|
|
|
|
{
|
|
|
|
|
// 调用统一方法,参数设置为:不跳过已有公司、不更新地址、不更新经纬度、失败时不设置company_id
|
|
|
|
|
return self::updateCompanyFromUser($user, false, false, false, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 更新经纬度信息
|
|
|
|
|
* @param int $companyId 公司ID
|
|
|
|
|
|