Files
x-server-mobile/backend/app/Model/Company.php
T

94 lines
1.8 KiB
PHP
Raw Normal View History

<?php
namespace App\Model;
use support\Model;
class Company extends Model
{
protected $table = 'companies';
protected $fillable = [
'name',
'abbr',
'tenant_key',
'sort',
'status',
'feishu_enabled',
];
protected $casts = [
'sort' => 'integer',
'status' => 'integer',
'feishu_enabled' => 'boolean',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
/**
* 关联用户
*/
public function users()
{
return $this->hasMany(User::class, 'company_id');
}
/**
* 获取启用的公司
*/
public function scopeActive($query)
{
return $query->where('status', 1);
}
/**
* 根据租户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}%");
});
}
/**
* 获取飞书应用配置
*/
public function feishuApps()
{
return $this->hasMany(CompanyFeishuApp::class, 'company_id');
}
/**
* 获取默认飞书应用(X-Server)
*/
public function getDefaultFeishuApp()
{
return $this->hasOne(CompanyFeishuApp::class, 'company_id')
->where('system_id', 1)
->where('status', 1)
->orderBy('sort');
}
}