This commit is contained in:
2025-06-21 16:39:10 +08:00
commit 7ff2185f8e
17 changed files with 1058 additions and 0 deletions

77
src/model/StaffDept.php Normal file
View File

@ -0,0 +1,77 @@
<?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 = ['notDeleted'];
public static function topItems()
{
return self::where(['pid' => 0])->field("id, name")->select()->unshift([
'id' => 0,
'name' => '顶级部门',
]);
}
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();
}
public function getHeadmanNameAttr()
{
if (!$this->headman_id) {
return '无';
}
return $this->headman ? $this->headman['name'] : '无';
}
}

38
src/model/StaffUser.php Normal file
View File

@ -0,0 +1,38 @@
<?php
namespace jerryyan\staff\model;
use think\admin\Model;
use think\db\Query;
use think\model\relation\HasOne;
class StaffUser extends Model
{
protected $createTime = 'create_at';
protected $updateTime = false;
protected $oplogName = '部门员工';
protected $oplogType = '部门员工管理';
protected $globalScope = ['notDeleted'];
public function scopeDeleted(Query $query): void
{
$query->where('is_deleted', '=', 1);
}
public function scopeNotDeleted(Query $query): void
{
$query->where('is_deleted', '=', 0);
}
public function dept(): HasOne
{
return $this->hasOne(StaffDept::class, 'id', 'dept_id')->where([
'status' => 1, 'is_deleted' => 0,
]);
}
public function getDeptNameAttr($value, $data)
{
return $this->dept ? $this->dept['name'] : '';
}
}