|
@@ -10,6 +10,7 @@ use App\Model\EmployeeMenuPermission;
|
|
|
use App\Model\EmployeeRole;
|
|
|
use App\Model\EmployeeTeamPermission;
|
|
|
use App\Model\Role;
|
|
|
+use App\Model\RoleMenu;
|
|
|
use App\Model\SysMenu;
|
|
|
use App\Model\Team;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
@@ -170,119 +171,243 @@ class EmployeeService extends Service
|
|
|
}
|
|
|
|
|
|
public function roleEdit($data){
|
|
|
- list($status,$msg) = $this->roleRule($data);
|
|
|
+ list($status,$msg) = $this->roleRule($data, false);
|
|
|
if(!$status) return [$status,$msg];
|
|
|
- $first = Role::where('title',$data['title'])->where('id','<>',$data['id'])->where('del_time',0)->first();
|
|
|
- if(!empty($first))return [false,'名称已存在!'];
|
|
|
|
|
|
$model = new Role();
|
|
|
$model = $model->where('id',$data['id'])->first();
|
|
|
-
|
|
|
$model->title = $data['title'];
|
|
|
$model->save();
|
|
|
+
|
|
|
return [true,'保存成功!'];
|
|
|
}
|
|
|
|
|
|
public function roleAdd($data,$user){
|
|
|
list($status,$msg) = $this->roleRule($data);
|
|
|
if(!$status) return [$status,$msg];
|
|
|
- $first = SysMenu::where('title',$data['title'])->where('del_time',0)->first();
|
|
|
- if(!empty($first))return [false,'名称已存在!'];
|
|
|
|
|
|
$model = new Role();
|
|
|
-
|
|
|
$model->title = $data['title'] ;
|
|
|
-
|
|
|
$model->save();
|
|
|
|
|
|
return [true,'保存成功!'];
|
|
|
-
|
|
|
}
|
|
|
|
|
|
public function roleDel($data){
|
|
|
if($this->isEmpty($data,'id')) return [false,'ID必须!'];
|
|
|
|
|
|
+ $bool = EmployeeRole::where('del_time',0)
|
|
|
+ ->whereIn('role_id',$data['id'])
|
|
|
+ ->exists();
|
|
|
+ if($bool) return [false,'角色已绑定人员!'];
|
|
|
+
|
|
|
Role::where('id',$data['id'])->update([
|
|
|
- 'del_time'=>time()
|
|
|
+ 'del_time' => time()
|
|
|
]);
|
|
|
|
|
|
return [true,'删除成功'];
|
|
|
}
|
|
|
|
|
|
public function roleList($data){
|
|
|
- $list = Role::where('del_time',0)->select('title','title','crt_time','id','upd_time')->orderBy('id','desc');
|
|
|
-
|
|
|
+ $list = Role::where('del_time',0)
|
|
|
+ ->select('title','title','crt_time','id','upd_time')
|
|
|
+ ->orderBy('id','desc');
|
|
|
$list = $this->limit($list,'',$data);
|
|
|
|
|
|
return [200,$list];
|
|
|
}
|
|
|
|
|
|
- public function roleRule($data){
|
|
|
- if($this->isEmpty($data,'title')) return [false,'名称不存在!'];
|
|
|
+ public function roleRule($data,$is_check = true){
|
|
|
+ if($this->isEmpty($data,'title')) return [false,'名称不能为空!'];
|
|
|
+
|
|
|
+ if($is_check){
|
|
|
+ $bool = Role::where('title',$data['title'])
|
|
|
+ ->where('del_time',0)
|
|
|
+ ->exists();
|
|
|
+ if($bool) return [false,'角色名称已存在!'];
|
|
|
+ }else{
|
|
|
+ if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
|
|
|
+ $bool = Role::where('title',$data['title'])
|
|
|
+ ->where('id','<>',$data['id'])
|
|
|
+ ->where('del_time',0)
|
|
|
+ ->exists();
|
|
|
+ if($bool) return [false,'角色名称已存在!'];
|
|
|
+ }
|
|
|
|
|
|
return [true,''];
|
|
|
}
|
|
|
|
|
|
- public function departEdit($data){
|
|
|
- list($status,$msg) = $this->departRule($data);
|
|
|
- if(!$status) return [$status,$msg];
|
|
|
- $first = Depart::where('title',$data['title'])->where('id','<>',$data['id'])->where('del_time',0)->first();
|
|
|
- if(!empty($first))return [false,'名称已存在!'];
|
|
|
+ public function roleMenu($data){
|
|
|
+ if(empty($data['role_id'])) return [false,'角色不能为空!'];
|
|
|
+ if(empty($data['menu'])) return [false,'菜单数据不能为空!'];
|
|
|
|
|
|
- $model = new Depart();
|
|
|
- $model = $model->where('id',$data['id'])->first();
|
|
|
+ DB::beginTransaction();
|
|
|
+ try {
|
|
|
+ RoleMenu::where('role_id',$data['role_id'])->update(['del_time' => time()]);
|
|
|
|
|
|
- $model->title = $data['title'];
|
|
|
- $model->code = $data['code']??'';
|
|
|
- $model->save();
|
|
|
- return [true,'保存成功!'];
|
|
|
+ $insert = [];
|
|
|
+ foreach ($data['menu'] as $t){
|
|
|
+ $insert[] = [
|
|
|
+ 'role_id' => $data['role_id'],
|
|
|
+ 'menu_id' => $t['menu_id'],
|
|
|
+ 'type' => $t['type'],
|
|
|
+ 'crt_time' => time()
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ RoleMenu::insert($insert);
|
|
|
|
|
|
+ DB::commit();
|
|
|
+ }catch (\Throwable $exception){
|
|
|
+ DB::rollBack();
|
|
|
+ return [false,$exception->getMessage()];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true,'保存成功!'];
|
|
|
}
|
|
|
|
|
|
- public function departAdd($data,$user){
|
|
|
+ public function roleDetail($data){
|
|
|
+ if(empty($data['role_id'])) return [false,'请选择角色'];
|
|
|
|
|
|
+ $role = Role::where('id',$data['role_id'])
|
|
|
+ ->where('del_time',0)
|
|
|
+ ->select('id','code','title')
|
|
|
+ ->first();
|
|
|
+ if(empty($role)) return [false,'角色不存在或已被删除'];
|
|
|
+ $role = $role->toArray();
|
|
|
|
|
|
-// if($this->isEmpty($data,'title')) return [201,'名称不存在!'];
|
|
|
- list($status,$msg) = $this->departRule($data);
|
|
|
+ $menu = RoleMenu::where('role_id',$data['role_id'])
|
|
|
+ ->where('del_time',0)
|
|
|
+ ->select('menu_id','type')
|
|
|
+ ->get()->toArray();
|
|
|
+ $role['menu'] = $menu;
|
|
|
+
|
|
|
+ return [true, $role];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function departEdit($data){
|
|
|
+ list($status,$msg) = $this->departRule($data,false);
|
|
|
if(!$status) return [$status,$msg];
|
|
|
- $first = Depart::where('title',$data['title'])->where('id','<>',$data['id'])->where('del_time',0)->first();
|
|
|
- if(!empty($first))return [false,'名称已存在!'];
|
|
|
+
|
|
|
+ $update = $msg['data'][0];
|
|
|
|
|
|
$model = new Depart();
|
|
|
+ $model->where('id',$data['id'])->update($update);
|
|
|
|
|
|
- $model->title = $data['title'] ;
|
|
|
- $model->code = $data['code'] ?? '' ;
|
|
|
+ return [true,'保存成功!'];
|
|
|
+ }
|
|
|
|
|
|
- $model->save();
|
|
|
+ public function departAdd($data,$user){
|
|
|
+ list($status,$msg) = $this->departRule($data);
|
|
|
+ if(!$status) return [$status,$msg];
|
|
|
|
|
|
- return [true,'保存成功!'];
|
|
|
+ Depart::insert($msg['data']);
|
|
|
|
|
|
+ return [true,'保存成功!'];
|
|
|
}
|
|
|
|
|
|
public function departDel($data){
|
|
|
- if($this->isEmpty($data,'id')) return [false,'ID必须!'];
|
|
|
+ list($status,$msg) = $this->checkDepartDel($data);
|
|
|
+ if(! $status) return [false, $msg];
|
|
|
|
|
|
- Depart::where('id',$data['id'])->update([
|
|
|
+ Depart::whereIn('id',$data['id'])->update([
|
|
|
'del_time'=>time()
|
|
|
]);
|
|
|
|
|
|
return [true,'删除成功'];
|
|
|
}
|
|
|
|
|
|
- public function departList($data){
|
|
|
- $list = Depart::where('del_time',0)->select('title','crt_time','id','upd_time','code')->orderBy('id','desc');
|
|
|
+ public function checkDepartDel($data){
|
|
|
+ if($this->isEmpty($data,'id')) return [false,'ID必须!'];
|
|
|
|
|
|
- $list = $this->limit($list,'',$data);
|
|
|
+ $bool = Depart::whereIn('parent_id',$data['id'])->where('del_time',0)->exists();
|
|
|
+ if($bool) return [false,'部门下有子部门!'];
|
|
|
+
|
|
|
+ if($this->checkDepartHasPerson($data['id'])) return [false,'部门下有人员档案!'];
|
|
|
+
|
|
|
+ return [true, ''];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function departList($data){
|
|
|
+ $model = Depart::where('del_time',0)
|
|
|
+ ->select('title','id','code','parent_id','is_use')
|
|
|
+ ->orderby('code', 'asc');
|
|
|
+ if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
|
|
|
+ if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
|
|
|
+
|
|
|
+ $list = $model->get()->toArray();
|
|
|
+ if(! empty($list)) {
|
|
|
+ $list = $this->makeTree(0,$list);
|
|
|
+ $list = $this->set_sort_circle($list);
|
|
|
+ }
|
|
|
|
|
|
return [200,$list];
|
|
|
}
|
|
|
|
|
|
- public function departRule($data){
|
|
|
- if($this->isEmpty($data,'title')) return [false,'名称不存在!'];
|
|
|
+ public function departRule($data, $is_check){
|
|
|
+ if($this->isEmpty($data,'data')) return [false,'数据不能为空!'];
|
|
|
+
|
|
|
+ $code = array_column($data['data'],'code');
|
|
|
+ $title = array_column($data['data'],'title');
|
|
|
+ $code = array_map(function($val) {
|
|
|
+ return $val !== null ? $val : 0;
|
|
|
+ }, $code);
|
|
|
+ $title = array_map(function($val) {
|
|
|
+ return $val !== null ? $val : 0;
|
|
|
+ }, $title);
|
|
|
+ $code_count = array_count_values($code);
|
|
|
+ $title_count = array_count_values($title);
|
|
|
+ foreach ($code as $value){
|
|
|
+ if(empty($value)) return [false,'编码不能为空!'];
|
|
|
+ if($code_count[$value] > 1) return [false,'编码不能重复'];
|
|
|
+ }
|
|
|
+ foreach ($title as $value){
|
|
|
+ if(empty($value)) return [false,'名称不能为空!'];
|
|
|
+ if($title_count[$value] > 1) return [false,'名称不能重复'];
|
|
|
+ }
|
|
|
|
|
|
- return [true,''];
|
|
|
+ $depart_id = array_filter(array_column($data['data'],'parent_id'));
|
|
|
+ $res = $this->checkDepartHasPerson($depart_id);
|
|
|
+ if($res) return [false,'部门下已有人员,不能新建子部门!'];
|
|
|
+
|
|
|
+ foreach ($data['data'] as $key => $value){
|
|
|
+ if(empty($value['parent_id'])) $data['data'][$key]['parent_id'] = 0;
|
|
|
+
|
|
|
+ $data['data'][$key]['upd_time'] = time();
|
|
|
+ if($is_check){
|
|
|
+ $data['data'][$key]['crt_time'] = time();
|
|
|
+ $bool = Depart::whereRaw("(binary code = '{$value['code']}' OR title = '{$value['title']}')")
|
|
|
+ ->where('del_time',0)
|
|
|
+ ->exists();
|
|
|
+ }else{
|
|
|
+ if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
|
|
|
+
|
|
|
+ if(! $value['is_use']) {
|
|
|
+ $bool_is = $this->checkDepartHasPerson([$data['id']]);
|
|
|
+ if($bool_is) return [false,'部门下已经有人员,停用失败!'];
|
|
|
+ }
|
|
|
+ $bool = Depart::whereRaw("(binary code = '{$value['code']}' OR title = '{$value['title']}')")
|
|
|
+ ->where('id','<>',$data['id'])
|
|
|
+ ->where('del_time',0)
|
|
|
+ ->exists();
|
|
|
+ }
|
|
|
+ if($bool) return [false,'编码或部门名称不能重复'];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true, $data];
|
|
|
}
|
|
|
|
|
|
+ //检测部门下是否存在人员
|
|
|
+ public function checkDepartHasPerson($depart_id = []){
|
|
|
+ if(empty($depart_id)) return false;
|
|
|
+
|
|
|
+ $bool = EmployeeDepartPermission::from('employee_depart_permission as a')
|
|
|
+ ->leftJoin('employee as b','b.id','a.employee_id')
|
|
|
+ ->where('b.del_time',0)
|
|
|
+ ->whereIn('a.depart_id',$depart_id)
|
|
|
+ ->exists();
|
|
|
+
|
|
|
+ return $bool;
|
|
|
+ }
|
|
|
|
|
|
public function teamEdit($data){
|
|
|
list($status,$msg) = $this->teamRule($data,false);
|
|
@@ -320,8 +445,9 @@ class EmployeeService extends Service
|
|
|
}
|
|
|
|
|
|
public function teamList($data){
|
|
|
- $list = Team::where('del_time',0)->select('title','id','crt_time','upd_time','code')->orderBy('id','desc');
|
|
|
-
|
|
|
+ $list = Team::where('del_time',0)
|
|
|
+ ->select('title','id','crt_time','upd_time','code')
|
|
|
+ ->orderBy('id','desc');
|
|
|
$list = $this->limit($list,'',$data);
|
|
|
|
|
|
return [200,$list];
|
|
@@ -344,6 +470,18 @@ class EmployeeService extends Service
|
|
|
return [true,''];
|
|
|
}
|
|
|
|
|
|
+ public function teamDetail($data){
|
|
|
+ if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
|
|
|
+
|
|
|
+ $result = EmployeeTeamPermission::from('employee_team_permission as a')
|
|
|
+ ->leftJoin('employee as b','b.id','a.employee_id')
|
|
|
+ ->where('team_id',$data['id'])
|
|
|
+ ->select('b.id','b.emp_name','b.number as code')
|
|
|
+ ->get()->toArray();
|
|
|
+
|
|
|
+ return [true,$result];
|
|
|
+ }
|
|
|
+
|
|
|
public function employeeRole($data){
|
|
|
$role_ids = [];
|
|
|
$employee_ids = [];
|
|
@@ -477,6 +615,38 @@ class EmployeeService extends Service
|
|
|
return $role_id;
|
|
|
}
|
|
|
|
|
|
+ //获取登录账号的角色的菜单
|
|
|
+ public static function getMenuByRole($role_id,$user_id){
|
|
|
+ $menu = SysMenu::where('del_time',0)->select('id')->get()->toArray();
|
|
|
+ $object = [];//返回的模型
|
|
|
+ if($user_id == Employee::SPECIAL_ADMIN){
|
|
|
+ //超级管理员
|
|
|
+ foreach ($menu as $value){
|
|
|
+ $object[] = [
|
|
|
+ 'type' => 0,//所有权限
|
|
|
+ 'menu_id' => $value['id'],
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ return $object;
|
|
|
+ }
|
|
|
+
|
|
|
+ //没绑定角色
|
|
|
+ if(empty($role_id)) return [];
|
|
|
+
|
|
|
+ $search = RoleMenu::whereIn('role_id',$role_id)
|
|
|
+ ->where('del_time',0)
|
|
|
+ ->select('menu_id','type')
|
|
|
+ ->get()->toArray();
|
|
|
+ foreach ($search as $value){
|
|
|
+ $object[] = [
|
|
|
+ 'menu_id' => $value['menu_id'],
|
|
|
+ 'type' => $value['type'],
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ return $object;
|
|
|
+ }
|
|
|
+
|
|
|
//获取登录账号的权限部门
|
|
|
public static function getPersonDepart($employee_id){
|
|
|
if(empty($employee_id)) return [];
|