Department.php 5.6 KB

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