You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.5 KiB
56 lines
1.5 KiB
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
class Department extends SoftDeletesModel
|
|
{
|
|
protected $table = "departments";
|
|
|
|
public function parent()
|
|
{
|
|
return $this->belongsTo(Department::class, "pid");
|
|
}
|
|
|
|
public function users()
|
|
{
|
|
return $this->hasMany(Admin::class, "department_id");
|
|
}
|
|
|
|
public function leader()
|
|
{
|
|
return $this->hasOne(Admin::class, "id", "leader_id");
|
|
}
|
|
|
|
public function manager()
|
|
{
|
|
return $this->hasOne(Admin::class, "id", "manager_id");
|
|
}
|
|
|
|
public function getTopDepartments()
|
|
{
|
|
$all = Department::get()->toArray();
|
|
$top_departments = $this->where("pid", 0)->get();
|
|
foreach ($top_departments as $department) {
|
|
$department->descendants = find_children($all, "id", "pid", $department->id);
|
|
}
|
|
return $top_departments;
|
|
}
|
|
|
|
public function getSelections()
|
|
{
|
|
$departments = $this->orderby("myindex")->get()->toArray();
|
|
$departments = array2tree($departments);
|
|
$departments = flatten_tree($departments);
|
|
$level_symbol = "├";
|
|
foreach ($departments as &$department) {
|
|
$level_symbol_prefix = "";
|
|
for ($i = 0; $i < $department["level"]; $i++) {
|
|
$level_symbol_prefix .= $level_symbol;
|
|
}
|
|
$department["name"] = $level_symbol_prefix . $department["title"];
|
|
}
|
|
$departments = collect($departments)->pluck("name", "id")->toArray();
|
|
return $departments;
|
|
}
|
|
}
|