| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- <?php
- // +----------------------------------------------------------------------
- // | saiadmin [ saiadmin快速开发框架 ]
- // +----------------------------------------------------------------------
- // | Author: sai <1430792918@qq.com>
- // +----------------------------------------------------------------------
- namespace plugin\saiadmin\app\logic\system;
- use plugin\saiadmin\basic\BaseLogic;
- use plugin\saiadmin\exception\ApiException;
- use plugin\saiadmin\app\model\system\SystemDept;
- use plugin\saiadmin\app\model\system\SystemUser;
- use plugin\saiadmin\app\model\system\SystemDeptLeader;
- use plugin\saiadmin\utils\Helper;
- use plugin\saiadmin\utils\Arr;
- /**
- * 部门逻辑层
- */
- class SystemDeptLogic extends BaseLogic
- {
- // protected $gameLogic;
- /**
- * 构造函数
- */
- public function __construct()
- {
- $this->model = new SystemDept();
- // $this->gameLogic = new GameLogic();
- }
- /**
- * 添加数据
- */
- public function add($data): mixed
- {
- $data = $this->handleData($data);
- // 继承父部门的权限
- // $data['game_list'] = $this->getGameListByDeptId(['dept_id' => $data['parent_id']]);
- $this->model->save($data);
- return $this->model->getKey();
- }
- /**
- * 修改数据
- */
- public function edit($id, $data): mixed
- {
- $oldLevel = $data['level'].",".$id;
- $data = $this->handleData($data);
- if ($data['parent_id'] == $id) {
- throw new ApiException('不能设置父级为自身');
- }
- if (in_array($id, explode(',', $data['level']))) {
- throw new ApiException('不能设置父级为下级部门');
- }
- $newLevel = $data['level'].",".$id;
- $deptIds = $this->model->whereRaw('FIND_IN_SET("'.$id.'", level) > 0')->column('id');
- $this->model->whereIn('id', $deptIds)->exp('level', "REPLACE(level, '$oldLevel', '$newLevel')")->update();
- return $this->model->update($data, ['id' => $id]);
- }
- /**
- * 数据删除
- */
- public function destroy($ids)
- {
- $num = $this->model->where('parent_id', 'in', $ids)->count();
- if ($num > 0) {
- throw new ApiException('该部门下存在子部门,请先删除子部门');
- } else {
- $count = SystemUser::where('dept_id', 'in', $ids)->count();
- if ($count > 0) {
- throw new ApiException('该部门下存在用户,请先删除或者转移用户');
- }
- return $this->model->destroy($ids);
- }
- }
- /**
- * 数据处理
- */
- protected function handleData($data)
- {
- if (empty($data['parent_id']) || $data['parent_id'] == 0) {
- $data['level'] = '0';
- $data['parent_id'] = 0;
- } else {
- $parentMenu = SystemDept::findOrEmpty($data['parent_id']);
- $data['level'] = $parentMenu['level'] . ',' . $parentMenu['id'];
- }
- return $data;
- }
- /**
- * 数据树形化
- * @param array $where
- * @return array
- */
- public function tree(array $where = []): array
- {
- $query = $this->search($where);
- if (request()->input('tree', 'false') === 'true') {
- $query->field('id, id as value, name as label, parent_id');
- }
- $query->order('sort', 'desc');
- $query->with(['leader' => function($query) {
- $query->field('id, username, nickname');
- }]);
- $data = $this->getAll($query);
- return Helper::makeTree($data);
- }
- /**
- * 可操作部门
- * @param array $where
- * @return array
- */
- public function accessDept(array $where = []): array
- {
- $query = $this->search($where);
- $query->auth($this->adminInfo['deptList']);
- $query->field('id, id as value, name as label, parent_id');
- $query->order('sort', 'desc');
- $data = $this->getAll($query);
- foreach ($data as &$item) {
- if ($item['id'] === $this->adminInfo['dept_id']) {
- $item['disabled'] = true;
- } else {
- $item['disabled'] = false;
- }
- }
- return Helper::makeTree($data);
- }
- /**
- * 领导列表
- */
- public function leaders($where = [])
- {
- $dept_id = $where['dept_id'];
- unset($where['dept_id']);
- $logic = new SystemUserLogic();
- $query = $logic->search($where)->alias('user')->join('sa_system_dept_leader dept', 'user.id = dept.user_id')
- ->where('dept.dept_id', $dept_id);
- return $logic->getList($query);
- }
- /**
- * 添加领导
- */
- public function addLeader($dept_id ,$users)
- {
- $model = $this->model->findOrEmpty($dept_id);
- $leader = new SystemDeptLeader();
- foreach ($users as $key => $user) {
- $info = $leader->where('user_id', $user['user_id'])->where('dept_id', $dept_id)->findOrEmpty();
- if (!$info->isEmpty()) {
- unset($users[$key]);
- }
- }
- $model->leader()->saveAll(Arr::getArrayColumn($users, 'user_id'));
- }
- /**
- * 删除领导
- */
- public function delLeader($id, $ids)
- {
- $model = $this->model->findOrEmpty($id);
- $model->leader()->detach($ids);
- }
- /**
- * 根据部门ID获取游戏ID
- */
- public function getGameIdsByDeptId($dept_id)
- {
- $query = $this->model->field('game_list')->where('id', $dept_id);
- $data = $query->find();
- $game_ids = $data->game_list;
- return $game_ids;
- }
- /**
- * 设置部门游戏权限
- */
- public function setGameListByDeptId($dept_id, $game_list)
- {
- $result = $this->model->where('id', $dept_id)->update(['game_list' => $game_list]);
- if (!$result) {
- throw new ApiException('更新部门游戏权限失败');
- }
- return $result;
- }
- }
|