Department.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace App\Repositories\Models\Base;
  3. use App\Repositories\Enums\ModelStatusEnum;
  4. use App\Repositories\Models\Model;
  5. use Illuminate\Support\Facades\DB;
  6. use Laravel\Lumen\Http\Request;
  7. class Department extends Model
  8. {
  9. /**
  10. * @var string
  11. */
  12. protected $table = 'base_departments';
  13. protected $guarded = [];
  14. protected $casts = [
  15. 'similar_name' => 'array'
  16. ];
  17. /**
  18. * The attributes excluded from the model's JSON form.
  19. *
  20. * @var array
  21. */
  22. protected $hidden = [];
  23. protected static function booted()
  24. {
  25. static::saved(function (Department $department) {
  26. if ($department->isDirty('parent_id')) {
  27. $new_parent_id = $department->parent_id;
  28. $old_parent_id = $department->getOriginal('parent_id');
  29. self::query()->where('id', $new_parent_id)->update(['sub_count' => self::query()->where('parent_id', $new_parent_id)->count()]);
  30. self::query()->where('id', $old_parent_id)->update(['sub_count' => self::query()->where('parent_id', $old_parent_id)->count()]);
  31. }
  32. if ($department->type) {
  33. DB::table('base_departments')->update(['company_id' => $department->id]);
  34. }
  35. });
  36. static::deleted(function (Department $department) {
  37. if ($department->isDirty('parent_id')) {
  38. $new_parent_id = $department->parent_id;
  39. $old_parent_id = $department->getOriginal('parent_id');
  40. self::query()->where('id', $new_parent_id)->update(['sub_count' => self::query()->where('parent_id', $new_parent_id)->count()]);
  41. self::query()->where('id', $old_parent_id)->update(['sub_count' => self::query()->where('parent_id', $old_parent_id)->count()]);
  42. }
  43. });
  44. }
  45. /**
  46. * 根据id获取公司id
  47. * @param $id
  48. * @return
  49. */
  50. public static function byIdGetCompanyId($id)
  51. {
  52. $model = self::query()->where('id', $id)->select(['id', 'parent_id', 'type'])->first();
  53. if (!$model) return 0;
  54. if ($model['type'] == 1) {
  55. return $model->id;
  56. }
  57. if ($model['parent_id'] == 0) {
  58. return 0;
  59. }
  60. return self::byIdGetCompanyId($model['parent_id']);
  61. }
  62. /**
  63. * 递归生成无限极分类树
  64. *
  65. * @param array $data 待分类的数组
  66. * @param int $pid 父级ID
  67. * @param int $level 缩进
  68. */
  69. public function makeTree($data, $pid = 0, $level = 0)
  70. {
  71. static $arr = [];
  72. foreach ($data as $k => $v) {
  73. if ($v->pid == $pid) {
  74. if ($v->pid != 0) {
  75. $v->name = '|' . str_repeat('--', $level) . $v->name;
  76. }
  77. $v->level = $level;
  78. $arr[] = $v;
  79. $this->makeTree($data, $v->id, $level + 2);
  80. }
  81. }
  82. return $arr;
  83. }
  84. /**
  85. *
  86. * @param Request $request
  87. * @return
  88. */
  89. public static function byIdGetChildIds($id)
  90. {
  91. static $arr = [];
  92. $arr[] = $id;
  93. $ids = self::query()->where('parent_id', $id)->pluck('id')->toArray();
  94. if (!count($ids)) return $arr;
  95. foreach ($ids as $item) {
  96. self::byIdGetChildIds($item);
  97. }
  98. return $arr;
  99. }
  100. /**
  101. * 获取跟节点
  102. * @param $id
  103. * @return array
  104. */
  105. public static function getLast($id = 0)
  106. {
  107. static $arr = [];
  108. if ($id) {
  109. $ids = self::query()->where('parent_id', $id)->pluck('id')->toArray();
  110. } else {
  111. $ids = self::query()->where('parent_id', 0)->pluck('id')->toArray();
  112. }
  113. if (count($ids)) {
  114. foreach ($ids as $id) {
  115. self::getLast($id);
  116. }
  117. } else {
  118. $arr[] = [
  119. 'id' => $id,
  120. 'level' => self::query()->where('id', $id)->value('level'),
  121. ];
  122. }
  123. sort($arr);
  124. return $arr;
  125. }
  126. /**
  127. * 获取父级
  128. * @param $id
  129. * @return array
  130. */
  131. public static function byIdGetParentName($id, $f = false)
  132. {
  133. static $arr = [];
  134. if (!$f) $arr = [];
  135. $model = self::query()->where('id', $id)->select(['id', 'name', 'parent_id'])->first();
  136. $arr[] = $model->name;
  137. if ($model->parent_id) {
  138. self::byIdGetParentName($model->parent_id, true);
  139. }
  140. $arr = array_reverse($arr);
  141. return $arr;
  142. }
  143. /**
  144. * 获取名称
  145. * @return array
  146. */
  147. public static function getNames()
  148. {
  149. $departments = self::query()->where('status', ModelStatusEnum::OK)->get();
  150. $data = [];
  151. foreach ($departments as $department) {
  152. $da = $department->similar_name;
  153. if (!is_array($da)) $da = [];
  154. $da[] = $department->name;
  155. $data[] = [
  156. 'id' => $department->id,
  157. 'names' => $da,
  158. ];
  159. }
  160. return $data;
  161. }
  162. public static function statistics()
  163. {
  164. return self::query()->where('is_hidden_statistics', 0)->where('status', 1)->pluck('id')->toArray();
  165. }
  166. }