123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- <?php
- namespace App\Repositories\Models\Base;
- use App\Repositories\Enums\ModelStatusEnum;
- use App\Repositories\Models\Model;
- use Illuminate\Support\Facades\DB;
- use Laravel\Lumen\Http\Request;
- class Department extends Model
- {
- /**
- * @var string
- */
- protected $table = 'base_departments';
- protected $guarded = [];
- protected $casts = [
- 'similar_name' => '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();
- }
- }
|