Files
jianlizaojia/plugs/think-plugs-staff/src/model/StaffDept.php
2025-06-21 16:11:58 +08:00

61 lines
1.4 KiB
PHP

<?php
namespace jerryyan\staff\model;
use think\admin\Model;
use think\db\Query;
use think\model\relation\HasMany;
use think\model\relation\HasOne;
class StaffDept extends Model
{
protected $createTime = 'create_at';
protected $updateTime = false;
protected $oplogName = '部门';
protected $oplogType = '部门管理';
protected $globalScope = ['not_deleted'];
public function scopeDeleted(Query $query): void
{
$query->where(['is_deleted' => 1]);
}
public function scopeNotDeleted(Query $query): void
{
$query->where(['is_deleted' => 0]);
}
public static function items()
{
return self::mk()->where(['status' => 1])->select();
}
public function staffs(): HasMany
{
return $this->hasMany(StaffUser::class, 'dept_id', 'id')->where([
'status' => 1, 'is_deleted' => 0,
]);
}
public function headman(): HasOne
{
return $this->hasOne(StaffUser::class, 'id', 'headman_id')->where([
'status' => 1, 'is_deleted' => 0,
]);
}
public function children(): HasMany
{
return $this->hasMany(StaffDept::class, 'pid', 'id');
}
public function parent(): HasOne
{
return $this->hasOne(StaffDept::class, 'id', 'pid');
}
public static function tree()
{
return static::mk()->where(['is_deleted' => 0, 'pid' => 0])->with('children.children')->select();
}
}