'array' ]; /** * The attributes excluded from the model's JSON form. * * @var array */ protected $hidden = []; protected static function booted() { static::saved(function (Department $department) { if ($department->isDirty('parent_id')) { $new_parent_id = $department->parent_id; $old_parent_id = $department->getOriginal('parent_id'); self::query()->where('id', $new_parent_id)->update(['sub_count' => self::query()->where('parent_id', $new_parent_id)->count()]); self::query()->where('id', $old_parent_id)->update(['sub_count' => self::query()->where('parent_id', $old_parent_id)->count()]); } if ($department->type) { DB::table('base_departments')->update(['company_id' => $department->id]); } }); static::deleted(function (Department $department) { if ($department->isDirty('parent_id')) { $new_parent_id = $department->parent_id; $old_parent_id = $department->getOriginal('parent_id'); self::query()->where('id', $new_parent_id)->update(['sub_count' => self::query()->where('parent_id', $new_parent_id)->count()]); self::query()->where('id', $old_parent_id)->update(['sub_count' => self::query()->where('parent_id', $old_parent_id)->count()]); } }); } /** * 根据id获取公司id * @param $id * @return */ public static function byIdGetCompanyId($id) { $model = self::query()->where('id', $id)->select(['id', 'parent_id', 'type'])->first(); if (!$model) return 0; if ($model['type'] == 1) { return $model->id; } if ($model['parent_id'] == 0) { return 0; } return self::byIdGetCompanyId($model['parent_id']); } /** * 递归生成无限极分类树 * * @param array $data 待分类的数组 * @param int $pid 父级ID * @param int $level 缩进 */ public function makeTree($data, $pid = 0, $level = 0) { static $arr = []; foreach ($data as $k => $v) { if ($v->pid == $pid) { if ($v->pid != 0) { $v->name = '|' . str_repeat('--', $level) . $v->name; } $v->level = $level; $arr[] = $v; $this->makeTree($data, $v->id, $level + 2); } } return $arr; } /** * * @param Request $request * @return */ public static function byIdGetChildIds($id) { static $arr = []; $arr[] = $id; $ids = self::query()->where('parent_id', $id)->pluck('id')->toArray(); if (!count($ids)) return $arr; foreach ($ids as $item) { self::byIdGetChildIds($item); } return $arr; } /** * 获取跟节点 * @param $id * @return array */ public static function getLast($id = 0) { static $arr = []; if ($id) { $ids = self::query()->where('parent_id', $id)->pluck('id')->toArray(); } else { $ids = self::query()->where('parent_id', 0)->pluck('id')->toArray(); } if (count($ids)) { foreach ($ids as $id) { self::getLast($id); } } else { $arr[] = [ 'id' => $id, 'level' => self::query()->where('id', $id)->value('level'), ]; } sort($arr); return $arr; } /** * 获取父级 * @param $id * @return array */ public static function byIdGetParentName($id, $f = false) { static $arr = []; if (!$f) $arr = []; $model = self::query()->where('id', $id)->select(['id', 'name', 'parent_id'])->first(); $arr[] = $model->name; if ($model->parent_id) { self::byIdGetParentName($model->parent_id, true); } $arr = array_reverse($arr); return $arr; } /** * 获取名称 * @return array */ public static function getNames() { $departments = self::query()->where('status', ModelStatusEnum::OK)->get(); $data = []; foreach ($departments as $department) { $da = $department->similar_name; if (!is_array($da)) $da = []; $da[] = $department->name; $data[] = [ 'id' => $department->id, 'names' => $da, ]; } return $data; } public static function statistics() { return self::query()->where('is_hidden_statistics', 0)->where('status', 1)->pluck('id')->toArray(); } }