SystemDeptLogic.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. // 继承父部门的权限
  36. // $data['game_list'] = $this->getGameListByDeptId(['dept_id' => $data['parent_id']]);
  37. $this->model->save($data);
  38. return $this->model->getKey();
  39. }
  40. /**
  41. * 修改数据
  42. */
  43. public function edit($id, $data): mixed
  44. {
  45. $oldLevel = $data['level'].",".$id;
  46. $data = $this->handleData($data);
  47. if ($data['parent_id'] == $id) {
  48. throw new ApiException('不能设置父级为自身');
  49. }
  50. if (in_array($id, explode(',', $data['level']))) {
  51. throw new ApiException('不能设置父级为下级部门');
  52. }
  53. $newLevel = $data['level'].",".$id;
  54. $deptIds = $this->model->whereRaw('FIND_IN_SET("'.$id.'", level) > 0')->column('id');
  55. $this->model->whereIn('id', $deptIds)->exp('level', "REPLACE(level, '$oldLevel', '$newLevel')")->update();
  56. return $this->model->update($data, ['id' => $id]);
  57. }
  58. /**
  59. * 数据删除
  60. */
  61. public function destroy($ids)
  62. {
  63. $num = $this->model->where('parent_id', 'in', $ids)->count();
  64. if ($num > 0) {
  65. throw new ApiException('该部门下存在子部门,请先删除子部门');
  66. } else {
  67. $count = SystemUser::where('dept_id', 'in', $ids)->count();
  68. if ($count > 0) {
  69. throw new ApiException('该部门下存在用户,请先删除或者转移用户');
  70. }
  71. return $this->model->destroy($ids);
  72. }
  73. }
  74. /**
  75. * 数据处理
  76. */
  77. protected function handleData($data)
  78. {
  79. if (empty($data['parent_id']) || $data['parent_id'] == 0) {
  80. $data['level'] = '0';
  81. $data['parent_id'] = 0;
  82. } else {
  83. $parentMenu = SystemDept::findOrEmpty($data['parent_id']);
  84. $data['level'] = $parentMenu['level'] . ',' . $parentMenu['id'];
  85. }
  86. return $data;
  87. }
  88. /**
  89. * 数据树形化
  90. * @param array $where
  91. * @return array
  92. */
  93. public function tree(array $where = []): array
  94. {
  95. $query = $this->search($where);
  96. if (request()->input('tree', 'false') === 'true') {
  97. $query->field('id, id as value, name as label, parent_id');
  98. }
  99. $query->order('sort', 'desc');
  100. $query->with(['leader' => function($query) {
  101. $query->field('id, username, nickname');
  102. }]);
  103. $data = $this->getAll($query);
  104. return Helper::makeTree($data);
  105. }
  106. /**
  107. * 可操作部门
  108. * @param array $where
  109. * @return array
  110. */
  111. public function accessDept(array $where = []): array
  112. {
  113. $query = $this->search($where);
  114. $query->auth($this->adminInfo['deptList']);
  115. $query->field('id, id as value, name as label, parent_id');
  116. $query->order('sort', 'desc');
  117. $data = $this->getAll($query);
  118. foreach ($data as &$item) {
  119. if ($item['id'] === $this->adminInfo['dept_id']) {
  120. $item['disabled'] = true;
  121. } else {
  122. $item['disabled'] = false;
  123. }
  124. }
  125. return Helper::makeTree($data);
  126. }
  127. /**
  128. * 领导列表
  129. */
  130. public function leaders($where = [])
  131. {
  132. $dept_id = $where['dept_id'];
  133. unset($where['dept_id']);
  134. $logic = new SystemUserLogic();
  135. $query = $logic->search($where)->alias('user')->join('sa_system_dept_leader dept', 'user.id = dept.user_id')
  136. ->where('dept.dept_id', $dept_id);
  137. return $logic->getList($query);
  138. }
  139. /**
  140. * 添加领导
  141. */
  142. public function addLeader($dept_id ,$users)
  143. {
  144. $model = $this->model->findOrEmpty($dept_id);
  145. $leader = new SystemDeptLeader();
  146. foreach ($users as $key => $user) {
  147. $info = $leader->where('user_id', $user['user_id'])->where('dept_id', $dept_id)->findOrEmpty();
  148. if (!$info->isEmpty()) {
  149. unset($users[$key]);
  150. }
  151. }
  152. $model->leader()->saveAll(Arr::getArrayColumn($users, 'user_id'));
  153. }
  154. /**
  155. * 删除领导
  156. */
  157. public function delLeader($id, $ids)
  158. {
  159. $model = $this->model->findOrEmpty($id);
  160. $model->leader()->detach($ids);
  161. }
  162. /**
  163. * 根据部门ID获取游戏ID
  164. */
  165. public function getGameIdsByDeptId($dept_id)
  166. {
  167. $query = $this->model->field('game_list')->where('id', $dept_id);
  168. $data = $query->find();
  169. $game_ids = $data->game_list;
  170. return $game_ids;
  171. }
  172. /**
  173. * 设置部门游戏权限
  174. */
  175. public function setGameListByDeptId($dept_id, $game_list)
  176. {
  177. $result = $this->model->where('id', $dept_id)->update(['game_list' => $game_list]);
  178. if (!$result) {
  179. throw new ApiException('更新部门游戏权限失败');
  180. }
  181. return $result;
  182. }
  183. }