Department.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace app\admin\model;
  3. use app\BaseModel;
  4. class Department extends BaseModel
  5. {
  6. protected $autoWriteTimestamp = true;
  7. protected $createTime = 'created_at';
  8. protected $updateTime = 'updated_at';
  9. //获取指定部门的下级树形结构
  10. public static function getChildrenTree($department_id) {
  11. //查询所有下级部门
  12. $list = self::order('parent_id', 'asc')
  13. ->order('weight', 'desc')
  14. ->select();
  15. $tree = linear_to_tree($list, 'children', 'id', 'parent_id', $department_id);
  16. return $tree;
  17. }
  18. //获取所有下级的ID
  19. public static function getChildrenIds($children) {
  20. $ids = [];
  21. foreach ($children as $value) {
  22. $ids[] = $value['id'];
  23. if (!empty($value['children'])) {
  24. $ids = array_merge($ids, self::getChildrenIds($value['children']));
  25. }
  26. }
  27. return $ids;
  28. }
  29. //获取部门下级的所有客服ID
  30. public static function getDepartmentCsUids($admin_id) {
  31. $department_id = Admin::where('id', $admin_id)->value('department_id');
  32. $tree = self::getChildrenTree($department_id);
  33. $department_ids = self::getChildrenIds($tree);
  34. $cs_uids = Admin::alias('admin')->join('user', 'admin.id=user.uid','left')
  35. ->whereIn('admin.department_id', $department_ids)
  36. ->where('user.role', '>', 0)
  37. ->column('user.user_id');
  38. return $cs_uids;
  39. }
  40. }