2026-02-24 17:57:25 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Model;
|
|
|
|
|
|
|
|
|
|
use support\Model;
|
|
|
|
|
|
|
|
|
|
class Company extends Model
|
|
|
|
|
{
|
2026-02-24 18:12:18 +08:00
|
|
|
protected $table = 'companies';
|
2026-02-24 17:57:25 +08:00
|
|
|
|
|
|
|
|
protected $fillable = [
|
|
|
|
|
'name',
|
2026-02-24 18:12:18 +08:00
|
|
|
'abbr',
|
|
|
|
|
'tenant_key',
|
|
|
|
|
'sort',
|
2026-02-24 17:57:25 +08:00
|
|
|
'status',
|
2026-02-24 18:12:18 +08:00
|
|
|
'feishu_enabled',
|
2026-02-24 17:57:25 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
protected $casts = [
|
2026-02-24 18:12:18 +08:00
|
|
|
'sort' => 'integer',
|
2026-02-24 17:57:25 +08:00
|
|
|
'status' => 'integer',
|
2026-02-24 18:12:18 +08:00
|
|
|
'feishu_enabled' => 'boolean',
|
2026-02-24 17:57:25 +08:00
|
|
|
'created_at' => 'datetime',
|
|
|
|
|
'updated_at' => 'datetime',
|
|
|
|
|
];
|
|
|
|
|
|
2026-02-24 18:12:18 +08:00
|
|
|
/**
|
|
|
|
|
* 关联用户
|
|
|
|
|
*/
|
|
|
|
|
public function users()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(User::class, 'company_id');
|
|
|
|
|
}
|
2026-02-24 17:57:25 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取启用的公司
|
|
|
|
|
*/
|
|
|
|
|
public function scopeActive($query)
|
|
|
|
|
{
|
|
|
|
|
return $query->where('status', 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-02-24 18:12:18 +08:00
|
|
|
* 根据租户Key查找
|
|
|
|
|
*/
|
|
|
|
|
public function scopeByTenantKey($query, string $tenantKey)
|
|
|
|
|
{
|
|
|
|
|
return $query->where('tenant_key', $tenantKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据缩写查找
|
|
|
|
|
*/
|
|
|
|
|
public function scopeByAbbr($query, string $abbr)
|
|
|
|
|
{
|
|
|
|
|
return $query->where('abbr', $abbr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 搜索公司
|
|
|
|
|
*/
|
|
|
|
|
public function scopeSearch($query, ?string $keyword)
|
|
|
|
|
{
|
|
|
|
|
if (!$keyword) {
|
|
|
|
|
return $query;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $query->where(function ($q) use ($keyword) {
|
|
|
|
|
$q->where('name', 'like', "%{$keyword}%")
|
|
|
|
|
->orWhere('abbr', 'like', "%{$keyword}%");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取飞书应用配置
|
2026-02-24 17:57:25 +08:00
|
|
|
*/
|
2026-02-24 18:12:18 +08:00
|
|
|
public function feishuApps()
|
2026-02-24 17:57:25 +08:00
|
|
|
{
|
2026-02-24 18:12:18 +08:00
|
|
|
return $this->hasMany(CompanyFeishuApp::class, 'company_id');
|
2026-02-24 17:57:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-02-24 18:12:18 +08:00
|
|
|
* 获取默认飞书应用(X-Server)
|
2026-02-24 17:57:25 +08:00
|
|
|
*/
|
2026-02-24 18:12:18 +08:00
|
|
|
public function getDefaultFeishuApp()
|
2026-02-24 17:57:25 +08:00
|
|
|
{
|
2026-02-24 18:12:18 +08:00
|
|
|
return $this->hasOne(CompanyFeishuApp::class, 'company_id')
|
|
|
|
|
->where('system_id', 1)
|
|
|
|
|
->where('status', 1)
|
|
|
|
|
->orderBy('sort');
|
2026-02-24 17:57:25 +08:00
|
|
|
}
|
|
|
|
|
}
|