TeamController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace app\admin\controller\consultant;
  3. use app\admin\service\consultant\TeamService;
  4. use app\common\Tree;
  5. use app\controller\Curd;
  6. use app\model\Consultant;
  7. use app\model\SysDept;
  8. use app\model\SysUser;
  9. use support\Request;
  10. use support\Response;
  11. use Tinywan\Jwt\JwtToken;
  12. class TeamController extends Curd
  13. {
  14. public function __construct()
  15. {
  16. $this->model = new SysDept();
  17. }
  18. public function select(Request $request): Response
  19. {
  20. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  21. $format = 'tree';
  22. $order = $request->get('order', 'desc');
  23. $field = $field ?? 'dept_addtimes';
  24. $where['dept_category'] = '获客团队';
  25. $deptIds = TeamService::getIdsByUser();
  26. if (false === $deptIds) {
  27. //无权限
  28. $where['dept_id'] = ['=', 0];
  29. } else if (is_array($deptIds)) {
  30. //指定团队下的权限
  31. $where['dept_id'] = ['in', $deptIds];
  32. }
  33. $query = $this->doSelect($where, $field, $order);
  34. return $this->doFormat($query, $format, 1000);
  35. }
  36. protected function afterQuery($items)
  37. {
  38. foreach ($items as &$item) {
  39. $item->nums = Consultant::where('dept_id',$item->dept_id)->orWhere('top_dept_id', $item->dept_id)->count();
  40. }
  41. return $items;
  42. }
  43. protected function formatTree($items): Response
  44. {
  45. $format_items = [];
  46. foreach ($items as $item) {
  47. $format_items[] = [
  48. 'name' => $item->dept_name,
  49. 'value' => (string)$item->dept_id,
  50. 'id' => $item->id ?? $item->dept_id,
  51. 'pid' => $item->pid ?? $item->dept_super_id,
  52. 'dept_id' => $item->dept_id,
  53. 'dept_name' => $item->dept_name,
  54. 'dept_super_id' => $item->dept_super_id,
  55. 'dept_status' => $item->dept_status,
  56. 'dept_category' => $item->dept_category,
  57. 'dept_code' => $item->dept_code,
  58. 'dept_telephone' => $item->dept_telephone,
  59. 'category_addtimes' => $item->category_addtimes,
  60. 'nums' => $item->nums
  61. ];
  62. }
  63. $tree = new Tree($format_items);
  64. return json_success('success', $tree->getTree());
  65. }
  66. /**
  67. * Notes: 上级团队
  68. * User: yb
  69. * Date: 2024/8/1
  70. * Time: 17:16
  71. */
  72. public function parentList()
  73. {
  74. return TeamService::parentList();
  75. }
  76. /**
  77. * @Desc 部门详情
  78. * @Author Gorden
  79. * @Date 2024/2/21 10:35
  80. *
  81. * @param $id
  82. * @return \support\Response
  83. */
  84. public function deptInfo($id)
  85. {
  86. return TeamService::deptInfo($id);
  87. }
  88. /**
  89. * @Desc 创建部门
  90. * @Author Gorden
  91. * @Date 2024/2/21 9:22
  92. *
  93. * @param Request $request
  94. * @return \support\Response
  95. */
  96. public function addDept(Request $request)
  97. {
  98. $params = $request->post();
  99. if (empty($params['dept_name'])) {
  100. return json_fail('团队名称不能为空');
  101. }
  102. return TeamService::insertDept($params);
  103. }
  104. /**
  105. * @Desc 修改部门
  106. * @Author Gorden
  107. * @Date 2024/2/21 10:15
  108. *
  109. * @param $id
  110. * @param Request $request
  111. * @return \support\Response
  112. */
  113. public function updateDept(Request $request)
  114. {
  115. $params = $request->post();
  116. if (empty($params['id'])) {
  117. return json_fail('团队id不能为空');
  118. }
  119. if (empty($params['dept_name'])) {
  120. return json_fail('团队名称不能为空');
  121. }
  122. return TeamService::updateDept($params);
  123. }
  124. public function delDept(Request $request)
  125. {
  126. return TeamService::delDept($request);
  127. }
  128. /**
  129. * Notes: 检查管理员的部门信息
  130. * User: yb
  131. * Date: 2024/8/16
  132. * Time: 17:28
  133. * @return Response
  134. */
  135. public function checkDept()
  136. {
  137. $deptIds = TeamService::getIdsByUser(1);
  138. if (false === $deptIds) {
  139. //无权限
  140. $authDeptId = -1;
  141. } else if (is_array($deptIds)) {
  142. $userId = JwtToken::getCurrentId();
  143. $userInfo = SysUser::firstWhere(['user_id' => $userId]);
  144. $authDeptId = $userInfo->join_user_dept_id;
  145. } else {
  146. //超管权限
  147. $authDeptId = -2;
  148. }
  149. return json_success('请求成功', ['auth_dept_id' =>$authDeptId]);
  150. }
  151. }