SystemDeptLogic.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: sai <1430792918@qq.com>
  6. // +----------------------------------------------------------------------
  7. namespace plugin\saiadmin\app\logic\system;
  8. use plugin\saiadmin\basic\BaseLogic;
  9. use plugin\saiadmin\exception\ApiException;
  10. use plugin\saiadmin\app\model\system\SystemDept;
  11. use plugin\saiadmin\app\model\system\SystemUser;
  12. use plugin\saiadmin\app\model\system\SystemDeptLeader;
  13. use plugin\saiadmin\utils\Helper;
  14. use plugin\saiadmin\utils\Arr;
  15. /**
  16. * 部门逻辑层
  17. */
  18. class SystemDeptLogic extends BaseLogic
  19. {
  20. // protected $gameLogic;
  21. /**
  22. * 构造函数
  23. */
  24. public function __construct()
  25. {
  26. $this->model = new SystemDept();
  27. // $this->gameLogic = new GameLogic();
  28. }
  29. /**
  30. * 添加数据
  31. */
  32. public function add($data): mixed
  33. {
  34. $data = $this->handleData($data);
  35. $this->model->save($data);
  36. return $this->model->getKey();
  37. }
  38. /**
  39. * 修改数据
  40. */
  41. public function edit($id, $data): mixed
  42. {
  43. $oldLevel = $data['level'].",".$id;
  44. $data = $this->handleData($data);
  45. if ($data['parent_id'] == $id) {
  46. throw new ApiException('不能设置父级为自身');
  47. }
  48. if (in_array($id, explode(',', $data['level']))) {
  49. throw new ApiException('不能设置父级为下级部门');
  50. }
  51. $newLevel = $data['level'].",".$id;
  52. $deptIds = $this->model->whereRaw('FIND_IN_SET("'.$id.'", level) > 0')->column('id');
  53. $this->model->whereIn('id', $deptIds)->exp('level', "REPLACE(level, '$oldLevel', '$newLevel')")->update();
  54. return $this->model->update($data, ['id' => $id]);
  55. }
  56. /**
  57. * 数据删除
  58. */
  59. public function destroy($ids)
  60. {
  61. $num = $this->model->where('parent_id', 'in', $ids)->count();
  62. if ($num > 0) {
  63. throw new ApiException('该部门下存在子部门,请先删除子部门');
  64. } else {
  65. $count = SystemUser::where('dept_id', 'in', $ids)->count();
  66. if ($count > 0) {
  67. throw new ApiException('该部门下存在用户,请先删除或者转移用户');
  68. }
  69. return $this->model->destroy($ids);
  70. }
  71. }
  72. /**
  73. * 数据处理
  74. */
  75. protected function handleData($data)
  76. {
  77. if (empty($data['parent_id']) || $data['parent_id'] == 0) {
  78. $data['level'] = '0';
  79. $data['parent_id'] = 0;
  80. } else {
  81. $parentMenu = SystemDept::findOrEmpty($data['parent_id']);
  82. $data['level'] = $parentMenu['level'] . ',' . $parentMenu['id'];
  83. }
  84. return $data;
  85. }
  86. /**
  87. * 数据树形化
  88. * @param array $where
  89. * @return array
  90. */
  91. public function tree(array $where = []): array
  92. {
  93. $query = $this->search($where);
  94. if (request()->input('tree', 'false') === 'true') {
  95. $query->field('id, id as value, name as label, parent_id');
  96. }
  97. $query->order('sort', 'desc');
  98. $query->with(['leader' => function($query) {
  99. $query->field('id, username, nickname');
  100. }]);
  101. $data = $this->getAll($query);
  102. return Helper::makeTree($data);
  103. }
  104. /**
  105. * 可操作部门
  106. * @param array $where
  107. * @return array
  108. */
  109. public function accessDept(array $where = []): array
  110. {
  111. $query = $this->search($where);
  112. $query->auth($this->adminInfo['deptList']);
  113. $query->field('id, id as value, name as label, parent_id');
  114. $query->order('sort', 'desc');
  115. $data = $this->getAll($query);
  116. foreach ($data as &$item) {
  117. if ($item['id'] === $this->adminInfo['dept_id']) {
  118. $item['disabled'] = true;
  119. } else {
  120. $item['disabled'] = false;
  121. }
  122. }
  123. return Helper::makeTree($data);
  124. }
  125. /**
  126. * 组长列表
  127. */
  128. public function leaders($where = [])
  129. {
  130. $dept_id = $where['dept_id'];
  131. unset($where['dept_id']);
  132. $logic = new SystemUserLogic();
  133. $query = $logic->search($where)->alias('user')->join('sa_system_dept_leader dept', 'user.id = dept.user_id')
  134. ->where('dept.dept_id', $dept_id);
  135. return $logic->getList($query);
  136. }
  137. /**
  138. * 添加组长
  139. */
  140. public function addLeader($dept_id ,$users)
  141. {
  142. $model = $this->model->findOrEmpty($dept_id);
  143. $leader = new SystemDeptLeader();
  144. foreach ($users as $key => $user) {
  145. $info = $leader->where('user_id', $user['user_id'])->where('dept_id', $dept_id)->findOrEmpty();
  146. if (!$info->isEmpty()) {
  147. unset($users[$key]);
  148. }
  149. }
  150. $model->leader()->saveAll(Arr::getArrayColumn($users, 'user_id'));
  151. }
  152. /**
  153. * 删除组长
  154. */
  155. public function delLeader($id, $ids)
  156. {
  157. $model = $this->model->findOrEmpty($id);
  158. $model->leader()->detach($ids);
  159. }
  160. /**
  161. * 根据部门ID获取游戏ID
  162. */
  163. public function getGameIdsByDeptId($dept_id)
  164. {
  165. $query = $this->model->field('game_list')->where('id', $dept_id);
  166. $data = $query->find();
  167. $game_ids = $data->game_list;
  168. return $game_ids;
  169. }
  170. /**
  171. * 设置部门游戏权限
  172. */
  173. public function setGameListByDeptId($dept_id, $game_list)
  174. {
  175. $result = $this->model->where('id', $dept_id)->update(['game_list' => $game_list]);
  176. return $result;
  177. }
  178. }